yujun777 commented on code in PR #71199: URL: https://github.com/apache/doris/pull/71199#discussion_r4753778152
########## fe/fe-core/src/main/java/org/apache/doris/catalog/stream/StreamResumabilityChecker.java: ########## @@ -0,0 +1,171 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.catalog.stream; + +import org.apache.doris.catalog.OlapTable; +import org.apache.doris.changelog.ChangelogUtils; +import org.apache.doris.tso.TSOTimestamp; + +/** + * Shared checkpoint resumability rules for OLAP table streams, used by both the scan rewrite path + * and stream monitoring / consumption diagnostics. + */ +public final class StreamResumabilityChecker { + private StreamResumabilityChecker() { + } + + public enum Status { + RESUMABLE(1), + CANNOT_RESUME(0), + UNKNOWN(-1); + + private final int code; + + Status(int code) { + this.code = code; + } + + public int code() { + return code; + } + } + + public enum Reason { + NONE(""), + BASE_TABLE_MISSING("BASE_TABLE_MISSING"), + STREAM_DISABLED("STREAM_DISABLED"), + STREAM_STALE("STREAM_STALE"), + CHANGELOG_DISABLED("CHANGELOG_DISABLED"), + TTL_EXPIRED("TTL_EXPIRED"), + NO_BASELINE_OFFSET("NO_BASELINE_OFFSET"), + TSO_UNINITIALIZED("TSO_UNINITIALIZED"), + STATE_UNAVAILABLE("STATE_UNAVAILABLE"); + + private final String code; + + Reason(String code) { + this.code = code; + } + + public String code() { + return code; + } + } + + public record Result(Status status, Reason reason) { + public static Result resumable() { + return new Result(Status.RESUMABLE, Reason.NONE); + } + + public static Result cannotResume(Reason reason) { + return new Result(Status.CANNOT_RESUME, reason); + } + + public static Result unknown(Reason reason) { + return new Result(Status.UNKNOWN, reason); + } + } + + /** Compute the inclusive TTL expiration cutoff TSO, or null if TTL is disabled or referenceTso is unavailable. */ + public static Long calculateCutoffTso(OlapTable baseTable, Long referenceTso) { + if (baseTable == null || !baseTable.hasChangelogTtl()) { + return null; + } + if (referenceTso == null || referenceTso <= 0) { + return null; + } + return TSOTimestamp.calculateCutoff(referenceTso, baseTable.getChangelogConfig().getTtlSeconds()); + } + + /** Check stream-wide prerequisites before inspecting individual partitions. */ + public static Result checkStreamLevel(OlapTableStream stream, OlapTable baseTable) { + if (stream.isDisabled()) { + return Result.cannotResume(Reason.STREAM_DISABLED); + } + if (stream.isStale()) { + return Result.cannotResume(Reason.STREAM_STALE); + } + if (baseTable == null || baseTable.isDropped) { + return Result.cannotResume(Reason.BASE_TABLE_MISSING); + } + if (!baseTable.hasChangelog()) { + return Result.cannotResume(Reason.CHANGELOG_DISABLED); + } + return Result.resumable(); + } + + /** + * Evaluate whether one partition checkpoint can resume completely without losing changes. + * Even for scan types that clip an expired start TSO instead of throwing, clipping means + * incomplete resumption and reports {@link Status#CANNOT_RESUME}. + */ + public static Result checkPartition(boolean hasChangelogTtl, Long cutoffTso, boolean hasData, + Long offset, Long historicalTso, long endTso, long streamCreateTimeMs, long oldestPendingTimeMs) { + // Initial-snapshot partitions read the base table directly and do not depend on changelog retention. + if (historicalTso != null) { + return Result.resumable(); + } + if (!hasData) { + return Result.resumable(); + } + if (!hasChangelogTtl) { + return Result.resumable(); + } + if (cutoffTso == null) { + return Result.unknown(Reason.TSO_UNINITIALIZED); + } + if (offset != null && offset >= 0) { + Long startTso = TSOTimestamp.toExclusiveBound(offset); + if (ChangelogUtils.isStartTsoExpired(startTso, cutoffTso)) { + return Result.cannotResume(Reason.TTL_EXPIRED); + } + return Result.resumable(); + } + // Partition has data but no real consumption baseline (newly added or empty at stream creation). + if (endTso >= 0 && endTso <= cutoffTso) { + return Result.cannotResume(Reason.TTL_EXPIRED); + } + long cutoffMs = TSOTimestamp.extractPhysicalTime(cutoffTso); + if (oldestPendingTimeMs > 0) { + return oldestPendingTimeMs <= cutoffMs Review Comment: Closed the unpublished pre-creation transaction case in 5c5fb41b4d3: - Removed the `streamCreateTimeMs > cutoffMs` heuristic from `StreamResumabilityChecker.checkPartition()` completely. - No-baseline partitions (`offset == null` or `-1L`) now use the two-sided TSO-domain interval `[oldestPendingTimeMs, provenOldestWriteTimeMs]`: `provenOldestWriteTimeMs <= cutoffMs` (or `endTso <= cutoffTso`) proves `TTL_EXPIRED`; `oldestPendingTimeMs > cutoffMs` proves `RESUMABLE` only when backed by a verified caught-up observation with zero unpublished committed transactions; otherwise (cold start, unpublished pre-creation transaction, `REPLACE`/`RECOVER PARTITION`, or interval straddling cutoff) it returns `UNKNOWN (NO_BASELINE_OFFSET)`. -- 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]
