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.

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