Skip to content

Commit

Permalink
tests: Fix return time for failed requests globally
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 f54c3e1 commit ab7c4e0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
4 changes: 2 additions & 2 deletions tests/linearizability/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

type recordingClient struct {
client clientv3.Client
history *history
history *appendableHistory
}

func NewClient(endpoints []string, ids idProvider) (*recordingClient, error) {
Expand All @@ -39,7 +39,7 @@ func NewClient(endpoints []string, ids idProvider) (*recordingClient, error) {
}
return &recordingClient{
client: *cc,
history: NewHistory(ids),
history: newAppendableHistory(ids),
}, nil
}

Expand Down
48 changes: 34 additions & 14 deletions tests/linearizability/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,31 @@ import (
clientv3 "go.etcd.io/etcd/client/v3"
)

type history struct {
type appendableHistory 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
history
}

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

func (h *history) AppendGet(key string, start, end time.Time, resp *clientv3.GetResponse) {
func (h *appendableHistory) 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{
h.successful = append(h.successful, porcupine.Operation{
ClientId: h.id,
Input: EtcdRequest{Op: Get, Key: key},
Call: start.UnixNano(),
Expand All @@ -53,7 +54,7 @@ func (h *history) AppendGet(key string, start, end time.Time, resp *clientv3.Get
})
}

func (h *history) AppendPut(key, value string, start, end time.Time, resp *clientv3.PutResponse, err error) {
func (h *appendableHistory) 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,
Expand All @@ -65,12 +66,13 @@ func (h *history) AppendPut(key, value string, start, end time.Time, resp *clien
// 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()
return
}
var revision int64
if resp != nil && resp.Header != nil {
revision = resp.Header.Revision
}
h.operations = append(h.operations, porcupine.Operation{
h.successful = append(h.successful, porcupine.Operation{
ClientId: h.id,
Input: EtcdRequest{Op: Put, Key: key, PutData: value},
Call: start.UnixNano(),
Expand All @@ -79,10 +81,28 @@ func (h *history) AppendPut(key, value string, start, end time.Time, resp *clien
})
}

func (h *history) Operations() []porcupine.Operation {
operations := make([]porcupine.Operation, 0, len(h.operations)+len(h.failed))
type history struct {
successful []porcupine.Operation
// failed requests are kept separate as we don't know return time of failed operations.
failed []porcupine.Operation
}

func (h history) Merge(h2 history) history {
result := history{
successful: make([]porcupine.Operation, 0, len(h.successful)+len(h2.successful)),
failed: make([]porcupine.Operation, 0, len(h.failed)+len(h2.failed)),
}
result.successful = append(result.successful, h.successful...)
result.successful = append(result.successful, h2.successful...)
result.failed = append(result.failed, h.failed...)
result.failed = append(result.failed, h2.failed...)
return result
}

func (h history) Operations() []porcupine.Operation {
operations := make([]porcupine.Operation, 0, len(h.successful)+len(h.failed))
var maxTime int64
for _, op := range h.operations {
for _, op := range h.successful {
operations = append(operations, op)
if op.Return > maxTime {
maxTime = op.Return
Expand Down
6 changes: 4 additions & 2 deletions tests/linearizability/linearizability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ type FailpointConfig struct {
waitBetweenTriggers time.Duration
}

func simulateTraffic(ctx context.Context, t *testing.T, clus *e2e.EtcdProcessCluster, config trafficConfig) (operations []porcupine.Operation) {
func simulateTraffic(ctx context.Context, t *testing.T, clus *e2e.EtcdProcessCluster, config trafficConfig) []porcupine.Operation {
mux := sync.Mutex{}
endpoints := clus.EndpointsV3()

ids := newIdProvider()
h := history{}
limiter := rate.NewLimiter(rate.Limit(config.maximalQPS), 200)

startTime := time.Now()
Expand All @@ -162,12 +163,13 @@ func simulateTraffic(ctx context.Context, t *testing.T, clus *e2e.EtcdProcessClu

config.traffic.Run(ctx, c, limiter, ids)
mux.Lock()
operations = append(operations, c.history.Operations()...)
h = h.Merge(c.history.history)
mux.Unlock()
}(c)
}
wg.Wait()
endTime := time.Now()
operations := h.Operations()
t.Logf("Recorded %d operations", len(operations))

qps := float64(len(operations)) / float64(endTime.Sub(startTime)) * float64(time.Second)
Expand Down

0 comments on commit ab7c4e0

Please sign in to comment.