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.

397 lines
13 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. "sort"
  7. "sync"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  12. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  13. "github.com/chrislusf/seaweedfs/weed/stats"
  14. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  15. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  16. "github.com/chrislusf/seaweedfs/weed/storage/types"
  17. "github.com/klauspost/reedsolomon"
  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(ctx context.Context, vid needle.VolumeId, n *needle.Needle) (int, error) {
  99. for _, location := range s.Locations {
  100. if localEcVolume, found := location.FindEcVolume(vid); found {
  101. // read the volume version
  102. for localEcVolume.Version == 0 {
  103. err := s.readEcVolumeVersion(ctx, vid, localEcVolume)
  104. time.Sleep(1357 * time.Millisecond)
  105. glog.V(0).Infof("ReadEcShardNeedle vid %d version:%v: %v", vid, localEcVolume.Version, err)
  106. }
  107. version := localEcVolume.Version
  108. offset, size, intervals, err := localEcVolume.LocateEcShardNeedle(n.Id, version)
  109. if err != nil {
  110. return 0, fmt.Errorf("locate in local ec volume: %v", err)
  111. }
  112. if size == types.TombstoneFileSize {
  113. return 0, fmt.Errorf("entry %s is deleted", n.Id)
  114. }
  115. glog.V(4).Infof("read ec volume %d offset %d size %d intervals:%+v", vid, offset.ToAcutalOffset(), size, intervals)
  116. if len(intervals) > 1 {
  117. glog.V(4).Infof("ReadEcShardNeedle needle id %s intervals:%+v", n.String(), intervals)
  118. }
  119. bytes, err := s.readEcShardIntervals(ctx, vid, localEcVolume, intervals)
  120. if err != nil {
  121. return 0, fmt.Errorf("ReadEcShardIntervals: %v", err)
  122. }
  123. err = n.ReadBytes(bytes, offset.ToAcutalOffset(), size, version)
  124. if err != nil {
  125. return 0, fmt.Errorf("readbytes: %v", err)
  126. }
  127. return len(bytes), nil
  128. }
  129. }
  130. return 0, fmt.Errorf("ec shard %d not found", vid)
  131. }
  132. func (s *Store) readEcVolumeVersion(ctx context.Context, vid needle.VolumeId, ecVolume *erasure_coding.EcVolume) (err error) {
  133. interval := erasure_coding.Interval{
  134. BlockIndex: 0,
  135. InnerBlockOffset: 0,
  136. Size: _SuperBlockSize,
  137. IsLargeBlock: true, // it could be large block, but ok in this place
  138. LargeBlockRowsCount: 0,
  139. }
  140. data, err := s.readEcShardIntervals(ctx, vid, ecVolume, []erasure_coding.Interval{interval})
  141. if err == nil {
  142. ecVolume.Version = needle.Version(data[0])
  143. }
  144. return
  145. }
  146. func (s *Store) readEcShardIntervals(ctx context.Context, vid needle.VolumeId, ecVolume *erasure_coding.EcVolume, intervals []erasure_coding.Interval) (data []byte, err error) {
  147. if err = s.cachedLookupEcShardLocations(ctx, ecVolume); err != nil {
  148. return nil, fmt.Errorf("failed to locate shard via master grpc %s: %v", s.MasterAddress, err)
  149. }
  150. for i, interval := range intervals {
  151. if d, e := s.readOneEcShardInterval(ctx, ecVolume, interval); e != nil {
  152. return nil, e
  153. } else {
  154. if i == 0 {
  155. data = d
  156. } else {
  157. data = append(data, d...)
  158. }
  159. }
  160. }
  161. return
  162. }
  163. func (s *Store) readOneEcShardInterval(ctx context.Context, ecVolume *erasure_coding.EcVolume, interval erasure_coding.Interval) (data []byte, err error) {
  164. shardId, actualOffset := interval.ToShardIdAndOffset(erasure_coding.ErasureCodingLargeBlockSize, erasure_coding.ErasureCodingSmallBlockSize)
  165. data = make([]byte, interval.Size)
  166. if shard, found := ecVolume.FindEcVolumeShard(shardId); found {
  167. if _, err = shard.ReadAt(data, actualOffset); err != nil {
  168. glog.V(0).Infof("read local ec shard %d.%d: %v", ecVolume.VolumeId, shardId, err)
  169. return
  170. }
  171. } else {
  172. ecVolume.ShardLocationsLock.RLock()
  173. sourceDataNodes, hasShardIdLocation := ecVolume.ShardLocations[shardId]
  174. ecVolume.ShardLocationsLock.RUnlock()
  175. // try reading directly
  176. if hasShardIdLocation {
  177. _, err = s.readRemoteEcShardInterval(ctx, sourceDataNodes, ecVolume.VolumeId, shardId, data, actualOffset)
  178. if err == nil {
  179. return
  180. }
  181. glog.V(0).Infof("clearing ec shard %d.%d locations: %v", ecVolume.VolumeId, shardId, err)
  182. forgetShardId(ecVolume, shardId)
  183. }
  184. // try reading by recovering from other shards
  185. _, err = s.recoverOneRemoteEcShardInterval(ctx, ecVolume, shardId, data, actualOffset)
  186. if err == nil {
  187. return
  188. }
  189. glog.V(0).Infof("recover ec shard %d.%d : %v", ecVolume.VolumeId, shardId, err)
  190. }
  191. return
  192. }
  193. func forgetShardId(ecVolume *erasure_coding.EcVolume, shardId erasure_coding.ShardId) {
  194. // failed to access the source data nodes, clear it up
  195. ecVolume.ShardLocationsLock.Lock()
  196. delete(ecVolume.ShardLocations, shardId)
  197. ecVolume.ShardLocationsLock.Unlock()
  198. }
  199. func (s *Store) cachedLookupEcShardLocations(ctx context.Context, ecVolume *erasure_coding.EcVolume) (err error) {
  200. shardCount := len(ecVolume.ShardLocations)
  201. if shardCount < erasure_coding.DataShardsCount &&
  202. ecVolume.ShardLocationsRefreshTime.Add(11*time.Second).After(time.Now()) ||
  203. shardCount == erasure_coding.TotalShardsCount &&
  204. ecVolume.ShardLocationsRefreshTime.Add(37*time.Minute).After(time.Now()) ||
  205. shardCount >= erasure_coding.DataShardsCount &&
  206. ecVolume.ShardLocationsRefreshTime.Add(7*time.Minute).After(time.Now()) {
  207. // still fresh
  208. return nil
  209. }
  210. glog.V(3).Infof("lookup and cache ec volume %d locations", ecVolume.VolumeId)
  211. err = operation.WithMasterServerClient(s.MasterAddress, s.grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  212. req := &master_pb.LookupEcVolumeRequest{
  213. VolumeId: uint32(ecVolume.VolumeId),
  214. }
  215. resp, err := masterClient.LookupEcVolume(ctx, req)
  216. if err != nil {
  217. return fmt.Errorf("lookup ec volume %d: %v", ecVolume.VolumeId, err)
  218. }
  219. if len(resp.ShardIdLocations) < erasure_coding.DataShardsCount {
  220. return fmt.Errorf("only %d shards found but %d required", len(resp.ShardIdLocations), erasure_coding.DataShardsCount)
  221. }
  222. ecVolume.ShardLocationsLock.Lock()
  223. for _, shardIdLocations := range resp.ShardIdLocations {
  224. shardId := erasure_coding.ShardId(shardIdLocations.ShardId)
  225. delete(ecVolume.ShardLocations, shardId)
  226. for _, loc := range shardIdLocations.Locations {
  227. ecVolume.ShardLocations[shardId] = append(ecVolume.ShardLocations[shardId], loc.Url)
  228. }
  229. }
  230. ecVolume.ShardLocationsRefreshTime = time.Now()
  231. ecVolume.ShardLocationsLock.Unlock()
  232. return nil
  233. })
  234. return
  235. }
  236. func (s *Store) readRemoteEcShardInterval(ctx context.Context, sourceDataNodes []string, vid needle.VolumeId, shardId erasure_coding.ShardId, buf []byte, offset int64) (n int, err error) {
  237. if len(sourceDataNodes) == 0 {
  238. return 0, fmt.Errorf("failed to find ec shard %d.%d", vid, shardId)
  239. }
  240. for _, sourceDataNode := range sourceDataNodes {
  241. glog.V(4).Infof("read remote ec shard %d.%d from %s", vid, shardId, sourceDataNode)
  242. n, err = s.doReadRemoteEcShardInterval(ctx, sourceDataNode, vid, shardId, buf, offset)
  243. if err == nil {
  244. return
  245. }
  246. glog.V(1).Infof("read remote ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  247. }
  248. return
  249. }
  250. func (s *Store) doReadRemoteEcShardInterval(ctx context.Context, sourceDataNode string, vid needle.VolumeId, shardId erasure_coding.ShardId, buf []byte, offset int64) (n int, err error) {
  251. err = operation.WithVolumeServerClient(sourceDataNode, s.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  252. // copy data slice
  253. shardReadClient, err := client.VolumeEcShardRead(ctx, &volume_server_pb.VolumeEcShardReadRequest{
  254. VolumeId: uint32(vid),
  255. ShardId: uint32(shardId),
  256. Offset: offset,
  257. Size: int64(len(buf)),
  258. })
  259. if err != nil {
  260. return fmt.Errorf("failed to start reading ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  261. }
  262. for {
  263. resp, receiveErr := shardReadClient.Recv()
  264. if receiveErr == io.EOF {
  265. break
  266. }
  267. if receiveErr != nil {
  268. return fmt.Errorf("receiving ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  269. }
  270. copy(buf[n:n+len(resp.Data)], resp.Data)
  271. n += len(resp.Data)
  272. }
  273. return nil
  274. })
  275. if err != nil {
  276. return 0, fmt.Errorf("read ec shard %d.%d from %s: %v", vid, shardId, sourceDataNode, err)
  277. }
  278. return
  279. }
  280. func (s *Store) recoverOneRemoteEcShardInterval(ctx context.Context, ecVolume *erasure_coding.EcVolume, shardIdToRecover erasure_coding.ShardId, buf []byte, offset int64) (n int, err error) {
  281. glog.V(4).Infof("recover ec shard %d.%d from other locations", ecVolume.VolumeId, shardIdToRecover)
  282. enc, err := reedsolomon.New(erasure_coding.DataShardsCount, erasure_coding.ParityShardsCount)
  283. if err != nil {
  284. return 0, fmt.Errorf("failed to create encoder: %v", err)
  285. }
  286. bufs := make([][]byte, erasure_coding.TotalShardsCount)
  287. var wg sync.WaitGroup
  288. ecVolume.ShardLocationsLock.RLock()
  289. for shardId, locations := range ecVolume.ShardLocations {
  290. // skip currnent shard or empty shard
  291. if shardId == shardIdToRecover {
  292. continue
  293. }
  294. if len(locations) == 0 {
  295. glog.V(3).Infof("readRemoteEcShardInterval missing %d.%d from %+v", ecVolume.VolumeId, shardId, locations)
  296. continue
  297. }
  298. // read from remote locations
  299. wg.Add(1)
  300. go func(shardId erasure_coding.ShardId, locations []string) {
  301. defer wg.Done()
  302. data := make([]byte, len(buf))
  303. nRead, readErr := s.readRemoteEcShardInterval(ctx, locations, ecVolume.VolumeId, shardId, data, offset)
  304. if readErr != nil {
  305. glog.V(3).Infof("recover: readRemoteEcShardInterval %d.%d %d bytes from %+v: %v", ecVolume.VolumeId, shardId, nRead, locations, readErr)
  306. forgetShardId(ecVolume, shardId)
  307. }
  308. if nRead == len(buf) {
  309. bufs[shardId] = data
  310. }
  311. }(shardId, locations)
  312. }
  313. ecVolume.ShardLocationsLock.RUnlock()
  314. wg.Wait()
  315. if err = enc.ReconstructData(bufs); err != nil {
  316. glog.V(3).Infof("recovered ec shard %d.%d failed: %v", ecVolume.VolumeId, shardIdToRecover, err)
  317. return 0, err
  318. }
  319. glog.V(4).Infof("recovered ec shard %d.%d from other locations", ecVolume.VolumeId, shardIdToRecover)
  320. copy(buf, bufs[shardIdToRecover])
  321. return len(buf), nil
  322. }
  323. func (s *Store) EcVolumes() (ecVolumes []*erasure_coding.EcVolume) {
  324. for _, location := range s.Locations {
  325. location.ecVolumesLock.RLock()
  326. for _, v := range location.ecVolumes {
  327. ecVolumes = append(ecVolumes, v)
  328. }
  329. location.ecVolumesLock.RUnlock()
  330. }
  331. sort.Slice(ecVolumes, func(i, j int) bool {
  332. return ecVolumes[i].VolumeId > ecVolumes[j].VolumeId
  333. })
  334. return ecVolumes
  335. }