mirror of
https://github.com/FerretDB/documentdb.git
synced 2026-11-26 12:19:56 -05:00
### 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? Yes ### ChangeLog (Refer [Template](../oss/CHANGELOG.md)) ### Description When servers have default_readonly_transaction set, the current behavior is wonky - it will cause failures in underlying queries and pollute the errors, and it will go through a bunch of work only to fail. Additionally, background jobs like TTL will fail ungracefully. Handle this GUC by failing write commands early with a more useful error message. Additionally, in cases like TTL, add an opt-in way to prune rows even in the presence of this GUC if it's opted into. ---- #### AI description (iteration 1) #### PR Classification This pull request is a bug fix that improves error handling for write operations attempted during default read-only transactions. #### PR Summary The changes introduce a unified mechanism to check and enforce read-only transaction restrictions, ensuring graceful errors when write operations are attempted while in a read-only state. Key changes include: - **`include/commands/commands_common.h`**: Added the inline function `ThrowIfServerOrTransactionReadOnly` to validate transaction state and throw appropriate errors. - **Test files**: New regression tests under `/oss/pg_documentdb/src/test/regress/expected/` and `/oss/pg_documentdb/src/test/regress/sql/readonly_transaction_tests.sql` validate the expected error responses under read-only conditions. - **`ttl_index.c`**: Modified TTL job logic to handle read-only transactions, allowing jobs to commit and restart when the corresponding flag is enabled. - **Gateway and error handling**: Updated configuration in `TenantConfigurationBase.cs`, error augmentation in `PostgresMongoResultExtensions.cs`, and error mappings (e.g., adding error code 10107 for `NotWritablePrimary`) to propagate clear error messages. - **Command modules**: Added calls to `ThrowIfServerOrTransactionReadOnly` in several command implementations (e.g., in `insert.c`, `update.c`, `delete.c`, `create_indexes.c`) to enforce read-only restrictions uniformly. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->
144 lines
4.2 KiB
C
144 lines
4.2 KiB
C
/*-------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
*
|
|
* include/commands/commands_common.h
|
|
*
|
|
* Common declarations of Mongo commands.
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
|
|
#ifndef COMMANDS_COMMON_H
|
|
#define COMMANDS_COMMON_H
|
|
|
|
#include <utils/elog.h>
|
|
#include <metadata/collection.h>
|
|
#include <io/bson_core.h>
|
|
#include <utils/documentdb_errors.h>
|
|
#include <access/xact.h>
|
|
#include <access/xlog.h>
|
|
|
|
/*
|
|
* Maximum size of a output bson document is 16MB.
|
|
*/
|
|
#define BSON_MAX_ALLOWED_SIZE (16 * 1024 * 1024)
|
|
|
|
/*
|
|
* Maximum size of document produced by an intermediate stage of an aggregation pipeline.
|
|
* This is a Native Mongo constrains. For example, if the pipeline is [$facet, $unwind],
|
|
* $facet is allowed to generate document that is larger than 16MB, since $unwind can
|
|
* break them into smaller document. However, $facet is not allowed to generate a document
|
|
* that's larger than 100MB.
|
|
*/
|
|
#define BSON_MAX_ALLOWED_SIZE_INTERMEDIATE (100 * 1024 * 1024)
|
|
|
|
/* StringView that represents the _id field */
|
|
extern PGDLLIMPORT const StringView IdFieldStringView;
|
|
|
|
|
|
/*
|
|
* ApiGucPrefix.enable_create_collection_on_insert GUC determines whether
|
|
* an insert into a non-existent collection should create a collection.
|
|
*/
|
|
extern bool EnableCreateCollectionOnInsert;
|
|
|
|
/*
|
|
* Whether or not write operations are inlined or if they are dispatched
|
|
* to a remote shard. For single node scenarios like DocumentDB that don't need
|
|
* distributed dispatch. Reset in scenarios that need distributed dispatch.
|
|
*/
|
|
extern bool DefaultInlineWriteOperations;
|
|
extern int BatchWriteSubTransactionCount;
|
|
extern int MaxWriteBatchSize;
|
|
|
|
/*
|
|
* WriteError can be part of the response of a batch write operation.
|
|
*/
|
|
typedef struct WriteError
|
|
{
|
|
/* index in a write batch */
|
|
int index;
|
|
|
|
/* error code */
|
|
int code;
|
|
|
|
/* description of the error */
|
|
char *errmsg;
|
|
} WriteError;
|
|
|
|
|
|
bool FindShardKeyValueForDocumentId(MongoCollection *collection, pgbson *queryDoc,
|
|
bson_value_t *objectId, int64 *shardKeyValue);
|
|
|
|
bool IsCommonSpecIgnoredField(const char *fieldName);
|
|
|
|
WriteError * GetWriteErrorFromErrorData(ErrorData *errorData, int writeErrorIdx);
|
|
bool TryGetErrorMessageAndCode(ErrorData *errorData, int *code, char **errmessage);
|
|
|
|
pgbson * GetObjectIdFilterFromQueryDocument(pgbson *queryDoc, bool *hasNonIdFields);
|
|
|
|
|
|
pgbson * RewriteDocumentAddObjectId(pgbson *document);
|
|
pgbson * RewriteDocumentValueAddObjectId(const bson_value_t *value);
|
|
pgbson * RewriteDocumentWithCustomObjectId(pgbson *document,
|
|
pgbson *objectIdToWrite);
|
|
|
|
void ValidateIdField(const bson_value_t *idValue);
|
|
void SetExplicitStatementTimeout(int timeoutMilliseconds);
|
|
|
|
void CommitWriteProcedureAndReacquireCollectionLock(MongoCollection *collection,
|
|
bool setSnapshot);
|
|
|
|
extern bool SkipEnforceTransactionReadOnly;
|
|
extern bool SimulateRecoveryState;
|
|
extern bool DocumentDBPGReadOnlyForDiskFull;
|
|
|
|
inline static void
|
|
ThrowIfServerOrTransactionReadOnly(void)
|
|
{
|
|
if (!XactReadOnly)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (SkipEnforceTransactionReadOnly)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (RecoveryInProgress() || SimulateRecoveryState)
|
|
{
|
|
/*
|
|
* Skip these checks in recovery mode - let the system throw the appropriate
|
|
* error.
|
|
*/
|
|
return;
|
|
}
|
|
|
|
if (DocumentDBPGReadOnlyForDiskFull)
|
|
{
|
|
ereport(ERROR, (errcode(ERRCODE_DISK_FULL), errmsg(
|
|
"Can't execute write operation, The database disk is full")));
|
|
}
|
|
|
|
/* Error is coming because the server has been put in a read-only state, but we're a writable node (primary) */
|
|
if (DefaultXactReadOnly)
|
|
{
|
|
ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_NOTWRITABLEPRIMARY),
|
|
errmsg(
|
|
"cannot execute write operations when the server is in a read-only state."),
|
|
errdetail("the default transaction is read-only"),
|
|
errdetail_log(
|
|
"cannot execute write operations when default_transaction_read_only is set to true")));
|
|
}
|
|
|
|
/* Error is coming because the transaction has been in a readonly state */
|
|
ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_OPERATIONNOTSUPPORTEDINTRANSACTION),
|
|
errmsg(
|
|
"cannot execute write operation when the transaction is in a read-only state."),
|
|
errdetail("the current transaction is read-only")));
|
|
}
|
|
|
|
|
|
#endif
|