Search in sources :

Example 16 with CountDown

use of org.opensearch.common.util.concurrent.CountDown in project OpenSearch by opensearch-project.

the class GoogleCloudStorageBlobContainerRetriesTests method testWriteBlobWithRetries.

public void testWriteBlobWithRetries() throws Exception {
    final int maxRetries = randomIntBetween(2, 10);
    final CountDown countDown = new CountDown(maxRetries);
    final byte[] bytes = randomBlobContent();
    httpServer.createContext("/upload/storage/v1/b/bucket/o", safeHandler(exchange -> {
        assertThat(exchange.getRequestURI().getQuery(), containsString("uploadType=multipart"));
        if (countDown.countDown()) {
            Optional<Tuple<String, BytesReference>> content = parseMultipartRequestBody(exchange.getRequestBody());
            assertThat(content.isPresent(), is(true));
            assertThat(content.get().v1(), equalTo("write_blob_max_retries"));
            if (Objects.deepEquals(bytes, BytesReference.toBytes(content.get().v2()))) {
                byte[] response = ("{\"bucket\":\"bucket\",\"name\":\"" + content.get().v1() + "\"}").getBytes(UTF_8);
                exchange.getResponseHeaders().add("Content-Type", "application/json");
                exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
                exchange.getResponseBody().write(response);
            } else {
                exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1);
            }
            return;
        }
        if (randomBoolean()) {
            if (randomBoolean()) {
                Streams.readFully(exchange.getRequestBody(), new byte[randomIntBetween(1, Math.max(1, bytes.length - 1))]);
            } else {
                Streams.readFully(exchange.getRequestBody());
                exchange.sendResponseHeaders(HttpStatus.SC_INTERNAL_SERVER_ERROR, -1);
            }
        }
    }));
    final BlobContainer blobContainer = createBlobContainer(maxRetries, null, null, null);
    try (InputStream stream = new InputStreamIndexInput(new ByteArrayIndexInput("desc", bytes), bytes.length)) {
        blobContainer.writeBlob("write_blob_max_retries", stream, bytes.length, false);
    }
    assertThat(countDown.isCountedDown(), is(true));
}
Also used : Arrays(java.util.Arrays) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings) CountDown(org.opensearch.common.util.concurrent.CountDown) HttpStatus(org.apache.http.HttpStatus) StorageOptions(com.google.cloud.storage.StorageOptions) Strings(org.opensearch.common.Strings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) TOKEN_URI_SETTING(org.opensearch.repositories.gcs.GoogleCloudStorageClientSettings.TOKEN_URI_SETTING) SuppressForbidden(org.opensearch.common.SuppressForbidden) TimeValue(org.opensearch.common.unit.TimeValue) OpenSearchMockAPIBasedRepositoryIntegTestCase(org.opensearch.repositories.blobstore.OpenSearchMockAPIBasedRepositoryIntegTestCase) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Settings(org.opensearch.common.settings.Settings) RestStatus(org.opensearch.rest.RestStatus) InetSocketAddress(java.net.InetSocketAddress) Nullable(org.opensearch.common.Nullable) Tuple(org.opensearch.common.collect.Tuple) Duration(org.threeten.bp.Duration) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) BytesArray(org.opensearch.common.bytes.BytesArray) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Matchers.anyOf(org.hamcrest.Matchers.anyOf) Matchers.containsString(org.hamcrest.Matchers.containsString) InetAddresses(org.opensearch.common.network.InetAddresses) BytesReference(org.opensearch.common.bytes.BytesReference) BlobContainer(org.opensearch.common.blobstore.BlobContainer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) HashMap(java.util.HashMap) GoogleCloudStorageHttpHandler.getContentRangeEnd(fixture.gcs.GoogleCloudStorageHttpHandler.getContentRangeEnd) AtomicReference(java.util.concurrent.atomic.AtomicReference) RetrySettings(com.google.api.gax.retrying.RetrySettings) Streams(org.opensearch.common.io.Streams) AbstractBlobContainerRetriesTestCase(org.opensearch.repositories.blobstore.AbstractBlobContainerRetriesTestCase) SocketTimeoutException(java.net.SocketTimeoutException) GoogleCloudStorageHttpHandler.parseMultipartRequestBody(fixture.gcs.GoogleCloudStorageHttpHandler.parseMultipartRequestBody) READ_TIMEOUT_SETTING(org.opensearch.repositories.gcs.GoogleCloudStorageClientSettings.READ_TIMEOUT_SETTING) UUIDs(org.opensearch.common.UUIDs) HttpTransportOptions(com.google.cloud.http.HttpTransportOptions) FakeOAuth2HttpHandler(fixture.gcs.FakeOAuth2HttpHandler) InputStreamIndexInput(org.opensearch.common.lucene.store.InputStreamIndexInput) GoogleCloudStorageHttpHandler.getContentRangeStart(fixture.gcs.GoogleCloudStorageHttpHandler.getContentRangeStart) UTF_8(java.nio.charset.StandardCharsets.UTF_8) RestUtils(org.opensearch.rest.RestUtils) IOException(java.io.IOException) TestUtils.createServiceAccount(org.opensearch.repositories.gcs.TestUtils.createServiceAccount) GoogleCloudStorageHttpHandler.getContentRangeLimit(fixture.gcs.GoogleCloudStorageHttpHandler.getContentRangeLimit) ENDPOINT_SETTING(org.opensearch.repositories.gcs.GoogleCloudStorageClientSettings.ENDPOINT_SETTING) CREDENTIALS_FILE_SETTING(org.opensearch.repositories.gcs.GoogleCloudStorageClientSettings.CREDENTIALS_FILE_SETTING) BlobPath(org.opensearch.common.blobstore.BlobPath) ByteArrayIndexInput(org.opensearch.common.lucene.store.ByteArrayIndexInput) StorageException(com.google.cloud.storage.StorageException) HttpHandler(com.sun.net.httpserver.HttpHandler) InputStream(java.io.InputStream) OpenSearchBlobStoreRepositoryIntegTestCase.randomBytes(org.opensearch.repositories.blobstore.OpenSearchBlobStoreRepositoryIntegTestCase.randomBytes) BytesReference(org.opensearch.common.bytes.BytesReference) Optional(java.util.Optional) InputStream(java.io.InputStream) BlobContainer(org.opensearch.common.blobstore.BlobContainer) InputStreamIndexInput(org.opensearch.common.lucene.store.InputStreamIndexInput) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayIndexInput(org.opensearch.common.lucene.store.ByteArrayIndexInput) CountDown(org.opensearch.common.util.concurrent.CountDown)

