-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#25127] docdb: create pg_advisory_locks and add a helper class to lo…
…ad this table Summary: Create a system table pg_advisory_locks to store advisory locks with CatalogManagerBgTask. Add a helper class YsqlAdvisoryLocksTableManager to acess this table. GFlags: `num_advisory_locks_tablets` - Number of tablets of pg_advisory_locks table. 1 by default. `yb_enable_advisory_lock` - Whether to enable advisory lock at docdb layer. For this diff, it's used to control if pg_advisory_locks should be created. Jira: DB-14277 Test Plan: advisory_lock-test Reviewers: hsunder Reviewed By: hsunder Subscribers: ybase Differential Revision: https://phorge.dev.yugabyte.com/D40359
- Loading branch information
Showing
9 changed files
with
243 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright (c) YugaByte, Inc. | ||
// | ||
// 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 "yb/client/yb_table_name.h" | ||
#include "yb/master/master_defaults.h" | ||
#include "yb/tserver/ysql_advisory_lock_table.h" | ||
|
||
#include "yb/client/meta_cache.h" | ||
|
||
#include "yb/integration-tests/mini_cluster.h" | ||
#include "yb/integration-tests/yb_mini_cluster_test_base.h" | ||
|
||
DECLARE_int32(catalog_manager_bg_task_wait_ms); | ||
DECLARE_uint32(num_advisory_locks_tablets); | ||
DECLARE_bool(yb_enable_advisory_lock); | ||
|
||
namespace yb { | ||
namespace client { | ||
|
||
const int kNumAdvisoryLocksTablets = 1; | ||
|
||
class AdvisoryLockTest: public MiniClusterTestWithClient<MiniCluster> { | ||
public: | ||
void SetUp() override { | ||
MiniClusterTestWithClient::SetUp(); | ||
|
||
SetFlags(); | ||
auto opts = MiniClusterOptions(); | ||
opts.num_tablet_servers = 3; | ||
opts.num_masters = 1; | ||
cluster_.reset(new MiniCluster(opts)); | ||
ASSERT_OK(cluster_->Start()); | ||
|
||
ASSERT_OK(CreateClient()); | ||
if (ANNOTATE_UNPROTECTED_READ(FLAGS_yb_enable_advisory_lock)) { | ||
ASSERT_OK(WaitForCreateTableToFinish()); | ||
} | ||
} | ||
|
||
Status WaitForCreateTableToFinish() { | ||
YBTableName table_name( | ||
YQL_DATABASE_CQL, master::kSystemNamespaceName, kPgAdvisoryLocksTableName); | ||
return client_->WaitForCreateTableToFinish( | ||
table_name, CoarseMonoClock::Now() + 10s * kTimeMultiplier); | ||
} | ||
|
||
Status CheckNumTablets(const YBTablePtr& table) { | ||
auto future = client_->LookupAllTabletsFuture(table, CoarseMonoClock::Now() + 10s); | ||
SCHECK_EQ(VERIFY_RESULT(future.get()).size(), | ||
ANNOTATE_UNPROTECTED_READ(FLAGS_num_advisory_locks_tablets), | ||
IllegalState, "tablet number mismatch"); | ||
return Status::OK(); | ||
} | ||
|
||
std::unique_ptr<YsqlAdvisoryLocksTable> GetYsqlAdvisoryLocksTable() { | ||
return std::make_unique<YsqlAdvisoryLocksTable>(*client_.get()); | ||
} | ||
|
||
protected: | ||
virtual void SetFlags() { | ||
ANNOTATE_UNPROTECTED_WRITE(FLAGS_yb_enable_advisory_lock) = true; | ||
ANNOTATE_UNPROTECTED_WRITE(FLAGS_num_advisory_locks_tablets) = kNumAdvisoryLocksTablets; | ||
} | ||
}; | ||
|
||
TEST_F(AdvisoryLockTest, TestAdvisoryLockTableCreated) { | ||
auto table = GetYsqlAdvisoryLocksTable(); | ||
ASSERT_OK(CheckNumTablets(ASSERT_RESULT(table->GetTable()))); | ||
} | ||
|
||
class AdvisoryLocksDisabledTest : public AdvisoryLockTest { | ||
protected: | ||
void SetFlags() override { | ||
AdvisoryLockTest::SetFlags(); | ||
ANNOTATE_UNPROTECTED_WRITE(FLAGS_yb_enable_advisory_lock) = false; | ||
} | ||
}; | ||
|
||
TEST_F(AdvisoryLocksDisabledTest, ToggleAdvisoryLockFlag) { | ||
auto table = GetYsqlAdvisoryLocksTable(); | ||
// Wait for the background task to run a few times. | ||
SleepFor(FLAGS_catalog_manager_bg_task_wait_ms * kTimeMultiplier * 3ms); | ||
auto res = table->GetTable(); | ||
ASSERT_NOK(res); | ||
ASSERT_TRUE(res.status().IsNotSupported()); | ||
ANNOTATE_UNPROTECTED_WRITE(FLAGS_yb_enable_advisory_lock) = true; | ||
ASSERT_OK(WaitForCreateTableToFinish()); | ||
ASSERT_OK(CheckNumTablets(ASSERT_RESULT(table->GetTable()))); | ||
} | ||
|
||
} // namespace client | ||
} // namespace yb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// Copyright (c) YugabyteDB, Inc. | ||
// | ||
// 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 <yb/tserver/ysql_advisory_lock_table.h> | ||
#include "yb/client/yb_table_name.h" | ||
#include "yb/client/client.h" | ||
#include "yb/master/master_defaults.h" | ||
|
||
DECLARE_bool(yb_enable_advisory_lock); | ||
|
||
namespace yb { | ||
|
||
YsqlAdvisoryLocksTable::YsqlAdvisoryLocksTable(client::YBClient& client) | ||
: client_(client) {} | ||
|
||
YsqlAdvisoryLocksTable::~YsqlAdvisoryLocksTable() { | ||
} | ||
|
||
Result<client::YBTablePtr> YsqlAdvisoryLocksTable::GetTable() { | ||
SCHECK(FLAGS_yb_enable_advisory_lock, NotSupported, "Advisory locks are not enabled"); | ||
std::lock_guard<std::mutex> l(mutex_); | ||
if (!table_) { | ||
static const client::YBTableName table_name( | ||
YQL_DATABASE_CQL, master::kSystemNamespaceName, kPgAdvisoryLocksTableName); | ||
table_ = VERIFY_RESULT(client_.OpenTable(table_name)); | ||
} | ||
return table_; | ||
} | ||
|
||
} // namespace yb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// Copyright (c) YugabyteDB, Inc. | ||
// | ||
// 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. | ||
// | ||
// | ||
|
||
#pragma once | ||
|
||
#include "yb/client/client_fwd.h" | ||
|
||
namespace yb { | ||
|
||
constexpr char kPgAdvisoryLocksTableName[] = "pg_advisory_locks"; | ||
|
||
// Helper class for the advisory locks table. | ||
class YsqlAdvisoryLocksTable { | ||
public: | ||
explicit YsqlAdvisoryLocksTable(client::YBClient& client); | ||
~YsqlAdvisoryLocksTable(); | ||
|
||
Result<client::YBTablePtr> GetTable() EXCLUDES(mutex_); | ||
|
||
private: | ||
std::mutex mutex_; | ||
client::YBTablePtr table_ GUARDED_BY(mutex_);; | ||
client::YBClient& client_; | ||
}; | ||
|
||
} // namespace yb |