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.

246 lines
7.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package storage
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "time"
  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. )
  14. func (s *Store) CollectErasureCodingHeartbeat() *master_pb.Heartbeat {
  15. var ecShardMessages []*master_pb.VolumeEcShardInformationMessage
  16. for _, location := range s.Locations {
  17. location.ecVolumesLock.RLock()
  18. for _, ecShards := range location.ecVolumes {
  19. ecShardMessages = append(ecShardMessages, ecShards.ToVolumeEcShardInformationMessage()...)
  20. }
  21. location.ecVolumesLock.RUnlock()
  22. }
  23. return &master_pb.Heartbeat{
  24. EcShards: ecShardMessages,
  25. }
  26. }
  27. func (s *Store) MountEcShards(collection string, vid needle.VolumeId, shardId erasure_coding.ShardId) error {
  28. for _, location := range s.Locations {
  29. if err := location.LoadEcShard(collection, vid, shardId); err == nil {
  30. glog.V(0).Infof("MountEcShards %d.%d", vid, shardId)
  31. var shardBits erasure_coding.ShardBits
  32. s.NewEcShardsChan <- master_pb.VolumeEcShardInformationMessage{
  33. Id: uint32(vid),
  34. Collection: collection,
  35. EcIndexBits: uint32(shardBits.AddShardId(shardId)),
  36. }
  37. return nil
  38. }
  39. }
  40. return fmt.Errorf("MountEcShards %d.%d not found on disk", vid, shardId)
  41. }
  42. func (s *Store) UnmountEcShards(vid needle.VolumeId, shardId erasure_coding.ShardId) error {
  43. ecShard, found := s.findEcShard(vid, shardId)
  44. if !found {
  45. return nil
  46. }
  47. var shardBits erasure_coding.ShardBits
  48. message := master_pb.VolumeEcShardInformationMessage{
  49. Id: uint32(vid),
  50. Collection: ecShard.Collection,
  51. EcIndexBits: uint32(shardBits.AddShardId(shardId)),
  52. }
  53. for _, location := range s.Locations {
  54. if deleted := location.UnloadEcShard(vid, shardId); deleted {
  55. glog.V(0).Infof("UnmountEcShards %d.%d", vid, shardId)
  56. s.DeletedEcShardsChan <- message
  57. return nil
  58. }
  59. }
  60. return fmt.Errorf("UnmountEcShards %d.%d not found on disk", vid, shardId)
  61. }
  62. func (s *Store) findEcShard(vid needle.VolumeId, shardId erasure_coding.ShardId) (*erasure_coding.EcVolumeShard, bool) {
  63. for _, location := range s.Locations {
  64. if v, found := location.FindEcShard(vid, shardId); found {
  65. return v, found
  66. }
  67. }
  68. return nil, false
  69. }
  70. func (s *Store) FindEcVolume(vid needle.VolumeId) (*erasure_coding.EcVolume, bool) {
  71. for _, location := range s.Locations {
  72. if s, found := location.FindEcVolume(vid); found {
  73. return s, true
  74. }
  75. }
  76. return nil, false
  77. }
  78. func (s *Store) ReadEcShardNeedle(ctx context.Context, vid needle.VolumeId, n *needle.Needle) (int, error) {
  79. for _, location := range s.Locations {
  80. if localEcVolume, found := location.FindEcVolume(vid); found {
  81. // TODO need to read the version
  82. version := needle.CurrentVersion
  83. offset, size, intervals, err := localEcVolume.LocateEcShardNeedle(n, version)
  84. if err != nil {
  85. return 0, err
  86. }
  87. glog.V(4).Infof("read ec volume %d offset %d size %d intervals:%+v", vid, offset.ToAcutalOffset(), size, intervals)
  88. if len(intervals) > 1 {
  89. glog.V(4).Infof("ReadEcShardNeedle needle id %s intervals:%+v", n.String(), intervals)
  90. }
  91. bytes, err := s.readEcShardIntervals(ctx, vid, localEcVolume, intervals)
  92. if err != nil {
  93. return 0, fmt.Errorf("ReadEcShardIntervals: %v", err)
  94. }
  95. err = n.ReadBytes(bytes, offset.ToAcutalOffset(), size, version)
  96. if err != nil {
  97. return 0, fmt.Errorf("readbytes: %v", err)
  98. }
  99. return len(bytes), nil
  100. }
  101. }
  102. return 0, fmt.Errorf("ec shard %d not found", vid)
  103. }
  104. func (s *Store) readEcShardIntervals(ctx context.Context, vid needle.VolumeId, ecVolume *erasure_coding.EcVolume, intervals []erasure_coding.Interval) (data []byte, err error) {
  105. if err = s.cachedLookupEcShardLocations(ctx, ecVolume); err != nil {
  106. return nil, fmt.Errorf("failed to locate shard via master grpc %s: %v", s.MasterAddress, err)
  107. }
  108. for i, interval := range intervals {
  109. if d, e := s.readOneEcShardInterval(ctx, ecVolume, interval); e != nil {
  110. return nil, e
  111. } else {
  112. if i == 0 {
  113. data = d
  114. } else {
  115. data = append(data, d...)
  116. }
  117. }
  118. }
  119. return
  120. }
  121. func (s *Store) readOneEcShardInterval(ctx context.Context, ecVolume *erasure_coding.EcVolume, interval erasure_coding.Interval) (data []byte, err error) {
  122. shardId, actualOffset := interval.ToShardIdAndOffset(erasure_coding.ErasureCodingLargeBlockSize, erasure_coding.ErasureCodingSmallBlockSize)
  123. data = make([]byte, interval.Size)
  124. if shard, found := ecVolume.FindEcVolumeShard(shardId); found {
  125. if _, err = shard.ReadAt(data, actualOffset); err != nil {
  126. glog.V(0).Infof("read local ec shard %d.%d: %v", ecVolume.VolumeId, shardId, err)
  127. return
  128. }
  129. } else {
  130. ecVolume.ShardLocationsLock.RLock()
  131. sourceDataNodes, found := ecVolume.ShardLocations[shardId]
  132. ecVolume.ShardLocationsLock.RUnlock()
  133. if !found || len(sourceDataNodes) == 0 {
  134. return nil, fmt.Errorf("failed to find ec shard %d.%d", ecVolume.VolumeId, shardId)
  135. }
  136. glog.V(4).Infof("read remote ec shard %d.%d from %s", ecVolume.VolumeId, shardId, sourceDataNodes[0])
  137. _, err = s.readOneRemoteEcShardInterval(ctx, sourceDataNodes[0], ecVolume.VolumeId, shardId, data, actualOffset)
  138. if err != nil {
  139. glog.V(1).Infof("failed to read from %s for ec shard %d.%d : %v", sourceDataNodes[0], ecVolume.VolumeId, shardId, err)
  140. }
  141. }
  142. return
  143. }
  144. func (s *Store) cachedLookupEcShardLocations(ctx context.Context, ecVolume *erasure_coding.EcVolume) (err error) {
  145. if ecVolume.ShardLocationsRefreshTime.Add(10 * time.Minute).After(time.Now()) {
  146. // still fresh
  147. return nil
  148. }
  149. glog.V(3).Infof("lookup and cache ec volume %d locations", ecVolume.VolumeId)
  150. err = operation.WithMasterServerClient(s.MasterAddress, s.grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  151. req := &master_pb.LookupEcVolumeRequest{
  152. VolumeId: uint32(ecVolume.VolumeId),
  153. }
  154. resp, err := masterClient.LookupEcVolume(ctx, req)
  155. if err != nil {
  156. return fmt.Errorf("lookup ec volume %d: %v", ecVolume.VolumeId, err)
  157. }
  158. ecVolume.ShardLocationsLock.Lock()
  159. for _, shardIdLocations := range resp.ShardIdLocations {
  160. shardId := erasure_coding.ShardId(shardIdLocations.ShardId)
  161. delete(ecVolume.ShardLocations, shardId)
  162. for _, loc := range shardIdLocations.Locations {
  163. ecVolume.ShardLocations[shardId] = append(ecVolume.ShardLocations[shardId], loc.Url)
  164. }
  165. }
  166. ecVolume.ShardLocationsRefreshTime = time.Now()
  167. ecVolume.ShardLocationsLock.Unlock()
  168. return nil
  169. })
  170. return
  171. }
  172. func (s *Store) readOneRemoteEcShardInterval(ctx context.Context, sourceDataNode string, vid needle.VolumeId, shardId erasure_coding.ShardId, buf []byte, offset int64) (n int, err error) {
  173. err = operation.WithVolumeServerClient(sourceDataNode, s.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  174. // copy data slice
  175. shardReadClient, err := client.VolumeEcShardRead(ctx, &volume_server_pb.VolumeEcShardReadRequest{
  176. VolumeId: uint32(vid),
  177. ShardId: uint32(shardId),
  178. Offset: offset,
  179. Size: int64(len(buf)),
  180. })
  181. if err != nil {
  182. return fmt.Errorf("failed to start reading ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  183. }
  184. for {
  185. resp, receiveErr := shardReadClient.Recv()
  186. if receiveErr == io.EOF {
  187. break
  188. }
  189. if receiveErr != nil {
  190. return fmt.Errorf("receiving ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  191. }
  192. copy(buf[n:n+len(resp.Data)], resp.Data)
  193. n += len(resp.Data)
  194. }
  195. return nil
  196. })
  197. if err != nil {
  198. return 0, fmt.Errorf("read ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  199. }
  200. return
  201. }
  202. func (s *Store) recoverOneRemoteEcShardInterval(ctx context.Context, string, vid needle.VolumeId, shardIdToRecover erasure_coding.ShardId, buf []byte, offset int64) (n int, err error) {
  203. glog.V(1).Infof("recover ec shard %d.%d from other locations", vid, shardIdToRecover)
  204. // TODO add recovering
  205. return 0, fmt.Errorf("recover is not implemented yet")
  206. }