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

5 years ago
  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.FreeVolumeCount)*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. if freeEcSlots := countFreeShardSlots(dn); freeEcSlots > 0 {
  159. ecNodes = append(ecNodes, &EcNode{
  160. info: dn,
  161. dc: dc,
  162. rack: rack,
  163. freeEcSlot: int(freeEcSlots),
  164. })
  165. totalFreeEcSlots += freeEcSlots
  166. }
  167. })
  168. sortEcNodes(ecNodes)
  169. return
  170. }
  171. func sourceServerDeleteEcShards(ctx context.Context, grpcDialOption grpc.DialOption,
  172. collection string, volumeId needle.VolumeId, sourceLocation string, toBeDeletedShardIds []uint32) error {
  173. fmt.Printf("delete %d.%v from %s\n", volumeId, toBeDeletedShardIds, sourceLocation)
  174. return operation.WithVolumeServerClient(sourceLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  175. _, deleteErr := volumeServerClient.VolumeEcShardsDelete(ctx, &volume_server_pb.VolumeEcShardsDeleteRequest{
  176. VolumeId: uint32(volumeId),
  177. Collection: collection,
  178. ShardIds: toBeDeletedShardIds,
  179. })
  180. return deleteErr
  181. })
  182. }
  183. func unmountEcShards(ctx context.Context, grpcDialOption grpc.DialOption,
  184. 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(ctx, &volume_server_pb.VolumeEcShardsUnmountRequest{
  188. VolumeId: uint32(volumeId),
  189. ShardIds: toBeUnmountedhardIds,
  190. })
  191. return deleteErr
  192. })
  193. }
  194. func mountEcShards(ctx context.Context, grpcDialOption grpc.DialOption,
  195. collection string, volumeId needle.VolumeId, sourceLocation string, toBeMountedhardIds []uint32) error {
  196. fmt.Printf("mount %d.%v on %s\n", volumeId, toBeMountedhardIds, sourceLocation)
  197. return operation.WithVolumeServerClient(sourceLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  198. _, mountErr := volumeServerClient.VolumeEcShardsMount(ctx, &volume_server_pb.VolumeEcShardsMountRequest{
  199. VolumeId: uint32(volumeId),
  200. Collection: collection,
  201. ShardIds: toBeMountedhardIds,
  202. })
  203. return mountErr
  204. })
  205. }
  206. func ceilDivide(total, n int) int {
  207. return int(math.Ceil(float64(total) / float64(n)))
  208. }
  209. func findEcVolumeShards(ecNode *EcNode, vid needle.VolumeId) erasure_coding.ShardBits {
  210. for _, shardInfo := range ecNode.info.EcShardInfos {
  211. if needle.VolumeId(shardInfo.Id) == vid {
  212. return erasure_coding.ShardBits(shardInfo.EcIndexBits)
  213. }
  214. }
  215. return 0
  216. }
  217. func (ecNode *EcNode) addEcVolumeShards(vid needle.VolumeId, collection string, shardIds []uint32) *EcNode {
  218. foundVolume := false
  219. for _, shardInfo := range ecNode.info.EcShardInfos {
  220. if needle.VolumeId(shardInfo.Id) == vid {
  221. oldShardBits := erasure_coding.ShardBits(shardInfo.EcIndexBits)
  222. newShardBits := oldShardBits
  223. for _, shardId := range shardIds {
  224. newShardBits = newShardBits.AddShardId(erasure_coding.ShardId(shardId))
  225. }
  226. shardInfo.EcIndexBits = uint32(newShardBits)
  227. ecNode.freeEcSlot -= newShardBits.ShardIdCount() - oldShardBits.ShardIdCount()
  228. foundVolume = true
  229. break
  230. }
  231. }
  232. if !foundVolume {
  233. var newShardBits erasure_coding.ShardBits
  234. for _, shardId := range shardIds {
  235. newShardBits = newShardBits.AddShardId(erasure_coding.ShardId(shardId))
  236. }
  237. ecNode.info.EcShardInfos = append(ecNode.info.EcShardInfos, &master_pb.VolumeEcShardInformationMessage{
  238. Id: uint32(vid),
  239. Collection: collection,
  240. EcIndexBits: uint32(newShardBits),
  241. })
  242. ecNode.freeEcSlot -= len(shardIds)
  243. }
  244. return ecNode
  245. }
  246. func (ecNode *EcNode) deleteEcVolumeShards(vid needle.VolumeId, shardIds []uint32) *EcNode {
  247. for _, shardInfo := range ecNode.info.EcShardInfos {
  248. if needle.VolumeId(shardInfo.Id) == vid {
  249. oldShardBits := erasure_coding.ShardBits(shardInfo.EcIndexBits)
  250. newShardBits := oldShardBits
  251. for _, shardId := range shardIds {
  252. newShardBits = newShardBits.RemoveShardId(erasure_coding.ShardId(shardId))
  253. }
  254. shardInfo.EcIndexBits = uint32(newShardBits)
  255. ecNode.freeEcSlot -= newShardBits.ShardIdCount() - oldShardBits.ShardIdCount()
  256. }
  257. }
  258. return ecNode
  259. }
  260. func groupByCount(data []*EcNode, identifierFn func(*EcNode) (id string, count int)) map[string]int {
  261. countMap := make(map[string]int)
  262. for _, d := range data {
  263. id, count := identifierFn(d)
  264. countMap[id] += count
  265. }
  266. return countMap
  267. }
  268. func groupBy(data []*EcNode, identifierFn func(*EcNode) (id string)) map[string][]*EcNode {
  269. groupMap := make(map[string][]*EcNode)
  270. for _, d := range data {
  271. id := identifierFn(d)
  272. groupMap[id] = append(groupMap[id], d)
  273. }
  274. return groupMap
  275. }