summaryrefslogtreecommitdiff
path: root/src/mongo/db/error_labels.h
diff options
context:
space:
mode:
authorLingzhi Deng <lingzhi.deng@mongodb.com>2019-11-06 15:18:44 +0000
committerevergreen <evergreen@mongodb.com>2019-11-06 15:18:44 +0000
commit0ff5e5e7cc09c31d3fe260cf6602f461e6e20bb6 (patch)
tree7340799f87257c4fc5a69574ca9bf2ae0a9d3f5d /src/mongo/db/error_labels.h
parentad581efda69010443fac7f964180666476b99785 (diff)
downloadmongo-0ff5e5e7cc09c31d3fe260cf6602f461e6e20bb6.tar.gz
SERVER-41245: Add RetryableWriteError error label
Diffstat (limited to 'src/mongo/db/error_labels.h')
-rw-r--r--src/mongo/db/error_labels.h87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/mongo/db/error_labels.h b/src/mongo/db/error_labels.h
new file mode 100644
index 00000000000..7490d862372
--- /dev/null
+++ b/src/mongo/db/error_labels.h
@@ -0,0 +1,87 @@
+/**
+ * Copyright (C) 2018-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#pragma once
+
+#include "mongo/db/logical_session_id.h"
+
+namespace mongo {
+
+namespace ErrorLabel {
+// PLEASE CONSULT DRIVERS BEFORE ADDING NEW ERROR LABELS.
+static constexpr StringData kTransientTransaction = "TransientTransactionError"_sd;
+static constexpr StringData kRetryableWrite = "RetryableWriteError"_sd;
+static constexpr StringData kNonResumableChangeStream = "NonResumableChangeStreamError"_sd;
+} // namespace ErrorLabel
+
+class ErrorLabelBuilder {
+public:
+ ErrorLabelBuilder(const OperationSessionInfoFromClient& sessionOptions,
+ const std::string& commandName,
+ boost::optional<ErrorCodes::Error> code,
+ boost::optional<ErrorCodes::Error> wcCode,
+ bool isInternalClient)
+ : _sessionOptions(sessionOptions),
+ _commandName(commandName),
+ _code(code),
+ _wcCode(wcCode),
+ _isInternalClient(isInternalClient) {}
+
+ void build(BSONArrayBuilder& labels) const;
+
+ bool isTransientTransactionError() const;
+ bool isRetryableWriteError() const;
+ bool isNonResumableChangeStreamError() const;
+
+private:
+ bool _isCommitOrAbort() const;
+ const OperationSessionInfoFromClient& _sessionOptions;
+ const std::string& _commandName;
+ boost::optional<ErrorCodes::Error> _code;
+ boost::optional<ErrorCodes::Error> _wcCode;
+ bool _isInternalClient;
+};
+
+/**
+ * Returns the error labels for the given error.
+ */
+BSONObj getErrorLabels(const OperationSessionInfoFromClient& sessionOptions,
+ const std::string& commandName,
+ boost::optional<ErrorCodes::Error> code,
+ boost::optional<ErrorCodes::Error> wcCode,
+ bool isInternalClient);
+
+/**
+ * Whether a write error in a transaction should be labelled with "TransientTransactionError".
+ */
+bool isTransientTransactionError(ErrorCodes::Error code,
+ bool hasWriteConcernError,
+ bool isCommitOrAbort);
+
+} // namespace mongo