1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef SEND_TASK_SCHEDULER_H 17 #define SEND_TASK_SCHEDULER_H 18 19 #include <map> 20 #include <list> 21 #include <mutex> 22 #include <vector> 23 #include <string> 24 #include <cstdint> 25 #include "macro_utils.h" 26 #include "communicator_type_define.h" 27 28 namespace DistributedDB { 29 enum class TargetPolicy { 30 NO_DELAY = 0, 31 DELAY = 1, 32 }; 33 34 class SerialBuffer; // Forward Declaration 35 36 struct SendTask { 37 SerialBuffer *buffer; 38 std::string dstTarget; 39 OnSendEnd onEnd; 40 }; 41 42 struct SendTaskInfo { 43 bool delayFlag; 44 Priority taskPrio; 45 }; 46 47 using TaskListByTarget = std::map<std::string, std::list<SendTask>>; 48 49 class SendTaskScheduler { 50 public: 51 SendTaskScheduler() = default; // Default constructor must be explicitly provided due to DISABLE_COPY_ASSIGN_MOVE 52 ~SendTaskScheduler(); 53 54 DISABLE_COPY_ASSIGN_MOVE(SendTaskScheduler); 55 56 void Initialize(); 57 58 // This method for consumer 59 void Finalize(); 60 61 // This method for producer, support multiple thread 62 int AddSendTaskIntoSchedule(const SendTask &inTask, Priority inPrio); 63 64 // This method for consumer, not recommend for multiple thread 65 int ScheduleOutSendTask(SendTask &outTask); 66 int ScheduleOutSendTask(SendTask &outTask, SendTaskInfo &outTaskInfo); 67 68 // This method for consumer, call ScheduleOutSendTask at least one time before each calling this 69 int FinalizeLastScheduleTask(); 70 71 // These two mothods influence the task that will be schedule out next time 72 int DelayTaskByTarget(const std::string &inTarget); 73 int NoDelayTaskByTarget(const std::string &inTarget); 74 75 uint32_t GetTotalTaskCount() const; 76 uint32_t GetNoDelayTaskCount() const; 77 78 private: 79 int ScheduleDelayTask(SendTask &outTask, SendTaskInfo &outTaskInfo); 80 int ScheduleNoDelayTask(SendTask &outTask, SendTaskInfo &outTaskInfo); 81 82 mutable std::mutex overallMutex_; 83 uint32_t curTotalSizeByByte_ = 0; 84 uint32_t curTotalSizeByTask_ = 0; 85 uint32_t delayTaskCount_ = 0; 86 87 std::vector<Priority> priorityOrder_; 88 std::map<Priority, uint32_t> extraCapacityInByteByPrio_; 89 std::map<std::string, TargetPolicy> policyMap_; 90 91 std::map<Priority, uint32_t> taskCountByPrio_; 92 std::map<Priority, uint32_t> taskDelayCountByPrio_; 93 std::map<Priority, std::list<std::string>> taskOrderByPrio_; 94 std::map<Priority, TaskListByTarget> taskGroupByPrio_; 95 96 bool scheduledFlag_ = false; 97 std::string lastScheduleTarget_; 98 Priority lastSchedulePriority_ = Priority::LOW; 99 }; 100 } 101 102 #endif