Skip to content

Commit

Permalink
Return completablefuture
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Ho <dxho@amazon.com>
  • Loading branch information
derek-ho committed Nov 14, 2024
1 parent 95c390e commit 267cfd3
Showing 1 changed file with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -431,30 +432,34 @@ Future<Void> executeConfigurationInitialization(final SecurityMetadata securityM
return CompletableFuture.completedFuture(null);
}

public void createApiTokenIndex() {
try {
// wait for the cluster here until it will finish managed node election
while (clusterService.state().blocks().hasGlobalBlockWithStatus(RestStatus.SERVICE_UNAVAILABLE)) {
LOGGER.info("Wait for cluster to be available ...");
TimeUnit.SECONDS.sleep(1);
}

public CompletableFuture<Boolean> createApiTokenIndex() {
return CompletableFuture.supplyAsync(() -> {
try {
final ThreadContext threadContext = threadPool.getThreadContext();
try (StoredContext ctx = threadContext.stashContext()) {
threadContext.putHeader(ConfigConstants.OPENDISTRO_SECURITY_CONF_REQUEST_HEADER, "true");

createIndexIfAbsent(ConfigConstants.OPENSEARCH_API_TOKENS_INDEX);
waitForIndexToBeAtLeastYellow(ConfigConstants.OPENSEARCH_API_TOKENS_INDEX);
// wait for the cluster here until it will finish managed node election
while (clusterService.state().blocks().hasGlobalBlockWithStatus(RestStatus.SERVICE_UNAVAILABLE)) {
LOGGER.info("Wait for cluster to be available ...");
TimeUnit.SECONDS.sleep(1);
}

try {
final ThreadContext threadContext = threadPool.getThreadContext();
try (StoredContext ctx = threadContext.stashContext()) {
threadContext.putHeader(ConfigConstants.OPENDISTRO_SECURITY_CONF_REQUEST_HEADER, "true");

createIndexIfAbsent(ConfigConstants.OPENSEARCH_API_TOKENS_INDEX);
waitForIndexToBeAtLeastYellow(ConfigConstants.OPENSEARCH_API_TOKENS_INDEX);
}
return true;

} catch (Exception e) {
LOGGER.error("Cannot create API token index (this is maybe not an error!)", e);
throw new CompletionException(e);
}
} catch (Exception e) {
LOGGER.error("Cannot create API token index (this is maybe not an error!)", e);
LOGGER.error("Unexpected exception while initializing node " + e, e);
throw new CompletionException(e);
}
} catch (Exception e) {
LOGGER.error("Unexpected exception while initializing node " + e, e);
}

});
}

@Deprecated
Expand Down

0 comments on commit 267cfd3

Please sign in to comment.