forked from UWQuickstep/quickstep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryManagerBase.hpp
299 lines (260 loc) · 9.98 KB
/
QueryManagerBase.hpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* 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.
**/
#ifndef QUICKSTEP_QUERY_EXECUTION_QUERY_MANAGER_BASE_HPP_
#define QUICKSTEP_QUERY_EXECUTION_QUERY_MANAGER_BASE_HPP_
#include <cstddef>
#include <memory>
#include <unordered_set>
#include <vector>
#include "catalog/CatalogTypedefs.hpp"
#include "query_execution/QueryExecutionState.hpp"
#include "query_optimizer/QueryHandle.hpp"
#include "relational_operators/RelationalOperator.hpp"
#include "relational_operators/WorkOrder.hpp"
#include "storage/StorageBlockInfo.hpp"
#include "utility/DAG.hpp"
#include "utility/ExecutionDAGVisualizer.hpp"
#include "utility/Macros.hpp"
namespace quickstep {
/** \addtogroup QueryExecution
* @{
*/
/**
* @brief A base class that manages the execution of a query including
* generation of new work orders, and keeping track of the query
* exection state.
**/
class QueryManagerBase {
public:
typedef DAG<RelationalOperator, bool>::size_type_nodes dag_node_index;
/**
* @brief Return codes for queryStatus() function.
*
* @note When both operator and query get executed, kQueryExecuted takes
* precedence over kOperatorExecuted.
**/
enum class QueryStatusCode {
kOperatorExecuted = 0, // An operator in the query finished execution.
kQueryExecuted, // The query got executed.
kNone // None of the above.
};
/**
* @brief Constructor.
*
* @param query_handle The QueryHandle object for this query.
**/
explicit QueryManagerBase(QueryHandle *query_handle);
/**
* @brief Virtual destructor.
**/
virtual ~QueryManagerBase() {}
/**
* @brief Get the query handle.
**/
const QueryHandle* query_handle() const {
return query_handle_.get();
}
/**
* @brief Get the QueryExecutionState for this query.
**/
inline const QueryExecutionState& getQueryExecutionState() const {
return *query_exec_state_;
}
/**
* @brief Process the received WorkOrder complete message.
*
* @param op_index The index of the specified operator node in the query DAG
* for the completed WorkOrder.
* @param part_id The partition id.
**/
void processWorkOrderCompleteMessage(const dag_node_index op_index,
const partition_id part_id);
/**
* @brief Process the received RebuildWorkOrder complete message.
*
* @param op_index The index of the specified operator node in the query DAG
* for the completed RebuildWorkOrder.
* @param part_id The partition id.
**/
void processRebuildWorkOrderCompleteMessage(const dag_node_index op_index,
const partition_id part_id);
/**
* @brief Process the received data pipeline message.
*
* @param op_index The index of the specified operator node in the query DAG
* for the pipelining block.
* @param block The block id.
* @param rel_id The ID of the relation that produced 'block'.
* @param part_id The partition ID of 'block', if any. By default, a block
* blongs to the only partition (aka, no partition).
**/
void processDataPipelineMessage(const dag_node_index op_index,
const block_id block,
const relation_id rel_id,
const partition_id part_id = 0);
/**
* @brief Fetch all work orders currently available in relational operator and
* store them internally.
*
* @param index The index of the relational operator to be processed in the
* query plan DAG.
*
* @return Whether any work order was generated by op.
**/
virtual bool fetchNormalWorkOrders(const dag_node_index index) = 0;
/**
* @brief Process the received work order feedback message and notify
* relational operator.
*
* @param op_index The index of the specified operator node in the query DAG
* for the feedback message.
* @param message Feedback message from work order.
**/
void processFeedbackMessage(const dag_node_index op_index,
const WorkOrder::FeedbackMessage &message);
/**
* @brief Get the query status after processing an incoming message.
*
* @param op_index The index of the specified operator node in the query DAG
* for the incoming message.
*
* @return QueryStatusCode as determined after the message is processed.
**/
QueryStatusCode queryStatus(const dag_node_index op_index);
/**
* @brief Get the execution DAG visualizer.
*
* @return the execution DAG visualizer.
**/
ExecutionDAGVisualizer* dag_visualizer() {
return dag_visualizer_.get();
}
protected:
/**
* @brief This function does the following things:
* 1. Mark the given relational operator as "done".
* 2. For all the dependents of this operator, check if the given
* operator is done producing output. If it's done, inform the
* dependents that they won't receive input anymore from the given
* operator.
* 3. Check if all of their blocking dependencies are met. If so
* fetch normal work orders.
*
* @param index The index of the given relational operator in the DAG.
**/
void markOperatorFinished(const dag_node_index index);
/**
* @brief Check if all the blocking dependencies of the node at specified
* index have finished their execution.
*
* @note A blocking dependency is the one which is pipeline breaker. Output of
* a dependency can't be streamed to its dependent if the link between
* them is pipeline breaker.
*
* @param node_index The index of the specified node in the query DAG.
*
* @return True if all the blocking dependencies have finished their
* execution. False otherwise.
**/
inline bool checkAllBlockingDependenciesMet(
const dag_node_index node_index) const {
return blocking_dependencies_[node_index].empty();
}
/**
* @brief Check if the rebuild operation is required for a given operator.
*
* @param index The index of the given operator in the DAG.
*
* @return True if the rebuild operation is required, false otherwise.
**/
inline bool checkRebuildRequired(const dag_node_index index) const {
return query_exec_state_->isRebuildRequired(index);
}
/**
* @brief Check if the rebuild operation for a given operator has been
* initiated.
*
* @param index The index of the given operator in the DAG.
*
* @return True if the rebuild operation has been initiated, false otherwise.
**/
inline bool checkRebuildInitiated(const dag_node_index index) const {
return query_exec_state_->hasRebuildInitiated(index);
}
/**
* @brief Get the query's current memory consumption in bytes.
*
* @note This method returns a best guess consumption, at the time of the call.
**/
virtual std::size_t getQueryMemoryConsumptionBytes() const {
return 0;
}
std::unique_ptr<QueryHandle> query_handle_;
const std::size_t query_id_;
DAG<RelationalOperator, bool> *query_dag_; // Owned by 'query_handle_'.
const dag_node_index num_operators_in_dag_;
// For all nodes, store their receiving dependents.
std::vector<std::vector<dag_node_index>> output_consumers_;
// For all nodes, store their pipeline breaking dependencies (if any).
std::vector<std::unordered_set<dag_node_index>> blocking_dependencies_;
std::vector<dag_node_index> non_dependent_operators_;
std::unique_ptr<QueryExecutionState> query_exec_state_;
std::unique_ptr<ExecutionDAGVisualizer> dag_visualizer_;
private:
/**
* @brief Check if the given operator's normal execution is over.
*
* @note The conditions for a given operator's normal execution to get over:
* 1. All of its normal (i.e. non rebuild) WorkOrders have finished
* execution.
* 2. The operator is done generating work orders.
* 3. All of the dependencies of the given operator have been met.
*
* @param index The index of the given operator in the DAG.
*
* @return True if the normal execution of the given operator is over, false
* otherwise.
**/
virtual bool checkNormalExecutionOver(const dag_node_index index) const = 0;
/**
* @brief Initiate the rebuild process for partially filled blocks generated
* during the execution of the given operator.
*
* @param index The index of the given operator in the DAG.
*
* @return True if the rebuild is over immediately, i.e. the operator didn't
* generate any rebuild WorkOrders, false otherwise.
**/
virtual bool initiateRebuild(const dag_node_index index) = 0;
/**
* @brief Check if the rebuild operation for a given operator is over.
*
* @param index The index of the given operator in the DAG.
*
* @return True if the rebuild operation is over, false otherwise.
**/
virtual bool checkRebuildOver(const dag_node_index index) const = 0;
// For all nodes, store their pipeline breaking dependents (if any).
std::vector<std::vector<dag_node_index>> blocking_dependents_;
std::vector<std::unordered_set<dag_node_index>> non_blocking_dependencies_;
DISALLOW_COPY_AND_ASSIGN(QueryManagerBase);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_QUERY_EXECUTION_QUERY_MANAGER_BASE_HPP_