use of org.opensearch.client.AdminClient in project OpenSearch by opensearch-project.
the class TemplateUpgradeServiceTests method testUpdateTemplates.
@SuppressWarnings("unchecked")
public void testUpdateTemplates() {
int additionsCount = randomIntBetween(0, 5);
int deletionsCount = randomIntBetween(0, 3);
List<ActionListener<AcknowledgedResponse>> putTemplateListeners = new ArrayList<>();
List<ActionListener<AcknowledgedResponse>> deleteTemplateListeners = new ArrayList<>();
Client mockClient = mock(Client.class);
AdminClient mockAdminClient = mock(AdminClient.class);
IndicesAdminClient mockIndicesAdminClient = mock(IndicesAdminClient.class);
when(mockClient.admin()).thenReturn(mockAdminClient);
when(mockAdminClient.indices()).thenReturn(mockIndicesAdminClient);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
assert args.length == 2;
PutIndexTemplateRequest request = (PutIndexTemplateRequest) args[0];
assertThat(request.name(), equalTo("add_template_" + request.order()));
putTemplateListeners.add((ActionListener) args[1]);
return null;
}).when(mockIndicesAdminClient).putTemplate(any(PutIndexTemplateRequest.class), any(ActionListener.class));
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
assert args.length == 2;
DeleteIndexTemplateRequest request = (DeleteIndexTemplateRequest) args[0];
assertThat(request.name(), startsWith("remove_template_"));
deleteTemplateListeners.add((ActionListener) args[1]);
return null;
}).when(mockIndicesAdminClient).deleteTemplate(any(DeleteIndexTemplateRequest.class), any(ActionListener.class));
Set<String> deletions = new HashSet<>(deletionsCount);
for (int i = 0; i < deletionsCount; i++) {
deletions.add("remove_template_" + i);
}
Map<String, BytesReference> additions = new HashMap<>(additionsCount);
for (int i = 0; i < additionsCount; i++) {
additions.put("add_template_" + i, new BytesArray("{\"index_patterns\" : \"*\", \"order\" : " + i + "}"));
}
final TemplateUpgradeService service = new TemplateUpgradeService(mockClient, clusterService, threadPool, Collections.emptyList());
IllegalStateException ise = expectThrows(IllegalStateException.class, () -> service.upgradeTemplates(additions, deletions));
assertThat(ise.getMessage(), containsString("template upgrade service should always happen in a system context"));
// +2 to skip tryFinishUpgrade
service.upgradesInProgress.set(additionsCount + deletionsCount + 2);
final ThreadContext threadContext = threadPool.getThreadContext();
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
threadContext.markAsSystemContext();
service.upgradeTemplates(additions, deletions);
}
assertThat(putTemplateListeners, hasSize(additionsCount));
assertThat(deleteTemplateListeners, hasSize(deletionsCount));
for (int i = 0; i < additionsCount; i++) {
if (randomBoolean()) {
putTemplateListeners.get(i).onFailure(new RuntimeException("test - ignore"));
} else {
putTemplateListeners.get(i).onResponse(new AcknowledgedResponse(randomBoolean()) {
});
}
}
for (int i = 0; i < deletionsCount; i++) {
if (randomBoolean()) {
int prevUpdatesInProgress = service.upgradesInProgress.get();
deleteTemplateListeners.get(i).onFailure(new RuntimeException("test - ignore"));
assertThat(prevUpdatesInProgress - service.upgradesInProgress.get(), equalTo(1));
} else {
int prevUpdatesInProgress = service.upgradesInProgress.get();
deleteTemplateListeners.get(i).onResponse(new AcknowledgedResponse(randomBoolean()) {
});
assertThat(prevUpdatesInProgress - service.upgradesInProgress.get(), equalTo(1));
}
}
// tryFinishUpgrade was skipped
assertThat(service.upgradesInProgress.get(), equalTo(2));
}
use of org.opensearch.client.AdminClient in project OpenSearch by opensearch-project.
the class TemplateUpgradeServiceTests method testClusterStateUpdate.
@SuppressWarnings("unchecked")
public void testClusterStateUpdate() throws InterruptedException {
final AtomicReference<ActionListener<AcknowledgedResponse>> addedListener = new AtomicReference<>();
final AtomicReference<ActionListener<AcknowledgedResponse>> changedListener = new AtomicReference<>();
final AtomicReference<ActionListener<AcknowledgedResponse>> removedListener = new AtomicReference<>();
final Semaphore updateInvocation = new Semaphore(0);
final Semaphore calculateInvocation = new Semaphore(0);
final Semaphore changedInvocation = new Semaphore(0);
final Semaphore finishInvocation = new Semaphore(0);
Metadata metadata = randomMetadata(IndexTemplateMetadata.builder("user_template").patterns(randomIndexPatterns()).build(), IndexTemplateMetadata.builder("removed_test_template").patterns(randomIndexPatterns()).build(), IndexTemplateMetadata.builder("changed_test_template").patterns(randomIndexPatterns()).build());
Client mockClient = mock(Client.class);
AdminClient mockAdminClient = mock(AdminClient.class);
IndicesAdminClient mockIndicesAdminClient = mock(IndicesAdminClient.class);
when(mockClient.admin()).thenReturn(mockAdminClient);
when(mockAdminClient.indices()).thenReturn(mockIndicesAdminClient);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
assert args.length == 2;
PutIndexTemplateRequest request = (PutIndexTemplateRequest) args[0];
if (request.name().equals("added_test_template")) {
assertThat(addedListener.getAndSet((ActionListener) args[1]), nullValue());
} else if (request.name().equals("changed_test_template")) {
assertThat(changedListener.getAndSet((ActionListener) args[1]), nullValue());
} else {
fail("unexpected put template call for " + request.name());
}
return null;
}).when(mockIndicesAdminClient).putTemplate(any(PutIndexTemplateRequest.class), any(ActionListener.class));
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
assert args.length == 2;
DeleteIndexTemplateRequest request = (DeleteIndexTemplateRequest) args[0];
assertThat(request.name(), startsWith("removed_test_template"));
assertThat(removedListener.getAndSet((ActionListener) args[1]), nullValue());
return null;
}).when(mockIndicesAdminClient).deleteTemplate(any(DeleteIndexTemplateRequest.class), any(ActionListener.class));
new TemplateUpgradeService(mockClient, clusterService, threadPool, Arrays.asList(templates -> {
assertNull(templates.put("added_test_template", IndexTemplateMetadata.builder("added_test_template").patterns(Collections.singletonList("*")).build()));
return templates;
}, templates -> {
assertNotNull(templates.remove("removed_test_template"));
return templates;
}, templates -> {
assertNotNull(templates.put("changed_test_template", IndexTemplateMetadata.builder("changed_test_template").patterns(Collections.singletonList("*")).order(10).build()));
return templates;
})) {
@Override
void tryFinishUpgrade(AtomicBoolean anyUpgradeFailed) {
super.tryFinishUpgrade(anyUpgradeFailed);
finishInvocation.release();
}
@Override
void upgradeTemplates(Map<String, BytesReference> changes, Set<String> deletions) {
super.upgradeTemplates(changes, deletions);
updateInvocation.release();
}
@Override
Optional<Tuple<Map<String, BytesReference>, Set<String>>> calculateTemplateChanges(ImmutableOpenMap<String, IndexTemplateMetadata> templates) {
final Optional<Tuple<Map<String, BytesReference>, Set<String>>> ans = super.calculateTemplateChanges(templates);
calculateInvocation.release();
return ans;
}
@Override
public void clusterChanged(ClusterChangedEvent event) {
super.clusterChanged(event);
changedInvocation.release();
}
};
ClusterState prevState = ClusterState.EMPTY_STATE;
ClusterState state = ClusterState.builder(prevState).nodes(DiscoveryNodes.builder().add(new DiscoveryNode("node1", "node1", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, Version.CURRENT)).localNodeId("node1").masterNodeId("node1").build()).metadata(metadata).build();
setState(clusterService, state);
changedInvocation.acquire();
assertThat(changedInvocation.availablePermits(), equalTo(0));
calculateInvocation.acquire();
assertThat(calculateInvocation.availablePermits(), equalTo(0));
updateInvocation.acquire();
assertThat(updateInvocation.availablePermits(), equalTo(0));
assertThat(finishInvocation.availablePermits(), equalTo(0));
assertThat(addedListener.get(), notNullValue());
assertThat(changedListener.get(), notNullValue());
assertThat(removedListener.get(), notNullValue());
prevState = state;
state = ClusterState.builder(prevState).metadata(Metadata.builder(state.metadata()).removeTemplate("user_template")).build();
setState(clusterService, state);
// Make sure that update wasn't invoked since we are still running
changedInvocation.acquire();
assertThat(changedInvocation.availablePermits(), equalTo(0));
assertThat(calculateInvocation.availablePermits(), equalTo(0));
assertThat(updateInvocation.availablePermits(), equalTo(0));
assertThat(finishInvocation.availablePermits(), equalTo(0));
addedListener.getAndSet(null).onResponse(new AcknowledgedResponse(true) {
});
changedListener.getAndSet(null).onResponse(new AcknowledgedResponse(true) {
});
removedListener.getAndSet(null).onResponse(new AcknowledgedResponse(true) {
});
// 3 upgrades should be completed, in addition to the final calculate
finishInvocation.acquire(3);
assertThat(finishInvocation.availablePermits(), equalTo(0));
calculateInvocation.acquire();
assertThat(calculateInvocation.availablePermits(), equalTo(0));
setState(clusterService, state);
// Make sure that update was called this time since we are no longer running
changedInvocation.acquire();
assertThat(changedInvocation.availablePermits(), equalTo(0));
calculateInvocation.acquire();
assertThat(calculateInvocation.availablePermits(), equalTo(0));
updateInvocation.acquire();
assertThat(updateInvocation.availablePermits(), equalTo(0));
assertThat(finishInvocation.availablePermits(), equalTo(0));
addedListener.getAndSet(null).onFailure(new RuntimeException("test - ignore"));
changedListener.getAndSet(null).onFailure(new RuntimeException("test - ignore"));
removedListener.getAndSet(null).onFailure(new RuntimeException("test - ignore"));
finishInvocation.acquire(3);
assertThat(finishInvocation.availablePermits(), equalTo(0));
calculateInvocation.acquire();
assertThat(calculateInvocation.availablePermits(), equalTo(0));
setState(clusterService, state);
// Make sure that update wasn't called this time since the index template metadata didn't change
changedInvocation.acquire();
assertThat(changedInvocation.availablePermits(), equalTo(0));
assertThat(calculateInvocation.availablePermits(), equalTo(0));
assertThat(updateInvocation.availablePermits(), equalTo(0));
assertThat(finishInvocation.availablePermits(), equalTo(0));
}
use of org.opensearch.client.AdminClient in project OpenSearch by opensearch-project.
the class SnapshotResiliencyTests method testSnapshotPrimaryRelocations.
/**
* Simulates concurrent restarts of data and master nodes as well as relocating a primary shard, while starting and subsequently
* deleting a snapshot.
*/
public void testSnapshotPrimaryRelocations() {
final int masterNodeCount = randomFrom(1, 3, 5);
setupTestCluster(masterNodeCount, randomIntBetween(2, 5));
String repoName = "repo";
String snapshotName = "snapshot";
final String index = "test";
final int shards = randomIntBetween(1, 5);
final TestClusterNodes.TestClusterNode masterNode = testClusterNodes.currentMaster(testClusterNodes.nodes.values().iterator().next().clusterService.state());
final AtomicBoolean createdSnapshot = new AtomicBoolean();
final AdminClient masterAdminClient = masterNode.client.admin();
final StepListener<ClusterStateResponse> clusterStateResponseStepListener = new StepListener<>();
continueOrDie(createRepoAndIndex(repoName, index, shards), createIndexResponse -> client().admin().cluster().state(new ClusterStateRequest(), clusterStateResponseStepListener));
continueOrDie(clusterStateResponseStepListener, clusterStateResponse -> {
final ShardRouting shardToRelocate = clusterStateResponse.getState().routingTable().allShards(index).get(0);
final TestClusterNodes.TestClusterNode currentPrimaryNode = testClusterNodes.nodeById(shardToRelocate.currentNodeId());
final TestClusterNodes.TestClusterNode otherNode = testClusterNodes.randomDataNodeSafe(currentPrimaryNode.node.getName());
scheduleNow(() -> testClusterNodes.stopNode(currentPrimaryNode));
scheduleNow(new Runnable() {
@Override
public void run() {
final StepListener<ClusterStateResponse> updatedClusterStateResponseStepListener = new StepListener<>();
masterAdminClient.cluster().state(new ClusterStateRequest(), updatedClusterStateResponseStepListener);
continueOrDie(updatedClusterStateResponseStepListener, updatedClusterState -> {
final ShardRouting shardRouting = updatedClusterState.getState().routingTable().shardRoutingTable(shardToRelocate.shardId()).primaryShard();
if (shardRouting.unassigned() && shardRouting.unassignedInfo().getReason() == UnassignedInfo.Reason.NODE_LEFT) {
if (masterNodeCount > 1) {
scheduleNow(() -> testClusterNodes.stopNode(masterNode));
}
testClusterNodes.randomDataNodeSafe().client.admin().cluster().prepareCreateSnapshot(repoName, snapshotName).execute(ActionListener.wrap(() -> {
createdSnapshot.set(true);
testClusterNodes.randomDataNodeSafe().client.admin().cluster().deleteSnapshot(new DeleteSnapshotRequest(repoName, snapshotName), noopListener());
}));
scheduleNow(() -> testClusterNodes.randomMasterNodeSafe().client.admin().cluster().reroute(new ClusterRerouteRequest().add(new AllocateEmptyPrimaryAllocationCommand(index, shardRouting.shardId().id(), otherNode.node.getName(), true)), noopListener()));
} else {
scheduleSoon(this);
}
});
}
});
});
runUntil(() -> testClusterNodes.randomMasterNode().map(master -> {
if (createdSnapshot.get() == false) {
return false;
}
return master.clusterService.state().custom(SnapshotsInProgress.TYPE, SnapshotsInProgress.EMPTY).entries().isEmpty();
}).orElse(false), TimeUnit.MINUTES.toMillis(1L));
clearDisruptionsAndAwaitSync();
assertTrue(createdSnapshot.get());
assertThat(testClusterNodes.randomDataNodeSafe().clusterService.state().custom(SnapshotsInProgress.TYPE, SnapshotsInProgress.EMPTY).entries(), empty());
final Repository repository = testClusterNodes.randomMasterNodeSafe().repositoriesService.repository(repoName);
Collection<SnapshotId> snapshotIds = getRepositoryData(repository).getSnapshotIds();
assertThat(snapshotIds, either(hasSize(1)).or(hasSize(0)));
}
use of org.opensearch.client.AdminClient in project OpenSearch by opensearch-project.
the class MappingUpdatedActionTests method testSendUpdateMappingUsingAutoPutMappingAction.
public void testSendUpdateMappingUsingAutoPutMappingAction() {
DiscoveryNodes nodes = DiscoveryNodes.builder().add(new DiscoveryNode("first", buildNewFakeTransportAddress(), LegacyESVersion.V_7_9_0)).build();
ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).nodes(nodes).build();
ClusterService clusterService = mock(ClusterService.class);
when(clusterService.state()).thenReturn(clusterState);
IndicesAdminClient indicesAdminClient = mock(IndicesAdminClient.class);
AdminClient adminClient = mock(AdminClient.class);
when(adminClient.indices()).thenReturn(indicesAdminClient);
Client client = mock(Client.class);
when(client.admin()).thenReturn(adminClient);
MappingUpdatedAction mua = new MappingUpdatedAction(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), clusterService);
mua.setClient(client);
Settings indexSettings = Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).build();
final Mapper.BuilderContext context = new Mapper.BuilderContext(indexSettings, new ContentPath());
RootObjectMapper rootObjectMapper = new RootObjectMapper.Builder("name").build(context);
Mapping update = new Mapping(LegacyESVersion.V_7_9_0, rootObjectMapper, new MetadataFieldMapper[0], Map.of());
mua.sendUpdateMapping(new Index("name", "uuid"), update, ActionListener.wrap(() -> {
}));
verify(indicesAdminClient).execute(eq(AutoPutMappingAction.INSTANCE), any(), any());
}
use of org.opensearch.client.AdminClient in project OpenSearch by opensearch-project.
the class MappingUpdatedActionTests method testSendUpdateMappingUsingPutMappingAction.
public void testSendUpdateMappingUsingPutMappingAction() {
DiscoveryNodes nodes = DiscoveryNodes.builder().add(new DiscoveryNode("first", buildNewFakeTransportAddress(), LegacyESVersion.V_7_8_0)).build();
ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).nodes(nodes).build();
ClusterService clusterService = mock(ClusterService.class);
when(clusterService.state()).thenReturn(clusterState);
IndicesAdminClient indicesAdminClient = mock(IndicesAdminClient.class);
AdminClient adminClient = mock(AdminClient.class);
when(adminClient.indices()).thenReturn(indicesAdminClient);
Client client = mock(Client.class);
when(client.admin()).thenReturn(adminClient);
MappingUpdatedAction mua = new MappingUpdatedAction(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), clusterService);
mua.setClient(client);
Settings indexSettings = Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).build();
final Mapper.BuilderContext context = new Mapper.BuilderContext(indexSettings, new ContentPath());
RootObjectMapper rootObjectMapper = new RootObjectMapper.Builder("name").build(context);
Mapping update = new Mapping(LegacyESVersion.V_7_8_0, rootObjectMapper, new MetadataFieldMapper[0], Map.of());
mua.sendUpdateMapping(new Index("name", "uuid"), update, ActionListener.wrap(() -> {
}));
verify(indicesAdminClient).putMapping(any(), any());
}
Aggregations