Gabriel39 commented on code in PR #70708:
URL: https://github.com/apache/doris/pull/70708#discussion_r4674888704
##########
fe/fe-connector/fe-connector-mongodb/src/main/java/org/apache/doris/connector/mongodb/MongoDriverCatalogClient.java:
##########
@@ -158,33 +170,176 @@ public BsonTimestamp selectSnapshot(String database,
String collection, String p
// with limit=1/batchSize=1, to discover T without transferring a
potentially large source row.
// The driver may request an extra wire batch slot to exhaust a
limit-one cursor.
try (ClientSession session =
client.startSession(ClientSessionOptions.builder()
- .causallyConsistent(false).snapshot(true).build());
- MongoCursor<Document> cursor = snapshotDatabase(database,
deadline).getCollection(collection)
-
.withReadPreference(properties.readPreferenceForSnapshot(preference)).find(session)
- .projection(new Document("_id", 0)
- .append("__doris_snapshot_probe__", new
Document("$literal", 1)))
- .limit(1).batchSize(1).iterator()) {
- cursor.hasNext();
- // This is cursor.atClusterTime captured by the driver's
snapshot-session protocol;
- // operationTime, $clusterTime and the local wall clock are not
substitutes for T.
- BsonTimestamp timestamp = session.getSnapshotTimestamp();
- if (timestamp == null) {
- throw new DorisConnectorException("MongoDB snapshot probe did
not return a snapshot timestamp");
+ .causallyConsistent(false).snapshot(true).build())) {
+ // Each blocking step must observe scan cancellation before
starting the next one;
+ // an in-flight synchronous driver call still uses the original
CSOT deadline.
+ try (MongoCursor<Document> cursor = snapshotDatabase(database,
deadline, checkCancelled)
+
.getCollection(collection).withReadPreference(properties.readPreferenceForSnapshot(preference))
+ .find(session).projection(new Document("_id", 0)
+ .append("__doris_snapshot_probe__", new
Document("$literal", 1)))
+ .limit(1).batchSize(1).iterator()) {
+ checkSnapshot(deadline, checkCancelled);
+ cursor.hasNext();
+ checkSnapshot(deadline, checkCancelled);
+ // This is cursor.atClusterTime captured by the driver's
snapshot-session protocol;
+ // operationTime, $clusterTime and the local wall clock are
not substitutes for T.
+ BsonTimestamp timestamp = session.getSnapshotTimestamp();
+ if (timestamp == null) {
+ throw new DorisConnectorException("MongoDB snapshot probe
did not return a snapshot timestamp");
+ }
+ return timestamp;
}
- return timestamp;
} catch (MongoException failure) {
throw failure(failure);
}
}
- private MongoDatabase snapshotDatabase(String database, long deadline) {
- long remaining = deadline - System.nanoTime();
- if (remaining <= 0) {
+ @Override
+ public MongoSplitProbe probeSplits(MongoTableHandle table, String field,
String preference,
+ TMongoSnapshot snapshot, int rows, long timeoutMs, Runnable
checkCancelled) {
+ long deadline = System.nanoTime() +
TimeUnit.MILLISECONDS.toNanos(timeoutMs);
+ String index = null;
+ long bytes = 0;
+ try {
+ var collection = probeDatabase(table.getDatabase(), deadline,
checkCancelled)
+ .getCollection(table.getName());
+ // Fetch the bounded index inventory together: one RTT per index
can exhaust the
+ // optional probe deadline before a later qualifying ordinary
index is reached.
+ try (MongoCursor<Document> indexes =
collection.listIndexes().batchSize(128)
+ .timeoutMode(TimeoutMode.CURSOR_LIFETIME).iterator()) {
+ int count = 0;
+ while (true) {
+ // hasNext may issue getMore. Cancellation must be checked
before that request,
+ // not only after it returns; the lifetime timeout also
covers all cursor batches.
+ checkProbe(deadline, checkCancelled);
+ if (!indexes.hasNext()) {
+ break;
+ }
+ checkProbe(deadline, checkCancelled);
+ Document value = indexes.next();
+ bytes +=
MongoSplitPlan.encode(value.toBsonDocument()).length;
+ if (++count > 128 || bytes > 65536) {
+ throw new MongoRangePlanner.PlanningLimit();
+ }
+ if (eligibleIndex(value, field)) {
+ index = value.getString("name");
+ break;
+ }
+ }
+ }
+ if (index == null) {
+ return new MongoSplitProbe(List.of(), -1, bytes, false);
+ }
+ Document stats = probeDatabase(table.getDatabase(), deadline,
checkCancelled)
+ .runCommand(new Document("collStats",
table.getName()).append("scale", 1));
+ Number size = stats.get("size", Number.class);
+ long logicalBytes = size == null ? -1 : size.longValue();
+ List<BsonValue> samples = new ArrayList<>();
+ // Sampling an index prefix puts almost all increasing keys into
the final range.
+ // Keep $sample first so MongoDB can use its random cursor, then
retain only bounded
+ // fixed-width keys. Statistics tune cost only; they never narrow
source coverage.
+ // Unsupported large strings/arrays become null before they can
reach the FE.
+ Document type = new Document("$type", "$" + field);
+ Document supported = new Document("$in", List.of(type,
List.of("int", "long", "objectId", "date")));
+ Document projection = new Document("_id", 0).append("value",
+ new Document("$cond", List.of(supported, "$" + field, new
Document("$literal", null))));
+ // Do not shrink small-table requests below MongoDB's 5%
random-cursor threshold.
+ // Its historical random cursor can stall on concurrently
rewritten records; a bounded
+ // full sample of a small collection remains cheap and preserves
the pinned timestamp.
+ var pipeline = List.of(new Document("$sample", new
Document("size", rows)),
Review Comment:
Fixed in 2191942fd2bd7821a71034b43e2c2c9ba8a0e35c. The driver applies the
planner's existing auto size heuristic immediately after receiving and
accounting for collStats, before building or sending the sampling aggregate.
Small or unknown-size auto inputs return a metadata-only probe result; the
planner evaluates those decisions before interpreting an absent sample,
preserving small_collection / unknown_complement_cost diagnostics. Explicit
range retains its bounded sampling path.
The command-level regression fails on the baseline because it sends one
aggregate, and passes with zero sampling commands. A real MongoDB 8.0.20
replica-set check with 10000 documents and file_split_size=33554432 returns one
small_collection split and records no sampling aggregate. All 182 MongoDB FE
tests, 166 upstream connector SPI tests and full FE Checkstyle pass. Full
system SQL/BE execution, MongoDB 7/mongos and remote CI remain pending for this
increment.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]