Skip to content

Commit bdc6ca3

Browse files
committed
implemented in place for snapshots
1 parent 12ca463 commit bdc6ca3

File tree

10 files changed

+355
-322
lines changed

10 files changed

+355
-322
lines changed

client/client.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type Client interface {
5353
SendOutboundMessage(ctx context.Context, nodeName string, peerID string, op uint32, msgBody []byte) (*rpcpb.SendOutboundMessageResponse, error)
5454
Close() error
5555
SaveSnapshot(ctx context.Context, snapshotName string, force bool) (*rpcpb.SaveSnapshotResponse, error)
56-
LoadSnapshot(ctx context.Context, snapshotName string, opts ...OpOption) (*rpcpb.LoadSnapshotResponse, error)
56+
LoadSnapshot(ctx context.Context, snapshotName string, inPlace bool, opts ...OpOption) (*rpcpb.LoadSnapshotResponse, error)
5757
RemoveSnapshot(ctx context.Context, snapshotName string) (*rpcpb.RemoveSnapshotResponse, error)
5858
ListSnapshots(ctx context.Context) ([]string, error)
5959
ListSubnets(ctx context.Context) ([]string, error)
@@ -367,7 +367,7 @@ func (c *client) SaveSnapshot(ctx context.Context, snapshotName string, force bo
367367
return c.controlc.SaveSnapshot(ctx, &rpcpb.SaveSnapshotRequest{SnapshotName: snapshotName, Force: force})
368368
}
369369

370-
func (c *client) LoadSnapshot(ctx context.Context, snapshotName string, opts ...OpOption) (*rpcpb.LoadSnapshotResponse, error) {
370+
func (c *client) LoadSnapshot(ctx context.Context, snapshotName string, inPlace bool, opts ...OpOption) (*rpcpb.LoadSnapshotResponse, error) {
371371
c.log.Info("load snapshot", zap.String("snapshot-name", snapshotName))
372372
ret := &Op{}
373373
ret.applyOpts(opts)
@@ -376,6 +376,7 @@ func (c *client) LoadSnapshot(ctx context.Context, snapshotName string, opts ...
376376
ChainConfigs: ret.chainConfigs,
377377
UpgradeConfigs: ret.upgradeConfigs,
378378
SubnetConfigs: ret.subnetConfigs,
379+
InPlace: inPlace,
379380
}
380381
if ret.execPath != "" {
381382
req.ExecPath = &ret.execPath

cmd/control/control.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ var (
106106
dynamicPorts bool
107107
networkID uint32
108108
force bool
109+
inPlace bool
109110
)
110111

111112
func setLogs() error {
@@ -1281,6 +1282,12 @@ func newLoadSnapshotCommand() *cobra.Command {
12811282
false,
12821283
"true to reassign snapshot ports if already taken",
12831284
)
1285+
cmd.PersistentFlags().BoolVar(
1286+
&inPlace,
1287+
"in-place",
1288+
false,
1289+
"load snapshot in place, so as it always auto save",
1290+
)
12841291
return cmd
12851292
}
12861293

@@ -1333,7 +1340,7 @@ func loadSnapshotFunc(_ *cobra.Command, args []string) error {
13331340

13341341
ctx := getAsyncContext()
13351342

1336-
resp, err := cli.LoadSnapshot(ctx, args[0], opts...)
1343+
resp, err := cli.LoadSnapshot(ctx, args[0], inPlace, opts...)
13371344
if err != nil {
13381345
return err
13391346
}

local/network.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,18 @@ func (ln *localNetwork) addNode(nodeConfig node.Config) (node.Node, error) {
566566
return nil, fmt.Errorf("couldn't get node ID: %w", err)
567567
}
568568

569+
// If this node is a beacon, add its IP/ID to the beacon lists.
570+
// Note that we do this *after* we set this node's bootstrap IPs/IDs
571+
// so this node won't try to use itself as a beacon.
572+
if !isPausedNode && nodeConfig.IsBeacon {
573+
if err := ln.bootstraps.Add(beacon.New(nodeID, ips.IPPort{
574+
IP: net.ParseIP(nodeData.publicIP),
575+
Port: nodeData.p2pPort,
576+
})); err != nil {
577+
return nil, err
578+
}
579+
}
580+
569581
// Start the AvalancheGo node and pass it the flags defined above
570582
nodeProcess, err := ln.nodeProcessCreator.NewNodeProcess(nodeConfig, nodeData.args...)
571583
if err != nil {
@@ -611,15 +623,6 @@ func (ln *localNetwork) addNode(nodeConfig node.Config) (node.Node, error) {
611623
attachedPeers: map[string]peer.Peer{},
612624
}
613625
ln.nodes[node.name] = node
614-
// If this node is a beacon, add its IP/ID to the beacon lists.
615-
// Note that we do this *after* we set this node's bootstrap IPs/IDs
616-
// so this node won't try to use itself as a beacon.
617-
if !isPausedNode && nodeConfig.IsBeacon {
618-
err = ln.bootstraps.Add(beacon.New(nodeID, ips.IPPort{
619-
IP: net.ParseIP(nodeData.publicIP),
620-
Port: nodeData.p2pPort,
621-
}))
622-
}
623626
return node, ln.persistNetwork()
624627
}
625628

local/snapshot.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func NewNetworkFromSnapshot(
7373
reassignPortsIfUsed bool,
7474
redirectStdout bool,
7575
redirectStderr bool,
76+
inPlace bool,
7677
) (network.Network, error) {
7778
net, err := newNetwork(
7879
log,
@@ -101,6 +102,7 @@ func NewNetworkFromSnapshot(
101102
upgradeConfigs,
102103
subnetConfigs,
103104
flags,
105+
inPlace,
104106
)
105107
return net, err
106108
}
@@ -189,6 +191,10 @@ func (ln *localNetwork) SaveSnapshot(ctx context.Context, snapshotName string, f
189191
if _, err := os.Stat(snapshotDir); err == nil {
190192
exists = true
191193
}
194+
// check is network was loaded in place from the given snapshot
195+
if ln.rootDir == snapshotDir {
196+
return "", fmt.Errorf("already auto saving into the specified snapshot %q", snapshotName)
197+
}
192198
if !force && exists {
193199
return "", fmt.Errorf("snapshot %q already exists", snapshotName)
194200
}
@@ -201,7 +207,9 @@ func (ln *localNetwork) SaveSnapshot(ctx context.Context, snapshotName string, f
201207
}
202208
// remove if force save
203209
if force && exists {
204-
ln.RemoveSnapshot(snapshotName)
210+
if err := ln.RemoveSnapshot(snapshotName); err != nil {
211+
return "", err
212+
}
205213
}
206214
// copy all info
207215
if err := dircopy.Copy(ln.rootDir, snapshotDir); err != nil {
@@ -220,6 +228,7 @@ func (ln *localNetwork) loadSnapshot(
220228
upgradeConfigs map[string]string,
221229
subnetConfigs map[string]string,
222230
flags map[string]interface{},
231+
inPlace bool,
223232
) error {
224233
ln.lock.Lock()
225234
defer ln.lock.Unlock()
@@ -278,18 +287,19 @@ func (ln *localNetwork) loadSnapshot(
278287
}
279288
}
280289
// load snapshot dir
281-
if err := dircopy.Copy(snapshotDir, ln.rootDir); err != nil {
282-
return fmt.Errorf("failure loading snapshot root dir: %w", err)
290+
if !inPlace {
291+
if err := dircopy.Copy(snapshotDir, ln.rootDir); err != nil {
292+
return fmt.Errorf("failure loading snapshot root dir: %w", err)
293+
}
294+
} else {
295+
for _, nodeConfig := range networkConfig.NodeConfigs {
296+
// logs will be stored outside of the snapshot dir
297+
nodeConfig.Flags[config.LogsDirKey] = filepath.Join(ln.rootDir, nodeConfig.Name, "logs")
298+
}
299+
ln.rootDir = snapshotDir
283300
}
284301
// configure each node data dir
285302
for _, nodeConfig := range networkConfig.NodeConfigs {
286-
/*
287-
sourceDataDir := filepath.Join(snapshotDir, nodeConfig.Name)
288-
targetDataDir := filepath.Join(ln.rootDir, nodeConfig.Name)
289-
if err := dircopy.Copy(sourceDataDir, targetDataDir); err != nil {
290-
return fmt.Errorf("failure loading node %q data dir: %w", nodeConfig.Name, err)
291-
}
292-
*/
293303
nodeConfig.Flags[config.DataDirKey] = filepath.Join(ln.rootDir, nodeConfig.Name)
294304
}
295305
// replace binary path

0 commit comments

Comments
 (0)