use of org.opensearch.cluster.service.ClusterService in project OpenSearch by opensearch-project.
the class ZenDiscoveryIT method testHandleNodeJoin_incompatibleClusterState.
public void testHandleNodeJoin_incompatibleClusterState() throws InterruptedException, ExecutionException, TimeoutException {
String masterNode = internalCluster().startMasterOnlyNode();
String node1 = internalCluster().startNode();
ClusterService clusterService = internalCluster().getInstance(ClusterService.class, node1);
Coordinator coordinator = (Coordinator) internalCluster().getInstance(Discovery.class, masterNode);
final ClusterState state = clusterService.state();
Metadata.Builder mdBuilder = Metadata.builder(state.metadata());
mdBuilder.putCustom(CustomMetadata.TYPE, new CustomMetadata("data"));
ClusterState stateWithCustomMetadata = ClusterState.builder(state).metadata(mdBuilder).build();
final CompletableFuture<Throwable> future = new CompletableFuture<>();
DiscoveryNode node = state.nodes().getLocalNode();
coordinator.sendValidateJoinRequest(stateWithCustomMetadata, new JoinRequest(node, 0L, Optional.empty()), new JoinHelper.JoinCallback() {
@Override
public void onSuccess() {
future.completeExceptionally(new AssertionError("onSuccess should not be called"));
}
@Override
public void onFailure(Exception e) {
future.complete(e);
}
});
Throwable t = future.get(10, TimeUnit.SECONDS);
assertTrue(t instanceof IllegalStateException);
assertTrue(t.getCause() instanceof RemoteTransportException);
assertTrue(t.getCause().getCause() instanceof IllegalArgumentException);
assertThat(t.getCause().getCause().getMessage(), containsString("Unknown NamedWriteable"));
}
use of org.opensearch.cluster.service.ClusterService in project OpenSearch by opensearch-project.
the class ShardStateActionIT method testFollowupRerouteCanBeSetToHigherPriority.
public void testFollowupRerouteCanBeSetToHigherPriority() {
// Shows that in a cluster under unbearable pressure we can still assign replicas (for now at least) by setting
// cluster.routing.allocation.shard_state.reroute.priority to a higher priority. Can be removed when this setting is removed, as
// we should at that point be confident that the default priority is appropriate for all clusters.
internalCluster().ensureAtLeastNumDataNodes(2);
assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(Settings.builder().put(ShardStateAction.FOLLOW_UP_REROUTE_PRIORITY_SETTING.getKey(), "urgent")));
// ensure that the master always has a HIGH priority pending task
final AtomicBoolean stopSpammingMaster = new AtomicBoolean();
final ClusterService masterClusterService = internalCluster().getInstance(ClusterService.class, internalCluster().getMasterName());
masterClusterService.submitStateUpdateTask("spam", new ClusterStateUpdateTask(Priority.HIGH) {
@Override
public ClusterState execute(ClusterState currentState) {
return currentState;
}
@Override
public void onFailure(String source, Exception e) {
throw new AssertionError(source, e);
}
@Override
public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
if (stopSpammingMaster.get() == false) {
masterClusterService.submitStateUpdateTask("spam", this);
}
}
});
// even with the master under such pressure, all shards of the index can be assigned; in particular, after the primaries have
// started there's a follow-up reroute at a higher priority than the spam
createIndex("test");
assertFalse(client().admin().cluster().prepareHealth().setWaitForGreenStatus().get().isTimedOut());
stopSpammingMaster.set(true);
assertFalse(client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).get().isTimedOut());
assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(Settings.builder().putNull(ShardStateAction.FOLLOW_UP_REROUTE_PRIORITY_SETTING.getKey())));
}
use of org.opensearch.cluster.service.ClusterService in project OpenSearch by opensearch-project.
the class TransportClearVotingConfigExclusionsAction method masterOperation.
@Override
protected void masterOperation(ClearVotingConfigExclusionsRequest request, ClusterState initialState, ActionListener<ClearVotingConfigExclusionsResponse> listener) throws Exception {
final long startTimeMillis = threadPool.relativeTimeInMillis();
final Predicate<ClusterState> allExclusionsRemoved = newState -> {
for (VotingConfigExclusion tombstone : initialState.getVotingConfigExclusions()) {
// NB checking for the existence of any node with this persistent ID, because persistent IDs are how votes are counted.
if (newState.nodes().nodeExists(tombstone.getNodeId())) {
return false;
}
}
return true;
};
if (request.getWaitForRemoval() && allExclusionsRemoved.test(initialState) == false) {
final ClusterStateObserver clusterStateObserver = new ClusterStateObserver(initialState, clusterService, request.getTimeout(), logger, threadPool.getThreadContext());
clusterStateObserver.waitForNextChange(new Listener() {
@Override
public void onNewClusterState(ClusterState state) {
submitClearVotingConfigExclusionsTask(request, startTimeMillis, listener);
}
@Override
public void onClusterServiceClose() {
listener.onFailure(new OpenSearchException("cluster service closed while waiting for removal of nodes " + initialState.getVotingConfigExclusions()));
}
@Override
public void onTimeout(TimeValue timeout) {
listener.onFailure(new OpenSearchTimeoutException("timed out waiting for removal of nodes; if nodes should not be removed, set waitForRemoval to false. " + initialState.getVotingConfigExclusions()));
}
}, allExclusionsRemoved);
} else {
submitClearVotingConfigExclusionsTask(request, startTimeMillis, listener);
}
}
use of org.opensearch.cluster.service.ClusterService in project OpenSearch by opensearch-project.
the class TransportClusterHealthAction method executeHealth.
private void executeHealth(final ClusterHealthRequest request, final ClusterState currentState, final ActionListener<ClusterHealthResponse> listener, final int waitCount, final Consumer<ClusterState> onNewClusterStateAfterDelay) {
if (request.timeout().millis() == 0) {
listener.onResponse(getResponse(request, currentState, waitCount, TimeoutState.ZERO_TIMEOUT));
return;
}
final Predicate<ClusterState> validationPredicate = newState -> validateRequest(request, newState, waitCount);
if (validationPredicate.test(currentState)) {
listener.onResponse(getResponse(request, currentState, waitCount, TimeoutState.OK));
} else {
final ClusterStateObserver observer = new ClusterStateObserver(currentState, clusterService, null, logger, threadPool.getThreadContext());
final ClusterStateObserver.Listener stateListener = new ClusterStateObserver.Listener() {
@Override
public void onNewClusterState(ClusterState newState) {
onNewClusterStateAfterDelay.accept(newState);
}
@Override
public void onClusterServiceClose() {
listener.onFailure(new NodeClosedException(clusterService.localNode()));
}
@Override
public void onTimeout(TimeValue timeout) {
listener.onResponse(getResponse(request, observer.setAndGetObservedState(), waitCount, TimeoutState.TIMED_OUT));
}
};
observer.waitForNextChange(stateListener, validationPredicate, request.timeout());
}
}
use of org.opensearch.cluster.service.ClusterService in project OpenSearch by opensearch-project.
the class IndexRecoveryIT method testUsesFileBasedRecoveryIfRetentionLeaseAheadOfGlobalCheckpoint.
public void testUsesFileBasedRecoveryIfRetentionLeaseAheadOfGlobalCheckpoint() throws Exception {
internalCluster().ensureAtLeastNumDataNodes(2);
String indexName = "test-index";
createIndex(indexName, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true).put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), "12h").build());
indexRandom(randomBoolean(), randomBoolean(), randomBoolean(), IntStream.range(0, between(0, 100)).mapToObj(n -> client().prepareIndex(indexName).setSource("num", n)).collect(toList()));
ensureGreen(indexName);
final ShardId shardId = new ShardId(resolveIndex(indexName), 0);
final DiscoveryNodes discoveryNodes = clusterService().state().nodes();
final IndexShardRoutingTable indexShardRoutingTable = clusterService().state().routingTable().shardRoutingTable(shardId);
final IndexShard primary = internalCluster().getInstance(IndicesService.class, discoveryNodes.get(indexShardRoutingTable.primaryShard().currentNodeId()).getName()).getShardOrNull(shardId);
final ShardRouting replicaShardRouting = indexShardRoutingTable.replicaShards().get(0);
internalCluster().restartNode(discoveryNodes.get(replicaShardRouting.currentNodeId()).getName(), new InternalTestCluster.RestartCallback() {
@Override
public Settings onNodeStopped(String nodeName) throws Exception {
assertFalse(client().admin().cluster().prepareHealth().setWaitForNodes(Integer.toString(discoveryNodes.getSize() - 1)).setWaitForEvents(Priority.LANGUID).get().isTimedOut());
indexRandom(randomBoolean(), randomBoolean(), randomBoolean(), IntStream.range(0, between(1, 100)).mapToObj(n -> client().prepareIndex(indexName).setSource("num", n)).collect(toList()));
// We do not guarantee that the replica can recover locally all the way to its own global checkpoint before starting
// to recover from the primary, so we must be careful not to perform an operations-based recovery if this would require
// some operations that are not being retained. Emulate this by advancing the lease ahead of the replica's GCP:
primary.renewRetentionLease(ReplicationTracker.getPeerRecoveryRetentionLeaseId(replicaShardRouting), primary.seqNoStats().getMaxSeqNo() + 1, ReplicationTracker.PEER_RECOVERY_RETENTION_LEASE_SOURCE);
return super.onNodeStopped(nodeName);
}
});
ensureGreen(indexName);
// noinspection OptionalGetWithoutIsPresent because it fails the test if absent
final RecoveryState recoveryState = client().admin().indices().prepareRecoveries(indexName).get().shardRecoveryStates().get(indexName).stream().filter(rs -> rs.getPrimary() == false).findFirst().get();
assertThat(recoveryState.getIndex().totalFileCount(), greaterThan(0));
}
Aggregations