tests/robustness: Make Range a proper request type to allow setting Range.Revision != 0 for stale reads

Signed-off-by: Marek Siarkowicz <siarkowicz@google.com>
dependabot/go_modules/github.com/prometheus/procfs-0.11.0
Marek Siarkowicz 2023-06-14 13:43:55 +02:00
parent 974655e02c
commit 6979318108
6 changed files with 145 additions and 82 deletions

View File

@ -30,17 +30,25 @@ func describeEtcdNonDeterministicResponse(request EtcdRequest, response EtcdNonD
}
func describeEtcdResponse(request EtcdRequest, response EtcdResponse) string {
if request.Type == Txn {
switch request.Type {
case Range:
return fmt.Sprintf("%s, rev: %d", describeRangeResponse(request.Range.RangeOptions, *response.Range), response.Revision)
case Txn:
return fmt.Sprintf("%s, rev: %d", describeTxnResponse(request.Txn, response.Txn), response.Revision)
case LeaseGrant, LeaseRevoke, Defragment:
if response.Revision == 0 {
return "ok"
}
return fmt.Sprintf("ok, rev: %d", response.Revision)
default:
return fmt.Sprintf("<! unknown request type: %q !>", request.Type)
}
if response.Revision == 0 {
return "ok"
}
return fmt.Sprintf("ok, rev: %d", response.Revision)
}
func describeEtcdRequest(request EtcdRequest) string {
switch request.Type {
case Range:
return describeRangeRequest(request.Range.Key, request.Range.RangeOptions)
case Txn:
onSuccess := describeEtcdOperations(request.Txn.OperationsOnSuccess)
if len(request.Txn.Conditions) != 0 {
@ -101,13 +109,7 @@ func describeTxnResponse(request *TxnRequest, response *TxnResponse) string {
func describeEtcdOperation(op EtcdOperation) string {
switch op.Type {
case RangeOperation:
if op.WithPrefix {
if op.Limit != 0 {
return fmt.Sprintf("range(%q, limit=%d)", op.Key, op.Limit)
}
return fmt.Sprintf("range(%q)", op.Key)
}
return fmt.Sprintf("get(%q)", op.Key)
return describeRangeRequest(op.Key, op.RangeOptions)
case PutOperation:
if op.LeaseID != 0 {
return fmt.Sprintf("put(%q, %s, %d)", op.Key, describeValueOrHash(op.Value), op.LeaseID)
@ -120,22 +122,25 @@ func describeEtcdOperation(op EtcdOperation) string {
}
}
func describeRangeRequest(key string, opts RangeOptions) string {
kwargs := []string{}
if opts.Limit != 0 {
kwargs = append(kwargs, fmt.Sprintf("limit=%d", opts.Limit))
}
command := "get"
if opts.WithPrefix {
command = "range"
}
if len(kwargs) == 0 {
return fmt.Sprintf("%s(%q)", command, key)
}
return fmt.Sprintf("%s(%q, %s)", command, key, strings.Join(kwargs, ", "))
}
func describeEtcdOperationResponse(req EtcdOperation, resp EtcdOperationResult) string {
switch req.Type {
case RangeOperation:
if req.WithPrefix {
kvs := make([]string, len(resp.KVs))
for i, kv := range resp.KVs {
kvs[i] = describeValueOrHash(kv.Value)
}
return fmt.Sprintf("[%s], count: %d", strings.Join(kvs, ","), resp.Count)
} else {
if len(resp.KVs) == 0 {
return "nil"
} else {
return describeValueOrHash(resp.KVs[0].Value)
}
}
return describeRangeResponse(req.RangeOptions, resp.RangeResponse)
case PutOperation:
return fmt.Sprintf("ok")
case DeleteOperation:
@ -145,6 +150,22 @@ func describeEtcdOperationResponse(req EtcdOperation, resp EtcdOperationResult)
}
}
func describeRangeResponse(opts RangeOptions, response RangeResponse) string {
if opts.WithPrefix {
kvs := make([]string, len(response.KVs))
for i, kv := range response.KVs {
kvs[i] = describeValueOrHash(kv.Value)
}
return fmt.Sprintf("[%s], count: %d", strings.Join(kvs, ","), response.Count)
} else {
if len(response.KVs) == 0 {
return "nil"
} else {
return describeValueOrHash(response.KVs[0].Value)
}
}
}
func describeValueOrHash(value ValueOrHash) string {
if value.Hash != 0 {
return fmt.Sprintf("hash: %d", value.Hash)

View File

@ -95,18 +95,18 @@ func TestModelDescribe(t *testing.T) {
expectDescribe: `if(mod_rev(key9)==9).then(put("key9", "99")) -> err: "failed"`,
},
{
req: txnRequest([]EtcdCondition{{Key: "key9b", ExpectedRevision: 9}}, []EtcdOperation{{Type: PutOperation, Key: "key9b", Value: ValueOrHash{Value: "991"}}}, []EtcdOperation{{Type: RangeOperation, Key: "key9b"}}),
req: txnRequest([]EtcdCondition{{Key: "key9b", ExpectedRevision: 9}}, []EtcdOperation{{Type: PutOperation, Key: "key9b", PutOptions: PutOptions{Value: ValueOrHash{Value: "991"}}}}, []EtcdOperation{{Type: RangeOperation, Key: "key9b"}}),
resp: txnResponse([]EtcdOperationResult{{}}, true, 10),
expectDescribe: `if(mod_rev(key9b)==9).then(put("key9b", "991")).else(get("key9b")) -> success(ok), rev: 10`,
},
{
req: txnRequest([]EtcdCondition{{Key: "key9c", ExpectedRevision: 9}}, []EtcdOperation{{Type: PutOperation, Key: "key9c", Value: ValueOrHash{Value: "992"}}}, []EtcdOperation{{Type: RangeOperation, Key: "key9c"}}),
resp: txnResponse([]EtcdOperationResult{{KVs: []KeyValue{{Key: "key9c", ValueRevision: ValueRevision{Value: ValueOrHash{Value: "993"}, ModRevision: 10}}}}}, false, 10),
req: txnRequest([]EtcdCondition{{Key: "key9c", ExpectedRevision: 9}}, []EtcdOperation{{Type: PutOperation, Key: "key9c", PutOptions: PutOptions{Value: ValueOrHash{Value: "992"}}}}, []EtcdOperation{{Type: RangeOperation, Key: "key9c"}}),
resp: txnResponse([]EtcdOperationResult{{RangeResponse: RangeResponse{KVs: []KeyValue{{Key: "key9c", ValueRevision: ValueRevision{Value: ValueOrHash{Value: "993"}, ModRevision: 10}}}}}}, false, 10),
expectDescribe: `if(mod_rev(key9c)==9).then(put("key9c", "992")).else(get("key9c")) -> failure("993"), rev: 10`,
},
{
req: txnRequest(nil, []EtcdOperation{{Type: RangeOperation, Key: "10"}, {Type: PutOperation, Key: "11", Value: ValueOrHash{Value: "111"}}, {Type: DeleteOperation, Key: "12"}}, nil),
resp: txnResponse([]EtcdOperationResult{{KVs: []KeyValue{{ValueRevision: ValueRevision{Value: ValueOrHash{Value: "110"}}}}}, {}, {Deleted: 1}}, true, 10),
req: txnRequest(nil, []EtcdOperation{{Type: RangeOperation, Key: "10"}, {Type: PutOperation, Key: "11", PutOptions: PutOptions{Value: ValueOrHash{Value: "111"}}}, {Type: DeleteOperation, Key: "12"}}, nil),
resp: txnResponse([]EtcdOperationResult{{RangeResponse: RangeResponse{KVs: []KeyValue{{ValueRevision: ValueRevision{Value: ValueOrHash{Value: "110"}}}}}}, {}, {Deleted: 1}}, true, 10),
expectDescribe: `get("10"), put("11", "111"), delete("12") -> "110", ok, deleted: 1, rev: 10`,
},
{

View File

@ -73,6 +73,13 @@ func initState(request EtcdRequest, response EtcdResponse) etcdState {
state := emptyState()
state.Revision = response.Revision
switch request.Type {
case Range:
for _, kv := range response.Range.KVs {
state.KeyValues[kv.Key] = ValueRevision{
Value: kv.Value,
ModRevision: kv.ModRevision,
}
}
case Txn:
if response.Txn.Failure {
return state
@ -131,6 +138,9 @@ func (s etcdState) step(request EtcdRequest) (etcdState, EtcdResponse) {
}
s.KeyValues = newKVs
switch request.Type {
case Range:
resp := s.getRange(request.Range.Key, request.Range.RangeOptions)
return s, EtcdResponse{Range: &resp, Revision: s.Revision}
case Txn:
failure := false
for _, cond := range request.Txn.Conditions {
@ -149,32 +159,7 @@ func (s etcdState) step(request EtcdRequest) (etcdState, EtcdResponse) {
switch op.Type {
case RangeOperation:
opResp[i] = EtcdOperationResult{
KVs: []KeyValue{},
}
if op.WithPrefix {
var count int64
for k, v := range s.KeyValues {
if strings.HasPrefix(k, op.Key) {
opResp[i].KVs = append(opResp[i].KVs, KeyValue{Key: k, ValueRevision: v})
count += 1
}
}
sort.Slice(opResp[i].KVs, func(j, k int) bool {
return opResp[i].KVs[j].Key < opResp[i].KVs[k].Key
})
if op.Limit != 0 && count > op.Limit {
opResp[i].KVs = opResp[i].KVs[:op.Limit]
}
opResp[i].Count = count
} else {
value, ok := s.KeyValues[op.Key]
if ok {
opResp[i].KVs = append(opResp[i].KVs, KeyValue{
Key: op.Key,
ValueRevision: value,
})
opResp[i].Count = 1
}
RangeResponse: s.getRange(op.Key, op.RangeOptions),
}
case PutOperation:
_, leaseExists := s.Leases[op.LeaseID]
@ -238,6 +223,38 @@ func (s etcdState) step(request EtcdRequest) (etcdState, EtcdResponse) {
}
}
func (s etcdState) getRange(key string, options RangeOptions) RangeResponse {
response := RangeResponse{
KVs: []KeyValue{},
}
if options.WithPrefix {
var count int64
for k, v := range s.KeyValues {
if strings.HasPrefix(k, key) {
response.KVs = append(response.KVs, KeyValue{Key: k, ValueRevision: v})
count += 1
}
}
sort.Slice(response.KVs, func(j, k int) bool {
return response.KVs[j].Key < response.KVs[k].Key
})
if options.Limit != 0 && count > options.Limit {
response.KVs = response.KVs[:options.Limit]
}
response.Count = count
} else {
value, ok := s.KeyValues[key]
if ok {
response.KVs = append(response.KVs, KeyValue{
Key: key,
ValueRevision: value,
})
response.Count = 1
}
}
return response
}
func detachFromOldLease(s etcdState, key string) etcdState {
if oldLeaseId, ok := s.KeyLeases[key]; ok {
delete(s.Leases[oldLeaseId].Keys, key)
@ -255,6 +272,7 @@ func attachToNewLease(s etcdState, leaseID int64, key string) etcdState {
type RequestType string
const (
Range RequestType = "range"
Txn RequestType = "txn"
LeaseGrant RequestType = "leaseGrant"
LeaseRevoke RequestType = "leaseRevoke"
@ -265,10 +283,28 @@ type EtcdRequest struct {
Type RequestType
LeaseGrant *LeaseGrantRequest
LeaseRevoke *LeaseRevokeRequest
Range *RangeRequest
Txn *TxnRequest
Defragment *DefragmentRequest
}
type RangeRequest struct {
Key string
RangeOptions
// TODO: Implement stale read using revision
revision int64
}
type RangeOptions struct {
WithPrefix bool
Limit int64
}
type PutOptions struct {
Value ValueOrHash
LeaseID int64
}
type TxnRequest struct {
Conditions []EtcdCondition
OperationsOnSuccess []EtcdOperation
@ -281,12 +317,10 @@ type EtcdCondition struct {
}
type EtcdOperation struct {
Type OperationType
Key string
WithPrefix bool
Limit int64
Value ValueOrHash
LeaseID int64
Type OperationType
Key string
RangeOptions
PutOptions
}
type OperationType string
@ -308,6 +342,7 @@ type DefragmentRequest struct{}
type EtcdResponse struct {
Revision int64
Txn *TxnResponse
Range *RangeResponse
LeaseGrant *LeaseGrantReponse
LeaseRevoke *LeaseRevokeResponse
Defragment *DefragmentResponse
@ -318,6 +353,11 @@ type TxnResponse struct {
Results []EtcdOperationResult
}
type RangeResponse struct {
KVs []KeyValue
Count int64
}
type LeaseGrantReponse struct {
LeaseID int64
}
@ -325,8 +365,7 @@ type LeaseRevokeResponse struct{}
type DefragmentResponse struct{}
type EtcdOperationResult struct {
KVs []KeyValue
Count int64
RangeResponse
Deleted int64
}

View File

@ -252,9 +252,9 @@ func toEtcdOperation(op clientv3.Op) EtcdOperation {
panic("Unsupported operation")
}
return EtcdOperation{
Type: opType,
Key: string(op.KeyBytes()),
Value: ValueOrHash{Value: string(op.ValueBytes())},
Type: opType,
Key: string(op.KeyBytes()),
PutOptions: PutOptions{Value: ValueOrHash{Value: string(op.ValueBytes())}},
}
}
@ -273,8 +273,10 @@ func toEtcdOperationResult(resp *etcdserverpb.ResponseOp) EtcdOperationResult {
}
}
return EtcdOperationResult{
KVs: kvs,
Count: getResp.Count,
RangeResponse: RangeResponse{
KVs: kvs,
Count: getResp.Count,
},
}
case resp.GetResponsePut() != nil:
return EtcdOperationResult{}
@ -339,7 +341,7 @@ func getRequest(key string) EtcdRequest {
}
func rangeRequest(key string, withPrefix bool, limit int64) EtcdRequest {
return EtcdRequest{Type: Txn, Txn: &TxnRequest{OperationsOnSuccess: []EtcdOperation{{Type: RangeOperation, Key: key, WithPrefix: withPrefix, Limit: limit}}}}
return EtcdRequest{Type: Range, Range: &RangeRequest{Key: key, RangeOptions: RangeOptions{WithPrefix: withPrefix, Limit: limit}}}
}
func emptyGetResponse(revision int64) EtcdNonDeterministicResponse {
@ -351,7 +353,7 @@ func getResponse(key, value string, modRevision, revision int64) EtcdNonDetermin
}
func rangeResponse(kvs []*mvccpb.KeyValue, count int64, revision int64) EtcdNonDeterministicResponse {
result := EtcdOperationResult{KVs: make([]KeyValue, len(kvs))}
result := RangeResponse{KVs: make([]KeyValue, len(kvs)), Count: count}
for i, kv := range kvs {
result.KVs[i] = KeyValue{
@ -361,9 +363,8 @@ func rangeResponse(kvs []*mvccpb.KeyValue, count int64, revision int64) EtcdNonD
ModRevision: kv.ModRevision,
},
}
result.Count = count
}
return EtcdNonDeterministicResponse{EtcdResponse: EtcdResponse{Txn: &TxnResponse{Results: []EtcdOperationResult{result}}, Revision: revision}}
return EtcdNonDeterministicResponse{EtcdResponse: EtcdResponse{Range: &result, Revision: revision}}
}
func failedResponse(err error) EtcdNonDeterministicResponse {
@ -375,7 +376,7 @@ func unknownResponse(revision int64) EtcdNonDeterministicResponse {
}
func putRequest(key, value string) EtcdRequest {
return EtcdRequest{Type: Txn, Txn: &TxnRequest{OperationsOnSuccess: []EtcdOperation{{Type: PutOperation, Key: key, Value: ToValueOrHash(value)}}}}
return EtcdRequest{Type: Txn, Txn: &TxnRequest{OperationsOnSuccess: []EtcdOperation{{Type: PutOperation, Key: key, PutOptions: PutOptions{Value: ToValueOrHash(value)}}}}}
}
func putResponse(revision int64) EtcdNonDeterministicResponse {
@ -406,7 +407,7 @@ func compareRevision(key string, expectedRevision int64) *EtcdCondition {
}
func putOperation(key, value string) *EtcdOperation {
return &EtcdOperation{Type: PutOperation, Key: key, Value: ToValueOrHash(value)}
return &EtcdOperation{Type: PutOperation, Key: key, PutOptions: PutOptions{Value: ToValueOrHash(value)}}
}
func txnRequestSingleOperation(cond *EtcdCondition, onSuccess, onFailure *EtcdOperation) EtcdRequest {
@ -442,7 +443,7 @@ func txnResponse(result []EtcdOperationResult, succeeded bool, revision int64) E
}
func putWithLeaseRequest(key, value string, leaseID int64) EtcdRequest {
return EtcdRequest{Type: Txn, Txn: &TxnRequest{OperationsOnSuccess: []EtcdOperation{{Type: PutOperation, Key: key, Value: ToValueOrHash(value), LeaseID: leaseID}}}}
return EtcdRequest{Type: Txn, Txn: &TxnRequest{OperationsOnSuccess: []EtcdOperation{{Type: PutOperation, Key: key, PutOptions: PutOptions{Value: ToValueOrHash(value), LeaseID: leaseID}}}}}
}
func leaseGrantRequest(leaseID int64) EtcdRequest {

View File

@ -273,9 +273,9 @@ func toWatchEvent(event clientv3.Event) WatchEvent {
return WatchEvent{
Revision: event.Kv.ModRevision,
Op: model.EtcdOperation{
Type: op,
Key: string(event.Kv.Key),
Value: model.ToValueOrHash(string(event.Kv.Value)),
Type: op,
Key: string(event.Kv.Key),
PutOptions: model.PutOptions{Value: model.ToValueOrHash(string(event.Kv.Value))},
},
}
}

View File

@ -76,9 +76,11 @@ func matchWatchEvent(request *model.TxnRequest, watchEvents map[model.EtcdOperat
if etcdOp.Type == model.PutOperation {
// Remove LeaseID which is not exposed in watch.
event, ok := watchEvents[model.EtcdOperation{
Type: etcdOp.Type,
Key: etcdOp.Key,
Value: etcdOp.Value,
Type: etcdOp.Type,
Key: etcdOp.Key,
PutOptions: model.PutOptions{
Value: etcdOp.Value,
},
}]
if ok {
return &event