ronantakizawa/codereview-bench huggingface dataset:
Repository: etcd-io/etcd
File: server/auth/simple_token.go (Go)

Code before:
	for i := 0; i < defaultSimpleTokenLength; i++ {
		bInt, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
		if err != nil {
			return "", err
		}

		ret[i] = letters[bInt.Int64()]
	}

	return string(ret), nil
}

func (t *tokenSimple) assignSimpleTokenToUser(username, token string) {
	t.simpleTokensMu.Lock()
	defer t.simpleTokensMu.Unlock()
	if t.simpleTokenKeeper == nil {
		return
	}

	_, ok := t.simpleTokens[token]
	if ok {
		t.lg.Panic(
			"failed to assign already-used simple token to a user",
			zap.String("user-name", username),
			zap.String("token", token),
		)
	}

	t.simpleTokens[token] = username
	t.simpleTokenKeeper.addSimpleToken(token)
}

func (t *tokenSimple) invalidateUser(username string) {
	if t.simpleTokenKeeper == nil {
		return
	}
	t.simpleTokensMu.Lock()
	for token, name := range t.simpleTokens {
		if name == username {
			delete(t.simpleTokens, token)
			t.simpleTokenKeeper.deleteSimpleToken(token)
		}
	}
	t.simpleTokensMu.Unlock()
}

func (t *tokenSimple) enable() {
	t.simpleTokensMu.Lock()
	defer t.simpleTokensMu.Unlock()
	if t.simpleTokenKeeper != nil { // already enabled
		return

Review comment (suggestion):
Quick question: Do end users ever consume this value? If not, and the only goal is to keep it out of logs, we could just emit "REDACT" instead.

Code after:
	for i := 0; i < defaultSimpleTokenLength; i++ {
		bInt, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
		if err != nil {
			return "", err
		}

		ret[i] = letters[bInt.Int64()]
	}

	return string(ret), nil
}

func (t *tokenSimple) assignSimpleTokenToUser(username, token string) {
	t.simpleTokensMu.Lock()
	defer t.simpleTokensMu.Unlock()
	if t.simpleTokenKeeper == nil {
		return
	}

	_, ok := t.simpleTokens[token]
	if ok {
		tokenFingerprint := redactToken(token)
		t.lg.Panic(
			"failed to assign already-used simple token to a user",
			zap.String("user-name", username),
			zap.String("token-fingerprint", tokenFingerprint),
		)
	}

	t.simpleTokens[token] = username
	t.simpleTokenKeeper.addSimpleToken(token)
}

func (t *tokenSimple) invalidateUser(username string) {
	if t.simpleTokenKeeper == nil {
		return
	}
	t.simpleTokensMu.Lock()
	for token, name := range t.simpleTokens {
		if name == username {
			delete(t.simpleTokens, token)
			t.simpleTokenKeeper.deleteSimpleToken(token)
		}
	}
	t.simpleTokensMu.Unlock()
}

func (t *tokenSimple) enable() {
	t.simpleTokensMu.Lock()
	defer t.simpleTokensMu.Unlock()
	if t.simpleTokenKeeper != nil { // already enabled
Repository: etcd-io/etcd
File: tests/e2e/reproduce_20967_test.go (Go)

Code before:
package e2e

import (
	"encoding/json"
	"fmt"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
	"go.etcd.io/bbolt"
	"go.etcd.io/etcd/server/v3/datadir"
	"go.etcd.io/etcd/server/v3/etcdserver/api/membership"
	"go.etcd.io/etcd/server/v3/mvcc/buckets"
	"go.etcd.io/etcd/tests/v3/framework/e2e"
)

func TestIssue20967_Upgrade(t *testing.T) {
	e2e.BeforeTest(t)

	snapshotCount := 20

	t.Log("Creating cluster with zombie members...")
	epc := createClustewithZombieMembers(t, snapshotCount)

	t.Log("Upgradeing cluster to the new version...")
	for _, proc := range epc.Procs {
		serverCfg := proc.Config()

		t.Logf("Stopping node: %v", serverCfg.Name)
		require.NoError(t, proc.Stop(), "error closing etcd process (%v)", serverCfg.Name)

		serverCfg.ExecPath = e2e.BinPath

Review comment (suggestion):
curious why the golangci-lint workflow did not detect this minor issue? cc @ivanvc @joshjms 
```suggestion
	"github.com/stretchr/testify/require"

```

Code after:
package e2e

import (
	"encoding/json"
	"fmt"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"

	"go.etcd.io/bbolt"
	clientv3 "go.etcd.io/etcd/client/v3"
	"go.etcd.io/etcd/server/v3/datadir"
	"go.etcd.io/etcd/server/v3/etcdserver/api/membership"
	"go.etcd.io/etcd/server/v3/mvcc/buckets"
	"go.etcd.io/etcd/tests/v3/framework/e2e"
)

func TestIssue20967_Upgrade(t *testing.T) {
	e2e.BeforeTest(t)

	snapshotCount := 20

	t.Log("Creating cluster with zombie members...")
	epc := createClustewithZombieMembers(t, snapshotCount)

	t.Log("Upgradeing cluster to the new version...")
	for _, proc := range epc.Procs {
		serverCfg := proc.Config()

		t.Logf("Stopping node: %v", serverCfg.Name)
		require.NoError(t, proc.Stop(), "error closing etcd process (%v)", serverCfg.Name)

		serverCfg.ExecPath = e2e.BinPath
Repository: etcd-io/etcd
File: tests/robustness/validate/patch_history.go (Go)

Code before:
						// So we can't reliably attribute them to explicit delete operations
					case model.CompactOperation:
					default:
						panic(fmt.Sprintf("unknown event type %q", event.Type))
					}
				}
			}
		}
	}
	return putRevisions
}

func patchOperations(
	operations []porcupine.Operation,
	putArgs map[model.PutOptions]patchArgs,
	delArgs map[model.DeleteOptions]patchArgs,
	compactArgs map[model.CompactOptions]patchArgs,
) []porcupine.Operation {
	newOperations := make([]porcupine.Operation, 0, len(operations))

	for _, op := range operations {
		request := op.Input.(model.EtcdRequest)
		resp := op.Output.(model.MaybeEtcdResponse)

		if request.Type == model.Compact {
			kv := model.CompactOptions{Revision: request.Compact.Revision}
			if arg, ok := compactArgs[kv]; ok && arg.clientCount == 1 && arg.returnTime > 0 {
				op.Return = min(op.Return, arg.returnTime)
			}
			newOperations = append(newOperations, op)
			continue
		}

		if resp.Error == "" || request.Type != model.Txn {
			// Cannot patch those requests.
			newOperations = append(newOperations, op)
			continue
		}
		var txnRevision int64
		var persisted bool
		for _, etcdOp := range request.Txn.AllOperations() {
			switch etcdOp.Type {
			case model.PutOperation:
				kv := model.PutOptions{Key: etcdOp.Put.Key, Value: etcdOp.Put.Value}
				arg, ok := putArgs[kv]
				if !ok {
					continue
				}
				if arg.persistedCount > 0 {
					persisted = true
				}

Review comment (suggestion):
```suggestion
			kv := model.CompactRequest{Revision: request.Compact.Revision}
```

Code after:
					default:
						panic(fmt.Sprintf("unknown event type %q", event.Type))
					}
				}
			}
		}
	}
	return putRevisions
}

func patchOperations(
	operations []porcupine.Operation,
	putArgs map[model.PutOptions]patchArgs,
	delArgs map[model.DeleteOptions]patchArgs,
	compactArgs map[model.CompactRequest]patchArgs,
) []porcupine.Operation {
	newOperations := make([]porcupine.Operation, 0, len(operations))

	for _, op := range operations {
		request := op.Input.(model.EtcdRequest)
		resp := op.Output.(model.MaybeEtcdResponse)

		if request.Type == model.Compact {
			kv := model.CompactRequest{Revision: request.Compact.Revision}
			if arg, ok := compactArgs[kv]; ok && arg.clientCount == 1 {
				if arg.persistedCount == 0 && resp.Error != "" {
					// the failed compact should be dropped
					continue
				}
				if arg.returnTime > 0 {
					op.Return = min(op.Return, arg.returnTime)
				}
			}
			newOperations = append(newOperations, op)
			continue
		}

		if resp.Error == "" || request.Type != model.Txn {
			// Cannot patch those requests.
			newOperations = append(newOperations, op)
			continue
		}
		var txnRevision int64
		var persisted bool
		for _, etcdOp := range request.Txn.AllOperations() {
			switch etcdOp.Type {
			case model.PutOperation:
				kv := model.PutOptions{Key: etcdOp.Put.Key, Value: etcdOp.Put.Value}
				arg, ok := putArgs[kv]
				if !ok {
					continue
Repository: etcd-io/etcd
File: tests/robustness/validate/patch_history.go (Go)

Code before:
		case model.Txn:
			for _, etcdOp := range request.Txn.AllOperations() {
				switch etcdOp.Type {
				case model.PutOperation:
					kv := model.PutOptions{Key: etcdOp.Put.Key, Value: etcdOp.Put.Value}
					if clientPutCount[kv] > 1 {
						continue
					}
					if returnTime, ok := putTimes[kv]; !ok || returnTime > op.Return {
						putTimes[kv] = op.Return
					}
				case model.DeleteOperation:
					kv := model.DeleteOptions{Key: etcdOp.Delete.Key}
					if clientDeleteCount[kv] > 1 {
						continue
					}
					if returnTime, ok := delTimes[kv]; !ok || returnTime > op.Return {
						delTimes[kv] = op.Return
					}
				}
			}
		case model.Range:
		case model.LeaseGrant:
		case model.LeaseRevoke:
		case model.Compact:
			kv := model.CompactOptions{Revision: request.Compact.Revision}
			if clientCompactCount[kv] > 1 {
				continue
			}
			if returnTime, ok := compactTimes[kv]; !ok || returnTime > op.Return {
				compactTimes[kv] = op.Return
			}
		case model.Defragment:
		default:
			panic(fmt.Sprintf("Unknown request type: %q", request.Type))
		}
		if op.Return > lastReturnTime {
			lastReturnTime = op.Return
		}
	}

	for i := len(persistedRequests) - 1; i >= 0; i-- {
		request := persistedRequests[i]
		switch request.Type {
		case model.Txn:
			if lastReturnTime != math.MaxInt64 {
				lastReturnTime--
			}
			for _, op := range request.Txn.AllOperations() {
				switch op.Type {
				case model.PutOperation:

Review comment (suggestion):
```suggestion
			kv := model.CompactRequest{Revision: request.Compact.Revision}
```

Code after:
			for _, etcdOp := range request.Txn.AllOperations() {
				switch etcdOp.Type {
				case model.PutOperation:
					kv := model.PutOptions{Key: etcdOp.Put.Key, Value: etcdOp.Put.Value}
					if clientPutCount[kv] > 1 {
						continue
					}
					if returnTime, ok := putTimes[kv]; !ok || returnTime > op.Return {
						putTimes[kv] = op.Return
					}
				case model.DeleteOperation:
					kv := model.DeleteOptions{Key: etcdOp.Delete.Key}
					if clientDeleteCount[kv] > 1 {
						continue
					}
					if returnTime, ok := delTimes[kv]; !ok || returnTime > op.Return {
						delTimes[kv] = op.Return
					}
				}
			}
		case model.Range:
		case model.LeaseGrant:
		case model.LeaseRevoke:
		case model.Compact:
			if clientCompactCount[*request.Compact] > 1 {
				continue
			}
			if returnTime, ok := compactTimes[*request.Compact]; !ok || returnTime > op.Return {
				compactTimes[*request.Compact] = op.Return
			}
		case model.Defragment:
		default:
			panic(fmt.Sprintf("Unknown request type: %q", request.Type))
		}
		if op.Return > lastReturnTime {
			lastReturnTime = op.Return
		}
	}

	for i := len(persistedRequests) - 1; i >= 0; i-- {
		request := persistedRequests[i]
		switch request.Type {
		case model.Txn:
			if lastReturnTime != math.MaxInt64 {
				lastReturnTime--
			}
			for _, op := range request.Txn.AllOperations() {
				switch op.Type {
				case model.PutOperation:
					kv := model.PutOptions{Key: op.Put.Key, Value: op.Put.Value}
					if clientPutCount[kv] > 1 {
Repository: etcd-io/etcd
File: tests/robustness/validate/patch_history.go (Go)

Code before:
		}
	}
	return counter
}

func countPersistedDeletes(requests []model.EtcdRequest) map[model.DeleteOptions]int64 {
	counter := map[model.DeleteOptions]int64{}
	for _, req := range requests {
		countDeletes(counter, req)
	}
	return counter
}

func countDeletes(counter map[model.DeleteOptions]int64, request model.EtcdRequest) {
	if request.Type != model.Txn {
		return
	}
	for _, operation := range request.Txn.AllOperations() {
		if operation.Type == model.DeleteOperation {
			counter[operation.Delete]++
		}
	}
}

func countClientCompacts(reports []report.ClientReport) map[model.CompactOptions]int64 {
	counter := map[model.CompactOptions]int64{}
	for _, client := range reports {
		for _, op := range client.KeyValue {
			request := op.Input.(model.EtcdRequest)
			countCompacts(counter, request)
		}
	}
	return counter
}

func countPersistedCompacts(requests []model.EtcdRequest) map[model.CompactOptions]int64 {
	counter := map[model.CompactOptions]int64{}
	for _, req := range requests {
		countCompacts(counter, req)
	}
	return counter
}

func countCompacts(counter map[model.CompactOptions]int64, request model.EtcdRequest) {
	if request.Type == model.Compact {
		opts := model.CompactOptions{
			Revision: request.Compact.Revision,
		}
		counter[opts]++
	}
}

Review comment (suggestion):
```suggestion
func countClientCompacts(reports []report.ClientReport) map[model.CompactRequest]int64 {
	counter := map[model.CompactRequest]int64{}
```

Code after:

func countPersistedDeletes(requests []model.EtcdRequest) map[model.DeleteOptions]int64 {
	counter := map[model.DeleteOptions]int64{}
	for _, req := range requests {
		countDeletes(counter, req)
	}
	return counter
}

func countDeletes(counter map[model.DeleteOptions]int64, request model.EtcdRequest) {
	if request.Type != model.Txn {
		return
	}
	for _, operation := range request.Txn.AllOperations() {
		if operation.Type == model.DeleteOperation {
			counter[operation.Delete]++
		}
	}
}

func countClientCompacts(reports []report.ClientReport) map[model.CompactRequest]int64 {
	counter := map[model.CompactRequest]int64{}
	for _, client := range reports {
		for _, op := range client.KeyValue {
			request := op.Input.(model.EtcdRequest)
			countCompacts(counter, request)
		}
	}
	return counter
}

func countPersistedCompacts(requests []model.EtcdRequest) map[model.CompactRequest]int64 {
	counter := map[model.CompactRequest]int64{}
	for _, req := range requests {
		countCompacts(counter, req)
	}
	return counter
}

func countCompacts(counter map[model.CompactRequest]int64, request model.EtcdRequest) {
	if request.Type == model.Compact {
		counter[*request.Compact]++
	}
}
Repository: etcd-io/etcd
File: tests/robustness/validate/patch_history.go (Go)

Code before:
}

func countDeletes(counter map[model.DeleteOptions]int64, request model.EtcdRequest) {
	if request.Type != model.Txn {
		return
	}
	for _, operation := range request.Txn.AllOperations() {
		if operation.Type == model.DeleteOperation {
			counter[operation.Delete]++
		}
	}
}

func countClientCompacts(reports []report.ClientReport) map[model.CompactOptions]int64 {
	counter := map[model.CompactOptions]int64{}
	for _, client := range reports {
		for _, op := range client.KeyValue {
			request := op.Input.(model.EtcdRequest)
			countCompacts(counter, request)
		}
	}
	return counter
}

func countPersistedCompacts(requests []model.EtcdRequest) map[model.CompactOptions]int64 {
	counter := map[model.CompactOptions]int64{}
	for _, req := range requests {
		countCompacts(counter, req)
	}
	return counter
}

func countCompacts(counter map[model.CompactOptions]int64, request model.EtcdRequest) {
	if request.Type == model.Compact {
		opts := model.CompactOptions{
			Revision: request.Compact.Revision,
		}
		counter[opts]++
	}
}


Review comment (suggestion):
```suggestion
func countPersistedCompacts(requests []model.EtcdRequest) map[model.CompactRequest]int64 {
	counter := map[model.CompactRequest]int64{}
```

Code after:
		return
	}
	for _, operation := range request.Txn.AllOperations() {
		if operation.Type == model.DeleteOperation {
			counter[operation.Delete]++
		}
	}
}

func countClientCompacts(reports []report.ClientReport) map[model.CompactRequest]int64 {
	counter := map[model.CompactRequest]int64{}
	for _, client := range reports {
		for _, op := range client.KeyValue {
			request := op.Input.(model.EtcdRequest)
			countCompacts(counter, request)
		}
	}
	return counter
}

func countPersistedCompacts(requests []model.EtcdRequest) map[model.CompactRequest]int64 {
	counter := map[model.CompactRequest]int64{}
	for _, req := range requests {
		countCompacts(counter, req)
	}
	return counter
}

func countCompacts(counter map[model.CompactRequest]int64, request model.EtcdRequest) {
	if request.Type == model.Compact {
		counter[*request.Compact]++
	}
}
Repository: etcd-io/etcd
File: tests/robustness/validate/patch_history.go (Go)

Code before:
					kv := model.PutOptions{Key: op.Put.Key, Value: op.Put.Value}
					if clientPutCount[kv] > 1 {
						continue
					}
					if returnTime, ok := putTimes[kv]; ok {
						lastReturnTime = min(returnTime, lastReturnTime)
						putTimes[kv] = lastReturnTime
					}
				case model.DeleteOperation:
					kv := model.DeleteOptions{Key: op.Delete.Key}
					if clientDeleteCount[kv] > 1 {
						continue
					}
					if returnTime, ok := delTimes[kv]; ok {
						lastReturnTime = min(returnTime, lastReturnTime)
						delTimes[kv] = lastReturnTime
					}
				}
			}
		case model.LeaseGrant:
		case model.LeaseRevoke:
		case model.Compact:
			if lastReturnTime != math.MaxInt64 {
				lastReturnTime--
			}
			kv := model.CompactOptions{Revision: request.Compact.Revision}
			if clientCompactCount[kv] > 1 {
				continue
			}
			if returnTime, ok := compactTimes[kv]; ok {
				lastReturnTime = min(returnTime, lastReturnTime)
				compactTimes[kv] = lastReturnTime
			}
		default:
			panic(fmt.Sprintf("Unknown request type: %q", request.Type))
		}
	}
	return putTimes, delTimes, compactTimes
}

func countClientPuts(reports []report.ClientReport) map[model.PutOptions]int64 {
	counter := map[model.PutOptions]int64{}
	for _, client := range reports {
		for _, op := range client.KeyValue {
			request := op.Input.(model.EtcdRequest)
			countPuts(counter, request)
		}
	}
	return counter
}


Review comment (suggestion):
```suggestion
			kv := model.CompactRequest{Revision: request.Compact.Revision}
```

Code after:
						continue
					}
					if returnTime, ok := putTimes[kv]; ok {
						lastReturnTime = min(returnTime, lastReturnTime)
						putTimes[kv] = lastReturnTime
					}
				case model.DeleteOperation:
					kv := model.DeleteOptions{Key: op.Delete.Key}
					if clientDeleteCount[kv] > 1 {
						continue
					}
					if returnTime, ok := delTimes[kv]; ok {
						lastReturnTime = min(returnTime, lastReturnTime)
						delTimes[kv] = lastReturnTime
					}
				}
			}
		case model.LeaseGrant:
		case model.LeaseRevoke:
		case model.Compact:
			if lastReturnTime != math.MaxInt64 {
				lastReturnTime--
			}
			if clientCompactCount[*request.Compact] > 1 {
				continue
			}
			if returnTime, ok := compactTimes[*request.Compact]; ok {
				lastReturnTime = min(returnTime, lastReturnTime)
				compactTimes[*request.Compact] = lastReturnTime
			}
		default:
			panic(fmt.Sprintf("Unknown request type: %q", request.Type))
		}
	}
	return putTimes, delTimes, compactTimes
}

func countClientPuts(reports []report.ClientReport) map[model.PutOptions]int64 {
	counter := map[model.PutOptions]int64{}
	for _, client := range reports {
		for _, op := range client.KeyValue {
			request := op.Input.(model.EtcdRequest)
			countPuts(counter, request)
		}
	}
	return counter
}

func countPersistedPuts(requests []model.EtcdRequest) map[model.PutOptions]int64 {
	counter := map[model.PutOptions]int64{}
	for _, request := range requests {
Repository: etcd-io/etcd
File: tests/robustness/validate/patch_history.go (Go)

Code before:
						continue
					}
					if returnTime, ok := putTimes[kv]; !ok || returnTime > op.Return {
						putTimes[kv] = op.Return
					}
				case model.DeleteOperation:
					kv := model.DeleteOptions{Key: etcdOp.Delete.Key}
					if clientDeleteCount[kv] > 1 {
						continue
					}
					if returnTime, ok := delTimes[kv]; !ok || returnTime > op.Return {
						delTimes[kv] = op.Return
					}
				}
			}
		case model.Range:
		case model.LeaseGrant:
		case model.LeaseRevoke:
		case model.Compact:
			kv := model.CompactRequest{Revision: request.Compact.Revision}
			if clientCompactCount[kv] > 1 {
				continue
			}
			if returnTime, ok := compactTimes[kv]; !ok || returnTime > op.Return {
				compactTimes[kv] = op.Return
			}
		case model.Defragment:
		default:
			panic(fmt.Sprintf("Unknown request type: %q", request.Type))
		}
		if op.Return > lastReturnTime {
			lastReturnTime = op.Return
		}
	}

	for i := len(persistedRequests) - 1; i >= 0; i-- {
		request := persistedRequests[i]
		switch request.Type {
		case model.Txn:
			if lastReturnTime != math.MaxInt64 {
				lastReturnTime--
			}
			for _, op := range request.Txn.AllOperations() {
				switch op.Type {
				case model.PutOperation:
					kv := model.PutOptions{Key: op.Put.Key, Value: op.Put.Value}
					if clientPutCount[kv] > 1 {
						continue
					}
					if returnTime, ok := putTimes[kv]; ok {
						lastReturnTime = min(returnTime, lastReturnTime)

Review comment (suggestion):
```suggestion
			if clientCompactCount[request.Compact] > 1 {
				continue
			}
			if returnTime, ok := compactTimes[request.Compact]; !ok || returnTime > op.Return {
				compactTimes[request.Compact] = op.Return
			}
```

Code after:
						continue
					}
					if returnTime, ok := putTimes[kv]; !ok || returnTime > op.Return {
						putTimes[kv] = op.Return
					}
				case model.DeleteOperation:
					kv := model.DeleteOptions{Key: etcdOp.Delete.Key}
					if clientDeleteCount[kv] > 1 {
						continue
					}
					if returnTime, ok := delTimes[kv]; !ok || returnTime > op.Return {
						delTimes[kv] = op.Return
					}
				}
			}
		case model.Range:
		case model.LeaseGrant:
		case model.LeaseRevoke:
		case model.Compact:
			if clientCompactCount[*request.Compact] > 1 {
				continue
			}
			if returnTime, ok := compactTimes[*request.Compact]; !ok || returnTime > op.Return {
				compactTimes[*request.Compact] = op.Return
			}
		case model.Defragment:
		default:
			panic(fmt.Sprintf("Unknown request type: %q", request.Type))
		}
		if op.Return > lastReturnTime {
			lastReturnTime = op.Return
		}
	}

	for i := len(persistedRequests) - 1; i >= 0; i-- {
		request := persistedRequests[i]
		switch request.Type {
		case model.Txn:
			if lastReturnTime != math.MaxInt64 {
				lastReturnTime--
			}
			for _, op := range request.Txn.AllOperations() {
				switch op.Type {
				case model.PutOperation:
					kv := model.PutOptions{Key: op.Put.Key, Value: op.Put.Value}
					if clientPutCount[kv] > 1 {
						continue
					}
					if returnTime, ok := putTimes[kv]; ok {
						lastReturnTime = min(returnTime, lastReturnTime)
						putTimes[kv] = lastReturnTime