|
@@ -17,6 +17,7 @@
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
#include <algorithm>
|
|
|
#include <chrono>
|
|
#include <chrono>
|
|
|
|
|
+#include <fstream>
|
|
|
#include <functional>
|
|
#include <functional>
|
|
|
#include <memory>
|
|
#include <memory>
|
|
|
#include <mutex>
|
|
#include <mutex>
|
|
@@ -31,7 +32,7 @@ namespace smartbotic::database {
|
|
|
namespace pb = smartbotic::databasepb;
|
|
namespace pb = smartbotic::databasepb;
|
|
|
|
|
|
|
|
namespace {
|
|
namespace {
|
|
|
-// v2.12.0 — refusal detail. See the comment above `service DatabaseService` in
|
|
|
|
|
|
|
+// v2.12.0 - refusal detail. See the comment above `service DatabaseService` in
|
|
|
// database.proto for the key and its JSON shape. Attached to the call's
|
|
// database.proto for the key and its JSON shape. Attached to the call's
|
|
|
// trailing metadata, so it accompanies an error status AND an in-band refusal
|
|
// trailing metadata, so it accompanies an error status AND an in-band refusal
|
|
|
// (status OK, success=false) alike.
|
|
// (status OK, success=false) alike.
|
|
@@ -82,7 +83,7 @@ grpc::Status refuseInBand(grpc::ServerContext* context, Response* response,
|
|
|
// Used by write handlers whose responses don't have set_success/set_error fields.
|
|
// Used by write handlers whose responses don't have set_success/set_error fields.
|
|
|
grpc::Status readOnlyStatus(grpc::ServerContext* context, const std::string& rpcName,
|
|
grpc::Status readOnlyStatus(grpc::ServerContext* context, const std::string& rpcName,
|
|
|
const DatabaseService& svc) {
|
|
const DatabaseService& svc) {
|
|
|
- // v2.12.0 — reason "read_only": FAILED_PRECONDITION alone is ambiguous with
|
|
|
|
|
|
|
+ // v2.12.0 - reason "read_only": FAILED_PRECONDITION alone is ambiguous with
|
|
|
// a relation refusal, which uses the same code.
|
|
// a relation refusal, which uses the same code.
|
|
|
attachErrorDetail(context, {{"reason", "read_only"}});
|
|
attachErrorDetail(context, {{"reason", "read_only"}});
|
|
|
return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION,
|
|
return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION,
|
|
@@ -470,7 +471,7 @@ grpc::Status DatabaseGrpcImpl::Insert(
|
|
|
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
|
|
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
|
|
|
"Invalid JSON: " + std::string(e.what()));
|
|
"Invalid JSON: " + std::string(e.what()));
|
|
|
} catch (const DuplicateDocumentId& e) {
|
|
} catch (const DuplicateDocumentId& e) {
|
|
|
- // v2.12.1 — a duplicate PRIMARY id is the caller's business exactly as
|
|
|
|
|
|
|
+ // v2.12.1 - a duplicate PRIMARY id is the caller's business exactly as
|
|
|
// a unique-index collision is; it used to fall through to INTERNAL.
|
|
// a unique-index collision is; it used to fall through to INTERNAL.
|
|
|
// No field collided - the identity did - so `fields` stays empty and
|
|
// No field collided - the identity did - so `fields` stays empty and
|
|
|
// the reason says which kind of collision this is.
|
|
// the reason says which kind of collision this is.
|
|
@@ -607,7 +608,7 @@ grpc::Status DatabaseGrpcImpl::Update(
|
|
|
return st;
|
|
return st;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // v2.12.0 — still in-band (status OK) so older clients see what they always
|
|
|
|
|
|
|
+ // v2.12.0 - still in-band (status OK) so older clients see what they always
|
|
|
// saw, now with error_code and a reason in the refusal detail.
|
|
// saw, now with error_code and a reason in the refusal detail.
|
|
|
if (service_.isReadOnly()) {
|
|
if (service_.isReadOnly()) {
|
|
|
return refuseInBand(context, response, grpc::StatusCode::FAILED_PRECONDITION, "read_only",
|
|
return refuseInBand(context, response, grpc::StatusCode::FAILED_PRECONDITION, "read_only",
|
|
@@ -645,7 +646,7 @@ grpc::Status DatabaseGrpcImpl::Update(
|
|
|
expiryOverrideFrom(*request)
|
|
expiryOverrideFrom(*request)
|
|
|
);
|
|
);
|
|
|
if (!success) {
|
|
if (!success) {
|
|
|
- // v2.12.0 — updateIfVersion answers false for a missing
|
|
|
|
|
|
|
+ // v2.12.0 - updateIfVersion answers false for a missing
|
|
|
// document AND a stale version, and both used to be reported
|
|
// document AND a stale version, and both used to be reported
|
|
|
// as "Version conflict". The message stays (older clients may
|
|
// as "Version conflict". The message stays (older clients may
|
|
|
// match it); the code and detail now tell them apart.
|
|
// match it); the code and detail now tell them apart.
|
|
@@ -710,7 +711,7 @@ grpc::Status DatabaseGrpcImpl::PatchDocument(
|
|
|
return st;
|
|
return st;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // v2.12.0 — still in-band (status OK) so older clients see what they always
|
|
|
|
|
|
|
+ // v2.12.0 - still in-band (status OK) so older clients see what they always
|
|
|
// saw, now with error_code and a reason in the refusal detail.
|
|
// saw, now with error_code and a reason in the refusal detail.
|
|
|
if (service_.isReadOnly()) {
|
|
if (service_.isReadOnly()) {
|
|
|
return refuseInBand(context, response, grpc::StatusCode::FAILED_PRECONDITION, "read_only",
|
|
return refuseInBand(context, response, grpc::StatusCode::FAILED_PRECONDITION, "read_only",
|
|
@@ -726,7 +727,7 @@ grpc::Status DatabaseGrpcImpl::PatchDocument(
|
|
|
"cannot write to view '" + request->collection() + "': views are read-only");
|
|
"cannot write to view '" + request->collection() + "': views are read-only");
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- // v2.12.0 — an increment-only request may send no merge patch at all.
|
|
|
|
|
|
|
+ // v2.12.0 - an increment-only request may send no merge patch at all.
|
|
|
nlohmann::json patch = request->patch_json().empty()
|
|
nlohmann::json patch = request->patch_json().empty()
|
|
|
? nlohmann::json::object()
|
|
? nlohmann::json::object()
|
|
|
: smartbotic::db::parse_to_nlohmann(request->patch_json());
|
|
: smartbotic::db::parse_to_nlohmann(request->patch_json());
|
|
@@ -1411,7 +1412,7 @@ grpc::Status DatabaseGrpcImpl::BatchDelete(
|
|
|
uint64_t deleted = 0;
|
|
uint64_t deleted = 0;
|
|
|
std::vector<std::string> plain; // ids with no destructive relation
|
|
std::vector<std::string> plain; // ids with no destructive relation
|
|
|
plain.reserve(ids.size());
|
|
plain.reserve(ids.size());
|
|
|
- std::unordered_set<std::string> gone; // v2.12.0 — for deleted_ids
|
|
|
|
|
|
|
+ std::unordered_set<std::string> gone; // v2.12.0 - for deleted_ids
|
|
|
for (const auto& id : ids) {
|
|
for (const auto& id : ids) {
|
|
|
bool handled = false;
|
|
bool handled = false;
|
|
|
bool parentExisted = false;
|
|
bool parentExisted = false;
|
|
@@ -1434,7 +1435,7 @@ grpc::Status DatabaseGrpcImpl::BatchDelete(
|
|
|
plain.push_back(id);
|
|
plain.push_back(id);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- // v2.12.0 — one remove() per id rather than bulkDelete(), which is the same
|
|
|
|
|
|
|
+ // v2.12.0 - one remove() per id rather than bulkDelete(), which is the same
|
|
|
// loop but reports only a count: the caller now learns WHICH ids existed.
|
|
// loop but reports only a count: the caller now learns WHICH ids existed.
|
|
|
for (const auto& id : plain) {
|
|
for (const auto& id : plain) {
|
|
|
if (store_.remove(request->collection(), id, request->actor())) {
|
|
if (store_.remove(request->collection(), id, request->actor())) {
|
|
@@ -1969,7 +1970,7 @@ grpc::Status DatabaseGrpcImpl::CreateCollection(
|
|
|
return grpc::Status::OK;
|
|
return grpc::Status::OK;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// v2.12.2 — forget everything declared on a collection name: disarm the
|
|
|
|
|
|
|
+// v2.12.2 - forget everything declared on a collection name: disarm the
|
|
|
// store (so no write maintains the old indexes), remove the config record,
|
|
// store (so no write maintains the old indexes), remove the config record,
|
|
|
// then drop the index and composite-constraint sub-dbs. Safe on a name whose
|
|
// then drop the index and composite-constraint sub-dbs. Safe on a name whose
|
|
|
// collection - or whole project - no longer exists.
|
|
// collection - or whole project - no longer exists.
|
|
@@ -2063,7 +2064,7 @@ grpc::Status DatabaseGrpcImpl::DropCollection(
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool dropped = store_.dropCollection(request->name());
|
|
bool dropped = store_.dropCollection(request->name());
|
|
|
- // v2.12.2 — and everything DECLARED on the name, whether or not the
|
|
|
|
|
|
|
+ // v2.12.2 - and everything DECLARED on the name, whether or not the
|
|
|
// collection still existed (dropped == false). Before this the config
|
|
// collection still existed (dropped == false). Before this the config
|
|
|
// record survived the drop, so a later collection of the same name
|
|
// record survived the drop, so a later collection of the same name
|
|
|
// inherited its indexes and unique constraints - a legitimate write refused
|
|
// inherited its indexes and unique constraints - a legitimate write refused
|
|
@@ -2226,7 +2227,7 @@ grpc::Status DatabaseGrpcImpl::DropProject(
|
|
|
if (service_.isReadOnly()) {
|
|
if (service_.isReadOnly()) {
|
|
|
return readOnlyStatus(context, "DropProject", service_);
|
|
return readOnlyStatus(context, "DropProject", service_);
|
|
|
}
|
|
}
|
|
|
- // v2.12.2 — collect the project's in-memory collections BEFORE the drop.
|
|
|
|
|
|
|
+ // v2.12.2 - collect the project's in-memory collections BEFORE the drop.
|
|
|
const std::string prefix = request->name() + ":";
|
|
const std::string prefix = request->name() + ":";
|
|
|
std::vector<std::string> doomed;
|
|
std::vector<std::string> doomed;
|
|
|
for (const auto& c : store_.listCollections()) {
|
|
for (const auto& c : store_.listCollections()) {
|
|
@@ -2235,7 +2236,7 @@ grpc::Status DatabaseGrpcImpl::DropProject(
|
|
|
|
|
|
|
|
std::string error;
|
|
std::string error;
|
|
|
bool dropped = service_.dropProject(request->name(), error);
|
|
bool dropped = service_.dropProject(request->name(), error);
|
|
|
- // v2.12.2 — the project drop removed the project's LMDB env, but its
|
|
|
|
|
|
|
+ // v2.12.2 - the project drop removed the project's LMDB env, but its
|
|
|
// collections stayed in MemoryStore and in the WAL. So the "dropped" data
|
|
// collections stayed in MemoryStore and in the WAL. So the "dropped" data
|
|
|
// was still served (an insert of a dropped id said "already exists"), and a
|
|
// was still served (an insert of a dropped id said "already exists"), and a
|
|
|
// restart would replay it back. Drop each collection the way DropCollection
|
|
// restart would replay it back. Drop each collection the way DropCollection
|
|
@@ -2244,6 +2245,15 @@ grpc::Status DatabaseGrpcImpl::DropProject(
|
|
|
// refuses some names (`default`), and purging first would destroy data the
|
|
// refuses some names (`default`), and purging first would destroy data the
|
|
|
// refused drop was meant to keep.
|
|
// refused drop was meant to keep.
|
|
|
if (dropped) {
|
|
if (dropped) {
|
|
|
|
|
+ // v2.12.3 - and its FILES. File records live in a global tree (not the
|
|
|
|
|
+ // project's env), so the env drop left every one of them - and their
|
|
|
|
|
+ // blobs - behind, served to a later project of the same name. Deleted
|
|
|
|
|
+ // one record at a time through the refcounted path, so a blob another
|
|
|
|
|
+ // project also references survives.
|
|
|
|
|
+ const uint32_t files = files_.deleteAllIn(request->name());
|
|
|
|
|
+ if (files > 0) {
|
|
|
|
|
+ spdlog::info("DropProject '{}': deleted {} file(s)", request->name(), files);
|
|
|
|
|
+ }
|
|
|
for (const auto& c : doomed) {
|
|
for (const auto& c : doomed) {
|
|
|
if (store_.dropCollection(c)) {
|
|
if (store_.dropCollection(c)) {
|
|
|
persistence_.logDropCollection(c);
|
|
persistence_.logDropCollection(c);
|
|
@@ -2286,11 +2296,24 @@ grpc::Status DatabaseGrpcImpl::UploadFile(
|
|
|
std::vector<uint8_t> fileData;
|
|
std::vector<uint8_t> fileData;
|
|
|
pb::FileMetadata metadata;
|
|
pb::FileMetadata metadata;
|
|
|
|
|
|
|
|
|
|
+ // v2.12.3 - enforce the size limit WHILE reading. It used to be checked
|
|
|
|
|
+ // only after the whole body had been buffered here, so an oversized upload
|
|
|
|
|
+ // cost its full size in memory (times max_concurrent_file_streams) before
|
|
|
|
|
+ // being refused - and the refusal surfaced as INTERNAL. Returning now
|
|
|
|
|
+ // stops reading: the client's remaining Writes fail and Finish() gets this
|
|
|
|
|
+ // status.
|
|
|
|
|
+ const uint64_t maxBytes = files_.maxFileSizeBytes();
|
|
|
while (reader->Read(&chunk)) {
|
|
while (reader->Read(&chunk)) {
|
|
|
if (chunk.has_metadata()) {
|
|
if (chunk.has_metadata()) {
|
|
|
metadata = chunk.metadata();
|
|
metadata = chunk.metadata();
|
|
|
} else if (chunk.has_data()) {
|
|
} else if (chunk.has_data()) {
|
|
|
const auto& data = chunk.data();
|
|
const auto& data = chunk.data();
|
|
|
|
|
+ if (fileData.size() + data.size() > maxBytes) {
|
|
|
|
|
+ attachErrorDetail(context, {{"reason", "file_too_large"},
|
|
|
|
|
+ {"max_bytes", maxBytes}});
|
|
|
|
|
+ return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
|
|
|
|
|
+ "File too large (max: " + std::to_string(maxBytes / (1024 * 1024)) + " MB)");
|
|
|
|
|
+ }
|
|
|
fileData.insert(fileData.end(), data.begin(), data.end());
|
|
fileData.insert(fileData.end(), data.begin(), data.end());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -2343,6 +2366,10 @@ grpc::Status DatabaseGrpcImpl::UploadFile(
|
|
|
|
|
|
|
|
return grpc::Status::OK;
|
|
return grpc::Status::OK;
|
|
|
|
|
|
|
|
|
|
+ } catch (const FileRejected& e) {
|
|
|
|
|
+ // v2.12.3 - the caller's doing (size, MIME type), not a server fault.
|
|
|
|
|
+ attachErrorDetail(context, {{"reason", e.reason}});
|
|
|
|
|
+ return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, e.what());
|
|
|
} catch (const std::exception& e) {
|
|
} catch (const std::exception& e) {
|
|
|
return grpc::Status(grpc::StatusCode::INTERNAL, e.what());
|
|
return grpc::Status(grpc::StatusCode::INTERNAL, e.what());
|
|
|
}
|
|
}
|
|
@@ -2394,15 +2421,29 @@ grpc::Status DatabaseGrpcImpl::DownloadFile(
|
|
|
}
|
|
}
|
|
|
writer->Write(metadataChunk);
|
|
writer->Write(metadataChunk);
|
|
|
|
|
|
|
|
- // Stream file data in chunks
|
|
|
|
|
- constexpr size_t CHUNK_SIZE = 64 * 1024; // 64KB chunks
|
|
|
|
|
- auto data = files_.readFileIn(request->project(), request->id());
|
|
|
|
|
-
|
|
|
|
|
- for (size_t offset = 0; offset < data.size() && !context->IsCancelled(); offset += CHUNK_SIZE) {
|
|
|
|
|
|
|
+ // Stream file data in chunks.
|
|
|
|
|
+ //
|
|
|
|
|
+ // v2.12.3 - straight from the blob on disk, one 64 KB chunk at a time.
|
|
|
|
|
+ // It used to read the WHOLE file into memory first, so every download
|
|
|
|
|
+ // cost the file's full size (up to max_file_size_mb, times the number
|
|
|
|
|
+ // of concurrent file streams) for as long as the transfer took.
|
|
|
|
|
+ constexpr size_t CHUNK_SIZE = 64 * 1024;
|
|
|
|
|
+ auto bp = files_.blobPathIn(request->project(), request->id());
|
|
|
|
|
+ if (!bp) {
|
|
|
|
|
+ return grpc::Status(grpc::StatusCode::NOT_FOUND, "File not found");
|
|
|
|
|
+ }
|
|
|
|
|
+ std::ifstream in(*bp, std::ios::binary);
|
|
|
|
|
+ if (!in) {
|
|
|
|
|
+ return grpc::Status(grpc::StatusCode::INTERNAL, "failed to open the file's content");
|
|
|
|
|
+ }
|
|
|
|
|
+ std::vector<char> buf(CHUNK_SIZE);
|
|
|
|
|
+ while (!context->IsCancelled()) {
|
|
|
|
|
+ in.read(buf.data(), static_cast<std::streamsize>(buf.size()));
|
|
|
|
|
+ const std::streamsize got = in.gcount();
|
|
|
|
|
+ if (got <= 0) break;
|
|
|
pb::FileChunk dataChunk;
|
|
pb::FileChunk dataChunk;
|
|
|
- size_t chunkSize = std::min(CHUNK_SIZE, data.size() - offset);
|
|
|
|
|
- dataChunk.set_data(data.data() + offset, chunkSize);
|
|
|
|
|
- writer->Write(dataChunk);
|
|
|
|
|
|
|
+ dataChunk.set_data(buf.data(), static_cast<size_t>(got));
|
|
|
|
|
+ if (!writer->Write(dataChunk)) break; // client went away
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return grpc::Status::OK;
|
|
return grpc::Status::OK;
|
|
@@ -3942,7 +3983,7 @@ grpc::Status DatabaseReplicationGrpcImpl::GetNodeState(
|
|
|
// error.
|
|
// error.
|
|
|
// -------------------------------------------------------------------------
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-// v2.12.0 — CreateIndex with `fields` set: a COMPOSITE unique constraint. See
|
|
|
|
|
|
|
+// v2.12.0 - CreateIndex with `fields` set: a COMPOSITE unique constraint. See
|
|
|
// CreateIndexRequest.fields in database.proto for the contract.
|
|
// CreateIndexRequest.fields in database.proto for the contract.
|
|
|
grpc::Status DatabaseGrpcImpl::createCompositeUnique(
|
|
grpc::Status DatabaseGrpcImpl::createCompositeUnique(
|
|
|
grpc::ServerContext* /*context*/,
|
|
grpc::ServerContext* /*context*/,
|
|
@@ -4215,13 +4256,13 @@ grpc::Status DatabaseGrpcImpl::DropIndex(
|
|
|
try {
|
|
try {
|
|
|
const auto rc = smartbotic::database::resolveCollection(request->collection());
|
|
const auto rc = smartbotic::database::resolveCollection(request->collection());
|
|
|
auto* ds = service_.docStore(rc.project);
|
|
auto* ds = service_.docStore(rc.project);
|
|
|
- // v2.12.2 — may be null: the project's storage is gone (dropped)
|
|
|
|
|
|
|
+ // v2.12.2 - may be null: the project's storage is gone (dropped)
|
|
|
// while a declaration on one of its names survived. The declaration
|
|
// while a declaration on one of its names survived. The declaration
|
|
|
// lives in the global `_collection_meta`, so it can and must still be
|
|
// lives in the global `_collection_meta`, so it can and must still be
|
|
|
// removed - refusing here is what made such a name unrepairable.
|
|
// removed - refusing here is what made such a name unrepairable.
|
|
|
auto* lmdb = dynamic_cast<smartbotic::db::storage::LmdbDocumentStore*>(ds);
|
|
auto* lmdb = dynamic_cast<smartbotic::db::storage::LmdbDocumentStore*>(ds);
|
|
|
|
|
|
|
|
- // v2.12.0 — a composite unique constraint, named by its exact field list.
|
|
|
|
|
|
|
+ // v2.12.0 - a composite unique constraint, named by its exact field list.
|
|
|
if (request->fields_size() > 0) {
|
|
if (request->fields_size() > 0) {
|
|
|
if (!request->field().empty()) {
|
|
if (!request->field().empty()) {
|
|
|
response->set_success(false);
|
|
response->set_success(false);
|
|
@@ -4332,7 +4373,7 @@ grpc::Status DatabaseGrpcImpl::ListIndexes(
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- // v2.12.0 — composite unique constraints. Hidden when ANY of their
|
|
|
|
|
|
|
+ // v2.12.0 - composite unique constraints. Hidden when ANY of their
|
|
|
// fields is masked, for the same reason a masked field's index is.
|
|
// fields is masked, for the same reason a masked field's index is.
|
|
|
for (const auto& cf : cfg.compositeUniques) {
|
|
for (const auto& cf : cfg.compositeUniques) {
|
|
|
bool masked = false;
|
|
bool masked = false;
|