Example 17 with CountDown

use of org.opensearch.common.util.concurrent.CountDown in project OpenSearch by opensearch-project.

the class TransportSearchAction method ccsRemoteReduce.

static void ccsRemoteReduce(SearchRequest searchRequest, OriginalIndices localIndices, Map<String, OriginalIndices> remoteIndices, SearchTimeProvider timeProvider, InternalAggregation.ReduceContextBuilder aggReduceContextBuilder, RemoteClusterService remoteClusterService, ThreadPool threadPool, ActionListener<SearchResponse> listener, BiConsumer<SearchRequest, ActionListener<SearchResponse>> localSearchConsumer) {
    if (localIndices == null && remoteIndices.size() == 1) {
        // if we are searching against a single remote cluster, we simply forward the original search request to such cluster
        // and we directly perform final reduction in the remote cluster
        Map.Entry<String, OriginalIndices> entry = remoteIndices.entrySet().iterator().next();
        String clusterAlias = entry.getKey();
        boolean skipUnavailable = remoteClusterService.isSkipUnavailable(clusterAlias);
        OriginalIndices indices = entry.getValue();
        SearchRequest ccsSearchRequest = SearchRequest.subSearchRequest(searchRequest, indices.indices(), clusterAlias, timeProvider.getAbsoluteStartMillis(), true);
        Client remoteClusterClient = remoteClusterService.getRemoteClusterClient(threadPool, clusterAlias);
        remoteClusterClient.search(ccsSearchRequest, new ActionListener<SearchResponse>() {

            @Override
            public void onResponse(SearchResponse searchResponse) {
                Map<String, ProfileShardResult> profileResults = searchResponse.getProfileResults();
                SearchProfileShardResults profile = profileResults == null || profileResults.isEmpty() ? null : new SearchProfileShardResults(profileResults);
                InternalSearchResponse internalSearchResponse = new InternalSearchResponse(searchResponse.getHits(), (InternalAggregations) searchResponse.getAggregations(), searchResponse.getSuggest(), profile, searchResponse.isTimedOut(), searchResponse.isTerminatedEarly(), searchResponse.getNumReducePhases());
                listener.onResponse(new SearchResponse(internalSearchResponse, searchResponse.getScrollId(), searchResponse.getTotalShards(), searchResponse.getSuccessfulShards(), searchResponse.getSkippedShards(), timeProvider.buildTookInMillis(), searchResponse.getShardFailures(), new SearchResponse.Clusters(1, 1, 0), searchResponse.pointInTimeId()));
            }

            @Override
            public void onFailure(Exception e) {
                if (skipUnavailable) {
                    listener.onResponse(SearchResponse.empty(timeProvider::buildTookInMillis, new SearchResponse.Clusters(1, 0, 1)));
                } else {
                    listener.onFailure(wrapRemoteClusterFailure(clusterAlias, e));
                }
            }
        });
    } else {
        SearchResponseMerger searchResponseMerger = createSearchResponseMerger(searchRequest.source(), timeProvider, aggReduceContextBuilder);
        AtomicInteger skippedClusters = new AtomicInteger(0);
        final AtomicReference<Exception> exceptions = new AtomicReference<>();
        int totalClusters = remoteIndices.size() + (localIndices == null ? 0 : 1);
        final CountDown countDown = new CountDown(totalClusters);
        for (Map.Entry<String, OriginalIndices> entry : remoteIndices.entrySet()) {
            String clusterAlias = entry.getKey();
            boolean skipUnavailable = remoteClusterService.isSkipUnavailable(clusterAlias);
            OriginalIndices indices = entry.getValue();
            SearchRequest ccsSearchRequest = SearchRequest.subSearchRequest(searchRequest, indices.indices(), clusterAlias, timeProvider.getAbsoluteStartMillis(), false);
            ActionListener<SearchResponse> ccsListener = createCCSListener(clusterAlias, skipUnavailable, countDown, skippedClusters, exceptions, searchResponseMerger, totalClusters, listener);
            Client remoteClusterClient = remoteClusterService.getRemoteClusterClient(threadPool, clusterAlias);
            remoteClusterClient.search(ccsSearchRequest, ccsListener);
        }
        if (localIndices != null) {
            ActionListener<SearchResponse> ccsListener = createCCSListener(RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY, false, countDown, skippedClusters, exceptions, searchResponseMerger, totalClusters, listener);
            SearchRequest ccsLocalSearchRequest = SearchRequest.subSearchRequest(searchRequest, localIndices.indices(), RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY, timeProvider.getAbsoluteStartMillis(), false);
            localSearchConsumer.accept(ccsLocalSearchRequest, ccsListener);
        }
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDown(org.opensearch.common.util.concurrent.CountDown) RemoteTransportException(org.opensearch.transport.RemoteTransportException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) SearchProfileShardResults(org.opensearch.search.profile.SearchProfileShardResults) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Client(org.opensearch.client.Client) NodeClient(org.opensearch.client.node.NodeClient) OriginSettingClient(org.opensearch.client.OriginSettingClient) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) OriginalIndices(org.opensearch.action.OriginalIndices) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 18 with CountDown

use of org.opensearch.common.util.concurrent.CountDown in project OpenSearch by opensearch-project.

the class TransportBroadcastReplicationAction method doExecute.

@Override
protected void doExecute(Task task, Request request, ActionListener<Response> listener) {
    final ClusterState clusterState = clusterService.state();
    List<ShardId> shards = shards(request, clusterState);
    final CopyOnWriteArrayList<ShardResponse> shardsResponses = new CopyOnWriteArrayList<>();
    if (shards.size() == 0) {
        finishAndNotifyListener(listener, shardsResponses);
    }
    final CountDown responsesCountDown = new CountDown(shards.size());
    for (final ShardId shardId : shards) {
        ActionListener<ShardResponse> shardActionListener = new ActionListener<ShardResponse>() {

            @Override
            public void onResponse(ShardResponse shardResponse) {
                shardsResponses.add(shardResponse);
                logger.trace("{}: got response from {}", actionName, shardId);
                if (responsesCountDown.countDown()) {
                    finishAndNotifyListener(listener, shardsResponses);
                }
            }

            @Override
            public void onFailure(Exception e) {
                logger.trace("{}: got failure from {}", actionName, shardId);
                int totalNumCopies = clusterState.getMetadata().getIndexSafe(shardId.getIndex()).getNumberOfReplicas() + 1;
                ShardResponse shardResponse = newShardResponse();
                ReplicationResponse.ShardInfo.Failure[] failures;
                if (TransportActions.isShardNotAvailableException(e)) {
                    failures = new ReplicationResponse.ShardInfo.Failure[0];
                } else {
                    ReplicationResponse.ShardInfo.Failure failure = new ReplicationResponse.ShardInfo.Failure(shardId, null, e, ExceptionsHelper.status(e), true);
                    failures = new ReplicationResponse.ShardInfo.Failure[totalNumCopies];
                    Arrays.fill(failures, failure);
                }
                shardResponse.setShardInfo(new ReplicationResponse.ShardInfo(totalNumCopies, 0, failures));
                shardsResponses.add(shardResponse);
                if (responsesCountDown.countDown()) {
                    finishAndNotifyListener(listener, shardsResponses);
                }
            }
        };
        shardExecute(task, request, shardId, shardActionListener);
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) CountDown(org.opensearch.common.util.concurrent.CountDown) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardId(org.opensearch.index.shard.ShardId) ActionListener(org.opensearch.action.ActionListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

CountDown (org.opensearch.common.util.concurrent.CountDown)18 BlobContainer (org.opensearch.common.blobstore.BlobContainer)9 InputStream (java.io.InputStream)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 TimeValue (org.opensearch.common.unit.TimeValue)6 IOException (java.io.IOException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Client (org.opensearch.client.Client)5 BytesReference (org.opensearch.common.bytes.BytesReference)5 Arrays (java.util.Arrays)4 ActionListener (org.opensearch.action.ActionListener)4 List (java.util.List)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 OriginalIndices (org.opensearch.action.OriginalIndices)3 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)3 Strings (org.opensearch.common.Strings)3 ByteArrayIndexInput (org.opensearch.common.lucene.store.ByteArrayIndexInput)3 InputStreamIndexInput (org.opensearch.common.lucene.store.InputStreamIndexInput)3 MD5DigestCalculatingInputStream (com.amazonaws.services.s3.internal.MD5DigestCalculatingInputStream)2