30
30
#include " yb/util/atomic.h"
31
31
#include " yb/util/logging.h"
32
32
#include " yb/util/stol_utils.h"
33
-
34
33
#include " yb/util/string_util.h"
34
+
35
35
#include " yb/yql/cql/ql/util/statement_result.h"
36
36
37
37
DEFINE_RUNTIME_int32 (cdc_state_table_num_tablets, 0 ,
@@ -323,8 +323,6 @@ CDCStateTable::CDCStateTable(std::shared_future<client::YBClient*> client_future
323
323
CHECK (client_future_.valid ());
324
324
}
325
325
326
- CDCStateTable::CDCStateTable (client::YBClient* client) : client_ (client) { CHECK_NOTNULL (client); }
327
-
328
326
std::string CDCStateTableKey::ToString () const {
329
327
return Format (
330
328
" TabletId: $0, StreamId: $1 $2" , tablet_id, stream_id,
@@ -435,33 +433,28 @@ Result<master::CreateTableRequestPB> CDCStateTable::GenerateCreateCdcStateTableR
435
433
}
436
434
437
435
Status CDCStateTable::WaitForCreateTableToFinishWithCache () {
438
- if (created_) {
439
- return Status::OK ();
436
+ if (!created_) {
437
+ RETURN_NOT_OK (WaitForCreateTableToFinishWithoutCache ());
438
+ created_ = true ;
440
439
}
441
- auto * client = VERIFY_RESULT (GetClient ());
442
- RETURN_NOT_OK (client->WaitForCreateTableToFinish (kCdcStateYBTableName ));
443
- created_ = true ;
444
440
return Status::OK ();
445
441
}
446
442
447
443
Status CDCStateTable::WaitForCreateTableToFinishWithoutCache () {
448
- auto * client = VERIFY_RESULT (GetClient ());
449
- return client->WaitForCreateTableToFinish (kCdcStateYBTableName );
444
+ return client ().WaitForCreateTableToFinish (kCdcStateYBTableName );
450
445
}
451
446
452
- Status CDCStateTable::OpenTable ( client::TableHandle* cdc_table ) {
453
- auto * client = VERIFY_RESULT ( GetClient () );
454
- RETURN_NOT_OK (cdc_table->Open (kCdcStateYBTableName , client));
455
- return Status::OK () ;
447
+ Result<std::shared_ptr< client::TableHandle>> CDCStateTable::OpenTable ( ) {
448
+ auto cdc_table = std::make_shared<client::TableHandle>( );
449
+ RETURN_NOT_OK (cdc_table->Open (kCdcStateYBTableName , & client () ));
450
+ return cdc_table ;
456
451
}
457
452
458
453
Result<std::shared_ptr<client::TableHandle>> CDCStateTable::GetTable () {
459
454
bool use_cache = GetAtomicFlag (&FLAGS_enable_cdc_state_table_caching);
460
455
if (!use_cache) {
461
456
RETURN_NOT_OK (WaitForCreateTableToFinishWithoutCache ());
462
- auto cdc_table = std::make_shared<client::TableHandle>();
463
- RETURN_NOT_OK (OpenTable (cdc_table.get ()));
464
- return cdc_table;
457
+ return OpenTable ();
465
458
}
466
459
467
460
{
@@ -472,30 +465,16 @@ Result<std::shared_ptr<client::TableHandle>> CDCStateTable::GetTable() {
472
465
}
473
466
474
467
std::lock_guard l (mutex_);
475
- if (cdc_table_) {
476
- return cdc_table_;
468
+ if (!cdc_table_) {
469
+ RETURN_NOT_OK (WaitForCreateTableToFinishWithCache ());
470
+ cdc_table_ = VERIFY_RESULT (OpenTable ());
477
471
}
478
- RETURN_NOT_OK (WaitForCreateTableToFinishWithCache ());
479
- auto cdc_table = std::make_shared<client::TableHandle>();
480
- RETURN_NOT_OK (OpenTable (cdc_table.get ()));
481
- cdc_table_.swap (cdc_table);
482
472
return cdc_table_;
483
473
}
484
474
485
- Result<client::YBClient*> CDCStateTable::GetClient () {
486
- if (!client_) {
487
- CHECK (client_future_.valid ());
488
- client_ = client_future_.get ();
489
- }
490
-
491
- SCHECK (client_, IllegalState, " CDC Client not initialized or shutting down" );
492
- return client_;
493
- }
494
-
495
- Result<std::shared_ptr<client::YBSession>> CDCStateTable::GetSession () {
496
- auto * client = VERIFY_RESULT (GetClient ());
497
- auto session = client->NewSession (client->default_rpc_timeout ());
498
- return session;
475
+ std::shared_ptr<client::YBSession> CDCStateTable::MakeSession () {
476
+ auto & c = client ();
477
+ return c.NewSession (c.default_rpc_timeout ());
499
478
}
500
479
501
480
template <class CDCEntry >
@@ -509,7 +488,7 @@ Status CDCStateTable::WriteEntriesAsync(
509
488
}
510
489
511
490
auto cdc_table = VERIFY_RESULT (GetTable ());
512
- auto session = VERIFY_RESULT ( GetSession () );
491
+ auto session = MakeSession ( );
513
492
514
493
std::vector<client::YBOperationPtr> ops;
515
494
ops.reserve (entries.size () * 2 );
@@ -621,14 +600,9 @@ Result<CDCStateTableRange> CDCStateTable::GetTableRange(
621
600
622
601
Result<CDCStateTableRange> CDCStateTable::GetTableRangeAsync (
623
602
CDCStateTableEntrySelector&& field_filter, Status* iteration_status) {
624
- auto * client = VERIFY_RESULT (GetClient ());
625
-
626
- bool table_creation_in_progress = false ;
627
- RETURN_NOT_OK (client->IsCreateTableInProgress (kCdcStateYBTableName , &table_creation_in_progress));
628
- if (table_creation_in_progress) {
629
- return STATUS (Uninitialized, " CDC State Table creation is in progress" );
630
- }
631
-
603
+ bool creation_in_progress = false ;
604
+ RETURN_NOT_OK (client ().IsCreateTableInProgress (kCdcStateYBTableName , &creation_in_progress));
605
+ SCHECK (!creation_in_progress, Uninitialized, " CDC State Table creation is in progress" );
632
606
return GetTableRange (std::move (field_filter), iteration_status);
633
607
}
634
608
@@ -645,7 +619,7 @@ Result<std::optional<CDCStateTableEntry>> CDCStateTable::TryFetchEntry(
645
619
narrow_cast<ColumnIdRep>(Schema::first_column_id () + kCdcStreamIdIdx );
646
620
647
621
auto cdc_table = VERIFY_RESULT (GetTable ());
648
- auto session = VERIFY_RESULT ( GetSession () );
622
+ auto session = MakeSession ( );
649
623
650
624
const auto read_op = cdc_table->NewReadOp ();
651
625
auto * const req_read = read_op->mutable_request ();
0 commit comments