-
Notifications
You must be signed in to change notification settings - Fork 30
/
store_grpc_server_create_shard.go
155 lines (125 loc) · 5.74 KB
/
store_grpc_server_create_shard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package store
import (
"fmt"
"github.com/chrislusf/glog"
"github.com/chrislusf/vasto/pb"
"github.com/chrislusf/vasto/topology"
"golang.org/x/net/context"
"os"
)
// CreateShard
// 1. if the shard is already created, do nothing
func (ss *storeServer) CreateShard(ctx context.Context, request *pb.CreateShardRequest) (*pb.CreateShardResponse, error) {
glog.V(1).Infof("%s create shard %v", ss.storeName, request)
err := ss.createShards(request.Keyspace, int(request.ServerId), int(request.ClusterSize), int(request.ReplicationFactor), false, func(shardId int) *topology.BootstrapPlan {
return &topology.BootstrapPlan{
ToClusterSize: int(request.ClusterSize),
}
})
if err != nil {
glog.Errorf("%s create keyspace %s: %v", ss.storeName, request.Keyspace, err)
return &pb.CreateShardResponse{
Error: err.Error(),
}, nil
}
return &pb.CreateShardResponse{
Error: "",
}, nil
}
func (ss *storeServer) createShards(keyspace string, serverId int, clusterSize, replicationFactor int, isCandidate bool, planGen func(shardId int) *topology.BootstrapPlan) error {
var existingPrimaryShards []*pb.ClusterNode
if cluster, found := ss.clusterListener.GetCluster(keyspace); found {
for i := 0; i < cluster.ExpectedSize(); i++ {
if n, ok := cluster.GetNode(i, 0); ok {
existingPrimaryShards = append(existingPrimaryShards, n)
} else {
glog.Errorf("missing server %d", i)
}
}
glog.V(1).Infof("%s existing shards: %+v", ss.storeName, existingPrimaryShards)
}
ss.clusterListener.AddNewKeyspace(keyspace, clusterSize, replicationFactor)
if _, found := ss.keyspaceShards.getShards(keyspace); found {
localShards, foundLocalShards := ss.getServerStatusInCluster(keyspace)
if !foundLocalShards {
return fmt.Errorf("%s missing local shard status for keyspace %s", ss.storeName, keyspace)
}
if int(localShards.ClusterSize) == clusterSize && int(localShards.ReplicationFactor) == replicationFactor {
return fmt.Errorf("%s keyspace %s already exists", ss.storeName, keyspace)
}
if serverId != int(localShards.Id) {
return fmt.Errorf("%s local server id = %d, not matching requested server id %d", ss.storeName, localShards.Id, serverId)
}
}
localShards := ss.getOrCreateServerStatusInCluster(keyspace, serverId, clusterSize, replicationFactor)
for _, clusterShard := range topology.LocalShards(serverId, clusterSize, replicationFactor) {
shardInfo, foundShardInfo := localShards.ShardMap[uint32(clusterShard.ShardId)]
if !foundShardInfo {
shardInfo = &pb.ShardInfo{
ServerId: uint32(serverId),
ShardId: uint32(clusterShard.ShardId),
KeyspaceName: keyspace,
ClusterSize: uint32(clusterSize),
ReplicationFactor: uint32(replicationFactor),
IsCandidate: isCandidate,
}
}
shard, foundShard := ss.keyspaceShards.getShard(keyspace, VastoShardId(clusterShard.ShardId))
if !foundShard {
glog.V(1).Infof("%s creating new shard %s", ss.storeName, shardInfo.IdentifierOnThisServer())
var shardCreationError error
if shard, shardCreationError = ss.openShard(shardInfo); shardCreationError != nil {
return fmt.Errorf("creating %s: %v", shardInfo.IdentifierOnThisServer(), shardCreationError)
}
glog.V(1).Infof("%s created new shard %s", ss.storeName, shard.String())
} else {
glog.V(1).Infof("%s found existing shard %s", ss.storeName, shard.String())
}
plan := planGen(clusterShard.ShardId)
glog.V(1).Infof("%s shard %s bootstrap plan: %s", ss.storeName, shardInfo.IdentifierOnThisServer(), plan.String())
if err := shard.startWithBootstrapPlan(plan, ss.selfAdminAddress(), existingPrimaryShards); err != nil {
return fmt.Errorf("%s bootstrap shard %v : %v", ss.storeName, shardInfo.IdentifierOnThisServer(), err)
}
localShards.ShardMap[uint32(clusterShard.ShardId)] = shardInfo
if !foundShardInfo {
ss.sendShardInfoToMaster(shardInfo, pb.ShardInfo_READY)
}
}
return ss.saveClusterConfig(localShards, keyspace)
}
func (ss *storeServer) startExistingNodes(keyspaceName string, storeStatus *pb.LocalShardsInCluster) error {
for _, shardInfo := range storeStatus.ShardMap {
shard, shardOpenError := ss.openShard(shardInfo)
if shardOpenError != nil {
return fmt.Errorf("%s open %s: %v", ss.storeName, shardInfo.IdentifierOnThisServer(), shardOpenError)
}
for fileId, meta := range shard.db.GetLiveFilesMetaData() {
glog.V(1).Infof("%s %d name:%s, level:%d size:%d SmallestKey:%s LargestKey:%s", ss.storeName, fileId, meta.Name, meta.Level, meta.Size, string(meta.SmallestKey), string(meta.LargestKey))
if meta.Level >= 6 {
shard.hasBackfilled = true
}
}
if err := shard.startWithBootstrapPlan(&topology.BootstrapPlan{
ToClusterSize: int(shardInfo.ClusterSize),
}, ss.selfAdminAddress(), nil); err != nil {
return fmt.Errorf("%s bootstrap shard %v : %v", ss.storeName, shardInfo.IdentifierOnThisServer(), err)
}
}
return nil
}
func (ss *storeServer) openShard(shardInfo *pb.ShardInfo) (shard *shard, err error) {
cluster := ss.clusterListener.GetOrSetCluster(shardInfo.KeyspaceName, int(shardInfo.ClusterSize), int(shardInfo.ReplicationFactor))
dir := fmt.Sprintf("%s/%s/%d", *ss.option.Dir, shardInfo.KeyspaceName, shardInfo.ShardId)
err = os.MkdirAll(dir, 0755)
if err != nil {
glog.V(1).Infof("%s mkdir %s: %v", ss.storeName, dir, err)
return nil, fmt.Errorf("%s mkdir %s: %v", ss.storeName, dir, err)
}
shard = newShard(shardInfo.KeyspaceName, dir, int(shardInfo.ServerId), int(shardInfo.ShardId), cluster, ss.clusterListener,
int(shardInfo.ReplicationFactor), *ss.option.LogFileSizeMb, *ss.option.LogFileCount)
shard.setCompactionFilterClusterSize(int(shardInfo.ClusterSize))
// println("loading shard", shard.String())
ss.keyspaceShards.addShards(shardInfo.KeyspaceName, shard)
ss.RegisterPeriodicTask(shard)
return shard, nil
}