Skip to content

Commit

Permalink
Add check for mod revision=0 in update txn
Browse files Browse the repository at this point in the history
Also, return proper GPRC errors to improve client handling.

Signed-off-by: Brad Davidson <[email protected]>
  • Loading branch information
brandond committed Oct 30, 2023
1 parent 7fae548 commit b190005
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions pkg/server/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (l *LimitedServer) delete(ctx context.Context, key string, revision int64)
ResponseRange: &etcdserverpb.RangeResponse{
Header: txnHeader(rev),
Kvs: toKVs(kv),
Count: 1,
},
},
},
Expand Down
11 changes: 3 additions & 8 deletions pkg/server/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"context"
"fmt"

"github.com/sirupsen/logrus"
"go.etcd.io/etcd/api/v3/etcdserverpb"
Expand Down Expand Up @@ -94,17 +93,17 @@ func toKV(kv *KeyValue) *mvccpb.KeyValue {
}

func (k *KVServerBridge) Put(ctx context.Context, r *etcdserverpb.PutRequest) (*etcdserverpb.PutResponse, error) {
return nil, fmt.Errorf("put is not supported")
return nil, unsupported("put")
}

func (k *KVServerBridge) DeleteRange(ctx context.Context, r *etcdserverpb.DeleteRangeRequest) (*etcdserverpb.DeleteRangeResponse, error) {
return nil, fmt.Errorf("delete is not supported")
return nil, unsupported("delete")
}

func (k *KVServerBridge) Txn(ctx context.Context, r *etcdserverpb.TxnRequest) (*etcdserverpb.TxnResponse, error) {
res, err := k.limited.Txn(ctx, r)
if err != nil {
logrus.Errorf("error in txn: %v", err)
logrus.Errorf("error in txn %s: %v", r, err)
}
return res, err
}
Expand All @@ -116,7 +115,3 @@ func (k *KVServerBridge) Compact(ctx context.Context, r *etcdserverpb.Compaction
},
}, nil
}

func unsupported(field string) error {
return fmt.Errorf("%s is unsupported", field)
}
3 changes: 1 addition & 2 deletions pkg/server/limited.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"context"
"fmt"

"go.etcd.io/etcd/api/v3/etcdserverpb"
)
Expand Down Expand Up @@ -38,7 +37,7 @@ func (l *LimitedServer) Txn(ctx context.Context, txn *etcdserverpb.TxnRequest) (
if isCompact(txn) {
return l.compact(ctx)
}
return nil, fmt.Errorf("unsupported transaction: %v", txn)
return nil, ErrNotSupported
}

type ResponseHeader struct {
Expand Down
8 changes: 8 additions & 0 deletions pkg/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import (
"database/sql"

"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var (
ErrNotSupported = status.New(codes.InvalidArgument, "etcdserver: unsupported operations in txn request").Err()

ErrKeyExists = rpctypes.ErrGRPCDuplicateKey
ErrCompacted = rpctypes.ErrGRPCCompacted
)
Expand Down Expand Up @@ -71,3 +75,7 @@ type Event struct {
KV *KeyValue
PrevKV *KeyValue
}

func unsupported(field string) error {
return status.New(codes.Unimplemented, field+" is not implemented by kine").Err()
}
7 changes: 6 additions & 1 deletion pkg/server/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func (l *LimitedServer) update(ctx context.Context, rev int64, key string, value

if rev == 0 {
rev, err = l.backend.Create(ctx, key, value, lease)
ok = true
if err == ErrKeyExists {
rev, kv, err = l.backend.Get(ctx, key, "", 1, rev)
} else {
ok = true
}
} else {
rev, kv, ok, err = l.backend.Update(ctx, key, value, rev, lease)
}
Expand Down Expand Up @@ -62,6 +66,7 @@ func (l *LimitedServer) update(ctx context.Context, rev int64, key string, value
ResponseRange: &etcdserverpb.RangeResponse{
Header: txnHeader(rev),
Kvs: toKVs(kv),
Count: 1,
},
},
},
Expand Down

0 comments on commit b190005

Please sign in to comment.