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