forked from Hacker0912/quickstep-datalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkOrder.hpp
341 lines (299 loc) · 9.99 KB
/
WorkOrder.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* 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_RELATIONAL_OPERATORS_WORK_UNIT_HPP_
#define QUICKSTEP_RELATIONAL_OPERATORS_WORK_UNIT_HPP_
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <vector>
#include "catalog/CatalogTypedefs.hpp"
#include "query_execution/QueryExecutionTypedefs.hpp"
#include "utility/Macros.hpp"
#include "glog/logging.h"
#include "tmb/address.h"
#include "tmb/id_typedefs.h"
#include "tmb/message_bus.h"
#include "tmb/message_style.h"
#include "tmb/tagged_message.h"
namespace quickstep {
/** \addtogroup RelationalOperators
* @{
*/
/**
* @brief A single unit of work in a query plan, produced by a
* RelationalOperator. Where possible, WorkOrders should be of
* single-block granularity to maximize the opportunity for parallelism.
**/
class WorkOrder {
public:
/**
* @brief Type of feedback message sent to relational operator.
*
* @note This is a per-operator type that is set and understood only by the
* relational operator.
*/
typedef std::uint16_t FeedbackMessageType;
/**
* @brief Header struct used in feedback message.
*
* @note This is a per-operator type that is set and understood only by the
* relational operator.
*/
struct FeedbackMessageHeader {
std::size_t query_id;
std::size_t rel_op_index;
std::size_t payload_size;
FeedbackMessageType payload_type;
/**
* @brief Header constructor.
*
* @param query_id The ID of the query.
* @param relational_op_index Index of the relation operator.
* @param payload_size Size of the payload of the message.
* @param payload_type Type of payload.
*/
FeedbackMessageHeader(const std::size_t query_id,
const std::size_t relational_op_index,
const std::size_t payload_size,
const FeedbackMessageType payload_type)
: query_id(query_id),
rel_op_index(relational_op_index),
payload_size(payload_size),
payload_type(payload_type) {}
};
/**
* @brief A generic tagged message that can be sent from work order to
* relational operator.
**/
class FeedbackMessage {
public:
/**
* @brief Feedback message constructor.
*
* @param type Type of the message.
* @param query_id The ID of the query.
* @param rel_op_index Relational operator index.
* @param payload Blob of payload.
* @param payload_size Size of the payload blob.
* @param ownership Whether to take ownership of the payload blob.
*/
FeedbackMessage(const FeedbackMessageType type,
const std::size_t query_id,
const std::size_t rel_op_index,
void *payload,
const std::size_t payload_size,
const bool ownership = true)
: header_(query_id, rel_op_index, payload_size, type),
payload_(payload),
ownership_(ownership) {}
/**
* @brief Deserializing feedback message constructor.
*
* @param serialized_bytes Serialized byte stream of feedback message.
* @param num_bytes Number of bytes in stream.
* @param copy_payload Copy payload and take ownership.
*/
FeedbackMessage(void *serialized_bytes,
const std::size_t num_bytes,
const bool copy_payload = false)
: header_(*static_cast<const FeedbackMessageHeader *>(serialized_bytes)),
ownership_(copy_payload) {
CHECK_EQ(num_bytes, sizeof(header_) + header_.payload_size);
if (copy_payload) {
payload_ = static_cast<char *>(std::malloc(header_.payload_size));
std::memcpy(payload_,
static_cast<const char *>(serialized_bytes) + sizeof(header_),
header_.payload_size);
} else {
payload_ = static_cast<char *>(serialized_bytes) + sizeof(header_);
}
}
/**
* @brief Move constructor.
*
* @param orig Message to be moved.
*/
FeedbackMessage(FeedbackMessage &&orig)
: header_(orig.header_),
payload_(orig.payload_),
ownership_(orig.ownership_) {
orig.header_.payload_size = 0;
orig.payload_ = nullptr;
}
/**
* @brief Move assignment.
*
* @param orig Message to be moved.
*/
FeedbackMessage& operator=(FeedbackMessage &&orig) {
if (this != &orig) {
if (ownership_) {
std::free(payload_);
}
header_ = orig.header_;
payload_ = orig.payload_;
ownership_ = orig.ownership_;
orig.payload_ = nullptr;
orig.header_.payload_size = 0;
}
return *this;
}
/**
* @brief Destructor.
*/
virtual ~FeedbackMessage() {
if (ownership_) {
std::free(payload_);
}
}
/**
* @brief Serialize the feedback message into a byte stream.
*
* @return A pair containing the message stream and the message size.
*
* @note The caller is responsible for freeing the byte stream.
*/
std::pair<void*, std::size_t> serializeMessage() const {
char *msg = static_cast<char *>(
std::malloc(sizeof(header_) + header_.payload_size));
CHECK(nullptr != msg) << "Unable to allocate byte stream.";
std::memcpy(msg, &header_, sizeof(header_));
std::memcpy(msg + sizeof(header_), payload_, header_.payload_size);
return std::make_pair(msg, sizeof(header_) + header_.payload_size);
}
/**
* @brief Message type accessor.
*/
FeedbackMessageType type() const { return header_.payload_type; }
/**
* @brief Header accessor.
*/
const FeedbackMessageHeader& header() const { return header_; }
/**
* @brief Payload accessor.
*/
const void* payload() const { return payload_; }
/**
* @brief Payload size accessor.
*/
std::size_t payload_size() const { return header_.payload_size; }
/**
* @brief Ownership.
*/
bool ownership() const { return ownership_; }
/**
* @brief Drop ownership.
*
* @note The entity calling this should take responsibility of freeing the
* payload.
*/
void dropOwnership() {
ownership_ = false;
}
private:
FeedbackMessageHeader header_;
void* payload_;
bool ownership_;
DISALLOW_COPY_AND_ASSIGN(FeedbackMessage);
};
/**
* @brief Virtual destructor.
**/
virtual ~WorkOrder() {}
/**
* @brief Run this WorkOrder in the calling thread.
**/
virtual void execute() = 0;
/**
* @brief Get the preferred NUMA node(s) where this WorkOrder should be
* executed.
*
* @return A vector of preferred NUMA nodes. An empty vector indicates that
* the WorkOrder can be executed on any NUMA node.
**/
const std::vector<int>& getPreferredNUMANodes() const {
return preferred_numa_nodes_;
}
/**
* @brief Send message to relational operator.
*
* @param bus A pointer to the TMB.
* @param sender_id The client ID of the sender.
* @param receiver_id The client ID of the receiver.
* @param feedback_msg Feedback message to be sent to relational operator.
**/
static void SendFeedbackMessage(tmb::MessageBus *bus,
tmb::client_id sender_id,
tmb::client_id receiver_id,
const FeedbackMessage &feedback_msg) {
std::pair<void *, std::size_t> stream = feedback_msg.serializeMessage();
tmb::TaggedMessage msg;
msg.acquire_message(stream.first, stream.second, kWorkOrderFeedbackMessage);
tmb::Address receiver_address;
receiver_address.AddRecipient(receiver_id);
tmb::MessageStyle single_receiver_style;
DCHECK(bus != nullptr);
DLOG(INFO) << "WorkOrder sent WorkOrderFeedbackMessage to Scheduler with Client " << receiver_id;
const tmb::MessageBus::SendStatus send_status =
bus->Send(sender_id,
receiver_address,
single_receiver_style,
std::move(msg));
CHECK(send_status == tmb::MessageBus::SendStatus::kOK);
}
/**
* @brief Get the ID of the query which this WorkOder belongs to.
**/
inline std::size_t getQueryID() const {
return query_id_;
}
/**
* @brief Get the partition id.
*
* @return The partition id.
*/
partition_id getPartitionId() const {
return partition_id_;
}
protected:
/**
* @brief Constructor.
*
* @param query_id The ID of the query to which this WorkOrder belongs.
* @param part_id The partition id.
**/
explicit WorkOrder(const std::size_t query_id,
const partition_id part_id = 0)
: query_id_(query_id),
partition_id_(part_id) {}
const std::size_t query_id_;
const partition_id partition_id_;
// A vector of preferred NUMA node IDs where this workorder should be executed.
// These node IDs typically indicate the NUMA node IDs of the input(s) of the
// workorder. Derived classes should ensure that there are no duplicate entries
// in this vector.
std::vector<int> preferred_numa_nodes_;
private:
DISALLOW_COPY_AND_ASSIGN(WorkOrder);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_RELATIONAL_OPERATORS_WORK_UNIT_HPP_