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