@Override public boolean getAvailable() { return !elasticsearchProperties.isElasticsearchDisabled(); }
private String getTtl() { DateTime now = DateTime.now(); DateTime expireTime = now.plus(elasticsearchProperties.getAgentExpiry()); return String.valueOf((expireTime.getMillis() - now.getMillis()) / 1000) + "s"; }
private Client getElasticClient() throws UnknownHostException { if (this.elasticClient == null) { Settings settings = Settings.settingsBuilder() .put("cluster.name", elasticsearchProperties.getElasticsearchClusterName()) .put("client.transport.ping_timeout", elasticsearchProperties.getElasticsearchTransportPingTimeout()) .build(); this.elasticClient = TransportClient.builder() .settings(settings) .build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(elasticsearchProperties.getElasticsearchHost()), elasticsearchProperties.getElasticsearchPort())); } return this.elasticClient; }
@Override public boolean canConnect() { try { initialize(); return isIndexAvailable(); } catch (Exception e) { logger.warn("Failed to verify an ability to connect to index \"{}\" in cluster \"{}\" in Elasticsearch at {}:{} due to error: ", policyIndexName, elasticsearchProperties.getElasticsearchClusterName(), elasticsearchProperties.getElasticsearchHost(), elasticsearchProperties.getElasticsearchPort(), e); } return false; }
private void ensureIndexAvailable() throws UnknownHostException { int availabilityAttempt = 0; try { while ((++availabilityAttempt <= elasticsearchProperties.getElasticsearchMaxIndexAvailabilityAttempts()) && !isIndexAvailable()) { Thread.sleep(elasticsearchProperties.getElasticsearchIndexAvailabilityDelay().toStandardDuration().getMillis()); } } catch (InterruptedException eInterrupted) { logger.warn("Interrupted while waiting to test the availability of the index \"{}\".", policyIndexName); } }
private Collection<AgentResult> getAgents(PercolateResponse response) throws UnknownHostException { Map<String, AgentResult> indexedAgents = new HashMap<>(); BoolQueryBuilder agentsQueryBuilder = QueryBuilders.boolQuery(); for (PercolateResponse.Match match : response) { String agentId = match.getId().string(); agentsQueryBuilder = agentsQueryBuilder.should(termQuery("_id", agentId)); indexedAgents.put(agentId, new AgentResult(agentId, match, null)); } SearchHits agentHits = getElasticClient() .prepareSearch(policyIndexName) .setTypes(percolatorTypeName) .setQuery(agentsQueryBuilder) .setSize(elasticsearchProperties.getElasticsearchMaxStoredqueries()) .get(elasticsearchProperties.getElasticsearchSearchTimeout()) .getHits(); for (SearchHit agentHit : agentHits) { AgentResult agentResult = indexedAgents.get(agentHit.getId()); if (agentResult != null) { agentResult.setSource(agentHit.sourceAsMap()); } } if (indexedAgents.values().stream().anyMatch(r -> r.getSource() == null)) { String error = "Failed to retrieve all the expected boolean agents from Elasticsearch"; logger.error(error); throw new RuntimeException(error); } return indexedAgents.values(); }
private boolean canGetSettings() throws UnknownHostException { GetSettingsResponse indicesResponse = getElasticClient() .admin() .indices() .prepareGetSettings(policyIndexName) .setMasterNodeTimeout(elasticsearchProperties.getElasticsearchMasterNodeTimeout()) .get(); if (indicesResponse.getIndexToSettings().isEmpty()) { logger.warn("Failed to retrieve settings for the configured Elasticsearch index {}", policyIndexName); return false; } return true; }
private boolean isClusterHealthy() throws UnknownHostException { ClusterHealthResponse healthResponse = getElasticClient() .admin() .cluster() .prepareHealth(policyIndexName) .setWaitForYellowStatus() .setTimeout(TimeValue.timeValueSeconds(elasticsearchProperties.getElasticsearchIndexStatusTimeout())) .get(); if (healthResponse.isTimedOut()) { logger.warn("Timed out while awaiting at least YELLOW status for Elasticsearch index {} ", policyIndexName); return false; } if (!healthResponse.getStatus().equals(ClusterHealthStatus.YELLOW) && !healthResponse.getStatus().equals(ClusterHealthStatus.GREEN)) { logger.warn("The Elasticsearch index {} is reporting status {}", policyIndexName, healthResponse.getStatus().name()); return false; } return true; }
@Override public HealthResult healthCheck() { HealthStatus status; if (applicationContext.getBean(ElasticsearchProperties.class).isElasticsearchDisabled()) { status = HealthStatus.HEALTHY; } else { BooleanAgentServices booleanAgentServices = applicationContext.getBean(BooleanAgentServices.class); if (booleanAgentServices.canConnect()) { status = HealthStatus.HEALTHY; } else { status = HealthStatus.UNHEALTHY; } } return new HealthResult(status); }
< DateTime.now().getMillis() - elasticsearchProperties.getAgentExpiry().toStandardDuration().getMillis();
if(elasticsearchProperties.isElasticsearchDisabled()){ configurableEnvironment.addActiveProfile("noelastic");