forked from asonawalla/gazette
/
shard_recover.go
79 lines (66 loc) · 1.89 KB
/
shard_recover.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package cmd
import (
"context"
"encoding/json"
"os"
"path"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/LiveRamp/gazette/pkg/gazette"
"github.com/LiveRamp/gazette/pkg/recoverylog"
)
var shardCmd = &cobra.Command{
Use: "shard",
Short: "Commands for working with a gazette consumer shard",
}
var shardRecoverCmd = &cobra.Command{
Use: "recover [hints-locator] [local-output-path]",
Short: "Recover contents of the indicated log hints.",
Long: `Recover replays the recoverylog indicated by the argument hints
into a local output directory, and additionally writes recovered JSON hints
upon completion.
hints-locator may be a path to a local file ("path/to/hints") or an Etcd
key specifier ("etcd:///path/to/hints").
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
cmd.Usage()
log.Fatal("invalid arguments")
}
var hints, tgtPath = loadHints(args[0]), args[1]
player, err := recoverylog.NewPlayer(hints, tgtPath)
if err != nil {
log.WithField("err", err).Fatal("preparing playback")
}
go func() {
if err := player.PlayContext(
context.Background(),
struct {
*gazette.Client
*gazette.WriteService
}{gazetteClient(), writeService()},
); err != nil {
log.WithField("err", err).Fatal("shard playback failed")
}
}()
var fsm = player.FinishAtWriteHead()
if fsm == nil {
return
}
// Write recovered hints under |tgtPath|.
var recoveredPath = path.Join(tgtPath, "recoveredHints.json")
fout, err := os.Create(recoveredPath)
if err == nil {
err = json.NewEncoder(fout).Encode(fsm.BuildHints())
}
if err != nil {
log.WithFields(log.Fields{"err": err, "path": recoveredPath}).Fatal("failed to write recovered hints")
} else {
log.WithField("path", recoveredPath).Info("wrote recovered hints")
}
},
}
func init() {
rootCmd.AddCommand(shardCmd)
shardCmd.AddCommand(shardRecoverCmd)
}