|
| 1 | +// Copyright (c) 2024-present, Qihoo, Inc. All rights reserved. |
| 2 | +// This source code is licensed under the BSD-style license found in the |
| 3 | +// LICENSE file in the root directory of this source tree. |
| 4 | + |
| 5 | +#include "batch_manager.h" |
| 6 | +#include <glog/logging.h> |
| 7 | +#include "pstd/include/pstd_defer.h" |
| 8 | + |
| 9 | +namespace pika_raft { |
| 10 | + |
| 11 | +// BatchClosure 实现 |
| 12 | +void BatchClosure::Run() { |
| 13 | + std::unique_ptr<BatchClosure> self_guard(this); |
| 14 | + |
| 15 | + // 根据 Raft 结果通知所有 promise |
| 16 | + if (status().ok()) { |
| 17 | + for (auto& promise : promises_) { |
| 18 | + if (promise) { |
| 19 | + promise->set_value(rocksdb::Status::OK()); |
| 20 | + } |
| 21 | + } |
| 22 | + } else { |
| 23 | + rocksdb::Status error_status = rocksdb::Status::IOError(status().error_str()); |
| 24 | + for (auto& promise : promises_) { |
| 25 | + if (promise) { |
| 26 | + promise->set_value(error_status); |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// BatchManager 实现 |
| 33 | +BatchManager::BatchManager(braft::Node* raft_node, const BatchConfig& config) |
| 34 | + : raft_node_(raft_node), config_(config) { |
| 35 | + pending_items_.reserve(config_.max_batch_size); |
| 36 | +} |
| 37 | + |
| 38 | +BatchManager::~BatchManager() { |
| 39 | + Stop(); |
| 40 | +} |
| 41 | + |
| 42 | +void BatchManager::Submit(const std::string& log_data, |
| 43 | + std::shared_ptr<std::promise<rocksdb::Status>> promise) { |
| 44 | + if (!config_.enable_batching) { |
| 45 | + // 批处理未启用,直接提交 |
| 46 | + auto* closure = new BatchClosure({promise}); |
| 47 | + braft::Task task; |
| 48 | + butil::IOBuf buf; |
| 49 | + buf.append(log_data); |
| 50 | + task.data = &buf; |
| 51 | + task.done = closure; |
| 52 | + raft_node_->apply(task); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + std::lock_guard<std::mutex> lock(mutex_); |
| 57 | + |
| 58 | + BatchItem item; |
| 59 | + item.log_data = log_data; |
| 60 | + item.promise = promise; |
| 61 | + item.submit_time_us = butil::gettimeofday_us(); |
| 62 | + |
| 63 | + pending_items_.push_back(std::move(item)); |
| 64 | + total_requests_.fetch_add(1, std::memory_order_relaxed); |
| 65 | + |
| 66 | + // 如果队列满了,立即通知工作线程 |
| 67 | + if (pending_items_.size() >= config_.max_batch_size) { |
| 68 | + cv_.notify_one(); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void BatchManager::Start() { |
| 73 | + if (running_.exchange(true)) { |
| 74 | + return; // 已经在运行 |
| 75 | + } |
| 76 | + |
| 77 | + batch_thread_ = std::thread([this]() { BatchWorker(); }); |
| 78 | + LOG(INFO) << "BatchManager started with config: timeout=" << config_.batch_timeout_ms |
| 79 | + << "ms, max_batch_size=" << config_.max_batch_size; |
| 80 | +} |
| 81 | + |
| 82 | +void BatchManager::Stop() { |
| 83 | + if (!running_.exchange(false)) { |
| 84 | + return; // 已经停止 |
| 85 | + } |
| 86 | + |
| 87 | + cv_.notify_one(); |
| 88 | + if (batch_thread_.joinable()) { |
| 89 | + batch_thread_.join(); |
| 90 | + } |
| 91 | + |
| 92 | + LOG(INFO) << "BatchManager stopped. Total requests: " << total_requests_.load() |
| 93 | + << ", Total batches: " << total_batches_.load(); |
| 94 | +} |
| 95 | + |
| 96 | +void BatchManager::BatchWorker() { |
| 97 | + LOG(INFO) << "BatchManager worker thread started"; |
| 98 | + |
| 99 | + while (running_.load(std::memory_order_relaxed)) { |
| 100 | + std::unique_lock<std::mutex> lock(mutex_); |
| 101 | + |
| 102 | + // 等待有数据或超时 |
| 103 | + cv_.wait_for(lock, std::chrono::milliseconds(config_.batch_timeout_ms), |
| 104 | + [this]() { |
| 105 | + return !pending_items_.empty() || |
| 106 | + !running_.load(std::memory_order_relaxed); |
| 107 | + }); |
| 108 | + |
| 109 | + if (!running_.load(std::memory_order_relaxed)) { |
| 110 | + // 处理剩余请求 |
| 111 | + if (!pending_items_.empty()) { |
| 112 | + FlushBatch(); |
| 113 | + } |
| 114 | + break; |
| 115 | + } |
| 116 | + |
| 117 | + if (!pending_items_.empty()) { |
| 118 | + FlushBatch(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + LOG(INFO) << "BatchManager worker thread stopped"; |
| 123 | +} |
| 124 | + |
| 125 | +void BatchManager::FlushBatch() { |
| 126 | + // 假设调用者已持有锁 |
| 127 | + |
| 128 | + if (pending_items_.empty()) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + size_t batch_size = pending_items_.size(); |
| 133 | + |
| 134 | + // 合并所有日志数据 |
| 135 | + // 格式:<count>\n<log1_len>\n<log1_data><log2_len>\n<log2_data>... |
| 136 | + std::string batch_data; |
| 137 | + batch_data.reserve(batch_size * 100); // 预估大小 |
| 138 | + |
| 139 | + // 写入批次大小 |
| 140 | + batch_data.append(std::to_string(batch_size)); |
| 141 | + batch_data.append("\n"); |
| 142 | + |
| 143 | + // 收集所有 promise |
| 144 | + std::vector<std::shared_ptr<std::promise<rocksdb::Status>>> promises; |
| 145 | + promises.reserve(batch_size); |
| 146 | + |
| 147 | + for (auto& item : pending_items_) { |
| 148 | + // 写入单个日志长度和数据 |
| 149 | + batch_data.append(std::to_string(item.log_data.size())); |
| 150 | + batch_data.append("\n"); |
| 151 | + batch_data.append(item.log_data); |
| 152 | + |
| 153 | + promises.push_back(std::move(item.promise)); |
| 154 | + } |
| 155 | + |
| 156 | + pending_items_.clear(); |
| 157 | + |
| 158 | + // 创建批量 closure |
| 159 | + auto* closure = new BatchClosure(std::move(promises)); |
| 160 | + |
| 161 | + // 提交到 Raft |
| 162 | + braft::Task task; |
| 163 | + butil::IOBuf buf; |
| 164 | + buf.append(batch_data); |
| 165 | + task.data = &buf; |
| 166 | + task.done = closure; |
| 167 | + |
| 168 | + raft_node_->apply(task); |
| 169 | + |
| 170 | + total_batches_.fetch_add(1, std::memory_order_relaxed); |
| 171 | + |
| 172 | + VLOG(1) << "Flushed batch of " << batch_size << " requests to Raft"; |
| 173 | +} |
| 174 | + |
| 175 | +BatchManager::Stats BatchManager::GetStats() const { |
| 176 | + Stats stats; |
| 177 | + stats.total_requests = total_requests_.load(std::memory_order_relaxed); |
| 178 | + stats.total_batches = total_batches_.load(std::memory_order_relaxed); |
| 179 | + |
| 180 | + if (stats.total_batches > 0) { |
| 181 | + stats.avg_batch_size = static_cast<double>(stats.total_requests) / stats.total_batches; |
| 182 | + } |
| 183 | + |
| 184 | + std::lock_guard<std::mutex> lock(const_cast<std::mutex&>(mutex_)); |
| 185 | + stats.pending_count = pending_items_.size(); |
| 186 | + |
| 187 | + return stats; |
| 188 | +} |
| 189 | + |
| 190 | +} // namespace pika_raft |
| 191 | + |
0 commit comments