-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
grpc_server.cc
2591 lines (2312 loc) · 94.2 KB
/
grpc_server.cc
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "grpc_server.h"
#include <google/protobuf/arena.h>
#include <grpc++/alarm.h>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <fstream>
#include <list>
#include <map>
#include <mutex>
#include <queue>
#include <sstream>
#include <thread>
#include "../classification.h"
#include "../common.h"
#include "grpc++/grpc++.h"
#include "grpc++/security/server_credentials.h"
#include "grpc++/server.h"
#include "grpc++/server_builder.h"
#include "grpc++/server_context.h"
#include "grpc++/support/status.h"
#include "triton/common/logging.h"
#include "triton/common/table_printer.h"
#include "triton/core/tritonserver.h"
#define TRITONJSON_STATUSTYPE TRITONSERVER_Error*
#define TRITONJSON_STATUSRETURN(M) \
return TRITONSERVER_ErrorNew(TRITONSERVER_ERROR_INTERNAL, (M).c_str())
#define TRITONJSON_STATUSSUCCESS nullptr
#include "triton/common/triton_json.h"
#ifdef TRITON_ENABLE_TRACING
#include "../tracer.h"
#endif // TRITON_ENABLE_TRACING
#define REGISTER_GRPC_INFER_THREAD_COUNT 2
namespace triton { namespace server { namespace grpc {
namespace {
//
// The server has separate handling mechanisms for inference RPCs
// and non-inference RPCs.
//
//=========================================================================
// The following section contains the handling mechanism for non-inference
// RPCs. A single thread is created to handle all these requests as they
// are deemed to be not performance critical.
//=========================================================================
template <typename ResponderType, typename RequestType, typename ResponseType>
class CommonCallData : public ICallData {
public:
using StandardRegisterFunc = std::function<void(
::grpc::ServerContext*, RequestType*, ResponderType*, void*)>;
using StandardCallbackFunc =
std::function<void(RequestType&, ResponseType*, ::grpc::Status*)>;
CommonCallData(
const std::string& name, const uint64_t id,
const StandardRegisterFunc OnRegister,
const StandardCallbackFunc OnExecute, const bool async,
::grpc::ServerCompletionQueue* cq,
const std::pair<std::string, std::string>& restricted_kv,
const uint64_t& response_delay = 0)
: name_(name), id_(id), OnRegister_(OnRegister), OnExecute_(OnExecute),
async_(async), cq_(cq), responder_(&ctx_), step_(Steps::START),
restricted_kv_(restricted_kv), response_delay_(response_delay)
{
OnRegister_(&ctx_, &request_, &responder_, this);
LOG_VERBOSE(1) << "Ready for RPC '" << name_ << "', " << id_;
}
~CommonCallData()
{
if (async_thread_.joinable()) {
async_thread_.join();
}
}
bool Process(bool ok) override;
std::string Name() override { return name_; }
uint64_t Id() override { return id_; }
private:
void Execute();
void AddToCompletionQueue();
void WriteResponse();
bool ExecutePrecondition();
const std::string name_;
const uint64_t id_;
const StandardRegisterFunc OnRegister_;
const StandardCallbackFunc OnExecute_;
const bool async_;
::grpc::ServerCompletionQueue* cq_;
::grpc::ServerContext ctx_;
::grpc::Alarm alarm_;
ResponderType responder_;
RequestType request_;
ResponseType response_;
::grpc::Status status_;
std::thread async_thread_;
Steps step_;
std::pair<std::string, std::string> restricted_kv_{"", ""};
const uint64_t response_delay_;
};
template <typename ResponderType, typename RequestType, typename ResponseType>
bool
CommonCallData<ResponderType, RequestType, ResponseType>::Process(bool rpc_ok)
{
LOG_VERBOSE(1) << "Process for " << name_ << ", rpc_ok=" << rpc_ok << ", "
<< id_ << " step " << step_;
// If RPC failed on a new request then the server is shutting down
// and so we should do nothing (including not registering for a new
// request). If RPC failed on a non-START step then there is nothing
// we can do since we one execute one step.
const bool shutdown = (!rpc_ok && (step_ == Steps::START));
if (shutdown) {
if (async_thread_.joinable()) {
async_thread_.join();
}
step_ = Steps::FINISH;
}
if (step_ == Steps::START) {
// Start a new request to replace this one...
if (!shutdown) {
new CommonCallData<ResponderType, RequestType, ResponseType>(
name_, id_ + 1, OnRegister_, OnExecute_, async_, cq_, restricted_kv_,
response_delay_);
}
if (!async_) {
// For synchronous calls, execute and write response
// here.
Execute();
WriteResponse();
} else {
// For asynchronous calls, delegate the execution to another
// thread.
step_ = Steps::ISSUED;
async_thread_ = std::thread(&CommonCallData::Execute, this);
}
} else if (step_ == Steps::WRITEREADY) {
// Will only come here for asynchronous mode.
WriteResponse();
} else if (step_ == Steps::COMPLETE) {
step_ = Steps::FINISH;
}
return step_ != Steps::FINISH;
}
template <typename ResponderType, typename RequestType, typename ResponseType>
void
CommonCallData<ResponderType, RequestType, ResponseType>::Execute()
{
if (ExecutePrecondition()) {
OnExecute_(request_, &response_, &status_);
} else {
status_ = ::grpc::Status(
::grpc::StatusCode::UNAVAILABLE,
std::string("This protocol is restricted, expecting header '") +
restricted_kv_.first + "'");
}
step_ = Steps::WRITEREADY;
if (async_) {
// For asynchronous operation, need to add itself onto the completion
// queue so that the response can be written once the object is
// taken up next for execution.
AddToCompletionQueue();
}
}
template <typename ResponderType, typename RequestType, typename ResponseType>
bool
CommonCallData<ResponderType, RequestType, ResponseType>::ExecutePrecondition()
{
if (!restricted_kv_.first.empty()) {
const auto& metadata = ctx_.client_metadata();
const auto it = metadata.find(restricted_kv_.first);
return (it != metadata.end()) && (it->second == restricted_kv_.second);
}
return true;
}
template <typename ResponderType, typename RequestType, typename ResponseType>
void
CommonCallData<ResponderType, RequestType, ResponseType>::AddToCompletionQueue()
{
alarm_.Set(cq_, gpr_now(gpr_clock_type::GPR_CLOCK_REALTIME), this);
}
template <typename ResponderType, typename RequestType, typename ResponseType>
void
CommonCallData<ResponderType, RequestType, ResponseType>::WriteResponse()
{
if (response_delay_ != 0) {
// Will delay the write of the response by the specified time.
// This can be used to test the flow where there are other
// responses available to be written.
LOG_VERBOSE(1) << "Delaying the write of the response by "
<< response_delay_ << " seconds";
std::this_thread::sleep_for(std::chrono::seconds(response_delay_));
}
step_ = Steps::COMPLETE;
responder_.Finish(response_, status_, this);
}
//
// CommonHandler
//
// A common handler for all non-inference requests.
//
class CommonHandler : public HandlerBase {
public:
CommonHandler(
const std::string& name,
const std::shared_ptr<TRITONSERVER_Server>& tritonserver,
const std::shared_ptr<SharedMemoryManager>& shm_manager,
TraceManager* trace_manager,
inference::GRPCInferenceService::AsyncService* service,
::grpc::health::v1::Health::AsyncService* health_service,
::grpc::ServerCompletionQueue* cq,
const RestrictedFeatures& restricted_keys, const uint64_t response_delay);
// Descriptive name of of the handler.
const std::string& Name() const { return name_; }
// Start handling requests.
void Start() override;
// Stop handling requests.
void Stop() override;
private:
void SetUpAllRequests();
// [FIXME] turn into generated code
void RegisterServerLive();
void RegisterServerReady();
void RegisterHealthCheck();
void RegisterModelReady();
void RegisterServerMetadata();
void RegisterModelMetadata();
void RegisterModelConfig();
void RegisterModelStatistics();
void RegisterTrace();
void RegisterLogging();
void RegisterSystemSharedMemoryStatus();
void RegisterSystemSharedMemoryRegister();
void RegisterSystemSharedMemoryUnregister();
void RegisterCudaSharedMemoryStatus();
void RegisterCudaSharedMemoryRegister();
void RegisterCudaSharedMemoryUnregister();
void RegisterRepositoryIndex();
void RegisterRepositoryModelLoad();
void RegisterRepositoryModelUnload();
// Set count and cumulative duration for 'RegisterModelStatistics()'
template <typename PBTYPE>
TRITONSERVER_Error* SetStatisticsDuration(
triton::common::TritonJson::Value& statistics_json,
const std::string& statistics_name,
PBTYPE* mutable_statistics_duration_protobuf) const;
const std::string name_;
std::shared_ptr<TRITONSERVER_Server> tritonserver_;
std::shared_ptr<SharedMemoryManager> shm_manager_;
TraceManager* trace_manager_;
inference::GRPCInferenceService::AsyncService* service_;
::grpc::health::v1::Health::AsyncService* health_service_;
::grpc::ServerCompletionQueue* cq_;
std::unique_ptr<std::thread> thread_;
RestrictedFeatures restricted_keys_{};
const uint64_t response_delay_ = 0;
};
CommonHandler::CommonHandler(
const std::string& name,
const std::shared_ptr<TRITONSERVER_Server>& tritonserver,
const std::shared_ptr<SharedMemoryManager>& shm_manager,
TraceManager* trace_manager,
inference::GRPCInferenceService::AsyncService* service,
::grpc::health::v1::Health::AsyncService* health_service,
::grpc::ServerCompletionQueue* cq,
const RestrictedFeatures& restricted_keys,
const uint64_t response_delay = 0)
: name_(name), tritonserver_(tritonserver), shm_manager_(shm_manager),
trace_manager_(trace_manager), service_(service),
health_service_(health_service), cq_(cq),
restricted_keys_(restricted_keys), response_delay_(response_delay)
{
}
void
CommonHandler::Start()
{
// Use a barrier to make sure we don't return until thread has
// started.
auto barrier = std::make_shared<Barrier>(2);
thread_.reset(new std::thread([this, barrier] {
SetUpAllRequests();
barrier->Wait();
void* tag;
bool ok;
while (cq_->Next(&tag, &ok)) {
ICallData* call_data = static_cast<ICallData*>(tag);
if (!call_data->Process(ok)) {
LOG_VERBOSE(1) << "Done for " << call_data->Name() << ", "
<< call_data->Id();
delete call_data;
}
}
}));
barrier->Wait();
LOG_VERBOSE(1) << "Thread started for " << Name();
}
void
CommonHandler::Stop()
{
if (thread_->joinable()) {
thread_->join();
}
LOG_VERBOSE(1) << "Thread exited for " << Name();
}
void
CommonHandler::SetUpAllRequests()
{
// Define all the RPCs to be handled by this handler below
//
// Within each of the Register function, the format of RPC specification is:
// 1. A OnRegister function: This will be called when the
// server is ready to receive the requests for this RPC.
// 2. A OnExecute function: This will be called when the
// to process the request.
// 3. Create a CommonCallData object with the above callback
// functions
// health (GRPC standard)
RegisterHealthCheck();
// health (Triton)
RegisterServerLive();
RegisterServerReady();
RegisterModelReady();
// Metadata
RegisterServerMetadata();
RegisterModelMetadata();
// model config
RegisterModelConfig();
// shared memory
// system..
RegisterSystemSharedMemoryStatus();
RegisterSystemSharedMemoryRegister();
RegisterSystemSharedMemoryUnregister();
// cuda..
RegisterCudaSharedMemoryStatus();
RegisterCudaSharedMemoryRegister();
RegisterCudaSharedMemoryUnregister();
// model repository
RegisterRepositoryIndex();
RegisterRepositoryModelLoad();
RegisterRepositoryModelUnload();
// statistics
RegisterModelStatistics();
// trace
RegisterTrace();
// logging
RegisterLogging();
}
void
CommonHandler::RegisterServerLive()
{
auto OnRegisterServerLive =
[this](
::grpc::ServerContext* ctx, inference::ServerLiveRequest* request,
::grpc::ServerAsyncResponseWriter<inference::ServerLiveResponse>*
responder,
void* tag) {
this->service_->RequestServerLive(
ctx, request, responder, this->cq_, this->cq_, tag);
};
auto OnExecuteServerLive = [this](
inference::ServerLiveRequest& request,
inference::ServerLiveResponse* response,
::grpc::Status* status) {
bool live = false;
TRITONSERVER_Error* err =
TRITONSERVER_ServerIsLive(tritonserver_.get(), &live);
response->set_live((err == nullptr) && live);
GrpcStatusUtil::Create(status, err);
TRITONSERVER_ErrorDelete(err);
};
const std::pair<std::string, std::string>& restricted_kv =
restricted_keys_.Get(RestrictedCategory::HEALTH);
new CommonCallData<
::grpc::ServerAsyncResponseWriter<inference::ServerLiveResponse>,
inference::ServerLiveRequest, inference::ServerLiveResponse>(
"ServerLive", 0, OnRegisterServerLive, OnExecuteServerLive,
false /* async */, cq_, restricted_kv, response_delay_);
}
void
CommonHandler::RegisterServerReady()
{
auto OnRegisterServerReady =
[this](
::grpc::ServerContext* ctx, inference::ServerReadyRequest* request,
::grpc::ServerAsyncResponseWriter<inference::ServerReadyResponse>*
responder,
void* tag) {
this->service_->RequestServerReady(
ctx, request, responder, this->cq_, this->cq_, tag);
};
auto OnExecuteServerReady = [this](
inference::ServerReadyRequest& request,
inference::ServerReadyResponse* response,
::grpc::Status* status) {
bool ready = false;
TRITONSERVER_Error* err =
TRITONSERVER_ServerIsReady(tritonserver_.get(), &ready);
response->set_ready((err == nullptr) && ready);
GrpcStatusUtil::Create(status, err);
TRITONSERVER_ErrorDelete(err);
};
const std::pair<std::string, std::string>& restricted_kv =
restricted_keys_.Get(RestrictedCategory::HEALTH);
new CommonCallData<
::grpc::ServerAsyncResponseWriter<inference::ServerReadyResponse>,
inference::ServerReadyRequest, inference::ServerReadyResponse>(
"ServerReady", 0, OnRegisterServerReady, OnExecuteServerReady,
false /* async */, cq_, restricted_kv, response_delay_);
}
void
CommonHandler::RegisterHealthCheck()
{
auto OnRegisterHealthCheck =
[this](
::grpc::ServerContext* ctx,
::grpc::health::v1::HealthCheckRequest* request,
::grpc::ServerAsyncResponseWriter<
::grpc::health::v1::HealthCheckResponse>* responder,
void* tag) {
this->health_service_->RequestCheck(
ctx, request, responder, this->cq_, this->cq_, tag);
};
auto OnExecuteHealthCheck = [this](
::grpc::health::v1::HealthCheckRequest&
request,
::grpc::health::v1::HealthCheckResponse*
response,
::grpc::Status* status) {
bool live = false;
TRITONSERVER_Error* err =
TRITONSERVER_ServerIsReady(tritonserver_.get(), &live);
auto serving_status =
::grpc::health::v1::HealthCheckResponse_ServingStatus_UNKNOWN;
if (err == nullptr) {
serving_status =
live ? ::grpc::health::v1::HealthCheckResponse_ServingStatus_SERVING
: ::grpc::health::v1::
HealthCheckResponse_ServingStatus_NOT_SERVING;
}
response->set_status(serving_status);
GrpcStatusUtil::Create(status, err);
TRITONSERVER_ErrorDelete(err);
};
const std::pair<std::string, std::string>& restricted_kv =
restricted_keys_.Get(RestrictedCategory::HEALTH);
new CommonCallData<
::grpc::ServerAsyncResponseWriter<
::grpc::health::v1::HealthCheckResponse>,
::grpc::health::v1::HealthCheckRequest,
::grpc::health::v1::HealthCheckResponse>(
"Check", 0, OnRegisterHealthCheck, OnExecuteHealthCheck,
false /* async */, cq_, restricted_kv, response_delay_);
}
void
CommonHandler::RegisterModelReady()
{
auto OnRegisterModelReady =
[this](
::grpc::ServerContext* ctx, inference::ModelReadyRequest* request,
::grpc::ServerAsyncResponseWriter<inference::ModelReadyResponse>*
responder,
void* tag) {
this->service_->RequestModelReady(
ctx, request, responder, this->cq_, this->cq_, tag);
};
auto OnExecuteModelReady = [this](
inference::ModelReadyRequest& request,
inference::ModelReadyResponse* response,
::grpc::Status* status) {
bool is_ready = false;
int64_t requested_model_version;
auto err =
GetModelVersionFromString(request.version(), &requested_model_version);
if (err == nullptr) {
err = TRITONSERVER_ServerModelIsReady(
tritonserver_.get(), request.name().c_str(), requested_model_version,
&is_ready);
}
response->set_ready(is_ready);
GrpcStatusUtil::Create(status, err);
TRITONSERVER_ErrorDelete(err);
};
const std::pair<std::string, std::string>& restricted_kv =
restricted_keys_.Get(RestrictedCategory::HEALTH);
new CommonCallData<
::grpc::ServerAsyncResponseWriter<inference::ModelReadyResponse>,
inference::ModelReadyRequest, inference::ModelReadyResponse>(
"ModelReady", 0, OnRegisterModelReady, OnExecuteModelReady,
false /* async */, cq_, restricted_kv, response_delay_);
}
void
CommonHandler::RegisterServerMetadata()
{
auto OnRegisterServerMetadata =
[this](
::grpc::ServerContext* ctx, inference::ServerMetadataRequest* request,
::grpc::ServerAsyncResponseWriter<inference::ServerMetadataResponse>*
responder,
void* tag) {
this->service_->RequestServerMetadata(
ctx, request, responder, this->cq_, this->cq_, tag);
};
auto OnExecuteServerMetadata =
[this](
inference::ServerMetadataRequest& request,
inference::ServerMetadataResponse* response, ::grpc::Status* status) {
TRITONSERVER_Message* server_metadata_message = nullptr;
TRITONSERVER_Error* err = TRITONSERVER_ServerMetadata(
tritonserver_.get(), &server_metadata_message);
GOTO_IF_ERR(err, earlyexit);
const char* buffer;
size_t byte_size;
err = TRITONSERVER_MessageSerializeToJson(
server_metadata_message, &buffer, &byte_size);
GOTO_IF_ERR(err, earlyexit);
{
triton::common::TritonJson::Value server_metadata_json;
err = server_metadata_json.Parse(buffer, byte_size);
GOTO_IF_ERR(err, earlyexit);
const char* name;
size_t namelen;
err = server_metadata_json.MemberAsString("name", &name, &namelen);
GOTO_IF_ERR(err, earlyexit);
const char* version;
size_t versionlen;
err = server_metadata_json.MemberAsString(
"version", &version, &versionlen);
GOTO_IF_ERR(err, earlyexit);
response->set_name(std::string(name, namelen));
response->set_version(std::string(version, versionlen));
if (server_metadata_json.Find("extensions")) {
triton::common::TritonJson::Value extensions_json;
err = server_metadata_json.MemberAsArray(
"extensions", &extensions_json);
GOTO_IF_ERR(err, earlyexit);
for (size_t idx = 0; idx < extensions_json.ArraySize(); ++idx) {
const char* ext;
size_t extlen;
err = extensions_json.IndexAsString(idx, &ext, &extlen);
GOTO_IF_ERR(err, earlyexit);
response->add_extensions(std::string(ext, extlen));
}
}
TRITONSERVER_MessageDelete(server_metadata_message);
}
earlyexit:
GrpcStatusUtil::Create(status, err);
TRITONSERVER_ErrorDelete(err);
};
const std::pair<std::string, std::string>& restricted_kv =
restricted_keys_.Get(RestrictedCategory::METADATA);
new CommonCallData<
::grpc::ServerAsyncResponseWriter<inference::ServerMetadataResponse>,
inference::ServerMetadataRequest, inference::ServerMetadataResponse>(
"ServerMetadata", 0, OnRegisterServerMetadata, OnExecuteServerMetadata,
false /* async */, cq_, restricted_kv, response_delay_);
}
void
CommonHandler::RegisterModelMetadata()
{
auto OnRegisterModelMetadata =
[this](
::grpc::ServerContext* ctx, inference::ModelMetadataRequest* request,
::grpc::ServerAsyncResponseWriter<inference::ModelMetadataResponse>*
responder,
void* tag) {
this->service_->RequestModelMetadata(
ctx, request, responder, this->cq_, this->cq_, tag);
};
auto OnExecuteModelMetadata = [this](
inference::ModelMetadataRequest& request,
inference::ModelMetadataResponse* response,
::grpc::Status* status) {
int64_t requested_model_version;
auto err =
GetModelVersionFromString(request.version(), &requested_model_version);
GOTO_IF_ERR(err, earlyexit);
{
TRITONSERVER_Message* model_metadata_message = nullptr;
err = TRITONSERVER_ServerModelMetadata(
tritonserver_.get(), request.name().c_str(), requested_model_version,
&model_metadata_message);
GOTO_IF_ERR(err, earlyexit);
const char* buffer;
size_t byte_size;
err = TRITONSERVER_MessageSerializeToJson(
model_metadata_message, &buffer, &byte_size);
GOTO_IF_ERR(err, earlyexit);
triton::common::TritonJson::Value model_metadata_json;
err = model_metadata_json.Parse(buffer, byte_size);
GOTO_IF_ERR(err, earlyexit);
const char* name;
size_t namelen;
err = model_metadata_json.MemberAsString("name", &name, &namelen);
GOTO_IF_ERR(err, earlyexit);
response->set_name(std::string(name, namelen));
if (model_metadata_json.Find("versions")) {
triton::common::TritonJson::Value versions_json;
err = model_metadata_json.MemberAsArray("versions", &versions_json);
GOTO_IF_ERR(err, earlyexit);
for (size_t idx = 0; idx < versions_json.ArraySize(); ++idx) {
const char* version;
size_t versionlen;
err = versions_json.IndexAsString(idx, &version, &versionlen);
GOTO_IF_ERR(err, earlyexit);
response->add_versions(std::string(version, versionlen));
}
}
const char* platform;
size_t platformlen;
err = model_metadata_json.MemberAsString(
"platform", &platform, &platformlen);
GOTO_IF_ERR(err, earlyexit);
response->set_platform(std::string(platform, platformlen));
if (model_metadata_json.Find("inputs")) {
triton::common::TritonJson::Value inputs_json;
err = model_metadata_json.MemberAsArray("inputs", &inputs_json);
GOTO_IF_ERR(err, earlyexit);
for (size_t idx = 0; idx < inputs_json.ArraySize(); ++idx) {
triton::common::TritonJson::Value io_json;
err = inputs_json.IndexAsObject(idx, &io_json);
GOTO_IF_ERR(err, earlyexit);
inference::ModelMetadataResponse::TensorMetadata* io =
response->add_inputs();
const char* name;
size_t namelen;
err = io_json.MemberAsString("name", &name, &namelen);
GOTO_IF_ERR(err, earlyexit);
const char* datatype;
size_t datatypelen;
err = io_json.MemberAsString("datatype", &datatype, &datatypelen);
GOTO_IF_ERR(err, earlyexit);
io->set_name(std::string(name, namelen));
io->set_datatype(std::string(datatype, datatypelen));
if (io_json.Find("shape")) {
triton::common::TritonJson::Value shape_json;
err = io_json.MemberAsArray("shape", &shape_json);
GOTO_IF_ERR(err, earlyexit);
for (size_t sidx = 0; sidx < shape_json.ArraySize(); ++sidx) {
int64_t d;
err = shape_json.IndexAsInt(sidx, &d);
GOTO_IF_ERR(err, earlyexit);
io->add_shape(d);
}
}
}
}
if (model_metadata_json.Find("outputs")) {
triton::common::TritonJson::Value outputs_json;
err = model_metadata_json.MemberAsArray("outputs", &outputs_json);
GOTO_IF_ERR(err, earlyexit);
for (size_t idx = 0; idx < outputs_json.ArraySize(); ++idx) {
triton::common::TritonJson::Value io_json;
err = outputs_json.IndexAsObject(idx, &io_json);
GOTO_IF_ERR(err, earlyexit);
inference::ModelMetadataResponse::TensorMetadata* io =
response->add_outputs();
const char* name;
size_t namelen;
err = io_json.MemberAsString("name", &name, &namelen);
GOTO_IF_ERR(err, earlyexit);
const char* datatype;
size_t datatypelen;
err = io_json.MemberAsString("datatype", &datatype, &datatypelen);
GOTO_IF_ERR(err, earlyexit);
io->set_name(std::string(name, namelen));
io->set_datatype(std::string(datatype, datatypelen));
if (io_json.Find("shape")) {
triton::common::TritonJson::Value shape_json;
err = io_json.MemberAsArray("shape", &shape_json);
GOTO_IF_ERR(err, earlyexit);
for (size_t sidx = 0; sidx < shape_json.ArraySize(); ++sidx) {
int64_t d;
err = shape_json.IndexAsInt(sidx, &d);
GOTO_IF_ERR(err, earlyexit);
io->add_shape(d);
}
}
}
}
TRITONSERVER_MessageDelete(model_metadata_message);
}
earlyexit:
GrpcStatusUtil::Create(status, err);
TRITONSERVER_ErrorDelete(err);
};
const std::pair<std::string, std::string>& restricted_kv =
restricted_keys_.Get(RestrictedCategory::METADATA);
new CommonCallData<
::grpc::ServerAsyncResponseWriter<inference::ModelMetadataResponse>,
inference::ModelMetadataRequest, inference::ModelMetadataResponse>(
"ModelMetadata", 0, OnRegisterModelMetadata, OnExecuteModelMetadata,
false /* async */, cq_, restricted_kv, response_delay_);
}
void
CommonHandler::RegisterModelConfig()
{
auto OnRegisterModelConfig =
[this](
::grpc::ServerContext* ctx, inference::ModelConfigRequest* request,
::grpc::ServerAsyncResponseWriter<inference::ModelConfigResponse>*
responder,
void* tag) {
this->service_->RequestModelConfig(
ctx, request, responder, this->cq_, this->cq_, tag);
};
auto OnExecuteModelConfig = [this](
inference::ModelConfigRequest& request,
inference::ModelConfigResponse* response,
::grpc::Status* status) {
int64_t requested_model_version;
auto err =
GetModelVersionFromString(request.version(), &requested_model_version);
if (err == nullptr) {
TRITONSERVER_Message* model_config_message = nullptr;
err = TRITONSERVER_ServerModelConfig(
tritonserver_.get(), request.name().c_str(), requested_model_version,
1 /* config_version */, &model_config_message);
if (err == nullptr) {
const char* buffer;
size_t byte_size;
err = TRITONSERVER_MessageSerializeToJson(
model_config_message, &buffer, &byte_size);
if (err == nullptr) {
::google::protobuf::util::JsonStringToMessage(
::google::protobuf::stringpiece_internal::StringPiece(
buffer, (int)byte_size),
response->mutable_config());
}
TRITONSERVER_MessageDelete(model_config_message);
}
}
GrpcStatusUtil::Create(status, err);
TRITONSERVER_ErrorDelete(err);
};
const std::pair<std::string, std::string>& restricted_kv =
restricted_keys_.Get(RestrictedCategory::MODEL_CONFIG);
new CommonCallData<
::grpc::ServerAsyncResponseWriter<inference::ModelConfigResponse>,
inference::ModelConfigRequest, inference::ModelConfigResponse>(
"ModelConfig", 0, OnRegisterModelConfig, OnExecuteModelConfig,
false /* async */, cq_, restricted_kv, response_delay_);
}
void
CommonHandler::RegisterModelStatistics()
{
auto OnRegisterModelStatistics =
[this](
::grpc::ServerContext* ctx,
inference::ModelStatisticsRequest* request,
::grpc::ServerAsyncResponseWriter<inference::ModelStatisticsResponse>*
responder,
void* tag) {
this->service_->RequestModelStatistics(
ctx, request, responder, this->cq_, this->cq_, tag);
};
auto OnExecuteModelStatistics = [this](
inference::ModelStatisticsRequest&
request,
inference::ModelStatisticsResponse*
response,
::grpc::Status* status) {
#ifdef TRITON_ENABLE_STATS
triton::common::TritonJson::Value model_stats_json;
int64_t requested_model_version;
auto err =
GetModelVersionFromString(request.version(), &requested_model_version);
GOTO_IF_ERR(err, earlyexit);
{
TRITONSERVER_Message* model_stats_message = nullptr;
err = TRITONSERVER_ServerModelStatistics(
tritonserver_.get(), request.name().c_str(), requested_model_version,
&model_stats_message);
GOTO_IF_ERR(err, earlyexit);
const char* buffer;
size_t byte_size;
err = TRITONSERVER_MessageSerializeToJson(
model_stats_message, &buffer, &byte_size);
GOTO_IF_ERR(err, earlyexit);
err = model_stats_json.Parse(buffer, byte_size);
GOTO_IF_ERR(err, earlyexit);
TRITONSERVER_MessageDelete(model_stats_message);
}
if (model_stats_json.Find("model_stats")) {
triton::common::TritonJson::Value stats_json;
err = model_stats_json.MemberAsArray("model_stats", &stats_json);
GOTO_IF_ERR(err, earlyexit);
for (size_t idx = 0; idx < stats_json.ArraySize(); ++idx) {
triton::common::TritonJson::Value model_stat;
err = stats_json.IndexAsObject(idx, &model_stat);
GOTO_IF_ERR(err, earlyexit);
auto statistics = response->add_model_stats();
const char* name;
size_t namelen;
err = model_stat.MemberAsString("name", &name, &namelen);
GOTO_IF_ERR(err, earlyexit);
const char* version;
size_t versionlen;
err = model_stat.MemberAsString("version", &version, &versionlen);
GOTO_IF_ERR(err, earlyexit);
statistics->set_name(std::string(name, namelen));
statistics->set_version(std::string(version, versionlen));
uint64_t ucnt;
err = model_stat.MemberAsUInt("last_inference", &ucnt);
GOTO_IF_ERR(err, earlyexit);
statistics->set_last_inference(ucnt);
err = model_stat.MemberAsUInt("inference_count", &ucnt);
GOTO_IF_ERR(err, earlyexit);
statistics->set_inference_count(ucnt);
err = model_stat.MemberAsUInt("execution_count", &ucnt);
GOTO_IF_ERR(err, earlyexit);
statistics->set_execution_count(ucnt);
{
triton::common::TritonJson::Value infer_stats_json;
err = model_stat.MemberAsObject("inference_stats", &infer_stats_json);
GOTO_IF_ERR(err, earlyexit);
err = SetStatisticsDuration(
infer_stats_json, "success",
statistics->mutable_inference_stats()->mutable_success());
GOTO_IF_ERR(err, earlyexit);
err = SetStatisticsDuration(
infer_stats_json, "fail",
statistics->mutable_inference_stats()->mutable_fail());
GOTO_IF_ERR(err, earlyexit);
err = SetStatisticsDuration(
infer_stats_json, "queue",
statistics->mutable_inference_stats()->mutable_queue());
GOTO_IF_ERR(err, earlyexit);
err = SetStatisticsDuration(
infer_stats_json, "compute_input",
statistics->mutable_inference_stats()->mutable_compute_input());
GOTO_IF_ERR(err, earlyexit);
err = SetStatisticsDuration(
infer_stats_json, "compute_infer",