forked from asonawalla/gazette
-
Notifications
You must be signed in to change notification settings - Fork 3
/
list.go
178 lines (158 loc) · 5.34 KB
/
list.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package client
import (
"context"
"errors"
"sync/atomic"
"time"
pb "github.com/LiveRamp/gazette/v2/pkg/protocol"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
// PolledList performs periodic polls of a ListRequest. Its most recent
// polled result may be accessed via List.
type PolledList struct {
ctx context.Context
client pb.JournalClient
req pb.ListRequest
resp atomic.Value
}
// NewPolledList returns a PolledList of the ListRequest which is initialized and
// ready for immediate use, and which will regularly refresh with interval |dur|.
// An error encountered in the first List RPC is returned. Subsequent RPC errors
// will be logged as warnings and retried as part of regular refreshes.
func NewPolledList(ctx context.Context, client pb.JournalClient, dur time.Duration, req pb.ListRequest) (*PolledList, error) {
var resp, err = ListAllJournals(ctx, client, req)
if err != nil {
return nil, err
}
var pl = &PolledList{ctx: ctx, client: client, req: req}
pl.resp.Store(resp)
go pl.periodicRefresh(dur)
return pl, nil
}
// List returns the most recent ListResponse.
func (pl *PolledList) List() *pb.ListResponse { return pl.resp.Load().(*pb.ListResponse) }
func (pl *PolledList) periodicRefresh(dur time.Duration) {
var ticker = time.NewTicker(dur)
for {
select {
case <-ticker.C:
var resp, err = ListAllJournals(pl.ctx, pl.client, pl.req)
if err != nil {
log.WithFields(log.Fields{"err": err, "req": pl.req.String()}).
Warn("periodic List refresh failed (will retry)")
} else {
pl.resp.Store(resp)
}
case <-pl.ctx.Done():
ticker.Stop()
return
}
}
}
// ListAllJournals performs multiple List RPCs, as required to join across multiple
// ListResponse pages, and returns the complete ListResponse of the ListRequest.
// Any encountered error is returned.
func ListAllJournals(ctx context.Context, client pb.JournalClient, req pb.ListRequest) (*pb.ListResponse, error) {
var resp *pb.ListResponse
for {
// List RPCs may be dispatched to any broker.
if r, err := client.List(pb.WithDispatchDefault(ctx), &req, grpc.FailFast(false)); err != nil {
return resp, mapGRPCCtxErr(ctx, err)
} else if err = r.Validate(); err != nil {
return resp, err
} else if r.Status != pb.Status_OK {
return resp, errors.New(r.Status.String())
} else {
req.PageToken, r.NextPageToken = r.NextPageToken, ""
if resp == nil {
resp = r
} else {
resp.Journals = append(resp.Journals, r.Journals...)
}
}
if req.PageToken == "" {
break // All done.
}
}
if dr, ok := client.(pb.DispatchRouter); ok {
for _, j := range resp.Journals {
dr.UpdateRoute(j.Spec.Name.String(), &j.Route)
}
}
return resp, nil
}
// ApplyJournals invokes the Apply RPC.
func ApplyJournals(ctx context.Context, jc pb.JournalClient, req *pb.ApplyRequest) (*pb.ApplyResponse, error) {
return ApplyJournalsLimit(ctx, jc, req, 0)
}
// ApplyJournalsLimit is a helper function for applying changes to journals which
// may be larger than the configured etcd transaction size limit. The changes in
// |parentReq| will be sent serially in batches of size |maxTxnSize|. If
// |maxTxnSize| is 0 all changes will be attempted as part of a single
// transaction. This function will return the response of the final
// ShardClient.Apply call. Response validation or !OK status from Apply RPC are
// mapped to error. In the event of an error any unapplied changes will be
// available on |parentReq|.
func ApplyJournalsLimit(
ctx context.Context,
jc pb.JournalClient,
parentReq *pb.ApplyRequest,
maxTxnSize int,
) (*pb.ApplyResponse, error) {
var changes []pb.ApplyRequest_Change
if maxTxnSize == 0 {
maxTxnSize = len(parentReq.Changes)
}
var finalResp *pb.ApplyResponse
for len(parentReq.Changes) > 0 {
if len(parentReq.Changes) > maxTxnSize {
changes = parentReq.Changes[:maxTxnSize]
} else {
changes = parentReq.Changes
}
var req = &pb.ApplyRequest{}
for _, change := range changes {
req.Changes = append(req.Changes, change)
}
var resp *pb.ApplyResponse
var err error
if resp, err = jc.Apply(pb.WithDispatchDefault(ctx), req, grpc.FailFast(false)); err != nil {
return resp, err
} else if err = resp.Validate(); err != nil {
return resp, err
} else if resp.Status != pb.Status_OK {
return resp, errors.New(resp.Status.String())
}
finalResp = resp
parentReq.Changes = parentReq.Changes[len(changes):]
}
return finalResp, nil
}
// ListAllFragments performs multiple Fragments RPCs, as required to join across multiple
// FragmentsResponse pages, and returns the completed FragmentResponse.
// Any encountered error is returned.
func ListAllFragments(ctx context.Context, client pb.RoutedJournalClient, req pb.FragmentsRequest) (*pb.FragmentsResponse, error) {
var resp *pb.FragmentsResponse
var routedCtx = pb.WithDispatchItemRoute(ctx, client, req.Journal.String(), false)
for {
if r, err := client.Fragments(routedCtx, &req); err != nil {
return resp, mapGRPCCtxErr(ctx, err)
} else if err = r.Validate(); err != nil {
return resp, err
} else if r.Status != pb.Status_OK {
return resp, errors.New(r.Status.String())
} else {
req.NextPageToken, r.NextPageToken = r.NextPageToken, 0
if resp == nil {
resp = r
} else {
resp.Fragments = append(resp.Fragments, r.Fragments...)
}
if req.NextPageToken == 0 {
break // All done.
}
}
}
return resp, nil
}