All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opensearch.indices.recovery.AsyncRecoveryTarget Maven / Gradle / Ivy

There is a newer version: 2.17.0
Show newest version
/*
 * SPDX-License-Identifier: Apache-2.0
 *
 * The OpenSearch Contributors require contributions made to
 * this file be licensed under the Apache-2.0 license or a
 * compatible open source license.
 */

/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*
 * Modifications Copyright OpenSearch Contributors. See
 * GitHub history for details.
 */

package org.opensearch.indices.recovery;

import org.apache.lucene.util.BytesRef;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.index.seqno.ReplicationTracker;
import org.opensearch.index.seqno.RetentionLeases;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.index.store.Store;
import org.opensearch.index.store.StoreFileMetadata;
import org.opensearch.index.translog.Translog;
import org.opensearch.indices.replication.SegmentReplicationTarget;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.function.Function;

/**
 * Wraps a {@link RecoveryTarget} to make all remote calls to be executed asynchronously using the provided {@code executor}.
 */
public class AsyncRecoveryTarget implements RecoveryTargetHandler {
    private final RecoveryTargetHandler target;
    private final Executor executor;

    private final IndexShard primary;

    private final IndexShard replica;

    private final Function, List> replicatePrimaryFunction;

    public AsyncRecoveryTarget(RecoveryTargetHandler target, Executor executor) {
        this.executor = executor;
        this.target = target;
        this.primary = null;
        this.replica = null;
        this.replicatePrimaryFunction = (a) -> null;
    }

    public AsyncRecoveryTarget(
        RecoveryTargetHandler target,
        Executor executor,
        IndexShard primary,
        IndexShard replica,
        Function, List> replicatePrimaryFunction
    ) {
        this.executor = executor;
        this.target = target;
        this.primary = primary;
        this.replica = replica;
        this.replicatePrimaryFunction = replicatePrimaryFunction;
    }

    @Override
    public void prepareForTranslogOperations(int totalTranslogOps, ActionListener listener) {
        executor.execute(() -> target.prepareForTranslogOperations(totalTranslogOps, listener));
    }

    @Override
    public void forceSegmentFileSync() {
        this.replicatePrimaryFunction.apply(List.of(primary, replica));
    }

    @Override
    public void finalizeRecovery(long globalCheckpoint, long trimAboveSeqNo, ActionListener listener) {
        executor.execute(() -> target.finalizeRecovery(globalCheckpoint, trimAboveSeqNo, listener));
    }

    @Override
    public void handoffPrimaryContext(ReplicationTracker.PrimaryContext primaryContext) {
        target.handoffPrimaryContext(primaryContext);
    }

    @Override
    public void indexTranslogOperations(
        List operations,
        int totalTranslogOps,
        long maxSeenAutoIdTimestampOnPrimary,
        long maxSeqNoOfDeletesOrUpdatesOnPrimary,
        RetentionLeases retentionLeases,
        long mappingVersionOnPrimary,
        ActionListener listener
    ) {
        executor.execute(
            () -> target.indexTranslogOperations(
                operations,
                totalTranslogOps,
                maxSeenAutoIdTimestampOnPrimary,
                maxSeqNoOfDeletesOrUpdatesOnPrimary,
                retentionLeases,
                mappingVersionOnPrimary,
                listener
            )
        );
    }

    @Override
    public void receiveFileInfo(
        List phase1FileNames,
        List phase1FileSizes,
        List phase1ExistingFileNames,
        List phase1ExistingFileSizes,
        int totalTranslogOps,
        ActionListener listener
    ) {
        executor.execute(
            () -> target.receiveFileInfo(
                phase1FileNames,
                phase1FileSizes,
                phase1ExistingFileNames,
                phase1ExistingFileSizes,
                totalTranslogOps,
                listener
            )
        );
    }

    @Override
    public void cleanFiles(
        int totalTranslogOps,
        long globalCheckpoint,
        Store.MetadataSnapshot sourceMetadata,
        ActionListener listener
    ) {
        executor.execute(() -> target.cleanFiles(totalTranslogOps, globalCheckpoint, sourceMetadata, listener));
    }

    @Override
    public void writeFileChunk(
        StoreFileMetadata fileMetadata,
        long position,
        BytesReference content,
        boolean lastChunk,
        int totalTranslogOps,
        ActionListener listener
    ) {
        final BytesReference copy = new BytesArray(BytesRef.deepCopyOf(content.toBytesRef()));
        executor.execute(() -> target.writeFileChunk(fileMetadata, position, copy, lastChunk, totalTranslogOps, listener));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy