Skip to content

Commit

Permalink
tests: Extract history from client
Browse files Browse the repository at this point in the history
Signed-off-by: Marek Siarkowicz <siarkowicz@google.com>
  • Loading branch information
serathius committed Dec 5, 2022
1 parent 3d2797d commit ec72022
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 70 deletions.
76 changes: 7 additions & 69 deletions tests/linearizability/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@ import (
"context"
"time"

"github.com/anishathalye/porcupine"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
)

type recordingClient struct {
client clientv3.Client

// id of the next write operation. If needed a new id might be requested from idProvider.
id int
idProvider idProvider

operations []porcupine.Operation
failed []porcupine.Operation
client clientv3.Client
history *history
}

func NewClient(endpoints []string, ids idProvider) (*recordingClient, error) {
Expand All @@ -45,11 +38,8 @@ func NewClient(endpoints []string, ids idProvider) (*recordingClient, error) {
return nil, err
}
return &recordingClient{
client: *cc,
id: ids.ClientId(),
idProvider: ids,
operations: []porcupine.Operation{},
failed: []porcupine.Operation{},
client: *cc,
history: NewHistory(ids),
}, nil
}

Expand All @@ -64,66 +54,14 @@ func (c *recordingClient) Get(ctx context.Context, key string) error {
if err != nil {
return err
}
var readData string
if len(resp.Kvs) == 1 {
readData = string(resp.Kvs[0].Value)
}
c.operations = append(c.operations, porcupine.Operation{
ClientId: c.id,
Input: EtcdRequest{Op: Get, Key: key},
Call: callTime.UnixNano(),
Output: EtcdResponse{GetData: readData, Revision: resp.Header.Revision},
Return: returnTime.UnixNano(),
})
c.history.AppendGet(key, callTime, returnTime, resp)
return nil
}

func (c *recordingClient) Put(ctx context.Context, key, value string) error {
callTime := time.Now()
resp, err := c.client.Put(ctx, key, value)
returnTime := time.Now()
if err != nil {
c.failed = append(c.failed, porcupine.Operation{
ClientId: c.id,
Input: EtcdRequest{Op: Put, Key: key, PutData: value},
Call: callTime.UnixNano(),
Output: EtcdResponse{Err: err},
Return: 0, // For failed writes we don't know when request has really finished.
})
// Operations of single client needs to be sequential.
// As we don't know return time of failed operations, all new writes need to be done with new client id.
c.id = c.idProvider.ClientId()
return err
}
var revision int64
if resp != nil && resp.Header != nil {
revision = resp.Header.Revision
}
c.operations = append(c.operations, porcupine.Operation{
ClientId: c.id,
Input: EtcdRequest{Op: Put, Key: key, PutData: value},
Call: callTime.UnixNano(),
Output: EtcdResponse{Err: err, Revision: revision},
Return: returnTime.UnixNano(),
})
return nil
}

func (c *recordingClient) Operations() []porcupine.Operation {
operations := make([]porcupine.Operation, 0, len(c.operations)+len(c.failed))
var maxTime int64
for _, op := range c.operations {
operations = append(operations, op)
if op.Return > maxTime {
maxTime = op.Return
}
}
for _, op := range c.failed {
if op.Call > maxTime {
continue
}
op.Return = maxTime + 1
operations = append(operations, op)
}
return operations
c.history.AppendPut(key, value, callTime, returnTime, resp, err)
return err
}
99 changes: 99 additions & 0 deletions tests/linearizability/history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2022 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package linearizability

import (
"time"

"github.com/anishathalye/porcupine"
clientv3 "go.etcd.io/etcd/client/v3"
)

type history struct {
// id of the next write operation. If needed a new id might be requested from idProvider.
id int
idProvider idProvider

operations []porcupine.Operation
failed []porcupine.Operation
}

func NewHistory(ids idProvider) *history {
return &history{
id: ids.ClientId(),
idProvider: ids,
operations: []porcupine.Operation{},
failed: []porcupine.Operation{},
}
}

func (h *history) AppendGet(key string, start, end time.Time, resp *clientv3.GetResponse) {
var readData string
if len(resp.Kvs) == 1 {
readData = string(resp.Kvs[0].Value)
}
h.operations = append(h.operations, porcupine.Operation{
ClientId: h.id,
Input: EtcdRequest{Op: Get, Key: key},
Call: start.UnixNano(),
Output: EtcdResponse{GetData: readData, Revision: resp.Header.Revision},
Return: end.UnixNano(),
})
}

func (h *history) AppendPut(key, value string, start, end time.Time, resp *clientv3.PutResponse, err error) {
if err != nil {
h.failed = append(h.failed, porcupine.Operation{
ClientId: h.id,
Input: EtcdRequest{Op: Put, Key: key, PutData: value},
Call: start.UnixNano(),
Output: EtcdResponse{Err: err},
Return: 0, // For failed writes we don't know when request has really finished.
})
// Operations of single client needs to be sequential.
// As we don't know return time of failed operations, all new writes need to be done with new client id.
h.id = h.idProvider.ClientId()
}
var revision int64
if resp != nil && resp.Header != nil {
revision = resp.Header.Revision
}
h.operations = append(h.operations, porcupine.Operation{
ClientId: h.id,
Input: EtcdRequest{Op: Put, Key: key, PutData: value},
Call: start.UnixNano(),
Output: EtcdResponse{Err: err, Revision: revision},
Return: end.UnixNano(),
})
}

func (h *history) Operations() []porcupine.Operation {
operations := make([]porcupine.Operation, 0, len(h.operations)+len(h.failed))
var maxTime int64
for _, op := range h.operations {
operations = append(operations, op)
if op.Return > maxTime {
maxTime = op.Return
}
}
for _, op := range h.failed {
if op.Call > maxTime {
continue
}
op.Return = maxTime + 1
operations = append(operations, op)
}
return operations
}
2 changes: 1 addition & 1 deletion tests/linearizability/linearizability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func simulateTraffic(ctx context.Context, t *testing.T, clus *e2e.EtcdProcessClu

config.traffic.Run(ctx, c, limiter, ids)
mux.Lock()
operations = append(operations, c.Operations()...)
operations = append(operations, c.history.Operations()...)
mux.Unlock()
}(c)
}
Expand Down

0 comments on commit ec72022

Please sign in to comment.