Skip to content

Commit 9e1dadd

Browse files
ivanvcfatihusta
andcommitted
etcdutl: Fix snapshot restore memory alloc issue
When running the snapshot command, allow receiving an initial memory map allocation for the database, avoiding future memory allocation issues. Backports commit: be28833 / PR: #17277 Co-authored-by: Fatih USTA <fatihusta86@gmail.com> Signed-off-by: Ivan Valdes <ivan@vald.es>
1 parent 6abc349 commit 9e1dadd

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

etcdctl/ctlv3/command/snapshot_command.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var (
4242
skipHashCheck bool
4343
markCompacted bool
4444
revisionBump uint64
45+
initialMmapSize uint64
4546
)
4647

4748
// NewSnapshotCommand returns the cobra command for "snapshot".
@@ -93,6 +94,7 @@ func NewSnapshotRestoreCommand() *cobra.Command {
9394
cmd.Flags().BoolVar(&skipHashCheck, "skip-hash-check", false, "Ignore snapshot integrity hash value (required if copied from data directory)")
9495
cmd.Flags().Uint64Var(&revisionBump, "bump-revision", 0, "How much to increase the latest revision after restore")
9596
cmd.Flags().BoolVar(&markCompacted, "mark-compacted", false, "Mark the latest revision after restore as the point of scheduled compaction (required if --bump-revision > 0, disallowed otherwise)")
97+
cmd.Flags().Uint64Var(&initialMmapSize, "initial-memory-map-size", initialMmapSize, "Initial memory map size of the database in bytes. It uses the default value if not defined or defined to 0")
9698

9799
return cmd
98100
}
@@ -131,7 +133,7 @@ func snapshotStatusCommandFunc(cmd *cobra.Command, args []string) {
131133
func snapshotRestoreCommandFunc(cmd *cobra.Command, args []string) {
132134
fmt.Fprintf(os.Stderr, "Deprecated: Use `etcdutl snapshot restore` instead.\n\n")
133135
etcdutl.SnapshotRestoreCommandFunc(restoreCluster, restoreClusterToken, restoreDataDir, restoreWalDir,
134-
restorePeerURLs, restoreName, skipHashCheck, revisionBump, markCompacted, args)
136+
restorePeerURLs, restoreName, skipHashCheck, initialMmapSize, revisionBump, markCompacted, args)
135137
}
136138

