Quellcode durchsuchen

fix: 2.13.0 - an export row too large for any message is refused by name

A row bigger than the listener's max send size made the stream write fail
with gRPC's own nameless RESOURCE_EXHAUSTED, so a caller could not tell
which row to skip. The handler now ends the export with RESOURCE_EXHAUSTED,
reason row_too_large, conflicting_id = that row (the rows before it are
delivered; resuming after it continues), and cuts batches at min(8 MB, half
the send limit) so a batch always fits in one message.

test_export runs its server with a 2 MB send limit: 300 KB rows batch by
bytes, a 3 MB row is refused by name and the rest resumes. Reverting the
check fails 1 (gRPC's nameless refusal).
fszontagh vor 1 Monat
Ursprung
Commit
ceab6ce820

Datei-Diff unterdrückt, da er zu groß ist
+ 0 - 0
CLAUDE.md


+ 7 - 1
docs/integration-guide.md

@@ -580,7 +580,13 @@ if (!sum.complete) { /* resume: exportCollection(..., sum.lastId, ...) */ }
   (never reinterpreted), and with `InvalidArgument` / reason `no_vector_index`
   when vectors are asked of a collection with `vector_dimension` 0.
 - `batchRows` (default 256) is clamped to 1024, never refused; batches are also
-  cut at 8 MB. The clamp is in `header.batchRowsEffective`.
+  cut at 8 MB (or half the server's send limit, if lower). The clamp is in
+  `header.batchRowsEffective`.
+- **A row too large for any one message** (bigger than the server's
+  `max_send_message_size_mb`) is never skipped silently: the export throws
+  `ResourceExhausted` with reason `row_too_large` and the row's id in
+  `errorDetail(e)->conflictingId`. The rows before it were delivered; resume with
+  `afterId` = that id to continue without it, deliberately.
 
 **`vectorState`**, reported as stored and never guessed by the library:
 

+ 3 - 0
proto/database.proto

@@ -525,6 +525,9 @@ message CountResponse {
 //     the gap with an id that sorts before the cursor.
 //   - A stream ends early (complete=false, last_id set) when it reaches the
 //     server's time cap (header.max_stream_seconds) - that is the resume case.
+//   - A row larger than the server's send limit ends the stream with
+//     RESOURCE_EXHAUSTED, reason row_too_large, conflicting_id = that row; the
+//     rows before it were sent. It is never skipped silently.
 //   - A stream that ends without a trailer did not complete. Treat it as
 //     failed; a client must never read a stream without a trailer as whole.
 //   - A restored row gets FRESH metadata: insert stamps _created_at,

+ 42 - 2
service/src/database_grpc_impl.cpp

@@ -1913,6 +1913,14 @@ grpc::Status DatabaseGrpcImpl::ExportCollection(
         };
     }
 
+    // A batch closes at 8 MB, or at half the listener's send limit if that is
+    // lower, so one batch always fits in one message.
+    const size_t batchByteCap = std::min(kExportMaxBatchBytes, maxSendBytes_ / 2);
+    // Room for the chunk/row framing around one row's bytes.
+    constexpr size_t kRowFraming = 1024;
+    std::string rowTooLarge;
+    size_t rowTooLargeBytes = 0;
+
     pb::ExportChunk batch;
     size_t batchBytes = 0;
     uint64_t rowsSent = 0;
@@ -1983,9 +1991,30 @@ grpc::Status DatabaseGrpcImpl::ExportCollection(
                     row->mutable_document()->set_vector(
                         std::string(reinterpret_cast<const char*>(vec), n * sizeof(float)));
                 }
-                batchBytes += row->ByteSizeLong();
+                const size_t rowBytes = row->ByteSizeLong();
+                if (rowBytes + kRowFraming > maxSendBytes_) {
+                    // No message can carry this row. Skipping it would make the
+                    // export look whole without it; stop and name it instead.
+                    rowTooLarge = row->document().id();
+                    rowTooLargeBytes = rowBytes;
+                    batch.mutable_rows()->mutable_rows()->RemoveLast();
+                    stoppedEarly = true;
+                    return false;
+                }
+                if (batchBytes > 0 && batchBytes + rowBytes > batchByteCap) {
+                    // This row would push the batch past the cap: send what is
+                    // there first, then start the next batch with this row.
+                    pb::ExportRow pending = *row;
+                    batch.mutable_rows()->mutable_rows()->RemoveLast();
+                    if (!flush()) {
+                        writeFailed = true;
+                        return false;
+                    }
+                    *batch.mutable_rows()->add_rows() = std::move(pending);
+                }
+                batchBytes += rowBytes;
                 if (batch.rows().rows_size() >= static_cast<int>(batchRows) ||
-                    batchBytes >= kExportMaxBatchBytes) {
+                    batchBytes >= batchByteCap) {
                     if (!flush()) {
                         writeFailed = true;
                         return false;
@@ -2004,6 +2033,17 @@ grpc::Status DatabaseGrpcImpl::ExportCollection(
     if (writeFailed || !flush()) {
         return grpc::Status(grpc::StatusCode::CANCELLED, "the client stopped reading the export");
     }
+    if (!rowTooLarge.empty()) {
+        // The rows before it were sent; the caller can resume after this id if
+        // it chooses to go on without it.
+        attachErrorDetail(context, {{"reason", "row_too_large"},
+                                    {"conflicting_id", rowTooLarge}});
+        return grpc::Status(grpc::StatusCode::RESOURCE_EXHAUSTED,
+            "row '" + rowTooLarge + "' is " + std::to_string(rowTooLargeBytes) +
+            " bytes, more than one message can carry (max send " +
+            std::to_string(maxSendBytes_) + " bytes); " + std::to_string(rowsSent) +
+            " rows before it were sent");
+    }
     if (shuttingDown_.load()) {
         // The trailer is still sent, so the caller gets a resume cursor.
         spdlog::info("ExportCollection {} cut short by shutdown after {} rows", collection, rowsSent);

+ 5 - 0
service/src/database_grpc_impl.hpp

@@ -454,6 +454,10 @@ public:
         maxFileStreams_ = maxFile;
     }
 
+    // v2.13.0 - the listener's max send size, so an export can refuse a row
+    // that no message could carry instead of failing the write blindly.
+    void setMaxSendBytes(size_t bytes) { maxSendBytes_ = bytes; }
+
 private:
     // Convert internal types to proto types
     static pb::Document toProto(const Document& doc);
@@ -645,6 +649,7 @@ private:
     // concurrent exports is capped well below it.
     std::atomic<uint32_t> activeExportStreams_{0};
     uint32_t maxExportStreams_ = 8;
+    size_t maxSendBytes_ = 100u * 1024 * 1024;
 };
 
 /**

+ 2 - 0
service/src/database_service.cpp

@@ -1549,6 +1549,8 @@ void DatabaseService::setupComponents() {
     storageImpl_->setStreamLimits(
         config_.grpc.maxConcurrentSubscribeStreams,
         config_.grpc.maxConcurrentFileStreams);
+    storageImpl_->setMaxSendBytes(
+        static_cast<size_t>(config_.grpc.maxSendMessageSizeMb) * 1024 * 1024);   // v2.13.0
     replicationImpl_ = std::make_unique<DatabaseReplicationGrpcImpl>(
         *store_, *replication_, *persistence_
     );

+ 29 - 0
tests/load_test/test_export.cpp

@@ -112,6 +112,13 @@ static int write() {
                       {"_updated_at", 6}, {"_created_by", "mallory"}, {"_updated_by", "mallory"},
                       {"_vector", vecFor(5000)}},
              "shadow");
+    // Payload edges, against the e2e server's 2 MB send limit: 300 KB rows
+    // must be cut into byte-bounded batches; a 3 MB row cannot be sent at all.
+    const std::string k300(300 * 1024, 'x');
+    for (int i = 0; i < 20; ++i) c.insert("wide", {{"n", i}, {"blob", k300}}, id3("k", i));
+    c.insert("big", {{"n", 0}}, "b0");
+    c.insert("big", {{"n", 1}, {"blob", std::string(3 * 1024 * 1024, 'y')}}, "b1");
+    c.insert("big", {{"n", 2}}, "b2");
     ck(c.createCollection("plain"), "create plain (no dimension)");
     for (int i = 0; i < 3; ++i) c.insert("plain", {{"n", i}}, id3("q", i));
     return 0;
@@ -253,6 +260,28 @@ static void check() {
     catch (const Client::Error& err) { nf = err.code() == Client::ErrorCode::NotFound; }
     ck(nf, "export of a missing collection throws NotFound");
 
+    // --- payload edges
+    auto wide = runExport(c, "wide", false, "", 0, 1024);
+    ck(wide.summary.complete && wide.rows.size() == 20,
+       "300 KB rows: byte-bounded batches deliver all 20 under a 2 MB send limit");
+    std::vector<Client::ExportRow> bigRows;
+    std::string bigReason, bigId;
+    Client::ErrorCode bigCode = Client::ErrorCode::Ok;
+    try {
+        c.exportCollection("big", false, "", nullptr,
+                           [&](Client::ExportRow&& r) { bigRows.push_back(std::move(r)); return true; });
+    } catch (const Client::Error& err) {
+        bigCode = err.code();
+        if (auto* d = Client::errorDetail(err)) { bigReason = d->reason; bigId = d->conflictingId; }
+    }
+    ck(bigCode == Client::ErrorCode::ResourceExhausted && bigReason == "row_too_large" && bigId == "b1",
+       "a row no message can carry: ResourceExhausted, row_too_large, naming b1 (got " +
+       std::to_string(int(bigCode)) + " " + bigReason + " " + bigId + ")");
+    ck(bigRows.size() == 1 && bigRows[0].id == "b0", "the rows before it were delivered");
+    auto afterBig = runExport(c, "big", false, "b1");
+    ck(afterBig.summary.complete && afterBig.rows.size() == 1 && afterBig.rows[0].id == "b2",
+       "resuming after the named row completes the rest");
+
     // --- file calls report a reason
     Client::Failure ff;
     ck(!c.setFileTtl("no-such-file", 60, ff) && ff.code == Client::ErrorCode::NotFound,

+ 2 - 1
tests/load_test/test_export.sh

@@ -23,7 +23,8 @@ cat > "$DIR/config.json" <<CFG
     "rpc_port": $PORT,
     "encryption": { "enabled": false },
     "migrations": { "enabled": false },
-    "replication": { "enabled": false }
+    "replication": { "enabled": false },
+    "grpc": { "max_send_message_size_mb": 2 }
   }
 }
 CFG

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.