This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
/
scheduler.go
276 lines (227 loc) · 6.64 KB
/
scheduler.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package scheduler
import (
"io/ioutil"
"log"
"sync"
"time"
orchestrator "code.cloudfoundry.org/go-orchestrator"
"code.cloudfoundry.org/log-cache/internal/routing"
rpc "code.cloudfoundry.org/log-cache/pkg/rpc/logcache_v1"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// Scheduler manages the routes of the Log Cache nodes.
type Scheduler struct {
log *log.Logger
interval time.Duration
count int
replicationFactor int
logCacheOrch *orchestrator.Orchestrator
dialOpts []grpc.DialOption
isLeader func() bool
logCacheClients []clientInfo
}
// NewScheduler returns a new Scheduler. Addrs are the addresses of the Cache
// nodes.
func NewScheduler(logCacheAddrs []string, opts ...SchedulerOption) *Scheduler {
s := &Scheduler{
log: log.New(ioutil.Discard, "", 0),
interval: time.Minute,
count: 100,
replicationFactor: 1,
dialOpts: []grpc.DialOption{grpc.WithInsecure()},
isLeader: func() bool { return true },
}
for _, o := range opts {
o(s)
}
s.logCacheOrch = orchestrator.New(&comm{
log: s.log,
isLeader: s.isLeader,
})
for _, addr := range logCacheAddrs {
conn, err := grpc.Dial(addr, s.dialOpts...)
if err != nil {
s.log.Panic(err)
}
s.logCacheClients = append(s.logCacheClients, clientInfo{l: rpc.NewOrchestrationClient(conn), addr: addr})
}
return s
}
// SchedulerOption configures a Scheduler.
type SchedulerOption func(*Scheduler)
// WithSchedulerLogger returns a SchedulerOption that configures the logger
// used for the Scheduler. Defaults to silent logger.
func WithSchedulerLogger(l *log.Logger) SchedulerOption {
return func(s *Scheduler) {
s.log = l
}
}
// WithSchedulerInterval returns a SchedulerOption that configures the
// interval for terms to take place. It defaults to a minute.
func WithSchedulerInterval(interval time.Duration) SchedulerOption {
return func(s *Scheduler) {
s.interval = interval
}
}
// WithSchedulerCount returns a SchedulerOption that configures the
// number of ranges to manage. Defaults to 100.
func WithSchedulerCount(count int) SchedulerOption {
return func(s *Scheduler) {
s.count = count
}
}
// WithSchedulerReplicationFactor returns a SchedulerOption that configures
// the replication factor for the Log Cache cluster. Replication factor is the
// total number of nodes to replicate data across. It defaults to 1 (meaning
// no replication).
func WithSchedulerReplicationFactor(replicationFactor int) SchedulerOption {
return func(s *Scheduler) {
s.replicationFactor = replicationFactor
}
}
// WithSchedulerDialOpts are the gRPC options used to dial peer Log Cache
// nodes. It defaults to WithInsecure().
func WithSchedulerDialOpts(opts ...grpc.DialOption) SchedulerOption {
return func(s *Scheduler) {
s.dialOpts = opts
}
}
// WithSchedulerLeadership sets the leadership decsision function that returns
// true if the scheduler node is the leader. Default to a fuction that returns
// true.
func WithSchedulerLeadership(isLeader func() bool) SchedulerOption {
return func(s *Scheduler) {
s.isLeader = isLeader
}
}
// Start starts the scheduler. It does not block.
func (s *Scheduler) Start() {
for _, lc := range s.logCacheClients {
s.logCacheOrch.AddWorker(lc)
}
maxHash := uint64(18446744073709551615)
x := maxHash / uint64(s.count)
var start uint64
for i := 0; i < s.count-1; i++ {
s.logCacheOrch.AddTask(routing.Range{
Start: start,
End: start + x,
},
orchestrator.WithTaskInstances(s.replicationFactor),
)
start += x + 1
}
s.logCacheOrch.AddTask(routing.Range{
Start: start,
End: maxHash,
},
orchestrator.WithTaskInstances(s.replicationFactor),
)
go func() {
// Waits until after the first run of the loop to read from t
// https://groups.google.com/forum/m/#!topic/golang-nuts/H_55uzPp98s
for t := time.Tick(s.interval); ; <-t {
// Apply changes
s.logCacheOrch.NextTerm(context.Background())
// Run again before setting remote tables to allow the
// orchestrator to go and query for updates.
s.logCacheOrch.NextTerm(context.Background())
if s.isLeader() {
s.setRemoteTables(s.logCacheClients, s.convertWorkerState(s.logCacheOrch.LastActual()))
}
}
}()
}
func (s *Scheduler) setRemoteTables(clients []clientInfo, m map[string]*rpc.Ranges) {
req := &rpc.SetRangesRequest{
Ranges: m,
}
for _, lc := range clients {
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
if _, err := lc.l.SetRanges(ctx, req); err != nil {
s.log.Printf("failed to set remote table: %s", err)
continue
}
}
}
func (s *Scheduler) convertWorkerState(ws []orchestrator.WorkerState) map[string]*rpc.Ranges {
m := make(map[string]*rpc.Ranges)
for _, w := range ws {
var ranges []*rpc.Range
for _, t := range w.Tasks {
sr := t.(routing.Range)
ranges = append(ranges, sr.ToRpcRange())
}
m[w.Name.(clientInfo).addr] = &rpc.Ranges{
Ranges: ranges,
}
}
return m
}
type clientInfo struct {
l rpc.OrchestrationClient
addr string
}
type comm struct {
isLeader func() bool
mu sync.Mutex
log *log.Logger
}
// List implements orchestrator.Communicator.
func (c *comm) List(ctx context.Context, worker interface{}) ([]interface{}, error) {
c.mu.Lock()
defer c.mu.Unlock()
lc := worker.(clientInfo)
ctx, _ = context.WithTimeout(ctx, 5*time.Second)
resp, err := lc.l.ListRanges(ctx, &rpc.ListRangesRequest{})
if err != nil {
c.log.Printf("failed to list ranges from %s: %s", lc.addr, err)
return nil, err
}
var results []interface{}
for _, r := range resp.Ranges {
var sr routing.Range
sr.CloneRpcRange(r)
results = append(results, sr)
}
return results, nil
}
// Add implements orchestrator.Communicator.
func (c *comm) Add(ctx context.Context, worker interface{}, task interface{}) error {
if !c.isLeader() {
return nil
}
c.mu.Lock()
defer c.mu.Unlock()
lc := worker.(clientInfo)
sr := task.(routing.Range)
ctx, _ = context.WithTimeout(ctx, 5*time.Second)
_, err := lc.l.AddRange(ctx, &rpc.AddRangeRequest{
Range: sr.ToRpcRange(),
})
if err != nil {
c.log.Printf("failed to add range to %s: %s", lc.addr, err)
return err
}
return nil
}
// Remove implements orchestrator.Communicator.
func (c *comm) Remove(ctx context.Context, worker interface{}, task interface{}) error {
if !c.isLeader() {
return nil
}
c.mu.Lock()
defer c.mu.Unlock()
lc := worker.(clientInfo)
sr := task.(routing.Range)
ctx, _ = context.WithTimeout(ctx, 5*time.Second)
_, err := lc.l.RemoveRange(ctx, &rpc.RemoveRangeRequest{
Range: sr.ToRpcRange(),
})
if err != nil {
c.log.Printf("failed to add range to %s: %s", lc.addr, err)
return err
}
return nil
}