Refine search
@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 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); } } }
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 ); }
private ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> getMappings(TransportClient client, GetMappingsRequest request) { try { return retry() .maxAttempts(maxAttempts) .exponentialBackoff(maxRetryTime) .run("getMappings", () -> client.admin() .indices() .getMappings(request) .actionGet(requestTimeout.toMillis()) .getMappings()); } catch (Exception e) { throw new RuntimeException(e); } }
/** * 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; }
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[] getIndices(TransportClient client, GetIndexRequest request) { try { return retry() .maxAttempts(maxAttempts) .exponentialBackoff(maxRetryTime) .run("getIndices", () -> client.admin() .indices() .getIndex(request) .actionGet(requestTimeout.toMillis()) .getIndices()); } catch (Exception e) { throw new RuntimeException(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); } }
client.admin().indices() .exists(Requests.indicesExistsRequest(indexKey)).actionGet() .isExists(); if (exists && newIndex) { client.admin().indices().prepareDelete(indexKey).get(); client.admin().indices().create( new CreateIndexRequest(indexKey) .settings( .put("index.number_of_shards", numberOfShards) .put("index.number_of_replicas", numberOfReplicas) )).actionGet(); client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();
@Test public void testCustomInterclusterRequestEvaluator() throws Exception { final Settings settings = Settings.builder() .put(ConfigConstants.SG_INTERCLUSTER_REQUEST_EVALUATOR_CLASS, "com.floragunn.searchguard.AlwaysFalseInterClusterRequestEvaluator") .put("discovery.initial_state_timeout","8s") .build(); setup(Settings.EMPTY, null, settings, false,ClusterConfiguration.DEFAULT ,5,1); Assert.assertEquals(1, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getNumberOfNodes()); Assert.assertEquals(ClusterHealthStatus.GREEN, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getStatus()); }
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();
final WhoAmIResponse whoAmIRes = tc.execute(WhoAmIAction.INSTANCE, new WhoAmIRequest()).actionGet(); System.out.println("Connected as "+whoAmIRes.getDn()); tc.execute(ConfigUpdateAction.INSTANCE, new ConfigUpdateRequest(new String[]{"config","roles","rolesmapping","internalusers","actiongroups"})).actionGet(); final AcknowledgedResponse response = tc.admin().indices().updateSettings((new UpdateSettingsRequest(index).settings(indexSettings))).actionGet(); System.out.println("Reload config on all nodes"); System.out.println("Update number of replicas to "+(updateSettings) +" with result: "+response.isAcknowledged()); tc.execute(ConfigUpdateAction.INSTANCE, new ConfigUpdateRequest(new String[]{"config","roles","rolesmapping","internalusers","actiongroups"})).actionGet(); System.out.println("Reload config on all nodes"); System.exit(0); LicenseInfoResponse res = tc.execute(LicenseInfoAction.INSTANCE, new LicenseInfoRequest()).actionGet(); System.out.println(res.toString()); System.exit(0); .put("index.auto_expand_replicas", replicaAutoExpand?"0-all":"false") .build(); tc.execute(ConfigUpdateAction.INSTANCE, new ConfigUpdateRequest(new String[]{"config","roles","rolesmapping","internalusers","actiongroups"})).actionGet(); final AcknowledgedResponse response = tc.admin().indices().updateSettings((new UpdateSettingsRequest(index).settings(indexSettings))).actionGet(); System.out.println("Reload config on all nodes"); System.out.println("Auto-expand replicas "+(replicaAutoExpand?"enabled":"disabled")); .transientSettings(ENABLE_ALL_ALLOCATIONS_SETTINGS) .persistentSettings(ENABLE_ALL_ALLOCATIONS_SETTINGS)) .actionGet() .isAcknowledged(); chr = tc.admin().cluster().health(chrequest).actionGet();
@SuppressWarnings("resource") @Test public void testNodeClientDisallowedWithNonServerCertificate2() throws Exception { setup(); Assert.assertEquals(clusterInfo.numNodes, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getNumberOfNodes()); Assert.assertEquals(ClusterHealthStatus.GREEN, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getStatus()); final Settings tcSettings = Settings.builder() .put(minimumSearchGuardSettings(Settings.EMPTY).get(0)) .put("cluster.name", clusterInfo.clustername) .put("node.data", false) .put("node.master", false) .put("node.ingest", false) .put("path.home", ".") .put("discovery.initial_state_timeout","8s") .put("searchguard.ssl.transport.keystore_filepath", FileHelper.getAbsoluteFilePathFromClassPath("spock-keystore.jks")) .put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_KEYSTORE_ALIAS,"spock") .build(); log.debug("Start node client"); try (Node node = new PluginAwareNode(false, tcSettings, Netty4Plugin.class, SearchGuardPlugin.class).start()) { Thread.sleep(50); Assert.assertEquals(1, node.client().admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet().getNodes().size()); } }
@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(); }
tc.admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet().getNodes().size()); tc.admin().indices().create(new CreateIndexRequest("searchguard")).actionGet(); } catch (Exception e) { tc.index(ir).actionGet(); .actionGet(); Assert.assertEquals(info.numNodes, cur.getNodes().size()); SearchResponse sr = tc.search(new SearchRequest("searchguard")).actionGet(); sr = tc.search(new SearchRequest("searchguard")).actionGet(); Assert.assertTrue(tc.get(new GetRequest("searchguard", "sg", "config")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","internalusers")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","roles")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","rolesmapping")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","actiongroups")).actionGet().isExists()); Assert.assertFalse(tc.get(new GetRequest("searchguard","sg","rolesmapping_xcvdnghtu165759i99465")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","config")).actionGet().isExists());
@SuppressWarnings("resource") @Test public void testNodeClientDisallowedWithNonServerCertificate() throws Exception { setup(); Assert.assertEquals(clusterInfo.numNodes, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getNumberOfNodes()); Assert.assertEquals(ClusterHealthStatus.GREEN, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getStatus()); final Settings tcSettings = Settings.builder() .put(minimumSearchGuardSettings(Settings.EMPTY).get(0)) .put("cluster.name", clusterInfo.clustername) .put("node.data", false) .put("node.master", false) .put("node.ingest", false) .put("path.home", ".") .put("discovery.initial_state_timeout","8s") .put("searchguard.ssl.transport.keystore_filepath", FileHelper.getAbsoluteFilePathFromClassPath("kirk-keystore.jks")) .put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_KEYSTORE_ALIAS,"kirk") .build(); log.debug("Start node client"); try (Node node = new PluginAwareNode(false, tcSettings, Netty4Plugin.class, SearchGuardPlugin.class).start()) { Thread.sleep(50); Assert.assertEquals(1, node.client().admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet().getNodes().size()); } catch (Exception e) { Assert.fail(e.toString()); } }
@Override public void commit() { if (this.client != null) { this.flushIndex(true); this.client.admin().indices() .refresh(new RefreshRequest(this.indexName)).actionGet(); // enable index auto refresh ImmutableSettings.Builder indexSettings = ImmutableSettings .settingsBuilder(); indexSettings.put("refresh_interval", 1); this.client.admin().indices().prepareUpdateSettings(this.indexName) .setSettings(indexSettings).execute().actionGet(); this.client.admin().indices().prepareOptimize(this.indexName) .setMaxNumSegments(5).execute().actionGet(); } }
IndicesExistsRequestBuilder indicesExistBld = admin.indices().prepareExists( tempMeta.getIndex() ); IndicesExistsResponse indicesExistResponse = indicesExistBld.execute().get(); if ( !indicesExistResponse.isExists() ) { RecoveryRequestBuilder indicesBld = admin.indices().prepareRecoveries( tempMeta.getIndex() ); ActionFuture<RecoveryResponse> lafInd = indicesBld.execute(); String shards = "" + lafInd.get().getSuccessfulShards() + "/" + lafInd.get().getTotalShards(); ClusterStateRequestBuilder clusterBld = admin.cluster().prepareState(); ActionFuture<ClusterStateResponse> lafClu = clusterBld.execute(); ClusterStateResponse cluResp = lafClu.actionGet(); String name = cluResp.getClusterName().value(); ClusterState cluState = cluResp.getState();