Repository: etcd-io/etcd
File: tests/e2e/reproduce_18667_test.go (Go)

Code before:

// TestReproduce18667 reproduces the issue: https://github.com/etcd-io/etcd/issues/18667.
func TestReproduce18667(t *testing.T) {
	e2e.BeforeTest(t)

	ctx := t.Context()
	clus, cerr := e2e.NewEtcdProcessCluster(ctx, t,
		e2e.WithClusterSize(3),
		e2e.WithSnapshotCount(1000),
	)
	require.NoError(t, cerr)

	t.Cleanup(func() { clus.Stop() })

	targetIdx := rand.Intn(3)
	cli := newClient(t, clus.Procs[targetIdx].EndpointsGRPC(), e2e.ClientConfig{})

	// Put key-value [k1...k20], revision = 21
	n := 20
	for i := 1; i <= n; i++ {
		_, err := cli.Put(ctx, fmt.Sprintf("k%d", i), fmt.Sprintf("v%d", i))
		require.NoError(t, err)
	}

	// Compact revision 21
	_, err := cli.Compact(ctx, int64(n+1))
	require.NoError(t, err)

	// Txn to make data inconsistent among etcdservers
	cmp := clientv3.Compare(clientv3.Value("k1"), "=", "v1")
	then := []clientv3.Op{
		clientv3.OpPut("k2", "foo"),
		clientv3.OpGet("k1", clientv3.WithRev(int64(n))),
	}
	_, err = cli.Txn(ctx).If(cmp).Then(then...).Commit()
	require.Error(t, err)
	require.True(t, strings.Contains(err.Error(), "etcdserver: mvcc: required revision has been compacted"))

	// k2 equals to v2 in the etcdserver which the Txn was sent to
	kv2, err := cli.Get(ctx, "k2")
	require.NoError(t, err)
	assert.Equal(t, "v2", string(kv2.Kvs[0].Value))

	// k2 equals to foo in other etcdservers
	targetIdx = (targetIdx + 1) % clus.Cfg.ClusterSize
	cli = newClient(t, clus.Procs[targetIdx].EndpointsGRPC(), e2e.ClientConfig{})
	followerK2, err := cli.Get(ctx, "k2")
	require.NoError(t, err)
	assert.Equal(t, "foo", string(followerK2.Kvs[0].Value))
}


Review comment (suggestion):
```suggestion
    var revision int64
	for i := 1; i <= 20; i++ {
		resp, err := cli.Put(ctx, fmt.Sprintf("k%d", i), fmt.Sprintf("v%d", i))
		require.NoError(t, err)
		revision = resp.Header.Revision
	}

	// Compact revision 21
	_, err := cli.Compact(ctx, revision)
```

Code after:

// TestReproduce18667 reproduces the issue: https://github.com/etcd-io/etcd/issues/18667.
func TestReproduce18667(t *testing.T) {
	e2e.BeforeTest(t)

	ctx := t.Context()
	clus, cerr := e2e.NewEtcdProcessCluster(ctx, t,
		e2e.WithClusterSize(3),
		e2e.WithSnapshotCount(1000),
	)
	require.NoError(t, cerr)

	t.Cleanup(func() { clus.Stop() })

	targetIdx := rand.Intn(3)
	cli := newClient(t, clus.Procs[targetIdx].EndpointsGRPC(), e2e.ClientConfig{})

	t.Log("Put key-value [k1...k20]")
	var revision int64
	for i := 1; i <= 20; i++ {
		resp, err := cli.Put(ctx, fmt.Sprintf("k%d", i), fmt.Sprintf("v%d", i))
		require.NoError(t, err)
		revision = resp.Header.Revision
	}

	t.Logf("Compact on the latest revision %d", revision)
	_, err := cli.Compact(ctx, revision)
	require.NoError(t, err)

	t.Log("Send txn to make data inconsistent among etcdservers")
	cmp := clientv3.Compare(clientv3.Value("k1"), "=", "v1")
	then := []clientv3.Op{
		clientv3.OpPut("k2", "foo"),
		clientv3.OpGet("k1", clientv3.WithRev(revision-1)),
	}
	_, err = cli.Txn(ctx).If(cmp).Then(then...).Commit()
	require.Error(t, err)
	require.Contains(t, err.Error(), rpctypes.ErrCompacted.Error())

	t.Log("Verify all members have consistent data on key 'k2'")
	for i := 0; i < clus.Cfg.ClusterSize; i++ {
		idx := (targetIdx + i) % clus.Cfg.ClusterSize
		cli = newClient(t, clus.Procs[idx].EndpointsGRPC(), e2e.ClientConfig{})
		resp, err := cli.Get(ctx, "k2")
		require.NoError(t, err)
		// TODO: All members are supposed to have the same key/value pair k2:v2.
		// However, the issue https://github.com/etcd-io/etcd/issues/18667
		// hasn't been resolved yet, so some members hold the wrong data
		// "k2:foo". Once the issue is fixed, we should remove the if-else
		// branch below, and all member should have consistent data k2:v2.
		if i == 0 {