Skip to content

Commit

Permalink
chore: minor fix (#1801)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjiachun authored Jun 20, 2023
1 parent 16c1ee2 commit e47ef1f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/meta-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ mod tests {
let tc = new_client("test_batch_put").await;

let mut req = BatchPutRequest::new();
for i in 0..256 {
for i in 0..275 {
req = req.add_kv(
tc.key(&format!("key-{}", i)),
format!("value-{}", i).into_bytes(),
Expand All @@ -769,7 +769,7 @@ mod tests {
let req = RangeRequest::new().with_prefix(tc.key("key-"));
let res = tc.client.range(req).await;
let kvs = res.unwrap().take_kvs();
assert_eq!(256, kvs.len());
assert_eq!(275, kvs.len());
}

#[tokio::test]
Expand Down
51 changes: 18 additions & 33 deletions src/meta-srv/src/service/store/etcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ use crate::error::Result;
use crate::metrics::METRIC_META_KV_REQUEST;
use crate::service::store::kv::{KvStore, KvStoreRef};

// Maximum number of operations permitted in a transaction.
// The etcd default configuration's `--max-txn-ops` is 128.
//
// For more detail, see: https://etcd.io/docs/v3.5/op-guide/configuration/
const MAX_TXN_SIZE: usize = 128;

pub struct EtcdStore {
Expand All @@ -55,7 +59,7 @@ impl EtcdStore {
Ok(Arc::new(Self { client }))
}

async fn do_multi_txn(&self, mut txn_ops: Vec<TxnOp>) -> Result<Vec<TxnResponse>> {
async fn do_multi_txn(&self, txn_ops: Vec<TxnOp>) -> Result<Vec<TxnResponse>> {
if txn_ops.len() < MAX_TXN_SIZE {
// fast path
let txn = Txn::new().and_then(txn_ops);
Expand All @@ -68,36 +72,17 @@ impl EtcdStore {
return Ok(vec![txn_res]);
}

let mut txns = vec![];
loop {
if txn_ops.is_empty() {
break;
}

if txn_ops.len() < MAX_TXN_SIZE {
let txn = Txn::new().and_then(txn_ops);
txns.push(txn);
break;
}

let part = txn_ops.drain(..MAX_TXN_SIZE).collect::<Vec<_>>();
let txn = Txn::new().and_then(part);
txns.push(txn);
}
let txns = txn_ops
.chunks(MAX_TXN_SIZE)
.map(|part| async move {
let txn = Txn::new().and_then(part);
self.client.kv_client().txn(txn).await
})
.collect::<Vec<_>>();

let mut txn_responses = Vec::with_capacity(txns.len());
// Considering the pressure on etcd, it would be more appropriate to execute txn in
// a serial manner.
for txn in txns {
let txn_res = self
.client
.kv_client()
.txn(txn)
.await
.context(error::EtcdFailedSnafu)?;
txn_responses.push(txn_res);
}
Ok(txn_responses)
futures::future::try_join_all(txns)
.await
.context(error::EtcdFailedSnafu)
}
}

Expand Down Expand Up @@ -241,7 +226,7 @@ impl KvStore for EtcdStore {
prev_kvs.push(KvPair::from_etcd_kv(prev_kv));
}
}
_ => unreachable!(), // never get here
_ => unreachable!(),
}
}
}
Expand Down Expand Up @@ -283,7 +268,7 @@ impl KvStore for EtcdStore {
prev_kvs.push(KvPair::from_etcd_kv(kv));
});
}
_ => unreachable!(), // never get here
_ => unreachable!(),
}
}
}
Expand Down Expand Up @@ -343,7 +328,7 @@ impl KvStore for EtcdStore {
let prev_kv = match op_res {
TxnOpResponse::Put(res) => res.prev_key().map(KvPair::from_etcd_kv),
TxnOpResponse::Get(res) => res.kvs().first().map(KvPair::from_etcd_kv),
_ => unreachable!(), // never get here
_ => unreachable!(),
};

let header = Some(ResponseHeader::success(cluster_id));
Expand Down

0 comments on commit e47ef1f

Please sign in to comment.