store_ec.go 13 KB

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