Skip to content

Commit bfe46ec

Browse files
authored
[fix](row binlog) make time-based incremental reads use a safe fence (#67594)
### What problem does this PR solve? Issue Number: None Related PR: #65850 Problem Summary: Time-based `@incr` reads previously allowed an `endTimestamp` later than the FE's current TSO. Such a future boundary cannot be closed by the transaction waiting logic: a transaction may start after the request begins but still fall before that future boundary, so the scan cannot guarantee a complete result. In addition, waiting only for currently running transactions is insufficient unless the TSO boundary, transaction watermark, transaction visibility, and table visible versions are coordinated under one authoritative fence. Otherwise, especially on a follower FE, a transaction can already be `VISIBLE` while the local catalog still exposes an older visible version, causing the incremental scan to miss rows. This PR establishes a safe read fence for both classic and cloud modes: 1. Acquire the current TSO from the master FE. 2. Capture the transaction ID watermark after the TSO snapshot. 3. Wait for transactions at or below the watermark that involve the scanned OLAP tables to reach a final visible or aborted state. 4. In classic mode, synchronize table publishers and wait for follower journal replay so catalog visible versions cross the same fence. 5. In cloud mode, refresh the latest visible versions from MetaService after the transaction wait. 6. Reject an `endTimestamp` later than the captured `CURRENT_TSO_PHYSICAL_TIME`. 7. Convert a physical timestamp `P` to TSO boundary `(P, 0)`, preserving half-open range semantics: `[startTimestamp, endTimestamp)`. #### Future endTimestamp error example Request: ```sql SELECT id, value, __DORIS_BINLOG_OP__ FROM example_table@incr( "startTimestamp" = "2026-09-01 00:00:00", "endTimestamp" = "2999-01-01 00:00:00", "incrementType" = "DETAIL" ); ``` Response: ```text ERROR 1105 (HY000): errCode = 2, detailMessage = endTimestamp exceeds the maximum supported time for an INCR read: requestedEndTimestampMs=<requested epoch milliseconds>, CURRENT_TSO_PHYSICAL_TIME=<maximum supported epoch milliseconds> ``` The maximum currently supported boundary can be obtained from: ```sql SELECT CURRENT_TSO_PHYSICAL_TIME FROM information_schema.tso_status; ``` ### Release note Time-based `@incr` reads now reject `endTimestamp` values after `CURRENT_TSO_PHYSICAL_TIME`. Timestamp ranges use half-open semantics `[startTimestamp, endTimestamp)`, with each physical timestamp mapped to logical counter zero. ### Check List (For Author) - Test: FE unit tests, FE and BE builds, and `row_binlog_p0/test_binlog_changes_syntax` regression test - Behavior changed: Yes - Future `endTimestamp` values are rejected with the maximum supported TSO physical time in the error message. - Physical timestamp boundaries are interpreted as `(physicalTime, 0)`. - Does this need documentation: No
1 parent 00b08ad commit bfe46ec

15 files changed

Lines changed: 526 additions & 184 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/cloud/transaction/CloudGlobalTransactionMgr.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,6 +2589,12 @@ public Long getNextTransactionId() throws UserException {
25892589
return getCurrentMaxTxnResponse.getCurrentMaxTxnId();
25902590
}
25912591

2592+
@Override
2593+
public long getTransactionIdWatermark() throws UserException {
2594+
// MetaService's conflict check treats end_txn_id as an exclusive upper bound.
2595+
return getNextTransactionId() + 1;
2596+
}
2597+
25922598
@Override
25932599
public int getRunningTxnNums(Long dbId) throws AnalysisException {
25942600
return 0;

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,11 +711,13 @@ private Pair<Long, Long> parseTimestampRange(TableScanParams scanParams) {
711711
Map<String, String> params = scanParams.getMapParams();
712712
Long startTimestamp = OlapScanNode.parseChangeTimestamp(
713713
params.getOrDefault(OlapScanNode.OLAP_START_TIMESTAMP, "0"));
714-
startTimestamp = TSOTimestamp.composeFullTimestamp(startTimestamp);
714+
// BE applies start_tso < commit_tso <= end_tso. Logical zero on both boundaries
715+
// therefore gives the user-facing physical interval [startTimestamp, endTimestamp).
716+
startTimestamp = TSOTimestamp.composePhysicalTimestamp(startTimestamp);
715717
Long endTimestamp = null;
716718
if (params.containsKey((OlapScanNode.OLAP_END_TIMESTAMP))) {
717719
endTimestamp = OlapScanNode.parseChangeTimestamp(params.get(OlapScanNode.OLAP_END_TIMESTAMP));
718-
endTimestamp = TSOTimestamp.composeFullTimestamp(endTimestamp);
720+
endTimestamp = TSOTimestamp.composePhysicalTimestamp(endTimestamp);
719721
}
720722
return Pair.of(startTimestamp, endTimestamp);
721723
}

‎fe/fe-core/src/main/java/org/apache/doris/qe/TimeBasedChangeVisibleWaiter.java‎

Lines changed: 218 additions & 115 deletions
Large diffs are not rendered by default.

‎fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
import org.apache.doris.qe.QueryState;
135135
import org.apache.doris.qe.SessionVariable;
136136
import org.apache.doris.qe.StmtExecutor;
137+
import org.apache.doris.qe.TimeBasedChangeVisibleWaiter;
137138
import org.apache.doris.qe.VariableMgr;
138139
import org.apache.doris.resource.BackendSelection;
139140
import org.apache.doris.resource.BackendSelectionManager;
@@ -154,6 +155,8 @@
154155
import org.apache.doris.thrift.FrontendService;
155156
import org.apache.doris.thrift.TAbortRemoteTxnRequest;
156157
import org.apache.doris.thrift.TAbortRemoteTxnResult;
158+
import org.apache.doris.thrift.TAcquireTimeBasedChangeReadFenceRequest;
159+
import org.apache.doris.thrift.TAcquireTimeBasedChangeReadFenceResult;
157160
import org.apache.doris.thrift.TAddOrDropPartitionsRequest;
158161
import org.apache.doris.thrift.TAddOrDropPartitionsResult;
159162
import org.apache.doris.thrift.TAutoIncrementRangeRequest;
@@ -3384,6 +3387,35 @@ public TFetchSchemaTableDataResult fetchSchemaTableData(TFetchSchemaTableDataReq
33843387
}
33853388
}
33863389

3390+
@Override
3391+
public TAcquireTimeBasedChangeReadFenceResult acquireTimeBasedChangeReadFence(
3392+
TAcquireTimeBasedChangeReadFenceRequest request) throws TException {
3393+
TAcquireTimeBasedChangeReadFenceResult result = new TAcquireTimeBasedChangeReadFenceResult();
3394+
TStatus status = checkMaster();
3395+
result.setStatus(status);
3396+
if (status.getStatusCode() != TStatusCode.OK) {
3397+
return result;
3398+
}
3399+
3400+
try {
3401+
TimeBasedChangeVisibleWaiter.ChangeReadFence fence =
3402+
TimeBasedChangeVisibleWaiter.acquireFenceOnMaster(
3403+
request.getDbToTableIds(),
3404+
request.isSetEndTimestampMs() ? request.getEndTimestampMs() : null,
3405+
request.getTimeoutMs(), request.isWaitForTransactions());
3406+
result.setCurrentTso(fence.getCurrentTso());
3407+
result.setMaxJournalId(fence.getMaxJournalId());
3408+
} catch (UserException e) {
3409+
status.setStatusCode(TStatusCode.ANALYSIS_ERROR);
3410+
status.addToErrorMsgs(e.getDetailMessage());
3411+
} catch (RuntimeException e) {
3412+
LOG.warn("failed to acquire time-based change read fence", e);
3413+
status.setStatusCode(TStatusCode.INTERNAL_ERROR);
3414+
status.addToErrorMsgs(Strings.nullToEmpty(e.getMessage()));
3415+
}
3416+
return result;
3417+
}
3418+
33873419
private TNetworkAddress getClientAddr() {
33883420
ThriftServerContext connectionContext = ThriftServerEventProcessor.getConnectionContext();
33893421
// For NonBlockingServer, we can not get client ip.

‎fe/fe-core/src/main/java/org/apache/doris/transaction/GlobalTransactionMgr.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,12 @@ public Long getNextTransactionId() {
928928
return this.idGenerator.getNextTransactionId();
929929
}
930930

931+
@Override
932+
public long getTransactionIdWatermark() {
933+
// The classic conflict check treats its upper bound as inclusive.
934+
return this.idGenerator.getCurrentTransactionId();
935+
}
936+
931937
@Override
932938
public void write(DataOutput out) throws IOException {
933939
int numTransactions = getTransactionNum();

‎fe/fe-core/src/main/java/org/apache/doris/transaction/GlobalTransactionMgrIface.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ public List<List<Comparable>> getPartitionTransInfo(long dbId, long tid, long ta
211211

212212
public Long getNextTransactionId() throws UserException;
213213

214+
/**
215+
* Return the transaction ID upper bound expected by {@link #isPreviousTransactionsFinished}.
216+
* This is a read-only operation and must not allocate a transaction ID.
217+
*/
218+
public long getTransactionIdWatermark() throws UserException;
219+
214220
public void readFields(DataInput in) throws IOException;
215221

216222
public void replayUpsertTransactionState(TransactionState transactionState) throws Exception;

‎fe/fe-core/src/main/java/org/apache/doris/transaction/TransactionIdGenerator.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public synchronized long getNextTransactionId() {
5454
}
5555
}
5656

57+
/** Return the current transaction ID watermark without allocating a new ID. */
58+
public synchronized long getCurrentTransactionId() {
59+
return nextId;
60+
}
61+
5762
public synchronized void initTransactionId(long id) {
5863
if (id > batchEndId) {
5964
batchEndId = id;

‎fe/fe-core/src/main/java/org/apache/doris/tso/TSOTimestamp.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ public static long composeFullTimestamp(long physicalTimestamp) {
112112
return composeTimestamp(physicalTimestamp, LOGICAL_MASK);
113113
}
114114

115+
/**
116+
* Compose the lower boundary of a physical millisecond.
117+
*
118+
* @return 64-bit TSO timestamp with a zero logical counter
119+
*/
120+
public static long composePhysicalTimestamp(long physicalTimestamp) {
121+
return composeTimestamp(physicalTimestamp, 0L);
122+
}
123+
115124
/**
116125
* Extract physical time (milliseconds) from TSO timestamp
117126
*

‎fe/fe-core/src/test/java/org/apache/doris/cloud/transaction/CloudGlobalTransactionMgrTest.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,24 @@ public void testGetNextTransactionId() throws Exception {
623623
}
624624
}
625625

626+
@Test
627+
public void testGetTransactionIdWatermarkUsesExclusiveMetaServiceBound() throws Exception {
628+
MetaServiceProxy mockProxy = Mockito.mock(MetaServiceProxy.class);
629+
try (MockedStatic<MetaServiceProxy> mockedStatic = Mockito.mockStatic(MetaServiceProxy.class)) {
630+
mockedStatic.when(MetaServiceProxy::getInstance).thenReturn(mockProxy);
631+
GetCurrentMaxTxnResponse response = GetCurrentMaxTxnResponse.newBuilder()
632+
.setStatus(Cloud.MetaServiceResponseStatus.newBuilder()
633+
.setCode(MetaServiceCode.OK).setMsg("OK"))
634+
.setCurrentMaxTxnId(1000)
635+
.build();
636+
Mockito.doReturn(response).when(mockProxy).getCurrentMaxTxnId(Mockito.any());
637+
638+
long result = masterTransMgr.getTransactionIdWatermark();
639+
640+
Assertions.assertEquals(1001, result);
641+
}
642+
}
643+
626644
private TxnInfoPB buildTxnInfo(long transactionId) {
627645
return TxnInfoPB.newBuilder()
628646
.setDbId(CatalogTestUtil.testDbId1)

‎fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/ExplainTableStreamPlanTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ public void testIncrTimestampRangePropagatesToScanRangeTso() throws Exception {
586586
// asserting every incremental scan range carries the composed start/end TSO for its partition.
587587
String startTs = "2026-05-25 20:51:28";
588588
String endTs = "2026-05-25 21:51:28";
589-
long expectedStartTso = TSOTimestamp.composeFullTimestamp(OlapScanNode.parseChangeTimestamp(startTs));
590-
long expectedEndTso = TSOTimestamp.composeFullTimestamp(OlapScanNode.parseChangeTimestamp(endTs));
589+
long expectedStartTso = TSOTimestamp.composePhysicalTimestamp(OlapScanNode.parseChangeTimestamp(startTs));
590+
long expectedEndTso = TSOTimestamp.composePhysicalTimestamp(OlapScanNode.parseChangeTimestamp(endTs));
591591

592592
ConnectContext ctx = createDefaultCtx();
593593
ctx.setDatabase("test_stream");

0 commit comments

Comments
 (0)