-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.go
74 lines (62 loc) · 1.7 KB
/
filesystem.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
package filesystem
import (
"context"
"log"
"time"
pb "github.com/poy/talaria/api/v1"
"google.golang.org/grpc"
)
type FileSystem struct {
schedClient pb.SchedulerClient
nodeAddrConverter map[string]string
bufferSize uint64
}
func New(bufferSize uint64, addr string, nodeAddrConverter map[string]string) *FileSystem {
return &FileSystem{
bufferSize: bufferSize,
schedClient: setupSchedClient(addr),
nodeAddrConverter: nodeAddrConverter,
}
}
func (f *FileSystem) Create(file string) (err error) {
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
_, err = f.schedClient.Create(ctx, &pb.CreateInfo{
Name: file,
BufferSize: f.bufferSize,
})
return err
}
func (f *FileSystem) List() (files []string, err error) {
m, err := f.Routes()
if err != nil {
return nil, err
}
for name, _ := range m {
files = append(files, name)
}
return files, nil
}
func (f *FileSystem) Routes() (routes map[string]string, err error) {
routes = make(map[string]string)
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
resp, err := f.schedClient.ListClusterInfo(ctx, new(pb.ListInfo))
if err != nil {
log.Printf("Failed to list cluster info: %s", err)
return nil, err
}
for _, info := range resp.Info {
addr, ok := f.nodeAddrConverter[info.Leader]
if !ok {
log.Printf("Unknown node address (info=%+v) (name=%s): '%s'\n", info, info.Name, info.Leader)
}
routes[info.Name] = addr
}
return routes, nil
}
func setupSchedClient(addr string) (client pb.SchedulerClient) {
conn, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("unable to connect to scheduler: %v", err)
}
return pb.NewSchedulerClient(conn)
}