Skip to content

Commit

Permalink
embed: etcd.Close() is closing Errc() channel as well.
Browse files Browse the repository at this point in the history
Inspired by #9612 by purpleidea@.
  • Loading branch information
ptabor committed Apr 7, 2021
1 parent dfb03ab commit 5da9cac
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-3.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ See [code changes](https://github.com/etcd-io/etcd/compare/v3.4.0...v3.5.0) and
- Previously supported [GRPCResolver was decomissioned](https://github.com/etcd-io/etcd/pull/12675). Use [resolver](https://github.com/etcd-io/etcd/blob/master/client/v3/naming/resolver/resolver.go) instead.
- Turned on [--pre-vote by default](https://github.com/etcd-io/etcd/pull/12770). Should prevent disrupting RAFT leader by an individual member.
- [ETCD_CLIENT_DEBUG env](https://github.com/etcd-io/etcd/pull/12786): Now supports log levels (debug, info, warn, error, dpanic, panic, fatal). Only when set, overrides application-wide grpc logging settings.
- [Embed Etcd.Close()](https://github.com/etcd-io/etcd/pull/12828) needs to called exactly once and closes Etcd.Err() stream.
###

- Make sure [save snapshot downloads checksum for integrity checks](https://github.com/etcd-io/etcd/pull/11896).
Expand Down
14 changes: 11 additions & 3 deletions server/embed/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ func (e *Etcd) Close() {
lg.Sync()
}()

e.closeOnce.Do(func() { close(e.stopc) })
e.closeOnce.Do(func() {
close(e.stopc)
})

// close client requests with request timeout
timeout := 2 * time.Second
Expand Down Expand Up @@ -383,12 +385,14 @@ func (e *Etcd) Close() {
cancel()
}
}
if e.errc != nil {
close(e.errc)
}
}

func stopServers(ctx context.Context, ss *servers) {
// first, close the http.Server
ss.http.Shutdown(ctx)

// do not grpc.Server.GracefulStop with TLS enabled etcd server
// See https://github.com/grpc/grpc-go/issues/1384#issuecomment-317124531
// and https://github.com/etcd-io/etcd/issues/8916
Expand Down Expand Up @@ -418,7 +422,11 @@ func stopServers(ctx context.Context, ss *servers) {
}
}

func (e *Etcd) Err() <-chan error { return e.errc }
// Err - return channel used to report errors during etcd run/shutdown.
// Since etcd 3.5 the channel is being closed when the etcd is over.
func (e *Etcd) Err() <-chan error {
return e.errc
}

func configurePeerListeners(cfg *Config) (peers []*peerListener, err error) {
if err = updateCipherSuites(&cfg.PeerTLSInfo, cfg.CipherSuites); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions server/embed/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ import (

// TestStartEtcdWrongToken ensures that StartEtcd with wrong configs returns with error.
func TestStartEtcdWrongToken(t *testing.T) {
tdir, err := ioutil.TempDir(os.TempDir(), "token-test")
tdir, err := ioutil.TempDir(t.TempDir(), "token-test")

if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tdir)

cfg := NewConfig()

Expand Down
6 changes: 2 additions & 4 deletions tests/integration/clientv3/snapshot/v3_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ import (
"go.etcd.io/etcd/pkg/v3/testutil"
"go.etcd.io/etcd/server/v3/embed"
"go.etcd.io/etcd/tests/v3/integration"

"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)

// TestSaveSnapshotFilePermissions ensures that the snapshot is saved with
Expand Down Expand Up @@ -99,11 +98,10 @@ func createSnapshotFile(t *testing.T, kvs []kv) string {
}

dpPath := filepath.Join(t.TempDir(), fmt.Sprintf("snapshot%d.db", time.Now().Nanosecond()))
if err = snapshot.Save(context.Background(), zap.NewExample(), ccfg, dpPath); err != nil {
if err = snapshot.Save(context.Background(), zaptest.NewLogger(t), ccfg, dpPath); err != nil {
t.Fatal(err)
}

srv.Close()
return dpPath
}

Expand Down
11 changes: 7 additions & 4 deletions tests/integration/embed/embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ func TestEmbedEtcd(t *testing.T) {
e.Close()
select {
case err := <-e.Err():
t.Errorf("#%d: unexpected error on close (%v)", i, err)
default:
if err != nil {
t.Errorf("#%d: unexpected error on close (%v)", i, err)
}
}
}
}
Expand Down Expand Up @@ -174,12 +175,14 @@ func testEmbedEtcdGracefulStop(t *testing.T, secure bool) {
close(donec)
}()
select {
case err := <-e.Err():
t.Fatal(err)
case <-donec:
case <-time.After(2*time.Second + e.Server.Cfg.ReqTimeout()):
t.Fatalf("took too long to close server")
}
err = <-e.Err()
if err != nil {
t.Fatal(err)
}
}

func newEmbedURLs(secure bool, n int) (urls []url.URL) {
Expand Down
1 change: 0 additions & 1 deletion tests/integration/snapshot/v3_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ func createSnapshotFile(t *testing.T, kvs []kv) string {
}

os.RemoveAll(cfg.Dir)
srv.Close()
return dpPath
}

Expand Down

0 comments on commit 5da9cac

Please sign in to comment.