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.

396 lines
13 KiB

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