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


##########
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");

Review Comment:
   [P2] Preserve old FE replies on an upgraded coordinator
   
   During a rolling upgrade, an old FE sends tablet assignments without 
versions. An old worker with a vanished local replica legitimately returns `OK` 
without a schema or coverage. This check rejects the reply even when another 
worker returns that tablet's schema. The reverse mix can also fail. If the new 
worker reports the missing copy, `check_remote_tablet_coverage()` ignores the 
healthy old worker's schema because it has no coverage field. Preserve the 
legacy merge for unversioned requests by merging successful schemas and 
tolerating a worker's empty success reply. Keep per-tablet verification for 
versioned requests and test both worker combinations behind a new 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),
+                                                                
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)) {

Review Comment:
   [P2] Recognize old coordinators that forward tablet versions
   
   A new FE sends `tablet_visible_version`, and an old coordinator copies the 
whole protobuf when changing `is_coordinator`. That copy preserves the unknown 
nested version field while leaving `report_tablet_coverage` unset. An upgraded 
worker with a vanished shared-nothing replica therefore takes the error path 
here, and the old coordinator discards healthy workers' schemas. The same 
mismatch makes a lagging local copy fatal. Both FE attempts can hit old 
coordinators in a mixed fleet. Use the coverage flag to preserve the legacy 
missing-copy reply for incapable coordinators, including when version fields 
survive forwarding, and add a test with versions present and coverage absent.



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