Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,42 @@ func CreateServer(ctx context.Context, cfg *config.Config, services []string, le
return s, nil
}

func (s *Server) startEtcd(ctx context.Context) error {
func (s *Server) startEtcd(ctx context.Context) (retErr error) {
newCtx, cancel := context.WithTimeout(ctx, EtcdStartTimeout)
defer cancel()

etcd, err := embed.StartEtcd(s.etcdCfg)
if err != nil {
return errs.ErrStartEtcd.Wrap(err).GenWithStackByCause()
}
cleanup := func() {
// NOTE: `embed.Etcd.Close()` can block for a long time in some failure paths
// (e.g. when starting a removed member that can never become ready). Avoid
// blocking the caller (tests may wait for the start error) by stopping the
// server synchronously and closing the embedded etcd asynchronously.
if etcd.Server != nil {
etcd.Server.Stop()
}
go etcd.Close()
if s.client != nil {
if cerr := s.client.Close(); cerr != nil {
log.Error("close etcd client meet error", errs.ZapError(errs.ErrCloseEtcdClient, cerr))
}
}
if s.electionClient != nil {
if cerr := s.electionClient.Close(); cerr != nil {
log.Error("close election client meet error", errs.ZapError(errs.ErrCloseEtcdClient, cerr))
}
}
if s.httpClient != nil {
s.httpClient.CloseIdleConnections()
}
}
defer func() {
if retErr != nil {
cleanup()
}
}()

// Check cluster ID
urlMap, err := etcdtypes.NewURLsMap(s.cfg.InitialCluster)
Expand Down
5 changes: 4 additions & 1 deletion tests/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,10 @@ func (c *TestCluster) runInitialServersWithRetry(maxRetries int) error {

errMsg := lastErr.Error()
switch {
case strings.Contains(errMsg, "address already in use"):
case strings.Contains(errMsg, "address already in use") || strings.Contains(errMsg, "Etcd cluster ID mismatch"):
// `Etcd cluster ID mismatch` can happen when the allocated peer URL happens to
// connect to another test's etcd cluster (port reuse across concurrent `go test`
// processes). Treat it as a port conflict and recreate servers with new ports.
log.Warn("port conflict detected, recreating servers with new ports",
zap.Int("attempt", i+1),
zap.Int("maxRetries", maxRetries),
Expand Down