| Giorgi Lekveishvili | b8f089f | 2020-03-18 23:28:12 +0400 | [diff] [blame] | 1 | package master |
| 2 | |
| 3 | import "context" |
| 4 | import "log" |
| 5 | import "math/rand" |
| 6 | |
| 7 | import "github.com/google/uuid" |
| 8 | |
| 9 | import "pcloud/api" |
| 10 | |
| 11 | type chunkServers struct { |
| 12 | address string |
| 13 | } |
| 14 | |
| 15 | type BlobStatus int |
| 16 | |
| 17 | const ( |
| 18 | NEW BlobStatus = iota |
| 19 | ) |
| 20 | |
| 21 | type ChunkStatus int |
| 22 | |
| 23 | const ( |
| 24 | ASSIGNED ChunkStatus = iota |
| 25 | STORED |
| 26 | ) |
| 27 | |
| 28 | type chunkReplica struct { |
| 29 | chunkServer string |
| 30 | status ChunkStatus |
| 31 | } |
| 32 | |
| 33 | type chunk struct { |
| 34 | id string |
| 35 | replica []chunkReplica |
| 36 | } |
| 37 | |
| 38 | type blob struct { |
| 39 | id string |
| 40 | status BlobStatus |
| 41 | chunks []chunk |
| 42 | } |
| 43 | |
| 44 | type MasterServer struct { |
| 45 | chunkServers []string |
| 46 | blobs []*blob |
| 47 | } |
| 48 | |
| 49 | func NewMasterServer() *MasterServer { |
| 50 | return &MasterServer{} |
| 51 | } |
| 52 | |
| 53 | func (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 | |
| 61 | func (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 | |
| 81 | func (s *MasterServer) GetBlobMetadata( |
| 82 | ctx context.Context, |
| 83 | request *api.GetBlobMetadataRequest) (*api.GetBlobMetadataResponse, error) { |
| 84 | return nil, nil |
| 85 | } |