19
19
#include " presto_cpp/main/thrift/ThriftIO.h"
20
20
#include " presto_cpp/main/thrift/gen-cpp2/PrestoThrift.h"
21
21
#include " presto_cpp/main/types/PrestoToVeloxQueryPlan.h"
22
+ #include " presto_cpp/main/thrift/experimental/ThriftUtils.h"
22
23
23
24
namespace facebook ::presto {
24
25
@@ -210,25 +211,35 @@ proxygen::RequestHandler* TaskResource::createOrUpdateTaskImpl(
210
211
const protocol::TaskId& taskId,
211
212
const std::string& updateJson,
212
213
const bool summarize,
213
- long startProcessCpuTime)>& createOrUpdateFunc) {
214
+ long startProcessCpuTime,
215
+ bool receiveThrift)>& createOrUpdateFunc) {
214
216
protocol::TaskId taskId = pathMatch[1 ];
215
217
bool summarize = message->hasQueryParam (" summarize" );
218
+
219
+ auto & headers = message->getHeaders ();
220
+ auto acceptHeader = headers.getSingleOrEmpty (proxygen::HTTP_HEADER_ACCEPT);
221
+ auto sendThrift =
222
+ acceptHeader.find (http::kMimeTypeApplicationThrift ) != std::string::npos;
223
+ auto contentHeader = headers.getSingleOrEmpty (proxygen::HTTP_HEADER_CONTENT_TYPE);
224
+ auto receiveThrift =
225
+ contentHeader.find (http::kMimeTypeApplicationThrift ) != std::string::npos;
226
+
216
227
return new http::CallbackRequestHandler (
217
- [this , taskId, summarize, createOrUpdateFunc](
228
+ [this , taskId, summarize, createOrUpdateFunc, sendThrift, receiveThrift ](
218
229
proxygen::HTTPMessage* /* message*/ ,
219
230
const std::vector<std::unique_ptr<folly::IOBuf>>& body,
220
231
proxygen::ResponseHandler* downstream,
221
232
std::shared_ptr<http::CallbackRequestHandlerState> handlerState) {
222
233
folly::via (
223
234
httpSrvCpuExecutor_,
224
- [this , &body, taskId, summarize, createOrUpdateFunc]() {
235
+ [this , &body, taskId, summarize, createOrUpdateFunc, receiveThrift ]() {
225
236
const auto startProcessCpuTimeNs = util::getProcessCpuTimeNs ();
226
237
std::string updateJson = util::extractMessageBody (body);
227
238
228
239
std::unique_ptr<protocol::TaskInfo> taskInfo;
229
240
try {
230
241
taskInfo = createOrUpdateFunc (
231
- taskId, updateJson, summarize, startProcessCpuTimeNs);
242
+ taskId, updateJson, summarize, startProcessCpuTimeNs, receiveThrift );
232
243
} catch (const velox::VeloxException& e) {
233
244
// Creating an empty task, putting errors inside so that next
234
245
// status fetch from coordinator will catch the error and well
@@ -243,12 +254,19 @@ proxygen::RequestHandler* TaskResource::createOrUpdateTaskImpl(
243
254
throw ;
244
255
}
245
256
}
246
- return json (* taskInfo) ;
257
+ return taskInfo;
247
258
})
248
259
.via (folly::EventBaseManager::get ()->getEventBase ())
249
- .thenValue ([downstream, handlerState]( auto && taskInfoJson ) {
260
+ .thenValue ([downstream, handlerState, sendThrift](std::unique_ptr<protocol::TaskInfo> taskInfo ) {
250
261
if (!handlerState->requestExpired ()) {
251
- http::sendOkResponse (downstream, taskInfoJson);
262
+ if (sendThrift) {
263
+ protocol::cpp2::TaskInfo thriftTaskInfo;
264
+ protocol::cpp2::toThrift (*taskInfo, thriftTaskInfo);
265
+ http::sendOkThriftResponse (
266
+ downstream, thriftWrite (thriftTaskInfo));
267
+ } else {
268
+ http::sendOkResponse (downstream, json (*taskInfo));
269
+ }
252
270
}
253
271
})
254
272
.thenError (
@@ -277,7 +295,8 @@ proxygen::RequestHandler* TaskResource::createOrUpdateBatchTask(
277
295
[&](const protocol::TaskId& taskId,
278
296
const std::string& updateJson,
279
297
const bool summarize,
280
- long startProcessCpuTime) {
298
+ long startProcessCpuTime,
299
+ bool receiveThrift) {
281
300
protocol::BatchTaskUpdateRequest batchUpdateRequest =
282
301
json::parse (updateJson);
283
302
auto updateRequest = batchUpdateRequest.taskUpdateRequest ;
@@ -329,13 +348,25 @@ proxygen::RequestHandler* TaskResource::createOrUpdateTask(
329
348
[&](const protocol::TaskId& taskId,
330
349
const std::string& updateJson,
331
350
const bool summarize,
332
- long startProcessCpuTime) {
333
- protocol::TaskUpdateRequest updateRequest = json::parse (updateJson);
351
+ long startProcessCpuTime,
352
+ bool receiveThrift) {
353
+ protocol::TaskUpdateRequest updateRequest;
354
+ if (receiveThrift) {
355
+ auto thriftTaskUpdateRequest = std::make_shared<protocol::cpp2::TaskUpdateRequest>();
356
+ thriftRead (updateJson, thriftTaskUpdateRequest);
357
+ protocol::cpp2::fromThrift (*thriftTaskUpdateRequest, updateRequest);
358
+ } else {
359
+ updateRequest = json::parse (updateJson);
360
+ }
334
361
velox::core::PlanFragment planFragment;
335
362
std::shared_ptr<velox::core::QueryCtx> queryCtx;
336
363
if (updateRequest.fragment ) {
337
- auto fragment =
338
- velox::encoding::Base64::decode (*updateRequest.fragment );
364
+ std::string fragment;
365
+ if (receiveThrift) {
366
+ fragment = *updateRequest.fragment ;
367
+ } else {
368
+ fragment = velox::encoding::Base64::decode (*updateRequest.fragment );
369
+ }
339
370
protocol::PlanFragment prestoPlan = json::parse (fragment);
340
371
341
372
queryCtx =
@@ -511,11 +542,11 @@ proxygen::RequestHandler* TaskResource::getTaskStatus(
511
542
512
543
auto & headers = message->getHeaders ();
513
544
auto acceptHeader = headers.getSingleOrEmpty (proxygen::HTTP_HEADER_ACCEPT);
514
- auto useThrift =
545
+ auto sendThrift =
515
546
acceptHeader.find (http::kMimeTypeApplicationThrift ) != std::string::npos;
516
547
517
548
return new http::CallbackRequestHandler (
518
- [this , useThrift , taskId, currentState, maxWait](
549
+ [this , sendThrift , taskId, currentState, maxWait](
519
550
proxygen::HTTPMessage* /* message*/ ,
520
551
const std::vector<std::unique_ptr<folly::IOBuf>>& /* body*/ ,
521
552
proxygen::ResponseHandler* downstream,
@@ -525,7 +556,7 @@ proxygen::RequestHandler* TaskResource::getTaskStatus(
525
556
httpSrvCpuExecutor_,
526
557
[this ,
527
558
evb,
528
- useThrift ,
559
+ sendThrift ,
529
560
taskId,
530
561
currentState,
531
562
maxWait,
@@ -535,12 +566,12 @@ proxygen::RequestHandler* TaskResource::getTaskStatus(
535
566
.getTaskStatus (taskId, currentState, maxWait, handlerState)
536
567
.via (evb)
537
568
.thenValue (
538
- [useThrift , downstream, taskId, handlerState](
569
+ [sendThrift , downstream, taskId, handlerState](
539
570
std::unique_ptr<protocol::TaskStatus> taskStatus) {
540
571
if (!handlerState->requestExpired ()) {
541
- if (useThrift ) {
542
- thrift ::TaskStatus thriftTaskStatus;
543
- toThrift (*taskStatus, thriftTaskStatus);
572
+ if (sendThrift ) {
573
+ protocol::cpp2 ::TaskStatus thriftTaskStatus;
574
+ protocol::cpp2:: toThrift (*taskStatus, thriftTaskStatus);
544
575
http::sendOkThriftResponse (
545
576
downstream, thriftWrite (thriftTaskStatus));
546
577
} else {
0 commit comments