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


##########
be/src/service/internal_service.cpp:
##########
@@ -1237,131 +1244,308 @@ void 
PInternalServiceImpl::_get_column_ids_by_tablet_ids(
     response->mutable_status()->set_status_code(TStatusCode::OK);
 }
 
-template <class RPCResponse>
-struct AsyncRPCContext {
-    RPCResponse response;
-    brpc::Controller cntl;
-    brpc::CallId cid;
+namespace {
+
+struct SchemaRpc {
+    PFetchRemoteSchemaRequest request;
+    PFetchRemoteSchemaResponse response;
+    brpc::Controller controller;
+    std::optional<brpc::CallId> call_id;
 };
 
-void 
PInternalService::fetch_remote_tablet_schema(google::protobuf::RpcController* 
controller,
-                                                  const 
PFetchRemoteSchemaRequest* request,
-                                                  PFetchRemoteSchemaResponse* 
response,
-                                                  google::protobuf::Closure* 
done) {
-    bool ret = _heavy_work_pool.try_offer([request, response, done]() {
-        brpc::ClosureGuard closure_guard(done);
-        Status st = Status::OK();
-        std::shared_ptr<MemTrackerLimiter> mem_tracker = 
MemTrackerLimiter::create_shared(
-                MemTrackerLimiter::Type::OTHER,
-                fmt::format("InternalService::fetch_remote_tablet_schema"));
-        SCOPED_ATTACH_TASK(mem_tracker);
-        if (request->is_coordinator()) {
-            // Spawn rpc request to none coordinator nodes, and finally merge 
them all
-            PFetchRemoteSchemaRequest remote_request(*request);
-            // set it none coordinator to get merged schema
-            remote_request.set_is_coordinator(false);
-            using PFetchRemoteTabletSchemaRpcContext = 
AsyncRPCContext<PFetchRemoteSchemaResponse>;
-            std::vector<PFetchRemoteTabletSchemaRpcContext> rpc_contexts(
-                    request->tablet_location_size());
-            for (int i = 0; i < request->tablet_location_size(); ++i) {
-                std::string host = request->tablet_location(i).host();
-                int32_t brpc_port = request->tablet_location(i).brpc_port();
-                std::shared_ptr<PBackendService_Stub> stub(
-                        
ExecEnv::GetInstance()->brpc_internal_client_cache()->get_client(
-                                host, brpc_port));
-                if (stub == nullptr) {
-                    LOG(WARNING) << "Failed to init rpc to " << host << ":" << 
brpc_port;
-                    st = Status::InternalError("Failed to init rpc to {}:{}", 
host, brpc_port);
-                    continue;
-                }
-                rpc_contexts[i].cid = rpc_contexts[i].cntl.call_id();
-                
rpc_contexts[i].cntl.set_timeout_ms(config::fetch_remote_schema_rpc_timeout_ms);
-                stub->fetch_remote_tablet_schema(&rpc_contexts[i].cntl, 
&remote_request,
-                                                 &rpc_contexts[i].response, 
brpc::DoNothing());
+bool has_required_tablet_coverage(const SchemaRpc& rpc) {
+    const auto& location = rpc.request.tablet_location(0);
+    return location.tablet_visible_version_size() == 0 ||
+           (rpc.response.has_tablet_coverage() && 
rpc.response.tablet_versions_verified());
+}
+
+bool has_legacy_remote_schema(const SchemaRpc& rpc) {
+    const auto& location = rpc.request.tablet_location(0);
+    return location.tablet_visible_version_size() > 0 &&
+           !rpc.response.has_tablet_versions_verified() && 
rpc.response.has_merged_schema();
+}
+
+Status check_remote_tablet_coverage(const std::vector<SchemaRpc>& rpcs,
+                                    std::unordered_set<int64_t>& 
legacy_tablets) {
+    // Healthy requests do not need per-tablet sets.
+    if (std::ranges::none_of(rpcs, [](const auto& rpc) {
+            return !has_required_tablet_coverage(rpc) ||
+                   rpc.response.tablet_coverage().missing_tablet_id_size() > 0;
+        })) {
+        return Status::OK();
+    }
+    std::unordered_set<int64_t> uncovered;
+    std::unordered_set<int64_t> covered;
+    for (const auto& rpc : rpcs) {
+        const auto& assigned = rpc.request.tablet_location(0).tablet_id();
+        const bool legacy = has_legacy_remote_schema(rpc);
+        if (!has_required_tablet_coverage(rpc) && !legacy) {
+            uncovered.insert(assigned.begin(), assigned.end());
+            continue;
+        }
+        if (!rpc.response.has_tablet_coverage() && !legacy) {
+            continue;
+        }
+        const auto& reported_missing = 
rpc.response.tablet_coverage().missing_tablet_id();
+        std::unordered_map<int64_t, bool> missing;
+        for (int64_t tablet_id : reported_missing) {
+            missing.try_emplace(tablet_id, false);
+        }
+        for (int64_t tablet_id : assigned) {
+            if (auto it = missing.find(tablet_id); it != missing.end()) {
+                it->second = true;
+                uncovered.insert(tablet_id);
+            } else if (legacy) {
+                legacy_tablets.insert(tablet_id);
+            } else if (rpc.response.has_merged_schema()) {
+                covered.insert(tablet_id);
             }
-            std::vector<TabletSchemaSPtr> schemas;
-            for (auto& rpc_context : rpc_contexts) {
-                brpc::Join(rpc_context.cid);
-                if (!st.ok()) {
-                    // make sure all flying rpc request is joined
+        }
+        for (const auto& [tablet_id, assigned_here] : missing) {
+            if (!assigned_here) {
+                return Status::InternalError("Worker reported an unassigned 
tablet {}", tablet_id);
+            }
+        }
+    }
+    for (int64_t tablet_id : covered) {
+        uncovered.erase(tablet_id);
+        legacy_tablets.erase(tablet_id);
+    }
+    // A legacy reply can replace missing version support. Reported missing 
tablets or an
+    // explicit negative verification still require another worker to prove 
coverage.
+    if (!uncovered.empty()) {
+        return Status::Error<ErrorCode::TABLE_NOT_FOUND>(
+                "Extended DESCRIBE could not read tablet {} ({} uncovered 
tablets)",
+                *std::min_element(uncovered.begin(), uncovered.end()), 
uncovered.size());
+    }
+    return Status::OK();
+}
+
+void append_remote_variant_schema(const PFetchRemoteSchemaResponse& response,
+                                  std::vector<TabletSchemaSPtr>& schemas) {
+    auto schema = std::make_shared<TabletSchema>();
+    schema->init_from_pb(response.merged_schema());
+    schemas.push_back(std::move(schema));
+}
+
+Status collect_remote_variant_schemas(const PFetchRemoteSchemaRequest& request,
+                                      std::vector<TabletSchemaSPtr>& schemas,
+                                      bool& versions_verified) {
+    std::vector<SchemaRpc> rpcs(request.tablet_location_size());
+    auto join_rpcs = [&] {
+        for (auto& rpc : rpcs) {
+            if (rpc.call_id.has_value()) {
+                brpc::Join(*rpc.call_id);
+                rpc.call_id.reset();
+            }
+        }
+    };
+    // Controllers, responses and the request must survive all dispatched RPCs 
on error too.
+    Defer join_on_exit {join_rpcs};
+    for (int i = 0; i < request.tablet_location_size(); ++i) {
+        const auto& location = request.tablet_location(i);
+        auto stub = 
ExecEnv::GetInstance()->brpc_internal_client_cache()->get_client(
+                location.host(), location.brpc_port());
+        if (stub == nullptr) {
+            return Status::NetworkError("Failed to initialize remote schema 
RPC to {}:{}",
+                                        location.host(), location.brpc_port());
+        }
+        auto& rpc = rpcs[i];
+        rpc.request.set_is_coordinator(false);
+        rpc.request.set_report_tablet_coverage(true);
+        *rpc.request.add_tablet_location() = location;
+        
rpc.controller.set_timeout_ms(config::fetch_remote_schema_rpc_timeout_ms);
+        stub->fetch_remote_tablet_schema(&rpc.controller, &rpc.request, 
&rpc.response,
+                                         brpc::DoNothing());
+        // Preparing the callback can throw before BRPC takes ownership of the 
call.
+        rpc.call_id = rpc.controller.call_id();
+    }
+    join_rpcs();
+    for (const auto& rpc : rpcs) {
+        if (rpc.controller.Failed()) {
+            ExecEnv::GetInstance()->brpc_internal_client_cache()->erase(
+                    rpc.controller.remote_side());
+            return Status::NetworkError("Remote schema RPC failed: {}", 
rpc.controller.ErrorText());
+        }
+        if (!rpc.response.has_status()) {
+            return Status::InternalError("Remote schema RPC did not return a 
status");
+        }
+        if (rpc.response.status().status_code() != 0) {
+            return Status::create(rpc.response.status());
+        }
+        if (!has_required_tablet_coverage(rpc)) {
+            continue;
+        }
+        if (!rpc.response.has_merged_schema()) {
+            // A new worker with no local copies can still contribute a 
coverage report.
+            const int missing = 
rpc.response.tablet_coverage().missing_tablet_id_size();
+            if (missing > 0 && missing == 
rpc.request.tablet_location(0).tablet_id_size()) {
+                continue;
+            }
+            return Status::InternalError("Remote schema RPC did not return a 
merged schema");
+        }
+        append_remote_variant_schema(rpc.response, schemas);
+    }
+    std::unordered_set<int64_t> legacy_tablets;
+    RETURN_IF_ERROR(check_remote_tablet_coverage(rpcs, legacy_tablets));
+    if (!legacy_tablets.empty()) {
+        versions_verified = false;
+        for (const auto& rpc : rpcs) {
+            if (has_legacy_remote_schema(rpc) &&
+                std::ranges::any_of(rpc.request.tablet_location(0).tablet_id(),
+                                    [&](int64_t id) { return 
legacy_tablets.contains(id); })) {
+                append_remote_variant_schema(rpc.response, schemas);
+            }
+        }
+    }
+    return Status::OK();
+}
+
+Result<std::vector<RowsetSharedPtr>> capture_desc_rowsets(const 
BaseTabletSPtr& tablet,
+                                                          int64_t 
visible_version) {
+    if (visible_version < 0) {
+        return tablet->get_snapshot_rowset();
+    }
+    if (auto cloud_tablet = std::dynamic_pointer_cast<CloudTablet>(tablet)) {
+        // The cached tablet might have been loaded before the partition 
committed this version.
+        // sync_rowsets skips the MS RPC when the requested version is already 
available locally.
+        SyncOptions options;
+        options.query_version = visible_version;
+        RETURN_IF_ERROR_RESULT(cloud_tablet->sync_rowsets(options));
+    }
+    std::shared_lock lock(tablet->get_header_lock());
+    if (tablet->max_version_unlocked() < visible_version) {
+        return ResultError(Status::Error<ErrorCode::VERSION_NOT_EXIST>(
+                "Tablet {} is behind visible version {}", tablet->tablet_id(), 
visible_version));
+    }
+    auto captured = tablet->capture_consistent_rowsets_unlocked(Version(0, 
visible_version),
+                                                                
CaptureRowsetOps {});
+    if (!captured.has_value()) {
+        return ResultError(std::move(captured.error()));
+    }
+    return std::move(captured.value().rowsets);
+}
+
+Status collect_local_variant_schemas(const PFetchRemoteSchemaRequest& request,
+                                     PFetchRemoteSchemaResponse* response,
+                                     std::vector<TabletSchemaSPtr>& schemas) {
+    const auto backend = BackendOptions::get_local_backend();
+    for (const auto& location : request.tablet_location()) {
+        if (backend.host != location.host() || config::brpc_port != 
location.brpc_port()) {
+            continue;
+        }
+        auto* coverage =
+                request.report_tablet_coverage() ? 
response->mutable_tablet_coverage() : nullptr;
+        for (int i = 0; i < location.tablet_id_size(); ++i) {
+            int64_t tablet_id = location.tablet_id(i);
+            auto tablet = ExecEnv::get_tablet(tablet_id);
+            if (!tablet.has_value()) {
+                // Local placement can be stale while another replica still 
covers this tablet.
+                // A Cloud lookup or file-read failure does not establish that 
a replica is absent.
+                if (!config::is_cloud_mode() && 
tablet.error().is<ErrorCode::TABLE_NOT_FOUND>() &&
+                    (coverage != nullptr || 
location.tablet_visible_version_size() == 0)) {
+                    // Old coordinators ignore the coverage field and accept a 
successful reply
+                    // without a schema. Preserve that reply even when every 
local copy is absent.
+                    
response->mutable_tablet_coverage()->add_missing_tablet_id(tablet_id);
                     continue;
                 }
-                if (rpc_context.cntl.Failed()) {
-                    LOG(WARNING) << "fetch_remote_tablet_schema rpc err:"
-                                 << rpc_context.cntl.ErrorText();
-                    
ExecEnv::GetInstance()->brpc_internal_client_cache()->erase(
-                            rpc_context.cntl.remote_side());
-                    st = Status::InternalError("fetch_remote_tablet_schema rpc 
err: {}",
-                                               rpc_context.cntl.ErrorText());
-                }
-                if (rpc_context.response.status().status_code() != 0) {
-                    st = Status::create(rpc_context.response.status());
-                }
-                if (rpc_context.response.has_merged_schema()) {
-                    TabletSchemaSPtr schema = std::make_shared<TabletSchema>();
-                    schema->init_from_pb(rpc_context.response.merged_schema());
-                    schemas.push_back(schema);
-                }
+                return tablet.error().prepend(
+                        fmt::format("Extended DESCRIBE could not get tablet 
{}. ", tablet_id));
             }
-            if (!schemas.empty() && st.ok()) {
-                // merge all
-                TabletSchemaSPtr merged_schema;
-                st = variant_util::get_least_common_schema(schemas, nullptr, 
merged_schema);
-                if (!st.ok()) {
-                    LOG(WARNING) << "Failed to get least common schema: " << 
st.to_string();
-                    st = Status::InternalError("Failed to get least common 
schema: {}",
-                                               st.to_string());
+            int64_t version = location.tablet_visible_version_size() == 0
+                                      ? -1
+                                      : location.tablet_visible_version(i);
+            auto rowsets = capture_desc_rowsets(tablet.value(), version);
+            if (!rowsets.has_value()) {
+                // A replica which has not caught up cannot prove coverage for 
this tablet.
+                if (coverage != nullptr && !config::is_cloud_mode() &&
+                    rowsets.error().is<ErrorCode::VERSION_NOT_EXIST>()) {
+                    coverage->add_missing_tablet_id(tablet_id);
+                    continue;
                 }
-                VLOG_DEBUG << "dump schema:" << 
merged_schema->dump_structure();
-                merged_schema->reserve_extracted_columns();
-                merged_schema->to_schema_pb(response->mutable_merged_schema());
+                return std::move(rowsets.error());
             }
-            st.to_protobuf(response->mutable_status());
-            return;
-        } else {
-            // This is not a coordinator, get it's tablet and merge schema
-            std::vector<int64_t> target_tablets;
-            for (int i = 0; i < request->tablet_location_size(); ++i) {
-                const auto& location = request->tablet_location(i);
-                auto backend = BackendOptions::get_local_backend();
-                // If this is the target backend
-                if (backend.host == location.host() && config::brpc_port == 
location.brpc_port()) {
-                    target_tablets.assign(location.tablet_id().begin(), 
location.tablet_id().end());
-                    break;
-                }
+            auto schema = DORIS_TRY(
+                    
variant_util::VariantCompactionUtil::calculate_variant_extended_schema(
+                            rowsets.value(), tablet.value()->tablet_schema()));

Review Comment:
   [P2] Use the captured schema to verify newly added Variant roots
   
   In Cloud, warm the tablet in a read-only compute group, then complete a 
light ADD VARIANT and an INSERT that materializes `extra.k` through another 
group. A subsequent DESC in the warmed group can sync and capture the new 
rowset while `tablet_schema()` still returns its old cached schema. 
`get_least_common_schema` only visits Variant root IDs in that base schema, so 
the worker omits `extra.k` and the FE accepts the reply as verified. Both 
changes completed before DESC, so the FE generation checks pass. Bind the 
merge's root IDs to the captured catalog schema or reject a worker schema that 
cannot verify them. Cover the cross-group case.



##########
regression-test/suites/fault_injection_p0/test_variant_desc_rpc_lock.groovy:
##########
@@ -0,0 +1,82 @@
+// 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.
+
+import java.util.concurrent.TimeUnit
+
+suite("test_variant_desc_rpc_lock", "nonConcurrent") {
+    if (isCloudMode() || !getFeConfig("enable_debug_points").toBoolean()) {
+        logger.info("This suite requires a shared-nothing cluster with FE 
debug points enabled")
+        return
+    }
+
+    sql "DROP TABLE IF EXISTS variant_desc_rpc_lock"
+    sql """
+        CREATE TABLE variant_desc_rpc_lock (id INT, payload VARIANT<PROPERTIES(
+                "variant_enable_doc_mode" = "false")>)

Review Comment:
   [P2] Pin materialization in both remaining path-based fixtures
   
   With debug points and session fuzzing enabled, the omitted materialization 
threshold can exceed the one-row input and leave `payload.detail` in sparse 
storage. The lock test then fails its path assertion even when Stream Load 
succeeds during the pause. `test_variant_desc_tablet_coverage.groovy` has the 
same dependency for its expected per-tablet paths. Set 
`variant_doc_materialization_min_rows` to 0 and a positive 
`variant_max_subcolumns_count` in both fixtures, as the Cloud lock and 
completeness suites already do.



##########
be/src/service/internal_service.cpp:
##########
@@ -1237,131 +1244,308 @@ void 
PInternalServiceImpl::_get_column_ids_by_tablet_ids(
     response->mutable_status()->set_status_code(TStatusCode::OK);
 }
 
-template <class RPCResponse>
-struct AsyncRPCContext {
-    RPCResponse response;
-    brpc::Controller cntl;
-    brpc::CallId cid;
+namespace {
+
+struct SchemaRpc {
+    PFetchRemoteSchemaRequest request;
+    PFetchRemoteSchemaResponse response;
+    brpc::Controller controller;
+    std::optional<brpc::CallId> call_id;
 };
 
-void 
PInternalService::fetch_remote_tablet_schema(google::protobuf::RpcController* 
controller,
-                                                  const 
PFetchRemoteSchemaRequest* request,
-                                                  PFetchRemoteSchemaResponse* 
response,
-                                                  google::protobuf::Closure* 
done) {
-    bool ret = _heavy_work_pool.try_offer([request, response, done]() {
-        brpc::ClosureGuard closure_guard(done);
-        Status st = Status::OK();
-        std::shared_ptr<MemTrackerLimiter> mem_tracker = 
MemTrackerLimiter::create_shared(
-                MemTrackerLimiter::Type::OTHER,
-                fmt::format("InternalService::fetch_remote_tablet_schema"));
-        SCOPED_ATTACH_TASK(mem_tracker);
-        if (request->is_coordinator()) {
-            // Spawn rpc request to none coordinator nodes, and finally merge 
them all
-            PFetchRemoteSchemaRequest remote_request(*request);
-            // set it none coordinator to get merged schema
-            remote_request.set_is_coordinator(false);
-            using PFetchRemoteTabletSchemaRpcContext = 
AsyncRPCContext<PFetchRemoteSchemaResponse>;
-            std::vector<PFetchRemoteTabletSchemaRpcContext> rpc_contexts(
-                    request->tablet_location_size());
-            for (int i = 0; i < request->tablet_location_size(); ++i) {
-                std::string host = request->tablet_location(i).host();
-                int32_t brpc_port = request->tablet_location(i).brpc_port();
-                std::shared_ptr<PBackendService_Stub> stub(
-                        
ExecEnv::GetInstance()->brpc_internal_client_cache()->get_client(
-                                host, brpc_port));
-                if (stub == nullptr) {
-                    LOG(WARNING) << "Failed to init rpc to " << host << ":" << 
brpc_port;
-                    st = Status::InternalError("Failed to init rpc to {}:{}", 
host, brpc_port);
-                    continue;
-                }
-                rpc_contexts[i].cid = rpc_contexts[i].cntl.call_id();
-                
rpc_contexts[i].cntl.set_timeout_ms(config::fetch_remote_schema_rpc_timeout_ms);
-                stub->fetch_remote_tablet_schema(&rpc_contexts[i].cntl, 
&remote_request,
-                                                 &rpc_contexts[i].response, 
brpc::DoNothing());
+bool has_required_tablet_coverage(const SchemaRpc& rpc) {
+    const auto& location = rpc.request.tablet_location(0);
+    return location.tablet_visible_version_size() == 0 ||
+           (rpc.response.has_tablet_coverage() && 
rpc.response.tablet_versions_verified());
+}
+
+bool has_legacy_remote_schema(const SchemaRpc& rpc) {
+    const auto& location = rpc.request.tablet_location(0);
+    return location.tablet_visible_version_size() > 0 &&
+           !rpc.response.has_tablet_versions_verified() && 
rpc.response.has_merged_schema();
+}
+
+Status check_remote_tablet_coverage(const std::vector<SchemaRpc>& rpcs,
+                                    std::unordered_set<int64_t>& 
legacy_tablets) {
+    // Healthy requests do not need per-tablet sets.
+    if (std::ranges::none_of(rpcs, [](const auto& rpc) {
+            return !has_required_tablet_coverage(rpc) ||
+                   rpc.response.tablet_coverage().missing_tablet_id_size() > 0;
+        })) {
+        return Status::OK();
+    }
+    std::unordered_set<int64_t> uncovered;
+    std::unordered_set<int64_t> covered;
+    for (const auto& rpc : rpcs) {
+        const auto& assigned = rpc.request.tablet_location(0).tablet_id();
+        const bool legacy = has_legacy_remote_schema(rpc);
+        if (!has_required_tablet_coverage(rpc) && !legacy) {
+            uncovered.insert(assigned.begin(), assigned.end());
+            continue;
+        }
+        if (!rpc.response.has_tablet_coverage() && !legacy) {
+            continue;
+        }
+        const auto& reported_missing = 
rpc.response.tablet_coverage().missing_tablet_id();
+        std::unordered_map<int64_t, bool> missing;
+        for (int64_t tablet_id : reported_missing) {
+            missing.try_emplace(tablet_id, false);
+        }
+        for (int64_t tablet_id : assigned) {
+            if (auto it = missing.find(tablet_id); it != missing.end()) {
+                it->second = true;
+                uncovered.insert(tablet_id);
+            } else if (legacy) {
+                legacy_tablets.insert(tablet_id);
+            } else if (rpc.response.has_merged_schema()) {
+                covered.insert(tablet_id);
             }
-            std::vector<TabletSchemaSPtr> schemas;
-            for (auto& rpc_context : rpc_contexts) {
-                brpc::Join(rpc_context.cid);
-                if (!st.ok()) {
-                    // make sure all flying rpc request is joined
+        }
+        for (const auto& [tablet_id, assigned_here] : missing) {
+            if (!assigned_here) {
+                return Status::InternalError("Worker reported an unassigned 
tablet {}", tablet_id);
+            }
+        }
+    }
+    for (int64_t tablet_id : covered) {
+        uncovered.erase(tablet_id);
+        legacy_tablets.erase(tablet_id);
+    }
+    // A legacy reply can replace missing version support. Reported missing 
tablets or an
+    // explicit negative verification still require another worker to prove 
coverage.
+    if (!uncovered.empty()) {
+        return Status::Error<ErrorCode::TABLE_NOT_FOUND>(
+                "Extended DESCRIBE could not read tablet {} ({} uncovered 
tablets)",
+                *std::min_element(uncovered.begin(), uncovered.end()), 
uncovered.size());
+    }
+    return Status::OK();
+}
+
+void append_remote_variant_schema(const PFetchRemoteSchemaResponse& response,
+                                  std::vector<TabletSchemaSPtr>& schemas) {
+    auto schema = std::make_shared<TabletSchema>();
+    schema->init_from_pb(response.merged_schema());
+    schemas.push_back(std::move(schema));
+}
+
+Status collect_remote_variant_schemas(const PFetchRemoteSchemaRequest& request,
+                                      std::vector<TabletSchemaSPtr>& schemas,
+                                      bool& versions_verified) {
+    std::vector<SchemaRpc> rpcs(request.tablet_location_size());
+    auto join_rpcs = [&] {
+        for (auto& rpc : rpcs) {
+            if (rpc.call_id.has_value()) {
+                brpc::Join(*rpc.call_id);
+                rpc.call_id.reset();
+            }
+        }
+    };
+    // Controllers, responses and the request must survive all dispatched RPCs 
on error too.
+    Defer join_on_exit {join_rpcs};
+    for (int i = 0; i < request.tablet_location_size(); ++i) {
+        const auto& location = request.tablet_location(i);
+        auto stub = 
ExecEnv::GetInstance()->brpc_internal_client_cache()->get_client(
+                location.host(), location.brpc_port());
+        if (stub == nullptr) {
+            return Status::NetworkError("Failed to initialize remote schema 
RPC to {}:{}",
+                                        location.host(), location.brpc_port());
+        }
+        auto& rpc = rpcs[i];
+        rpc.request.set_is_coordinator(false);
+        rpc.request.set_report_tablet_coverage(true);
+        *rpc.request.add_tablet_location() = location;
+        
rpc.controller.set_timeout_ms(config::fetch_remote_schema_rpc_timeout_ms);
+        stub->fetch_remote_tablet_schema(&rpc.controller, &rpc.request, 
&rpc.response,
+                                         brpc::DoNothing());
+        // Preparing the callback can throw before BRPC takes ownership of the 
call.
+        rpc.call_id = rpc.controller.call_id();
+    }
+    join_rpcs();
+    for (const auto& rpc : rpcs) {
+        if (rpc.controller.Failed()) {
+            ExecEnv::GetInstance()->brpc_internal_client_cache()->erase(
+                    rpc.controller.remote_side());
+            return Status::NetworkError("Remote schema RPC failed: {}", 
rpc.controller.ErrorText());
+        }
+        if (!rpc.response.has_status()) {
+            return Status::InternalError("Remote schema RPC did not return a 
status");
+        }
+        if (rpc.response.status().status_code() != 0) {
+            return Status::create(rpc.response.status());
+        }
+        if (!has_required_tablet_coverage(rpc)) {
+            continue;
+        }
+        if (!rpc.response.has_merged_schema()) {
+            // A new worker with no local copies can still contribute a 
coverage report.
+            const int missing = 
rpc.response.tablet_coverage().missing_tablet_id_size();
+            if (missing > 0 && missing == 
rpc.request.tablet_location(0).tablet_id_size()) {
+                continue;
+            }
+            return Status::InternalError("Remote schema RPC did not return a 
merged schema");
+        }
+        append_remote_variant_schema(rpc.response, schemas);
+    }
+    std::unordered_set<int64_t> legacy_tablets;
+    RETURN_IF_ERROR(check_remote_tablet_coverage(rpcs, legacy_tablets));
+    if (!legacy_tablets.empty()) {
+        versions_verified = false;
+        for (const auto& rpc : rpcs) {
+            if (has_legacy_remote_schema(rpc) &&
+                std::ranges::any_of(rpc.request.tablet_location(0).tablet_id(),
+                                    [&](int64_t id) { return 
legacy_tablets.contains(id); })) {
+                append_remote_variant_schema(rpc.response, schemas);
+            }
+        }
+    }
+    return Status::OK();
+}
+
+Result<std::vector<RowsetSharedPtr>> capture_desc_rowsets(const 
BaseTabletSPtr& tablet,
+                                                          int64_t 
visible_version) {
+    if (visible_version < 0) {
+        return tablet->get_snapshot_rowset();
+    }
+    if (auto cloud_tablet = std::dynamic_pointer_cast<CloudTablet>(tablet)) {
+        // The cached tablet might have been loaded before the partition 
committed this version.
+        // sync_rowsets skips the MS RPC when the requested version is already 
available locally.
+        SyncOptions options;
+        options.query_version = visible_version;
+        RETURN_IF_ERROR_RESULT(cloud_tablet->sync_rowsets(options));
+    }
+    std::shared_lock lock(tablet->get_header_lock());
+    if (tablet->max_version_unlocked() < visible_version) {
+        return ResultError(Status::Error<ErrorCode::VERSION_NOT_EXIST>(
+                "Tablet {} is behind visible version {}", tablet->tablet_id(), 
visible_version));
+    }
+    auto captured = tablet->capture_consistent_rowsets_unlocked(Version(0, 
visible_version),
+                                                                
CaptureRowsetOps {});
+    if (!captured.has_value()) {
+        return ResultError(std::move(captured.error()));
+    }
+    return std::move(captured.value().rowsets);
+}
+
+Status collect_local_variant_schemas(const PFetchRemoteSchemaRequest& request,
+                                     PFetchRemoteSchemaResponse* response,
+                                     std::vector<TabletSchemaSPtr>& schemas) {
+    const auto backend = BackendOptions::get_local_backend();
+    for (const auto& location : request.tablet_location()) {
+        if (backend.host != location.host() || config::brpc_port != 
location.brpc_port()) {
+            continue;
+        }
+        auto* coverage =
+                request.report_tablet_coverage() ? 
response->mutable_tablet_coverage() : nullptr;
+        for (int i = 0; i < location.tablet_id_size(); ++i) {
+            int64_t tablet_id = location.tablet_id(i);
+            auto tablet = ExecEnv::get_tablet(tablet_id);
+            if (!tablet.has_value()) {
+                // Local placement can be stale while another replica still 
covers this tablet.
+                // A Cloud lookup or file-read failure does not establish that 
a replica is absent.
+                if (!config::is_cloud_mode() && 
tablet.error().is<ErrorCode::TABLE_NOT_FOUND>() &&
+                    (coverage != nullptr || 
location.tablet_visible_version_size() == 0)) {
+                    // Old coordinators ignore the coverage field and accept a 
successful reply
+                    // without a schema. Preserve that reply even when every 
local copy is absent.
+                    
response->mutable_tablet_coverage()->add_missing_tablet_id(tablet_id);
                     continue;
                 }
-                if (rpc_context.cntl.Failed()) {
-                    LOG(WARNING) << "fetch_remote_tablet_schema rpc err:"
-                                 << rpc_context.cntl.ErrorText();
-                    
ExecEnv::GetInstance()->brpc_internal_client_cache()->erase(
-                            rpc_context.cntl.remote_side());
-                    st = Status::InternalError("fetch_remote_tablet_schema rpc 
err: {}",
-                                               rpc_context.cntl.ErrorText());
-                }
-                if (rpc_context.response.status().status_code() != 0) {
-                    st = Status::create(rpc_context.response.status());
-                }
-                if (rpc_context.response.has_merged_schema()) {
-                    TabletSchemaSPtr schema = std::make_shared<TabletSchema>();
-                    schema->init_from_pb(rpc_context.response.merged_schema());
-                    schemas.push_back(schema);
-                }
+                return tablet.error().prepend(
+                        fmt::format("Extended DESCRIBE could not get tablet 
{}. ", tablet_id));
             }
-            if (!schemas.empty() && st.ok()) {
-                // merge all
-                TabletSchemaSPtr merged_schema;
-                st = variant_util::get_least_common_schema(schemas, nullptr, 
merged_schema);
-                if (!st.ok()) {
-                    LOG(WARNING) << "Failed to get least common schema: " << 
st.to_string();
-                    st = Status::InternalError("Failed to get least common 
schema: {}",
-                                               st.to_string());
+            int64_t version = location.tablet_visible_version_size() == 0
+                                      ? -1
+                                      : location.tablet_visible_version(i);
+            auto rowsets = capture_desc_rowsets(tablet.value(), version);
+            if (!rowsets.has_value()) {
+                // A replica which has not caught up cannot prove coverage for 
this tablet.
+                if (coverage != nullptr && !config::is_cloud_mode() &&
+                    rowsets.error().is<ErrorCode::VERSION_NOT_EXIST>()) {
+                    coverage->add_missing_tablet_id(tablet_id);
+                    continue;
                 }
-                VLOG_DEBUG << "dump schema:" << 
merged_schema->dump_structure();
-                merged_schema->reserve_extracted_columns();
-                merged_schema->to_schema_pb(response->mutable_merged_schema());
+                return std::move(rowsets.error());
             }
-            st.to_protobuf(response->mutable_status());
-            return;
-        } else {
-            // This is not a coordinator, get it's tablet and merge schema
-            std::vector<int64_t> target_tablets;
-            for (int i = 0; i < request->tablet_location_size(); ++i) {
-                const auto& location = request->tablet_location(i);
-                auto backend = BackendOptions::get_local_backend();
-                // If this is the target backend
-                if (backend.host == location.host() && config::brpc_port == 
location.brpc_port()) {
-                    target_tablets.assign(location.tablet_id().begin(), 
location.tablet_id().end());
-                    break;
-                }
+            auto schema = DORIS_TRY(
+                    
variant_util::VariantCompactionUtil::calculate_variant_extended_schema(
+                            rowsets.value(), tablet.value()->tablet_schema()));
+            schemas.push_back(std::move(schema));
+        }
+        return Status::OK();
+    }
+    return Status::InternalError("No remote schema tablet assignment for 
{}:{}", backend.host,

Review Comment:
   [P2] Match worker assignments across a BE hostname change
   
   When the FE changes a BE hostname to a DNS alias for the same endpoint, the 
RPC can reach that BE before its next heartbeat updates the local hostname. The 
worker compares the two hostname strings and reaches this error before looking 
up the tablet. The coordinator then fails extended DESC even if another replica 
returned the complete schema. Match the worker using a stable backend ID or 
honor the sole assignment sent by a coverage-aware coordinator.



##########
be/src/service/internal_service.cpp:
##########
@@ -1237,131 +1244,308 @@ void 
PInternalServiceImpl::_get_column_ids_by_tablet_ids(
     response->mutable_status()->set_status_code(TStatusCode::OK);
 }
 
-template <class RPCResponse>
-struct AsyncRPCContext {
-    RPCResponse response;
-    brpc::Controller cntl;
-    brpc::CallId cid;
+namespace {
+
+struct SchemaRpc {
+    PFetchRemoteSchemaRequest request;
+    PFetchRemoteSchemaResponse response;
+    brpc::Controller controller;
+    std::optional<brpc::CallId> call_id;
 };
 
-void 
PInternalService::fetch_remote_tablet_schema(google::protobuf::RpcController* 
controller,
-                                                  const 
PFetchRemoteSchemaRequest* request,
-                                                  PFetchRemoteSchemaResponse* 
response,
-                                                  google::protobuf::Closure* 
done) {
-    bool ret = _heavy_work_pool.try_offer([request, response, done]() {
-        brpc::ClosureGuard closure_guard(done);
-        Status st = Status::OK();
-        std::shared_ptr<MemTrackerLimiter> mem_tracker = 
MemTrackerLimiter::create_shared(
-                MemTrackerLimiter::Type::OTHER,
-                fmt::format("InternalService::fetch_remote_tablet_schema"));
-        SCOPED_ATTACH_TASK(mem_tracker);
-        if (request->is_coordinator()) {
-            // Spawn rpc request to none coordinator nodes, and finally merge 
them all
-            PFetchRemoteSchemaRequest remote_request(*request);
-            // set it none coordinator to get merged schema
-            remote_request.set_is_coordinator(false);
-            using PFetchRemoteTabletSchemaRpcContext = 
AsyncRPCContext<PFetchRemoteSchemaResponse>;
-            std::vector<PFetchRemoteTabletSchemaRpcContext> rpc_contexts(
-                    request->tablet_location_size());
-            for (int i = 0; i < request->tablet_location_size(); ++i) {
-                std::string host = request->tablet_location(i).host();
-                int32_t brpc_port = request->tablet_location(i).brpc_port();
-                std::shared_ptr<PBackendService_Stub> stub(
-                        
ExecEnv::GetInstance()->brpc_internal_client_cache()->get_client(
-                                host, brpc_port));
-                if (stub == nullptr) {
-                    LOG(WARNING) << "Failed to init rpc to " << host << ":" << 
brpc_port;
-                    st = Status::InternalError("Failed to init rpc to {}:{}", 
host, brpc_port);
-                    continue;
-                }
-                rpc_contexts[i].cid = rpc_contexts[i].cntl.call_id();
-                
rpc_contexts[i].cntl.set_timeout_ms(config::fetch_remote_schema_rpc_timeout_ms);
-                stub->fetch_remote_tablet_schema(&rpc_contexts[i].cntl, 
&remote_request,
-                                                 &rpc_contexts[i].response, 
brpc::DoNothing());
+bool has_required_tablet_coverage(const SchemaRpc& rpc) {
+    const auto& location = rpc.request.tablet_location(0);
+    return location.tablet_visible_version_size() == 0 ||
+           (rpc.response.has_tablet_coverage() && 
rpc.response.tablet_versions_verified());
+}
+
+bool has_legacy_remote_schema(const SchemaRpc& rpc) {
+    const auto& location = rpc.request.tablet_location(0);
+    return location.tablet_visible_version_size() > 0 &&
+           !rpc.response.has_tablet_versions_verified() && 
rpc.response.has_merged_schema();
+}
+
+Status check_remote_tablet_coverage(const std::vector<SchemaRpc>& rpcs,
+                                    std::unordered_set<int64_t>& 
legacy_tablets) {
+    // Healthy requests do not need per-tablet sets.
+    if (std::ranges::none_of(rpcs, [](const auto& rpc) {
+            return !has_required_tablet_coverage(rpc) ||
+                   rpc.response.tablet_coverage().missing_tablet_id_size() > 0;
+        })) {
+        return Status::OK();
+    }
+    std::unordered_set<int64_t> uncovered;
+    std::unordered_set<int64_t> covered;
+    for (const auto& rpc : rpcs) {
+        const auto& assigned = rpc.request.tablet_location(0).tablet_id();
+        const bool legacy = has_legacy_remote_schema(rpc);
+        if (!has_required_tablet_coverage(rpc) && !legacy) {
+            uncovered.insert(assigned.begin(), assigned.end());
+            continue;
+        }
+        if (!rpc.response.has_tablet_coverage() && !legacy) {
+            continue;
+        }
+        const auto& reported_missing = 
rpc.response.tablet_coverage().missing_tablet_id();
+        std::unordered_map<int64_t, bool> missing;
+        for (int64_t tablet_id : reported_missing) {
+            missing.try_emplace(tablet_id, false);
+        }
+        for (int64_t tablet_id : assigned) {
+            if (auto it = missing.find(tablet_id); it != missing.end()) {
+                it->second = true;
+                uncovered.insert(tablet_id);
+            } else if (legacy) {
+                legacy_tablets.insert(tablet_id);
+            } else if (rpc.response.has_merged_schema()) {
+                covered.insert(tablet_id);
             }
-            std::vector<TabletSchemaSPtr> schemas;
-            for (auto& rpc_context : rpc_contexts) {
-                brpc::Join(rpc_context.cid);
-                if (!st.ok()) {
-                    // make sure all flying rpc request is joined
+        }
+        for (const auto& [tablet_id, assigned_here] : missing) {
+            if (!assigned_here) {
+                return Status::InternalError("Worker reported an unassigned 
tablet {}", tablet_id);
+            }
+        }
+    }
+    for (int64_t tablet_id : covered) {
+        uncovered.erase(tablet_id);
+        legacy_tablets.erase(tablet_id);
+    }
+    // A legacy reply can replace missing version support. Reported missing 
tablets or an
+    // explicit negative verification still require another worker to prove 
coverage.
+    if (!uncovered.empty()) {
+        return Status::Error<ErrorCode::TABLE_NOT_FOUND>(
+                "Extended DESCRIBE could not read tablet {} ({} uncovered 
tablets)",
+                *std::min_element(uncovered.begin(), uncovered.end()), 
uncovered.size());
+    }
+    return Status::OK();
+}
+
+void append_remote_variant_schema(const PFetchRemoteSchemaResponse& response,
+                                  std::vector<TabletSchemaSPtr>& schemas) {
+    auto schema = std::make_shared<TabletSchema>();
+    schema->init_from_pb(response.merged_schema());
+    schemas.push_back(std::move(schema));
+}
+
+Status collect_remote_variant_schemas(const PFetchRemoteSchemaRequest& request,
+                                      std::vector<TabletSchemaSPtr>& schemas,
+                                      bool& versions_verified) {
+    std::vector<SchemaRpc> rpcs(request.tablet_location_size());
+    auto join_rpcs = [&] {
+        for (auto& rpc : rpcs) {
+            if (rpc.call_id.has_value()) {
+                brpc::Join(*rpc.call_id);
+                rpc.call_id.reset();
+            }
+        }
+    };
+    // Controllers, responses and the request must survive all dispatched RPCs 
on error too.
+    Defer join_on_exit {join_rpcs};
+    for (int i = 0; i < request.tablet_location_size(); ++i) {
+        const auto& location = request.tablet_location(i);
+        auto stub = 
ExecEnv::GetInstance()->brpc_internal_client_cache()->get_client(
+                location.host(), location.brpc_port());
+        if (stub == nullptr) {
+            return Status::NetworkError("Failed to initialize remote schema 
RPC to {}:{}",
+                                        location.host(), location.brpc_port());
+        }
+        auto& rpc = rpcs[i];
+        rpc.request.set_is_coordinator(false);
+        rpc.request.set_report_tablet_coverage(true);
+        *rpc.request.add_tablet_location() = location;
+        
rpc.controller.set_timeout_ms(config::fetch_remote_schema_rpc_timeout_ms);
+        stub->fetch_remote_tablet_schema(&rpc.controller, &rpc.request, 
&rpc.response,
+                                         brpc::DoNothing());
+        // Preparing the callback can throw before BRPC takes ownership of the 
call.
+        rpc.call_id = rpc.controller.call_id();
+    }
+    join_rpcs();
+    for (const auto& rpc : rpcs) {
+        if (rpc.controller.Failed()) {
+            ExecEnv::GetInstance()->brpc_internal_client_cache()->erase(
+                    rpc.controller.remote_side());
+            return Status::NetworkError("Remote schema RPC failed: {}", 
rpc.controller.ErrorText());
+        }
+        if (!rpc.response.has_status()) {
+            return Status::InternalError("Remote schema RPC did not return a 
status");
+        }
+        if (rpc.response.status().status_code() != 0) {
+            return Status::create(rpc.response.status());
+        }
+        if (!has_required_tablet_coverage(rpc)) {
+            continue;
+        }
+        if (!rpc.response.has_merged_schema()) {
+            // A new worker with no local copies can still contribute a 
coverage report.
+            const int missing = 
rpc.response.tablet_coverage().missing_tablet_id_size();
+            if (missing > 0 && missing == 
rpc.request.tablet_location(0).tablet_id_size()) {
+                continue;
+            }
+            return Status::InternalError("Remote schema RPC did not return a 
merged schema");
+        }
+        append_remote_variant_schema(rpc.response, schemas);
+    }
+    std::unordered_set<int64_t> legacy_tablets;
+    RETURN_IF_ERROR(check_remote_tablet_coverage(rpcs, legacy_tablets));
+    if (!legacy_tablets.empty()) {
+        versions_verified = false;
+        for (const auto& rpc : rpcs) {
+            if (has_legacy_remote_schema(rpc) &&
+                std::ranges::any_of(rpc.request.tablet_location(0).tablet_id(),
+                                    [&](int64_t id) { return 
legacy_tablets.contains(id); })) {
+                append_remote_variant_schema(rpc.response, schemas);
+            }
+        }
+    }
+    return Status::OK();
+}
+
+Result<std::vector<RowsetSharedPtr>> capture_desc_rowsets(const 
BaseTabletSPtr& tablet,
+                                                          int64_t 
visible_version) {
+    if (visible_version < 0) {
+        return tablet->get_snapshot_rowset();
+    }
+    if (auto cloud_tablet = std::dynamic_pointer_cast<CloudTablet>(tablet)) {
+        // The cached tablet might have been loaded before the partition 
committed this version.
+        // sync_rowsets skips the MS RPC when the requested version is already 
available locally.
+        SyncOptions options;
+        options.query_version = visible_version;
+        RETURN_IF_ERROR_RESULT(cloud_tablet->sync_rowsets(options));
+    }
+    std::shared_lock lock(tablet->get_header_lock());
+    if (tablet->max_version_unlocked() < visible_version) {
+        return ResultError(Status::Error<ErrorCode::VERSION_NOT_EXIST>(
+                "Tablet {} is behind visible version {}", tablet->tablet_id(), 
visible_version));
+    }
+    auto captured = tablet->capture_consistent_rowsets_unlocked(Version(0, 
visible_version),

Review Comment:
   [P2] Refresh the Cloud snapshot when compaction merges past its boundary
   
   If the FE captures version N, a load can publish N+1 and compaction can 
combine N and N+1 before a cold worker syncs the tablet. The worker loads the 
active compacted rowset and has no stale copy ending at N. This exact capture 
returns `VERSION_ALREADY_MERGED`, which the FE treats as terminal once the 
coordinator has accepted the request. Extended DESC then fails on a healthy 
table even though a fresh snapshot can be served. Retry this specific outcome 
once from a fresh FE snapshot within the original deadline, keeping the exact 
version bound on each attempt.



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