|
|
@@ -186,6 +186,11 @@ struct FailureSink {
|
|
|
}
|
|
|
// An in-band refusal (status OK, success=false): the code is in the
|
|
|
// response from a 2.12.0+ server, Unknown from an older one.
|
|
|
+ // Copy a reason a nested call already recorded (v2.13.1).
|
|
|
+ void copy(const Client::FailureDetail& d) const {
|
|
|
+ if (failure) { failure->code = d.code; failure->message = d.message; }
|
|
|
+ if (detail) *detail = d;
|
|
|
+ }
|
|
|
void recordInBand(int32_t errorCode, const std::string& message,
|
|
|
const std::string& detailJson) const {
|
|
|
record(errorCode != 0 ? static_cast<Client::ErrorCode>(errorCode) : Client::ErrorCode::Unknown,
|
|
|
@@ -221,9 +226,20 @@ nlohmann::json documentToJson(const smartbotic::databasepb::Document& doc) {
|
|
|
// writes (insert with explicit ID, updateIfVersion, upsert, remove, patch).
|
|
|
// All other codes (INVALID_ARGUMENT, FAILED_PRECONDITION, NOT_FOUND, ...) are
|
|
|
// final — no retry.
|
|
|
-bool retryableStatus(const grpc::Status& s) {
|
|
|
+//
|
|
|
+// v2.13.1 - DEADLINE_EXCEEDED is retried only for an IDEMPOTENT write. The
|
|
|
+// deadline can expire while the server finishes the write (seen by
|
|
|
+// smartbotic-vectorapi on large patches: every one reported failure and had
|
|
|
+// been applied), so a retry repeats a write that already happened: an id-less
|
|
|
+// insert duplicates the row, an increment counts twice, and an insert with an
|
|
|
+// id, a conditional write or a delete reports a false AlreadyExists / version
|
|
|
+// conflict / not-found. Those are not retried; the failure carries reason
|
|
|
+// outcome_unknown (see outcomeUnknownDetail). UNAVAILABLE and
|
|
|
+// RESOURCE_EXHAUSTED still retry: the server did not act on the request.
|
|
|
+bool retryableStatus(const grpc::Status& s, bool idempotent) {
|
|
|
switch (s.error_code()) {
|
|
|
- case grpc::StatusCode::DEADLINE_EXCEEDED: // server queued behind eviction/WAL
|
|
|
+ case grpc::StatusCode::DEADLINE_EXCEEDED: // may have been applied
|
|
|
+ return idempotent;
|
|
|
case grpc::StatusCode::RESOURCE_EXHAUSTED: // admission control or gRPC concurrency cap
|
|
|
case grpc::StatusCode::UNAVAILABLE: // transient connection issue
|
|
|
return true;
|
|
|
@@ -232,6 +248,16 @@ bool retryableStatus(const grpc::Status& s) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// v2.13.1 - a write that ended in DEADLINE_EXCEEDED may or may not have been
|
|
|
+// applied; say so in the reason, so a caller checks before redoing it instead
|
|
|
+// of reading "the write failed". Leaves a server-supplied detail alone.
|
|
|
+std::string outcomeUnknownDetail(const grpc::Status& s, const std::string& detailJson) {
|
|
|
+ if (s.error_code() == grpc::StatusCode::DEADLINE_EXCEEDED && detailJson.empty()) {
|
|
|
+ return R"({"reason":"outcome_unknown"})";
|
|
|
+ }
|
|
|
+ return detailJson;
|
|
|
+}
|
|
|
+
|
|
|
// Exponential backoff with jitter. attempt=0 => baseMs; attempt=1 => baseMs*2; ...
|
|
|
uint32_t computeBackoffMs(uint32_t baseMs, uint32_t attempt, uint32_t capMs, double jitterPct) {
|
|
|
uint64_t exp = static_cast<uint64_t>(baseMs) << attempt;
|
|
|
@@ -337,6 +363,21 @@ public:
|
|
|
|
|
|
const char* scheme = config_.tls_enabled ? "tls" : "plaintext";
|
|
|
const char* authed = config_.auth_token.empty() ? "no-auth" : "bearer-auth";
|
|
|
+ // v2.13.1 - actually try. Creating a gRPC channel opens nothing, so
|
|
|
+ // this used to return true and log "connected" against a dead port
|
|
|
+ // (reported by smartbotic): connect() and isConnected() were not
|
|
|
+ // reachability checks at all. Now it waits up to timeoutMs for the
|
|
|
+ // channel to connect. On failure the client stays usable - gRPC
|
|
|
+ // keeps reconnecting on later calls - but the answer is honest.
|
|
|
+ const bool reachable = channel_->WaitForConnected(
|
|
|
+ std::chrono::system_clock::now() + std::chrono::milliseconds(config_.timeoutMs));
|
|
|
+ reached_ = reachable;
|
|
|
+ if (!reachable) {
|
|
|
+ spdlog::warn("Database client could not reach {} ({}, {}) within {} ms; "
|
|
|
+ "it will keep trying on later calls",
|
|
|
+ config_.address, scheme, authed, config_.timeoutMs);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
spdlog::info("Database client connected to {} ({}, {})",
|
|
|
config_.address, scheme, authed);
|
|
|
return true;
|
|
|
@@ -412,6 +453,7 @@ public:
|
|
|
|
|
|
void disconnect() {
|
|
|
connected_ = false;
|
|
|
+ reached_ = false;
|
|
|
stub_.reset();
|
|
|
channel_.reset();
|
|
|
}
|
|
|
@@ -420,11 +462,13 @@ public:
|
|
|
if (!connected_ || !channel_) {
|
|
|
return false;
|
|
|
}
|
|
|
- // Check if channel is in a usable state (not failed or shutdown)
|
|
|
+ // READY is connected. IDLE (no calls for a while) and CONNECTING
|
|
|
+ // count only if the last connection attempt succeeded (v2.13.1):
|
|
|
+ // against a frozen server the kernel accepts the TCP connection and
|
|
|
+ // the channel sits in CONNECTING forever, which used to read as true.
|
|
|
auto state = channel_->GetState(false);
|
|
|
- return state == GRPC_CHANNEL_READY ||
|
|
|
- state == GRPC_CHANNEL_IDLE ||
|
|
|
- state == GRPC_CHANNEL_CONNECTING;
|
|
|
+ if (state == GRPC_CHANNEL_READY) return true;
|
|
|
+ return reached_ && (state == GRPC_CHANNEL_IDLE || state == GRPC_CHANNEL_CONNECTING);
|
|
|
}
|
|
|
|
|
|
// ===== Document Operations =====
|
|
|
@@ -450,11 +494,11 @@ public:
|
|
|
std::string detailJson; // v2.12.0 - the refusal detail, if any
|
|
|
for (uint32_t attempt = 0; attempt <= config_.writeRetries; ++attempt) {
|
|
|
grpc::ClientContext context;
|
|
|
- setDeadline(context);
|
|
|
+ setWriteDeadline(context, request.ByteSizeLong()); // v2.13.1
|
|
|
response.Clear();
|
|
|
status = stub_->Insert(&context, request, &response);
|
|
|
detailJson = trailerDetail(context); // v2.12.0
|
|
|
- if (status.ok() || !retryableStatus(status)) {
|
|
|
+ if (status.ok() || !retryableStatus(status, /*idempotent=*/false)) {
|
|
|
break;
|
|
|
}
|
|
|
if (attempt < config_.writeRetries) {
|
|
|
@@ -467,6 +511,7 @@ public:
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(backoffMs));
|
|
|
}
|
|
|
}
|
|
|
+ detailJson = outcomeUnknownDetail(status, detailJson); // v2.13.1
|
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::insert failed after retries: {}", status.error_message());
|
|
|
throwStatus(status, status.error_message(), detailJson);
|
|
|
@@ -553,9 +598,26 @@ public:
|
|
|
// Attempt version-checked update. Each attempt overwrites
|
|
|
// failureOut, so what the caller reads is the LAST attempt's
|
|
|
// reason, and a success leaves it clean.
|
|
|
- if (updateIfVersion(collection, id, cleanData, version, actor, failureOut)) {
|
|
|
+ Client::FailureDetail attemptFailure;
|
|
|
+ if (updateIfVersion(collection, id, cleanData, version, actor, &attemptFailure)) {
|
|
|
+ failureOut.reset();
|
|
|
return true;
|
|
|
}
|
|
|
+ failureOut.copy(attemptFailure);
|
|
|
+ // v2.13.1 - only a version conflict (another writer between our
|
|
|
+ // get() and this write) is worth another round. Every other
|
|
|
+ // refusal - read-only, memory pressure, a document too large, an
|
|
|
+ // outage - was retried here too and then logged as "version
|
|
|
+ // conflicts", though no retry could change the answer.
|
|
|
+ // A pre-2.12.0 server reports the conflict in-band with no code
|
|
|
+ // (Unknown) and the text "Version conflict".
|
|
|
+ const bool conflict =
|
|
|
+ attemptFailure.code == Client::ErrorCode::Aborted ||
|
|
|
+ (attemptFailure.code == Client::ErrorCode::Unknown &&
|
|
|
+ attemptFailure.message.find("ersion conflict") != std::string::npos);
|
|
|
+ if (!conflict) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
// Version conflict — another client wrote between our get() and update
|
|
|
if (attempt < config_.maxRetries) {
|
|
|
@@ -588,11 +650,11 @@ public:
|
|
|
std::string detailJson; // v2.12.0 - the refusal detail, if any
|
|
|
for (uint32_t attempt = 0; attempt <= config_.writeRetries; ++attempt) {
|
|
|
grpc::ClientContext context;
|
|
|
- setDeadline(context);
|
|
|
+ setWriteDeadline(context, request.ByteSizeLong()); // v2.13.1
|
|
|
response.Clear();
|
|
|
status = stub_->Update(&context, request, &response);
|
|
|
detailJson = trailerDetail(context); // v2.12.0
|
|
|
- if (status.ok() || !retryableStatus(status)) {
|
|
|
+ if (status.ok() || !retryableStatus(status, /*idempotent=*/false)) {
|
|
|
break;
|
|
|
}
|
|
|
if (attempt < config_.writeRetries) {
|
|
|
@@ -605,6 +667,7 @@ public:
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(backoffMs));
|
|
|
}
|
|
|
}
|
|
|
+ detailJson = outcomeUnknownDetail(status, detailJson); // v2.13.1
|
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::updateIfVersion failed after retries: {}", status.error_message());
|
|
|
failureOut.record(status, detailJson);
|
|
|
@@ -648,11 +711,11 @@ public:
|
|
|
std::string detailJson; // v2.12.0 - the refusal detail, if any
|
|
|
for (uint32_t attempt = 0; attempt <= config_.writeRetries; ++attempt) {
|
|
|
grpc::ClientContext context;
|
|
|
- setDeadline(context);
|
|
|
+ setWriteDeadline(context, request.ByteSizeLong()); // v2.13.1
|
|
|
response.Clear();
|
|
|
status = stub_->PatchDocument(&context, request, &response);
|
|
|
detailJson = trailerDetail(context); // v2.12.0
|
|
|
- if (status.ok() || !retryableStatus(status)) {
|
|
|
+ if (status.ok() || !retryableStatus(status, /*idempotent=*/!request.has_expected_version() && request.increments_json().empty())) {
|
|
|
break;
|
|
|
}
|
|
|
if (attempt < config_.writeRetries) {
|
|
|
@@ -665,6 +728,7 @@ public:
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(backoffMs));
|
|
|
}
|
|
|
}
|
|
|
+ detailJson = outcomeUnknownDetail(status, detailJson); // v2.13.1
|
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::patch failed after retries: {}", status.error_message());
|
|
|
failureOut.record(status, detailJson);
|
|
|
@@ -706,11 +770,11 @@ public:
|
|
|
std::string detailJson; // v2.12.0 - the refusal detail, if any
|
|
|
for (uint32_t attempt = 0; attempt <= config_.writeRetries; ++attempt) {
|
|
|
grpc::ClientContext context;
|
|
|
- setDeadline(context);
|
|
|
+ setWriteDeadline(context, request.ByteSizeLong()); // v2.13.1
|
|
|
response.Clear();
|
|
|
status = stub_->Upsert(&context, request, &response);
|
|
|
detailJson = trailerDetail(context); // v2.12.0
|
|
|
- if (status.ok() || !retryableStatus(status)) {
|
|
|
+ if (status.ok() || !retryableStatus(status, /*idempotent=*/!id.empty())) {
|
|
|
break;
|
|
|
}
|
|
|
if (attempt < config_.writeRetries) {
|
|
|
@@ -723,6 +787,7 @@ public:
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(backoffMs));
|
|
|
}
|
|
|
}
|
|
|
+ detailJson = outcomeUnknownDetail(status, detailJson); // v2.13.1
|
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::upsert failed after retries: {}", status.error_message());
|
|
|
throwStatus(status, status.error_message(), detailJson);
|
|
|
@@ -753,11 +818,11 @@ public:
|
|
|
std::string detailJson; // v2.12.0 - the refusal detail, if any
|
|
|
for (uint32_t attempt = 0; attempt <= config_.writeRetries; ++attempt) {
|
|
|
grpc::ClientContext context;
|
|
|
- setDeadline(context);
|
|
|
+ setWriteDeadline(context, request.ByteSizeLong()); // v2.13.1
|
|
|
response.Clear();
|
|
|
status = stub_->Delete(&context, request, &response);
|
|
|
detailJson = trailerDetail(context); // v2.12.0
|
|
|
- if (status.ok() || !retryableStatus(status)) {
|
|
|
+ if (status.ok() || !retryableStatus(status, /*idempotent=*/false)) {
|
|
|
break;
|
|
|
}
|
|
|
if (attempt < config_.writeRetries) {
|
|
|
@@ -770,6 +835,7 @@ public:
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(backoffMs));
|
|
|
}
|
|
|
}
|
|
|
+ detailJson = outcomeUnknownDetail(status, detailJson); // v2.13.1
|
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::remove failed after retries: {}", status.error_message());
|
|
|
if (errorOut != nullptr) *errorOut = status.error_message();
|
|
|
@@ -812,22 +878,24 @@ public:
|
|
|
smartbotic::databasepb::BatchDeleteResponse response;
|
|
|
grpc::Status status;
|
|
|
std::string detailJson;
|
|
|
- // Retried like the other deletes: deleting an id twice is harmless, and
|
|
|
- // a retry after a lost reply reports the ids the first attempt removed
|
|
|
- // as not found - which is why deletedIds is "what THIS call removed".
|
|
|
+ // v2.13.1 - retried only when the server cannot have acted (UNAVAILABLE,
|
|
|
+ // RESOURCE_EXHAUSTED). After a DEADLINE_EXCEEDED the first attempt may
|
|
|
+ // have deleted the ids, and a retry would report them as not found, so
|
|
|
+ // that failure is returned with reason outcome_unknown instead.
|
|
|
for (uint32_t attempt = 0; attempt <= config_.writeRetries; ++attempt) {
|
|
|
grpc::ClientContext context;
|
|
|
- setDeadline(context);
|
|
|
+ setWriteDeadline(context, request.ByteSizeLong()); // v2.13.1
|
|
|
response.Clear();
|
|
|
status = stub_->BatchDelete(&context, request, &response);
|
|
|
detailJson = trailerDetail(context);
|
|
|
- if (status.ok() || !retryableStatus(status)) break;
|
|
|
+ if (status.ok() || !retryableStatus(status, /*idempotent=*/false)) break;
|
|
|
if (attempt < config_.writeRetries) {
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(computeBackoffMs(
|
|
|
config_.writeRetryBackoffMs, attempt,
|
|
|
config_.writeRetryMaxBackoffMs, config_.writeRetryJitter)));
|
|
|
}
|
|
|
}
|
|
|
+ detailJson = outcomeUnknownDetail(status, detailJson); // v2.13.1
|
|
|
if (!status.ok()) {
|
|
|
spdlog::error("Client::removeManyAs failed: {}", status.error_message());
|
|
|
failureOut.record(status, detailJson);
|
|
|
@@ -2842,6 +2910,18 @@ private:
|
|
|
attachAuth(context);
|
|
|
}
|
|
|
|
|
|
+ // v2.13.1 - a write's deadline grows with its size: timeoutMs plus one
|
|
|
+ // second per 4 MiB sent. A 20 MiB patch on a 60 MiB row legitimately takes
|
|
|
+ // longer than a small write, and a fixed deadline made it time out while the
|
|
|
+ // server completed it - the shape that produced "failed" writes that had
|
|
|
+ // been applied.
|
|
|
+ void setWriteDeadline(grpc::ClientContext& context, size_t requestBytes) {
|
|
|
+ const auto extraMs = static_cast<uint64_t>(requestBytes / (4u * 1024 * 1024)) * 1000;
|
|
|
+ context.set_deadline(std::chrono::system_clock::now() +
|
|
|
+ std::chrono::milliseconds(config_.timeoutMs + extraMs));
|
|
|
+ attachAuth(context);
|
|
|
+ }
|
|
|
+
|
|
|
// v2.4 — attach the bearer-token authorization metadata if configured.
|
|
|
// Use this on contexts that set their own deadline (streams,
|
|
|
// long-running ops) so the auth metadata still lands.
|
|
|
@@ -2855,6 +2935,7 @@ private:
|
|
|
std::shared_ptr<grpc::Channel> channel_;
|
|
|
std::unique_ptr<smartbotic::databasepb::DatabaseService::Stub> stub_;
|
|
|
std::atomic<bool> connected_{false};
|
|
|
+ std::atomic<bool> reached_{false}; // v2.13.1 - the last connect() attempt succeeded
|
|
|
};
|
|
|
|
|
|
// ===== Client Public Interface Implementation =====
|