forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult_queue_mgr.cpp
More file actions
119 lines (106 loc) · 4.36 KB
/
Copy pathresult_queue_mgr.cpp
File metadata and controls
119 lines (106 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "runtime/result_queue_mgr.h"
#include <gen_cpp/Types_types.h>
#include <utility>
#include "common/config.h"
#include "common/metrics/doris_metrics.h"
#include "common/metrics/metrics.h"
#include "common/status.h"
#include "runtime/record_batch_queue.h"
#include "util/hash_util.hpp"
namespace doris {
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(result_block_queue_count, MetricUnit::NOUNIT);
ResultQueueMgr::ResultQueueMgr() {
// Each BlockingQueue has a limited size (default 20, by config::max_memory_sink_batch_count),
// it's not needed to count the actual size of all BlockingQueue.
REGISTER_HOOK_METRIC(result_block_queue_count, [this]() {
// std::lock_guard<std::mutex> l(_lock);
return _fragment_queue_map.size();
});
}
ResultQueueMgr::~ResultQueueMgr() {
DEREGISTER_HOOK_METRIC(result_block_queue_count);
}
Status ResultQueueMgr::fetch_result(const TUniqueId& fragment_instance_id,
std::shared_ptr<arrow::RecordBatch>* result, bool* eos) {
BlockQueueSharedPtr queue;
{
std::lock_guard<std::mutex> l(_lock);
auto iter = _fragment_queue_map.find(fragment_instance_id);
if (_fragment_queue_map.end() != iter) {
queue = iter->second;
} else {
return Status::InternalError("fragment_instance_id does not exists");
}
}
// check queue status before get result
RETURN_IF_ERROR(queue->status());
bool success = queue->blocking_get(result);
if (success) {
// sentinel nullptr indicates scan end
if (*result == nullptr) {
*eos = true;
// put sentinel for consistency, avoid repeated invoking fetch result when have no rowbatch
queue->blocking_put(nullptr);
} else {
*eos = false;
}
} else {
*eos = true;
}
return Status::OK();
}
void ResultQueueMgr::create_queue(const TUniqueId& fragment_instance_id,
BlockQueueSharedPtr* queue) {
std::lock_guard<std::mutex> l(_lock);
auto iter = _fragment_queue_map.find(fragment_instance_id);
if (iter != _fragment_queue_map.end()) {
*queue = iter->second;
} else {
// max_elements will not take effect, because when queue size reaches max_memory_sink_batch_count,
// MemoryScratchSink will block queue dependency, in this way, one queue have 20 * 1024 rows at most.
// use MemoryScratchSink queue dependency instead of BlockingQueue to achieve blocking.
BlockQueueSharedPtr tmp(new RecordBatchQueue(config::max_memory_sink_batch_count * 2));
_fragment_queue_map.insert(std::make_pair(fragment_instance_id, tmp));
*queue = tmp;
}
}
Status ResultQueueMgr::cancel(const TUniqueId& fragment_instance_id) {
std::lock_guard<std::mutex> l(_lock);
auto iter = _fragment_queue_map.find(fragment_instance_id);
if (iter != _fragment_queue_map.end()) {
// first remove RecordBatch from queue
// avoid MemoryScratchSink block on send or close operation
iter->second->shutdown();
// remove this queue from map
_fragment_queue_map.erase(fragment_instance_id);
}
return Status::OK();
}
void ResultQueueMgr::update_queue_status(const TUniqueId& fragment_instance_id,
const Status& status) {
if (status.ok()) {
return;
}
std::lock_guard<std::mutex> l(_lock);
auto iter = _fragment_queue_map.find(fragment_instance_id);
if (iter != _fragment_queue_map.end()) {
iter->second->update_status(status);
}
}
} // namespace doris