open-nomad/helper/raftutil/snapshot.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1 KiB
Go
Raw Normal View History

2021-11-04 14:16:12 +00:00
package raftutil
import (
"fmt"
"io"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/raft"
2021-11-04 14:16:12 +00:00
"github.com/hashicorp/nomad/helper/snapshot"
"github.com/hashicorp/nomad/nomad"
2021-11-04 14:16:12 +00:00
"github.com/hashicorp/nomad/nomad/state"
)
func RestoreFromArchive(archive io.Reader, filter *nomad.FSMFilter) (*state.StateStore, *raft.SnapshotMeta, error) {
2021-11-04 14:16:12 +00:00
logger := hclog.L()
fsm, err := dummyFSM(logger)
if err != nil {
return nil, nil, fmt.Errorf("failed to create FSM: %w", err)
}
// r is closed by RestoreFiltered, w is closed by CopySnapshot
r, w := io.Pipe()
2021-11-04 14:16:12 +00:00
errCh := make(chan error)
metaCh := make(chan *raft.SnapshotMeta)
2021-11-04 14:16:12 +00:00
go func() {
meta, err := snapshot.CopySnapshot(archive, w)
if err != nil {
errCh <- fmt.Errorf("failed to read snapshot: %w", err)
} else {
metaCh <- meta
}
}()
2021-11-04 14:16:12 +00:00
err = fsm.RestoreWithFilter(r, filter)
2021-11-04 14:16:12 +00:00
if err != nil {
return nil, nil, fmt.Errorf("failed to restore from snapshot: %w", err)
}
select {
case err := <-errCh:
return nil, nil, err
case meta := <-metaCh:
return fsm.State(), meta, nil
}
2021-11-04 14:16:12 +00:00
}