topology_ec.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package topology
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/glog"
  4. "github.com/seaweedfs/seaweedfs/weed/pb"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  8. )
  9. type EcShardLocations struct {
  10. Collection string
  11. Locations [erasure_coding.TotalShardsCount][]*DataNode
  12. }
  13. func (t *Topology) SyncDataNodeEcShards(shardInfos []*master_pb.VolumeEcShardInformationMessage, dn *DataNode) (newShards, deletedShards []*erasure_coding.EcVolumeInfo) {
  14. // convert into in memory struct storage.VolumeInfo
  15. var shards []*erasure_coding.EcVolumeInfo
  16. for _, shardInfo := range shardInfos {
  17. // Create EcVolumeInfo directly with optimized format
  18. ecVolumeInfo := &erasure_coding.EcVolumeInfo{
  19. VolumeId: needle.VolumeId(shardInfo.Id),
  20. Collection: shardInfo.Collection,
  21. ShardBits: erasure_coding.ShardBits(shardInfo.EcIndexBits),
  22. DiskType: shardInfo.DiskType,
  23. DiskId: shardInfo.DiskId,
  24. ExpireAtSec: shardInfo.ExpireAtSec,
  25. ShardSizes: shardInfo.ShardSizes,
  26. }
  27. shards = append(shards, ecVolumeInfo)
  28. }
  29. // find out the delta volumes
  30. newShards, deletedShards = dn.UpdateEcShards(shards)
  31. for _, v := range newShards {
  32. t.RegisterEcShards(v, dn)
  33. }
  34. for _, v := range deletedShards {
  35. t.UnRegisterEcShards(v, dn)
  36. }
  37. return
  38. }
  39. func (t *Topology) IncrementalSyncDataNodeEcShards(newEcShards, deletedEcShards []*master_pb.VolumeEcShardInformationMessage, dn *DataNode) {
  40. // convert into in memory struct storage.VolumeInfo
  41. var newShards, deletedShards []*erasure_coding.EcVolumeInfo
  42. for _, shardInfo := range newEcShards {
  43. // Create EcVolumeInfo directly with optimized format
  44. ecVolumeInfo := &erasure_coding.EcVolumeInfo{
  45. VolumeId: needle.VolumeId(shardInfo.Id),
  46. Collection: shardInfo.Collection,
  47. ShardBits: erasure_coding.ShardBits(shardInfo.EcIndexBits),
  48. DiskType: shardInfo.DiskType,
  49. DiskId: shardInfo.DiskId,
  50. ExpireAtSec: shardInfo.ExpireAtSec,
  51. ShardSizes: shardInfo.ShardSizes,
  52. }
  53. newShards = append(newShards, ecVolumeInfo)
  54. }
  55. for _, shardInfo := range deletedEcShards {
  56. // Create EcVolumeInfo directly with optimized format
  57. ecVolumeInfo := &erasure_coding.EcVolumeInfo{
  58. VolumeId: needle.VolumeId(shardInfo.Id),
  59. Collection: shardInfo.Collection,
  60. ShardBits: erasure_coding.ShardBits(shardInfo.EcIndexBits),
  61. DiskType: shardInfo.DiskType,
  62. DiskId: shardInfo.DiskId,
  63. ExpireAtSec: shardInfo.ExpireAtSec,
  64. ShardSizes: shardInfo.ShardSizes,
  65. }
  66. deletedShards = append(deletedShards, ecVolumeInfo)
  67. }
  68. dn.DeltaUpdateEcShards(newShards, deletedShards)
  69. for _, v := range newShards {
  70. t.RegisterEcShards(v, dn)
  71. }
  72. for _, v := range deletedShards {
  73. t.UnRegisterEcShards(v, dn)
  74. }
  75. }
  76. func NewEcShardLocations(collection string) *EcShardLocations {
  77. return &EcShardLocations{
  78. Collection: collection,
  79. }
  80. }
  81. func (loc *EcShardLocations) AddShard(shardId erasure_coding.ShardId, dn *DataNode) (added bool) {
  82. dataNodes := loc.Locations[shardId]
  83. for _, n := range dataNodes {
  84. if n.Id() == dn.Id() {
  85. return false
  86. }
  87. }
  88. loc.Locations[shardId] = append(dataNodes, dn)
  89. return true
  90. }
  91. func (loc *EcShardLocations) DeleteShard(shardId erasure_coding.ShardId, dn *DataNode) (deleted bool) {
  92. dataNodes := loc.Locations[shardId]
  93. foundIndex := -1
  94. for index, n := range dataNodes {
  95. if n.Id() == dn.Id() {
  96. foundIndex = index
  97. }
  98. }
  99. if foundIndex < 0 {
  100. return false
  101. }
  102. loc.Locations[shardId] = append(dataNodes[:foundIndex], dataNodes[foundIndex+1:]...)
  103. return true
  104. }
  105. func (t *Topology) RegisterEcShards(ecShardInfos *erasure_coding.EcVolumeInfo, dn *DataNode) {
  106. t.ecShardMapLock.Lock()
  107. defer t.ecShardMapLock.Unlock()
  108. locations, found := t.ecShardMap[ecShardInfos.VolumeId]
  109. if !found {
  110. locations = NewEcShardLocations(ecShardInfos.Collection)
  111. t.ecShardMap[ecShardInfos.VolumeId] = locations
  112. }
  113. for _, shardId := range ecShardInfos.ShardIds() {
  114. locations.AddShard(shardId, dn)
  115. }
  116. }
  117. func (t *Topology) UnRegisterEcShards(ecShardInfos *erasure_coding.EcVolumeInfo, dn *DataNode) {
  118. glog.Infof("removing ec shard info:%+v", ecShardInfos)
  119. t.ecShardMapLock.Lock()
  120. defer t.ecShardMapLock.Unlock()
  121. locations, found := t.ecShardMap[ecShardInfos.VolumeId]
  122. if !found {
  123. return
  124. }
  125. for _, shardId := range ecShardInfos.ShardIds() {
  126. locations.DeleteShard(shardId, dn)
  127. }
  128. }
  129. func (t *Topology) LookupEcShards(vid needle.VolumeId) (locations *EcShardLocations, found bool) {
  130. t.ecShardMapLock.RLock()
  131. defer t.ecShardMapLock.RUnlock()
  132. locations, found = t.ecShardMap[vid]
  133. return
  134. }
  135. func (t *Topology) ListEcServersByCollection(collection string) (dataNodes []pb.ServerAddress) {
  136. t.ecShardMapLock.RLock()
  137. defer t.ecShardMapLock.RUnlock()
  138. dateNodeMap := make(map[pb.ServerAddress]bool)
  139. for _, ecVolumeLocation := range t.ecShardMap {
  140. if ecVolumeLocation.Collection == collection {
  141. for _, locations := range ecVolumeLocation.Locations {
  142. for _, loc := range locations {
  143. dateNodeMap[loc.ServerAddress()] = true
  144. }
  145. }
  146. }
  147. }
  148. for k, _ := range dateNodeMap {
  149. dataNodes = append(dataNodes, k)
  150. }
  151. return
  152. }
  153. func (t *Topology) DeleteEcCollection(collection string) {
  154. t.ecShardMapLock.Lock()
  155. defer t.ecShardMapLock.Unlock()
  156. var vids []needle.VolumeId
  157. for vid, ecVolumeLocation := range t.ecShardMap {
  158. if ecVolumeLocation.Collection == collection {
  159. vids = append(vids, vid)
  160. }
  161. }
  162. for _, vid := range vids {
  163. delete(t.ecShardMap, vid)
  164. }
  165. }