From 3b609ecd8ff0c8df9eec0075b6366091804c33eb Mon Sep 17 00:00:00 2001 From: Vinod Sridharan Date: Mon, 23 Dec 2024 06:23:51 +0000 Subject: [PATCH] Merged PR 1540011: [Add-node] More fixes for add-node scenarios ### Does this PR have any customer impact? Yes ### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc) Bugfix ### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes) No ### Are you introducing any new config? If yes, do you have tests with and without them being set? ### Release Note (Refer [Template](../docs/release_notes/pgmongo_template.md)) ### Description - Cluster Data was not replicated fully on add node (upgrades would fail) - Checks for index wasn't handled in add-node scenarios. (Create/Drop indexes would fail) - Get versions on the GW on MX of the added node reutrns NullRef - fix these. ---- #### AI description (iteration 1) #### PR Classification Bug fix for add-node scenarios. #### PR Summary This pull request addresses issues related to ensuring metadata tables are properly replicated during add-node operations. - `distributed_hooks.c`: Refactored `EnsureMetadataTableReplicatedCore` to return a boolean and added `ExecuteMetadataChecksForReferenceTables`. - `create_indexes_background.c`: Added checks to ensure metadata tables are replicated before starting index creation. - `cluster_operations.c`: Ensured `cluster_data` table is replicated on all nodes during cluster setup. - `api_hooks.c` and `api_hooks.h`: Modified `EnsureMetadataTableReplicated` to return a boolean indicating if replication occurred. - `ExtensionVersionExtensions.cs`: Added null checks for version strings before writing them. --- .../src/distribution/cluster_operations.c | 5 +++ .../src/distribution/distributed_hooks.c | 37 ++++++++++++++----- pg_documentdb/include/api_hooks.h | 2 +- pg_documentdb/include/api_hooks_def.h | 2 +- pg_documentdb/src/api_hooks.c | 6 ++- .../src/commands/create_indexes_background.c | 11 ++++++ 6 files changed, 49 insertions(+), 14 deletions(-) diff --git a/internal/pg_documentdb_distributed/src/distribution/cluster_operations.c b/internal/pg_documentdb_distributed/src/distribution/cluster_operations.c index be230580..bf4a518d 100644 --- a/internal/pg_documentdb_distributed/src/distribution/cluster_operations.c +++ b/internal/pg_documentdb_distributed/src/distribution/cluster_operations.c @@ -134,6 +134,11 @@ SetupCluster(bool isInitialize) ExtensionVersion lastUpgradeVersion = { 0 }; ExtensionVersion installedVersion = { 0 }; + /* Ensure that the cluster_data table is replicated on all nodes + * otherwise, writes to cluster_data will fail. + */ + EnsureMetadataTableReplicated("collections"); + char *lastUpgradeVersionString = UpdateClusterMetadata(isInitialize); ParseVersionString(&lastUpgradeVersion, lastUpgradeVersionString); diff --git a/internal/pg_documentdb_distributed/src/distribution/distributed_hooks.c b/internal/pg_documentdb_distributed/src/distribution/distributed_hooks.c index c0508bfc..a8cf3755 100644 --- a/internal/pg_documentdb_distributed/src/distribution/distributed_hooks.c +++ b/internal/pg_documentdb_distributed/src/distribution/distributed_hooks.c @@ -401,14 +401,9 @@ GetDistributedApplicationNameCore(void) } -static void -EnsureMetadataTableReplicatedCore(const char *tableName) +static bool +ExecuteMetadataChecksForReferenceTables(const char *tableName) { - if (!EnableMetadataReferenceTableSync) - { - return; - } - /* First get the shard_id for the table */ StringInfo queryStringInfo = makeStringInfo(); appendStringInfo(queryStringInfo, @@ -421,7 +416,7 @@ EnsureMetadataTableReplicatedCore(const char *tableName) if (isNull) { - return; + return false; } int64 shardId = DatumGetInt64(result); @@ -432,7 +427,7 @@ EnsureMetadataTableReplicatedCore(const char *tableName) false, SPI_OK_SELECT, &isNull); if (isNull) { - return; + return false; } int numNodes = DatumGetInt32(result); @@ -445,7 +440,7 @@ EnsureMetadataTableReplicatedCore(const char *tableName) &isNull); if (isNull) { - return; + return false; } int numPlacements = DatumGetInt32(result); @@ -455,7 +450,29 @@ EnsureMetadataTableReplicatedCore(const char *tableName) /* There was an add node but the metadata table needed wasn't replicated: Call replicate_reference_tables first */ ExtensionExecuteQueryOnLocalhostViaLibPQ( "SELECT pg_catalog.replicate_reference_tables('block_writes')"); + return true; } + else + { + return false; + } +} + + +static bool +EnsureMetadataTableReplicatedCore(const char *tableName) +{ + if (!EnableMetadataReferenceTableSync) + { + return false; + } + + /* Set min messagees to reduce log spam in tests */ + int savedGUCLevel = NewGUCNestLevel(); + SetGUCLocally("client_min_messages", "WARNING"); + bool result = ExecuteMetadataChecksForReferenceTables(tableName); + RollbackGUCChange(savedGUCLevel); + return result; } diff --git a/pg_documentdb/include/api_hooks.h b/pg_documentdb/include/api_hooks.h index 1084d8a4..d38b63cb 100644 --- a/pg_documentdb/include/api_hooks.h +++ b/pg_documentdb/include/api_hooks.h @@ -146,7 +146,7 @@ bool IsChangeStreamFeatureAvailableAndCompatible(void); /* * Ensure the given metadata catalog table is replicated. */ -void EnsureMetadataTableReplicated(const char *tableName); +bool EnsureMetadataTableReplicated(const char *tableName); /* * The hook allows the extension to do any additional setup diff --git a/pg_documentdb/include/api_hooks_def.h b/pg_documentdb/include/api_hooks_def.h index 15f702eb..731546fd 100644 --- a/pg_documentdb/include/api_hooks_def.h +++ b/pg_documentdb/include/api_hooks_def.h @@ -156,7 +156,7 @@ extern GetDistributedApplicationName_HookType get_distributed_application_name_h typedef bool (*IsNtoReturnSupported_HookType)(void); extern IsNtoReturnSupported_HookType is_n_to_return_supported_hook; -typedef void (*EnsureMetadataTableReplicated_HookType)(const char *); +typedef bool (*EnsureMetadataTableReplicated_HookType)(const char *); extern EnsureMetadataTableReplicated_HookType ensure_metadata_table_replicated_hook; typedef void (*PostSetupCluster_HookType)(bool); diff --git a/pg_documentdb/src/api_hooks.c b/pg_documentdb/src/api_hooks.c index 6f9a6dea..23c59a09 100644 --- a/pg_documentdb/src/api_hooks.c +++ b/pg_documentdb/src/api_hooks.c @@ -309,9 +309,10 @@ IsNtoReturnSupported(void) /* * Ensure that the given metadata table is replicated on all nodes - * as applicable + * as applicable. + * Returns true if something changed and was replicated. */ -void +bool EnsureMetadataTableReplicated(const char *tableName) { if (ensure_metadata_table_replicated_hook != NULL) @@ -320,6 +321,7 @@ EnsureMetadataTableReplicated(const char *tableName) } /* Single node default - it's always replicated */ + return false; } diff --git a/pg_documentdb/src/commands/create_indexes_background.c b/pg_documentdb/src/commands/create_indexes_background.c index e83c7a0f..628dd360 100644 --- a/pg_documentdb/src/commands/create_indexes_background.c +++ b/pg_documentdb/src/commands/create_indexes_background.c @@ -152,6 +152,17 @@ command_build_index_concurrently(PG_FUNCTION_ARGS) PG_RETURN_VOID(); } + /* Before starting, ensure that tables are replicated + * If this action did replicate tables, try again in the + * next loop to ensure the transaction is committed. + */ + if (EnsureMetadataTableReplicated("collections")) + { + ereport(LOG, (errmsg( + "Metadata tables were replicated. Retrying index checks in another round."))); + PG_RETURN_VOID(); + } + List *excludeCollectionIds = NIL; uint64 *collectionIds = GetCollectionIdsForIndexBuild( CREATE_INDEX_COMMAND_TYPE, excludeCollectionIds);