|
22 | 22 | #include <gen_cpp/FrontendService_types.h> |
23 | 23 | #include <glog/logging.h> |
24 | 24 |
|
| 25 | +#include <algorithm> |
25 | 26 | #include <string> |
26 | 27 | #include <utility> |
27 | 28 |
|
28 | 29 | #include "common/compiler_util.h" // IWYU pragma: keep |
| 30 | +#include "common/config.h" |
29 | 31 | #include "common/status.h" |
30 | 32 | #include "core/block/block.h" |
31 | 33 | #include "runtime/runtime_state.h" |
32 | 34 | #include "storage/tablet_info.h" |
33 | 35 |
|
34 | 36 | namespace doris { |
| 37 | + |
| 38 | +void AdaptiveRandomBucketState::init_partition(int64_t partition_id, |
| 39 | + const std::vector<int64_t>& tablets, |
| 40 | + const std::vector<int32_t>& bucket_seqs, |
| 41 | + int32_t start_tablet_idx) { |
| 42 | + if (partition_id < 0 || tablets.empty()) { |
| 43 | + return; |
| 44 | + } |
| 45 | + std::lock_guard<std::mutex> lock(_mutex); |
| 46 | + if (_partition_states.contains(partition_id)) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + PartitionState state; |
| 51 | + state.partition_id = partition_id; |
| 52 | + state.tablets = tablets; |
| 53 | + state.bucket_seqs = bucket_seqs; |
| 54 | + if (start_tablet_idx >= 0 && start_tablet_idx < state.tablets.size()) { |
| 55 | + state.tablet_pos = start_tablet_idx; |
| 56 | + } |
| 57 | + state.current_tablet_id = state.tablets[state.tablet_pos]; |
| 58 | + |
| 59 | + for (int32_t tablet_pos = 0; tablet_pos < state.tablets.size(); ++tablet_pos) { |
| 60 | + _tablet_to_partition[state.tablets[tablet_pos]] = partition_id; |
| 61 | + _tablet_to_bucket[state.tablets[tablet_pos]] = tablet_pos; |
| 62 | + } |
| 63 | + _partition_states.emplace(partition_id, std::move(state)); |
| 64 | + LOG(INFO) << "FIND_TABLET_RANDOM_BUCKET: load_id=" << _load_id << ", partition=" << partition_id |
| 65 | + << ", local tablet count=" << tablets.size() |
| 66 | + << ", start tablet=" << _partition_states.at(partition_id).current_tablet_id; |
| 67 | +} |
| 68 | + |
| 69 | +int64_t AdaptiveRandomBucketState::current_tablet(int64_t partition_id) { |
| 70 | + std::lock_guard<std::mutex> lock(_mutex); |
| 71 | + auto it = _partition_states.find(partition_id); |
| 72 | + if (it == _partition_states.end()) { |
| 73 | + return -1; |
| 74 | + } |
| 75 | + return it->second.current_tablet_id; |
| 76 | +} |
| 77 | + |
| 78 | +void AdaptiveRandomBucketState::rotate_by_tablet(int64_t tablet_id) { |
| 79 | + if (!config::enable_adaptive_random_bucket_load_bucket_rotation) { |
| 80 | + return; |
| 81 | + } |
| 82 | + std::lock_guard<std::mutex> lock(_mutex); |
| 83 | + auto partition_it = _tablet_to_partition.find(tablet_id); |
| 84 | + if (partition_it == _tablet_to_partition.end()) { |
| 85 | + return; |
| 86 | + } |
| 87 | + auto state_it = _partition_states.find(partition_it->second); |
| 88 | + if (state_it == _partition_states.end()) { |
| 89 | + return; |
| 90 | + } |
| 91 | + auto bucket_it = _tablet_to_bucket.find(tablet_id); |
| 92 | + if (bucket_it == _tablet_to_bucket.end()) { |
| 93 | + return; |
| 94 | + } |
| 95 | + auto& state = state_it->second; |
| 96 | + if (bucket_it->second != state.tablet_pos) { |
| 97 | + return; |
| 98 | + } |
| 99 | + int32_t next_pos = (state.tablet_pos + 1) % static_cast<int32_t>(state.tablets.size()); |
| 100 | + LOG(INFO) << "FIND_TABLET_RANDOM_BUCKET: load_id=" << _load_id |
| 101 | + << ", partition=" << state.partition_id << " shared rotate tablet " |
| 102 | + << state.current_tablet_id << " -> " << state.tablets[next_pos] |
| 103 | + << " after tablet=" << tablet_id << " memtable flushed"; |
| 104 | + state.tablet_pos = next_pos; |
| 105 | + state.current_tablet_id = state.tablets[next_pos]; |
| 106 | +} |
| 107 | + |
35 | 108 | Status OlapTabletFinder::find_tablets(RuntimeState* state, Block* block, int rows, |
36 | 109 | std::vector<VOlapTablePartition*>& partitions, |
37 | 110 | std::vector<uint32_t>& tablet_index, std::vector<bool>& skip, |
@@ -82,8 +155,11 @@ Status OlapTabletFinder::find_tablets(RuntimeState* state, Block* block, int row |
82 | 155 |
|
83 | 156 | if (_find_tablet_mode == FindTabletMode::FIND_TABLET_EVERY_ROW) { |
84 | 157 | _vpartition->find_tablets(block, qualified_rows, partitions, tablet_index); |
| 158 | + } else if (_find_tablet_mode == FindTabletMode::FIND_TABLET_RANDOM_BUCKET) { |
| 159 | + // Receiver-side random bucket mode only needs partition ids on sender side. |
| 160 | + // The receiver decides the concrete tablet from its local ordered tablet list. |
85 | 161 | } else { |
86 | | - // for random distribution |
| 162 | + // FIND_TABLET_EVERY_BATCH / FIND_TABLET_EVERY_SINK |
87 | 163 | _vpartition->find_tablets(block, qualified_rows, partitions, tablet_index, |
88 | 164 | &_partition_to_tablet_map); |
89 | 165 | if (_find_tablet_mode == FindTabletMode::FIND_TABLET_EVERY_BATCH) { |
|
0 commit comments