Skip to content

refactor(meta): replace manual struct instantiation with constructor methods #18393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/meta/api/src/schema_api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2188,9 +2188,7 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
txn.if_then
.push(txn_op_put(&tbid, serialize_struct(&new_table_meta)?));
txn.else_then.push(TxnOp {
request: Some(Request::Get(TxnGetRequest {
key: tbid.to_string_key(),
})),
request: Some(Request::Get(TxnGetRequest::new(tbid.to_string_key()))),
});

new_table_meta_map.insert(req.0.table_id, new_table_meta);
Expand Down
15 changes: 2 additions & 13 deletions src/meta/api/src/schema_api_test_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ use databend_common_meta_kvapi::kvapi;
use databend_common_meta_kvapi::kvapi::Key;
use databend_common_meta_types::MatchSeq;
use databend_common_meta_types::MetaError;
use databend_common_meta_types::Operation;
use databend_common_meta_types::UpsertKV;
use fastrace::func_name;
use log::debug;
Expand Down Expand Up @@ -240,12 +239,7 @@ async fn upsert_test_data(
value: Vec<u8>,
) -> Result<u64, KVAppError> {
let res = kv_api
.upsert_kv(UpsertKV {
key: key.to_string_key(),
seq: MatchSeq::GE(0),
value: Operation::Update(value),
value_meta: None,
})
.upsert_kv(UpsertKV::update(key.to_string_key(), &value))
.await?;

let seq_v = res.result.unwrap();
Expand All @@ -257,12 +251,7 @@ async fn delete_test_data(
key: &impl kvapi::Key,
) -> Result<(), KVAppError> {
let _res = kv_api
.upsert_kv(UpsertKV {
key: key.to_string_key(),
seq: MatchSeq::GE(0),
value: Operation::Delete,
value_meta: None,
})
.upsert_kv(UpsertKV::delete(key.to_string_key()))
.await?;

Ok(())
Expand Down
9 changes: 1 addition & 8 deletions src/meta/api/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ use databend_common_meta_types::txn_condition::Target;
use databend_common_meta_types::ConditionResult;
use databend_common_meta_types::InvalidArgument;
use databend_common_meta_types::InvalidReply;
use databend_common_meta_types::MatchSeq;
use databend_common_meta_types::MetaError;
use databend_common_meta_types::MetaNetworkError;
use databend_common_meta_types::Operation;
use databend_common_meta_types::SeqV;
use databend_common_meta_types::TxnCondition;
use databend_common_meta_types::TxnGetResponse;
Expand Down Expand Up @@ -200,12 +198,7 @@ pub async fn fetch_id<T: kvapi::Key>(
generator: T,
) -> Result<u64, MetaError> {
let res = kv_api
.upsert_kv(UpsertKV {
key: generator.to_string_key(),
seq: MatchSeq::GE(0),
value: Operation::Update(b"".to_vec()),
value_meta: None,
})
.upsert_kv(UpsertKV::update(generator.to_string_key(), b""))
.await?;

// seq: MatchSeq::Any always succeeds
Expand Down
13 changes: 5 additions & 8 deletions src/meta/binaries/meta/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,11 @@ async fn do_register(meta_node: &Arc<MetaNode>, conf: &Config) -> Result<(), Met
println!("Register this node: {{{}}}", node);
println!();

let ent = LogEntry {
time_ms: None,
cmd: Cmd::AddNode {
node_id,
node,
overriding: true,
},
};
let ent = LogEntry::new(Cmd::AddNode {
node_id,
node,
overriding: true,
});
info!("Raft log entry for updating node: {:?}", ent);

meta_node.write(ent).await?;
Expand Down
23 changes: 7 additions & 16 deletions src/meta/client/src/grpc_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use databend_common_meta_types::protobuf::RaftRequest;
use databend_common_meta_types::protobuf::StreamItem;
use databend_common_meta_types::protobuf::WatchRequest;
use databend_common_meta_types::protobuf::WatchResponse;
use databend_common_meta_types::GrpcHelper;
use databend_common_meta_types::InvalidArgument;
use databend_common_meta_types::TxnReply;
use databend_common_meta_types::TxnRequest;
Expand Down Expand Up @@ -71,10 +72,7 @@ impl TryInto<MetaGrpcReq> for Request<RaftRequest> {

impl From<MetaGrpcReq> for RaftRequest {
fn from(v: MetaGrpcReq) -> Self {
let raft_request = RaftRequest {
// Safe unwrap(): serialize to string must be ok.
data: serde_json::to_string(&v).unwrap(),
};
let raft_request = GrpcHelper::encode_raft_request(&v).expect("fail to serialize");

debug!(
req :? =(&raft_request);
Expand All @@ -87,10 +85,8 @@ impl From<MetaGrpcReq> for RaftRequest {

impl MetaGrpcReq {
pub fn to_raft_request(&self) -> Result<RaftRequest, InvalidArgument> {
let raft_request = RaftRequest {
data: serde_json::to_string(self)
.map_err(|e| InvalidArgument::new(e, "fail to encode request"))?,
};
let raft_request = GrpcHelper::encode_raft_request(self)
.map_err(|e| InvalidArgument::new(e, "fail to encode request"))?;

debug!(
req :? =(&raft_request);
Expand Down Expand Up @@ -124,10 +120,7 @@ impl RequestFor for MetaGrpcReadReq {

impl From<MetaGrpcReadReq> for RaftRequest {
fn from(v: MetaGrpcReadReq) -> Self {
let raft_request = RaftRequest {
// Safe unwrap(): serialize to string must be ok.
data: serde_json::to_string(&v).unwrap(),
};
let raft_request = GrpcHelper::encode_raft_request(&v).expect("fail to serialize");

debug!(
req :? =(&raft_request);
Expand All @@ -140,10 +133,8 @@ impl From<MetaGrpcReadReq> for RaftRequest {

impl MetaGrpcReadReq {
pub fn to_raft_request(&self) -> Result<RaftRequest, InvalidArgument> {
let raft_request = RaftRequest {
data: serde_json::to_string(self)
.map_err(|e| InvalidArgument::new(e, "fail to encode request"))?,
};
let raft_request = GrpcHelper::encode_raft_request(self)
.map_err(|e| InvalidArgument::new(e, "fail to encode request"))?;

debug!(
req :? =(&raft_request);
Expand Down
2 changes: 1 addition & 1 deletion src/meta/control/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ async fn init_new_cluster(

let entry: Entry = Entry {
log_id,
payload: EntryPayload::Normal(LogEntry { time_ms: None, cmd }),
payload: EntryPayload::Normal(LogEntry::new(cmd)),
};

sto.blocking_append([entry]).await?;
Expand Down
18 changes: 7 additions & 11 deletions src/meta/kvapi-test-suite/src/kvapi_test_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,23 +834,19 @@ impl TestSuite {
TxnOp::put(txn_key2.clone(), b("new_v2")),
// get k1
TxnOp {
request: Some(txn_op::Request::Get(TxnGetRequest {
key: txn_key1.clone(),
})),
request: Some(txn_op::Request::Get(TxnGetRequest::new(txn_key1.clone()))),
},
// delete k1
TxnOp {
request: Some(txn_op::Request::Delete(TxnDeleteRequest {
key: txn_key1.clone(),
prev_value: true,
match_seq: None,
})),
request: Some(txn_op::Request::Delete(TxnDeleteRequest::new(
txn_key1.clone(),
true,
None,
))),
},
// get k1
TxnOp {
request: Some(txn_op::Request::Get(TxnGetRequest {
key: txn_key1.clone(),
})),
request: Some(txn_op::Request::Get(TxnGetRequest::new(txn_key1.clone()))),
},
];

Expand Down
36 changes: 15 additions & 21 deletions src/meta/process/src/kv_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ where F: Fn(&str, Vec<u8>) -> Result<Vec<u8>, anyhow::Error>
Cmd::RemoveNode { .. } => Ok(None),
Cmd::SetFeature { .. } => Ok(None),
Cmd::UpsertKV(ups) => {
let x = LogEntry {
time_ms: log_entry.time_ms,
cmd: Cmd::UpsertKV(unwrap_or_return!(self.proc_upsert_kv(ups)?)),
};
let x = LogEntry::new_with_time(
Cmd::UpsertKV(unwrap_or_return!(self.proc_upsert_kv(ups)?)),
log_entry.time_ms,
);
Ok(Some(x))
}
Cmd::Transaction(tx) => {
Expand All @@ -160,10 +160,10 @@ where F: Fn(&str, Vec<u8>) -> Result<Vec<u8>, anyhow::Error>
else_then.push(self.proc_txop(op)?);
}

Ok(Some(LogEntry {
time_ms: log_entry.time_ms,
cmd: Cmd::Transaction(TxnRequest::new(condition, if_then).with_else(else_then)),
}))
Ok(Some(LogEntry::new_with_time(
Cmd::Transaction(TxnRequest::new(condition, if_then).with_else(else_then)),
log_entry.time_ms,
)))
}
}
}
Expand All @@ -173,12 +173,12 @@ where F: Fn(&str, Vec<u8>) -> Result<Vec<u8>, anyhow::Error>
Operation::Update(v) => {
let buf = (self.process_pb)(&ups.key, v)?;

Ok(Some(UpsertKV {
key: ups.key,
seq: ups.seq,
value: Operation::Update(buf),
value_meta: ups.value_meta,
}))
Ok(Some(UpsertKV::new(
ups.key,
ups.seq,
Operation::Update(buf),
ups.value_meta,
)))
}
Operation::Delete => Ok(None),
#[allow(deprecated)]
Expand Down Expand Up @@ -220,13 +220,7 @@ where F: Fn(&str, Vec<u8>) -> Result<Vec<u8>, anyhow::Error>
fn proc_tx_put_request(&self, p: TxnPutRequest) -> Result<TxnPutRequest, anyhow::Error> {
let value = (self.process_pb)(&p.key, p.value)?;

let pr = TxnPutRequest {
key: p.key,
value,
prev_value: p.prev_value,
expire_at: p.expire_at,
ttl_ms: p.ttl_ms,
};
let pr = TxnPutRequest::new(p.key, value, p.prev_value, p.expire_at, p.ttl_ms);

Ok(pr)
}
Expand Down
20 changes: 8 additions & 12 deletions src/meta/raft-store/src/state_machine/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ pub fn snapshot_logs() -> (Vec<Entry>, Vec<String>) {
Entry::new_blank(new_log_id(1, 0, 3)),
Entry {
log_id: new_log_id(1, 0, 4),
payload: EntryPayload::Normal(LogEntry {
time_ms: None,
cmd: Cmd::UpsertKV(UpsertKV::update("a", b"A")),
}),
payload: EntryPayload::Normal(LogEntry::new(Cmd::UpsertKV(UpsertKV::update(
"a", b"A",
)))),
},
Entry {
log_id: new_log_id(1, 0, 5),
Expand All @@ -60,14 +59,11 @@ pub fn snapshot_logs() -> (Vec<Entry>, Vec<String>) {
},
Entry {
log_id: new_log_id(1, 0, 9),
payload: EntryPayload::Normal(LogEntry {
time_ms: None,
cmd: Cmd::AddNode {
node_id: 5,
node: Default::default(),
overriding: false,
},
}),
payload: EntryPayload::Normal(LogEntry::new(Cmd::AddNode {
node_id: 5,
node: Default::default(),
overriding: false,
})),
},
];
let want = [ //
Expand Down
9 changes: 3 additions & 6 deletions src/meta/service/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use databend_common_meta_types::protobuf::RaftRequest;
use databend_common_meta_types::raft_types::NodeId;
use databend_common_meta_types::AppliedState;
use databend_common_meta_types::Endpoint;
use databend_common_meta_types::GrpcHelper;
use databend_common_meta_types::LogEntry;
use databend_common_meta_types::MetaAPIError;

Expand Down Expand Up @@ -150,18 +151,14 @@ pub enum ForwardResponse {

impl tonic::IntoRequest<RaftRequest> for ForwardRequest<ForwardRequestBody> {
fn into_request(self) -> tonic::Request<RaftRequest> {
let mes = RaftRequest {
data: serde_json::to_string(&self).expect("fail to serialize"),
};
let mes = GrpcHelper::encode_raft_request(&self).expect("fail to serialize");
tonic::Request::new(mes)
}
}

impl tonic::IntoRequest<RaftRequest> for ForwardRequest<MetaGrpcReadReq> {
fn into_request(self) -> tonic::Request<RaftRequest> {
let mes = RaftRequest {
data: serde_json::to_string(&self).expect("fail to serialize"),
};
let mes = GrpcHelper::encode_raft_request(&self).expect("fail to serialize");
tonic::Request::new(mes)
}
}
Expand Down
20 changes: 7 additions & 13 deletions src/meta/service/src/meta_service/meta_leader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,12 @@ impl<'a> MetaLeader<'a> {
return Ok(());
}

let ent = LogEntry {
time_ms: None,
cmd: Cmd::AddNode {
node_id,
node: Node::new(node_id, endpoint)
.with_grpc_advertise_address(req.grpc_api_advertise_address),
overriding: false,
},
};
let ent = LogEntry::new(Cmd::AddNode {
node_id,
node: Node::new(node_id, endpoint)
.with_grpc_advertise_address(req.grpc_api_advertise_address),
overriding: false,
});
self.write(ent).await?;

self.raft
Expand Down Expand Up @@ -249,10 +246,7 @@ impl<'a> MetaLeader<'a> {
.await?;

// 2. Remove node info
let ent = LogEntry {
time_ms: None,
cmd: Cmd::RemoveNode { node_id },
};
let ent = LogEntry::new(Cmd::RemoveNode { node_id });
self.write(ent).await?;

Ok(())
Expand Down
Loading
Loading