You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
10 KiB

  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 sortEcNodes(ecNodes []*EcNode) {
  91. sort.Slice(ecNodes, func(i, j int) bool {
  92. return ecNodes[i].freeEcSlot > ecNodes[j].freeEcSlot
  93. })
  94. }
  95. type CandidateEcNode struct {
  96. ecNode *EcNode
  97. shardCount int
  98. }
  99. // if the index node changed the freeEcSlot, need to keep every EcNode still sorted
  100. func ensureSortedEcNodes(data []*CandidateEcNode, index int, lessThan func(i, j int) bool) {
  101. for i := index - 1; i >= 0; i-- {
  102. if lessThan(i+1, i) {
  103. swap(data, i, i+1)
  104. } else {
  105. break
  106. }
  107. }
  108. for i := index + 1; i < len(data); i++ {
  109. if lessThan(i, i-1) {
  110. swap(data, i, i-1)
  111. } else {
  112. break
  113. }
  114. }
  115. }
  116. func swap(data []*CandidateEcNode, i, j int) {
  117. t := data[i]
  118. data[i] = data[j]
  119. data[j] = t
  120. }
  121. func countShards(ecShardInfos []*master_pb.VolumeEcShardInformationMessage) (count int) {
  122. for _, ecShardInfo := range ecShardInfos {
  123. shardBits := erasure_coding.ShardBits(ecShardInfo.EcIndexBits)
  124. count += shardBits.ShardIdCount()
  125. }
  126. return
  127. }
  128. func countFreeShardSlots(dn *master_pb.DataNodeInfo) (count int) {
  129. return int(dn.MaxVolumeCount-dn.ActiveVolumeCount)*erasure_coding.DataShardsCount - countShards(dn.EcShardInfos)
  130. }
  131. type RackId string
  132. type EcNodeId string
  133. type EcNode struct {
  134. info *master_pb.DataNodeInfo
  135. dc string
  136. rack RackId
  137. freeEcSlot int
  138. }
  139. type EcRack struct {
  140. ecNodes map[EcNodeId]*EcNode
  141. freeEcSlot int
  142. }
  143. func collectEcNodes(ctx context.Context, commandEnv *CommandEnv, selectedDataCenter string) (ecNodes []*EcNode, totalFreeEcSlots int, err error) {
  144. // list all possible locations
  145. var resp *master_pb.VolumeListResponse
  146. err = commandEnv.MasterClient.WithClient(ctx, func(client master_pb.SeaweedClient) error {
  147. resp, err = client.VolumeList(ctx, &master_pb.VolumeListRequest{})
  148. return err
  149. })
  150. if err != nil {
  151. return nil, 0, err
  152. }
  153. // find out all volume servers with one slot left.
  154. eachDataNode(resp.TopologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  155. if selectedDataCenter != "" && selectedDataCenter != dc {
  156. return
  157. }
  158. freeEcSlots := countFreeShardSlots(dn)
  159. ecNodes = append(ecNodes, &EcNode{
  160. info: dn,
  161. dc: dc,
  162. rack: rack,
  163. freeEcSlot: int(freeEcSlots),
  164. })
  165. totalFreeEcSlots += freeEcSlots
  166. })
  167. sortEcNodes(ecNodes)
  168. return
  169. }
  170. func sourceServerDeleteEcShards(ctx context.Context, grpcDialOption grpc.DialOption,
  171. collection string, volumeId needle.VolumeId, sourceLocation string, toBeDeletedShardIds []uint32) error {
  172. fmt.Printf("delete %d.%v from %s\n", volumeId, toBeDeletedShardIds, sourceLocation)
  173. return operation.WithVolumeServerClient(sourceLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  174. _, deleteErr := volumeServerClient.VolumeEcShardsDelete(ctx, &volume_server_pb.VolumeEcShardsDeleteRequest{
  175. VolumeId: uint32(volumeId),
  176. Collection: collection,
  177. ShardIds: toBeDeletedShardIds,
  178. })
  179. return deleteErr
  180. })
  181. }
  182. func unmountEcShards(ctx context.Context, grpcDialOption grpc.DialOption,
  183. volumeId needle.VolumeId, sourceLocation string, toBeUnmountedhardIds []uint32) error {
  184. fmt.Printf("unmount %d.%v from %s\n", volumeId, toBeUnmountedhardIds, sourceLocation)
  185. return operation.WithVolumeServerClient(sourceLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  186. _, deleteErr := volumeServerClient.VolumeEcShardsUnmount(ctx, &volume_server_pb.VolumeEcShardsUnmountRequest{
  187. VolumeId: uint32(volumeId),
  188. ShardIds: toBeUnmountedhardIds,
  189. })
  190. return deleteErr
  191. })
  192. }
  193. func mountEcShards(ctx context.Context, grpcDialOption grpc.DialOption,
  194. collection string, volumeId needle.VolumeId, sourceLocation string, toBeMountedhardIds []uint32) error {
  195. fmt.Printf("mount %d.%v on %s\n", volumeId, toBeMountedhardIds, sourceLocation)
  196. return operation.WithVolumeServerClient(sourceLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  197. _, mountErr := volumeServerClient.VolumeEcShardsMount(ctx, &volume_server_pb.VolumeEcShardsMountRequest{
  198. VolumeId: uint32(volumeId),
  199. Collection: collection,
  200. ShardIds: toBeMountedhardIds,
  201. })
  202. return mountErr
  203. })
  204. }
  205. func ceilDivide(total, n int) int {
  206. return int(math.Ceil(float64(total) / float64(n)))
  207. }
  208. func findEcVolumeShards(ecNode *EcNode, vid needle.VolumeId) erasure_coding.ShardBits {
  209. for _, shardInfo := range ecNode.info.EcShardInfos {
  210. if needle.VolumeId(shardInfo.Id) == vid {
  211. return erasure_coding.ShardBits(shardInfo.EcIndexBits)
  212. }
  213. }
  214. return 0
  215. }
  216. func (ecNode *EcNode) addEcVolumeShards(vid needle.VolumeId, collection string, shardIds []uint32) *EcNode {
  217. foundVolume := false
  218. for _, shardInfo := range ecNode.info.EcShardInfos {
  219. if needle.VolumeId(shardInfo.Id) == vid {
  220. oldShardBits := erasure_coding.ShardBits(shardInfo.EcIndexBits)
  221. newShardBits := oldShardBits
  222. for _, shardId := range shardIds {
  223. newShardBits = newShardBits.AddShardId(erasure_coding.ShardId(shardId))
  224. }
  225. shardInfo.EcIndexBits = uint32(newShardBits)
  226. ecNode.freeEcSlot -= newShardBits.ShardIdCount() - oldShardBits.ShardIdCount()
  227. foundVolume = true
  228. break
  229. }
  230. }
  231. if !foundVolume {
  232. var newShardBits erasure_coding.ShardBits
  233. for _, shardId := range shardIds {
  234. newShardBits = newShardBits.AddShardId(erasure_coding.ShardId(shardId))
  235. }
  236. ecNode.info.EcShardInfos = append(ecNode.info.EcShardInfos, &master_pb.VolumeEcShardInformationMessage{
  237. Id: uint32(vid),
  238. Collection: collection,
  239. EcIndexBits: uint32(newShardBits),
  240. })
  241. ecNode.freeEcSlot -= len(shardIds)
  242. }
  243. return ecNode
  244. }
  245. func (ecNode *EcNode) deleteEcVolumeShards(vid needle.VolumeId, shardIds []uint32) *EcNode {
  246. for _, shardInfo := range ecNode.info.EcShardInfos {
  247. if needle.VolumeId(shardInfo.Id) == vid {
  248. oldShardBits := erasure_coding.ShardBits(shardInfo.EcIndexBits)
  249. newShardBits := oldShardBits
  250. for _, shardId := range shardIds {
  251. newShardBits = newShardBits.RemoveShardId(erasure_coding.ShardId(shardId))
  252. }
  253. shardInfo.EcIndexBits = uint32(newShardBits)
  254. ecNode.freeEcSlot -= newShardBits.ShardIdCount() - oldShardBits.ShardIdCount()
  255. }
  256. }
  257. return ecNode
  258. }
  259. func groupByCount(data []*EcNode, identifierFn func(*EcNode) (id string, count int)) map[string]int {
  260. countMap := make(map[string]int)
  261. for _, d := range data {
  262. id, count := identifierFn(d)
  263. countMap[id] += count
  264. }
  265. return countMap
  266. }
  267. func groupBy(data []*EcNode, identifierFn func(*EcNode) (id string)) map[string][]*EcNode {
  268. groupMap := make(map[string][]*EcNode)
  269. for _, d := range data {
  270. id := identifierFn(d)
  271. groupMap[id] = append(groupMap[id], d)
  272. }
  273. return groupMap
  274. }