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

org.elasticsearch.repositories.blobstore.ShardSnapshotTaskRunner Maven / Gradle / Ivy

There is a newer version: 8.13.4
Show newest version
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.repositories.blobstore;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRunnable;
import org.elasticsearch.common.CheckedBiConsumer;
import org.elasticsearch.common.util.concurrent.PrioritizedThrottledTaskRunner;
import org.elasticsearch.repositories.SnapshotShardContext;

import java.io.IOException;
import java.util.Comparator;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Supplier;

import static org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo;

/**
 * {@link ShardSnapshotTaskRunner} performs snapshotting tasks, prioritizing {@link ShardSnapshotTask}
 * over {@link FileSnapshotTask}. Each enqueued shard to snapshot results in one {@link ShardSnapshotTask}
 * and zero or more {@link FileSnapshotTask}s.
 */
public class ShardSnapshotTaskRunner {
    private final PrioritizedThrottledTaskRunner taskRunner;
    private final Consumer shardSnapshotter;
    private final CheckedBiConsumer fileSnapshotter;

    abstract static class SnapshotTask implements Comparable, Runnable {

        private static final Comparator COMPARATOR = Comparator.comparingLong(
            (SnapshotTask t) -> t.context().snapshotStartTime()
        ).thenComparing(t -> t.context().snapshotId().getUUID()).thenComparingInt(SnapshotTask::priority);

        protected final SnapshotShardContext context;

        SnapshotTask(SnapshotShardContext context) {
            this.context = context;
        }

        public abstract int priority();

        public SnapshotShardContext context() {
            return context;
        }

        @Override
        public final int compareTo(SnapshotTask other) {
            return COMPARATOR.compare(this, other);
        }
    }

    class ShardSnapshotTask extends SnapshotTask {
        ShardSnapshotTask(SnapshotShardContext context) {
            super(context);
        }

        @Override
        public void run() {
            shardSnapshotter.accept(context);
        }

        @Override
        public int priority() {
            return 1;
        }

        @Override
        public String toString() {
            return getClass().getSimpleName() + "{snapshotID=[" + context.snapshotId() + "], indexID=[" + context.indexId() + "]}";
        }
    }

    class FileSnapshotTask extends SnapshotTask {
        private final Supplier fileInfos;
        private final ActionListener fileSnapshotListener;

        FileSnapshotTask(SnapshotShardContext context, Supplier fileInfos, ActionListener fileSnapshotListener) {
            super(context);
            this.fileInfos = fileInfos;
            this.fileSnapshotListener = fileSnapshotListener;
        }

        @Override
        public void run() {
            ActionRunnable.run(fileSnapshotListener, () -> {
                FileInfo fileInfo = fileInfos.get();
                if (fileInfo != null) {
                    fileSnapshotter.accept(context, fileInfo);
                }
            }).run();
        }

        @Override
        public int priority() {
            return 2;
        }

        @Override
        public String toString() {
            return getClass().getSimpleName() + "{snapshotID=[" + context.snapshotId() + "], indexID=[" + context.indexId() + "]}";
        }
    }

    public ShardSnapshotTaskRunner(
        final int maxRunningTasks,
        final Executor executor,
        final Consumer shardSnapshotter,
        final CheckedBiConsumer fileSnapshotter
    ) {
        this.taskRunner = new PrioritizedThrottledTaskRunner<>("ShardSnapshotTaskRunner", maxRunningTasks, executor);
        this.shardSnapshotter = shardSnapshotter;
        this.fileSnapshotter = fileSnapshotter;
    }

    public void enqueueShardSnapshot(final SnapshotShardContext context) {
        ShardSnapshotTask task = new ShardSnapshotTask(context);
        taskRunner.enqueueTask(task);
    }

    public void enqueueFileSnapshot(
        final SnapshotShardContext context,
        final Supplier fileInfos,
        final ActionListener listener
    ) {
        final FileSnapshotTask task = new FileSnapshotTask(context, fileInfos, listener);
        taskRunner.enqueueTask(task);
    }

    // visible for testing
    int runningTasks() {
        return taskRunner.runningTasks();
    }

    // visible for testing
    int queueSize() {
        return taskRunner.queueSize();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy