-
Notifications
You must be signed in to change notification settings - Fork 322
/
db_sdk.cc
730 lines (672 loc) · 27.2 KB
/
db_sdk.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
/*
* Copyright 2021 4Paradigm
*
* Licensed 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 "sdk/db_sdk.h"
#ifdef DISALLOW_COPY_AND_ASSIGN
#undef DISALLOW_COPY_AND_ASSIGN
#endif
#include <snappy.h>
#include <algorithm>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/hash.h"
#include "base/strings.h"
#include "glog/logging.h"
#include "schema/schema_adapter.h"
namespace openmldb::sdk {
std::shared_ptr<::openmldb::client::NsClient> DBSDK::GetNsClient() {
auto ns_client = std::atomic_load_explicit(&ns_client_, std::memory_order_relaxed);
if (ns_client) return ns_client;
std::string endpoint, real_endpoint;
if (!GetNsAddress(&endpoint, &real_endpoint)) {
DLOG(ERROR) << "fail to get ns address";
return {};
}
if (auto options = GetOptions(); !options->user.empty()) {
ns_client = std::make_shared<::openmldb::client::NsClient>(
endpoint, real_endpoint, authn::UserToken{options->user, codec::Encrypt(options->password)});
} else {
ns_client = std::make_shared<::openmldb::client::NsClient>(endpoint, real_endpoint);
}
int ret = ns_client->Init();
if (ret != 0) {
// We GetNsClient and use it without checking not null. It's intolerable.
LOG(DFATAL) << "fail to init ns client with endpoint " << endpoint;
return {};
}
LOG(INFO) << "init ns client with endpoint " << endpoint << " done";
std::atomic_store_explicit(&ns_client_, ns_client, std::memory_order_relaxed);
return ns_client;
}
std::shared_ptr<::openmldb::client::TaskManagerClient> DBSDK::GetTaskManagerClient() {
auto taskmanager_client = std::atomic_load_explicit(&taskmanager_client_, std::memory_order_relaxed);
if (taskmanager_client) return taskmanager_client;
std::string endpoint, real_endpoint;
if (!GetTaskManagerAddress(&endpoint, &real_endpoint)) {
LOG(ERROR) << "fail to get TaskManager address";
return {};
}
taskmanager_client = std::make_shared<::openmldb::client::TaskManagerClient>(endpoint, real_endpoint);
int ret = taskmanager_client->Init();
if (ret != 0) {
LOG(DFATAL) << "fail to init TaskManager client with endpoint " << endpoint;
return {};
}
DLOG(INFO) << "init TaskManager client with endpoint " << endpoint << " done";
std::atomic_store_explicit(&taskmanager_client_, taskmanager_client, std::memory_order_relaxed);
return taskmanager_client;
}
std::string DBSDK::GetFunSignature(const ::openmldb::common::ExternalFun& fun) {
std::string signature = fun.name();
for (int idx = 0; idx < fun.arg_type_size(); idx++) {
signature.append(".");
signature.append(DataType_Name(fun.arg_type(idx)));
}
return signature;
}
bool DBSDK::InitExternalFun() {
auto ns_client = GetNsClient();
if (!ns_client) {
return false;
}
std::vector<::openmldb::common::ExternalFun> fun_vec;
if (!ns_client->ShowFunction("", &fun_vec).OK()) {
return false;
}
std::vector<std::string> remove_funs;
std::vector<std::shared_ptr<openmldb::common::ExternalFun>> add_funs;
{
std::lock_guard<::openmldb::base::SpinMutex> lock(mu_);
for (const auto& kv : external_fun_) {
bool not_in = true;
for (const auto& fun : fun_vec) {
if (fun.name() == kv.first) {
not_in = false;
break;
}
}
if (not_in) {
remove_funs.emplace_back(kv.first);
}
}
for (const auto& fun : fun_vec) {
auto iter = external_fun_.find(fun.name());
if (iter == external_fun_.end()) {
add_funs.emplace_back(std::make_shared<openmldb::common::ExternalFun>(fun));
} else if (GetFunSignature(*iter->second) != GetFunSignature(fun)) {
remove_funs.emplace_back(fun.name());
add_funs.emplace_back(std::make_shared<openmldb::common::ExternalFun>(fun));
}
}
}
for (const auto& name : remove_funs) {
RemoveExternalFun(name);
}
for (const auto& fun : add_funs) {
RegisterExternalFun(fun);
}
return true;
}
bool DBSDK::RegisterExternalFun(const std::shared_ptr<openmldb::common::ExternalFun>& fun) {
if (!fun) {
return false;
}
::hybridse::node::DataType return_type;
::openmldb::schema::SchemaAdapter::ConvertType(fun->return_type(), &return_type);
std::vector<::hybridse::node::DataType> arg_types;
for (int i = 0; i < fun->arg_type_size(); i++) {
::hybridse::node::DataType data_type;
::openmldb::schema::SchemaAdapter::ConvertType(fun->arg_type(i), &data_type);
arg_types.emplace_back(data_type);
}
if (engine_
->RegisterExternalFunction(fun->name(), return_type, fun->return_nullable(), arg_types, fun->arg_nullable(),
fun->is_aggregate(), "")
.isOK()) {
std::lock_guard<::openmldb::base::SpinMutex> lock(mu_);
external_fun_.emplace(fun->name(), fun);
return true;
}
return false;
}
bool DBSDK::RemoveExternalFun(const std::string& name) {
std::shared_ptr<::openmldb::common::ExternalFun> fun;
{
std::lock_guard<::openmldb::base::SpinMutex> lock(mu_);
auto iter = external_fun_.find(name);
if (iter == external_fun_.end()) {
return false;
}
fun = iter->second;
}
std::vector<::hybridse::node::DataType> arg_types;
for (int i = 0; i < fun->arg_type_size(); i++) {
::hybridse::node::DataType data_type;
::openmldb::schema::SchemaAdapter::ConvertType(fun->arg_type(i), &data_type);
arg_types.emplace_back(data_type);
}
engine_->RemoveExternalFunction(fun->name(), arg_types, "");
std::lock_guard<::openmldb::base::SpinMutex> lock(mu_);
external_fun_.erase(name);
return true;
}
ClusterSDK::ClusterSDK(const std::shared_ptr<SQLRouterOptions>& options)
: options_(options),
session_id_(0),
table_root_path_(options->zk_path + "/table/db_table_data"),
sp_root_path_(options->zk_path + "/store_procedure/db_sp_data"),
notify_path_(options->zk_path + "/table/notify"),
globalvar_changed_notify_path_(options->zk_path + "/notify/global_variable"),
leader_path_(options->zk_path + "/leader"),
taskmanager_leader_path_(options->zk_path + "/taskmanager/leader"),
zk_client_(nullptr),
pool_(1) {
if (!options->user.empty()) {
client_manager_ = std::make_shared<::openmldb::catalog::ClientManager>(
authn::UserToken{options->user, codec::Encrypt(options->password)});
} else {
client_manager_ = std::make_shared<::openmldb::catalog::ClientManager>();
}
catalog_ = std::make_shared<catalog::SDKCatalog>(client_manager_);
}
ClusterSDK::~ClusterSDK() {
pool_.Stop(false);
if (zk_client_ != nullptr) {
zk_client_->CloseZK();
delete zk_client_;
zk_client_ = nullptr;
}
}
void ClusterSDK::CheckZk() {
// ensure that zk client is alive
if (zk_client_->EnsureConnected()) {
if (session_id_ == 0) {
WatchNotify();
} else if (session_id_ != zk_client_->GetSessionTerm()) {
LOG(WARNING) << "session changed, re-watch notify";
WatchNotify();
}
} else {
// 5min print once
LOG_EVERY_N(WARNING, 150) << "zk client is not connected, reconnect later";
}
pool_.DelayTask(2000, [this] { CheckZk(); });
}
bool ClusterSDK::Init() {
zk_client_ = new ::openmldb::zk::ZkClient(options_->zk_cluster, "", options_->zk_session_timeout, "",
options_->zk_path, options_->zk_auth_schema, options_->zk_cert);
bool ok = zk_client_->Init(options_->zk_log_level, options_->zk_log_file);
if (!ok) {
LOG(WARNING) << "fail to init zk client with " << options_->to_string();
return false;
}
LOG(INFO) << "init zk client with " << options_->to_string() << " and session id " << zk_client_->GetSessionTerm();
::hybridse::vm::EngineOptions eopt;
eopt.SetCompileOnly(true);
eopt.SetPlanOnly(true);
engine_ = new ::hybridse::vm::Engine(catalog_, eopt);
ok = BuildCatalog();
if (!ok) return false;
CheckZk();
if (!InitExternalFun()) {
return false;
}
return true;
}
void ClusterSDK::WatchNotify() {
LOG(INFO) << "start to watch notify on table, function, ns leader, taskamanger leader";
session_id_ = zk_client_->GetSessionTerm();
zk_client_->CancelWatchItem(notify_path_);
zk_client_->WatchItem(notify_path_, [this] { Refresh(); });
zk_client_->WatchChildren(options_->zk_path + "/data/function",
[this](auto&& PH1) { RefreshExternalFun(std::forward<decltype(PH1)>(PH1)); });
zk_client_->WatchChildren(leader_path_, [this](auto&& PH1) { RefreshNsClient(std::forward<decltype(PH1)>(PH1)); });
zk_client_->WatchItem(taskmanager_leader_path_, [this] { RefreshTaskManagerClient(); });
}
void ClusterSDK::RefreshExternalFun(const std::vector<std::string>& funs) { InitExternalFun(); }
void ClusterSDK::RefreshNsClient(const std::vector<std::string>& leader_children) {
// just reset ns client, lazy get
std::atomic_store_explicit(&ns_client_, {}, std::memory_order_relaxed);
}
void ClusterSDK::RefreshTaskManagerClient() {
// just reset taskmanager client, lazy get
std::atomic_store_explicit(&taskmanager_client_, {}, std::memory_order_relaxed);
}
bool ClusterSDK::TriggerNotify(::openmldb::type::NotifyType type) const {
if (type == ::openmldb::type::NotifyType::kTable) {
LOG(INFO) << "Trigger table notify node";
return zk_client_->Increment(notify_path_);
} else if (type == ::openmldb::type::NotifyType::kGlobalVar) {
return zk_client_->Increment(globalvar_changed_notify_path_);
}
LOG(ERROR) << "unsupport notify type";
return false;
}
bool ClusterSDK::GetNsAddress(std::string* endpoint, std::string* real_endpoint) {
std::vector<std::string> children;
if (!zk_client_->GetChildren(leader_path_, children) || children.empty()) {
LOG(WARNING) << "no nameserver exists";
return false;
}
std::sort(children.begin(), children.end());
std::string real_path = leader_path_ + "/" + children[0];
if (!zk_client_->GetNodeValue(real_path, *endpoint)) {
LOG(WARNING) << "fail to get zk value with path " << real_path;
return false;
}
DLOG(INFO) << "leader path " << real_path << " with value " << endpoint;
if (!GetRealEndpointFromZk(*endpoint, real_endpoint)) {
return false;
}
return true;
}
bool ClusterSDK::GetTaskManagerAddress(std::string* endpoint, std::string* real_endpoint) {
if (!zk_client_->GetNodeValue(taskmanager_leader_path_, *endpoint)) {
LOG(WARNING) << "fail to get zk value with path " << taskmanager_leader_path_;
return false;
}
DLOG(INFO) << "leader path " << taskmanager_leader_path_ << " with value " << endpoint;
// TODO(tobe): Maybe allow users to set backup TaskManager endpoint
*real_endpoint = "";
return true;
}
// TODO(hw): refactor
bool ClusterSDK::UpdateCatalog(const std::vector<std::string>& table_datas, const std::vector<std::string>& sp_datas) {
std::vector<::openmldb::nameserver::TableInfo> tables;
std::map<std::string, std::map<std::string, std::shared_ptr<::openmldb::nameserver::TableInfo>>> mapping;
auto new_catalog = std::make_shared<::openmldb::catalog::SDKCatalog>(client_manager_);
for (const auto& table_data : table_datas) {
if (table_data.empty()) continue;
std::string value;
bool ok = zk_client_->GetNodeValue(table_root_path_ + "/" + table_data, value);
if (!ok) {
LOG(WARNING) << "fail to get table data " << table_root_path_ << "/" << table_data;
continue;
}
std::shared_ptr<::openmldb::nameserver::TableInfo> table_info(new ::openmldb::nameserver::TableInfo());
ok = table_info->ParseFromString(value);
if (!ok) {
LOG(WARNING) << "fail to parse table proto with " << value;
continue;
}
DLOG(INFO) << "parse table " << table_info->name() << " ok";
tables.push_back(*(table_info));
auto it = mapping.find(table_info->db());
if (it == mapping.end()) {
std::map<std::string, std::shared_ptr<::openmldb::nameserver::TableInfo>> table_in_db;
table_in_db.insert(std::make_pair(table_info->name(), table_info));
mapping.insert(std::make_pair(table_info->db(), table_in_db));
} else {
it->second.insert(std::make_pair(table_info->name(), table_info));
}
DLOG(INFO) << "load table info with name " << table_info->name() << " in db " << table_info->db();
}
Procedures db_sp_map;
for (const auto& node : sp_datas) {
if (node.empty()) continue;
std::string value;
bool ok = zk_client_->GetNodeValue(sp_root_path_ + "/" + node, value);
if (!ok) {
LOG(WARNING) << "fail to get procedure data. node: " << node;
continue;
}
std::string uncompressed;
::snappy::Uncompress(value.c_str(), value.length(), &uncompressed);
::openmldb::api::ProcedureInfo sp_info_pb;
ok = sp_info_pb.ParseFromString(uncompressed);
if (!ok) {
LOG(WARNING) << "fail to parse procedure proto. node: " << node << " value: " << value;
continue;
}
DLOG(INFO) << "parse procedure " << sp_info_pb.sp_name() << " ok";
auto sp_info = std::make_shared<openmldb::catalog::ProcedureInfoImpl>(sp_info_pb);
if (!sp_info) {
LOG(WARNING) << "convert procedure info failed, sp_name: " << sp_info_pb.sp_name()
<< " db: " << sp_info_pb.db_name();
continue;
}
auto it = db_sp_map.find(sp_info->GetDbName());
if (it == db_sp_map.end()) {
std::map<std::string, std::shared_ptr<hybridse::sdk::ProcedureInfo>> sp_in_db = {
{sp_info->GetSpName(), sp_info}};
db_sp_map.insert(std::make_pair(sp_info->GetDbName(), sp_in_db));
} else {
it->second.insert(std::make_pair(sp_info->GetSpName(), sp_info));
}
DLOG(INFO) << "load procedure info with sp name " << sp_info->GetSpName() << " in db " << sp_info->GetDbName();
}
if (!new_catalog->Init(tables, db_sp_map)) {
LOG(WARNING) << "fail to init catalog";
return false;
}
{
std::lock_guard<::openmldb::base::SpinMutex> lock(mu_);
table_to_tablets_ = mapping;
catalog_ = new_catalog;
}
engine_->UpdateCatalog(new_catalog);
return true;
}
bool ClusterSDK::InitTabletClient() {
std::vector<std::string> tablets;
bool ok = zk_client_->GetNodes(tablets);
if (!ok) {
LOG(WARNING) << "fail to get tablets from zk";
return false;
}
std::map<std::string, std::string> real_ep_map;
for (const auto& endpoint : tablets) {
std::string cur_endpoint = ::openmldb::base::ExtractEndpoint(endpoint);
std::string real_endpoint;
if (!GetRealEndpointFromZk(cur_endpoint, &real_endpoint)) {
return false;
}
real_ep_map.emplace(cur_endpoint, real_endpoint);
}
// TODO(hw): update won't delete the old clients in mgr, should create a new mgr?
client_manager_->UpdateClient(real_ep_map);
return true;
}
bool ClusterSDK::BuildCatalog() {
if (!InitTabletClient()) {
return false;
}
std::vector<std::string> table_datas;
if (zk_client_->IsExistNode(table_root_path_) == 0) {
bool ok = zk_client_->GetChildren(table_root_path_, table_datas);
if (!ok) {
LOG(WARNING) << "fail to get table list with path " << table_root_path_;
return false;
}
} else {
LOG(INFO) << "no tables in db";
}
std::vector<std::string> sp_datas;
if (zk_client_->IsExistNode(sp_root_path_) == 0) {
bool ok = zk_client_->GetChildren(sp_root_path_, sp_datas);
if (!ok) {
LOG(WARNING) << "fail to get procedure list with path " << sp_root_path_;
return false;
}
} else {
LOG(INFO) << "no procedures in db";
}
// The empty database can't be find if we only get table datas, but database no notify, so we get alldbs from
// nameserver in GetAllDbs()
return UpdateCatalog(table_datas, sp_datas);
}
std::vector<std::string> DBSDK::GetAllDbs() {
std::vector<std::string> all_dbs;
std::string st;
if (!GetNsClient()->ShowDatabase(&all_dbs, st)) {
LOG(WARNING) << "show db from ns failed, msg: " << st;
return {};
}
return all_dbs;
}
uint32_t DBSDK::GetTableId(const std::string& db, const std::string& tname) {
auto table_handler = GetCatalog()->GetTable(db, tname);
auto* sdk_table_handler = dynamic_cast<::openmldb::catalog::SDKTableHandler*>(table_handler.get());
return sdk_table_handler->GetTid();
}
std::shared_ptr<::openmldb::nameserver::TableInfo> DBSDK::GetTableInfo(const std::string& db,
const std::string& tname) {
std::lock_guard<::openmldb::base::SpinMutex> lock(mu_);
auto it = table_to_tablets_.find(db);
if (it == table_to_tablets_.end()) {
return {};
}
auto sit = it->second.find(tname);
if (sit == it->second.end()) {
return {};
}
auto table_info = sit->second;
return table_info;
}
std::vector<std::shared_ptr<::openmldb::nameserver::TableInfo>> DBSDK::GetTables(const std::string& db) {
std::lock_guard<::openmldb::base::SpinMutex> lock(mu_);
std::vector<std::shared_ptr<::openmldb::nameserver::TableInfo>> tables;
auto it = table_to_tablets_.find(db);
if (it == table_to_tablets_.end()) {
return tables;
}
auto iit = it->second.begin();
for (; iit != it->second.end(); ++iit) {
tables.push_back(iit->second);
}
return tables;
}
std::vector<std::string> DBSDK::GetAllTables() {
std::map<std::string, std::shared_ptr<nameserver::TableInfo>> table_map;
std::vector<std::string> all_tables;
for (auto db_name_iter = table_to_tablets_.begin(); db_name_iter != table_to_tablets_.end(); db_name_iter++) {
table_map = db_name_iter->second;
for (auto table_name_iter = table_map.begin(); table_name_iter != table_map.end(); table_name_iter++) {
all_tables.push_back(table_name_iter->first);
}
}
return all_tables;
}
std::vector<std::string> DBSDK::GetTableNames(const std::string& db) {
std::lock_guard<::openmldb::base::SpinMutex> lock(mu_);
std::vector<std::string> tableNames;
auto it = table_to_tablets_.find(db);
if (it == table_to_tablets_.end()) {
return tableNames;
}
auto iit = it->second.begin();
for (; iit != it->second.end(); ++iit) {
tableNames.push_back(iit->second->name());
}
return tableNames;
}
bool ClusterSDK::GetRealEndpointFromZk(const std::string& endpoint, std::string* real_endpoint) {
if (real_endpoint == nullptr) {
return false;
}
std::string sdk_path = options_->zk_path + "/map/sdkendpoints/" + endpoint;
if (zk_client_->IsExistNode(sdk_path) == 0) {
if (!zk_client_->GetNodeValue(sdk_path, *real_endpoint)) {
DLOG(WARNING) << "get zk failed! : sdk_path: " << sdk_path;
return false;
}
}
if (real_endpoint->empty()) {
std::string sname_path = options_->zk_path + "/map/names/" + endpoint;
if (zk_client_->IsExistNode(sname_path) == 0) {
if (!zk_client_->GetNodeValue(sname_path, *real_endpoint)) {
DLOG(WARNING) << "get zk failed! : sname_path: " << sname_path;
return false;
}
}
}
return true;
}
std::shared_ptr<::openmldb::catalog::TabletAccessor> DBSDK::GetTablet() { return GetCatalog()->GetTablet(); }
std::vector<std::shared_ptr<::openmldb::catalog::TabletAccessor>> DBSDK::GetAllTablet() {
return GetCatalog()->GetAllTablet();
}
std::shared_ptr<::openmldb::catalog::TabletAccessor> DBSDK::GetTablet(const std::string& db, const std::string& name) {
auto table_handler = GetCatalog()->GetTable(db, name);
if (table_handler) {
auto* sdk_table_handler = dynamic_cast<::openmldb::catalog::SDKTableHandler*>(table_handler.get());
if (sdk_table_handler) {
uint32_t pid_num = sdk_table_handler->GetPartitionNum();
uint32_t pid = 0;
if (pid_num > 0) {
pid = rand_.Uniform(pid_num);
}
return sdk_table_handler->GetTablet(pid);
}
}
return {};
}
bool DBSDK::GetTablet(const std::string& db, const std::string& name,
std::vector<std::shared_ptr<::openmldb::catalog::TabletAccessor>>* tablets) {
auto table_handler = GetCatalog()->GetTable(db, name);
if (table_handler) {
auto* sdk_table_handler = dynamic_cast<::openmldb::catalog::SDKTableHandler*>(table_handler.get());
if (sdk_table_handler) {
return sdk_table_handler->GetTablet(tablets);
}
}
return false;
}
std::shared_ptr<::openmldb::catalog::TabletAccessor> DBSDK::GetTablet(const std::string& db, const std::string& name,
uint32_t pid) {
auto table_handler = GetCatalog()->GetTable(db, name);
if (table_handler) {
auto* sdk_table_handler = dynamic_cast<::openmldb::catalog::SDKTableHandler*>(table_handler.get());
if (sdk_table_handler) {
return sdk_table_handler->GetTablet(pid);
}
}
return {};
}
std::vector<std::shared_ptr<::openmldb::catalog::TabletAccessor>> DBSDK::GetTabletFollowers(const std::string& db,
const std::string& name,
uint32_t pid) {
auto table_handler = GetCatalog()->GetTable(db, name);
if (table_handler) {
auto* sdk_table_handler = dynamic_cast<::openmldb::catalog::SDKTableHandler*>(table_handler.get());
if (sdk_table_handler) {
return sdk_table_handler->GetTabletFollowers(pid);
}
}
return {};
}
std::shared_ptr<::openmldb::catalog::TabletAccessor> DBSDK::GetTablet(const std::string& db, const std::string& name,
const std::string& pk) {
auto table_handler = GetCatalog()->GetTable(db, name);
if (table_handler) {
auto sdk_table_handler = dynamic_cast<::openmldb::catalog::SDKTableHandler*>(table_handler.get());
if (sdk_table_handler) {
uint32_t pid_num = sdk_table_handler->GetPartitionNum();
uint32_t pid = 0;
if (pid_num > 0) {
pid = ::openmldb::base::hash64(pk) % pid_num;
}
return sdk_table_handler->GetTablet(pid);
}
}
return {};
}
std::shared_ptr<hybridse::sdk::ProcedureInfo> DBSDK::GetProcedureInfo(const std::string& db, const std::string& sp_name,
std::string* msg) {
if (msg == nullptr) {
return {};
}
if (db.empty() || sp_name.empty()) {
*msg = "db or sp_name is empty";
return {};
} else {
std::lock_guard<::openmldb::base::SpinMutex> lock(mu_);
auto sp = catalog_->GetProcedureInfo(db, sp_name);
if (!sp) {
*msg = sp_name + " does not exist in " + db;
return {};
}
return sp;
}
}
std::vector<std::shared_ptr<hybridse::sdk::ProcedureInfo>> DBSDK::GetProcedureInfo(std::string* msg) {
std::vector<std::shared_ptr<hybridse::sdk::ProcedureInfo>> sp_infos;
if (msg == nullptr) {
return sp_infos;
}
std::lock_guard<::openmldb::base::SpinMutex> lock(mu_);
auto& db_sp_map = catalog_->GetProcedures();
for (const auto& db_kv : db_sp_map) {
for (const auto& sp_kv : db_kv.second) {
sp_infos.push_back(sp_kv.second);
}
}
if (sp_infos.empty()) {
*msg = "procedure set is empty";
return sp_infos;
}
return sp_infos;
}
bool StandAloneSDK::Init() {
::hybridse::vm::EngineOptions opt;
opt.SetCompileOnly(true);
opt.SetPlanOnly(true);
engine_ = new ::hybridse::vm::Engine(catalog_, opt);
if (!InitExternalFun()) {
return false;
}
return PeriodicRefresh();
}
bool StandAloneSDK::BuildCatalog() {
// InitTabletClients
std::vector<client::TabletInfo> tablets;
std::string msg;
if (!GetNsClient()->ShowTablet(tablets, msg)) {
LOG(WARNING) << msg;
return false;
}
std::map<std::string, std::string> real_ep_map;
for (const auto& tablet : tablets) {
std::string cur_endpoint = ::openmldb::base::ExtractEndpoint(tablet.endpoint);
std::string real_endpoint = tablet.real_endpoint;
real_ep_map.emplace(cur_endpoint, real_endpoint);
}
client_manager_->UpdateClient(real_ep_map);
// TableInfos
std::vector<::openmldb::nameserver::TableInfo> tables;
if (!GetNsClient()->ShowAllTable(tables, msg)) {
LOG(WARNING) << "show all table from ns failed, msg: " << msg;
return false;
}
std::map<std::string, std::map<std::string, std::shared_ptr<nameserver::TableInfo>>> mapping;
auto new_catalog = std::make_shared<catalog::SDKCatalog>(client_manager_);
for (const auto& table : tables) {
auto& db_map = mapping[table.db()];
db_map[table.name()] = std::make_shared<nameserver::TableInfo>(table);
VLOG(5) << "load table info with name " << table.name() << " in db " << table.db();
}
std::vector<api::ProcedureInfo> procedures;
// empty db & sp names means show all
if (!GetNsClient()->ShowProcedure("", "", &procedures, &msg)) {
LOG(WARNING) << "show procedure from ns failed, msg: " << msg;
return false;
}
// api::ProcedureInfo to hybridse::sdk::ProcedureInfo
catalog::Procedures db_sp_map;
for (auto& sp : procedures) {
auto sdk_sp = std::make_shared<catalog::ProcedureInfoImpl>(sp);
if (!sdk_sp) {
LOG(WARNING) << "sp convert failed, skip sp: " << sp.db_name() << "-" << sp.sp_name();
continue;
}
db_sp_map[sp.db_name()][sp.sp_name()] = sdk_sp;
}
if (!new_catalog->Init(tables, db_sp_map)) {
LOG(WARNING) << "fail to init catalog";
return false;
}
{
std::lock_guard<::openmldb::base::SpinMutex> lock(mu_);
table_to_tablets_ = mapping;
catalog_ = new_catalog;
}
engine_->UpdateCatalog(new_catalog);
return true;
}
} // namespace openmldb::sdk