store_ec.go 13 KB

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