Repository: etcd-io/etcd
File: tests/robustness/model/history.go (Go)

Code before:
func (h *AppendableHistory) appendFailed(request EtcdRequest, start, end time.Duration, err error) {
	op := porcupine.Operation{
		ClientId: h.streamID,
		Input:    request,
		Call:     start.Nanoseconds(),
		Output:   failedResponse(err),
		Return:   end.Nanoseconds(),
	}
	isRead := request.IsRead()
	if !isRead {
		// Failed writes can still be persisted, setting -1 for now as don't know when request has took effect.
		op.Return = -1
		// 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 stream id.
		h.streamID = h.idProvider.NewStreamID()
	}
	h.append(op)
}

func (h *AppendableHistory) append(op porcupine.Operation) {
	if op.Return != -1 && op.Call >= op.Return {
		panic(fmt.Sprintf("Invalid operation, call(%d) >= return(%d)", op.Call, op.Return))
	}
	if len(h.operations) > 0 {
		prev := h.operations[len(h.operations)-1]
		if op.Call <= prev.Call {
			panic(fmt.Sprintf("Out of order append, new.call(%d) <= prev.call(%d)", op.Call, prev.Call))
		}
		if op.Call <= prev.Return {
			panic(fmt.Sprintf("Overlapping operations, new.call(%d) <= prev.return(%d)", op.Call, prev.Return))
		}
	}
	h.operations = append(h.operations, op)
}

func getRequest(key string) EtcdRequest {
	return rangeRequest(key, "", 0)
}

func staleGetRequest(key string, revision int64) EtcdRequest {
	return staleRangeRequest(key, "", 0, revision)
}

func rangeRequest(start, end string, limit int64) EtcdRequest {
	return staleRangeRequest(start, end, limit, 0)
}

func listRequest(key string, limit int64) EtcdRequest {
	return staleListRequest(key, limit, 0)
}


Review comment (bug):
I think in the end this conditional check is enough, instead of the need of using the map as we discussed offline, because the only place that the `ClientId` would be incremented is in `appendFailed` (code snippet attached below)

```
func (h *AppendableHistory) appendFailed(request EtcdRequest, start, end time.Duration, err error) {
	op := porcupine.Operation{
		ClientId: h.streamID, /* ClientId is set here */
		Input:    request,
		Call:     start.Nanoseconds(),
		Output:   failedResponse(err),
		Return:   end.Nanoseconds(),
	}
	isRead := request.IsRead()
	if !isRead {
		op.Return = math.MaxInt64
		h.streamID = h.idProvider.NewStreamID() /* ClientId is incremented here only in the entire test */
	}
	h.append(op)
}
```

Code after:

func (h *AppendableHistory) appendFailed(request EtcdRequest, start, end time.Duration, err error) {
	op := porcupine.Operation{
		ClientId: h.streamID,
		Input:    request,
		Call:     start.Nanoseconds(),
		Output:   failedResponse(err),
		Return:   end.Nanoseconds(),
	}
	isRead := request.IsRead()
	if !isRead {
		// Failed writes can still be persisted, setting -1 for now as don't know when request has took effect.
		op.Return = math.MaxInt64
		// 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 stream id.
		h.streamID = h.idProvider.NewStreamID()
	}
	h.append(op)
}

func (h *AppendableHistory) append(op porcupine.Operation) {
	if op.Return != math.MaxInt64 && op.Call >= op.Return {
		panic(fmt.Sprintf("Invalid operation, call(%d) >= return(%d)", op.Call, op.Return))
	}

	if len(h.operations) > 0 && op.ClientId == h.operations[len(h.operations)-1].ClientId {
		prev := h.operations[len(h.operations)-1]
		if op.Call <= prev.Call {
			panic(fmt.Sprintf("Out of order append, new.call(%d) <= prev.call(%d)", op.Call, prev.Call))
		}
		if op.Call <= prev.Return {
			panic(fmt.Sprintf("Overlapping operations, new.call(%d) <= prev.return(%d)", op.Call, prev.Return))
		}
	}
	h.operations = append(h.operations, op)
}

func getRequest(key string) EtcdRequest {
	return rangeRequest(key, "", 0)
}

func staleGetRequest(key string, revision int64) EtcdRequest {
	return staleRangeRequest(key, "", 0, revision)
}

func rangeRequest(start, end string, limit int64) EtcdRequest {
	return staleRangeRequest(start, end, limit, 0)
}

func listRequest(key string, limit int64) EtcdRequest {
	return staleListRequest(key, limit, 0)