mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #6167 from xiang90/fix_txn_rev
etcdserver: fix wrong rev in header when nothing is actually got executed
This commit is contained in:
commit
82a3d90763
@ -345,34 +345,23 @@ func (a *applierV3backend) Txn(rt *pb.TxnRequest) (*pb.TxnResponse, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
revision := a.s.KV().Rev()
|
|
||||||
|
|
||||||
// When executing the operations of txn, we need to hold the txn lock.
|
// When executing the operations of txn, we need to hold the txn lock.
|
||||||
// So the reader will not see any intermediate results.
|
// So the reader will not see any intermediate results.
|
||||||
txnID := a.s.KV().TxnBegin()
|
txnID := a.s.KV().TxnBegin()
|
||||||
defer func() {
|
|
||||||
|
resps := make([]*pb.ResponseOp, len(reqs))
|
||||||
|
for i := range reqs {
|
||||||
|
resps[i] = a.applyUnion(txnID, reqs[i])
|
||||||
|
}
|
||||||
|
|
||||||
err := a.s.KV().TxnEnd(txnID)
|
err := a.s.KV().TxnEnd(txnID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprint("unexpected error when closing txn", txnID))
|
panic(fmt.Sprint("unexpected error when closing txn", txnID))
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
resps := make([]*pb.ResponseOp, len(reqs))
|
|
||||||
changedKV := false
|
|
||||||
for i := range reqs {
|
|
||||||
if reqs[i].GetRequestRange() == nil {
|
|
||||||
changedKV = true
|
|
||||||
}
|
|
||||||
resps[i] = a.applyUnion(txnID, reqs[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
if changedKV {
|
|
||||||
revision += 1
|
|
||||||
}
|
|
||||||
|
|
||||||
txnResp := &pb.TxnResponse{}
|
txnResp := &pb.TxnResponse{}
|
||||||
txnResp.Header = &pb.ResponseHeader{}
|
txnResp.Header = &pb.ResponseHeader{}
|
||||||
txnResp.Header.Revision = revision
|
txnResp.Header.Revision = a.s.KV().Rev()
|
||||||
txnResp.Responses = resps
|
txnResp.Responses = resps
|
||||||
txnResp.Succeeded = ok
|
txnResp.Succeeded = ok
|
||||||
return txnResp, nil
|
return txnResp, nil
|
||||||
|
@ -288,6 +288,18 @@ func TestV3TxnRevision(t *testing.T) {
|
|||||||
t.Fatalf("got rev %d, wanted rev %d", tresp.Header.Revision, presp.Header.Revision)
|
t.Fatalf("got rev %d, wanted rev %d", tresp.Header.Revision, presp.Header.Revision)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
txndr := &pb.RequestOp{Request: &pb.RequestOp_RequestDeleteRange{RequestDeleteRange: &pb.DeleteRangeRequest{Key: []byte("def")}}}
|
||||||
|
txn = &pb.TxnRequest{Success: []*pb.RequestOp{txndr}}
|
||||||
|
tresp, err = kvc.Txn(context.TODO(), txn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// did not update revision
|
||||||
|
if presp.Header.Revision != tresp.Header.Revision {
|
||||||
|
t.Fatalf("got rev %d, wanted rev %d", tresp.Header.Revision, presp.Header.Revision)
|
||||||
|
}
|
||||||
|
|
||||||
txnput := &pb.RequestOp{Request: &pb.RequestOp_RequestPut{RequestPut: &pb.PutRequest{Key: []byte("abc"), Value: []byte("123")}}}
|
txnput := &pb.RequestOp{Request: &pb.RequestOp_RequestPut{RequestPut: &pb.PutRequest{Key: []byte("abc"), Value: []byte("123")}}}
|
||||||
txn = &pb.TxnRequest{Success: []*pb.RequestOp{txnput}}
|
txn = &pb.TxnRequest{Success: []*pb.RequestOp{txnput}}
|
||||||
tresp, err = kvc.Txn(context.TODO(), txn)
|
tresp, err = kvc.Txn(context.TODO(), txn)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user