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.

220 lines
6.8 KiB

6 years ago
6 years ago
6 years ago
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. offset, size, intervals, err := localEcVolume.LocateEcShardNeedle(n)
  82. if err != nil {
  83. return 0, err
  84. }
  85. bytes, err := s.readEcShardIntervals(ctx, vid, localEcVolume, intervals)
  86. if err != nil {
  87. return 0, fmt.Errorf("ReadEcShardIntervals: %v", err)
  88. }
  89. version := needle.CurrentVersion
  90. err = n.ReadBytes(bytes, offset.ToAcutalOffset(), size, version)
  91. if err != nil {
  92. return 0, fmt.Errorf("readbytes: %v", err)
  93. }
  94. return len(bytes), nil
  95. }
  96. }
  97. return 0, fmt.Errorf("ec shard %d not found", vid)
  98. }
  99. func (s *Store) readEcShardIntervals(ctx context.Context, vid needle.VolumeId, ecVolume *erasure_coding.EcVolume, intervals []erasure_coding.Interval) (data []byte, err error) {
  100. if err = s.cachedLookupEcShardLocations(ctx, ecVolume); err != nil {
  101. return nil, fmt.Errorf("failed to locate shard via master grpc %s: %v", s.MasterAddress, err)
  102. }
  103. for i, interval := range intervals {
  104. if d, e := s.readOneEcShardInterval(ctx, ecVolume, interval); e != nil {
  105. return nil, e
  106. } else {
  107. if i == 0 {
  108. data = d
  109. } else {
  110. data = append(data, d...)
  111. }
  112. }
  113. }
  114. return
  115. }
  116. func (s *Store) readOneEcShardInterval(ctx context.Context, ecVolume *erasure_coding.EcVolume, interval erasure_coding.Interval) (data []byte, err error) {
  117. shardId, actualOffset := interval.ToShardIdAndOffset(erasure_coding.ErasureCodingLargeBlockSize, erasure_coding.ErasureCodingSmallBlockSize)
  118. data = make([]byte, interval.Size)
  119. if shard, found := ecVolume.FindEcVolumeShard(shardId); found {
  120. if _, err = shard.ReadAt(data, actualOffset); err != nil {
  121. return
  122. }
  123. } else {
  124. ecVolume.ShardLocationsLock.RLock()
  125. sourceDataNodes, found := ecVolume.ShardLocations[shardId]
  126. ecVolume.ShardLocationsLock.RUnlock()
  127. if !found || len(sourceDataNodes) == 0 {
  128. return nil, fmt.Errorf("failed to find ec shard %d.%d", ecVolume.VolumeId, shardId)
  129. }
  130. _, err = s.readOneRemoteEcShardInterval(ctx, sourceDataNodes[0], ecVolume.VolumeId, shardId, data, actualOffset)
  131. if err != nil {
  132. glog.V(1).Infof("failed to read from %s for ec shard %d.%d : %v", sourceDataNodes[0], ecVolume.VolumeId, shardId, err)
  133. }
  134. }
  135. return
  136. }
  137. func (s *Store) cachedLookupEcShardLocations(ctx context.Context, ecVolume *erasure_coding.EcVolume) (err error) {
  138. if ecVolume.ShardLocationsRefreshTime.Add(10 * time.Minute).After(time.Now()) {
  139. // still fresh
  140. return nil
  141. }
  142. ecVolume.ShardLocationsLock.Lock()
  143. defer ecVolume.ShardLocationsLock.Unlock()
  144. err = operation.WithMasterServerClient(s.MasterAddress, s.grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  145. return nil
  146. })
  147. return
  148. }
  149. func (s *Store) readOneRemoteEcShardInterval(ctx context.Context, sourceDataNode string, vid needle.VolumeId, shardId erasure_coding.ShardId, buf []byte, offset int64) (n int, err error) {
  150. err = operation.WithVolumeServerClient(sourceDataNode, s.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  151. // copy data slice
  152. shardReadClient, err := client.VolumeEcShardRead(ctx, &volume_server_pb.VolumeEcShardReadRequest{
  153. VolumeId: uint32(vid),
  154. ShardId: uint32(shardId),
  155. Offset: offset,
  156. Size: int64(len(buf)),
  157. })
  158. if err != nil {
  159. return fmt.Errorf("failed to start reading ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  160. }
  161. for {
  162. resp, receiveErr := shardReadClient.Recv()
  163. if receiveErr == io.EOF {
  164. break
  165. }
  166. if receiveErr != nil {
  167. return fmt.Errorf("receiving ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  168. }
  169. copy(buf[n:n+len(resp.Data)], resp.Data)
  170. n += len(resp.Data)
  171. }
  172. return nil
  173. })
  174. if err != nil {
  175. return 0, fmt.Errorf("read ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  176. }
  177. return
  178. }
  179. func (s *Store) recoverOneRemoteEcShardInterval(ctx context.Context, string, vid needle.VolumeId, shardIdToRecover erasure_coding.ShardId, buf []byte, offset int64) (n int, err error) {
  180. glog.V(1).Infof("recover ec shard %d.%d from other locations", vid, shardIdToRecover)
  181. // TODO add recovering
  182. return 0, fmt.Errorf("recover is not implemented yet")
  183. }