use of org.opensearch.client.Client in project OpenSearch by opensearch-project.
the class ClusterShardLimitIT method testRestoreSnapshotOverLimit.
public void testRestoreSnapshotOverLimit() {
Client client = client();
logger.info("--> creating repository");
Settings.Builder repoSettings = Settings.builder();
repoSettings.put("location", randomRepoPath());
repoSettings.put("compress", randomBoolean());
repoSettings.put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES);
assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(repoSettings.build()));
int dataNodes = client().admin().cluster().prepareState().get().getState().getNodes().getDataNodes().size();
ShardCounts counts = ShardCounts.forDataNodeCount(dataNodes);
createIndex("snapshot-index", Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, counts.getFailingIndexShards()).put(SETTING_NUMBER_OF_REPLICAS, counts.getFailingIndexReplicas()).build());
ensureGreen();
logger.info("--> snapshot");
CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("snapshot-index").get();
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
List<SnapshotInfo> snapshotInfos = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots();
assertThat(snapshotInfos.size(), equalTo(1));
SnapshotInfo snapshotInfo = snapshotInfos.get(0);
assertThat(snapshotInfo.state(), equalTo(SnapshotState.SUCCESS));
assertThat(snapshotInfo.version(), equalTo(Version.CURRENT));
// Test restore after index deletion
logger.info("--> delete indices");
cluster().wipeIndices("snapshot-index");
// Reduce the shard limit and fill it up
setShardsPerNode(counts.getShardsPerNode());
createIndex("test-fill", Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, counts.getFirstIndexShards()).put(SETTING_NUMBER_OF_REPLICAS, counts.getFirstIndexReplicas()).build());
logger.info("--> restore one index after deletion");
try {
RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("snapshot-index").execute().actionGet();
fail("Should not have been able to restore snapshot in full cluster");
} catch (IllegalArgumentException e) {
verifyException(dataNodes, counts, e);
}
ensureGreen();
ClusterState clusterState = client.admin().cluster().prepareState().get().getState();
assertFalse(clusterState.getMetadata().hasIndex("snapshot-index"));
}
use of org.opensearch.client.Client in project OpenSearch by opensearch-project.
the class ClusterShardLimitIT method testOpenIndexOverLimit.
public void testOpenIndexOverLimit() {
Client client = client();
int dataNodes = client().admin().cluster().prepareState().get().getState().getNodes().getDataNodes().size();
ShardCounts counts = ShardCounts.forDataNodeCount(dataNodes);
createIndex("test-index-1", Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, counts.getFailingIndexShards()).put(SETTING_NUMBER_OF_REPLICAS, counts.getFailingIndexReplicas()).build());
ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
assertFalse(healthResponse.isTimedOut());
AcknowledgedResponse closeIndexResponse = client.admin().indices().prepareClose("test-index-1").execute().actionGet();
assertTrue(closeIndexResponse.isAcknowledged());
// Fill up the cluster
setShardsPerNode(counts.getShardsPerNode());
createIndex("test-fill", Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, counts.getFirstIndexShards()).put(SETTING_NUMBER_OF_REPLICAS, counts.getFirstIndexReplicas()).build());
try {
client.admin().indices().prepareOpen("test-index-1").execute().actionGet();
fail("should not have been able to open index");
} catch (IllegalArgumentException e) {
verifyException(dataNodes, counts, e);
}
ClusterState clusterState = client.admin().cluster().prepareState().get().getState();
assertFalse(clusterState.getMetadata().hasIndex("snapshot-index"));
}
use of org.opensearch.client.Client in project OpenSearch by opensearch-project.
the class GatewayIndexStateIT method testIndexDeletionWhenNodeRejoins.
/**
* This test ensures that when an index deletion takes place while a node is offline, when that
* node rejoins the cluster, it deletes the index locally instead of importing it as a dangling index.
*/
public void testIndexDeletionWhenNodeRejoins() throws Exception {
final String indexName = "test-index-del-on-node-rejoin-idx";
final int numNodes = 2;
final List<String> nodes;
logger.info("--> starting a cluster with " + numNodes + " nodes");
nodes = internalCluster().startNodes(numNodes, Settings.builder().put(IndexGraveyard.SETTING_MAX_TOMBSTONES.getKey(), randomIntBetween(10, 100)).build());
logger.info("--> create an index");
createIndex(indexName);
logger.info("--> waiting for green status");
ensureGreen();
final String indexUUID = resolveIndex(indexName).getUUID();
logger.info("--> restart a random date node, deleting the index in between stopping and restarting");
internalCluster().restartRandomDataNode(new RestartCallback() {
@Override
public Settings onNodeStopped(final String nodeName) throws Exception {
nodes.remove(nodeName);
logger.info("--> stopped node[{}], remaining nodes {}", nodeName, nodes);
assert nodes.size() > 0;
final String otherNode = nodes.get(0);
logger.info("--> delete index and verify it is deleted");
final Client client = client(otherNode);
client.admin().indices().prepareDelete(indexName).execute().actionGet();
assertFalse(client.admin().indices().prepareExists(indexName).execute().actionGet().isExists());
logger.info("--> index deleted");
return super.onNodeStopped(nodeName);
}
});
logger.info("--> wait until all nodes are back online");
client().admin().cluster().health(Requests.clusterHealthRequest().waitForEvents(Priority.LANGUID).waitForNodes(Integer.toString(numNodes))).actionGet();
logger.info("--> waiting for green status");
ensureGreen();
logger.info("--> verify that the deleted index is removed from the cluster and not reimported as dangling by the restarted node");
assertFalse(client().admin().indices().prepareExists(indexName).execute().actionGet().isExists());
assertBusy(() -> {
final NodeEnvironment nodeEnv = internalCluster().getInstance(NodeEnvironment.class);
try {
assertFalse("index folder " + indexUUID + " should be deleted", nodeEnv.availableIndexFolders().contains(indexUUID));
} catch (IOException e) {
logger.error("Unable to retrieve available index folders from the node", e);
fail("Unable to retrieve available index folders from the node");
}
});
}
use of org.opensearch.client.Client in project OpenSearch by opensearch-project.
the class SimpleClusterStateIT method testLargeClusterStatePublishing.
public void testLargeClusterStatePublishing() throws Exception {
int estimatedBytesSize = scaledRandomIntBetween(ByteSizeValue.parseBytesSizeValue("10k", "estimatedBytesSize").bytesAsInt(), ByteSizeValue.parseBytesSizeValue("256k", "estimatedBytesSize").bytesAsInt());
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties");
int counter = 0;
int numberOfFields = 0;
while (true) {
mapping.startObject(UUIDs.randomBase64UUID()).field("type", "text").endObject();
// each field is about 10 bytes, assuming compression in place
counter += 10;
numberOfFields++;
if (counter > estimatedBytesSize) {
break;
}
}
logger.info("number of fields [{}], estimated bytes [{}]", numberOfFields, estimatedBytesSize);
mapping.endObject().endObject().endObject();
int numberOfShards = scaledRandomIntBetween(1, cluster().numDataNodes());
// if the create index is ack'ed, then all nodes have successfully processed the cluster state
assertAcked(client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numberOfShards).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), Long.MAX_VALUE)).addMapping("type", mapping).setTimeout("60s").get());
// wait for green state, so its both green, and there are no more pending events
ensureGreen();
MappingMetadata masterMappingMetadata = client().admin().indices().prepareGetMappings("test").get().getMappings().get("test");
for (Client client : clients()) {
MappingMetadata mappingMetadata = client.admin().indices().prepareGetMappings("test").setLocal(true).get().getMappings().get("test");
assertThat(mappingMetadata.source().string(), equalTo(masterMappingMetadata.source().string()));
assertThat(mappingMetadata, equalTo(masterMappingMetadata));
}
}
use of org.opensearch.client.Client in project OpenSearch by opensearch-project.
the class TransportSearchAction method collectSearchShards.
static void collectSearchShards(IndicesOptions indicesOptions, String preference, String routing, AtomicInteger skippedClusters, Map<String, OriginalIndices> remoteIndicesByCluster, RemoteClusterService remoteClusterService, ThreadPool threadPool, ActionListener<Map<String, ClusterSearchShardsResponse>> listener) {
final CountDown responsesCountDown = new CountDown(remoteIndicesByCluster.size());
final Map<String, ClusterSearchShardsResponse> searchShardsResponses = new ConcurrentHashMap<>();
final AtomicReference<Exception> exceptions = new AtomicReference<>();
for (Map.Entry<String, OriginalIndices> entry : remoteIndicesByCluster.entrySet()) {
final String clusterAlias = entry.getKey();
boolean skipUnavailable = remoteClusterService.isSkipUnavailable(clusterAlias);
Client clusterClient = remoteClusterService.getRemoteClusterClient(threadPool, clusterAlias);
final String[] indices = entry.getValue().indices();
ClusterSearchShardsRequest searchShardsRequest = new ClusterSearchShardsRequest(indices).indicesOptions(indicesOptions).local(true).preference(preference).routing(routing);
clusterClient.admin().cluster().searchShards(searchShardsRequest, new CCSActionListener<ClusterSearchShardsResponse, Map<String, ClusterSearchShardsResponse>>(clusterAlias, skipUnavailable, responsesCountDown, skippedClusters, exceptions, listener) {
@Override
void innerOnResponse(ClusterSearchShardsResponse clusterSearchShardsResponse) {
searchShardsResponses.put(clusterAlias, clusterSearchShardsResponse);
}
@Override
Map<String, ClusterSearchShardsResponse> createFinalResponse() {
return searchShardsResponses;
}
});
}
}
Aggregations