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.

353 lines
11 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. "sync"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/operation"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  12. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/klauspost/reedsolomon"
  15. )
  16. func (s *Store) CollectErasureCodingHeartbeat() *master_pb.Heartbeat {
  17. var ecShardMessages []*master_pb.VolumeEcShardInformationMessage
  18. for _, location := range s.Locations {
  19. location.ecVolumesLock.RLock()
  20. for _, ecShards := range location.ecVolumes {
  21. ecShardMessages = append(ecShardMessages, ecShards.ToVolumeEcShardInformationMessage()...)
  22. }
  23. location.ecVolumesLock.RUnlock()
  24. }
  25. return &master_pb.Heartbeat{
  26. EcShards: ecShardMessages,
  27. }
  28. }
  29. func (s *Store) MountEcShards(collection string, vid needle.VolumeId, shardId erasure_coding.ShardId) error {
  30. for _, location := range s.Locations {
  31. if err := location.LoadEcShard(collection, vid, shardId); err == nil {
  32. glog.V(0).Infof("MountEcShards %d.%d", vid, shardId)
  33. var shardBits erasure_coding.ShardBits
  34. s.NewEcShardsChan <- master_pb.VolumeEcShardInformationMessage{
  35. Id: uint32(vid),
  36. Collection: collection,
  37. EcIndexBits: uint32(shardBits.AddShardId(shardId)),
  38. }
  39. return nil
  40. }
  41. }
  42. return fmt.Errorf("MountEcShards %d.%d not found on disk", vid, shardId)
  43. }
  44. func (s *Store) UnmountEcShards(vid needle.VolumeId, shardId erasure_coding.ShardId) error {
  45. ecShard, found := s.findEcShard(vid, shardId)
  46. if !found {
  47. return nil
  48. }
  49. var shardBits erasure_coding.ShardBits
  50. message := master_pb.VolumeEcShardInformationMessage{
  51. Id: uint32(vid),
  52. Collection: ecShard.Collection,
  53. EcIndexBits: uint32(shardBits.AddShardId(shardId)),
  54. }
  55. for _, location := range s.Locations {
  56. if deleted := location.UnloadEcShard(vid, shardId); deleted {
  57. glog.V(0).Infof("UnmountEcShards %d.%d", vid, shardId)
  58. s.DeletedEcShardsChan <- message
  59. return nil
  60. }
  61. }
  62. return fmt.Errorf("UnmountEcShards %d.%d not found on disk", vid, shardId)
  63. }
  64. func (s *Store) findEcShard(vid needle.VolumeId, shardId erasure_coding.ShardId) (*erasure_coding.EcVolumeShard, bool) {
  65. for _, location := range s.Locations {
  66. if v, found := location.FindEcShard(vid, shardId); found {
  67. return v, found
  68. }
  69. }
  70. return nil, false
  71. }
  72. func (s *Store) FindEcVolume(vid needle.VolumeId) (*erasure_coding.EcVolume, bool) {
  73. for _, location := range s.Locations {
  74. if s, found := location.FindEcVolume(vid); found {
  75. return s, true
  76. }
  77. }
  78. return nil, false
  79. }
  80. func (s *Store) ReadEcShardNeedle(ctx context.Context, vid needle.VolumeId, n *needle.Needle) (int, error) {
  81. for _, location := range s.Locations {
  82. if localEcVolume, found := location.FindEcVolume(vid); found {
  83. // read the volume version
  84. for localEcVolume.Version == 0 {
  85. err := s.readEcVolumeVersion(ctx, vid, localEcVolume)
  86. time.Sleep(1357 * time.Millisecond)
  87. glog.V(0).Infof("ReadEcShardNeedle vid %d version:%v: %v", vid, localEcVolume.Version, err)
  88. }
  89. version := localEcVolume.Version
  90. offset, size, intervals, err := localEcVolume.LocateEcShardNeedle(n, version)
  91. if err != nil {
  92. return 0, err
  93. }
  94. glog.V(4).Infof("read ec volume %d offset %d size %d intervals:%+v", vid, offset.ToAcutalOffset(), size, intervals)
  95. if len(intervals) > 1 {
  96. glog.V(4).Infof("ReadEcShardNeedle needle id %s intervals:%+v", n.String(), intervals)
  97. }
  98. bytes, err := s.readEcShardIntervals(ctx, vid, localEcVolume, intervals)
  99. if err != nil {
  100. return 0, fmt.Errorf("ReadEcShardIntervals: %v", err)
  101. }
  102. err = n.ReadBytes(bytes, offset.ToAcutalOffset(), size, version)
  103. if err != nil {
  104. return 0, fmt.Errorf("readbytes: %v", err)
  105. }
  106. return len(bytes), nil
  107. }
  108. }
  109. return 0, fmt.Errorf("ec shard %d not found", vid)
  110. }
  111. func (s *Store) readEcVolumeVersion(ctx context.Context, vid needle.VolumeId, ecVolume *erasure_coding.EcVolume) (err error) {
  112. interval := erasure_coding.Interval{
  113. BlockIndex: 0,
  114. InnerBlockOffset: 0,
  115. Size: _SuperBlockSize,
  116. IsLargeBlock: true, // it could be large block, but ok in this place
  117. LargeBlockRowsCount: 0,
  118. }
  119. data, err := s.readEcShardIntervals(ctx, vid, ecVolume, []erasure_coding.Interval{interval})
  120. if err == nil {
  121. ecVolume.Version = needle.Version(data[0])
  122. }
  123. return
  124. }
  125. func (s *Store) readEcShardIntervals(ctx context.Context, vid needle.VolumeId, ecVolume *erasure_coding.EcVolume, intervals []erasure_coding.Interval) (data []byte, err error) {
  126. if err = s.cachedLookupEcShardLocations(ctx, ecVolume); err != nil {
  127. return nil, fmt.Errorf("failed to locate shard via master grpc %s: %v", s.MasterAddress, err)
  128. }
  129. for i, interval := range intervals {
  130. if d, e := s.readOneEcShardInterval(ctx, ecVolume, interval); e != nil {
  131. return nil, e
  132. } else {
  133. if i == 0 {
  134. data = d
  135. } else {
  136. data = append(data, d...)
  137. }
  138. }
  139. }
  140. return
  141. }
  142. func (s *Store) readOneEcShardInterval(ctx context.Context, ecVolume *erasure_coding.EcVolume, interval erasure_coding.Interval) (data []byte, err error) {
  143. shardId, actualOffset := interval.ToShardIdAndOffset(erasure_coding.ErasureCodingLargeBlockSize, erasure_coding.ErasureCodingSmallBlockSize)
  144. data = make([]byte, interval.Size)
  145. if shard, found := ecVolume.FindEcVolumeShard(shardId); found {
  146. if _, err = shard.ReadAt(data, actualOffset); err != nil {
  147. glog.V(0).Infof("read local ec shard %d.%d: %v", ecVolume.VolumeId, shardId, err)
  148. return
  149. }
  150. } else {
  151. ecVolume.ShardLocationsLock.RLock()
  152. sourceDataNodes, hasShardIdLocation := ecVolume.ShardLocations[shardId]
  153. ecVolume.ShardLocationsLock.RUnlock()
  154. // try reading directly
  155. if hasShardIdLocation {
  156. _, err = s.readRemoteEcShardInterval(ctx, sourceDataNodes, ecVolume.VolumeId, shardId, data, actualOffset)
  157. if err == nil {
  158. return
  159. }
  160. glog.V(0).Infof("clearing ec shard %d.%d locations: %v", ecVolume.VolumeId, shardId, err)
  161. forgetShardId(ecVolume, shardId)
  162. }
  163. // try reading by recovering from other shards
  164. _, err = s.recoverOneRemoteEcShardInterval(ctx, ecVolume, shardId, data, actualOffset)
  165. if err == nil {
  166. return
  167. }
  168. glog.V(0).Infof("recover ec shard %d.%d : %v", ecVolume.VolumeId, shardId, err)
  169. }
  170. return
  171. }
  172. func forgetShardId(ecVolume *erasure_coding.EcVolume, shardId erasure_coding.ShardId) {
  173. // failed to access the source data nodes, clear it up
  174. ecVolume.ShardLocationsLock.Lock()
  175. delete(ecVolume.ShardLocations, shardId)
  176. ecVolume.ShardLocationsLock.Unlock()
  177. }
  178. func (s *Store) cachedLookupEcShardLocations(ctx context.Context, ecVolume *erasure_coding.EcVolume) (err error) {
  179. if ecVolume.ShardLocationsRefreshTime.Add(10 * time.Minute).After(time.Now()) {
  180. // still fresh
  181. return nil
  182. }
  183. glog.V(3).Infof("lookup and cache ec volume %d locations", ecVolume.VolumeId)
  184. err = operation.WithMasterServerClient(s.MasterAddress, s.grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  185. req := &master_pb.LookupEcVolumeRequest{
  186. VolumeId: uint32(ecVolume.VolumeId),
  187. }
  188. resp, err := masterClient.LookupEcVolume(ctx, req)
  189. if err != nil {
  190. return fmt.Errorf("lookup ec volume %d: %v", ecVolume.VolumeId, err)
  191. }
  192. if len(resp.ShardIdLocations) < erasure_coding.DataShardsCount {
  193. return fmt.Errorf("only %d shards found but %d required", len(resp.ShardIdLocations), erasure_coding.DataShardsCount)
  194. }
  195. ecVolume.ShardLocationsLock.Lock()
  196. for _, shardIdLocations := range resp.ShardIdLocations {
  197. shardId := erasure_coding.ShardId(shardIdLocations.ShardId)
  198. delete(ecVolume.ShardLocations, shardId)
  199. for _, loc := range shardIdLocations.Locations {
  200. ecVolume.ShardLocations[shardId] = append(ecVolume.ShardLocations[shardId], loc.Url)
  201. }
  202. }
  203. ecVolume.ShardLocationsRefreshTime = time.Now()
  204. ecVolume.ShardLocationsLock.Unlock()
  205. return nil
  206. })
  207. return
  208. }
  209. func (s *Store) readRemoteEcShardInterval(ctx context.Context, sourceDataNodes []string, vid needle.VolumeId, shardId erasure_coding.ShardId, buf []byte, offset int64) (n int, err error) {
  210. if len(sourceDataNodes) == 0 {
  211. return 0, fmt.Errorf("failed to find ec shard %d.%d", vid, shardId)
  212. }
  213. for _, sourceDataNode := range sourceDataNodes {
  214. glog.V(4).Infof("read remote ec shard %d.%d from %s", vid, shardId, sourceDataNode)
  215. n, err = s.doReadRemoteEcShardInterval(ctx, sourceDataNode, vid, shardId, buf, offset)
  216. if err == nil {
  217. return
  218. }
  219. glog.V(1).Infof("read remote ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  220. }
  221. return
  222. }
  223. func (s *Store) doReadRemoteEcShardInterval(ctx context.Context, sourceDataNode string, vid needle.VolumeId, shardId erasure_coding.ShardId, buf []byte, offset int64) (n int, err error) {
  224. err = operation.WithVolumeServerClient(sourceDataNode, s.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  225. // copy data slice
  226. shardReadClient, err := client.VolumeEcShardRead(ctx, &volume_server_pb.VolumeEcShardReadRequest{
  227. VolumeId: uint32(vid),
  228. ShardId: uint32(shardId),
  229. Offset: offset,
  230. Size: int64(len(buf)),
  231. })
  232. if err != nil {
  233. return fmt.Errorf("failed to start reading ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  234. }
  235. for {
  236. resp, receiveErr := shardReadClient.Recv()
  237. if receiveErr == io.EOF {
  238. break
  239. }
  240. if receiveErr != nil {
  241. return fmt.Errorf("receiving ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  242. }
  243. copy(buf[n:n+len(resp.Data)], resp.Data)
  244. n += len(resp.Data)
  245. }
  246. return nil
  247. })
  248. if err != nil {
  249. return 0, fmt.Errorf("read ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  250. }
  251. return
  252. }
  253. func (s *Store) recoverOneRemoteEcShardInterval(ctx context.Context, ecVolume *erasure_coding.EcVolume, shardIdToRecover erasure_coding.ShardId, buf []byte, offset int64) (n int, err error) {
  254. glog.V(4).Infof("recover ec shard %d.%d from other locations", ecVolume.VolumeId, shardIdToRecover)
  255. enc, err := reedsolomon.New(erasure_coding.DataShardsCount, erasure_coding.ParityShardsCount)
  256. if err != nil {
  257. return 0, fmt.Errorf("failed to create encoder: %v", err)
  258. }
  259. bufs := make([][]byte, erasure_coding.TotalShardsCount)
  260. var wg sync.WaitGroup
  261. ecVolume.ShardLocationsLock.RLock()
  262. for shardId, locations := range ecVolume.ShardLocations {
  263. // skip currnent shard or empty shard
  264. if shardId == shardIdToRecover {
  265. continue
  266. }
  267. if len(locations) == 0 {
  268. glog.V(3).Infof("readRemoteEcShardInterval missing %d.%d from %+v", ecVolume.VolumeId, shardId, locations)
  269. continue
  270. }
  271. // read from remote locations
  272. wg.Add(1)
  273. go func(shardId erasure_coding.ShardId, locations []string) {
  274. defer wg.Done()
  275. data := make([]byte, len(buf))
  276. nRead, readErr := s.readRemoteEcShardInterval(ctx, locations, ecVolume.VolumeId, shardId, data, offset)
  277. if readErr != nil {
  278. glog.V(3).Infof("recover: readRemoteEcShardInterval %d.%d %d bytes from %+v: %v", ecVolume.VolumeId, shardId, nRead, locations, readErr)
  279. forgetShardId(ecVolume, shardId)
  280. }
  281. if nRead == len(buf) {
  282. bufs[shardId] = data
  283. }
  284. }(shardId, locations)
  285. }
  286. ecVolume.ShardLocationsLock.RUnlock()
  287. wg.Wait()
  288. if err = enc.ReconstructData(bufs); err != nil {
  289. glog.V(3).Infof("recovered ec shard %d.%d failed: %v", ecVolume.VolumeId, shardIdToRecover, err)
  290. return 0, err
  291. }
  292. glog.V(4).Infof("recovered ec shard %d.%d from other locations", ecVolume.VolumeId, shardIdToRecover)
  293. copy(buf, bufs[shardIdToRecover])
  294. return len(buf), nil
  295. }