mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #15031 from ahrtr/SnapshotWithVersion_nil_panic_20221220
clientv3: add protection code to prevent SnapshotWithVersion from panicking
This commit is contained in:
commit
054b24b425
@ -239,6 +239,7 @@ func (m *maintenance) SnapshotWithVersion(ctx context.Context) (*SnapshotRespons
|
|||||||
resp, err := ss.Recv()
|
resp, err := ss.Recv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.logAndCloseWithError(err, pw)
|
m.logAndCloseWithError(err, pw)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
// Saving response is blocking
|
// Saving response is blocking
|
||||||
@ -260,10 +261,11 @@ func (m *maintenance) SnapshotWithVersion(ctx context.Context) (*SnapshotRespons
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return &SnapshotResponse{
|
return &SnapshotResponse{
|
||||||
Header: resp.Header,
|
Header: resp.GetHeader(),
|
||||||
Snapshot: &snapshotReadCloser{ctx: ctx, ReadCloser: pr},
|
Snapshot: &snapshotReadCloser{ctx: ctx, ReadCloser: pr},
|
||||||
Version: resp.Version,
|
Version: resp.GetVersion(),
|
||||||
}, err
|
}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ func SaveWithVersion(ctx context.Context, lg *zap.Logger, cfg clientv3.Config, d
|
|||||||
start := time.Now()
|
start := time.Now()
|
||||||
resp, err := cli.SnapshotWithVersion(ctx)
|
resp, err := cli.SnapshotWithVersion(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return resp.Version, err
|
return "", err
|
||||||
}
|
}
|
||||||
defer resp.Snapshot.Close()
|
defer resp.Snapshot.Close()
|
||||||
lg.Info("fetching snapshot", zap.String("endpoint", cfg.Endpoints[0]))
|
lg.Info("fetching snapshot", zap.String("endpoint", cfg.Endpoints[0]))
|
||||||
@ -99,16 +99,3 @@ func SaveWithVersion(ctx context.Context, lg *zap.Logger, cfg clientv3.Config, d
|
|||||||
lg.Info("saved", zap.String("path", dbPath))
|
lg.Info("saved", zap.String("path", dbPath))
|
||||||
return resp.Version, nil
|
return resp.Version, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save fetches snapshot from remote etcd server and saves data
|
|
||||||
// to target path. If the context "ctx" is canceled or timed out,
|
|
||||||
// snapshot save stream will error out (e.g. context.Canceled,
|
|
||||||
// context.DeadlineExceeded). Make sure to specify only one endpoint
|
|
||||||
// in client configuration. Snapshot API must be requested to a
|
|
||||||
// selected node, and saved snapshot is the point-in-time state of
|
|
||||||
// the selected node.
|
|
||||||
// Deprecated: Use SaveWithVersion instead.
|
|
||||||
func Save(ctx context.Context, lg *zap.Logger, cfg clientv3.Config, dbPath string) error {
|
|
||||||
_, err := SaveWithVersion(ctx, lg, cfg, dbPath)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user