2016-05-13 06:51:48 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
2016-01-04 22:13:43 +03:00
|
|
|
//
|
|
|
|
// 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
|
2016-03-20 00:04:45 +03:00
|
|
|
// limitations under the License.
|
|
|
|
|
2016-01-04 22:13:43 +03:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2017-08-23 00:43:14 +03:00
|
|
|
"context"
|
2016-01-04 22:13:43 +03:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2016-03-11 22:54:30 +03:00
|
|
|
v3 "github.com/coreos/etcd/clientv3"
|
|
|
|
"github.com/coreos/etcd/clientv3/concurrency"
|
2017-08-23 00:43:14 +03:00
|
|
|
"github.com/coreos/etcd/pkg/testutil"
|
2016-01-04 22:13:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestSTMConflict tests that conflicts are retried.
|
|
|
|
func TestSTMConflict(t *testing.T) {
|
2016-01-29 06:07:35 +03:00
|
|
|
clus := NewClusterV3(t, &ClusterConfig{Size: 3})
|
2016-01-04 22:13:43 +03:00
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
2016-01-28 01:08:32 +03:00
|
|
|
etcdc := clus.RandClient()
|
2016-03-11 22:54:30 +03:00
|
|
|
keys := make([]string, 5)
|
2016-01-04 22:13:43 +03:00
|
|
|
for i := 0; i < len(keys); i++ {
|
2016-03-11 22:54:30 +03:00
|
|
|
keys[i] = fmt.Sprintf("foo-%d", i)
|
|
|
|
if _, err := etcdc.Put(context.TODO(), keys[i], "100"); err != nil {
|
2016-01-04 22:13:43 +03:00
|
|
|
t.Fatalf("could not make key (%v)", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-11 22:54:30 +03:00
|
|
|
errc := make(chan error)
|
|
|
|
for i := range keys {
|
2016-01-28 01:08:32 +03:00
|
|
|
curEtcdc := clus.RandClient()
|
2016-03-11 22:54:30 +03:00
|
|
|
srcKey := keys[i]
|
|
|
|
applyf := func(stm concurrency.STM) error {
|
|
|
|
src := stm.Get(srcKey)
|
2016-01-04 22:13:43 +03:00
|
|
|
// must be different key to avoid double-adding
|
|
|
|
dstKey := srcKey
|
|
|
|
for dstKey == srcKey {
|
2016-03-11 22:54:30 +03:00
|
|
|
dstKey = keys[rand.Intn(len(keys))]
|
2016-01-04 22:13:43 +03:00
|
|
|
}
|
2016-03-11 22:54:30 +03:00
|
|
|
dst := stm.Get(dstKey)
|
2016-01-04 22:13:43 +03:00
|
|
|
srcV, _ := strconv.ParseInt(src, 10, 64)
|
|
|
|
dstV, _ := strconv.ParseInt(dst, 10, 64)
|
2016-03-28 23:06:07 +03:00
|
|
|
if srcV == 0 {
|
|
|
|
// can't rand.Intn on 0, so skip this transaction
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-04 22:13:43 +03:00
|
|
|
xfer := int64(rand.Intn(int(srcV)) / 2)
|
|
|
|
stm.Put(srcKey, fmt.Sprintf("%d", srcV-xfer))
|
|
|
|
stm.Put(dstKey, fmt.Sprintf("%d", dstV+xfer))
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-11 22:54:30 +03:00
|
|
|
go func() {
|
2016-12-30 20:38:00 +03:00
|
|
|
iso := concurrency.WithIsolation(concurrency.RepeatableReads)
|
|
|
|
_, err := concurrency.NewSTM(curEtcdc, applyf, iso)
|
2016-03-11 22:54:30 +03:00
|
|
|
errc <- err
|
|
|
|
}()
|
2016-01-04 22:13:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait for txns
|
2016-03-11 22:54:30 +03:00
|
|
|
for range keys {
|
|
|
|
if err := <-errc; err != nil {
|
2016-01-04 22:13:43 +03:00
|
|
|
t.Fatalf("apply failed (%v)", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure sum matches initial sum
|
|
|
|
sum := 0
|
2016-03-11 22:54:30 +03:00
|
|
|
for _, oldkey := range keys {
|
|
|
|
rk, err := etcdc.Get(context.TODO(), oldkey)
|
2016-01-04 22:13:43 +03:00
|
|
|
if err != nil {
|
2016-03-11 22:54:30 +03:00
|
|
|
t.Fatalf("couldn't fetch key %s (%v)", oldkey, err)
|
2016-01-04 22:13:43 +03:00
|
|
|
}
|
2016-03-11 22:54:30 +03:00
|
|
|
v, _ := strconv.ParseInt(string(rk.Kvs[0].Value), 10, 64)
|
2016-01-04 22:13:43 +03:00
|
|
|
sum += int(v)
|
|
|
|
}
|
|
|
|
if sum != len(keys)*100 {
|
|
|
|
t.Fatalf("bad sum. got %d, expected %d", sum, len(keys)*100)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 08:42:39 +03:00
|
|
|
// TestSTMPutNewKey confirms a STM put on a new key is visible after commit.
|
2016-01-04 22:13:43 +03:00
|
|
|
func TestSTMPutNewKey(t *testing.T) {
|
2016-01-29 06:07:35 +03:00
|
|
|
clus := NewClusterV3(t, &ClusterConfig{Size: 1})
|
2016-01-04 22:13:43 +03:00
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
2016-01-28 01:08:32 +03:00
|
|
|
etcdc := clus.RandClient()
|
2016-03-11 22:54:30 +03:00
|
|
|
applyf := func(stm concurrency.STM) error {
|
2016-01-04 22:13:43 +03:00
|
|
|
stm.Put("foo", "bar")
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-30 20:38:00 +03:00
|
|
|
|
|
|
|
iso := concurrency.WithIsolation(concurrency.RepeatableReads)
|
|
|
|
if _, err := concurrency.NewSTM(etcdc, applyf, iso); err != nil {
|
2016-01-04 22:13:43 +03:00
|
|
|
t.Fatalf("error on stm txn (%v)", err)
|
|
|
|
}
|
|
|
|
|
2016-03-11 22:54:30 +03:00
|
|
|
resp, err := etcdc.Get(context.TODO(), "foo")
|
2016-01-04 22:13:43 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error fetching key (%v)", err)
|
|
|
|
}
|
2016-03-11 22:54:30 +03:00
|
|
|
if string(resp.Kvs[0].Value) != "bar" {
|
|
|
|
t.Fatalf("bad value. got %+v, expected 'bar' value", resp)
|
2016-01-04 22:13:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSTMAbort tests that an aborted txn does not modify any keys.
|
|
|
|
func TestSTMAbort(t *testing.T) {
|
2016-01-29 06:07:35 +03:00
|
|
|
clus := NewClusterV3(t, &ClusterConfig{Size: 1})
|
2016-01-04 22:13:43 +03:00
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
2016-01-28 01:08:32 +03:00
|
|
|
etcdc := clus.RandClient()
|
2016-03-11 22:54:30 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.TODO())
|
|
|
|
applyf := func(stm concurrency.STM) error {
|
2016-01-04 22:13:43 +03:00
|
|
|
stm.Put("foo", "baz")
|
2016-03-11 22:54:30 +03:00
|
|
|
cancel()
|
|
|
|
stm.Put("foo", "bap")
|
2016-01-04 22:13:43 +03:00
|
|
|
return nil
|
|
|
|
}
|
2016-12-30 20:38:00 +03:00
|
|
|
|
|
|
|
iso := concurrency.WithIsolation(concurrency.RepeatableReads)
|
|
|
|
sctx := concurrency.WithAbortContext(ctx)
|
|
|
|
if _, err := concurrency.NewSTM(etcdc, applyf, iso, sctx); err == nil {
|
2016-03-11 22:54:30 +03:00
|
|
|
t.Fatalf("no error on stm txn")
|
2016-01-04 22:13:43 +03:00
|
|
|
}
|
|
|
|
|
2016-03-11 22:54:30 +03:00
|
|
|
resp, err := etcdc.Get(context.TODO(), "foo")
|
2016-01-04 22:13:43 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error fetching key (%v)", err)
|
|
|
|
}
|
2016-03-11 22:54:30 +03:00
|
|
|
if len(resp.Kvs) != 0 {
|
|
|
|
t.Fatalf("bad value. got %+v, expected nothing", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSTMSerialize tests that serialization is honored when serializable.
|
|
|
|
func TestSTMSerialize(t *testing.T) {
|
|
|
|
clus := NewClusterV3(t, &ClusterConfig{Size: 3})
|
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
|
|
|
etcdc := clus.RandClient()
|
|
|
|
|
|
|
|
// set up initial keys
|
|
|
|
keys := make([]string, 5)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i] = fmt.Sprintf("foo-%d", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// update keys in full batches
|
|
|
|
updatec := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(updatec)
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
s := fmt.Sprintf("%d", i)
|
|
|
|
ops := []v3.Op{}
|
|
|
|
for _, k := range keys {
|
|
|
|
ops = append(ops, v3.OpPut(k, s))
|
|
|
|
}
|
|
|
|
if _, err := etcdc.Txn(context.TODO()).Then(ops...).Commit(); err != nil {
|
|
|
|
t.Fatalf("couldn't put keys (%v)", err)
|
|
|
|
}
|
|
|
|
updatec <- struct{}{}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// read all keys in txn, make sure all values match
|
|
|
|
errc := make(chan error)
|
|
|
|
for range updatec {
|
|
|
|
curEtcdc := clus.RandClient()
|
|
|
|
applyf := func(stm concurrency.STM) error {
|
|
|
|
vs := []string{}
|
|
|
|
for i := range keys {
|
|
|
|
vs = append(vs, stm.Get(keys[i]))
|
|
|
|
}
|
|
|
|
for i := range vs {
|
|
|
|
if vs[0] != vs[i] {
|
|
|
|
return fmt.Errorf("got vs[%d] = %v, want %v", i, vs[i], vs[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
go func() {
|
2016-12-30 20:38:00 +03:00
|
|
|
iso := concurrency.WithIsolation(concurrency.Serializable)
|
|
|
|
_, err := concurrency.NewSTM(curEtcdc, applyf, iso)
|
2016-03-11 22:54:30 +03:00
|
|
|
errc <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
if err := <-errc; err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-01-04 22:13:43 +03:00
|
|
|
}
|
|
|
|
}
|
2017-01-20 08:04:58 +03:00
|
|
|
|
|
|
|
// TestSTMApplyOnConcurrentDeletion ensures that concurrent key deletion
|
|
|
|
// fails the first GET revision comparison within STM; trigger retry.
|
|
|
|
func TestSTMApplyOnConcurrentDeletion(t *testing.T) {
|
|
|
|
clus := NewClusterV3(t, &ClusterConfig{Size: 1})
|
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
|
|
|
etcdc := clus.RandClient()
|
|
|
|
if _, err := etcdc.Put(context.TODO(), "foo", "bar"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
donec, readyc := make(chan struct{}), make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
<-readyc
|
|
|
|
if _, err := etcdc.Delete(context.TODO(), "foo"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
close(donec)
|
|
|
|
}()
|
|
|
|
|
|
|
|
try := 0
|
|
|
|
applyf := func(stm concurrency.STM) error {
|
|
|
|
try++
|
|
|
|
stm.Get("foo")
|
|
|
|
if try == 1 {
|
|
|
|
// trigger delete to make GET rev comparison outdated
|
|
|
|
close(readyc)
|
|
|
|
<-donec
|
|
|
|
}
|
|
|
|
stm.Put("foo2", "bar2")
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-30 20:38:00 +03:00
|
|
|
|
|
|
|
iso := concurrency.WithIsolation(concurrency.RepeatableReads)
|
|
|
|
if _, err := concurrency.NewSTM(etcdc, applyf, iso); err != nil {
|
2017-01-20 08:04:58 +03:00
|
|
|
t.Fatalf("error on stm txn (%v)", err)
|
|
|
|
}
|
|
|
|
if try != 2 {
|
|
|
|
t.Fatalf("STM apply expected to run twice, got %d", try)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := etcdc.Get(context.TODO(), "foo2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error fetching key (%v)", err)
|
|
|
|
}
|
|
|
|
if string(resp.Kvs[0].Value) != "bar2" {
|
|
|
|
t.Fatalf("bad value. got %+v, expected 'bar2' value", resp)
|
|
|
|
}
|
|
|
|
}
|
2017-08-23 00:43:14 +03:00
|
|
|
|
|
|
|
func TestSTMSerializableSnapshotPut(t *testing.T) {
|
|
|
|
clus := NewClusterV3(t, &ClusterConfig{Size: 1})
|
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
|
|
|
cli := clus.Client(0)
|
|
|
|
// key with lower create/mod revision than keys being updated
|
|
|
|
_, err := cli.Put(context.TODO(), "a", "0")
|
|
|
|
testutil.AssertNil(t, err)
|
|
|
|
|
|
|
|
tries := 0
|
|
|
|
applyf := func(stm concurrency.STM) error {
|
|
|
|
if tries > 2 {
|
|
|
|
return fmt.Errorf("too many retries")
|
|
|
|
}
|
|
|
|
tries++
|
|
|
|
stm.Get("a")
|
|
|
|
stm.Put("b", "1")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
iso := concurrency.WithIsolation(concurrency.SerializableSnapshot)
|
|
|
|
_, err = concurrency.NewSTM(cli, applyf, iso)
|
|
|
|
testutil.AssertNil(t, err)
|
|
|
|
_, err = concurrency.NewSTM(cli, applyf, iso)
|
|
|
|
testutil.AssertNil(t, err)
|
|
|
|
|
|
|
|
resp, err := cli.Get(context.TODO(), "b")
|
|
|
|
testutil.AssertNil(t, err)
|
|
|
|
if resp.Kvs[0].Version != 2 {
|
|
|
|
t.Fatalf("bad version. got %+v, expected version 2", resp)
|
|
|
|
}
|
|
|
|
}
|