@Override public void clearStorage() throws BackendException { try { try { client.admin().indices() .delete(new DeleteIndexRequest(indexName)).actionGet(); // We wait for one second to let ES delete the river Thread.sleep(1000); } catch (IndexMissingException e) { // Index does not exist... Fine } } catch (Exception e) { throw new PermanentBackendException("Could not delete index " + indexName, e); } finally { close(); } }
private void updateLogIndexName() { this.logIndexName = this.logIndexPrefix + "_" + SIMPLE_DATE_FORMAT.format(new Date()); try { elasticSearchClient.admin() .indices() .prepareGetIndex() .addIndices(logIndexName) .execute() .actionGet(); } catch (IndexNotFoundException infe) { try { elasticSearchClient.admin() .indices() .prepareCreate(logIndexName) .execute() .actionGet(); } catch (ResourceAlreadyExistsException ilee) { // no-op } catch (Exception e) { logger.error("Failed to update log index name: {}", logIndexName, e); } } }
/** * If ES already contains this instance's target index, then do nothing. * Otherwise, create the index, then wait {@link #CREATE_SLEEP}. * <p> * The {@code client} field must point to a live, connected client. * The {@code indexName} field must be non-null and point to the name * of the index to check for existence or create. * * @param config the config for this ElasticSearchIndex * @throws java.lang.IllegalArgumentException if the index could not be created */ private void checkForOrCreateIndex(Configuration config) { Preconditions.checkState(null != client); //Create index if it does not already exist IndicesExistsResponse response = client.admin().indices().exists(new IndicesExistsRequest(indexName)).actionGet(); if (!response.isExists()) { ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder(); ElasticSearchSetup.applySettingsFromTitanConf(settings, config, ES_CREATE_EXTRAS_NS); CreateIndexResponse create = client.admin().indices().prepareCreate(indexName) .setSettings(settings.build()).execute().actionGet(); try { final long sleep = config.get(CREATE_SLEEP); log.debug("Sleeping {} ms after {} index creation returned from actionGet()", sleep, indexName); Thread.sleep(sleep); } catch (InterruptedException e) { throw new TitanException("Interrupted while waiting for index to settle in", e); } if (!create.isAcknowledged()) throw new IllegalArgumentException("Could not create index: " + indexName); } }
private static void issueWarnings(Client tc) { NodesInfoResponse nir = tc.admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet(); Version maxVersion = nir.getNodes().stream().max((n1,n2) -> n1.getVersion().compareTo(n2.getVersion())).get().getVersion(); Version minVersion = nir.getNodes().stream().min((n1,n2) -> n1.getVersion().compareTo(n2.getVersion())).get().getVersion(); if(!maxVersion.equals(minVersion)) { System.out.println("WARNING: Your cluster consists of different node versions. It is not recommended to run sgadmin against a mixed cluster. This may fail."); System.out.println(" Minimum node version is "+minVersion.toString()); System.out.println(" Maximum node version is "+maxVersion.toString()); } else { System.out.println("Elasticsearch Version: "+minVersion.toString()); } if(nir.getNodes().size() > 0) { List<PluginInfo> pluginInfos = nir.getNodes().get(0).getPlugins().getPluginInfos(); String sgVersion = pluginInfos.stream().filter(p->p.getClassname().equals("com.floragunn.searchguard.SearchGuardPlugin")).map(p->p.getVersion()).findFirst().orElse("<unknown>"); System.out.println("Search Guard Version: "+sgVersion); } } }
public boolean wait_ready(long maxtimemillis, ClusterHealthStatus status) { // wait for yellow status long start = System.currentTimeMillis(); boolean is_ready; do { // wait for yellow status ClusterHealthResponse health = elasticsearchClient.admin().cluster().prepareHealth().setWaitForStatus(status).execute().actionGet(); is_ready = !health.isTimedOut(); if (!is_ready && System.currentTimeMillis() - start > maxtimemillis) return false; } while (!is_ready); return is_ready; }
semaphore.acquire(); logger.trace("dropRecreateMapping index[{}] - type[{}]", index, type); client.admin().indices().prepareRefresh(index).get(); ImmutableOpenMap<String, MappingMetaData> mappings = client.admin().cluster().prepareState().get().getState().getMetaData() .index(index).mappings(); logger.trace("mappings contains type {}: {}", type, mappings.containsKey(type)); if (client.admin().indices().prepareDeleteMapping(index).setType(type).get().isAcknowledged()) { PutMappingResponse pmr = client.admin().indices().preparePutMapping(index).setType(type) .setSource(mapping.getSourceAsMap()).get(); if (!pmr.isAcknowledged()) {
private void addIndex(String indexName) { try { elasticSearchClient.admin() .indices() .prepareGetIndex() .addIndices(indexName) .execute() .actionGet(); } catch (IndexNotFoundException infe) { try { elasticSearchClient.admin() .indices() .prepareCreate(indexName) .execute() .actionGet(); } catch (ResourceAlreadyExistsException done) { // no-op } } }
final WhoAmIResponse whoAmIRes = tc.execute(WhoAmIAction.INSTANCE, new WhoAmIRequest()).actionGet(); sb.append(Strings.toString(whoAmIRes,true, true)); } catch (Exception e1) { LicenseInfoResponse res = tc.execute(LicenseInfoAction.INSTANCE, new LicenseInfoRequest()).actionGet(); sb.append(Strings.toString(res,true, true)); } catch (Exception e1) { ClusterHealthResponse nir = tc.admin().cluster().health(new ClusterHealthRequest()).actionGet(); sb.append(Strings.toString(nir,true, true)); } catch (Exception e1) { NodesInfoResponse nir = tc.admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet(); sb.append(Strings.toString(nir,true, true)); } catch (Exception e1) { NodesStatsResponse nir = tc.admin().cluster().nodesStats(new NodesStatsRequest()).actionGet(); sb.append(Strings.toString(nir,true, true)); } catch (Exception e1) { PendingClusterTasksResponse nir = tc.admin().cluster().pendingClusterTasks(new PendingClusterTasksRequest()).actionGet(); sb.append(Strings.toString(nir,true, true)); } catch (Exception e1) { IndicesStatsResponse nir = tc.admin().indices().stats(new IndicesStatsRequest()).actionGet(); sb.append(Strings.toString(nir, true, true)); } catch (Exception e1) {
private String[] getIndexesFromEs(final String aliasName){ final AdminClient adminClient = this.provider.getClient().admin(); //remove write alias, can only have one ImmutableOpenMap<String, List<AliasMetaData>> aliasMap = adminClient.indices().getAliases( new GetAliasesRequest( aliasName ) ).actionGet().getAliases(); return aliasMap.keys().toArray( String.class ); }
/** * Check health of this specific index. */ @Override public Health getIndexHealth() { try { String[] indexNames = this.getIndexes(); final ActionFuture<ClusterHealthResponse> future = esProvider.getClient().admin().cluster().health( new ClusterHealthRequest( indexNames ) ); //only wait 2 seconds max ClusterHealthResponse chr = future.actionGet(2000); return Health.valueOf( chr.getStatus().name() ); } catch ( Exception ex ) { logger.error( "Error connecting to ElasticSearch", ex.getMessage() ); } // this is bad, red alert! return Health.RED; }
public ElasticSearchIndex(Configuration config) { indexName = config.get(INDEX_NAME); useDeprecatedIgnoreUnmapped = config.get(USE_EDEPRECATED_IGNORE_UNMAPPED_OPTION); checkExpectedClientVersion(); final ElasticSearchSetup.Connection c; if (!config.has(INTERFACE)) { c = legacyConfiguration(config); } else { c = interfaceConfiguration(config); } node = c.getNode(); client = c.getClient(); maxResultsSize = config.get(INDEX_MAX_RESULT_SET_SIZE); log.debug("Configured ES query result set max size to {}", maxResultsSize); client.admin().cluster().prepareHealth().setTimeout(config.get(HEALTH_REQUEST_TIMEOUT)) .setWaitForYellowStatus().execute().actionGet(); checkForOrCreateIndex(config); }
private void deleteAllIndices() { ImmutableOpenMap<String, IndexMetaData> indices = elasticSearchClient.admin().cluster() .prepareState().get().getState() .getMetaData().getIndices(); indices.forEach(cursor -> { try { elasticSearchClient.admin() .indices() .delete(new DeleteIndexRequest(cursor.value.getIndex().getName())) .get(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); } }); }
public void setMapping(String indexName, File json) { try { this.elasticsearchClient.admin().indices().preparePutMapping(indexName) .setSource(new String(Files.readAllBytes(json.toPath()), StandardCharsets.UTF_8)) .setUpdateAllTypes(true) .setType("_default_") .execute() .actionGet(); } catch (Throwable e) { DAO.severe(e); }; }
client.admin().indices() .exists(Requests.indicesExistsRequest(indexKey)).actionGet() .isExists(); if (exists && newdb) { client.admin().indices().prepareDelete(indexKey).execute().actionGet(); client.admin().indices().create( new CreateIndexRequest(indexKey) .settings( .put("index.number_of_replicas", numberOfReplicas) .put("index.mapping._id.indexed", true) )).actionGet(); client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();
@Test public void testIndexCreationOptions() throws InterruptedException, BackendException { final int shards = 77; ElasticsearchRunner esr = new ElasticsearchRunner(".", "indexCreationOptions.yml"); esr.start(); CommonsConfiguration cc = new CommonsConfiguration(new BaseConfiguration()); cc.set("index." + INDEX_NAME + ".elasticsearch.create.ext.number_of_shards", String.valueOf(shards)); cc.set("index." + INDEX_NAME + ".elasticsearch.ext.cluster.name", "indexCreationOptions"); ModifiableConfiguration config = new ModifiableConfiguration(GraphDatabaseConfiguration.ROOT_NS, cc, BasicConfiguration.Restriction.NONE); config.set(INTERFACE, ElasticSearchSetup.NODE.toString(), INDEX_NAME); Configuration indexConfig = config.restrictTo(INDEX_NAME); IndexProvider idx = new ElasticSearchIndex(indexConfig); simpleWriteAndQuery(idx); ImmutableSettings.Builder settingsBuilder = ImmutableSettings.settingsBuilder(); settingsBuilder.put("discovery.zen.ping.multicast.enabled", "false"); settingsBuilder.put("discovery.zen.ping.unicast.hosts", "localhost,127.0.0.1:9300"); settingsBuilder.put("cluster.name", "indexCreationOptions"); NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().settings(settingsBuilder.build()); nodeBuilder.client(true).data(false).local(false); Node n = nodeBuilder.build().start(); GetSettingsResponse response = n.client().admin().indices().getSettings(new GetSettingsRequest().indices("titan")).actionGet(); assertEquals(String.valueOf(shards), response.getSetting("titan", "index.number_of_shards")); idx.close(); n.stop(); esr.stop(); }