yujun777 commented on code in PR #71199:
URL: https://github.com/apache/doris/pull/71199#discussion_r4752673450


##########
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:
   Fixed in 5724df473df:
   - `OlapStreamMonitorCollector` no longer captures a single round-start 
`referenceTso`. Local mode captures `referenceTso` alongside `sampleTimeNanos` 
immediately after releasing each stream's metadata read locks; Cloud mode 
captures `referenceTso` alongside `sampleTimeNanos` when each chunk/fallback 
RPC completes (retaining the earliest slice freshness timestamp and latest TSO 
across multi-slice wide streams).
   - Also updated 
`TableStreamManager.fillCloudStreamConsumptionValuesMetadataResult` to compute 
`referenceTso` and `cutoffTso` after the MetaService batch RPC returns.
   - Added unit tests in `OlapStreamMonitorCollectorTest` covering advancing 
TSO/clock during slow Local and Cloud rounds.



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