137139
func initialClusterFromName(name string) string {

etcdutl/etcdutl/snapshot_command.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"go.etcd.io/etcd/etcdutl/v3/snapshot"
2222
"go.etcd.io/etcd/pkg/v3/cobrautl"
2323
"go.etcd.io/etcd/server/v3/datadir"
24+
"go.etcd.io/etcd/server/v3/mvcc/backend"
2425

2526
"github.com/spf13/cobra"
2627
)
@@ -38,6 +39,7 @@ var (
3839
restorePeerURLs string
3940
restoreName string
4041
skipHashCheck bool
42+
initialMmapSize = backend.InitialMmapSize
4143
markCompacted bool
4244
revisionBump uint64
4345
)
@@ -93,6 +95,7 @@ func NewSnapshotRestoreCommand() *cobra.Command {
9395
cmd.Flags().StringVar(&restorePeerURLs, "initial-advertise-peer-urls", defaultInitialAdvertisePeerURLs, "List of this member's peer URLs to advertise to the rest of the cluster")
9496
cmd.Flags().StringVar(&restoreName, "name", defaultName, "Human-readable name for this member")
9597
cmd.Flags().BoolVar(&skipHashCheck, "skip-hash-check", false, "Ignore snapshot integrity hash value (required if copied from data directory)")
98+
cmd.Flags().Uint64Var(&initialMmapSize, "initial-memory-map-size", initialMmapSize, "Initial memory map size of the database in bytes. It uses the default value if not defined or defined to 0")
9699
cmd.Flags().Uint64Var(&revisionBump, "bump-revision", 0, "How much to increase the latest revision after restore")
97100
cmd.Flags().BoolVar(&markCompacted, "mark-compacted", false, "Mark the latest revision after restore as the point of scheduled compaction (required if --bump-revision > 0, disallowed otherwise)")
98101

@@ -119,7 +122,7 @@ func SnapshotStatusCommandFunc(cmd *cobra.Command, args []string) {
119122

120123
func snapshotRestoreCommandFunc(_ *cobra.Command, args []string) {
121124
SnapshotRestoreCommandFunc(restoreCluster, restoreClusterToken, restoreDataDir, restoreWalDir,
122-
restorePeerURLs, restoreName, skipHashCheck, revisionBump, markCompacted, args)
125+
restorePeerURLs, restoreName, skipHashCheck, initialMmapSize, revisionBump, markCompacted, args)
123126
}
124127

125128
func SnapshotRestoreCommandFunc(restoreCluster string,
@@ -129,6 +132,7 @@ func SnapshotRestoreCommandFunc(restoreCluster string,
129132
restorePeerURLs string,
130133
restoreName string,
131134
skipHashCheck bool,
135+
initialMmapSize uint64,
132136
revisionBump uint64,
133137
markCompacted bool,
134138
args []string) {
@@ -164,6 +168,7 @@ func SnapshotRestoreCommandFunc(restoreCluster string,
164168
InitialCluster: restoreCluster,
165169
InitialClusterToken: restoreClusterToken,
166170
SkipHashCheck: skipHashCheck,
171+
InitialMmapSize: initialMmapSize,
167172
RevisionBump: revisionBump,
168173
MarkCompacted: markCompacted,
169174
}); err != nil {

etcdutl/snapshot/v3_snapshot.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ type v3Manager struct {
8383
snapDir string
8484
cl *membership.RaftCluster
8585

86-
skipHashCheck bool
86+
skipHashCheck bool
87+
initialMmapSize uint64
8788
}
8889

8990
// hasChecksum returns "true" if the file size "n"
@@ -197,6 +198,9 @@ type RestoreConfig struct {
197198
// (required if copied from data directory).
198199
SkipHashCheck bool
199200

201+
// InitialMmapSize is the database initial memory map size.
202+
InitialMmapSize uint64
203+
200204
// RevisionBump is the amount to increase the latest revision after restore,
201205
// to allow administrators to trick clients into thinking that revision never decreased.
202206
// If 0, revision bumping is skipped.
@@ -256,13 +260,15 @@ func (s *v3Manager) Restore(cfg RestoreConfig) error {
256260
s.walDir = walDir
257261
s.snapDir = filepath.Join(dataDir, "member", "snap")
258262
s.skipHashCheck = cfg.SkipHashCheck
263+
s.initialMmapSize = cfg.InitialMmapSize
259264

260265
s.lg.Info(
261266
"restoring snapshot",
262267
zap.String("path", s.srcDbPath),
263268
zap.String("wal-dir", s.walDir),
264269
zap.String("data-dir", dataDir),
265270
zap.String("snap-dir", s.snapDir),
271+
zap.Uint64("initial-memory-map-size", s.initialMmapSize),
266272
)
267273

268274
if err = s.saveDB(); err != nil {
@@ -290,6 +296,7 @@ func (s *v3Manager) Restore(cfg RestoreConfig) error {
290296
zap.String("wal-dir", s.walDir),
291297
zap.String("data-dir", dataDir),
292298
zap.String("snap-dir", s.snapDir),
299+
zap.Uint64("initial-memory-map-size", s.initialMmapSize),
293300
)
294301

295302
return verify.VerifyIfEnabled(verify.Config{
@@ -310,7 +317,7 @@ func (s *v3Manager) saveDB() error {
310317
return err
311318
}
312319

313-
be := backend.NewDefaultBackend(s.outDbPath())
320+
be := backend.NewDefaultBackend(s.outDbPath(), backend.WithMmapSize(s.initialMmapSize))
314321
defer be.Close()
315322

316323
err = membership.TrimMembershipFromBackend(s.lg, be)
@@ -324,7 +331,7 @@ func (s *v3Manager) saveDB() error {
324331
// modifyLatestRevision can increase the latest revision by the given amount and sets the scheduled compaction
325332
// to that revision so that the server will consider this revision compacted.
326333
func (s *v3Manager) modifyLatestRevision(bumpAmount uint64) error {
327-
be := backend.NewDefaultBackend(s.outDbPath())
334+
be := backend.NewDefaultBackend(s.outDbPath(), backend.WithMmapSize(s.initialMmapSize))
328335
defer func() {
329336
be.ForceCommit()
330337
be.Close()
@@ -467,7 +474,7 @@ func (s *v3Manager) saveWALAndSnap() (*raftpb.HardState, error) {
467474
// add members again to persist them to the store we create.
468475
st := v2store.New(etcdserver.StoreClusterPrefix, etcdserver.StoreKeysPrefix)
469476
s.cl.SetStore(st)
470-
be := backend.NewDefaultBackend(s.outDbPath())
477+
be := backend.NewDefaultBackend(s.outDbPath(), backend.WithMmapSize(s.initialMmapSize))
471478
defer be.Close()
472479
s.cl.SetBackend(be)
473480
for _, m := range s.cl.Members() {

server/mvcc/backend/backend.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ var (
3636

3737
defragLimit = 10000
3838

39-
// initialMmapSize is the initial size of the mmapped region. Setting this larger than
39+
// InitialMmapSize is the initial size of the mmapped region. Setting this larger than
4040
// the potential max db size can prevent writer from blocking reader.
4141
// This only works for linux.
42-
initialMmapSize = uint64(10 * 1024 * 1024 * 1024)
42+
InitialMmapSize = uint64(10 * 1024 * 1024 * 1024)
4343

4444
// minSnapshotWarningTimeout is the minimum threshold to trigger a long running snapshot warning.
4545
minSnapshotWarningTimeout = 30 * time.Second
@@ -157,7 +157,7 @@ func DefaultBackendConfig() BackendConfig {
157157
return BackendConfig{
158158
BatchInterval: defaultBatchInterval,
159159
BatchLimit: defaultBatchLimit,
160-
MmapSize: initialMmapSize,
160+
MmapSize: InitialMmapSize,
161161
}
162162
}
163163

0 commit comments

Comments
 (0)