Skip to content

Commit

Permalink
snapshot: initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
  • Loading branch information
gyuho committed Jan 8, 2018
1 parent 44e1f6f commit 7ec33ab
Show file tree
Hide file tree
Showing 5 changed files with 906 additions and 0 deletions.
16 changes: 16 additions & 0 deletions snapshot/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018 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 snapshot implements utilities around etcd snapshot.
package snapshot
72 changes: 72 additions & 0 deletions snapshot/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2018 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 snapshot

import (
"log"

"github.com/coreos/pkg/capnslog"
"google.golang.org/grpc/grpclog"
)

// DefaultLogger defines default snapshot logger based on "capnslog".
var DefaultLogger grpclog.LoggerV2

func init() {
plog := capnslog.NewPackageLogger("github.com/coreos/etcd", "snapshot")
DefaultLogger = toGPCLoggerV2(plog)
}

func toGPCLoggerV2(p *capnslog.PackageLogger) grpclog.LoggerV2 {
return &grpcLogggerV2{p: p}
}

type grpcLogggerV2 struct {
p *capnslog.PackageLogger
}

func (l *grpcLogggerV2) Info(args ...interface{}) { l.p.Info(args...) }
func (l *grpcLogggerV2) Infoln(args ...interface{}) { l.p.Info(args...) }
func (l *grpcLogggerV2) Infof(format string, args ...interface{}) { l.p.Infof(format, args...) }
func (l *grpcLogggerV2) Warning(args ...interface{}) { l.p.Warning(args...) }
func (l *grpcLogggerV2) Warningln(args ...interface{}) { l.p.Warning(args...) }
func (l *grpcLogggerV2) Warningf(format string, args ...interface{}) { l.p.Warningf(format, args...) }
func (l *grpcLogggerV2) Error(args ...interface{}) { l.p.Error(args...) }
func (l *grpcLogggerV2) Errorln(args ...interface{}) { l.p.Error(args...) }
func (l *grpcLogggerV2) Errorf(format string, args ...interface{}) { l.p.Errorf(format, args...) }
func (l *grpcLogggerV2) Fatal(args ...interface{}) { l.p.Fatal(args...) }
func (l *grpcLogggerV2) Fatalln(args ...interface{}) { l.p.Fatal(args...) }
func (l *grpcLogggerV2) Fatalf(format string, args ...interface{}) { l.p.Fatalf(format, args...) }
func (l *grpcLogggerV2) V(lvl int) bool {
return l.p.LevelAt(capnslog.LogLevel(lvl))
}

type discardLogger struct{}

func (l *discardLogger) Info(args ...interface{}) {}
func (l *discardLogger) Infoln(args ...interface{}) {}
func (l *discardLogger) Infof(format string, args ...interface{}) {}
func (l *discardLogger) Warning(args ...interface{}) {}
func (l *discardLogger) Warningln(args ...interface{}) {}
func (l *discardLogger) Warningf(format string, args ...interface{}) {}
func (l *discardLogger) Error(args ...interface{}) {}
func (l *discardLogger) Errorln(args ...interface{}) {}
func (l *discardLogger) Errorf(format string, args ...interface{}) {}
func (l *discardLogger) Fatal(args ...interface{}) { log.Fatal(args...) }
func (l *discardLogger) Fatalln(args ...interface{}) { log.Fatalln(args...) }
func (l *discardLogger) Fatalf(format string, args ...interface{}) { log.Fatalf(format, args...) }
func (l *discardLogger) V(lvl int) bool {
return false
}
110 changes: 110 additions & 0 deletions snapshot/member_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2018 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 snapshot

import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"time"

"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/embed"
"github.com/coreos/etcd/etcdserver"
)

func TestSnapshotV3RestoreMultiMemberAdd(t *testing.T) {
kvs := []kv{{"foo1", "bar1"}, {"foo2", "bar2"}, {"foo3", "bar3"}}
dbPath := createSnapshot(t, kvs)

clusterN := 3
cURLs, pURLs, srvs := restoreCluster(t, clusterN, dbPath)
defer func() {
for i := 0; i < clusterN; i++ {
os.RemoveAll(srvs[i].Config().Dir)
srvs[i].Close()
}
}()

// wait for health interval + leader election
time.Sleep(etcdserver.HealthInterval + 2*time.Second)

cli, err := clientv3.New(clientv3.Config{Endpoints: []string{cURLs[0].String()}})
if err != nil {
t.Fatal(err)
}
defer cli.Close()

urls := newEmbedURLs(2)
newCURLs, newPURLs := urls[:1], urls[1:]
if _, err = cli.MemberAdd(context.Background(), []string{newPURLs[0].String()}); err != nil {
t.Fatal(err)
}

time.Sleep(2 * time.Second)

cfg := embed.NewConfig()
cfg.Name = "3"
cfg.InitialClusterToken = "tkn"
cfg.ClusterState = "existing"
cfg.LCUrls, cfg.ACUrls = newCURLs, newCURLs
cfg.LPUrls, cfg.APUrls = newPURLs, newPURLs
cfg.InitialCluster = ""
for i := 0; i < clusterN; i++ {
cfg.InitialCluster += fmt.Sprintf(",%d=%s", i, pURLs[i].String())
}
cfg.InitialCluster = cfg.InitialCluster[1:]
cfg.InitialCluster += fmt.Sprintf(",%s=%s", cfg.Name, newPURLs[0].String())
cfg.Dir = filepath.Join(os.TempDir(), fmt.Sprint(time.Now().Nanosecond()))

srv, err := embed.StartEtcd(cfg)
if err != nil {
t.Fatal(err)
}
defer func() {
os.RemoveAll(cfg.Dir)
srv.Close()
}()
select {
case <-srv.Server.ReadyNotify():
case <-time.After(3 * time.Second):
t.Fatalf("failed to start embed.Etcd")
}

cli2, err := clientv3.New(clientv3.Config{Endpoints: []string{newCURLs[0].String()}})
if err != nil {
t.Fatal(err)
}
defer cli2.Close()
mresp, err := cli2.MemberList(context.Background())
if err != nil {
t.Fatal(err)
}
if len(mresp.Members) != 4 {
t.Fatalf("expected 4 members, got %+v", mresp)
}
for i := range kvs {
var gresp *clientv3.GetResponse
gresp, err = cli2.Get(context.Background(), kvs[i].k)
if err != nil {
t.Fatal(err)
}
if string(gresp.Kvs[0].Value) != kvs[i].v {
t.Fatalf("#%d: value expected %s, got %s", i, kvs[i].v, string(gresp.Kvs[0].Value))
}
}
}
Loading

0 comments on commit 7ec33ab

Please sign in to comment.