// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #pragma once #include "isequencedtaskexecutor.h" #include #include #include namespace vespalib { class ThreadExecutor; class SyncableThreadExecutor; /** * Class to run multiple tasks in parallel, but tasks with same * id has to be run in sequence. */ class SequencedTaskExecutor final : public ISequencedTaskExecutor { public: using ISequencedTaskExecutor::getExecutorId; using OptimizeFor = vespalib::Executor::OptimizeFor; ~SequencedTaskExecutor() override; void setTaskLimit(uint32_t taskLimit) override; void executeTask(ExecutorId id, vespalib::Executor::Task::UP task) override; ExecutorId getExecutorId(uint64_t componentId) const override; void sync_all() override; ExecutorStats getStats() override; void wakeup() override; /** * Returns the ExecutorStats of each underlying executor. * * Calling this function resets the statistics of each underlying executor, * similar to calling getStats(). */ std::vector get_raw_stats(); static std::unique_ptr create(Runnable::init_fun_t func, uint32_t threads); static std::unique_ptr create(Runnable::init_fun_t func, uint32_t threads, uint32_t taskLimit); static std::unique_ptr create(Runnable::init_fun_t func, uint32_t threads, uint32_t taskLimit, bool is_task_limit_hard, OptimizeFor optimize); static std::unique_ptr create(Runnable::init_fun_t func, uint32_t threads, uint32_t taskLimit, bool is_task_limit_hard, OptimizeFor optimize, uint32_t kindOfWatermark); /** * For testing only */ uint32_t getComponentHashSize() const { return _component2IdImperfect.size(); } uint32_t getComponentEffectiveHashSize() const { return _nextId; } const vespalib::ThreadExecutor* first_executor() const; private: explicit SequencedTaskExecutor(std::vector> executor); std::optional getExecutorIdPerfect(uint64_t componentId) const; ExecutorId getExecutorIdImPerfect(uint64_t componentId) const; std::vector> _executors; using PerfectKeyT = uint16_t; const bool _lazyExecutors; mutable std::unique_ptr _component2IdPerfect; mutable std::vector _component2IdImperfect; mutable std::mutex _mutex; mutable uint32_t _nextId; }; } // namespace search