forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_helper.cpp
More file actions
1280 lines (1150 loc) · 56.2 KB
/
Copy pathhttp_helper.cpp
File metadata and controls
1280 lines (1150 loc) · 56.2 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
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
// 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 "http_helper.h"
#include <brpc/controller.h>
#include <brpc/http_status_code.h>
#include <brpc/uri.h>
#include <fmt/core.h>
#include <fmt/format.h>
#include <gen_cpp/cloud.pb.h>
#include <glog/logging.h>
#include <google/protobuf/message.h>
#include <google/protobuf/service.h>
#include <google/protobuf/util/json_util.h>
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/stringbuffer.h>
#include <algorithm>
#include <cstdint>
#include <string>
#include <type_traits>
#include <vector>
#include "common/metric.h"
#include "cpp/token_bucket_rate_limiter.h"
#include "meta-service/meta_service.h"
#include "meta-service/meta_service_helper.h"
#include "meta-service/meta_service_http.h"
#include "meta-service/meta_service_rate_limit_helper.h"
#include "recycler/recycler.h"
#include "recycler/recycler_service.h"
namespace doris::cloud {
template <typename Message>
static google::protobuf::util::Status parse_json_message(const std::string& unresolved_path,
const std::string& body, Message* req) {
static_assert(std::is_base_of_v<google::protobuf::Message, Message>);
auto st = google::protobuf::util::JsonStringToMessage(body, req);
if (!st.ok()) {
std::string msg = "failed to strictly parse http request for '" + unresolved_path +
"' error: " + st.ToString();
LOG_WARNING(msg).tag("body", encryt_sk(hide_access_key(body)));
// ignore unknown fields
google::protobuf::util::JsonParseOptions json_parse_options;
json_parse_options.ignore_unknown_fields = true;
return google::protobuf::util::JsonStringToMessage(body, req, json_parse_options);
}
return {};
}
#define PARSE_MESSAGE_OR_RETURN(ctrl, req) \
do { \
std::string body = ctrl->request_attachment().to_string(); \
auto& unresolved_path = ctrl->http_request().unresolved_path(); \
auto st = parse_json_message(unresolved_path, body, &req); \
if (!st.ok()) { \
std::string msg = "parse http request '" + unresolved_path + "': " + st.ToString(); \
LOG_WARNING(msg).tag("body", encryt_sk(hide_access_key(body))); \
return http_json_reply(MetaServiceCode::PROTOBUF_PARSE_ERR, msg); \
} \
} while (0)
const std::unordered_map<std::string_view, HttpHandlerInfo>& get_http_handlers() {
using MS = MetaServiceImpl;
using RS = RecyclerServiceImpl;
static const auto handlers = [] {
return std::unordered_map<std::string_view, HttpHandlerInfo> {
// MetaService APIs
{"add_cluster",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_cluster((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"drop_cluster",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_cluster((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"rename_cluster",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_cluster((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"update_cluster_endpoint",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_cluster((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"update_cluster_mysql_user_name",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_cluster((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"add_node",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_cluster((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"drop_node",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_cluster((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"decommission_node",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_cluster((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"set_cluster_status",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_cluster((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"notify_decommissioned",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_cluster((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"alter_vcluster_info",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_cluster((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"create_instance",
{.handler = [](void* s,
brpc::Controller* c) { return process_create_instance((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"drop_instance",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_instance((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"rename_instance",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_instance((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"enable_instance_sse",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_instance((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"disable_instance_sse",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_instance((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"set_instance_status",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_instance((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"add_obj_info",
{.handler =
[](void* s, brpc::Controller* c) {
return process_alter_obj_store_info((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"legacy_update_ak_sk",
{.handler =
[](void* s, brpc::Controller* c) {
return process_alter_obj_store_info((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"update_ak_sk",
{.handler = [](void* s,
brpc::Controller* c) { return process_update_ak_sk((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"show_storage_vaults",
{.handler =
[](void* s, brpc::Controller* c) {
return process_get_obj_store_info((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"add_hdfs_vault",
{.handler =
[](void* s, brpc::Controller* c) {
return process_alter_storage_vault((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"add_s3_vault",
{.handler =
[](void* s, brpc::Controller* c) {
return process_alter_storage_vault((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"alter_s3_vault",
{.handler =
[](void* s, brpc::Controller* c) {
return process_alter_storage_vault((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"drop_s3_vault",
{.handler =
[](void* s, brpc::Controller* c) {
return process_alter_storage_vault((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"drop_hdfs_vault",
{.handler =
[](void* s, brpc::Controller* c) {
return process_alter_storage_vault((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"alter_obj_info",
{.handler =
[](void* s, brpc::Controller* c) {
return process_alter_obj_store_info((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"decode_key",
{.handler = [](void* s,
brpc::Controller* c) { return process_decode_key((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"encode_key",
{.handler = [](void* s,
brpc::Controller* c) { return process_encode_key((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"get_value",
{.handler = [](void* s,
brpc::Controller* c) { return process_get_value((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"set_value",
{.handler = [](void* s,
brpc::Controller* c) { return process_set_value((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"show_meta_ranges",
{.handler =
[](void* s, brpc::Controller* c) {
return process_show_meta_ranges((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"txn_lazy_commit",
{.handler = [](void* s,
brpc::Controller* c) { return process_txn_lazy_commit((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"injection_point",
{.handler = [](void* s,
brpc::Controller* c) { return process_injection_point((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"fix_tablet_stats",
{.handler =
[](void* s, brpc::Controller* c) {
return process_fix_tablet_stats((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"fix_tablet_db_id",
{.handler =
[](void* s, brpc::Controller* c) {
return process_fix_tablet_db_id((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"get_instance",
{.handler =
[](void* s, brpc::Controller* c) {
return process_get_instance_info((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"get_obj_store_info",
{.handler =
[](void* s, brpc::Controller* c) {
return process_get_obj_store_info((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"get_cluster",
{.handler = [](void* s,
brpc::Controller* c) { return process_get_cluster((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"get_tablet_stats",
{.handler =
[](void* s, brpc::Controller* c) {
return process_get_tablet_stats((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"get_stage",
{.handler = [](void* s,
brpc::Controller* c) { return process_get_stage((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"get_cluster_status",
{.handler =
[](void* s, brpc::Controller* c) {
return process_get_cluster_status((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"set_rpc_rate_limit_whitelist",
{.handler =
[](void* s, brpc::Controller* c) {
return process_set_rpc_rate_limit_whitelist((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"get_rpc_rate_limit_whitelist",
{.handler =
[](void* s, brpc::Controller* c) {
return process_get_rpc_rate_limit_whitelist((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"list_snapshot",
{.handler = [](void* s,
brpc::Controller* c) { return process_list_snapshot((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"drop_snapshot",
{.handler = [](void* s,
brpc::Controller* c) { return process_drop_snapshot((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"set_snapshot_property",
{.handler =
[](void* s, brpc::Controller* c) {
return process_set_snapshot_property((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"get_snapshot_property",
{.handler =
[](void* s, brpc::Controller* c) {
return process_get_snapshot_property((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"set_multi_version_status",
{.handler =
[](void* s, brpc::Controller* c) {
return process_set_multi_version_status((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"compact_snapshot",
{.handler =
[](void* s, brpc::Controller* c) {
return process_compact_snapshot((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"decouple_instance",
{.handler =
[](void* s, brpc::Controller* c) {
return process_decouple_instance((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"abort_txn",
{.handler = [](void* s,
brpc::Controller* c) { return process_abort_txn((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"abort_tablet_job",
{.handler =
[](void* s, brpc::Controller* c) {
return process_abort_tablet_job((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"alter_ram_user",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_ram_user((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"alter_iam",
{.handler = [](void* s,
brpc::Controller* c) { return process_alter_iam((MS*)s, c); },
.role = HttpRole::META_SERVICE}},
{"adjust_rate_limit",
{.handler =
[](void* s, brpc::Controller* c) {
return process_adjust_rate_limit((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
{"list_rate_limit",
{.handler =
[](void* s, brpc::Controller* c) {
return process_query_rate_limit((MS*)s, c);
},
.role = HttpRole::META_SERVICE}},
// Recycler APIs
{"recycle_instance",
{.handler =
[](void* s, brpc::Controller* c) {
return process_recycle_instance((RS*)s, c);
},
.role = HttpRole::RECYCLER}},
{"statistics_recycle",
{.handler =
[](void* s, brpc::Controller* c) {
return process_statistics_recycle((RS*)s, c);
},
.role = HttpRole::RECYCLER}},
{"recycle_copy_jobs",
{.handler =
[](void* s, brpc::Controller* c) {
return process_recycle_copy_jobs((RS*)s, c);
},
.role = HttpRole::RECYCLER}},
{"recycle_job_info",
{.handler =
[](void* s, brpc::Controller* c) {
return process_recycle_job_info((RS*)s, c);
},
.role = HttpRole::RECYCLER}},
{"check_instance",
{.handler = [](void* s,
brpc::Controller* c) { return process_check_instance((RS*)s, c); },
.role = HttpRole::RECYCLER}},
{"check_job_info",
{.handler = [](void* s,
brpc::Controller* c) { return process_check_job_info((RS*)s, c); },
.role = HttpRole::RECYCLER}},
{"check_meta",
{.handler = [](void* s,
brpc::Controller* c) { return process_check_meta((RS*)s, c); },
.role = HttpRole::RECYCLER}},
{"adjust_rate_limiter",
{.handler =
[](void* s, brpc::Controller* c) {
return process_adjust_rate_limiter((RS*)s, c);
},
.role = HttpRole::RECYCLER}},
// Shared APIs
{"show_config",
{.handler = [](void* s,
brpc::Controller* c) { return process_show_config((MS*)s, c); },
.role = HttpRole::BOTH}},
{"update_config",
{.handler = [](void* s,
brpc::Controller* c) { return process_update_config((MS*)s, c); },
.role = HttpRole::BOTH}},
};
}();
return handlers;
}
HttpResponse process_alter_cluster(MetaServiceImpl* service, brpc::Controller* ctrl) {
static std::unordered_map<std::string_view, AlterClusterRequest::Operation> operations {
{"add_cluster", AlterClusterRequest::ADD_CLUSTER},
{"drop_cluster", AlterClusterRequest::DROP_CLUSTER},
{"rename_cluster", AlterClusterRequest::RENAME_CLUSTER},
{"update_cluster_endpoint", AlterClusterRequest::UPDATE_CLUSTER_ENDPOINT},
{"update_cluster_mysql_user_name", AlterClusterRequest::UPDATE_CLUSTER_MYSQL_USER_NAME},
{"add_node", AlterClusterRequest::ADD_NODE},
{"drop_node", AlterClusterRequest::DROP_NODE},
{"decommission_node", AlterClusterRequest::DECOMMISSION_NODE},
{"set_cluster_status", AlterClusterRequest::SET_CLUSTER_STATUS},
{"notify_decommissioned", AlterClusterRequest::NOTIFY_DECOMMISSIONED},
{"alter_vcluster_info", AlterClusterRequest::ALTER_VCLUSTER_INFO},
};
auto& path = ctrl->http_request().unresolved_path();
auto body = ctrl->request_attachment().to_string();
auto it = operations.find(http_api_route(path));
if (it == operations.end()) {
std::string msg = "not supportted alter cluster operation: " + path;
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, msg);
}
AlterClusterRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
req.set_op(it->second);
AlterClusterResponse resp;
service->alter_cluster(ctrl, &req, &resp, nullptr);
return http_json_reply(resp.status());
}
HttpResponse process_get_obj_store_info(MetaServiceImpl* service, brpc::Controller* ctrl) {
GetObjStoreInfoRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
GetObjStoreInfoResponse resp;
service->get_obj_store_info(ctrl, &req, &resp, nullptr);
return http_json_reply_message(resp.status(), resp);
}
HttpResponse process_alter_obj_store_info(MetaServiceImpl* service, brpc::Controller* ctrl) {
static std::unordered_map<std::string_view, AlterObjStoreInfoRequest::Operation> operations {
{"add_obj_info", AlterObjStoreInfoRequest::ADD_OBJ_INFO},
{"legacy_update_ak_sk", AlterObjStoreInfoRequest::LEGACY_UPDATE_AK_SK},
{"alter_obj_info", AlterObjStoreInfoRequest::ALTER_OBJ_INFO}};
auto& path = ctrl->http_request().unresolved_path();
auto it = operations.find(http_api_route(path));
if (it == operations.end()) {
std::string msg = "not supportted alter obj store info operation: " + path;
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, msg);
}
AlterObjStoreInfoRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
req.set_op(it->second);
AlterObjStoreInfoResponse resp;
service->alter_obj_store_info(ctrl, &req, &resp, nullptr);
return http_json_reply(resp.status());
}
HttpResponse process_alter_storage_vault(MetaServiceImpl* service, brpc::Controller* ctrl) {
static std::unordered_map<std::string_view, AlterObjStoreInfoRequest::Operation> operations {
{"drop_s3_vault", AlterObjStoreInfoRequest::DROP_S3_VAULT},
{"add_s3_vault", AlterObjStoreInfoRequest::ADD_S3_VAULT},
{"alter_s3_vault", AlterObjStoreInfoRequest::ALTER_S3_VAULT},
{"drop_hdfs_vault", AlterObjStoreInfoRequest::DROP_HDFS_INFO},
{"add_hdfs_vault", AlterObjStoreInfoRequest::ADD_HDFS_INFO}};
auto& path = ctrl->http_request().unresolved_path();
auto it = operations.find(http_api_route(path));
if (it == operations.end()) {
std::string msg = "not supportted alter storage vault operation: " + path;
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, msg);
}
AlterObjStoreInfoRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
req.set_op(it->second);
AlterObjStoreInfoResponse resp;
service->alter_storage_vault(ctrl, &req, &resp, nullptr);
return http_json_reply(resp.status());
}
HttpResponse process_update_ak_sk(MetaServiceImpl* service, brpc::Controller* ctrl) {
UpdateAkSkRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
UpdateAkSkResponse resp;
service->update_ak_sk(ctrl, &req, &resp, nullptr);
return http_json_reply(resp.status());
}
HttpResponse process_create_instance(MetaServiceImpl* service, brpc::Controller* ctrl) {
CreateInstanceRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
CreateInstanceResponse resp;
service->create_instance(ctrl, &req, &resp, nullptr);
return http_json_reply(resp.status());
}
HttpResponse process_alter_instance(MetaServiceImpl* service, brpc::Controller* ctrl) {
static std::unordered_map<std::string_view, std::vector<AlterInstanceRequest::Operation>>
operations {{"rename_instance", {AlterInstanceRequest::RENAME}},
{"enable_instance_sse", {AlterInstanceRequest::ENABLE_SSE}},
{"disable_instance_sse", {AlterInstanceRequest::DISABLE_SSE}},
{"drop_instance", {AlterInstanceRequest::DROP}},
{"set_instance_status",
{AlterInstanceRequest::SET_NORMAL, AlterInstanceRequest::SET_OVERDUE}}};
auto& path = ctrl->http_request().unresolved_path();
auto it = operations.find(http_api_route(path));
if (it == operations.end()) {
std::string msg = "not supportted alter instance operation: '" + path +
"', route=" + std::string(http_api_route(path));
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, msg);
}
AlterInstanceRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
// for unresolved path whose corresponding operation is signal, we need set operation by ourselves.
if ((it->second).size() == 1) {
req.set_op((it->second)[0]);
}
AlterInstanceResponse resp;
service->alter_instance(ctrl, &req, &resp, nullptr);
return http_json_reply(resp.status());
}
HttpResponse process_abort_txn(MetaServiceImpl* service, brpc::Controller* ctrl) {
AbortTxnRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
AbortTxnResponse resp;
service->abort_txn(ctrl, &req, &resp, nullptr);
return http_json_reply(resp.status());
}
HttpResponse process_abort_tablet_job(MetaServiceImpl* service, brpc::Controller* ctrl) {
FinishTabletJobRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
req.set_action(FinishTabletJobRequest::ABORT);
FinishTabletJobResponse resp;
service->finish_tablet_job(ctrl, &req, &resp, nullptr);
return http_json_reply(resp.status());
}
HttpResponse process_alter_ram_user(MetaServiceImpl* service, brpc::Controller* ctrl) {
AlterRamUserRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
AlterRamUserResponse resp;
service->alter_ram_user(ctrl, &req, &resp, nullptr);
return http_json_reply(resp.status());
}
HttpResponse process_alter_iam(MetaServiceImpl* service, brpc::Controller* ctrl) {
AlterIamRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
AlterIamResponse resp;
service->alter_iam(ctrl, &req, &resp, nullptr);
return http_json_reply(resp.status());
}
HttpResponse process_adjust_rate_limit(MetaServiceImpl* service, brpc::Controller* cntl) {
const auto& uri = cntl->http_request().uri();
auto qps_limit_str = std::string {http_query(uri, "qps_limit")};
auto rpc_name = std::string {http_query(uri, "rpc_name")};
auto instance_id = std::string {http_query(uri, "instance_id")};
auto process_set_qps_limit = [&](std::function<bool(int64_t)> cb) -> HttpResponse {
DCHECK(!qps_limit_str.empty());
int64_t qps_limit = -1;
try {
qps_limit = std::stoll(qps_limit_str);
} catch (const std::exception& ex) {
return http_json_reply(
MetaServiceCode::INVALID_ARGUMENT,
fmt::format("param `qps_limit` is not a legal int64 type:{}", ex.what()));
}
if (qps_limit < 0) {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT,
"`qps_limit` should not be less than 0");
}
if (cb(qps_limit)) {
return http_json_reply(MetaServiceCode::OK, "sucess to adjust rate limit");
}
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT,
fmt::format("failed to adjust rate limit for qps_limit={}, "
"rpc_name={}, instance_id={}, plz ensure correct "
"rpc/instance name",
qps_limit_str, rpc_name, instance_id));
};
auto set_global_qps_limit = [process_set_qps_limit, service]() {
return process_set_qps_limit([service](int64_t qps_limit) {
return service->rate_limiter()->set_rate_limit(qps_limit);
});
};
auto set_rpc_qps_limit = [&]() {
return process_set_qps_limit([&](int64_t qps_limit) {
return service->rate_limiter()->set_rate_limit(qps_limit, rpc_name);
});
};
auto set_instance_qps_limit = [&]() {
return process_set_qps_limit([&](int64_t qps_limit) {
return service->rate_limiter()->set_instance_rate_limit(qps_limit, instance_id);
});
};
auto set_instance_rpc_qps_limit = [&]() {
return process_set_qps_limit([&](int64_t qps_limit) {
return service->rate_limiter()->set_rate_limit(qps_limit, rpc_name, instance_id);
});
};
auto process_invalid_arguments = [&]() -> HttpResponse {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT,
fmt::format("invalid argument: qps_limit(required)={}, "
"rpc_name(optional)={}, instance_id(optional)={}",
qps_limit_str, rpc_name, instance_id));
};
// We have 3 optional params and 2^3 combination, and 4 of them are illegal.
// We register callbacks for them in porcessors accordings to the level, represented by 3 bits.
std::array<std::function<HttpResponse()>, 8> processors;
std::fill_n(processors.begin(), 8, std::move(process_invalid_arguments));
processors[0b001] = std::move(set_global_qps_limit);
processors[0b011] = std::move(set_rpc_qps_limit);
processors[0b101] = std::move(set_instance_qps_limit);
processors[0b111] = std::move(set_instance_rpc_qps_limit);
uint8_t level = (0x01 & !qps_limit_str.empty()) | ((0x01 & !rpc_name.empty()) << 1) |
((0x01 & !instance_id.empty()) << 2);
DCHECK_LT(level, 8);
return processors[level]();
}
HttpResponse process_query_rate_limit(MetaServiceImpl* service, brpc::Controller* cntl) {
auto rate_limiter = service->rate_limiter();
rapidjson::Document d;
d.SetObject();
auto get_qps_limit = [&d](std::string_view rpc_name,
std::shared_ptr<RpcRateLimiter> rpc_limiter) {
rapidjson::Document node;
node.SetObject();
rapidjson::Document sub;
sub.SetObject();
auto get_qps_token_limit = [&](std::string_view instance_id,
std::shared_ptr<RpcRateLimiter::QpsToken> qps_token) {
sub.AddMember(rapidjson::StringRef(instance_id.data(), instance_id.size()),
qps_token->max_qps_limit(), d.GetAllocator());
};
rpc_limiter->for_each_qps_token(std::move(get_qps_token_limit));
node.AddMember("RPC qps limit", rpc_limiter->max_qps_limit(), d.GetAllocator());
node.AddMember("instance specific qps limit", sub, d.GetAllocator());
d.AddMember(rapidjson::StringRef(rpc_name.data(), rpc_name.size()), node, d.GetAllocator());
};
rate_limiter->for_each_rpc_limiter(std::move(get_qps_limit));
rapidjson::StringBuffer sb;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
d.Accept(writer);
return http_json_reply(MetaServiceCode::OK, "", sb.GetString());
}
// Recycler HTTP handlers
HttpResponse process_recycle_instance(RecyclerServiceImpl* service, brpc::Controller* cntl) {
std::string request_body = cntl->request_attachment().to_string();
RecycleInstanceRequest req;
auto st = google::protobuf::util::JsonStringToMessage(request_body, &req);
if (!st.ok()) {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT,
"failed to parse RecycleInstanceRequest");
}
RecycleInstanceResponse res;
service->recycle_instance(cntl, &req, &res, nullptr);
return http_text_reply(res.status(), res.status().msg());
}
HttpResponse process_statistics_recycle(RecyclerServiceImpl* service, brpc::Controller* cntl) {
std::string request_body = cntl->request_attachment().to_string();
StatisticsRecycleRequest req;
auto st = google::protobuf::util::JsonStringToMessage(request_body, &req);
if (!st.ok()) {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT,
"failed to parse StatisticsRecycleRequest");
}
MetaServiceCode code = MetaServiceCode::OK;
std::string msg = "OK";
service->statistics_recycle(req, code, msg);
return http_text_reply(code, msg, msg);
}
HttpResponse process_recycle_copy_jobs(RecyclerServiceImpl* service, brpc::Controller* cntl) {
const auto* instance_id = cntl->http_request().uri().GetQuery("instance_id");
if (!instance_id || instance_id->empty()) {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, "no instance id");
}
MetaServiceCode code = MetaServiceCode::OK;
std::string msg = "OK";
recycle_copy_jobs(service->txn_kv(), *instance_id, code, msg,
service->recycler()->thread_pool_group(), service->txn_lazy_committer());
return http_text_reply(code, msg, msg);
}
HttpResponse process_recycle_job_info(RecyclerServiceImpl* service, brpc::Controller* cntl) {
const auto* instance_id = cntl->http_request().uri().GetQuery("instance_id");
if (!instance_id || instance_id->empty()) {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, "no instance id");
}
MetaServiceCode code = MetaServiceCode::OK;
std::string msg, key;
job_recycle_key({*instance_id}, &key);
recycle_job_info(service->txn_kv(), *instance_id, key, code, msg);
return http_text_reply(code, msg, msg);
}
HttpResponse process_check_instance(RecyclerServiceImpl* service, brpc::Controller* cntl) {
const auto* instance_id = cntl->http_request().uri().GetQuery("instance_id");
if (!instance_id || instance_id->empty()) {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, "no instance id");
}
if (!service->checker()) {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, "checker not enabled");
}
MetaServiceCode code = MetaServiceCode::OK;
std::string msg = "OK";
service->check_instance(*instance_id, code, msg);
return http_text_reply(code, msg, msg);
}
HttpResponse process_check_job_info(RecyclerServiceImpl* service, brpc::Controller* cntl) {
const auto* instance_id = cntl->http_request().uri().GetQuery("instance_id");
if (!instance_id || instance_id->empty()) {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, "no instance id");
}
MetaServiceCode code = MetaServiceCode::OK;
std::string msg, key;
job_check_key({*instance_id}, &key);
recycle_job_info(service->txn_kv(), *instance_id, key, code, msg);
return http_text_reply(code, msg, msg);
}
HttpResponse process_check_meta(RecyclerServiceImpl* service, brpc::Controller* cntl) {
const auto& uri = cntl->http_request().uri();
const auto* instance_id = uri.GetQuery("instance_id");
const auto* host = uri.GetQuery("host");
const auto* port = uri.GetQuery("port");
const auto* user = uri.GetQuery("user");
const auto* password = uri.GetQuery("password");
if (!instance_id || instance_id->empty() || !host || host->empty() || !port || port->empty() ||
!password || !user || user->empty()) {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, "missing required parameters");
}
std::string msg = "OK";
check_meta(service->txn_kv(), *instance_id, *host, *port, *user, *password, msg);
return http_text_reply(MetaServiceCode::OK, msg, msg);
}
HttpResponse process_adjust_rate_limiter(RecyclerServiceImpl*, brpc::Controller* cntl) {
const auto& uri = cntl->http_request().uri();
const auto* type_string = uri.GetQuery("type");
const auto* speed = uri.GetQuery("speed");
const auto* burst = uri.GetQuery("burst");
const auto* limit = uri.GetQuery("limit");
if (!type_string || type_string->empty() || !speed || !burst || !limit ||
(*type_string != "get" && *type_string != "put")) {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, "invalid arguments");
}
auto max_speed = speed->empty() ? 0 : std::stoul(*speed);
auto max_burst = burst->empty() ? 0 : std::stoul(*burst);
auto max_limit = limit->empty() ? 0 : std::stoul(*limit);
if (reset_s3_rate_limiter(string_to_s3_rate_limit_type(*type_string), max_speed, max_burst,
max_limit) != 0) {
return http_json_reply(MetaServiceCode::UNDEFINED_ERR, "adjust failed");
}
return http_json_reply(MetaServiceCode::OK, "");
}
HttpResponse process_show_config(MetaServiceImpl*, brpc::Controller* cntl) {
auto& uri = cntl->http_request().uri();
std::string_view conf_name = http_query(uri, "conf_key");
if (config::full_conf_map == nullptr) {
return http_json_reply(MetaServiceCode::UNDEFINED_ERR, "config map not initialized");
}
std::string result = config::show_config(std::string(conf_name));
return http_json_reply(MetaServiceCode::OK, "", result);
}
HttpResponse process_update_config(MetaServiceImpl* service, brpc::Controller* cntl) {
const auto& uri = cntl->http_request().uri();
bool persist = (http_query(uri, "persist") == "true");
auto configs = std::string {http_query(uri, "configs")};
auto reason = std::string {http_query(uri, "reason")};
LOG(INFO) << "modify configs for reason=" << reason << ", configs=" << configs
<< ", persist=" << http_query(uri, "persist");
if (auto [succ, cause] = config::update_config(configs, persist, config::custom_conf_path);
!succ) {
LOG(WARNING) << cause;
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, cause);
}
return http_json_reply(MetaServiceCode::OK, "");
}
HttpResponse process_decode_key(MetaServiceImpl*, brpc::Controller* ctrl) {
auto& uri = ctrl->http_request().uri();
std::string_view key = http_query(uri, "key");
if (key.empty()) {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, "no key to decode");
}
bool unicode = http_query(uri, "unicode") != "false";
std::string body = prettify_key(key, unicode);
if (body.empty()) {
std::string msg = "failed to decode key, key=" + std::string(key);
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, msg);
}
return http_text_reply(MetaServiceCode::OK, "", body);
}
HttpResponse process_encode_key(MetaServiceImpl*, brpc::Controller* ctrl) {
return process_http_encode_key(ctrl->http_request().uri());
}
HttpResponse process_get_value(MetaServiceImpl* service, brpc::Controller* ctrl) {
return process_http_get_value(service->txn_kv().get(), ctrl->http_request().uri());
}
HttpResponse process_set_value(MetaServiceImpl* service, brpc::Controller* ctrl) {
return process_http_set_value(service->txn_kv().get(), ctrl);
}
// show all key ranges and their count.
HttpResponse process_show_meta_ranges(MetaServiceImpl* service, brpc::Controller* ctrl) {
auto txn_kv = std::dynamic_pointer_cast<FdbTxnKv>(service->txn_kv());
if (!txn_kv) {
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT,
"this method only support fdb txn kv");
}
std::vector<std::string> partition_boundaries;
TxnErrorCode code = txn_kv->get_partition_boundaries(&partition_boundaries);
if (code != TxnErrorCode::TXN_OK) {
auto msg = fmt::format("failed to get boundaries, code={}", code);
return http_json_reply(MetaServiceCode::UNDEFINED_ERR, msg);
}
std::unordered_map<std::string, size_t> partition_count;
get_kv_range_boundaries_count(partition_boundaries, partition_count);
// sort ranges by count
std::vector<std::pair<std::string, size_t>> meta_ranges;
meta_ranges.reserve(partition_count.size());
for (auto&& [key, count] : partition_count) {
meta_ranges.emplace_back(key, count);
}
std::sort(meta_ranges.begin(), meta_ranges.end(),
[](const auto& lhs, const auto& rhs) { return lhs.second > rhs.second; });
std::string body = fmt::format("total meta ranges: {}\n", partition_boundaries.size());
for (auto&& [key, count] : meta_ranges) {
body += fmt::format("{}: {}\n", key, count);
}
return http_text_reply(MetaServiceCode::OK, "", body);
}
HttpResponse process_get_instance_info(MetaServiceImpl* service, brpc::Controller* ctrl) {
auto& uri = ctrl->http_request().uri();
std::string_view instance_id = http_query(uri, "instance_id");
std::string_view cloud_unique_id = http_query(uri, "cloud_unique_id");
InstanceInfoPB instance;
auto [code, msg] = service->get_instance_info(std::string(instance_id),
std::string(cloud_unique_id), &instance);
return http_json_reply_message(code, msg, instance);
}
HttpResponse process_get_cluster(MetaServiceImpl* service, brpc::Controller* ctrl) {
GetClusterRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
bool get_all_cluster_info = false;
// if cluster_id、cluster_name、mysql_user_name all empty, get this instance's all cluster info.
if (req.cluster_id().empty() && req.cluster_name().empty() && req.mysql_user_name().empty()) {
get_all_cluster_info = true;
}
GetClusterResponse resp;
service->get_cluster(ctrl, &req, &resp, nullptr);
if (resp.status().code() == MetaServiceCode::OK) {
if (get_all_cluster_info) {
return http_json_reply_message(resp.status(), resp);
} else {
// ATTN: only returns the first cluster pb.
return http_json_reply_message(resp.status(), resp.cluster(0));
}
} else {
return http_json_reply(resp.status());
}
}
HttpResponse process_get_tablet_stats(MetaServiceImpl* service, brpc::Controller* ctrl) {
GetTabletStatsRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
GetTabletStatsResponse resp;
service->get_tablet_stats(ctrl, &req, &resp, nullptr);
std::string body;
if (resp.status().code() == MetaServiceCode::OK) {
body = resp.DebugString();
}
return http_text_reply(resp.status(), body);
}
HttpResponse process_fix_tablet_stats(MetaServiceImpl* service, brpc::Controller* ctrl) {
auto& uri = ctrl->http_request().uri();
std::string_view cloud_unique_id = http_query(uri, "cloud_unique_id");
std::string_view table_id = http_query(uri, "table_id");
std::string_view tablet_id = http_query(uri, "tablet_id");
MetaServiceResponseStatus st = service->fix_tablet_stats(
std::string(cloud_unique_id), std::string(table_id), std::string(tablet_id));
return http_text_reply(st, st.DebugString());
}
HttpResponse process_fix_tablet_db_id(MetaServiceImpl* service, brpc::Controller* ctrl) {
auto& uri = ctrl->http_request().uri();
std::string instance_id(http_query(uri, "instance_id"));
std::string tablet_id_str(http_query(uri, "tablet_id"));
std::string db_id_str(http_query(uri, "db_id"));
int64_t tablet_id = 0, db_id = 0;
try {
db_id = std::stol(db_id_str);
} catch (const std::exception& e) {
auto msg = fmt::format("db_id {} must be a number, meet error={}", db_id_str, e.what());
LOG(WARNING) << msg;
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, msg);
}
try {
tablet_id = std::stol(tablet_id_str);
} catch (const std::exception& e) {
auto msg = fmt::format("tablet_id {} must be a number, meet error={}", tablet_id_str,
e.what());
LOG(WARNING) << msg;
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, msg);
}
auto [code, msg] = service->fix_tablet_db_id(instance_id, tablet_id, db_id);
return http_text_reply(code, msg, "");
}
HttpResponse process_get_stage(MetaServiceImpl* service, brpc::Controller* ctrl) {
GetStageRequest req;
PARSE_MESSAGE_OR_RETURN(ctrl, req);
GetStageResponse resp;
service->get_stage(ctrl, &req, &resp, nullptr);