blob: b6e76fa21bcf977977b6e7fc3b185201834352db [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"
giolekva1f6577a2020-03-25 12:53:06 +04009 "google.golang.org/grpc"
Giorgi Lekveishvilib8f089f2020-03-18 23:28:12 +040010
giolekvac5126d92020-03-21 16:39:56 +040011 "pcloud/api"
12)
Giorgi Lekveishvilib8f089f2020-03-18 23:28:12 +040013
14type chunkServers struct {
15 address string
16}
17
18type BlobStatus int
19
20const (
21 NEW BlobStatus = iota
22)
23
24type ChunkStatus int
25
26const (
27 ASSIGNED ChunkStatus = iota
28 STORED
29)
30
31type chunkReplica struct {
32 chunkServer string
33 status ChunkStatus
34}
35
36type chunk struct {
37 id string
38 replica []chunkReplica
39}
40
41type blob struct {
42 id string
43 status BlobStatus
44 chunks []chunk
45}
46
47type MasterServer struct {
giolekva1f6577a2020-03-25 12:53:06 +040048 chunkServers []*chunkServer
Giorgi Lekveishvilib8f089f2020-03-18 23:28:12 +040049 blobs []*blob
50}
51
52func NewMasterServer() *MasterServer {
53 return &MasterServer{}
54}
55
56func (s *MasterServer) AddChunkServer(
57 ctx context.Context,
giolekva1f6577a2020-03-25 12:53:06 +040058 req *api.AddChunkServerRequest) (*api.AddChunkServerResponse, error) {
59 s.chunkServers = append(s.chunkServers, &chunkServer{
60 address: req.Address,
61 status: Healthy})
62 log.Printf("Registered Chunk server: %s", req.Address)
Giorgi Lekveishvilib8f089f2020-03-18 23:28:12 +040063 return &api.AddChunkServerResponse{}, nil
64}
65
66func (s *MasterServer) CreateBlob(
67 ctx context.Context,
giolekva1f6577a2020-03-25 12:53:06 +040068 req *api.CreateBlobRequest) (*api.CreateBlobResponse, error) {
69 if int(req.NumReplicas) > len(s.chunkServers) {
Giorgi Lekveishvilib8f089f2020-03-18 23:28:12 +040070 return nil, nil
71 }
72 resp := api.CreateBlobResponse{
73 BlobId: uuid.New().String(),
74 Chunk: []*api.ChunkStorageMetadata{
75 {ChunkId: uuid.New().String()},
76 }}
giolekva1f6577a2020-03-25 12:53:06 +040077 assigned := 0
78 chunkId := resp.Chunk[0].ChunkId
79 for i := range rand.Perm(len(s.chunkServers)) {
80 if assigned == int(req.NumReplicas) {
81 break
82 }
83 address := s.chunkServers[i].address
84 log.Printf(address)
85 var opts []grpc.DialOption
86 opts = append(opts, grpc.WithInsecure())
87 opts = append(opts, grpc.WithBlock())
88 conn, err := grpc.Dial(address, opts...)
89 if err != nil {
90 continue
91 }
92 defer conn.Close()
93 client := api.NewChunkStorageClient(conn)
94 createChunkReq := api.CreateChunkRequest{
95 ChunkId: chunkId,
96 Size: req.SizeBytes}
97 if assigned == 0 {
98 createChunkReq.Role = api.ReplicaRole_PRIMARY
99 } else {
100 createChunkReq.Role = api.ReplicaRole_SECONDARY
101 createChunkReq.PrimaryAddress = resp.Chunk[0].Server[0]
102 }
103 _, err = client.CreateChunk(ctx, &createChunkReq)
104 if err == nil {
105 resp.Chunk[0].Server = append(resp.Chunk[0].Server, address)
106 assigned++
107 }
Giorgi Lekveishvilib8f089f2020-03-18 23:28:12 +0400108 }
109 return &resp, nil
110}
111
112func (s *MasterServer) GetBlobMetadata(
113 ctx context.Context,
giolekva1f6577a2020-03-25 12:53:06 +0400114 req *api.GetBlobMetadataRequest) (*api.GetBlobMetadataResponse, error) {
Giorgi Lekveishvilib8f089f2020-03-18 23:28:12 +0400115 return nil, nil
116}