-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
493 lines (443 loc) · 15.3 KB
/
main.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"time"
viper "github.com/spf13/viper"
"google.golang.org/grpc"
pbIntra "fedota/fl-selector/genproto/fl_intra"
pbRound "fedota/fl-selector/genproto/fl_round"
)
var start time.Time
// constants
const (
varNumCheckins = iota
varNumUpdatesStart = iota
varNumUpdatesFinish = iota
varNumSelected = iota
)
// to handle read writes
type readOp struct {
varType int
response chan int
}
type writeOp struct {
varType int
// val int
response chan int
}
// server struct to implement gRPC Round service interface
type server struct {
selectorID string
coordinatorAddress string
flRootPath string
clientCountReads chan readOp
clientCountWrites chan writeOp
updateCountReads chan readOp
updateCountWrites chan writeOp
selected chan bool // hold clients before configuration starts
numCheckIns int
numSelected int
numUpdatesStart int
numUpdatesFinish int
}
func init() {
start = time.Now()
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath(".") // optionally look for config in the working directory
viper.AutomaticEnv() // enable viper to read env
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s", err))
}
// TODO: Add defaults for config using viper
}
func main() {
// Enable line numbers in logging
log.SetFlags(log.LstdFlags | log.Lshortfile)
port := viper.GetString("PORT")
selectorID := viper.GetString("HOSTNAME")
log.Println(viper.GetString("SELECTOR_SERVICE"))
if viper.IsSet("SELECTOR_SERVICE") {
selectorID += "." + viper.GetString("SELECTOR_SERVICE") + ":" + port
} else {
selectorID += ":" + port
}
log.Println(selectorID)
coordinatorAddress := viper.GetString("COORDINATOR_ADDRESS")
flRootPath := viper.GetString("FL_ROOT_PATH")
address := ":" + port
// listen
lis, err := net.Listen("tcp", address)
check(err, "Failed to listen on " + address)
srv := grpc.NewServer()
// server impl instance
flServer := &server{
selectorID: selectorID,
coordinatorAddress: coordinatorAddress,
flRootPath: flRootPath,
numCheckIns: 0,
numUpdatesStart: 0,
numUpdatesFinish: 0,
numSelected: 0,
clientCountReads: make(chan readOp),
clientCountWrites: make(chan writeOp),
updateCountReads: make(chan readOp),
updateCountWrites: make(chan writeOp),
selected: make(chan bool)}
// register FL round server
pbRound.RegisterFlRoundServer(srv, flServer)
// register FL intra broadcast server
pbIntra.RegisterFLGoalCountBroadcastServer(srv, flServer)
go flServer.ClientSelectionHandler()
go flServer.ClientUpdateConnectionHandler()
// start serving
log.Println("Starting server on port:", port, "Time:", time.Since(start))
err = srv.Serve(lis)
check(err, "Failed to serve on port "+port)
}
// Check In rpc
// Clients check in with FL selector
// Selector send count to Coordinator and waits for signal from it
func (s *server) CheckIn(stream pbRound.FlRound_CheckInServer) error {
var (
buf []byte
n int
)
// receive check-in request
checkinReq, err := stream.Recv()
check(err, "Unable to receive checkin request by client")
log.Println("CheckIn Request: Client Name: ", checkinReq.Message, "Time:", time.Since(start))
// create a write operation
write := writeOp{
varType: varNumCheckins,
response: make(chan int)}
// send to handler(ClientSelectionHandler) via writes channel
s.clientCountWrites <- write
// wait for start of configuration (by ClientSelectionHandler)
// <-write.response waits for that client to be selected by client
// selected clients then have to wait (on the s.selected channel) for configuration to be started once goal count is reached
if (<-write.response) == -1 || !(<-s.selected) {
// not selected
log.Println("Not selected")
err := stream.Send(&pbRound.FlData{
IntVal: viper.GetInt64("POST_CHECKIN_RECONNECTION_TIME"),
Type: pbRound.Type_FL_INT,
})
if err != nil {
log.Println("CheckIn: Unable to send reconnection time. Time:", time.Since(start))
log.Println(err)
return err
}
return nil
}
// Proceed with sending initial files
completeInitPath := filepath.Join(s.flRootPath, viper.GetString("INIT_FILES_PATH"))
err = filepath.Walk(completeInitPath, func(path string, info os.FileInfo, errX error) error {
// Skip if directory
if info.IsDir() {
return nil
}
// open file
file, err := os.Open(path)
if err != nil {
log.Println("CheckIn: Unable to open init file. Time:", time.Since(start))
log.Println(err)
return err
}
defer file.Close()
// make a buffer of a defined chunk size
buf = make([]byte, viper.GetInt64("CHUNK_SIZE"))
for {
// read the content (by using chunks)
n, err = file.Read(buf)
if err == io.EOF {
return nil
}
if err != nil {
log.Println("CheckIn: Unable to read init file. Time:", time.Since(start))
log.Println(err)
return err
}
// send the FL checkpoint Data (file chunk + type: FL checkpoint)
err = stream.Send(&pbRound.FlData{
Chunk: buf[:n],
Type: pbRound.Type_FL_FILES,
FilePath: info.Name(),
})
if err != nil {
log.Println("CheckIn: Unable to stream init file. Time:", time.Since(start))
log.Println(err)
return err
}
}
})
return err
}
// Update rpc
// Accumulate FL checkpoint update sent by client
func (s *server) Update(stream pbRound.FlRound_UpdateServer) error {
log.Println("Update Request: Time:", time.Since(start))
// create a write operation
write := writeOp{
varType: varNumUpdatesStart,
response: make(chan int)}
// send to handler (ClientUpdateConnectionHandler) via writes channel
s.updateCountWrites <- write
index := <-write.response
log.Println("Index : ", index)
// open the file to save checkpoint received
checkpointFilePath := filepath.Join(s.flRootPath, s.selectorID, viper.GetString("ROUND_CHECKPOINT_UPDATES_DIR")) + strconv.Itoa(index)
checkpointWeightPath := filepath.Join(s.flRootPath, s.selectorID, viper.GetString("ROUND_CHECKPOINT_WEIGHT_DIR")) + strconv.Itoa(index)
os.MkdirAll(path.Dir(checkpointFilePath), os.ModePerm)
os.MkdirAll(path.Dir(checkpointWeightPath), os.ModePerm)
file, err := os.OpenFile(checkpointFilePath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
log.Println("Update: Unable to open file. Time:", time.Since(start))
os.Remove(checkpointFilePath)
log.Println(err)
return err
}
defer file.Close()
for {
// receive Fl data
flData, err := stream.Recv()
// exit after data transfer completes
if err == io.EOF {
// create a write operation
write = writeOp{
varType: varNumUpdatesFinish,
response: make(chan int)}
// send to handler (ConnectionHandler) via writes channel
s.updateCountWrites <- write
<-write.response // proceed
return stream.SendAndClose(&pbRound.FlData{
IntVal: viper.GetInt64("POST_UPDATE_RECONNECTION_TIME"),
Type: pbRound.Type_FL_INT,
})
}
if err != nil {
log.Println("Update: Unable to receive file. Time:", time.Since(start))
os.Remove(checkpointFilePath)
log.Println(err)
return err
}
if flData.Type == pbRound.Type_FL_FILES {
// write data to file
_, err = file.Write(flData.Chunk)
if err != nil {
log.Println("Update: unable to write into file. Time:", time.Since(start))
os.Remove(checkpointFilePath)
return err
}
} else if flData.Type == pbRound.Type_FL_INT {
// write weight to file
os.MkdirAll(path.Dir(checkpointWeightPath), os.ModePerm)
weightFile, err := os.OpenFile(checkpointWeightPath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
log.Println("Update: Unable to open file. Time:", time.Since(start))
os.Remove(checkpointWeightPath)
log.Println(err)
return err
}
log.Println("Checkpoint weight: ", strconv.FormatInt(flData.IntVal, 10))
_, err = weightFile.WriteString(strconv.FormatInt(flData.IntVal, 10))
if err != nil {
log.Println("Update: Unable to write into weight file. Time:", time.Since(start))
os.Remove(checkpointWeightPath)
return err
}
defer weightFile.Close()
}
}
}
// Once broadcast to proceed with configuration phase is received from the coordinator
// based on the count of routines waiting on selected channel, they are sent messages to proceed
func (s *server) GoalCountReached(ctx context.Context, empty *pbIntra.Empty) (*pbIntra.Empty, error) {
log.Println("Broadcast Received")
// get the number of selected clients
read := readOp{
varType: varNumSelected,
response: make(chan int)}
s.clientCountReads <- read
numSelected := <-read.response
// get the number of checkIn clients
read = readOp{
varType: varNumCheckins,
response: make(chan int)}
s.clientCountReads <- read
numCheckIns := <-read.response
log.Println("GoalCountReached ==> CheckIns: ", numCheckIns, "Selected: ", numSelected, "Time:", time.Since(start))
// select num selected number of clients
for i := 0; i < numSelected; i++ {
s.selected <- true
}
// reject the rest
for i := 0; i < numCheckIns-numSelected; i++ {
s.selected <- false
}
return &pbIntra.Empty{}, nil
}
// Runs mid averaging (federated averaging wrt it clients)
// then stores the aggregated checkpoint and total weight to the coordinator
func (s *server) MidAveraging() {
// build arguments for federated averaging process
var argsList []string
var path = filepath.Join(s.flRootPath, s.selectorID)
argsList = append(argsList, "mid_averaging.py", "--cf", filepath.Join(path, viper.GetString("AGG_CHECKPOINT_FILE_PATH")), "--mf", filepath.Join(s.flRootPath, viper.GetString("INIT_FILES_PATH"), viper.GetString("MODEL_FILE")), "--u")
var totalWeight int64 = 0
for i := 1; i <= s.numUpdatesFinish; i++ {
checkpointFilePath := filepath.Join(path, viper.GetString("ROUND_CHECKPOINT_UPDATES_DIR")) + strconv.Itoa(i)
checkpointWeightPath := filepath.Join(path, viper.GetString("ROUND_CHECKPOINT_WEIGHT_DIR")) + strconv.Itoa(i)
data, err := ioutil.ReadFile(checkpointWeightPath)
if err != nil {
log.Println("MidAveraging: Unable to read checkpoint weight file. Time:", time.Since(start))
log.Println(err)
return
}
dataInt, _ := strconv.ParseInt(string(data), 10, 64)
totalWeight += dataInt
argsList = append(argsList, string(data), checkpointFilePath)
}
log.Println("MidAveraging ==> Arguments passed to federated averaging python file: ", argsList)
// model path
cmd := exec.Command("python", argsList...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Println("MidAveraging ==> Unable to run mid federated averaging. Time:", time.Since(start))
log.Println(err)
return
}
// store aggregated weight in file
aggCheckpointWeightPath := filepath.Join(path, viper.GetString("AGG_CHECKPOINT_WEIGHT_PATH"))
os.MkdirAll(path, os.ModePerm)
aggWeightFile, err := os.OpenFile(aggCheckpointWeightPath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
log.Println("Mid Averaging: Unable to open agg checkpoint weight file. Time:", time.Since(start))
os.Remove(aggCheckpointWeightPath)
log.Println(err)
return
}
defer aggWeightFile.Close()
_, err = aggWeightFile.WriteString(strconv.FormatInt(totalWeight, 10))
if err != nil {
log.Println("MidAveraging: unable to write to agg checkpoint weight. Time:", time.Since(start))
log.Println(err)
return
}
// send indication of mid averaging completed to coordinator
conn, err := grpc.Dial(s.coordinatorAddress, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Println("MidAveraging: unable to connect to coordinator. Time:", time.Since(start))
log.Println(err)
return
}
client := pbIntra.NewFlIntraClient(conn)
_, err = client.SelectorAggregationComplete(context.Background(), &pbIntra.SelectorId{Id: s.selectorID})
if err != nil {
log.Println("MidAveraging: unable to send aggregation complete to coordinator. Time:", time.Since(start))
log.Println(err)
return
}
log.Println("MidAveraging: sent aggregation complete to coordinator. Time", time.Since(start))
}
// Handler for communicating with coordinator for selection process for clients
func (s *server) ClientSelectionHandler() {
for {
select {
case read := <-s.clientCountReads:
switch read.varType {
case varNumCheckins:
log.Println("Selection Handler ==> Read Query:", read.varType, "Time:", time.Since(start))
read.response <- s.numCheckIns
case varNumSelected:
log.Println("Selection Handler ==> Read Query:", read.varType, "Time:", time.Since(start))
read.response <- s.numSelected
}
case write := <-s.clientCountWrites:
log.Println("Selection Handler ==> Write Query:", write.varType, "Time:", time.Since(start))
s.numCheckIns++
log.Println("Selection Handler ==> numCheckIns", s.numCheckIns, "Time:", time.Since(start))
// TODO reject clients if we know they won't be selected after limit is reached
// send client count to coordinator
conn, err := grpc.Dial(s.coordinatorAddress, grpc.WithInsecure(), grpc.WithBlock())
check(err, "Unable to connect to coordinator")
client := pbIntra.NewFlIntraClient(conn)
result, err := client.ClientCountUpdate(context.Background(), &pbIntra.ClientCount{Count: uint32(s.numCheckIns), Id: s.selectorID})
if err != nil {
log.Println("Selection Handler: unable to send client count. Time:", time.Since(start))
log.Println(err)
} else {
log.Println("Selection Handler ==> Sent client count", s.numCheckIns, ". Time:", time.Since(start))
// accepted connection
if result.Accepted {
s.numSelected++
// select the client for configuration
write.response <- s.numSelected
} else {
write.response <- -1
}
}
}
}
}
// Handler for maintaining counts of client connections updated received
func (s *server) ClientUpdateConnectionHandler() {
for {
select {
// read query
case read := <-s.updateCountReads:
log.Println("Update Handler ==> Read Query:", read.varType, "Time:", time.Since(start))
switch read.varType {
case varNumUpdatesStart:
read.response <- s.numUpdatesStart
case varNumUpdatesFinish:
read.response <- s.numUpdatesFinish
}
// write query
case write := <-s.updateCountWrites:
// log.Println("Update Handler ==> Write Query:", write.varType, "Time:", time.Since(start))
switch write.varType {
case varNumUpdatesStart:
s.numUpdatesStart++
log.Println("Update Handler ==> numUpdates", s.numUpdatesStart, "Time:", time.Since(start))
write.response <- s.numUpdatesStart
case varNumUpdatesFinish:
s.numUpdatesFinish++
log.Println("Update Handler ==> numUpdates: ", s.numUpdatesFinish, "Finish Time:", time.Since(start))
write.response <- s.numUpdatesStart
// if enough updates available, start FA
if s.numUpdatesFinish == s.numSelected {
// begin federated averaging process
log.Println("Update Handler ==> Begin Mid Federated Averaging", "Time:", time.Since(start))
s.MidAveraging()
s.resetFLVariables()
}
}
}
}
}
// reset variables
func (s *server) resetFLVariables() {
s.numCheckIns = 0
s.numSelected = 0
s.numUpdatesStart = 0
s.numUpdatesFinish = 0
}
// Check for error, log and exit if err
func check(err error, errorMsg string) {
if err != nil {
log.Fatalf(errorMsg, " ==> ", err)
}
}