command_ec_common.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "math"
  6. "sort"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  11. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  12. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  13. "google.golang.org/grpc"
  14. )
  15. func moveMountedShardToEcNode(ctx context.Context, commandEnv *CommandEnv, existingLocation *EcNode, collection string, vid needle.VolumeId, shardId erasure_coding.ShardId, destinationEcNode *EcNode, applyBalancing bool) (err error) {
  16. copiedShardIds := []uint32{uint32(shardId)}
  17. if applyBalancing {
  18. // ask destination node to copy shard and the ecx file from source node, and mount it
  19. copiedShardIds, err = oneServerCopyAndMountEcShardsFromSource(ctx, commandEnv.option.GrpcDialOption, destinationEcNode, uint32(shardId), 1, vid, collection, existingLocation.info.Id)
  20. if err != nil {
  21. return err
  22. }
  23. // unmount the to be deleted shards
  24. err = unmountEcShards(ctx, commandEnv.option.GrpcDialOption, vid, existingLocation.info.Id, copiedShardIds)
  25. if err != nil {
  26. return err
  27. }
  28. // ask source node to delete the shard, and maybe the ecx file
  29. err = sourceServerDeleteEcShards(ctx, commandEnv.option.GrpcDialOption, collection, vid, existingLocation.info.Id, copiedShardIds)
  30. if err != nil {
  31. return err
  32. }
  33. fmt.Printf("moved ec shard %d.%d %s => %s\n", vid, shardId, existingLocation.info.Id, destinationEcNode.info.Id)
  34. }
  35. destinationEcNode.addEcVolumeShards(vid, collection, copiedShardIds)
  36. existingLocation.deleteEcVolumeShards(vid, copiedShardIds)
  37. return nil
  38. }
  39. func oneServerCopyAndMountEcShardsFromSource(ctx context.Context, grpcDialOption grpc.DialOption,
  40. targetServer *EcNode, startFromShardId uint32, shardCount int,
  41. volumeId needle.VolumeId, collection string, existingLocation string) (copiedShardIds []uint32, err error) {
  42. var shardIdsToCopy []uint32
  43. for shardId := startFromShardId; shardId < startFromShardId+uint32(shardCount); shardId++ {
  44. shardIdsToCopy = append(shardIdsToCopy, shardId)
  45. }
  46. fmt.Printf("allocate %d.%v %s => %s\n", volumeId, shardIdsToCopy, existingLocation, targetServer.info.Id)
  47. err = operation.WithVolumeServerClient(targetServer.info.Id, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  48. if targetServer.info.Id != existingLocation {
  49. fmt.Printf("copy %d.%v %s => %s\n", volumeId, shardIdsToCopy, existingLocation, targetServer.info.Id)
  50. _, copyErr := volumeServerClient.VolumeEcShardsCopy(ctx, &volume_server_pb.VolumeEcShardsCopyRequest{
  51. VolumeId: uint32(volumeId),
  52. Collection: collection,
  53. ShardIds: shardIdsToCopy,
  54. CopyEcxFile: true,
  55. SourceDataNode: existingLocation,
  56. })
  57. if copyErr != nil {
  58. return fmt.Errorf("copy %d.%v %s => %s : %v\n", volumeId, shardIdsToCopy, existingLocation, targetServer.info.Id, copyErr)
  59. }
  60. }
  61. fmt.Printf("mount %d.%v on %s\n", volumeId, shardIdsToCopy, targetServer.info.Id)
  62. _, mountErr := volumeServerClient.VolumeEcShardsMount(ctx, &volume_server_pb.VolumeEcShardsMountRequest{
  63. VolumeId: uint32(volumeId),
  64. Collection: collection,
  65. ShardIds: shardIdsToCopy,
  66. })
  67. if mountErr != nil {
  68. return fmt.Errorf("mount %d.%v on %s : %v\n", volumeId, shardIdsToCopy, targetServer.info.Id, mountErr)
  69. }
  70. if targetServer.info.Id != existingLocation {
  71. copiedShardIds = shardIdsToCopy
  72. glog.V(0).Infof("%s ec volume %d deletes shards %+v", existingLocation, volumeId, copiedShardIds)
  73. }
  74. return nil
  75. })
  76. if err != nil {
  77. return
  78. }
  79. return
  80. }
  81. func eachDataNode(topo *master_pb.TopologyInfo, fn func(dc string, rack RackId, dn *master_pb.DataNodeInfo)) {
  82. for _, dc := range topo.DataCenterInfos {
  83. for _, rack := range dc.RackInfos {
  84. for _, dn := range rack.DataNodeInfos {
  85. fn(dc.Id, RackId(rack.Id), dn)
  86. }
  87. }
  88. }
  89. }
  90. func sortEcNodesByFreeslotsDecending(ecNodes []*EcNode) {
  91. sort.Slice(ecNodes, func(i, j int) bool {
  92. return ecNodes[i].freeEcSlot > ecNodes[j].freeEcSlot
  93. })
  94. }
  95. func sortEcNodesByFreeslotsAscending(ecNodes []*EcNode) {
  96. sort.Slice(ecNodes, func(i, j int) bool {
  97. return ecNodes[i].freeEcSlot < ecNodes[j].freeEcSlot
  98. })
  99. }
  100. type CandidateEcNode struct {
  101. ecNode *EcNode
  102. shardCount int
  103. }
  104. // if the index node changed the freeEcSlot, need to keep every EcNode still sorted
  105. func ensureSortedEcNodes(data []*CandidateEcNode, index int, lessThan func(i, j int) bool) {
  106. for i := index - 1; i >= 0; i-- {
  107. if lessThan(i+1, i) {
  108. swap(data, i, i+1)
  109. } else {
  110. break
  111. }
  112. }
  113. for i := index + 1; i < len(data); i++ {
  114. if lessThan(i, i-1) {
  115. swap(data, i, i-1)
  116. } else {
  117. break
  118. }
  119. }
  120. }
  121. func swap(data []*CandidateEcNode, i, j int) {
  122. t := data[i]
  123. data[i] = data[j]
  124. data[j] = t
  125. }
  126. func countShards(ecShardInfos []*master_pb.VolumeEcShardInformationMessage) (count int) {
  127. for _, ecShardInfo := range ecShardInfos {
  128. shardBits := erasure_coding.ShardBits(ecShardInfo.EcIndexBits)
  129. count += shardBits.ShardIdCount()
  130. }
  131. return
  132. }
  133. func countFreeShardSlots(dn *master_pb.DataNodeInfo) (count int) {
  134. return int(dn.MaxVolumeCount-dn.ActiveVolumeCount)*erasure_coding.DataShardsCount - countShards(dn.EcShardInfos)
  135. }
  136. type RackId string
  137. type EcNodeId string
  138. type EcNode struct {
  139. info *master_pb.DataNodeInfo
  140. dc string
  141. rack RackId
  142. freeEcSlot int
  143. }
  144. type EcRack struct {
  145. ecNodes map[EcNodeId]*EcNode
  146. freeEcSlot int
  147. }
  148. func collectEcNodes(ctx context.Context, commandEnv *CommandEnv, selectedDataCenter string) (ecNodes []*EcNode, totalFreeEcSlots int, err error) {
  149. // list all possible locations
  150. var resp *master_pb.VolumeListResponse
  151. err = commandEnv.MasterClient.WithClient(ctx, func(client master_pb.SeaweedClient) error {
  152. resp, err = client.VolumeList(ctx, &master_pb.VolumeListRequest{})
  153. return err
  154. })
  155. if err != nil {
  156. return nil, 0, err
  157. }
  158. // find out all volume servers with one slot left.
  159. eachDataNode(resp.TopologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  160. if selectedDataCenter != "" && selectedDataCenter != dc {
  161. return
  162. }
  163. freeEcSlots := countFreeShardSlots(dn)
  164. ecNodes = append(ecNodes, &EcNode{
  165. info: dn,
  166. dc: dc,
  167. rack: rack,
  168. freeEcSlot: int(freeEcSlots),
  169. })
  170. totalFreeEcSlots += freeEcSlots
  171. })
  172. sortEcNodesByFreeslotsDecending(ecNodes)
  173. return
  174. }
  175. func sourceServerDeleteEcShards(ctx context.Context, grpcDialOption grpc.DialOption,
  176. collection string, volumeId needle.VolumeId, sourceLocation string, toBeDeletedShardIds []uint32) error {
  177. fmt.Printf("delete %d.%v from %s\n", volumeId, toBeDeletedShardIds, sourceLocation)
  178. return operation.WithVolumeServerClient(sourceLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  179. _, deleteErr := volumeServerClient.VolumeEcShardsDelete(ctx, &volume_server_pb.VolumeEcShardsDeleteRequest{
  180. VolumeId: uint32(volumeId),
  181. Collection: collection,
  182. ShardIds: toBeDeletedShardIds,
  183. })
  184. return deleteErr
  185. })
  186. }
  187. func unmountEcShards(ctx context.Context, grpcDialOption grpc.DialOption,
  188. volumeId needle.VolumeId, sourceLocation string, toBeUnmountedhardIds []uint32) error {
  189. fmt.Printf("unmount %d.%v from %s\n", volumeId, toBeUnmountedhardIds, sourceLocation)
  190. return operation.WithVolumeServerClient(sourceLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  191. _, deleteErr := volumeServerClient.VolumeEcShardsUnmount(ctx, &volume_server_pb.VolumeEcShardsUnmountRequest{
  192. VolumeId: uint32(volumeId),
  193. ShardIds: toBeUnmountedhardIds,
  194. })
  195. return deleteErr
  196. })
  197. }
  198. func mountEcShards(ctx context.Context, grpcDialOption grpc.DialOption,
  199. collection string, volumeId needle.VolumeId, sourceLocation string, toBeMountedhardIds []uint32) error {
  200. fmt.Printf("mount %d.%v on %s\n", volumeId, toBeMountedhardIds, sourceLocation)
  201. return operation.WithVolumeServerClient(sourceLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  202. _, mountErr := volumeServerClient.VolumeEcShardsMount(ctx, &volume_server_pb.VolumeEcShardsMountRequest{
  203. VolumeId: uint32(volumeId),
  204. Collection: collection,
  205. ShardIds: toBeMountedhardIds,
  206. })
  207. return mountErr
  208. })
  209. }
  210. func ceilDivide(total, n int) int {
  211. return int(math.Ceil(float64(total) / float64(n)))
  212. }
  213. func findEcVolumeShards(ecNode *EcNode, vid needle.VolumeId) erasure_coding.ShardBits {
  214. for _, shardInfo := range ecNode.info.EcShardInfos {
  215. if needle.VolumeId(shardInfo.Id) == vid {
  216. return erasure_coding.ShardBits(shardInfo.EcIndexBits)
  217. }
  218. }
  219. return 0
  220. }
  221. func (ecNode *EcNode) addEcVolumeShards(vid needle.VolumeId, collection string, shardIds []uint32) *EcNode {
  222. foundVolume := false
  223. for _, shardInfo := range ecNode.info.EcShardInfos {
  224. if needle.VolumeId(shardInfo.Id) == vid {
  225. oldShardBits := erasure_coding.ShardBits(shardInfo.EcIndexBits)
  226. newShardBits := oldShardBits
  227. for _, shardId := range shardIds {
  228. newShardBits = newShardBits.AddShardId(erasure_coding.ShardId(shardId))
  229. }
  230. shardInfo.EcIndexBits = uint32(newShardBits)
  231. ecNode.freeEcSlot -= newShardBits.ShardIdCount() - oldShardBits.ShardIdCount()
  232. foundVolume = true
  233. break
  234. }
  235. }
  236. if !foundVolume {
  237. var newShardBits erasure_coding.ShardBits
  238. for _, shardId := range shardIds {
  239. newShardBits = newShardBits.AddShardId(erasure_coding.ShardId(shardId))
  240. }
  241. ecNode.info.EcShardInfos = append(ecNode.info.EcShardInfos, &master_pb.VolumeEcShardInformationMessage{
  242. Id: uint32(vid),
  243. Collection: collection,
  244. EcIndexBits: uint32(newShardBits),
  245. })
  246. ecNode.freeEcSlot -= len(shardIds)
  247. }
  248. return ecNode
  249. }
  250. func (ecNode *EcNode) deleteEcVolumeShards(vid needle.VolumeId, shardIds []uint32) *EcNode {
  251. for _, shardInfo := range ecNode.info.EcShardInfos {
  252. if needle.VolumeId(shardInfo.Id) == vid {
  253. oldShardBits := erasure_coding.ShardBits(shardInfo.EcIndexBits)
  254. newShardBits := oldShardBits
  255. for _, shardId := range shardIds {
  256. newShardBits = newShardBits.RemoveShardId(erasure_coding.ShardId(shardId))
  257. }
  258. shardInfo.EcIndexBits = uint32(newShardBits)
  259. ecNode.freeEcSlot -= newShardBits.ShardIdCount() - oldShardBits.ShardIdCount()
  260. }
  261. }
  262. return ecNode
  263. }
  264. func groupByCount(data []*EcNode, identifierFn func(*EcNode) (id string, count int)) map[string]int {
  265. countMap := make(map[string]int)
  266. for _, d := range data {
  267. id, count := identifierFn(d)
  268. countMap[id] += count
  269. }
  270. return countMap
  271. }
  272. func groupBy(data []*EcNode, identifierFn func(*EcNode) (id string)) map[string][]*EcNode {
  273. groupMap := make(map[string][]*EcNode)
  274. for _, d := range data {
  275. id := identifierFn(d)
  276. groupMap[id] = append(groupMap[id], d)
  277. }
  278. return groupMap
  279. }