Skip to content

Commit

Permalink
add bexpr filters to the RestoreFromArchive helper.
Browse files Browse the repository at this point in the history
The operator can pass these as `-filter` arguments to `nomad operator
snapshot state` (and other commands in the future) to include only
desired data when reading the snapshot.
  • Loading branch information
tgross committed Jul 8, 2022
1 parent f7d7a13 commit bc12749
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 81 deletions.
33 changes: 29 additions & 4 deletions command/operator_snapshot_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"os"
"strings"

flaghelper "github.com/hashicorp/nomad/helper/flags"
"github.com/hashicorp/nomad/helper/raftutil"
"github.com/hashicorp/nomad/nomad"
"github.com/posener/complete"
)

Expand All @@ -16,13 +18,19 @@ type OperatorSnapshotStateCommand struct {

func (c *OperatorSnapshotStateCommand) Help() string {
helpText := `
Usage: nomad operator snapshot state <file>
Usage: nomad operator snapshot state [options] <file>
Displays a JSON representation of state in the snapshot.
To inspect the file "backup.snap":
$ nomad operator snapshot state backup.snap
Snapshot State Options:
-filter
Specifies an expression used to filter query results.
`
return strings.TrimSpace(helpText)
}
Expand All @@ -42,22 +50,39 @@ func (c *OperatorSnapshotStateCommand) Synopsis() string {
func (c *OperatorSnapshotStateCommand) Name() string { return "operator snapshot state" }

func (c *OperatorSnapshotStateCommand) Run(args []string) int {
var filterExpr flaghelper.StringFlag

flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }

flags.Var(&filterExpr, "filter", "")
if err := flags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err))
return 1
}

filter, err := nomad.NewFSMFilter(filterExpr.String())
if err != nil {
c.Ui.Error(fmt.Sprintf("Invalid filter expression %q: %s", filterExpr, err))
return 1
}

// Check that we either got no filename or exactly one.
if len(args) != 1 {
if len(flags.Args()) != 1 {
c.Ui.Error("This command takes one argument: <file>")
c.Ui.Error(commandErrorText(c))
return 1
}

path := args[0]
path := flags.Args()[0]
f, err := os.Open(path)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error opening snapshot file: %s", err))
return 1
}
defer f.Close()

state, meta, err := raftutil.RestoreFromArchive(f)
state, meta, err := raftutil.RestoreFromArchive(f, filter)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to read archive file: %s", err))
return 1
Expand Down
1 change: 1 addition & 0 deletions helper/raftutil/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type nomadFSM interface {
raft.FSM
State() *state.StateStore
Restore(io.ReadCloser) error
RestoreWithFilter(io.ReadCloser, *nomad.FSMFilter) error
}

type FSMHelper struct {
Expand Down
7 changes: 4 additions & 3 deletions helper/raftutil/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import (
"github.com/hashicorp/raft"

"github.com/hashicorp/nomad/helper/snapshot"
"github.com/hashicorp/nomad/nomad"
"github.com/hashicorp/nomad/nomad/state"
)

func RestoreFromArchive(archive io.Reader) (*state.StateStore, *raft.SnapshotMeta, error) {
func RestoreFromArchive(archive io.Reader, filter *nomad.FSMFilter) (*state.StateStore, *raft.SnapshotMeta, error) {
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 Restore, w is closed by CopySnapshot
// r is closed by RestoreFiltered, w is closed by CopySnapshot
r, w := io.Pipe()

errCh := make(chan error)
Expand All @@ -35,7 +36,7 @@ func RestoreFromArchive(archive io.Reader) (*state.StateStore, *raft.SnapshotMet
}
}()

err = fsm.Restore(r)
err = fsm.RestoreWithFilter(r, filter)
if err != nil {
return nil, nil, fmt.Errorf("failed to restore from snapshot: %w", err)
}
Expand Down
Loading

0 comments on commit bc12749

Please sign in to comment.