Skip to content

Commit

Permalink
Merge pull request #16355 from serathius/txn-refactor
Browse files Browse the repository at this point in the history
server: Separate internal txn functions for recursion and have public function create transaction and trace
  • Loading branch information
serathius authored Aug 4, 2023
2 parents 10c7e81 + fa21c07 commit 524fddc
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 88 deletions.
24 changes: 12 additions & 12 deletions server/etcdserver/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ type applierV3 interface {
// delegates the actual execution to the applyFunc method.
Apply(ctx context.Context, r *pb.InternalRaftRequest, shouldApplyV3 membership.ShouldApplyV3, applyFunc applyFunc) *Result

Put(ctx context.Context, txn mvcc.TxnWrite, p *pb.PutRequest) (*pb.PutResponse, *traceutil.Trace, error)
Range(ctx context.Context, txn mvcc.TxnRead, r *pb.RangeRequest) (*pb.RangeResponse, error)
DeleteRange(txn mvcc.TxnWrite, dr *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error)
Put(ctx context.Context, p *pb.PutRequest) (*pb.PutResponse, *traceutil.Trace, error)
Range(ctx context.Context, r *pb.RangeRequest) (*pb.RangeResponse, error)
DeleteRange(dr *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error)
Txn(ctx context.Context, rt *pb.TxnRequest) (*pb.TxnResponse, *traceutil.Trace, error)
Compaction(compaction *pb.CompactionRequest) (*pb.CompactionResponse, <-chan struct{}, *traceutil.Trace, error)

Expand Down Expand Up @@ -157,16 +157,16 @@ func (a *applierV3backend) Apply(ctx context.Context, r *pb.InternalRaftRequest,
return applyFunc(ctx, r, shouldApplyV3)
}

func (a *applierV3backend) Put(ctx context.Context, txn mvcc.TxnWrite, p *pb.PutRequest) (resp *pb.PutResponse, trace *traceutil.Trace, err error) {
return mvcctxn.Put(ctx, a.lg, a.lessor, a.kv, txn, p)
func (a *applierV3backend) Put(ctx context.Context, p *pb.PutRequest) (resp *pb.PutResponse, trace *traceutil.Trace, err error) {
return mvcctxn.Put(ctx, a.lg, a.lessor, a.kv, p)
}

func (a *applierV3backend) DeleteRange(txn mvcc.TxnWrite, dr *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
return mvcctxn.DeleteRange(a.kv, txn, dr)
func (a *applierV3backend) DeleteRange(dr *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
return mvcctxn.DeleteRange(a.kv, dr)
}

func (a *applierV3backend) Range(ctx context.Context, txn mvcc.TxnRead, r *pb.RangeRequest) (*pb.RangeResponse, error) {
return mvcctxn.Range(ctx, a.lg, a.kv, txn, r)
func (a *applierV3backend) Range(ctx context.Context, r *pb.RangeRequest) (*pb.RangeResponse, error) {
return mvcctxn.Range(ctx, a.lg, a.kv, r)
}

func (a *applierV3backend) Txn(ctx context.Context, rt *pb.TxnRequest) (*pb.TxnResponse, *traceutil.Trace, error) {
Expand Down Expand Up @@ -255,7 +255,7 @@ type applierV3Capped struct {
// with Puts so that the number of keys in the store is capped.
func newApplierV3Capped(base applierV3) applierV3 { return &applierV3Capped{applierV3: base} }

func (a *applierV3Capped) Put(_ context.Context, _ mvcc.TxnWrite, _ *pb.PutRequest) (*pb.PutResponse, *traceutil.Trace, error) {
func (a *applierV3Capped) Put(_ context.Context, _ *pb.PutRequest) (*pb.PutResponse, *traceutil.Trace, error) {
return nil, nil, errors.ErrNoSpace
}

Expand Down Expand Up @@ -447,9 +447,9 @@ func newQuotaApplierV3(lg *zap.Logger, quotaBackendBytesCfg int64, be backend.Ba
return &quotaApplierV3{app, serverstorage.NewBackendQuota(lg, quotaBackendBytesCfg, be, "v3-applier")}
}

func (a *quotaApplierV3) Put(ctx context.Context, txn mvcc.TxnWrite, p *pb.PutRequest) (*pb.PutResponse, *traceutil.Trace, error) {
func (a *quotaApplierV3) Put(ctx context.Context, p *pb.PutRequest) (*pb.PutResponse, *traceutil.Trace, error) {
ok := a.q.Available(p)
resp, trace, err := a.applierV3.Put(ctx, txn, p)
resp, trace, err := a.applierV3.Put(ctx, p)
if err == nil && !ok {
err = errors.ErrNoSpace
}
Expand Down
13 changes: 6 additions & 7 deletions server/etcdserver/apply/apply_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"go.etcd.io/etcd/server/v3/etcdserver/api/membership"
"go.etcd.io/etcd/server/v3/etcdserver/txn"
"go.etcd.io/etcd/server/v3/lease"
"go.etcd.io/etcd/server/v3/storage/mvcc"
)

type authApplierV3 struct {
Expand Down Expand Up @@ -65,7 +64,7 @@ func (aa *authApplierV3) Apply(ctx context.Context, r *pb.InternalRaftRequest, s
return ret
}

func (aa *authApplierV3) Put(ctx context.Context, txn mvcc.TxnWrite, r *pb.PutRequest) (*pb.PutResponse, *traceutil.Trace, error) {
func (aa *authApplierV3) Put(ctx context.Context, r *pb.PutRequest) (*pb.PutResponse, *traceutil.Trace, error) {
if err := aa.as.IsPutPermitted(&aa.authInfo, r.Key); err != nil {
return nil, nil, err
}
Expand All @@ -84,17 +83,17 @@ func (aa *authApplierV3) Put(ctx context.Context, txn mvcc.TxnWrite, r *pb.PutRe
return nil, nil, err
}
}
return aa.applierV3.Put(ctx, txn, r)
return aa.applierV3.Put(ctx, r)
}

func (aa *authApplierV3) Range(ctx context.Context, txn mvcc.TxnRead, r *pb.RangeRequest) (*pb.RangeResponse, error) {
func (aa *authApplierV3) Range(ctx context.Context, r *pb.RangeRequest) (*pb.RangeResponse, error) {
if err := aa.as.IsRangePermitted(&aa.authInfo, r.Key, r.RangeEnd); err != nil {
return nil, err
}
return aa.applierV3.Range(ctx, txn, r)
return aa.applierV3.Range(ctx, r)
}

func (aa *authApplierV3) DeleteRange(txn mvcc.TxnWrite, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
func (aa *authApplierV3) DeleteRange(r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
if err := aa.as.IsDeleteRangePermitted(&aa.authInfo, r.Key, r.RangeEnd); err != nil {
return nil, err
}
Expand All @@ -105,7 +104,7 @@ func (aa *authApplierV3) DeleteRange(txn mvcc.TxnWrite, r *pb.DeleteRangeRequest
}
}

return aa.applierV3.DeleteRange(txn, r)
return aa.applierV3.DeleteRange(r)
}

func (aa *authApplierV3) Txn(ctx context.Context, rt *pb.TxnRequest) (*pb.TxnResponse, *traceutil.Trace, error) {
Expand Down
14 changes: 7 additions & 7 deletions server/etcdserver/apply/apply_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func TestAuthApplierV3_Put(t *testing.T) {
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
setAuthInfo(authApplier, tc.userName)
_, _, err := authApplier.Put(ctx, nil, tc.request)
_, _, err := authApplier.Put(ctx, tc.request)
require.Equalf(t, tc.expectError, err, "Put returned unexpected error (or lack thereof), expected: %v, got: %v", tc.expectError, err)
})
}
Expand All @@ -466,7 +466,7 @@ func TestAuthApplierV3_LeasePut(t *testing.T) {

// The user should be able to put the key
setAuthInfo(authApplier, userWriteOnly)
_, _, err = authApplier.Put(ctx, nil, &pb.PutRequest{
_, _, err = authApplier.Put(ctx, &pb.PutRequest{
Key: []byte(key),
Value: []byte("1"),
Lease: LeaseId,
Expand All @@ -475,7 +475,7 @@ func TestAuthApplierV3_LeasePut(t *testing.T) {

// Put a key under the lease outside user's key range
setAuthInfo(authApplier, userRoot)
_, _, err = authApplier.Put(ctx, nil, &pb.PutRequest{
_, _, err = authApplier.Put(ctx, &pb.PutRequest{
Key: []byte(keyOutsideRange),
Value: []byte("1"),
Lease: LeaseId,
Expand All @@ -484,7 +484,7 @@ func TestAuthApplierV3_LeasePut(t *testing.T) {

// The user should not be able to put the key anymore
setAuthInfo(authApplier, userWriteOnly)
_, _, err = authApplier.Put(ctx, nil, &pb.PutRequest{
_, _, err = authApplier.Put(ctx, &pb.PutRequest{
Key: []byte(key),
Value: []byte("1"),
Lease: LeaseId,
Expand Down Expand Up @@ -532,7 +532,7 @@ func TestAuthApplierV3_Range(t *testing.T) {
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
setAuthInfo(authApplier, tc.userName)
_, err := authApplier.Range(ctx, nil, tc.request)
_, err := authApplier.Range(ctx, tc.request)
require.Equalf(t, tc.expectError, err, "Range returned unexpected error (or lack thereof), expected: %v, got: %v", tc.expectError, err)
})
}
Expand Down Expand Up @@ -596,7 +596,7 @@ func TestAuthApplierV3_DeleteRange(t *testing.T) {
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
setAuthInfo(authApplier, tc.userName)
_, err := authApplier.DeleteRange(nil, tc.request)
_, err := authApplier.DeleteRange(tc.request)
require.Equalf(t, tc.expectError, err, "Range returned unexpected error (or lack thereof), expected: %v, got: %v", tc.expectError, err)
})
}
Expand Down Expand Up @@ -703,7 +703,7 @@ func TestAuthApplierV3_LeaseRevoke(t *testing.T) {

// Put a key under the lease outside user's key range
setAuthInfo(authApplier, userRoot)
_, _, err = authApplier.Put(ctx, nil, &pb.PutRequest{
_, _, err = authApplier.Put(ctx, &pb.PutRequest{
Key: []byte(keyOutsideRange),
Value: []byte("1"),
Lease: LeaseId,
Expand Down
7 changes: 3 additions & 4 deletions server/etcdserver/apply/corrupt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
pb "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/pkg/v3/traceutil"
"go.etcd.io/etcd/server/v3/etcdserver/errors"
"go.etcd.io/etcd/server/v3/storage/mvcc"
)

type applierV3Corrupt struct {
Expand All @@ -29,15 +28,15 @@ type applierV3Corrupt struct {

func newApplierV3Corrupt(a applierV3) *applierV3Corrupt { return &applierV3Corrupt{a} }

func (a *applierV3Corrupt) Put(_ context.Context, _ mvcc.TxnWrite, _ *pb.PutRequest) (*pb.PutResponse, *traceutil.Trace, error) {
func (a *applierV3Corrupt) Put(_ context.Context, _ *pb.PutRequest) (*pb.PutResponse, *traceutil.Trace, error) {
return nil, nil, errors.ErrCorrupt
}

func (a *applierV3Corrupt) Range(_ context.Context, _ mvcc.TxnRead, _ *pb.RangeRequest) (*pb.RangeResponse, error) {
func (a *applierV3Corrupt) Range(_ context.Context, _ *pb.RangeRequest) (*pb.RangeResponse, error) {
return nil, errors.ErrCorrupt
}

func (a *applierV3Corrupt) DeleteRange(_ mvcc.TxnWrite, _ *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
func (a *applierV3Corrupt) DeleteRange(_ *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
return nil, errors.ErrCorrupt
}

Expand Down
6 changes: 3 additions & 3 deletions server/etcdserver/apply/uber_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ func (a *uberApplier) dispatch(ctx context.Context, r *pb.InternalRaftRequest, s
switch {
case r.Range != nil:
op = "Range"
ar.Resp, ar.Err = a.applyV3.Range(ctx, nil, r.Range)
ar.Resp, ar.Err = a.applyV3.Range(ctx, r.Range)
case r.Put != nil:
op = "Put"
ar.Resp, ar.Trace, ar.Err = a.applyV3.Put(ctx, nil, r.Put)
ar.Resp, ar.Trace, ar.Err = a.applyV3.Put(ctx, r.Put)
case r.DeleteRange != nil:
op = "DeleteRange"
ar.Resp, ar.Err = a.applyV3.DeleteRange(nil, r.DeleteRange)
ar.Resp, ar.Err = a.applyV3.DeleteRange(r.DeleteRange)
case r.Txn != nil:
op = "Txn"
ar.Resp, ar.Trace, ar.Err = a.applyV3.Txn(ctx, r.Txn)
Expand Down
Loading

0 comments on commit 524fddc

Please sign in to comment.