hello-stephen commented on code in PR #70969:
URL: https://github.com/apache/doris/pull/70969#discussion_r4698898759
##########
be/src/service/internal_service.cpp:
##########
@@ -1130,131 +1137,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 (coverage != nullptr && !config::is_cloud_mode() &&
+ tablet.error().is<ErrorCode::TABLE_NOT_FOUND>()) {
+ 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 = vectorized::schema_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>()) {
Review Comment:
[P2] Treat gaps in the version path as uncovered replicas
On a DUP_KEYS tablet, a live replica can contain `[0,1]` and `[3,3]` after
missing publish 2 while healthy replicas make partition version 3 visible. It
still passes `Replica.isAlive()` and `capture_desc_rowsets`' max-version check,
but `capture_consistent_versions_unlocked` returns `INTERNAL_ERROR` for the
gap. This `VERSION_NOT_EXIST`-only branch then aborts the worker, and the
coordinator rejects its status before considering healthy replicas' coverage.
Extended DESC/SHOW PROC requests sampling this tablet therefore fail until that
replica catches up, despite a complete snapshot being available elsewhere.
Classify missing-version capture failures as uncovered tablets too, while
preserving actual I/O/read errors.
--
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]