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.

337 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(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(commandEnv.option.GrpcDialOption, destinationEcNode, []uint32{uint32(shardId)}, vid, collection, existingLocation.info.Id)
  20. if err != nil {
  21. return err
  22. }
  23. // unmount the to be deleted shards
  24. err = unmountEcShards(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(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(grpcDialOption grpc.DialOption,
  40. targetServer *EcNode, shardIdsToCopy []uint32,
  41. volumeId needle.VolumeId, collection string, existingLocation string) (copiedShardIds []uint32, err error) {
  42. fmt.Printf("allocate %d.%v %s => %s\n", volumeId, shardIdsToCopy, existingLocation, targetServer.info.Id)
  43. err = operation.WithVolumeServerClient(targetServer.info.Id, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  44. if targetServer.info.Id != existingLocation {
  45. fmt.Printf("copy %d.%v %s => %s\n", volumeId, shardIdsToCopy, existingLocation, targetServer.info.Id)
  46. _, copyErr := volumeServerClient.VolumeEcShardsCopy(context.Background(), &volume_server_pb.VolumeEcShardsCopyRequest{
  47. VolumeId: uint32(volumeId),
  48. Collection: collection,
  49. ShardIds: shardIdsToCopy,
  50. CopyEcxFile: true,
  51. CopyEcjFile: true,
  52. CopyVifFile: true,
  53. SourceDataNode: existingLocation,
  54. })
  55. if copyErr != nil {
  56. return fmt.Errorf("copy %d.%v %s => %s : %v\n", volumeId, shardIdsToCopy, existingLocation, targetServer.info.Id, copyErr)
  57. }
  58. }
  59. fmt.Printf("mount %d.%v on %s\n", volumeId, shardIdsToCopy, targetServer.info.Id)
  60. _, mountErr := volumeServerClient.VolumeEcShardsMount(context.Background(), &volume_server_pb.VolumeEcShardsMountRequest{
  61. VolumeId: uint32(volumeId),
  62. Collection: collection,
  63. ShardIds: shardIdsToCopy,
  64. })
  65. if mountErr != nil {
  66. return fmt.Errorf("mount %d.%v on %s : %v\n", volumeId, shardIdsToCopy, targetServer.info.Id, mountErr)
  67. }
  68. if targetServer.info.Id != existingLocation {
  69. copiedShardIds = shardIdsToCopy
  70. glog.V(0).Infof("%s ec volume %d deletes shards %+v", existingLocation, volumeId, copiedShardIds)
  71. }
  72. return nil
  73. })
  74. if err != nil {
  75. return
  76. }
  77. return
  78. }
  79. func eachDataNode(topo *master_pb.TopologyInfo, fn func(dc string, rack RackId, dn *master_pb.DataNodeInfo)) {
  80. for _, dc := range topo.DataCenterInfos {
  81. for _, rack := range dc.RackInfos {
  82. for _, dn := range rack.DataNodeInfos {
  83. fn(dc.Id, RackId(rack.Id), dn)
  84. }
  85. }
  86. }
  87. }
  88. func sortEcNodesByFreeslotsDecending(ecNodes []*EcNode) {
  89. sort.Slice(ecNodes, func(i, j int) bool {
  90. return ecNodes[i].freeEcSlot > ecNodes[j].freeEcSlot
  91. })
  92. }
  93. func sortEcNodesByFreeslotsAscending(ecNodes []*EcNode) {
  94. sort.Slice(ecNodes, func(i, j int) bool {
  95. return ecNodes[i].freeEcSlot < ecNodes[j].freeEcSlot
  96. })
  97. }
  98. type CandidateEcNode struct {
  99. ecNode *EcNode
  100. shardCount int
  101. }
  102. // if the index node changed the freeEcSlot, need to keep every EcNode still sorted
  103. func ensureSortedEcNodes(data []*CandidateEcNode, index int, lessThan func(i, j int) bool) {
  104. for i := index - 1; i >= 0; i-- {
  105. if lessThan(i+1, i) {
  106. swap(data, i, i+1)
  107. } else {
  108. break
  109. }
  110. }
  111. for i := index + 1; i < len(data); i++ {
  112. if lessThan(i, i-1) {
  113. swap(data, i, i-1)
  114. } else {
  115. break
  116. }
  117. }
  118. }
  119. func swap(data []*CandidateEcNode, i, j int) {
  120. t := data[i]
  121. data[i] = data[j]
  122. data[j] = t
  123. }
  124. func countShards(ecShardInfos []*master_pb.VolumeEcShardInformationMessage) (count int) {
  125. for _, ecShardInfo := range ecShardInfos {
  126. shardBits := erasure_coding.ShardBits(ecShardInfo.EcIndexBits)
  127. count += shardBits.ShardIdCount()
  128. }
  129. return
  130. }
  131. func countFreeShardSlots(dn *master_pb.DataNodeInfo) (count int) {
  132. return int(dn.MaxVolumeCount-dn.ActiveVolumeCount)*erasure_coding.DataShardsCount - countShards(dn.EcShardInfos)
  133. }
  134. type RackId string
  135. type EcNodeId string
  136. type EcNode struct {
  137. info *master_pb.DataNodeInfo
  138. dc string
  139. rack RackId
  140. freeEcSlot int
  141. }
  142. type EcRack struct {
  143. ecNodes map[EcNodeId]*EcNode
  144. freeEcSlot int
  145. }
  146. func collectEcNodes(commandEnv *CommandEnv, selectedDataCenter string) (ecNodes []*EcNode, totalFreeEcSlots int, err error) {
  147. // list all possible locations
  148. var resp *master_pb.VolumeListResponse
  149. err = commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  150. resp, err = client.VolumeList(context.Background(), &master_pb.VolumeListRequest{})
  151. return err
  152. })
  153. if err != nil {
  154. return nil, 0, err
  155. }
  156. // find out all volume servers with one slot left.
  157. eachDataNode(resp.TopologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  158. if selectedDataCenter != "" && selectedDataCenter != dc {
  159. return
  160. }
  161. freeEcSlots := countFreeShardSlots(dn)
  162. ecNodes = append(ecNodes, &EcNode{
  163. info: dn,
  164. dc: dc,
  165. rack: rack,
  166. freeEcSlot: int(freeEcSlots),
  167. })
  168. totalFreeEcSlots += freeEcSlots
  169. })
  170. sortEcNodesByFreeslotsDecending(ecNodes)
  171. return
  172. }
  173. func sourceServerDeleteEcShards(grpcDialOption grpc.DialOption, collection string, volumeId needle.VolumeId, sourceLocation string, toBeDeletedShardIds []uint32) error {
  174. fmt.Printf("delete %d.%v from %s\n", volumeId, toBeDeletedShardIds, sourceLocation)
  175. return operation.WithVolumeServerClient(sourceLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  176. _, deleteErr := volumeServerClient.VolumeEcShardsDelete(context.Background(), &volume_server_pb.VolumeEcShardsDeleteRequest{
  177. VolumeId: uint32(volumeId),
  178. Collection: collection,
  179. ShardIds: toBeDeletedShardIds,
  180. })
  181. return deleteErr
  182. })
  183. }
  184. func unmountEcShards(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceLocation string, toBeUnmountedhardIds []uint32) error {
  185. fmt.Printf("unmount %d.%v from %s\n", volumeId, toBeUnmountedhardIds, sourceLocation)
  186. return operation.WithVolumeServerClient(sourceLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  187. _, deleteErr := volumeServerClient.VolumeEcShardsUnmount(context.Background(), &volume_server_pb.VolumeEcShardsUnmountRequest{
  188. VolumeId: uint32(volumeId),
  189. ShardIds: toBeUnmountedhardIds,
  190. })
  191. return deleteErr
  192. })
  193. }
  194. func mountEcShards(grpcDialOption grpc.DialOption, 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(context.Background(), &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. }