|
24 | 24 | #include "butil/containers/flat_map.h" |
25 | 25 | #include "butil/status.h" |
26 | 26 | #include "common/constant.h" |
| 27 | +#include "common/context.h" |
27 | 28 | #include "common/logging.h" |
28 | 29 | #include "coordinator/auto_increment_control.h" |
29 | 30 | #include "coordinator/coordinator_control.h" |
@@ -4029,6 +4030,73 @@ void MetaServiceImpl::ImportIdEpochType(google::protobuf::RpcController *control |
4029 | 4030 | } |
4030 | 4031 | } |
4031 | 4032 |
|
| 4033 | +void DoCreateOrUpdateAutoIncrements(google::protobuf::RpcController *controller, |
| 4034 | + const pb::meta::CreateOrUpdateAutoIncrementsRequest *request, |
| 4035 | + pb::meta::CreateOrUpdateAutoIncrementsResponse *response, TrackClosure *done, |
| 4036 | + std::shared_ptr<AutoIncrementControl> auto_increment_control, |
| 4037 | + std::shared_ptr<Engine> raft_engine) { |
| 4038 | + brpc::ClosureGuard done_guard(done); |
| 4039 | + |
| 4040 | + DINGO_LOG(INFO) << request->ShortDebugString(); |
| 4041 | + |
| 4042 | + pb::coordinator_internal::MetaIncrement meta_increment; |
| 4043 | + |
| 4044 | + for (const auto &table_increment : request->table_increment_group().table_increments()) { |
| 4045 | + if (!auto_increment_control->IsLeader()) { |
| 4046 | + return auto_increment_control->RedirectResponse(response); |
| 4047 | + } |
| 4048 | + auto table_id = table_increment.table_id(); |
| 4049 | + auto start_id = table_increment.start_id(); |
| 4050 | + auto ret = auto_increment_control->CreateOrUpdateAutoIncrement(table_id, start_id, meta_increment); |
| 4051 | + if (!ret.ok()) { |
| 4052 | + DINGO_LOG(ERROR) << "failed, " << table_id << "|" << start_id << " , error_msg : " << ret.error_str(); |
| 4053 | + response->mutable_error()->set_errcode(static_cast<pb::error::Errno>(ret.error_code())); |
| 4054 | + response->mutable_error()->set_errmsg(ret.error_str()); |
| 4055 | + return; |
| 4056 | + } |
| 4057 | + } |
| 4058 | + std::shared_ptr<Context> ctx = |
| 4059 | + std::make_shared<Context>(static_cast<brpc::Controller *>(controller), nullptr, response); |
| 4060 | + ctx->SetRegionId(Constant::kAutoIncrementRegionId); |
| 4061 | + ctx->SetTracker(done->Tracker()); |
| 4062 | + |
| 4063 | + // this is a async operation will be block by closure |
| 4064 | + auto ret2 = raft_engine->Write(ctx, WriteDataBuilder::BuildWrite(ctx->CfName(), meta_increment)); |
| 4065 | + if (!ret2.ok()) { |
| 4066 | + DINGO_LOG(ERROR) << "failed, " << " | " << ret2.error_str(); |
| 4067 | + ServiceHelper::SetError(response->mutable_error(), ret2.error_code(), ret2.error_str()); |
| 4068 | + |
| 4069 | + if (ret2.error_code() == pb::error::Errno::ERAFT_NOTLEADER) { |
| 4070 | + auto_increment_control->RedirectResponse(response); |
| 4071 | + } |
| 4072 | + return; |
| 4073 | + } |
| 4074 | + |
| 4075 | + DINGO_LOG(INFO) << "CreateOrUpdateAutoIncrements Success. "; |
| 4076 | +} |
| 4077 | + |
| 4078 | +void MetaServiceImpl::CreateOrUpdateAutoIncrements(google::protobuf::RpcController *controller, |
| 4079 | + const pb::meta::CreateOrUpdateAutoIncrementsRequest *request, |
| 4080 | + pb::meta::CreateOrUpdateAutoIncrementsResponse *response, |
| 4081 | + google::protobuf::Closure *done) { |
| 4082 | + brpc::ClosureGuard done_guard(done); |
| 4083 | + if (!auto_increment_control_->IsLeader()) { |
| 4084 | + return RedirectResponse(response); |
| 4085 | + } |
| 4086 | + DINGO_LOG(INFO) << request->ShortDebugString(); |
| 4087 | + |
| 4088 | + // Run in queue. |
| 4089 | + auto *svr_done = new CoordinatorServiceClosure(__func__, done_guard.release(), request, response); |
| 4090 | + auto task = std::make_shared<ServiceTask>([this, controller, request, response, svr_done]() { |
| 4091 | + DoCreateOrUpdateAutoIncrements(controller, request, response, svr_done, auto_increment_control_, engine_); |
| 4092 | + }); |
| 4093 | + bool ret = worker_set_->ExecuteRR(task); |
| 4094 | + if (!ret) { |
| 4095 | + brpc::ClosureGuard done_guard(svr_done); |
| 4096 | + ServiceHelper::SetError(response->mutable_error(), pb::error::EREQUEST_FULL, "Commit execute queue failed"); |
| 4097 | + } |
| 4098 | +} |
| 4099 | + |
4032 | 4100 | void DoCreateTenant(google::protobuf::RpcController * /*controller*/, const pb::meta::CreateTenantRequest *request, |
4033 | 4101 | pb::meta::CreateTenantResponse *response, TrackClosure *done, |
4034 | 4102 | std::shared_ptr<CoordinatorControl> coordinator_control, std::shared_ptr<Engine> /*raft_engine*/) { |
|
0 commit comments