blob: 2eb2898190eb377ced394611480e66b53ac37a62 [file] [log] [blame]
Giorgi Lekveishvilib8f089f2020-03-18 23:28:12 +04001package master
2
3import "context"
4import "log"
5import "math/rand"
6
7import "github.com/google/uuid"
8
9import "pcloud/api"
10
11type chunkServers struct {
12 address string
13}
14
15type BlobStatus int
16
17const (
18 NEW BlobStatus = iota
19)
20
21type ChunkStatus int
22
23const (
24 ASSIGNED ChunkStatus = iota
25 STORED
26)
27
28type chunkReplica struct {
29 chunkServer string
30 status ChunkStatus
31}
32
33type chunk struct {
34 id string
35 replica []chunkReplica
36}
37
38type blob struct {
39 id string
40 status BlobStatus
41 chunks []chunk
42}
43
44type MasterServer struct {
45 chunkServers []string
46 blobs []*blob
47}
48
49func NewMasterServer() *MasterServer {
50 return &MasterServer{}
51}
52
53func (s *MasterServer) AddChunkServer(
54 ctx context.Context,
55 request *api.AddChunkServerRequest) (*api.AddChunkServerResponse, error) {
56 s.chunkServers = append(s.chunkServers, request.Address)
57 log.Printf("Registered Chunk server: %s", request.Address)
58 return &api.AddChunkServerResponse{}, nil
59}
60
61func (s *MasterServer) CreateBlob(
62 ctx context.Context,
63 request *api.CreateBlobRequest) (*api.CreateBlobResponse, error) {
64 if int(request.NumReplicas) > len(s.chunkServers) {
65 return nil, nil
66 }
67 resp := api.CreateBlobResponse{
68 BlobId: uuid.New().String(),
69 Chunk: []*api.ChunkStorageMetadata{
70 {ChunkId: uuid.New().String()},
71 }}
72 ids := rand.Perm(len(s.chunkServers))
73 for i := 0; i < int(request.NumReplicas); i++ {
74 resp.Chunk[0].Server = append(
75 resp.Chunk[0].Server,
76 s.chunkServers[ids[i]])
77 }
78 return &resp, nil
79}
80
81func (s *MasterServer) GetBlobMetadata(
82 ctx context.Context,
83 request *api.GetBlobMetadataRequest) (*api.GetBlobMetadataResponse, error) {
84 return nil, nil
85}