@@ -4,8 +4,8 @@ use crate::{
44 collect_first,
55 pd:: PdClient ,
66 request:: {
7- Collect , CollectSingle , CollectWithShard , DefaultProcessor , HasNextBatch , KvRequest , Merge ,
8- NextBatch , Process , ResponseWithShard , Shardable , SingleKey ,
7+ Batchable , Collect , CollectSingle , CollectWithShard , DefaultProcessor , HasNextBatch ,
8+ KvRequest , Merge , NextBatch , Process , ResponseWithShard , Shardable , SingleKey ,
99 } ,
1010 store:: { store_stream_for_keys, store_stream_for_range, RegionStore } ,
1111 timestamp:: TimestampExt ,
@@ -14,14 +14,19 @@ use crate::{
1414 KvPair , Result , Value ,
1515} ;
1616use either:: Either ;
17- use futures:: stream:: BoxStream ;
17+ use futures:: {
18+ stream:: { self , BoxStream } ,
19+ StreamExt ,
20+ } ;
1821use std:: { cmp, iter, sync:: Arc } ;
1922use tikv_client_common:: Error :: PessimisticLockError ;
2023use tikv_client_proto:: {
2124 kvrpcpb:: { self , LockInfo , TxnHeartBeatResponse , TxnInfo } ,
2225 pdpb:: Timestamp ,
2326} ;
2427
28+ use super :: transaction:: TXN_COMMIT_BATCH_SIZE ;
29+
2530// implement HasLocks for a response type that has a `pairs` field,
2631// where locks can be extracted from both the `pairs` and `error` fields
2732macro_rules! pair_locks {
@@ -256,7 +261,18 @@ impl Shardable for kvrpcpb::PrewriteRequest {
256261 ) -> BoxStream < ' static , Result < ( Self :: Shard , RegionStore ) > > {
257262 let mut mutations = self . mutations . clone ( ) ;
258263 mutations. sort_by ( |a, b| a. key . cmp ( & b. key ) ) ;
264+
259265 store_stream_for_keys ( mutations. into_iter ( ) , pd_client. clone ( ) )
266+ . flat_map ( |result| match result {
267+ Ok ( ( mutations, store) ) => stream:: iter ( kvrpcpb:: PrewriteRequest :: batches (
268+ mutations,
269+ TXN_COMMIT_BATCH_SIZE ,
270+ ) )
271+ . map ( move |batch| Ok ( ( batch, store. clone ( ) ) ) )
272+ . boxed ( ) ,
273+ Err ( e) => stream:: iter ( Err ( e) ) . boxed ( ) ,
274+ } )
275+ . boxed ( )
260276 }
261277
262278 fn apply_shard ( & mut self , shard : Self :: Shard , store : & RegionStore ) -> Result < ( ) > {
@@ -277,6 +293,16 @@ impl Shardable for kvrpcpb::PrewriteRequest {
277293 }
278294}
279295
296+ impl Batchable for kvrpcpb:: PrewriteRequest {
297+ type Item = kvrpcpb:: Mutation ;
298+
299+ fn item_size ( item : & Self :: Item ) -> u64 {
300+ let mut size = item. get_key ( ) . len ( ) as u64 ;
301+ size += item. get_value ( ) . len ( ) as u64 ;
302+ size
303+ }
304+ }
305+
280306pub fn new_commit_request (
281307 keys : Vec < Vec < u8 > > ,
282308 start_version : u64 ,
@@ -294,7 +320,42 @@ impl KvRequest for kvrpcpb::CommitRequest {
294320 type Response = kvrpcpb:: CommitResponse ;
295321}
296322
297- shardable_keys ! ( kvrpcpb:: CommitRequest ) ;
323+ impl Shardable for kvrpcpb:: CommitRequest {
324+ type Shard = Vec < Vec < u8 > > ;
325+
326+ fn shards (
327+ & self ,
328+ pd_client : & Arc < impl PdClient > ,
329+ ) -> BoxStream < ' static , Result < ( Self :: Shard , RegionStore ) > > {
330+ let mut keys = self . keys . clone ( ) ;
331+ keys. sort ( ) ;
332+
333+ store_stream_for_keys ( keys. into_iter ( ) , pd_client. clone ( ) )
334+ . flat_map ( |result| match result {
335+ Ok ( ( keys, store) ) => {
336+ stream:: iter ( kvrpcpb:: CommitRequest :: batches ( keys, TXN_COMMIT_BATCH_SIZE ) )
337+ . map ( move |batch| Ok ( ( batch, store. clone ( ) ) ) )
338+ . boxed ( )
339+ }
340+ Err ( e) => stream:: iter ( Err ( e) ) . boxed ( ) ,
341+ } )
342+ . boxed ( )
343+ }
344+
345+ fn apply_shard ( & mut self , shard : Self :: Shard , store : & RegionStore ) -> Result < ( ) > {
346+ self . set_context ( store. region_with_leader . context ( ) ?) ;
347+ self . set_keys ( shard. into_iter ( ) . map ( Into :: into) . collect ( ) ) ;
348+ Ok ( ( ) )
349+ }
350+ }
351+
352+ impl Batchable for kvrpcpb:: CommitRequest {
353+ type Item = Vec < u8 > ;
354+
355+ fn item_size ( item : & Self :: Item ) -> u64 {
356+ item. len ( ) as u64
357+ }
358+ }
298359
299360pub fn new_batch_rollback_request (
300361 keys : Vec < Vec < u8 > > ,
0 commit comments