@@ -105,15 +105,13 @@ func (c *ShardedCoordinator) dispatchTxn(startTS uint64, commitTS uint64, elems
105105 return nil , errors .WithStack (ErrTxnPrimaryKeyRequired )
106106 }
107107
108- if commitTS == 0 {
109- commitTS = c .nextTxnTSAfter (startTS )
110- } else {
111- // Observe caller-provided commitTS to keep the HLC monotonic; without
112- // this the clock could later issue timestamps smaller than commitTS.
113- c .clock .Observe (commitTS )
108+ commitTS , err = c .resolveTxnCommitTS (startTS , commitTS )
109+ if err != nil {
110+ return nil , err
114111 }
115- if commitTS == 0 || commitTS <= startTS {
116- return nil , errors .WithStack (ErrTxnCommitTSRequired )
112+
113+ if len (gids ) == 1 {
114+ return c .dispatchSingleShardTxn (startTS , commitTS , primaryKey , gids [0 ], elems )
117115 }
118116
119117 prepared , err := c .prewriteTxn (startTS , commitTS , primaryKey , grouped , gids )
@@ -131,6 +129,37 @@ func (c *ShardedCoordinator) dispatchTxn(startTS uint64, commitTS uint64, elems
131129 return & CoordinateResponse {CommitIndex : maxIndex }, nil
132130}
133131
132+ func (c * ShardedCoordinator ) resolveTxnCommitTS (startTS , commitTS uint64 ) (uint64 , error ) {
133+ if commitTS == 0 {
134+ commitTS = c .nextTxnTSAfter (startTS )
135+ } else {
136+ // Observe caller-provided commitTS to keep the HLC monotonic; without
137+ // this the clock could later issue timestamps smaller than commitTS.
138+ c .clock .Observe (commitTS )
139+ }
140+ if commitTS == 0 || commitTS <= startTS {
141+ return 0 , errors .WithStack (ErrTxnCommitTSRequired )
142+ }
143+ return commitTS , nil
144+ }
145+
146+ func (c * ShardedCoordinator ) dispatchSingleShardTxn (startTS , commitTS uint64 , primaryKey []byte , gid uint64 , elems []* Elem [OP ]) (* CoordinateResponse , error ) {
147+ g , err := c .txnGroupForID (gid )
148+ if err != nil {
149+ return nil , err
150+ }
151+ resp , err := g .Txn .Commit ([]* pb.Request {
152+ onePhaseTxnRequest (startTS , commitTS , primaryKey , elems ),
153+ })
154+ if err != nil {
155+ return nil , errors .WithStack (err )
156+ }
157+ if resp == nil {
158+ return & CoordinateResponse {}, nil
159+ }
160+ return & CoordinateResponse {CommitIndex : resp .CommitIndex }, nil
161+ }
162+
134163type preparedGroup struct {
135164 gid uint64
136165 keys []* pb.Mutation
@@ -487,16 +516,9 @@ func (c *ShardedCoordinator) txnLogs(reqs *OperationGroup[OP]) ([]*pb.Request, e
487516 if len (gids ) != 1 {
488517 return nil , errors .WithStack (ErrInvalidRequest )
489518 }
490- commitTS := reqs .CommitTS
491- if commitTS == 0 {
492- commitTS = c .nextTxnTSAfter (reqs .StartTS )
493- } else {
494- // Observe caller-provided commitTS to keep the HLC monotonic; without
495- // this the clock could later issue timestamps smaller than commitTS.
496- c .clock .Observe (commitTS )
497- }
498- if commitTS == 0 || commitTS <= reqs .StartTS {
499- return nil , errors .WithStack (ErrTxnCommitTSRequired )
519+ commitTS , err := c .resolveTxnCommitTS (reqs .StartTS , reqs .CommitTS )
520+ if err != nil {
521+ return nil , err
500522 }
501523 return buildTxnLogs (reqs .StartTS , commitTS , grouped , gids )
502524}
0 commit comments