hello-stephen commented on code in PR #71199:
URL: https://github.com/apache/doris/pull/71199#discussion_r4751520174


##########
fe/fe-core/src/main/java/org/apache/doris/catalog/stream/OlapStreamMonitorCollector.java:
##########
@@ -0,0 +1,624 @@
+// 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.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.changelog.ChangelogMonitor;
+import org.apache.doris.changelog.ChangelogMonitorSnapshot;
+import org.apache.doris.changelog.ChangelogMonitorSnapshot.PartitionHealth;
+import org.apache.doris.changelog.ChangelogMonitorSnapshot.StreamHealth;
+import org.apache.doris.cloud.proto.Cloud;
+import org.apache.doris.cloud.rpc.CloudTableStreamReadStateHelper;
+import org.apache.doris.common.Config;
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.util.MetaLockUtils;
+import org.apache.doris.rpc.RpcException;
+import org.apache.doris.tso.TSOService;
+import org.apache.doris.tso.TSOTimestamp;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.function.LongSupplier;
+
+/**
+ * Batch-samples registered OLAP table streams for {@link 
org.apache.doris.changelog.ChangelogMonitor}.
+ *
+ * <p>Table read locks are always acquired in ascending {@code Table::getId} 
order with a bounded timeout
+ * so background monitoring can never deadlock against multi-table write-lock 
paths such as {@code DROP DATABASE}.
+ * In cloud mode, partition identities are copied under short read locks and 
authoritative states are fetched
+ * in bounded lock-free batch RPCs (chunked across streams and within wide 
streams), isolating per-stream logic
+ * errors without amplifying MetaService transport/timeout, server-side 
rate-limit, or FDB transaction outages.
+ */
+public final class OlapStreamMonitorCollector {
+    private static final Logger LOG = 
LogManager.getLogger(OlapStreamMonitorCollector.class);
+    static final long LOCK_TIMEOUT_MS = 500L;
+    private static final long MAX_FALLBACK_BUDGET_MS = 10_000L;
+
+    private static final Set<Cloud.MetaServiceCode> SERVICE_OUTAGE_CODES = 
EnumSet.of(
+            Cloud.MetaServiceCode.MAX_QPS_LIMIT,
+            Cloud.MetaServiceCode.MS_TOO_BUSY,
+            Cloud.MetaServiceCode.KV_TXN_CREATE_ERR,
+            Cloud.MetaServiceCode.KV_TXN_GET_ERR,
+            Cloud.MetaServiceCode.KV_TXN_COMMIT_ERR,
+            Cloud.MetaServiceCode.KV_TXN_CONFLICT,
+            Cloud.MetaServiceCode.KV_TXN_STORE_GET_RETRYABLE,
+            Cloud.MetaServiceCode.KV_TXN_STORE_COMMIT_RETRYABLE,
+            Cloud.MetaServiceCode.KV_TXN_STORE_CREATE_RETRYABLE,
+            Cloud.MetaServiceCode.KV_TXN_TOO_OLD,
+            Cloud.MetaServiceCode.KV_TXN_MAYBE_COMMITTED,
+            Cloud.MetaServiceCode.KV_TXN_CONFLICT_RETRY_EXCEEDED_MAX_TIMES);
+
+    private OlapStreamMonitorCollector() {
+    }
+
+    @FunctionalInterface
+    public interface CloudStateFetcher {
+        Map<Cloud.TableStreamIdentityPB, Map<Long, 
Cloud.TableStreamPartitionReadStatePB>> fetch(
+                Map<Cloud.TableStreamIdentityPB, Set<Long>> 
requestedPartitions) throws UserException;
+    }
+
+    public static List<StreamHealth> collect(Env env, LongSupplier 
wallClockMs, LongSupplier nanoClock,
+            StreamPendingAgeTracker tracker, Map<Long, StreamHealth> 
previousByStreamId) {
+        return collect(env, wallClockMs, nanoClock, tracker, 
previousByStreamId,
+                CloudTableStreamReadStateHelper::getReadStates);
+    }
+
+    public static List<StreamHealth> collect(Env env, long sampleTimeMs, 
StreamPendingAgeTracker tracker,
+            Map<Long, StreamHealth> previousByStreamId, CloudStateFetcher 
cloudFetcher) {
+        long sampleTimeNanos = TimeUnit.MILLISECONDS.toNanos(sampleTimeMs);
+        return collect(env, () -> sampleTimeMs, () -> sampleTimeNanos, 
tracker, previousByStreamId, cloudFetcher);
+    }
+
+    public static List<StreamHealth> collect(Env env, LongSupplier 
wallClockMs, LongSupplier nanoClock,
+            StreamPendingAgeTracker tracker, Map<Long, StreamHealth> 
previousByStreamId,
+            CloudStateFetcher cloudFetcher) {
+        TableStreamManager manager = env.getTableStreamManager();
+        if (manager == null) {
+            return List.of();
+        }
+        Map<Long, Set<Long>> dbStreamMap = manager.copyDbStreamMap();
+        if (dbStreamMap.isEmpty()) {
+            tracker.clear();
+            return List.of();
+        }
+        Long referenceTso = getReferenceTso(env);

Review Comment:
   [P2] Refresh the TSO reference before timestamping late samples
   
   `referenceTso` is captured once before every stream's lock waits and all 
cloud RPC chunks, but successful results receive a post-lock/post-RPC 
`sampleTimeNanos`. For example, start at TSO physical time 100s with TTL 60s; 
120 preceding 500ms lock timeouts delay another stream until 160s. With 
consumed offset 50s and end TSO 80s, its cutoff is still 40s, so 
`checkPartition()` reports resumable, although the current cutoff is 100s and 
that backlog has expired. The 160s sample timestamp then presents this result 
as fresh for another default 45s. Cloud collection has the same mismatch. 
Refresh the TSO reference outside metadata locks near evaluation, or retain the 
reference's original freshness timestamp so delayed results expire.



-- 
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]

Reply via email to