forked from asonawalla/gazette
-
Notifications
You must be signed in to change notification settings - Fork 3
/
journals_apply.go
77 lines (64 loc) · 2.3 KB
/
journals_apply.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
package main
import (
"context"
"os"
"github.com/LiveRamp/gazette/v2/pkg/client"
mbp "github.com/LiveRamp/gazette/v2/pkg/mainboilerplate"
pb "github.com/LiveRamp/gazette/v2/pkg/protocol"
"github.com/LiveRamp/gazette/v2/pkg/protocol/journalspace"
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
"github.com/gogo/protobuf/proto"
log "github.com/sirupsen/logrus"
)
type cmdJournalsApply struct {
ApplyConfig
}
func (cmd *cmdJournalsApply) Execute([]string) error {
startup()
// Decode journal specification tree from YAML.
var tree journalspace.Node
mbp.Must(cmd.decode(&tree), "failed to decode journal tree")
mbp.Must(tree.Validate(), "journal tree failed to validate")
var req = newJournalSpecApplyRequest(&tree)
mbp.Must(req.Validate(), "failed to validate ApplyRequest")
if cmd.DryRun {
_ = proto.MarshalText(os.Stdout, req)
return nil
}
var ctx = context.Background()
resp, err := client.ApplyJournalsInBatches(ctx, journalsCfg.Broker.JournalClient(ctx), req, cmd.MaxTxnSize)
if err == rpctypes.ErrGRPCTooManyOps {
tooManyOpsPanic(len(req.Changes))
}
mbp.Must(err, "failed to apply journals")
log.WithField("rev", resp.Header.Etcd.Revision).Info("successfully applied")
return nil
}
// newJournalSpecApplyRequest flattens a journal specification tree into
// concrete JournalSpecs and builds the request.
func newJournalSpecApplyRequest(tree *journalspace.Node) *pb.ApplyRequest {
var req = new(pb.ApplyRequest)
tree.PushDown()
_ = tree.WalkTerminalNodes(func(node *journalspace.Node) error {
var change = pb.ApplyRequest_Change{ExpectModRevision: node.Revision}
if node.Delete != nil && *node.Delete {
change.Delete = node.Spec.Name
} else {
change.Upsert = &node.Spec
}
req.Changes = append(req.Changes, change)
return nil
})
return req
}
func tooManyOpsPanic(num int) {
log.Panicf(`
This operation has generated more changes (%d) than are possible in a single etcd
transaction given the current server configation (default 128). Gazctl supports a
max transaction size flag (--max-txn-size) which will send the changes in
batches of at most the max transaction size, however this means a loss
of transactionality and should be used with caution. Instead it is recomended
that additional label selectors are used to limit the number of changes within
this operation.
`, num)
}