3636 * 3. Send commands in batches to the consensus coordinator with batch-level synchronization
3737 * 4. Support asynchronous callback notification of command processing results
3838 * 5. Track performance metrics for batch processing
39- * 6. Provide intelligent retry mechanisms for failed batches
39+
4040*/
4141class PikaCommandCollector {
4242 public:
@@ -46,14 +46,13 @@ class PikaCommandCollector {
4646 /* *
4747 * @brief constructor
4848 * @param coordinator consensus coordinator reference
49- * @param batch_size batch size (number of commands)
50- * @param batch_max_wait_time forced flush interval (milliseconds)
49+ * @param batch_max_wait_time maximum wait time in milliseconds
5150 */
5251 // Constructor with raw pointer (original)
53- PikaCommandCollector (ConsensusCoordinator* coordinator, size_t batch_size = 100 , int batch_max_wait_time = 5 );
52+ PikaCommandCollector (ConsensusCoordinator* coordinator, int batch_max_wait_time = 5 );
5453
5554 // Constructor with shared_ptr (for compatibility with make_shared calls)
56- PikaCommandCollector (std::shared_ptr<ConsensusCoordinator> coordinator, size_t batch_size = 100 , int batch_max_wait_time = 5 );
55+ PikaCommandCollector (std::shared_ptr<ConsensusCoordinator> coordinator, int batch_max_wait_time = 5 );
5756
5857 ~PikaCommandCollector ();
5958
@@ -65,31 +64,6 @@ class PikaCommandCollector {
6564 */
6665 bool AddCommand (const std::shared_ptr<Cmd>& cmd_ptr, CommandCallback callback);
6766
68- /* *
69- * @brief Called periodically by external systems to process batches
70- * @param force Force processing even if batch is not full or timeout not reached
71- * @return Number of commands processed
72- */
73-
74- /* *
75- * @brief Immediately process all currently collected commands
76- * @return The number of commands processed
77- */
78- size_t FlushCommands (bool force = false );
79-
80-
81- /* *
82- * @brief Get the current number of pending commands
83- * @return number of commands
84- */
85- size_t PendingCommands () const ;
86-
87- /* *
88- * @brief Set the batch size
89- * @param batch_size batch size
90- */
91- void SetBatchSize (size_t batch_size);
92-
9367 /* *
9468 * @brief Set the batch max wait time
9569 * @param batch_max_wait_time maximum wait time in milliseconds
@@ -101,91 +75,17 @@ class PikaCommandCollector {
10175 * @return Pair of (total_processed_commands, total_batches)
10276 */
10377 std::pair<uint64_t , uint64_t > GetBatchStats () const ;
104-
105- /* *
106- * @brief Get average batch processing time in milliseconds
107- * @return Average processing time or nullopt if no batches processed
108- */
109- std::optional<double > GetAverageBatchTime () const ;
110-
111- private:
112-
113- /* *
114- * @brief batch processing command
115- * @param batch command batch
116- * @return Whether the processing is successful
117- */
118- pstd::Status ProcessBatch (const std::vector<std::shared_ptr<Cmd>>& commands,
119- const std::vector<CommandCallback>& callbacks);
120-
121- /* *
122- * @brief Check for conflicts based on command type and key name
123- * @param cmd_ptr command pointer
124- * @return true if there is a conflict (should be replaced), false if there is no conflict
125- */
126- bool CheckConflict (const std::shared_ptr<Cmd>& cmd_ptr) const ;
127-
128- /* *
129- * @brief Handle key conflicts and remove conflicting commands
130- * @param cmd_ptr new command
131- */
132- void HandleConflict (const std::shared_ptr<Cmd>& cmd_ptr);
133-
134- /* *
135- * @brief Retry batch processing commands
136- * @param commands List of commands to retry
137- * @param callbacks Corresponding callback function list
138- * @param priority Priority level for the retry (higher means more urgent)
139- * @return Whether the commands were successfully requeued
140- */
141- bool RetryBatch (const std::vector<std::shared_ptr<Cmd>>& commands,
142- const std::vector<CommandCallback>& callbacks,
143- int priority = 100 );
14478
14579 private:
14680 // Consensus coordinator reference
14781 ConsensusCoordinator* coordinator_;
14882
14983 // Batch processing configuration
150- std::atomic<size_t > batch_size_;
15184 std::atomic<int > batch_max_wait_time_;
15285
153- // Retry configuration
154- std::atomic<int > max_retry_attempts_{3 };
155- std::atomic<int > retry_backoff_ms_{50 };
156-
157- // Command collection and processing
158- mutable std::mutex mutex_;
159-
160- // Pending command queue and callbacks
161- std::list<std::pair<std::shared_ptr<Cmd>, CommandCallback>> pending_commands_;
162-
163- // Priority queue for retries
164- std::deque<std::tuple<int , std::vector<std::shared_ptr<Cmd>>, std::vector<CommandCallback>>> retry_queue_;
165-
166- // Command key mapping, used to handle same-key conflicts
167- std::unordered_map<std::string, std::list<std::pair<std::shared_ptr<Cmd>, CommandCallback>>::iterator> key_map_;
168-
16986 // Batch statistics
17087 std::atomic<uint64_t > total_processed_{0 };
17188 std::atomic<uint64_t > total_batches_{0 };
172- std::atomic<uint64_t > total_retries_{0 };
173- std::atomic<uint64_t > total_conflicts_{0 };
174- std::atomic<uint64_t > total_batch_time_ms_{0 };
175- std::chrono::time_point<std::chrono::steady_clock> batch_start_time_;
176-
177- // Performance tracking
178- struct BatchMetrics {
179- uint64_t batch_size;
180- uint64_t processing_time_ms;
181- uint64_t wait_time_ms;
182- bool successful;
183- };
184-
185- // Circular buffer for recent batch metrics
186- static constexpr size_t kMetricsBufferSize = 100 ;
187- std::vector<BatchMetrics> recent_metrics_;
188- std::mutex metrics_mutex_;
18989};
19090
19191#endif // PIKA_COMMAND_COLLECTOR_H_
0 commit comments