Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ml_metadata/metadata_store/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,16 @@ cc_library(
],
)

# Uses the same PostgreSQL connection flags as the standalone source test.
cc_test(
name = "standalone_postgresql_query_executor_test",
tags = ["local", "manual"],
deps = [
":postgresql_query_executor_test",
":test_postgresql_standalone_metadata_source_initializer",
],
)

cc_library(
name = "postgresql_query_executor_test",
testonly = 1,
Expand All @@ -1017,6 +1027,11 @@ cc_library(
":test_postgresql_metadata_source_initializer",
"@com_google_googletest//:gtest_main",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
"@com_github_gflags_gflags//:gflags_nothreads",
"//ml_metadata/proto:metadata_source_proto",
"//ml_metadata/util:metadata_source_query_config",
],
)
Expand Down
7 changes: 7 additions & 0 deletions ml_metadata/metadata_store/postgresql_query_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,13 @@ absl::Status PostgreSQLQueryExecutor::SelectTypeByNameAndVersion(
absl::Status PostgreSQLQueryExecutor::SelectTypesByNamesAndVersions(
absl::Span<std::pair<std::string, std::string>> names_and_versions,
TypeKind type_kind, RecordSet* record_set) {
// This lookup starts the read-modify-write transaction used by UpsertTypes.
// PostgreSQL row locks do not protect absent types. Serialize type writers
// before reading, and retain the lock until commit/rollback, so concurrent
// creators cannot both observe a missing (name, version, type_kind). Ordinary
// type readers remain unblocked by this lock.
MLMD_RETURN_IF_ERROR(metadata_source_->ExecuteQuery(
"LOCK TABLE Type IN SHARE ROW EXCLUSIVE MODE", nullptr));
auto partition = std::partition(
names_and_versions.begin(), names_and_versions.end(),
[](std::pair<absl::string_view, absl::string_view> name_and_version) {
Expand Down
158 changes: 156 additions & 2 deletions ml_metadata/metadata_store/postgresql_query_executor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@ limitations under the License.
// Test suite for a PostgreSQL query config-based QueryExecutor.
#include "ml_metadata/metadata_store/postgresql_query_executor.h"

#include <gtest/gtest.h>

#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#include <gtest/gtest.h>
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/types/span.h"
#include "absl/strings/string_view.h"
#include "gflags/gflags.h"
#include "ml_metadata/metadata_store/query_executor.h"
#include "ml_metadata/metadata_store/query_executor_test.h"
#include "ml_metadata/metadata_store/test_postgresql_metadata_source_initializer.h"
#include "ml_metadata/proto/metadata_source.pb.h"
#include "ml_metadata/util/metadata_source_query_config.h"

namespace ml_metadata {
Expand All @@ -44,7 +56,9 @@ class PostgreSQLQueryExecutorContainer : public QueryConfigExecutorContainer {
util::GetPostgreSQLMetadataSourceQueryConfig(), metadata_source_));
}

~PostgreSQLQueryExecutorContainer() override = default;
~PostgreSQLQueryExecutorContainer() override {
metadata_source_initializer_->Cleanup();
}

MetadataSource* GetMetadataSource() override { return metadata_source_; }
QueryExecutor* GetQueryExecutor() override { return query_executor_.get(); }
Expand All @@ -60,6 +74,140 @@ class PostgreSQLQueryExecutorContainer : public QueryConfigExecutorContainer {
std::unique_ptr<QueryExecutor> query_executor_;
};

// Two independent connections are required: a process-local mutex would not
// prevent different metadata server requests from creating duplicate types.
class PostgreSQLTypeUpsertLockTest
: public ::testing::TestWithParam<std::tuple<TypeKind, bool>> {
protected:
void SetUp() override {
first_initializer_ = GetTestPostgreSQLMetadataSourceInitializer();
second_initializer_ = GetTestPostgreSQLMetadataSourceInitializer();
first_source_ = first_initializer_->Init();
if (!first_source_->is_connected()) {
ASSERT_EQ(first_source_->Connect(), absl::OkStatus());
}
first_executor_ = std::make_unique<PostgreSQLQueryExecutor>(
util::GetPostgreSQLMetadataSourceQueryConfig(), first_source_);
ASSERT_EQ(first_source_->Begin(), absl::OkStatus());
ASSERT_EQ(first_executor_->InitMetadataSource(), absl::OkStatus());
ASSERT_EQ(first_source_->Commit(), absl::OkStatus());
second_source_ = second_initializer_->Init();
if (!second_source_->is_connected()) {
ASSERT_EQ(second_source_->Connect(), absl::OkStatus());
}
second_executor_ = std::make_unique<PostgreSQLQueryExecutor>(
util::GetPostgreSQLMetadataSourceQueryConfig(), second_source_);
}

void TearDown() override {
// Close both connections before dropping their shared database, including
// when a fatal assertion leaves either transaction open.
if (second_source_ != nullptr && second_source_->is_connected()) {
EXPECT_EQ(second_source_->Close(), absl::OkStatus());
}
if (first_source_ != nullptr && first_source_->is_connected()) {
EXPECT_EQ(first_source_->Close(), absl::OkStatus());
}
first_initializer_->Cleanup();
}

absl::Status FindTypes(QueryExecutor* executor, RecordSet* result) {
std::vector<std::pair<std::string, std::string>> names = {
{"concurrent_type", std::get<1>(GetParam()) ? "v1" : ""}};
return executor->SelectTypesByNamesAndVersions(
absl::MakeSpan(names), std::get<0>(GetParam()), result);
}

absl::Status InsertType(int64_t* id) {
std::optional<absl::string_view> version = std::nullopt;
if (std::get<1>(GetParam())) version = "v1";
switch (std::get<0>(GetParam())) {
case TypeKind::ARTIFACT_TYPE:
return first_executor_->InsertArtifactType(
"concurrent_type", version, std::nullopt, std::nullopt, id);
case TypeKind::EXECUTION_TYPE:
return first_executor_->InsertExecutionType("concurrent_type", version,
std::nullopt, nullptr,
nullptr, std::nullopt, id);
case TypeKind::CONTEXT_TYPE:
return first_executor_->InsertContextType(
"concurrent_type", version, std::nullopt, std::nullopt, id);
default:
return absl::InvalidArgumentError("Unexpected type kind");
}
}

std::unique_ptr<TestPostgreSQLMetadataSourceInitializer> first_initializer_;
std::unique_ptr<TestPostgreSQLMetadataSourceInitializer> second_initializer_;
PostgreSQLMetadataSource* first_source_ = nullptr;
PostgreSQLMetadataSource* second_source_ = nullptr;
std::unique_ptr<PostgreSQLQueryExecutor> first_executor_;
std::unique_ptr<PostgreSQLQueryExecutor> second_executor_;
};

TEST_P(PostgreSQLTypeUpsertLockTest, MissingTypeIsLockedUntilCommit) {
ASSERT_EQ(first_source_->Begin(), absl::OkStatus());
RecordSet first_result;
ASSERT_EQ(FindTypes(first_executor_.get(), &first_result), absl::OkStatus());
ASSERT_EQ(first_result.records_size(), 0);

ASSERT_EQ(second_source_->Begin(), absl::OkStatus());
ASSERT_EQ(
second_source_->ExecuteQuery("SET LOCAL lock_timeout = '100ms'", nullptr),
absl::OkStatus());
// Ordinary type reads do not participate in the upsert lock.
RecordSet ordinary_result;
ASSERT_EQ(second_executor_->SelectAllTypes(std::get<0>(GetParam()),
&ordinary_result),
absl::OkStatus());
RecordSet second_result;
const absl::Status blocked =
FindTypes(second_executor_.get(), &second_result);
// A lock timeout makes the overlap deterministic without timing threads.
// Without the fix this read succeeds and observes the same missing type.
EXPECT_FALSE(blocked.ok());
EXPECT_NE(blocked.message().find("lock timeout"), std::string::npos);
ASSERT_EQ(second_source_->Rollback(), absl::OkStatus());

int64_t id;
ASSERT_EQ(InsertType(&id), absl::OkStatus());
ASSERT_EQ(first_source_->Commit(), absl::OkStatus());

ASSERT_EQ(second_source_->Begin(), absl::OkStatus());
ASSERT_EQ(
second_source_->ExecuteQuery("SET LOCAL lock_timeout = '1s'", nullptr),
absl::OkStatus());
second_result.Clear();
ASSERT_EQ(FindTypes(second_executor_.get(), &second_result),
absl::OkStatus());
ASSERT_EQ(second_result.records_size(), 1);
EXPECT_EQ(second_result.records(0).values(0), std::to_string(id));
ASSERT_EQ(second_source_->Commit(), absl::OkStatus());
}

TEST_P(PostgreSQLTypeUpsertLockTest, RollbackReleasesMissingTypeLock) {
ASSERT_EQ(first_source_->Begin(), absl::OkStatus());
RecordSet result;
ASSERT_EQ(FindTypes(first_executor_.get(), &result), absl::OkStatus());
ASSERT_EQ(result.records_size(), 0);
ASSERT_EQ(first_source_->Rollback(), absl::OkStatus());

ASSERT_EQ(second_source_->Begin(), absl::OkStatus());
ASSERT_EQ(
second_source_->ExecuteQuery("SET LOCAL lock_timeout = '1s'", nullptr),
absl::OkStatus());
ASSERT_EQ(FindTypes(second_executor_.get(), &result), absl::OkStatus());
EXPECT_EQ(result.records_size(), 0);
ASSERT_EQ(second_source_->Commit(), absl::OkStatus());
}

INSTANTIATE_TEST_SUITE_P(
PostgreSQL, PostgreSQLTypeUpsertLockTest,
::testing::Combine(::testing::Values(TypeKind::ARTIFACT_TYPE,
TypeKind::EXECUTION_TYPE,
TypeKind::CONTEXT_TYPE),
::testing::Bool()));

} // namespace

INSTANTIATE_TEST_SUITE_P(
Expand All @@ -70,3 +218,9 @@ INSTANTIATE_TEST_SUITE_P(

} // namespace testing
} // namespace ml_metadata

int main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);
return RUN_ALL_TESTS();
}