Skip to content

Commit

Permalink
tests/e2e: add new case to reproduce 17780
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Fu <fuweid89@gmail.com>
  • Loading branch information
fuweid committed Apr 17, 2024
1 parent e37a67e commit dcd63a7
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions tests/e2e/reproduce_17780_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2024 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 e2e

import (
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/require"
pb "go.etcd.io/etcd/api/v3/etcdserverpb"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/pkg/v3/stringutil"
"go.etcd.io/etcd/tests/v3/framework/e2e"
)

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

ctx := context.TODO()
clus, cerr := e2e.NewEtcdProcessCluster(ctx, t,
e2e.WithClusterSize(1),
e2e.WithGoFailEnabled(true),
e2e.WithSnapshotCount(1000),
e2e.WithCompactionBatchLimit(100),
e2e.WithWatchProcessNotifyInterval(100*time.Millisecond),
)
require.NoError(t, cerr)

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

cli := newClient(t, clus.EndpointsGRPC(), e2e.ClientConfig{})

// Revision: 2 -> 198 for new keys
n := 198
valueSize := 16
for i := 2; i <= n; i++ {
_, err := cli.Put(ctx, fmt.Sprintf("%d", i), stringutil.RandString(uint(valueSize)))
require.NoError(t, err)
}

// Revision: 199 -> 201 for delete keys with compared revision
for i := 199; i <= 201; i++ {
key := fmt.Sprintf("%d", i-100)
rev := i - 100

resp, err := cli.Txn(ctx).If(clientv3.Cmp{
Result: pb.Compare_EQUAL,
Target: pb.Compare_MOD,
Key: []byte(key),
TargetUnion: &pb.Compare_ModRevision{int64(rev)},
}).Then(clientv3.OpDelete(key)).
Else(clientv3.OpGet("/")).Commit()
require.NoError(t, err)
require.True(t, resp.Succeeded)
}

// NOTE: Want to hold lock and make sure that new put request can
// write WAL but it doesn't have chance to be persist into bbolt.
require.NoError(t, clus.Procs[0].Failpoints().SetupHTTP(ctx, "compactBeforeSetFinishedCompact", `sleep("10s")`))

_, err := cli.Compact(ctx, 201)
require.NoError(t, err)

// Wait for compaction failpoint to start.
time.Sleep(2 * time.Second)

waitCh := make(chan struct{})

go func() {
defer close(waitCh)

// NOTE: Ideally, key=202 should have revision 202.
_, err := cli.Put(ctx, fmt.Sprintf("%d", 202), stringutil.RandString(uint(valueSize)))
require.Error(t, err)
}()

time.Sleep(3 * time.Second)
clus.Procs[0].Kill()
<-waitCh

require.NoError(t, clus.Restart(ctx))

resp, err := cli.Get(ctx, fmt.Sprintf("%d", 202))
require.NoError(t, err)
require.True(t, resp.Count == 1)
// The 199/200 revision has been deleted. The max rev is 198. So,
// replay WAL will get 199.
//
// That's why revision has been decreased.
require.GreaterOrEqual(t, resp.Header.Revision, int64(202))
}

0 comments on commit dcd63a7

Please sign in to comment.