w41ter commented on code in PR #38243: URL: https://github.com/apache/doris/pull/38243#discussion_r1692571333
########## cloud/src/meta-service/meta_service.cpp: ########## @@ -237,46 +238,65 @@ void MetaServiceImpl::get_version(::google::protobuf::RpcController* controller, partition_version_key({instance_id, db_id, table_id, partition_id}, &ver_key); } - std::unique_ptr<Transaction> txn; - TxnErrorCode err = txn_kv_->create_txn(&txn); - if (err != TxnErrorCode::TXN_OK) { - msg = "failed to create txn"; - code = cast_as<ErrCategory::CREATE>(err); - return; - } + do { + code = MetaServiceCode::OK; + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv_->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + msg = "failed to create txn"; + code = cast_as<ErrCategory::CREATE>(err); + return; + } - std::string ver_val; - // 0 for success get a key, 1 for key not found, negative for error - err = txn->get(ver_key, &ver_val); - VLOG_DEBUG << "xxx get version_key=" << hex(ver_key); - if (err == TxnErrorCode::TXN_OK) { - if (is_table_version) { - int64_t version = 0; - if (!txn->decode_atomic_int(ver_val, &version)) { - code = MetaServiceCode::PROTOBUF_PARSE_ERR; - msg = "malformed table version value"; - return; - } - response->set_version(version); - } else { - VersionPB version_pb; - if (!version_pb.ParseFromString(ver_val)) { - code = MetaServiceCode::PROTOBUF_PARSE_ERR; - msg = "malformed version value"; - return; + std::string ver_val; + // 0 for success get a key, 1 for key not found, negative for error + err = txn->get(ver_key, &ver_val); + VLOG_DEBUG << "xxx get version_key=" << hex(ver_key); + if (err == TxnErrorCode::TXN_OK) { + if (is_table_version) { + int64_t version = 0; + if (!txn->decode_atomic_int(ver_val, &version)) { + code = MetaServiceCode::PROTOBUF_PARSE_ERR; + msg = "malformed table version value"; + return; + } + response->set_version(version); + } else { + VersionPB version_pb; + if (!version_pb.ParseFromString(ver_val)) { + code = MetaServiceCode::PROTOBUF_PARSE_ERR; + msg = "malformed version value"; + return; + } + + if (version_pb.has_txn_id()) { + txn.reset(); + std::shared_ptr<TxnLazyCommitTask> task = + txn_lazy_committer_->submit(instance_id, version_pb.txn_id()); + std::pair<MetaServiceCode, std::string> ret = task->wait(); + code = ret.first; + msg = ret.second; + if (code != MetaServiceCode::OK) { + LOG(WARNING) + << "wait txn lazy commit failed, txn_id=" << version_pb.txn_id(); + return; + } + continue; Review Comment: This can be simplified a bit. After the `wait()` ends, the transaction has already been committed, and the corresponding version is visible. We can return this version directly without retrying. ########## cloud/src/meta-service/meta_service.cpp: ########## @@ -387,10 +412,30 @@ void MetaServiceImpl::batch_get_version(::google::protobuf::RpcController* contr msg = "malformed version value"; break; } + if (version_pb.has_txn_id()) { + txn.reset(); + std::shared_ptr<TxnLazyCommitTask> task = + txn_lazy_committer_->submit(instance_id, version_pb.txn_id()); + std::pair<MetaServiceCode, std::string> ret = task->wait(); + code = ret.first; + msg = ret.second; + if (code != MetaServiceCode::OK) { + LOG(WARNING) << "wait txn lazy commit failed, txn_id=" + << version_pb.txn_id(); + break; + } + goto TRY_AGAIN; Review Comment: Directly fetching all versions and then waiting for all transactions at once is a simpler approach. For now, let's keep the current method to push the PR merge, and we can address this improvement in another PR. ########## cloud/src/meta-service/txn_lazy_committer.h: ########## @@ -0,0 +1,65 @@ +// 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. + +#pragma once + +#include <gen_cpp/cloud.pb.h> + +#include <atomic> + +#include "common/simple_thread_pool.h" +#include "meta-service/txn_kv.h" + +namespace doris::cloud { + +class TxnLazyCommitter; + +class TxnLazyCommitTask { +public: + TxnLazyCommitTask(const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + TxnLazyCommitter* txn_lazy_committer_); Review Comment: ```suggestion TxnLazyCommitter* txn_lazy_committer); ``` ########## cloud/src/meta-service/txn_lazy_committer.cpp: ########## @@ -0,0 +1,209 @@ +// 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. + +#include "txn_lazy_committer.h" + +#include "common/logging.h" +#include "common/util.h" +#include "meta-service/keys.h" +#include "meta-service/meta_service_helper.h" + +namespace doris::cloud { + +extern void scan_tmp_rowset( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t* db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>* tmp_rowsets_meta); + +extern void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids); + +extern void make_committed_txn_visible(const std::string& instance_id, int64_t db_id, + int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg); Review Comment: `extern` is not required here. ########## cloud/src/meta-service/txn_lazy_committer.cpp: ########## @@ -0,0 +1,209 @@ +// 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. + +#include "txn_lazy_committer.h" + +#include "common/logging.h" +#include "common/util.h" +#include "meta-service/keys.h" +#include "meta-service/meta_service_helper.h" + +namespace doris::cloud { + +extern void scan_tmp_rowset( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t* db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>* tmp_rowsets_meta); + +extern void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids); + +extern void make_committed_txn_visible(const std::string& instance_id, int64_t db_id, + int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg); + +TxnLazyCommitTask::TxnLazyCommitTask(const std::string& instance_id, + int64_t txn_id, + std::shared_ptr<TxnKv> txn_kv, + TxnLazyCommitter* txn_lazy_committer) { + DCHECK(txn_id > 0); + DCHECK(txn_lazy_committer != nullptr); + txn_id_ = txn_id; + txn_lazy_committer_ = txn_lazy_committer; Review Comment: Initialize in member initializer list, likes: ```c++ TxnLazyCommitTask::TxnLazyCommitTask(const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, TxnLazyCommitter* txn_lazy_committer) : instance_id_(instance_id), txn_id_(txn_id), txn_kv_(std::move(txn_id)), txn_lazy_committer_(txn_lazy_committer) { DCHECK(txn_id > 0); // ... } ``` ########## cloud/src/meta-service/txn_lazy_committer.cpp: ########## @@ -0,0 +1,209 @@ +// 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. + +#include "txn_lazy_committer.h" + +#include "common/logging.h" +#include "common/util.h" +#include "meta-service/keys.h" +#include "meta-service/meta_service_helper.h" + +namespace doris::cloud { + +extern void scan_tmp_rowset( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t* db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>* tmp_rowsets_meta); + +extern void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids); + +extern void make_committed_txn_visible(const std::string& instance_id, int64_t db_id, + int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg); + +TxnLazyCommitTask::TxnLazyCommitTask(const std::string& instance_id, + int64_t txn_id, + std::shared_ptr<TxnKv> txn_kv, + TxnLazyCommitter* txn_lazy_committer) { + DCHECK(txn_id > 0); + DCHECK(txn_lazy_committer != nullptr); + txn_id_ = txn_id; + txn_lazy_committer_ = txn_lazy_committer; + task_func_ = [=, this]() { + MetaServiceCode code; + std::stringstream ss; + std::string msg; + + do { + code = MetaServiceCode::OK; + int64_t db_id; + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>> tmp_rowset_metas; + scan_tmp_rowset(instance_id, txn_id, txn_kv, code, msg, &db_id, &tmp_rowset_metas); + if (code != MetaServiceCode::OK) { + LOG(WARNING) << "scan_tmp_rowset failed, txn_id=" << txn_id << " code=" << code; + break; + } + + VLOG_DEBUG << "txn_id=" << txn_id + << " tmp_rowset_metas.size()=" << tmp_rowset_metas.size(); + if (tmp_rowset_metas.size() == 0) { + LOG(INFO) << "empty tmp_rowset_metas, txn_id=" << txn_id; + break; + } + + // <partition_id, tmp_rowsets> + std::unordered_map<int64_t, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>> + partition_to_tmp_rowset_metas; + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowset_metas) { + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].emplace_back(); + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].back().first = + tmp_rowset_key; + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].back().second = + tmp_rowset_pb; + } + + // tablet_id -> TabletIndexPB + std::unordered_map<int64_t, TabletIndexPB> tablet_ids; + for (auto& [partition_id, tmp_rowset_metas] : partition_to_tmp_rowset_metas) { + for (size_t i = 0; i < tmp_rowset_metas.size(); + i += config::txn_lazy_max_rowsets_per_batch) { + size_t end = + (i + config::txn_lazy_max_rowsets_per_batch) > tmp_rowset_metas.size() + ? tmp_rowset_metas.size() + : i + config::txn_lazy_max_rowsets_per_batch; + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>> + sub_partition_tmp_rowset_metas(tmp_rowset_metas.begin() + i, + tmp_rowset_metas.begin() + end); + convert_tmp_rowsets(instance_id, txn_id, txn_kv, code, msg, db_id, + sub_partition_tmp_rowset_metas, tablet_ids); + if (code != MetaServiceCode::OK) break; + } + if (code != MetaServiceCode::OK) break; + + DCHECK(tmp_rowset_metas.size() > 0); + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::CREATE>(err); + ss << "failed to create txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + break; + } + + int64_t table_id = -1; + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowset_metas) { + if (table_id <= 0) { + table_id = tablet_ids[tmp_rowset_pb.tablet_id()].table_id(); + } + txn->remove(tmp_rowset_key); + } + + DCHECK(table_id > 0); + DCHECK(partition_id > 0); + + std::string ver_val; + std::string ver_key = + partition_version_key({instance_id, db_id, table_id, partition_id}); + err = txn->get(ver_key, &ver_val); + if (TxnErrorCode::TXN_OK != err) { + code = err == TxnErrorCode::TXN_KEY_NOT_FOUND + ? MetaServiceCode::TXN_ID_NOT_FOUND + : cast_as<ErrCategory::READ>(err); + ss << "failed to get partiton version, txn_id=" << txn_id + << " key=" << hex(ver_key) << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + break; + } + VersionPB version_pb; + if (!version_pb.ParseFromString(ver_val)) { + code = MetaServiceCode::PROTOBUF_PARSE_ERR; + ss << "failed to parse version pb txn_id=" << txn_id << " key=" << hex(ver_key); + msg = ss.str(); + break; + } + + if (version_pb.has_txn_id() && version_pb.txn_id() == txn_id) { + version_pb.clear_txn_id(); + ver_val.clear(); + if (!version_pb.SerializeToString(&ver_val)) { + code = MetaServiceCode::PROTOBUF_SERIALIZE_ERR; + ss << "failed to serialize version_pb when saving, txn_id=" << txn_id; + msg = ss.str(); + return; + } + txn->put(ver_key, ver_val); + } + + err = txn->commit(); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::COMMIT>(err); + ss << "failed to commit kv txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + break; + } + } + make_committed_txn_visible(instance_id, db_id, txn_id, txn_kv, code, msg); + } while (false); + + std::unique_lock<std::mutex> lock(mutex_); + this->code_ = code; + this->msg_ = std::move(msg); + this->finished_ = true; + this->cond_.notify_all(); + }; +} + +std::pair<MetaServiceCode, std::string> TxnLazyCommitTask::wait() { + std::unique_lock<std::mutex> lock(mutex_); + cond_.wait(lock, [this]() { return this->finished_ == true; }); + txn_lazy_committer_->remove(txn_id_); + return std::make_pair(this->code_, this->msg_); +} + +TxnLazyCommitter::TxnLazyCommitter(std::shared_ptr<TxnKv> txn_kv) { + txn_kv_ = txn_kv; + worker_pool_ = std::make_unique<SimpleThreadPool>(config::txn_lazy_commit_worker_num); + worker_pool_->start(); +} + +std::shared_ptr<TxnLazyCommitTask> TxnLazyCommitter::submit(const std::string& instance_id, + int64_t txn_id) { + std::unique_lock<std::mutex> lock(mutex_); + auto iter = running_tasks_.find(txn_id); + if (iter != running_tasks_.end()) { + return iter->second; + } + + auto task = std::make_shared<TxnLazyCommitTask>(instance_id, txn_id, txn_kv_, this); + running_tasks_.emplace(txn_id, task); + worker_pool_->submit(task->task_func_); + return task; Review Comment: The use of locks here can be further optimized. For example, it can be modified as follows: ```suggestion std::shared_ptr<TxnLazyCommitTask> task; { std::unique_lock<std::mutex> lock(mutex_); auto iter = running_tasks_.find(txn_id); if (iter != running_tasks_.end()) { return iter->second; } task = std::make_shared<TxnLazyCommitTask>(instance_id, txn_id, txn_kv_, this); running_tasks_.emplace(txn_id, task); } worker_pool_->submit(task->task_func_); return task; ``` ########## cloud/src/meta-service/txn_lazy_committer.cpp: ########## @@ -0,0 +1,209 @@ +// 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. + +#include "txn_lazy_committer.h" + +#include "common/logging.h" +#include "common/util.h" +#include "meta-service/keys.h" +#include "meta-service/meta_service_helper.h" + +namespace doris::cloud { + +extern void scan_tmp_rowset( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t* db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>* tmp_rowsets_meta); + +extern void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids); + +extern void make_committed_txn_visible(const std::string& instance_id, int64_t db_id, + int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg); + +TxnLazyCommitTask::TxnLazyCommitTask(const std::string& instance_id, + int64_t txn_id, + std::shared_ptr<TxnKv> txn_kv, + TxnLazyCommitter* txn_lazy_committer) { + DCHECK(txn_id > 0); + DCHECK(txn_lazy_committer != nullptr); + txn_id_ = txn_id; + txn_lazy_committer_ = txn_lazy_committer; + task_func_ = [=, this]() { + MetaServiceCode code; + std::stringstream ss; + std::string msg; + + do { + code = MetaServiceCode::OK; + int64_t db_id; + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>> tmp_rowset_metas; + scan_tmp_rowset(instance_id, txn_id, txn_kv, code, msg, &db_id, &tmp_rowset_metas); + if (code != MetaServiceCode::OK) { + LOG(WARNING) << "scan_tmp_rowset failed, txn_id=" << txn_id << " code=" << code; + break; + } + + VLOG_DEBUG << "txn_id=" << txn_id + << " tmp_rowset_metas.size()=" << tmp_rowset_metas.size(); + if (tmp_rowset_metas.size() == 0) { + LOG(INFO) << "empty tmp_rowset_metas, txn_id=" << txn_id; + break; + } + + // <partition_id, tmp_rowsets> + std::unordered_map<int64_t, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>> + partition_to_tmp_rowset_metas; + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowset_metas) { + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].emplace_back(); + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].back().first = + tmp_rowset_key; + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].back().second = + tmp_rowset_pb; Review Comment: ```suggestion for (auto& rowset_pair : tmp_rowset_metas) { int64_t partition_id = rowset_pair.second.partition_id(); partition_to_tmp_rowset_metas[partition_id].push_back(std::move(rowset_pair)); ``` ########## cloud/src/meta-service/meta_service_txn.cpp: ########## @@ -788,6 +788,164 @@ void update_tablet_stats(const StatsTabletKeyInfo& info, const TabletStats& stat txn->put(key, val); } } + +void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids) { + std::stringstream ss; + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::CREATE>(err); + ss << "failed to create txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + // partition_id -> VersionPB + std::unordered_map<int64_t, VersionPB> partition_versions; + // tablet_id -> stats + std::unordered_map<int64_t, TabletStats> tablet_stats; + + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowsets_meta) { + std::string tmp_rowst_data; + err = txn->get(tmp_rowset_key, &tmp_rowst_data); + if (TxnErrorCode::TXN_KEY_NOT_FOUND == err) { + // the tmp rowset has been converted + continue; + } + + if (TxnErrorCode::TXN_OK != err) { + code = cast_as<ErrCategory::READ>(err); + ss << "failed to get tmp_rowset_key, txn_id=" << txn_id + << " key=" << hex(tmp_rowset_key) << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + if (tablet_ids.find(tmp_rowset_pb.tablet_id()) == tablet_ids.end()) { + std::string tablet_idx_key; + meta_tablet_idx_key({instance_id, tmp_rowset_pb.tablet_id()}, &tablet_idx_key); + std::string tablet_idx_val; + err = txn->get(tablet_idx_key, &tablet_idx_val); + if (TxnErrorCode::TXN_OK != err) { + code = err == TxnErrorCode::TXN_KEY_NOT_FOUND ? MetaServiceCode::TXN_ID_NOT_FOUND + : cast_as<ErrCategory::READ>(err); + ss << "failed to get tablet idx, txn_id=" << txn_id + << " key=" << hex(tablet_idx_key) << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + TabletIndexPB tablet_idx_pb; + if (!tablet_idx_pb.ParseFromString(tablet_idx_val)) { + code = MetaServiceCode::PROTOBUF_PARSE_ERR; + ss << "failed to parse tablet idx pb txn_id=" << txn_id + << " key=" << hex(tablet_idx_key); + msg = ss.str(); + return; + } + tablet_ids.emplace(tmp_rowset_pb.tablet_id(), tablet_idx_pb); + } + const TabletIndexPB& tablet_idx_pb = tablet_ids[tmp_rowset_pb.tablet_id()]; + + if (partition_versions.find(tmp_rowset_pb.partition_id()) == partition_versions.end()) { + std::string ver_val; + std::string ver_key = partition_version_key( + {instance_id, db_id, tablet_idx_pb.table_id(), tmp_rowset_pb.partition_id()}); + err = txn->get(ver_key, &ver_val); + if (TxnErrorCode::TXN_OK != err) { + code = err == TxnErrorCode::TXN_KEY_NOT_FOUND ? MetaServiceCode::TXN_ID_NOT_FOUND + : cast_as<ErrCategory::READ>(err); + ss << "failed to get partiton version, txn_id=" << txn_id << " key=" << hex(ver_key) + << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + VersionPB version_pb; + if (!version_pb.ParseFromString(ver_val)) { + code = MetaServiceCode::PROTOBUF_PARSE_ERR; + ss << "failed to parse version pb txn_id=" << txn_id << " key=" << hex(ver_key); + msg = ss.str(); + return; + } + LOG(INFO) << "txn_id=" << txn_id << " key=" << hex(ver_key) + << " version_pb:" << version_pb.ShortDebugString(); + DCHECK(version_pb.has_txn_id()); + DCHECK(version_pb.txn_id() == txn_id); + partition_versions.emplace(tmp_rowset_pb.partition_id(), version_pb); + } + + const VersionPB& version_pb = partition_versions[tmp_rowset_pb.partition_id()]; + + std::string rowset_key = + meta_rowset_key({instance_id, tmp_rowset_pb.tablet_id(), version_pb.version()}); + std::string rowset_val; + err = txn->get(rowset_key, &rowset_val); + if (TxnErrorCode::TXN_OK == err) { + continue; + } + + if (err != TxnErrorCode::TXN_KEY_NOT_FOUND) { + code = cast_as<ErrCategory::READ>(err); + ss << "failed to get rowset_key, txn_id=" << txn_id << " key=" << hex(rowset_key) + << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + DCHECK(err == TxnErrorCode::TXN_KEY_NOT_FOUND); + + tmp_rowset_pb.set_start_version(version_pb.version()); + tmp_rowset_pb.set_end_version(version_pb.version()); + + rowset_val.clear(); + if (!tmp_rowset_pb.SerializeToString(&rowset_val)) { + code = MetaServiceCode::PROTOBUF_SERIALIZE_ERR; + ss << "failed to serialize rowset_meta, txn_id=" << txn_id + << " key=" << hex(rowset_key); + msg = ss.str(); + return; + } + + txn->put(rowset_key, rowset_val); + LOG(INFO) << "xxx put rowset_key=" << hex(rowset_key) << " txn_id=" << txn_id + << " rowset_size=" << rowset_key.size() + rowset_val.size(); + + // Accumulate affected rows + auto& stats = tablet_stats[tmp_rowset_pb.tablet_id()]; + stats.data_size += tmp_rowset_pb.data_disk_size(); + stats.num_rows += tmp_rowset_pb.num_rows(); + ++stats.num_rowsets; + stats.num_segs += tmp_rowset_pb.num_segments(); + } + + DCHECK(partition_versions.size() == 1); + + for (auto& [tablet_id, stats] : tablet_stats) { + DCHECK(tablet_ids.count(tablet_id)); + auto& tablet_idx = tablet_ids[tablet_id]; + StatsTabletKeyInfo info {instance_id, tablet_idx.table_id(), tablet_idx.index_id(), + tablet_idx.partition_id(), tablet_id}; + update_tablet_stats(info, stats, txn, code, msg); + if (code != MetaServiceCode::OK) return; + } + + err = txn->commit(); + if (err != TxnErrorCode::TXN_OK) { Review Comment: There may be conflicts caused by concurrent commits. According to our design expectations, TXN_CONFLICT also needs to be handled. One simple solution is to consider the data already converted if TXN_CONFLICT occurs (this requires that each task uses the same split batch method). Another solution is to retry once if TXN_CONFLICT occurs. It's essential to manage the competition between transactions properly here. ########## cloud/src/meta-service/meta_service_txn.cpp: ########## @@ -913,320 +1458,420 @@ void commit_txn_immediately( LOG(WARNING) << msg << " err=" << err << " txn_id=" << txn_id; return; } - if (!tablet_ids[tablet_id].ParseFromString(tablet_idx_values[i].value())) [[unlikely]] { + if (!(*tablet_ids)[tablet_id].ParseFromString(tablet_idx_values[i].value())) [[unlikely]] { code = MetaServiceCode::PROTOBUF_PARSE_ERR; ss << "malformed tablet index value tablet_id=" << tablet_id << " txn_id=" << txn_id; msg = ss.str(); LOG(WARNING) << msg; return; } - table_id_tablet_ids[tablet_ids[tablet_id].table_id()].push_back(tablet_id); + (*table_id_tablet_ids)[(*tablet_ids)[tablet_id].table_id()].push_back(tablet_id); VLOG_DEBUG << "tablet_id:" << tablet_id - << " value:" << tablet_ids[tablet_id].ShortDebugString(); + << " value:" << (*tablet_ids)[tablet_id].ShortDebugString(); } tablet_idx_keys.clear(); tablet_idx_values.clear(); +} - // {table/partition} -> version - std::unordered_map<std::string, uint64_t> new_versions; - std::vector<std::string> version_keys; - for (auto& [_, i] : tmp_rowsets_meta) { - int64_t tablet_id = i.tablet_id(); - int64_t table_id = tablet_ids[tablet_id].table_id(); - int64_t partition_id = i.partition_id(); - std::string ver_key = partition_version_key({instance_id, db_id, table_id, partition_id}); - if (new_versions.count(ver_key) == 0) { - new_versions.insert({ver_key, 0}); - version_keys.push_back(std::move(ver_key)); - } +void make_committed_txn_visible(const std::string& instance_id, int64_t db_id, int64_t txn_id, + std::shared_ptr<TxnKv> txn_kv, MetaServiceCode& code, + std::string& msg) { + // 1. visible txn info + // 2. remove running key and put recycle txn key + + std::stringstream ss; + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::CREATE>(err); + ss << "failed to create txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; } - std::vector<std::optional<std::string>> version_values; - err = txn->batch_get(&version_values, version_keys); + + std::string info_val; + const std::string info_key = txn_info_key({instance_id, db_id, txn_id}); + err = txn->get(info_key, &info_val); if (err != TxnErrorCode::TXN_OK) { - code = cast_as<ErrCategory::READ>(err); - ss << "failed to get partition versions, err=" << err; + code = err == TxnErrorCode::TXN_KEY_NOT_FOUND ? MetaServiceCode::TXN_ID_NOT_FOUND + : cast_as<ErrCategory::READ>(err); + ss << "failed to get txn_info, db_id=" << db_id << " txn_id=" << txn_id << " err=" << err; msg = ss.str(); - LOG(WARNING) << msg << " txn_id=" << txn_id; + LOG(WARNING) << msg; return; } - size_t total_versions = version_keys.size(); - for (size_t i = 0; i < total_versions; i++) { - int64_t version; - if (version_values[i].has_value()) { - VersionPB version_pb; - if (!version_pb.ParseFromString(version_values[i].value())) { - code = MetaServiceCode::PROTOBUF_PARSE_ERR; - ss << "failed to parse version pb txn_id=" << txn_id - << " key=" << hex(version_keys[i]); - msg = ss.str(); - return; - } - version = version_pb.version(); - } else { - version = 1; + + TxnInfoPB txn_info; + if (!txn_info.ParseFromString(info_val)) { + code = MetaServiceCode::PROTOBUF_PARSE_ERR; + ss << "failed to parse txn_info, txn_id=" << txn_id; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + VLOG_DEBUG << "txn_info:" << txn_info.ShortDebugString(); + DCHECK((txn_info.status() == TxnStatusPB::TXN_STATUS_COMMITTED) || + (txn_info.status() == TxnStatusPB::TXN_STATUS_VISIBLE)); + + if (txn_info.status() == TxnStatusPB::TXN_STATUS_COMMITTED) { + txn_info.set_status(TxnStatusPB::TXN_STATUS_VISIBLE); + txn->put(info_key, info_val); + LOG(INFO) << "xxx put info_key=" << hex(info_key) << " txn_id=" << txn_id; + + const std::string running_key = txn_running_key({instance_id, db_id, txn_id}); + LOG(INFO) << "xxx remove running_key=" << hex(running_key) << " txn_id=" << txn_id; + txn->remove(running_key); + + std::string recycle_val; + std::string recycle_key = recycle_txn_key({instance_id, db_id, txn_id}); + RecycleTxnPB recycle_pb; + auto now_time = system_clock::now(); + uint64_t visible_time = duration_cast<milliseconds>(now_time.time_since_epoch()).count(); + recycle_pb.set_creation_time(visible_time); + recycle_pb.set_label(txn_info.label()); + + if (!recycle_pb.SerializeToString(&recycle_val)) { + code = MetaServiceCode::PROTOBUF_SERIALIZE_ERR; + ss << "failed to serialize recycle_pb, txn_id=" << txn_id; + msg = ss.str(); + return; } - new_versions[version_keys[i]] = version + 1; + txn->put(recycle_key, recycle_val); } - version_keys.clear(); - version_values.clear(); - std::vector<std::pair<std::string, std::string>> rowsets; - std::unordered_map<int64_t, TabletStats> tablet_stats; // tablet_id -> stats - rowsets.reserve(tmp_rowsets_meta.size()); - for (auto& [_, i] : tmp_rowsets_meta) { - int64_t tablet_id = i.tablet_id(); - int64_t table_id = tablet_ids[tablet_id].table_id(); - int64_t partition_id = i.partition_id(); - std::string ver_key = partition_version_key({instance_id, db_id, table_id, partition_id}); - if (new_versions[ver_key] == 0) [[unlikely]] { - // it is impossible. - code = MetaServiceCode::UNDEFINED_ERR; - ss << "failed to get partition version key, the target version not exists in " - "new_versions." - << " txn_id=" << txn_id; + err = txn->commit(); + if (err != TxnErrorCode::TXN_OK) { Review Comment: Handle `TXN_CONFLICT`. ########## cloud/src/meta-service/txn_lazy_committer.h: ########## @@ -0,0 +1,65 @@ +// 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. + +#pragma once + +#include <gen_cpp/cloud.pb.h> + +#include <atomic> + +#include "common/simple_thread_pool.h" +#include "meta-service/txn_kv.h" + +namespace doris::cloud { + +class TxnLazyCommitter; + +class TxnLazyCommitTask { +public: + TxnLazyCommitTask(const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + TxnLazyCommitter* txn_lazy_committer_); + + std::pair<MetaServiceCode, std::string> wait(); Review Comment: How about adding a comment? ########## cloud/src/meta-service/meta_service_txn.cpp: ########## @@ -804,91 +962,483 @@ void update_tablet_stats(const StatsTabletKeyInfo& info, const TabletStats& stat */ void commit_txn_immediately( const CommitTxnRequest* request, CommitTxnResponse* response, - std::shared_ptr<TxnKv>& txn_kv, MetaServiceCode& code, std::string& msg, - const std::string& instance_id, int64_t db_id, + std::shared_ptr<TxnKv>& txn_kv, std::shared_ptr<TxnLazyCommitter>& txn_lazy_committer, Review Comment: `std::shared_ptr` inherently carries ownership semantics. If `commit_txn_immediately` does not need to transfer the `shared_ptr` (eg, inc reference count), then using a reference would be more appropriate (making the semantics more accurate). For example, `shared_ptr<TxnKv>&` can be changed to `TxnKv&`. ########## cloud/src/meta-service/txn_lazy_committer.h: ########## @@ -0,0 +1,65 @@ +// 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. + +#pragma once + +#include <gen_cpp/cloud.pb.h> + +#include <atomic> + +#include "common/simple_thread_pool.h" +#include "meta-service/txn_kv.h" + +namespace doris::cloud { + +class TxnLazyCommitter; + +class TxnLazyCommitTask { +public: + TxnLazyCommitTask(const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + TxnLazyCommitter* txn_lazy_committer_); + + std::pair<MetaServiceCode, std::string> wait(); + +private: + friend class TxnLazyCommitter; + int64_t txn_id_; Review Comment: ```suggestion friend class TxnLazyCommitter; int64_t txn_id_; ``` ########## cloud/src/meta-service/txn_lazy_committer.h: ########## @@ -0,0 +1,65 @@ +// 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. + +#pragma once + +#include <gen_cpp/cloud.pb.h> + +#include <atomic> + +#include "common/simple_thread_pool.h" +#include "meta-service/txn_kv.h" + +namespace doris::cloud { + +class TxnLazyCommitter; + +class TxnLazyCommitTask { Review Comment: `TxnLazyCommitTask` doesn't need to be exposed to external users, so it can be defined in the cpp file to speed up compilation. This will introduce the issue of `TxnLazyCommitter` not finding the `TxnLazyCommitTask` definition during destruction, which can be resolved by adding `TxnLazyCommitter::~TxnLazyCommitter() = default;` in the cpp file. ########## cloud/src/meta-service/meta_service_txn.cpp: ########## @@ -788,6 +788,164 @@ void update_tablet_stats(const StatsTabletKeyInfo& info, const TabletStats& stat txn->put(key, val); } } + +void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids) { + std::stringstream ss; + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::CREATE>(err); + ss << "failed to create txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + // partition_id -> VersionPB + std::unordered_map<int64_t, VersionPB> partition_versions; + // tablet_id -> stats + std::unordered_map<int64_t, TabletStats> tablet_stats; + + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowsets_meta) { + std::string tmp_rowst_data; + err = txn->get(tmp_rowset_key, &tmp_rowst_data); + if (TxnErrorCode::TXN_KEY_NOT_FOUND == err) { + // the tmp rowset has been converted Review Comment: Add a debug log. ########## cloud/src/meta-service/meta_service_txn.cpp: ########## @@ -788,6 +788,164 @@ void update_tablet_stats(const StatsTabletKeyInfo& info, const TabletStats& stat txn->put(key, val); } } + +void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids) { + std::stringstream ss; + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::CREATE>(err); + ss << "failed to create txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + // partition_id -> VersionPB + std::unordered_map<int64_t, VersionPB> partition_versions; + // tablet_id -> stats + std::unordered_map<int64_t, TabletStats> tablet_stats; + + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowsets_meta) { + std::string tmp_rowst_data; + err = txn->get(tmp_rowset_key, &tmp_rowst_data); + if (TxnErrorCode::TXN_KEY_NOT_FOUND == err) { + // the tmp rowset has been converted + continue; + } + + if (TxnErrorCode::TXN_OK != err) { + code = cast_as<ErrCategory::READ>(err); + ss << "failed to get tmp_rowset_key, txn_id=" << txn_id + << " key=" << hex(tmp_rowset_key) << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + if (tablet_ids.find(tmp_rowset_pb.tablet_id()) == tablet_ids.end()) { + std::string tablet_idx_key; + meta_tablet_idx_key({instance_id, tmp_rowset_pb.tablet_id()}, &tablet_idx_key); Review Comment: ```suggestion std::string tablet_idx_key = meta_tablet_idx_key({instance_id, tmp_rowset_pb.tablet_id()}); ``` ########## cloud/src/meta-service/txn_lazy_committer.cpp: ########## @@ -0,0 +1,209 @@ +// 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. + +#include "txn_lazy_committer.h" + +#include "common/logging.h" +#include "common/util.h" +#include "meta-service/keys.h" +#include "meta-service/meta_service_helper.h" + +namespace doris::cloud { + +extern void scan_tmp_rowset( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t* db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>* tmp_rowsets_meta); + +extern void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids); + +extern void make_committed_txn_visible(const std::string& instance_id, int64_t db_id, + int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg); + +TxnLazyCommitTask::TxnLazyCommitTask(const std::string& instance_id, + int64_t txn_id, + std::shared_ptr<TxnKv> txn_kv, + TxnLazyCommitter* txn_lazy_committer) { + DCHECK(txn_id > 0); + DCHECK(txn_lazy_committer != nullptr); + txn_id_ = txn_id; + txn_lazy_committer_ = txn_lazy_committer; + task_func_ = [=, this]() { + MetaServiceCode code; + std::stringstream ss; + std::string msg; + + do { + code = MetaServiceCode::OK; + int64_t db_id; + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>> tmp_rowset_metas; + scan_tmp_rowset(instance_id, txn_id, txn_kv, code, msg, &db_id, &tmp_rowset_metas); + if (code != MetaServiceCode::OK) { + LOG(WARNING) << "scan_tmp_rowset failed, txn_id=" << txn_id << " code=" << code; + break; + } + + VLOG_DEBUG << "txn_id=" << txn_id + << " tmp_rowset_metas.size()=" << tmp_rowset_metas.size(); + if (tmp_rowset_metas.size() == 0) { + LOG(INFO) << "empty tmp_rowset_metas, txn_id=" << txn_id; + break; + } + + // <partition_id, tmp_rowsets> + std::unordered_map<int64_t, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>> + partition_to_tmp_rowset_metas; + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowset_metas) { + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].emplace_back(); + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].back().first = + tmp_rowset_key; + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].back().second = + tmp_rowset_pb; + } + + // tablet_id -> TabletIndexPB + std::unordered_map<int64_t, TabletIndexPB> tablet_ids; + for (auto& [partition_id, tmp_rowset_metas] : partition_to_tmp_rowset_metas) { + for (size_t i = 0; i < tmp_rowset_metas.size(); + i += config::txn_lazy_max_rowsets_per_batch) { + size_t end = + (i + config::txn_lazy_max_rowsets_per_batch) > tmp_rowset_metas.size() + ? tmp_rowset_metas.size() + : i + config::txn_lazy_max_rowsets_per_batch; + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>> + sub_partition_tmp_rowset_metas(tmp_rowset_metas.begin() + i, + tmp_rowset_metas.begin() + end); + convert_tmp_rowsets(instance_id, txn_id, txn_kv, code, msg, db_id, + sub_partition_tmp_rowset_metas, tablet_ids); + if (code != MetaServiceCode::OK) break; + } + if (code != MetaServiceCode::OK) break; + + DCHECK(tmp_rowset_metas.size() > 0); + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::CREATE>(err); + ss << "failed to create txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + break; + } + + int64_t table_id = -1; + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowset_metas) { + if (table_id <= 0) { + table_id = tablet_ids[tmp_rowset_pb.tablet_id()].table_id(); + } + txn->remove(tmp_rowset_key); + } + + DCHECK(table_id > 0); + DCHECK(partition_id > 0); + + std::string ver_val; + std::string ver_key = + partition_version_key({instance_id, db_id, table_id, partition_id}); + err = txn->get(ver_key, &ver_val); + if (TxnErrorCode::TXN_OK != err) { + code = err == TxnErrorCode::TXN_KEY_NOT_FOUND + ? MetaServiceCode::TXN_ID_NOT_FOUND + : cast_as<ErrCategory::READ>(err); + ss << "failed to get partiton version, txn_id=" << txn_id + << " key=" << hex(ver_key) << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + break; + } + VersionPB version_pb; + if (!version_pb.ParseFromString(ver_val)) { + code = MetaServiceCode::PROTOBUF_PARSE_ERR; + ss << "failed to parse version pb txn_id=" << txn_id << " key=" << hex(ver_key); + msg = ss.str(); + break; + } + + if (version_pb.has_txn_id() && version_pb.txn_id() == txn_id) { + version_pb.clear_txn_id(); + ver_val.clear(); + if (!version_pb.SerializeToString(&ver_val)) { + code = MetaServiceCode::PROTOBUF_SERIALIZE_ERR; + ss << "failed to serialize version_pb when saving, txn_id=" << txn_id; + msg = ss.str(); + return; + } + txn->put(ver_key, ver_val); + } + + err = txn->commit(); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::COMMIT>(err); + ss << "failed to commit kv txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + break; + } + } + make_committed_txn_visible(instance_id, db_id, txn_id, txn_kv, code, msg); + } while (false); + + std::unique_lock<std::mutex> lock(mutex_); + this->code_ = code; + this->msg_ = std::move(msg); + this->finished_ = true; + this->cond_.notify_all(); + }; +} + +std::pair<MetaServiceCode, std::string> TxnLazyCommitTask::wait() { + std::unique_lock<std::mutex> lock(mutex_); + cond_.wait(lock, [this]() { return this->finished_ == true; }); + txn_lazy_committer_->remove(txn_id_); + return std::make_pair(this->code_, this->msg_); +} + +TxnLazyCommitter::TxnLazyCommitter(std::shared_ptr<TxnKv> txn_kv) { Review Comment: Initialize in the member initializer list too ########## cloud/src/meta-service/txn_lazy_committer.cpp: ########## @@ -0,0 +1,209 @@ +// 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. + +#include "txn_lazy_committer.h" + +#include "common/logging.h" +#include "common/util.h" +#include "meta-service/keys.h" +#include "meta-service/meta_service_helper.h" + +namespace doris::cloud { + +extern void scan_tmp_rowset( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t* db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>* tmp_rowsets_meta); + +extern void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids); + +extern void make_committed_txn_visible(const std::string& instance_id, int64_t db_id, + int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg); + +TxnLazyCommitTask::TxnLazyCommitTask(const std::string& instance_id, + int64_t txn_id, + std::shared_ptr<TxnKv> txn_kv, + TxnLazyCommitter* txn_lazy_committer) { + DCHECK(txn_id > 0); + DCHECK(txn_lazy_committer != nullptr); + txn_id_ = txn_id; + txn_lazy_committer_ = txn_lazy_committer; + task_func_ = [=, this]() { Review Comment: Declare this as a member function of `TxnLazyCommitTask`, eg: `TxnLazyCommiter::commit` then change `TxnLazyCommitter::submit` to: ```c++ auto task = std::make_shared<TxnLazyCommitTask>(instance_id, txn_id, txn_kv_, this); running_tasks_.emplace(txn_id, task); worker_pool_->submit([task]() { task->commit(); }); ``` ########## cloud/src/meta-service/meta_service_txn.cpp: ########## @@ -788,6 +788,164 @@ void update_tablet_stats(const StatsTabletKeyInfo& info, const TabletStats& stat txn->put(key, val); } } + +void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids) { + std::stringstream ss; + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::CREATE>(err); + ss << "failed to create txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + // partition_id -> VersionPB + std::unordered_map<int64_t, VersionPB> partition_versions; + // tablet_id -> stats + std::unordered_map<int64_t, TabletStats> tablet_stats; + + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowsets_meta) { + std::string tmp_rowst_data; + err = txn->get(tmp_rowset_key, &tmp_rowst_data); + if (TxnErrorCode::TXN_KEY_NOT_FOUND == err) { + // the tmp rowset has been converted + continue; + } + + if (TxnErrorCode::TXN_OK != err) { + code = cast_as<ErrCategory::READ>(err); + ss << "failed to get tmp_rowset_key, txn_id=" << txn_id + << " key=" << hex(tmp_rowset_key) << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + if (tablet_ids.find(tmp_rowset_pb.tablet_id()) == tablet_ids.end()) { Review Comment: ```suggestion if (!tablet_ids.contains(tmp_rowset_pb.tablet_id())) { ``` ########## cloud/src/meta-service/meta_service_txn.cpp: ########## @@ -1810,8 +2455,17 @@ void MetaServiceImpl::commit_txn(::google::protobuf::RpcController* controller, LOG(WARNING) << "scan_tmp_rowset failed, txn_id=" << txn_id << " code=" << code; return; } - commit_txn_immediately(request, response, txn_kv_, code, msg, instance_id, db_id, - tmp_rowsets_meta); + VLOG_DEBUG << "txn_id=" << txn_id << " tmp_rowset size=" << tmp_rowsets_meta.size(); + if (config::enable_txn_lazy_commit && + tmp_rowsets_meta.size() >= config::txn_lazy_commit_rowsets_thresold) { + commit_txn_eventually(request, response, txn_kv_, txn_lazy_committer_, code, msg, + instance_id, db_id, tmp_rowsets_meta); + return; + } + + commit_txn_immediately(request, response, txn_kv_, txn_lazy_committer_, code, msg, instance_id, + db_id, tmp_rowsets_meta); + return; Review Comment: Redundant `return`. ########## cloud/src/meta-service/txn_lazy_committer.cpp: ########## @@ -0,0 +1,209 @@ +// 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. + +#include "txn_lazy_committer.h" + +#include "common/logging.h" +#include "common/util.h" +#include "meta-service/keys.h" +#include "meta-service/meta_service_helper.h" + +namespace doris::cloud { + +extern void scan_tmp_rowset( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t* db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>* tmp_rowsets_meta); + +extern void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids); + +extern void make_committed_txn_visible(const std::string& instance_id, int64_t db_id, + int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg); + +TxnLazyCommitTask::TxnLazyCommitTask(const std::string& instance_id, + int64_t txn_id, + std::shared_ptr<TxnKv> txn_kv, + TxnLazyCommitter* txn_lazy_committer) { + DCHECK(txn_id > 0); + DCHECK(txn_lazy_committer != nullptr); + txn_id_ = txn_id; + txn_lazy_committer_ = txn_lazy_committer; + task_func_ = [=, this]() { + MetaServiceCode code; + std::stringstream ss; + std::string msg; + + do { + code = MetaServiceCode::OK; + int64_t db_id; + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>> tmp_rowset_metas; + scan_tmp_rowset(instance_id, txn_id, txn_kv, code, msg, &db_id, &tmp_rowset_metas); + if (code != MetaServiceCode::OK) { + LOG(WARNING) << "scan_tmp_rowset failed, txn_id=" << txn_id << " code=" << code; + break; + } + + VLOG_DEBUG << "txn_id=" << txn_id + << " tmp_rowset_metas.size()=" << tmp_rowset_metas.size(); + if (tmp_rowset_metas.size() == 0) { + LOG(INFO) << "empty tmp_rowset_metas, txn_id=" << txn_id; + break; + } + + // <partition_id, tmp_rowsets> + std::unordered_map<int64_t, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>> + partition_to_tmp_rowset_metas; + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowset_metas) { + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].emplace_back(); + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].back().first = + tmp_rowset_key; + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].back().second = + tmp_rowset_pb; + } + + // tablet_id -> TabletIndexPB + std::unordered_map<int64_t, TabletIndexPB> tablet_ids; + for (auto& [partition_id, tmp_rowset_metas] : partition_to_tmp_rowset_metas) { + for (size_t i = 0; i < tmp_rowset_metas.size(); + i += config::txn_lazy_max_rowsets_per_batch) { + size_t end = + (i + config::txn_lazy_max_rowsets_per_batch) > tmp_rowset_metas.size() + ? tmp_rowset_metas.size() + : i + config::txn_lazy_max_rowsets_per_batch; + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>> + sub_partition_tmp_rowset_metas(tmp_rowset_metas.begin() + i, + tmp_rowset_metas.begin() + end); + convert_tmp_rowsets(instance_id, txn_id, txn_kv, code, msg, db_id, + sub_partition_tmp_rowset_metas, tablet_ids); + if (code != MetaServiceCode::OK) break; + } + if (code != MetaServiceCode::OK) break; + + DCHECK(tmp_rowset_metas.size() > 0); + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::CREATE>(err); + ss << "failed to create txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + break; + } + + int64_t table_id = -1; + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowset_metas) { + if (table_id <= 0) { + table_id = tablet_ids[tmp_rowset_pb.tablet_id()].table_id(); + } + txn->remove(tmp_rowset_key); + } + + DCHECK(table_id > 0); + DCHECK(partition_id > 0); + + std::string ver_val; + std::string ver_key = + partition_version_key({instance_id, db_id, table_id, partition_id}); + err = txn->get(ver_key, &ver_val); + if (TxnErrorCode::TXN_OK != err) { + code = err == TxnErrorCode::TXN_KEY_NOT_FOUND + ? MetaServiceCode::TXN_ID_NOT_FOUND + : cast_as<ErrCategory::READ>(err); + ss << "failed to get partiton version, txn_id=" << txn_id + << " key=" << hex(ver_key) << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + break; + } + VersionPB version_pb; + if (!version_pb.ParseFromString(ver_val)) { + code = MetaServiceCode::PROTOBUF_PARSE_ERR; + ss << "failed to parse version pb txn_id=" << txn_id << " key=" << hex(ver_key); + msg = ss.str(); + break; + } + + if (version_pb.has_txn_id() && version_pb.txn_id() == txn_id) { + version_pb.clear_txn_id(); + ver_val.clear(); + if (!version_pb.SerializeToString(&ver_val)) { + code = MetaServiceCode::PROTOBUF_SERIALIZE_ERR; + ss << "failed to serialize version_pb when saving, txn_id=" << txn_id; + msg = ss.str(); + return; + } + txn->put(ver_key, ver_val); + } + + err = txn->commit(); + if (err != TxnErrorCode::TXN_OK) { Review Comment: Handle `TXN_CONFLICT` too. ########## cloud/src/meta-service/txn_lazy_committer.cpp: ########## @@ -0,0 +1,209 @@ +// 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. + +#include "txn_lazy_committer.h" + +#include "common/logging.h" +#include "common/util.h" +#include "meta-service/keys.h" +#include "meta-service/meta_service_helper.h" + +namespace doris::cloud { + +extern void scan_tmp_rowset( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t* db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>* tmp_rowsets_meta); + +extern void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids); + +extern void make_committed_txn_visible(const std::string& instance_id, int64_t db_id, + int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg); + +TxnLazyCommitTask::TxnLazyCommitTask(const std::string& instance_id, + int64_t txn_id, + std::shared_ptr<TxnKv> txn_kv, + TxnLazyCommitter* txn_lazy_committer) { + DCHECK(txn_id > 0); + DCHECK(txn_lazy_committer != nullptr); + txn_id_ = txn_id; + txn_lazy_committer_ = txn_lazy_committer; + task_func_ = [=, this]() { + MetaServiceCode code; + std::stringstream ss; + std::string msg; + + do { + code = MetaServiceCode::OK; + int64_t db_id; + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>> tmp_rowset_metas; + scan_tmp_rowset(instance_id, txn_id, txn_kv, code, msg, &db_id, &tmp_rowset_metas); + if (code != MetaServiceCode::OK) { + LOG(WARNING) << "scan_tmp_rowset failed, txn_id=" << txn_id << " code=" << code; + break; + } + + VLOG_DEBUG << "txn_id=" << txn_id + << " tmp_rowset_metas.size()=" << tmp_rowset_metas.size(); + if (tmp_rowset_metas.size() == 0) { + LOG(INFO) << "empty tmp_rowset_metas, txn_id=" << txn_id; + break; + } + + // <partition_id, tmp_rowsets> + std::unordered_map<int64_t, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>> + partition_to_tmp_rowset_metas; + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowset_metas) { + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].emplace_back(); + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].back().first = + tmp_rowset_key; + partition_to_tmp_rowset_metas[tmp_rowset_pb.partition_id()].back().second = + tmp_rowset_pb; + } + + // tablet_id -> TabletIndexPB + std::unordered_map<int64_t, TabletIndexPB> tablet_ids; + for (auto& [partition_id, tmp_rowset_metas] : partition_to_tmp_rowset_metas) { + for (size_t i = 0; i < tmp_rowset_metas.size(); + i += config::txn_lazy_max_rowsets_per_batch) { + size_t end = + (i + config::txn_lazy_max_rowsets_per_batch) > tmp_rowset_metas.size() + ? tmp_rowset_metas.size() + : i + config::txn_lazy_max_rowsets_per_batch; + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>> + sub_partition_tmp_rowset_metas(tmp_rowset_metas.begin() + i, + tmp_rowset_metas.begin() + end); + convert_tmp_rowsets(instance_id, txn_id, txn_kv, code, msg, db_id, + sub_partition_tmp_rowset_metas, tablet_ids); + if (code != MetaServiceCode::OK) break; + } + if (code != MetaServiceCode::OK) break; + + DCHECK(tmp_rowset_metas.size() > 0); + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::CREATE>(err); + ss << "failed to create txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + break; + } + + int64_t table_id = -1; + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowset_metas) { + if (table_id <= 0) { + table_id = tablet_ids[tmp_rowset_pb.tablet_id()].table_id(); + } + txn->remove(tmp_rowset_key); + } + + DCHECK(table_id > 0); + DCHECK(partition_id > 0); + + std::string ver_val; + std::string ver_key = + partition_version_key({instance_id, db_id, table_id, partition_id}); + err = txn->get(ver_key, &ver_val); + if (TxnErrorCode::TXN_OK != err) { + code = err == TxnErrorCode::TXN_KEY_NOT_FOUND + ? MetaServiceCode::TXN_ID_NOT_FOUND + : cast_as<ErrCategory::READ>(err); + ss << "failed to get partiton version, txn_id=" << txn_id + << " key=" << hex(ver_key) << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + break; + } + VersionPB version_pb; + if (!version_pb.ParseFromString(ver_val)) { + code = MetaServiceCode::PROTOBUF_PARSE_ERR; + ss << "failed to parse version pb txn_id=" << txn_id << " key=" << hex(ver_key); + msg = ss.str(); + break; + } + + if (version_pb.has_txn_id() && version_pb.txn_id() == txn_id) { + version_pb.clear_txn_id(); + ver_val.clear(); + if (!version_pb.SerializeToString(&ver_val)) { + code = MetaServiceCode::PROTOBUF_SERIALIZE_ERR; + ss << "failed to serialize version_pb when saving, txn_id=" << txn_id; + msg = ss.str(); + return; + } + txn->put(ver_key, ver_val); + } + + err = txn->commit(); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::COMMIT>(err); + ss << "failed to commit kv txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + break; + } + } + make_committed_txn_visible(instance_id, db_id, txn_id, txn_kv, code, msg); + } while (false); + + std::unique_lock<std::mutex> lock(mutex_); + this->code_ = code; + this->msg_ = std::move(msg); + this->finished_ = true; + this->cond_.notify_all(); + }; +} + +std::pair<MetaServiceCode, std::string> TxnLazyCommitTask::wait() { + std::unique_lock<std::mutex> lock(mutex_); + cond_.wait(lock, [this]() { return this->finished_ == true; }); + txn_lazy_committer_->remove(txn_id_); + return std::make_pair(this->code_, this->msg_); Review Comment: `txn_lazy_committer_->remove(txn_id_)` has lock, try: ```suggestion { std::unique_lock<std::mutex> lock(mutex_); cond_.wait(lock, [this]() { return this->finished_ == true; }); } txn_lazy_committer_->remove(txn_id_); // The lock has memory barriers, so access code_, msg_ without lock is safety. return std::make_pair(this->code_, this->msg_); ``` ########## cloud/src/meta-service/meta_service_txn.cpp: ########## @@ -788,6 +788,164 @@ void update_tablet_stats(const StatsTabletKeyInfo& info, const TabletStats& stat txn->put(key, val); } } + +void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids) { + std::stringstream ss; + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::CREATE>(err); + ss << "failed to create txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + // partition_id -> VersionPB + std::unordered_map<int64_t, VersionPB> partition_versions; Review Comment: `partition_versions` is not needed here, since all rowsets belong to the same partition. Read the partition version in here directly. ########## cloud/src/meta-service/meta_service_txn.cpp: ########## @@ -788,6 +788,164 @@ void update_tablet_stats(const StatsTabletKeyInfo& info, const TabletStats& stat txn->put(key, val); } } + +void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids) { + std::stringstream ss; + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::CREATE>(err); + ss << "failed to create txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + // partition_id -> VersionPB + std::unordered_map<int64_t, VersionPB> partition_versions; + // tablet_id -> stats + std::unordered_map<int64_t, TabletStats> tablet_stats; + + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowsets_meta) { + std::string tmp_rowst_data; + err = txn->get(tmp_rowset_key, &tmp_rowst_data); + if (TxnErrorCode::TXN_KEY_NOT_FOUND == err) { + // the tmp rowset has been converted + continue; + } + + if (TxnErrorCode::TXN_OK != err) { + code = cast_as<ErrCategory::READ>(err); + ss << "failed to get tmp_rowset_key, txn_id=" << txn_id + << " key=" << hex(tmp_rowset_key) << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + if (tablet_ids.find(tmp_rowset_pb.tablet_id()) == tablet_ids.end()) { + std::string tablet_idx_key; + meta_tablet_idx_key({instance_id, tmp_rowset_pb.tablet_id()}, &tablet_idx_key); + std::string tablet_idx_val; + err = txn->get(tablet_idx_key, &tablet_idx_val); Review Comment: Would using snapshot semantics be sufficient? ########## cloud/src/meta-service/meta_service_txn.cpp: ########## @@ -788,6 +788,164 @@ void update_tablet_stats(const StatsTabletKeyInfo& info, const TabletStats& stat txn->put(key, val); } } + +void convert_tmp_rowsets( + const std::string& instance_id, int64_t txn_id, std::shared_ptr<TxnKv> txn_kv, + MetaServiceCode& code, std::string& msg, int64_t db_id, + std::vector<std::pair<std::string, doris::RowsetMetaCloudPB>>& tmp_rowsets_meta, + std::unordered_map<int64_t, TabletIndexPB>& tablet_ids) { + std::stringstream ss; + std::unique_ptr<Transaction> txn; + TxnErrorCode err = txn_kv->create_txn(&txn); + if (err != TxnErrorCode::TXN_OK) { + code = cast_as<ErrCategory::CREATE>(err); + ss << "failed to create txn, txn_id=" << txn_id << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + // partition_id -> VersionPB + std::unordered_map<int64_t, VersionPB> partition_versions; + // tablet_id -> stats + std::unordered_map<int64_t, TabletStats> tablet_stats; + + for (auto& [tmp_rowset_key, tmp_rowset_pb] : tmp_rowsets_meta) { + std::string tmp_rowst_data; + err = txn->get(tmp_rowset_key, &tmp_rowst_data); + if (TxnErrorCode::TXN_KEY_NOT_FOUND == err) { + // the tmp rowset has been converted + continue; + } + + if (TxnErrorCode::TXN_OK != err) { + code = cast_as<ErrCategory::READ>(err); + ss << "failed to get tmp_rowset_key, txn_id=" << txn_id + << " key=" << hex(tmp_rowset_key) << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + if (tablet_ids.find(tmp_rowset_pb.tablet_id()) == tablet_ids.end()) { + std::string tablet_idx_key; + meta_tablet_idx_key({instance_id, tmp_rowset_pb.tablet_id()}, &tablet_idx_key); + std::string tablet_idx_val; + err = txn->get(tablet_idx_key, &tablet_idx_val); + if (TxnErrorCode::TXN_OK != err) { + code = err == TxnErrorCode::TXN_KEY_NOT_FOUND ? MetaServiceCode::TXN_ID_NOT_FOUND + : cast_as<ErrCategory::READ>(err); + ss << "failed to get tablet idx, txn_id=" << txn_id + << " key=" << hex(tablet_idx_key) << " err=" << err; + msg = ss.str(); + LOG(WARNING) << msg; + return; + } + + TabletIndexPB tablet_idx_pb; + if (!tablet_idx_pb.ParseFromString(tablet_idx_val)) { + code = MetaServiceCode::PROTOBUF_PARSE_ERR; + ss << "failed to parse tablet idx pb txn_id=" << txn_id + << " key=" << hex(tablet_idx_key); + msg = ss.str(); + return; + } + tablet_ids.emplace(tmp_rowset_pb.tablet_id(), tablet_idx_pb); + } + const TabletIndexPB& tablet_idx_pb = tablet_ids[tmp_rowset_pb.tablet_id()]; + + if (partition_versions.find(tmp_rowset_pb.partition_id()) == partition_versions.end()) { Review Comment: ditto -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org