|
@@ -1,4 +1,5 @@
|
|
|
#include "collection_registry.hpp"
|
|
#include "collection_registry.hpp"
|
|
|
|
|
+#include "db_errors.hpp"
|
|
|
|
|
|
|
|
#include "errors.hpp"
|
|
#include "errors.hpp"
|
|
|
|
|
|
|
@@ -44,40 +45,39 @@ bool CollectionRegistry::add(const std::string& project, const CollectionMeta& m
|
|
|
|
|
|
|
|
std::optional<CollectionMeta> CollectionRegistry::get(const std::string& project,
|
|
std::optional<CollectionMeta> CollectionRegistry::get(const std::string& project,
|
|
|
const std::string& name) {
|
|
const std::string& name) {
|
|
|
- const std::string c = coll(project);
|
|
|
|
|
- if (auto doc = db_.client().get(c, name)) return CollectionMeta::fromJson(*doc);
|
|
|
|
|
-
|
|
|
|
|
- // An empty answer from the client is AMBIGUOUS: `Client::get` returns
|
|
|
|
|
- // nullopt both for "no such document" and for a transport failure, which it
|
|
|
|
|
- // only logs. (It throws for PERMISSION_DENIED precisely so a denial cannot
|
|
|
|
|
- // look like no data; the same reasoning was never extended to UNAVAILABLE.)
|
|
|
|
|
|
|
+ // Since database client 2.13.0 this is simply honest: a read whose "nothing
|
|
|
|
|
+ // there" value is also a valid answer THROWS on a failed call, and returns
|
|
|
|
|
+ // nullopt only for a genuine absence. So nullopt means absent, and that is
|
|
|
|
|
+ // the whole of it.
|
|
|
//
|
|
//
|
|
|
- // Left alone, that ambiguity reaches a caller as 404 "no such collection"
|
|
|
|
|
- // for a collection that exists - and a 404 is unretryable BY DESIGN for a
|
|
|
|
|
|
|
+ // What used to be here, and why it is gone: `Client::get` returned nullopt
|
|
|
|
|
+ // BOTH for "no such document" and for a transport failure, which it only
|
|
|
|
|
+ // logged. Left alone that reached a caller as 404 "no such collection" for
|
|
|
|
|
+ // a collection that exists - and a 404 is unretryable BY DESIGN for a
|
|
|
// careful client, because adding it to a retry set would retry every
|
|
// careful client, because adding it to a retry set would retry every
|
|
|
// genuine absence. A consumer lost a 6-minute ingest of an 8 GB mailbox to
|
|
// genuine absence. A consumer lost a 6-minute ingest of an 8 GB mailbox to
|
|
|
- // exactly this, and the collection was present before and after.
|
|
|
|
|
- //
|
|
|
|
|
- // So: ask again. A genuine absence answers nullopt twice and costs one
|
|
|
|
|
- // cheap registry read on a path that was already going to 404. A transient
|
|
|
|
|
- // failure almost always succeeds on the second call, which is both the
|
|
|
|
|
- // correct answer AND cheaper for the caller than a retryable error.
|
|
|
|
|
- if (auto retried = db_.client().get(c, name)) {
|
|
|
|
|
- spdlog::warn("registry lookup for {}:{} failed once and succeeded on retry - a transient "
|
|
|
|
|
- "database failure was about to be reported as 'no such collection'",
|
|
|
|
|
- project, name);
|
|
|
|
|
- return CollectionMeta::fromJson(*retried);
|
|
|
|
|
|
|
+ // exactly that, with the collection present before and after. We worked
|
|
|
|
|
+ // around it by reading twice and then asking healthCheck() before daring to
|
|
|
|
|
+ // report absence. The database took the report and fixed it at the source,
|
|
|
|
|
+ // so the workaround is deleted rather than kept "just in case": a retry
|
|
|
|
|
+ // that cannot distinguish anything is just a slower wrong answer, and
|
|
|
|
|
+ // keeping it would hide the real behaviour from the next reader.
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (auto doc = db_.client().get(coll(project), name)) return CollectionMeta::fromJson(*doc);
|
|
|
|
|
+ return std::nullopt;
|
|
|
|
|
+ } catch (const smartbotic::database::Client::Error& e) {
|
|
|
|
|
+ // Keep the message that says what we could not determine. The global
|
|
|
|
|
+ // handler would also map this to 503 now, but generically: "cannot
|
|
|
|
|
+ // determine whether collection 'x' exists" tells the caller which
|
|
|
|
|
+ // question went unanswered, and that is worth preserving on the one
|
|
|
|
|
+ // path where a wrong answer is an unretryable 404.
|
|
|
|
|
+ ApiError mapped = apiErrorFor(e);
|
|
|
|
|
+ if (mapped.code == ErrCode::Unavailable)
|
|
|
|
|
+ throw ApiError(ErrCode::Unavailable, "db_error",
|
|
|
|
|
+ "cannot determine whether collection '" + name +
|
|
|
|
|
+ "' exists: the database is not reachable");
|
|
|
|
|
+ throw mapped;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // Still empty. Either it really is absent, or the database is properly
|
|
|
|
|
- // down - and those must not share an answer. healthCheck() is the only
|
|
|
|
|
- // thing that can tell them apart here, and it is asked ONLY on this path,
|
|
|
|
|
- // so a hot lookup never pays for it.
|
|
|
|
|
- if (!db_.client().healthCheck())
|
|
|
|
|
- throw ApiError(ErrCode::Unavailable, "db_error",
|
|
|
|
|
- "cannot determine whether collection '" + name +
|
|
|
|
|
- "' exists: the database is not reachable");
|
|
|
|
|
- return std::nullopt;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
std::vector<CollectionMeta> CollectionRegistry::list(const std::string& project) {
|
|
std::vector<CollectionMeta> CollectionRegistry::list(const std::string& project) {
|