Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

etcdserver: special apply for readonly transactions #14113

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion server/etcdserver/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,15 @@ func (a *applierV3backend) Txn(ctx context.Context, rt *pb.TxnRequest) (*pb.TxnR
if isWrite {
txn.End()
txn = a.s.KV().Write(trace)
a.applyTxn(ctx, txn, rt, txnPath, txnResp)
} else {
// readonly transactions are allowed to fail
err := a.applyReadOnlyTxn(ctx, txn, rt, txnPath, txnResp)
if err !=nil {
return nil, nil, err
}
}
a.applyTxn(ctx, txn, rt, txnPath, txnResp)

rev := txn.Rev()
if len(txn.Changes()) != 0 {
rev++
Expand Down Expand Up @@ -617,6 +624,35 @@ func compareKV(c *pb.Compare, ckv mvccpb.KeyValue) bool {
return true
}

//applyReadOnlyTxn handles a special case of readonly transactions (only Range) that can't be nested and are allowed to fail
func (a *applierV3backend) applyReadOnlyTxn(ctx context.Context, txn mvcc.TxnWrite, rt *pb.TxnRequest, txnPath []bool, tresp *pb.TxnResponse) error {
trace := traceutil.Get(ctx)
reqs := rt.Success
if !txnPath[0] {
reqs = rt.Failure
}

for i, req := range reqs {
respi := tresp.Responses[i].Response
switch tv := req.Request.(type) {
case *pb.RequestOp_RequestRange:
trace.StartSubTrace(
traceutil.Field{Key: "req_type", Value: "range"},
traceutil.Field{Key: "range_begin", Value: string(tv.RequestRange.Key)},
traceutil.Field{Key: "range_end", Value: string(tv.RequestRange.RangeEnd)})
resp, err := a.Range(ctx, txn, tv.RequestRange)
if err != nil {
return err
}
respi.(*pb.ResponseOp_ResponseRange).ResponseRange = resp
trace.StopSubTrace()
default:
// empty union
}
}
return nil
}

func (a *applierV3backend) applyTxn(ctx context.Context, txn mvcc.TxnWrite, rt *pb.TxnRequest, txnPath []bool, tresp *pb.TxnResponse) (txns int) {
trace := traceutil.Get(ctx)
reqs := rt.Success
Expand Down