|
@@ -16,7 +16,31 @@ namespace {
|
|
|
/// the generic 503 - a caller that must now de-duplicate its data needs the
|
|
/// the generic 503 - a caller that must now de-duplicate its data needs the
|
|
|
/// examples whichever form it used.
|
|
/// examples whichever form it used.
|
|
|
ApiError indexDeclineError(const smartbotic::database::Client::CreateIndexResult& r,
|
|
ApiError indexDeclineError(const smartbotic::database::Client::CreateIndexResult& r,
|
|
|
|
|
+ const smartbotic::database::Client::FailureDetail& f,
|
|
|
const std::string& what) {
|
|
const std::string& what) {
|
|
|
|
|
+ // A PERMANENT refusal must not be reported as a transient one. Database
|
|
|
|
|
+ // 2.17.0 refuses an index on a field it stores encrypted, and before the
|
|
|
|
|
+ // FailureDetail overloads existed that arrived here indistinguishable from
|
|
|
|
|
+ // an outage and fell through to 503 `db_error` - whose whole meaning is
|
|
|
|
|
+ // "retry as-is", for something no retry can ever fix. That is the mirror
|
|
|
|
|
+ // of the FailedPrecondition bug: this one told a caller its own permanent
|
|
|
|
|
+ // mistake was our outage, so it would wait forever instead of changing the
|
|
|
|
|
+ // field.
|
|
|
|
|
+ //
|
|
|
|
|
+ // Upstream deliberately added `FailureDetail&` OVERLOADS rather than
|
|
|
|
|
+ // growing CreateIndexResult: that struct is returned BY VALUE, so adding a
|
|
|
|
|
+ // member changes its size under one soname, which is what crashed a
|
|
|
|
|
+ // consumer in 2.7.1. We asked for the field and were right to be refused.
|
|
|
|
|
+ if (!f.reason.empty() || f.code != smartbotic::database::Client::ErrorCode::Ok) {
|
|
|
|
|
+ // An IN-BAND refusal (status OK, success=false) arrives as code
|
|
|
|
|
+ // Unknown with no reason, and the duplicate-values case below still
|
|
|
|
|
+ // owns that path - so only translate when there is something to
|
|
|
|
|
+ // translate.
|
|
|
|
|
+ if (f.reason == "encrypted_field" ||
|
|
|
|
|
+ f.code == smartbotic::database::Client::ErrorCode::InvalidArgument ||
|
|
|
|
|
+ f.code == smartbotic::database::Client::ErrorCode::FailedPrecondition)
|
|
|
|
|
+ return apiErrorFor(f, "no such collection");
|
|
|
|
|
+ }
|
|
|
if (!r.duplicateExamples.empty()) {
|
|
if (!r.duplicateExamples.empty()) {
|
|
|
nlohmann::json ex = nlohmann::json::array();
|
|
nlohmann::json ex = nlohmann::json::array();
|
|
|
for (const auto& id : r.duplicateExamples) ex.push_back(id);
|
|
for (const auto& id : r.duplicateExamples) ex.push_back(id);
|
|
@@ -63,9 +87,10 @@ void registerIndexRoutes(ApiServer& s) {
|
|
|
throw ApiError(ErrCode::NotFound, "not_found", "collection not found");
|
|
throw ApiError(ErrCode::NotFound, "not_found", "collection not found");
|
|
|
|
|
|
|
|
if (want.composite()) {
|
|
if (want.composite()) {
|
|
|
- auto r = d->db.client().createCompositeUniqueIndex(qc, want.fields);
|
|
|
|
|
|
|
+ smartbotic::database::Client::FailureDetail f;
|
|
|
|
|
+ auto r = d->db.client().createCompositeUniqueIndex(qc, want.fields, f);
|
|
|
if (!r.success)
|
|
if (!r.success)
|
|
|
- throw indexDeclineError(r, "the combination of fields");
|
|
|
|
|
|
|
+ throw indexDeclineError(r, f, "the combination of fields");
|
|
|
nlohmann::json out = compositeJson(want.fields);
|
|
nlohmann::json out = compositeJson(want.fields);
|
|
|
out["rows_indexed"] = r.rowsIndexed;
|
|
out["rows_indexed"] = r.rowsIndexed;
|
|
|
out["already_existed"] = r.alreadyExisted;
|
|
out["already_existed"] = r.alreadyExisted;
|
|
@@ -75,9 +100,10 @@ void registerIndexRoutes(ApiServer& s) {
|
|
|
|
|
|
|
|
const std::string& field = want.field;
|
|
const std::string& field = want.field;
|
|
|
if (want.unique) {
|
|
if (want.unique) {
|
|
|
- auto r = d->db.client().createUniqueIndex(qc, field);
|
|
|
|
|
|
|
+ smartbotic::database::Client::FailureDetail f;
|
|
|
|
|
+ auto r = d->db.client().createUniqueIndex(qc, field, f);
|
|
|
if (!r.success)
|
|
if (!r.success)
|
|
|
- throw indexDeclineError(r, "field '" + field + "'");
|
|
|
|
|
|
|
+ throw indexDeclineError(r, f, "field '" + field + "'");
|
|
|
sendJson(res, 201, {{"field", field}, {"unique", true},
|
|
sendJson(res, 201, {{"field", field}, {"unique", true},
|
|
|
{"rows_indexed", r.rowsIndexed},
|
|
{"rows_indexed", r.rowsIndexed},
|
|
|
{"already_existed", r.alreadyExisted}});
|
|
{"already_existed", r.alreadyExisted}});
|
|
@@ -93,8 +119,13 @@ void registerIndexRoutes(ApiServer& s) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
uint64_t rows = 0;
|
|
uint64_t rows = 0;
|
|
|
- if (!d->db.client().createIndex(qc, field, rows))
|
|
|
|
|
- throw ApiError(ErrCode::Unavailable, "db_error", "failed to create index");
|
|
|
|
|
|
|
+ smartbotic::database::Client::FailureDetail f;
|
|
|
|
|
+ if (!d->db.client().createIndex(qc, field, rows, f)) {
|
|
|
|
|
+ // The non-unique path had no way to report a reason at all before
|
|
|
|
|
+ // 2.17.2, so an encrypted field here was a flat 503.
|
|
|
|
|
+ smartbotic::database::Client::CreateIndexResult empty;
|
|
|
|
|
+ throw indexDeclineError(empty, f, "field '" + field + "'");
|
|
|
|
|
+ }
|
|
|
sendJson(res, 201, {{"field", field}, {"unique", false},
|
|
sendJson(res, 201, {{"field", field}, {"unique", false},
|
|
|
{"rows_indexed", rows}, {"already_existed", alreadyExisted}});
|
|
{"rows_indexed", rows}, {"already_existed", alreadyExisted}});
|
|
|
});
|
|
});
|
|
@@ -188,10 +219,44 @@ void registerIndexRoutes(ApiServer& s) {
|
|
|
std::string project = req.matches[1], coll = req.matches[2], field = req.matches[3];
|
|
std::string project = req.matches[1], coll = req.matches[2], field = req.matches[3];
|
|
|
requireCapability(*d, k, req, project, coll, KeyOp::Read);
|
|
requireCapability(*d, k, req, project, coll, KeyOp::Read);
|
|
|
|
|
|
|
|
|
|
+ // REFUSE a parameter a caller sends EXPECTING AN EFFECT that this route
|
|
|
|
|
+ // does not implement. Measured on prod: `?offset=1000` answered 200
|
|
|
|
|
+ // with the FIRST page, because the handler reads only `limit` and
|
|
|
|
|
+ // `order` and the client's indexValues() has no offset to honour - so
|
|
|
|
|
+ // a caller paginating looped forever believing it was progressing. A
|
|
|
|
|
+ // 200 with the wrong answer to a well-formed request, which is exactly
|
|
|
|
|
+ // what the search body refuses as `unknown_option`; that lesson was
|
|
|
|
|
+ // applied to the handler where it was learned and not to this one.
|
|
|
|
|
+ //
|
|
|
|
|
+ // NOT a blanket refusal of unknown query params, deliberately. Every
|
|
|
|
|
+ // key in a JSON body is deliberate, but a query string legitimately
|
|
|
|
|
+ // collects things nobody meant as options - a cache-buster, a proxy's
|
|
|
|
|
+ // addition, a UI's `_=<timestamp>` - so refusing everything unknown
|
|
|
|
|
+ // would be a regression. Refusing `offset` is the point; refusing
|
|
|
|
|
+ // `_=1760000000` is not.
|
|
|
|
|
+ static constexpr const char* kPretenders[] = {"offset", "page", "cursor",
|
|
|
|
|
+ "sort", "fields", "after", "skip"};
|
|
|
|
|
+ std::vector<std::string> pretending;
|
|
|
|
|
+ for (const char* k : kPretenders)
|
|
|
|
|
+ if (req.has_param(k)) pretending.push_back(k);
|
|
|
|
|
+ if (!pretending.empty()) {
|
|
|
|
|
+ std::string names;
|
|
|
|
|
+ for (size_t i = 0; i < pretending.size(); ++i)
|
|
|
|
|
+ names += (i ? ", " : "") + pretending[i];
|
|
|
|
|
+ throw ApiError(ErrCode::Unprocessable, "unknown_option",
|
|
|
|
|
+ "this route does not support " + names +
|
|
|
|
|
+ ". It is not paginated and has no total: it returns at most "
|
|
|
|
|
+ "`limit` distinct values (max 1000) and sets `truncated` when "
|
|
|
|
|
+ "there may be more. Accepted parameters are `limit` and `order`.",
|
|
|
|
|
+ {{"unsupported", pretending},
|
|
|
|
|
+ {"accepted", nlohmann::json::array({"limit", "order"})}});
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ constexpr uint32_t kValuesLimitCap = 1000;
|
|
|
uint32_t limit = 100;
|
|
uint32_t limit = 100;
|
|
|
if (req.has_param("limit")) {
|
|
if (req.has_param("limit")) {
|
|
|
unsigned long wide = std::strtoul(req.get_param_value("limit").c_str(), nullptr, 10);
|
|
unsigned long wide = std::strtoul(req.get_param_value("limit").c_str(), nullptr, 10);
|
|
|
- if (wide == 0 || wide > 1000)
|
|
|
|
|
|
|
+ if (wide == 0 || wide > kValuesLimitCap)
|
|
|
throw ApiError(ErrCode::Unprocessable, "validation", "limit must be 1..1000");
|
|
throw ApiError(ErrCode::Unprocessable, "validation", "limit must be 1..1000");
|
|
|
limit = (uint32_t)wide;
|
|
limit = (uint32_t)wide;
|
|
|
}
|
|
}
|
|
@@ -200,7 +265,22 @@ void registerIndexRoutes(ApiServer& s) {
|
|
|
auto vals = d->db.client().indexValues(qualify(project, coll), field, limit, ascending);
|
|
auto vals = d->db.client().indexValues(qualify(project, coll), field, limit, ascending);
|
|
|
nlohmann::json out = nlohmann::json::array();
|
|
nlohmann::json out = nlohmann::json::array();
|
|
|
for (const auto& v : vals) out.push_back({{"value", v.value}, {"count", v.count}});
|
|
for (const auto& v : vals) out.push_back({{"value", v.value}, {"count", v.count}});
|
|
|
- sendJson(res, 200, {{"values", out}});
|
|
|
|
|
|
|
+ // `truncated` is emitted UNCONDITIONALLY, true or false, never omitted -
|
|
|
|
|
+ // the same rule as `filter_mode` and `scan_capped`, so an ABSENT field
|
|
|
|
|
+ // means exactly one thing: a server older than this release. Emitting
|
|
|
|
|
+ // it only when true would recreate the ambiguity those two were fixed
|
|
|
|
|
+ // for. It was the absence of any such flag that let a consumer read a
|
|
|
|
|
+ // 1000-value page summing to 72% of a collection as a DATA anomaly on
|
|
|
|
|
+ // one field rather than a limit of this route - it looks like a field
|
|
|
|
|
+ // problem because only high-cardinality fields cross the cap.
|
|
|
|
|
+ //
|
|
|
|
|
+ // There is deliberately NO total: the client offers no distinct-value
|
|
|
|
|
+ // count and count() counts rows, not distinct values. A number we
|
|
|
|
|
+ // cannot stand behind would be worse than saying there is none.
|
|
|
|
|
+ const bool truncated = vals.size() >= limit;
|
|
|
|
|
+ nlohmann::json body = {{"values", out}, {"truncated", truncated}};
|
|
|
|
|
+ if (truncated) body["limit"] = limit;
|
|
|
|
|
+ sendJson(res, 200, body);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
svr.Delete(R"(/api/v1/projects/([^/]+)/collections/([^/]+)/indexes/([^/]+))",
|
|
svr.Delete(R"(/api/v1/projects/([^/]+)/collections/([^/]+)/indexes/([^/]+))",
|