2019-05-23 07:42:28 +00:00
|
|
|
package topology
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
|
|
|
)
|
|
|
|
|
2019-05-24 06:34:29 +00:00
|
|
|
type EcShardLocations struct {
|
|
|
|
Collection string
|
2019-05-28 07:13:13 +00:00
|
|
|
Locations [erasure_coding.TotalShardsCount][]*DataNode
|
2019-05-24 06:34:29 +00:00
|
|
|
}
|
2019-05-23 07:42:28 +00:00
|
|
|
|
|
|
|
func (t *Topology) SyncDataNodeEcShards(shardInfos []*master_pb.VolumeEcShardInformationMessage, dn *DataNode) (newShards, deletedShards []*erasure_coding.EcVolumeInfo) {
|
|
|
|
// convert into in memory struct storage.VolumeInfo
|
|
|
|
var shards []*erasure_coding.EcVolumeInfo
|
|
|
|
for _, shardInfo := range shardInfos {
|
2019-05-24 20:28:44 +00:00
|
|
|
shards = append(shards,
|
|
|
|
erasure_coding.NewEcVolumeInfo(
|
|
|
|
shardInfo.Collection,
|
|
|
|
needle.VolumeId(shardInfo.Id),
|
|
|
|
erasure_coding.ShardBits(shardInfo.EcIndexBits)))
|
2019-05-23 07:42:28 +00:00
|
|
|
}
|
|
|
|
// find out the delta volumes
|
|
|
|
newShards, deletedShards = dn.UpdateEcShards(shards)
|
|
|
|
for _, v := range newShards {
|
|
|
|
t.RegisterEcShards(v, dn)
|
|
|
|
}
|
|
|
|
for _, v := range deletedShards {
|
|
|
|
t.UnRegisterEcShards(v, dn)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-26 07:21:17 +00:00
|
|
|
func (t *Topology) IncrementalSyncDataNodeEcShards(newEcShards, deletedEcShards []*master_pb.VolumeEcShardInformationMessage, dn *DataNode) {
|
|
|
|
// convert into in memory struct storage.VolumeInfo
|
|
|
|
var newShards, deletedShards []*erasure_coding.EcVolumeInfo
|
|
|
|
for _, shardInfo := range newEcShards {
|
|
|
|
newShards = append(newShards,
|
|
|
|
erasure_coding.NewEcVolumeInfo(
|
|
|
|
shardInfo.Collection,
|
|
|
|
needle.VolumeId(shardInfo.Id),
|
|
|
|
erasure_coding.ShardBits(shardInfo.EcIndexBits)))
|
|
|
|
}
|
|
|
|
for _, shardInfo := range deletedEcShards {
|
|
|
|
deletedShards = append(deletedShards,
|
|
|
|
erasure_coding.NewEcVolumeInfo(
|
|
|
|
shardInfo.Collection,
|
|
|
|
needle.VolumeId(shardInfo.Id),
|
|
|
|
erasure_coding.ShardBits(shardInfo.EcIndexBits)))
|
|
|
|
}
|
|
|
|
|
|
|
|
dn.DeltaUpdateEcShards(newShards, deletedShards)
|
|
|
|
|
|
|
|
for _, v := range newShards {
|
|
|
|
t.RegisterEcShards(v, dn)
|
|
|
|
}
|
|
|
|
for _, v := range deletedShards {
|
|
|
|
t.UnRegisterEcShards(v, dn)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-24 06:34:29 +00:00
|
|
|
func NewEcShardLocations(collection string) *EcShardLocations {
|
|
|
|
return &EcShardLocations{
|
|
|
|
Collection: collection,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (loc *EcShardLocations) AddShard(shardId erasure_coding.ShardId, dn *DataNode) (added bool) {
|
2019-05-28 07:13:13 +00:00
|
|
|
dataNodes := loc.Locations[shardId]
|
2019-05-24 06:34:29 +00:00
|
|
|
for _, n := range dataNodes {
|
|
|
|
if n.Id() == dn.Id() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 07:13:13 +00:00
|
|
|
loc.Locations[shardId] = append(dataNodes, dn)
|
2019-05-24 06:34:29 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (loc *EcShardLocations) DeleteShard(shardId erasure_coding.ShardId, dn *DataNode) (deleted bool) {
|
2019-05-28 07:13:13 +00:00
|
|
|
dataNodes := loc.Locations[shardId]
|
2019-05-24 06:34:29 +00:00
|
|
|
foundIndex := -1
|
|
|
|
for index, n := range dataNodes {
|
|
|
|
if n.Id() == dn.Id() {
|
|
|
|
foundIndex = index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if foundIndex < 0 {
|
|
|
|
return false
|
|
|
|
}
|
2019-05-28 07:13:13 +00:00
|
|
|
loc.Locations[shardId] = append(dataNodes[:foundIndex], dataNodes[foundIndex+1:]...)
|
2019-05-24 06:34:29 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-05-23 07:42:28 +00:00
|
|
|
func (t *Topology) RegisterEcShards(ecShardInfos *erasure_coding.EcVolumeInfo, dn *DataNode) {
|
2019-05-24 06:34:29 +00:00
|
|
|
|
|
|
|
t.ecShardMapLock.Lock()
|
|
|
|
defer t.ecShardMapLock.Unlock()
|
|
|
|
|
|
|
|
locations, found := t.ecShardMap[ecShardInfos.VolumeId]
|
|
|
|
if !found {
|
|
|
|
locations = NewEcShardLocations(ecShardInfos.Collection)
|
|
|
|
t.ecShardMap[ecShardInfos.VolumeId] = locations
|
|
|
|
}
|
|
|
|
for _, shardId := range ecShardInfos.ShardIds() {
|
|
|
|
locations.AddShard(shardId, dn)
|
|
|
|
}
|
2019-05-23 07:42:28 +00:00
|
|
|
}
|
2019-05-24 06:34:29 +00:00
|
|
|
|
2019-05-23 07:42:28 +00:00
|
|
|
func (t *Topology) UnRegisterEcShards(ecShardInfos *erasure_coding.EcVolumeInfo, dn *DataNode) {
|
|
|
|
glog.Infof("removing ec shard info:%+v", ecShardInfos)
|
2019-05-24 06:34:29 +00:00
|
|
|
t.ecShardMapLock.Lock()
|
|
|
|
defer t.ecShardMapLock.Unlock()
|
|
|
|
|
|
|
|
locations, found := t.ecShardMap[ecShardInfos.VolumeId]
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, shardId := range ecShardInfos.ShardIds() {
|
|
|
|
locations.DeleteShard(shardId, dn)
|
|
|
|
}
|
2019-05-23 07:42:28 +00:00
|
|
|
}
|
2019-05-28 07:13:13 +00:00
|
|
|
|
2019-05-29 04:29:07 +00:00
|
|
|
func (t *Topology) LookupEcShards(vid needle.VolumeId) (locations *EcShardLocations, found bool) {
|
2019-05-28 07:13:13 +00:00
|
|
|
t.ecShardMapLock.RLock()
|
|
|
|
defer t.ecShardMapLock.RUnlock()
|
|
|
|
|
|
|
|
locations, found = t.ecShardMap[vid]
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|