disk_ec.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package topology
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
  4. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  5. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  6. )
  7. func (d *Disk) GetEcShards() (ret []*erasure_coding.EcVolumeInfo) {
  8. d.RLock()
  9. for _, ecVolumeInfo := range d.ecShards {
  10. ret = append(ret, ecVolumeInfo)
  11. }
  12. d.RUnlock()
  13. return ret
  14. }
  15. func (d *Disk) AddOrUpdateEcShard(s *erasure_coding.EcVolumeInfo) {
  16. d.ecShardsLock.Lock()
  17. defer d.ecShardsLock.Unlock()
  18. delta := 0
  19. if existing, ok := d.ecShards[s.VolumeId]; !ok {
  20. d.ecShards[s.VolumeId] = s
  21. delta = s.ShardBits.ShardIdCount()
  22. } else {
  23. oldCount := existing.ShardBits.ShardIdCount()
  24. existing.ShardBits = existing.ShardBits.Plus(s.ShardBits)
  25. delta = existing.ShardBits.ShardIdCount() - oldCount
  26. }
  27. deltaDiskUsages := newDiskUsages()
  28. deltaDiskUsage := deltaDiskUsages.getOrCreateDisk(types.ToDiskType(string(d.Id())))
  29. deltaDiskUsage.ecShardCount = int64(delta)
  30. d.UpAdjustDiskUsageDelta(deltaDiskUsages)
  31. }
  32. func (d *Disk) DeleteEcShard(s *erasure_coding.EcVolumeInfo) {
  33. d.ecShardsLock.Lock()
  34. defer d.ecShardsLock.Unlock()
  35. if existing, ok := d.ecShards[s.VolumeId]; ok {
  36. oldCount := existing.ShardBits.ShardIdCount()
  37. existing.ShardBits = existing.ShardBits.Minus(s.ShardBits)
  38. delta := existing.ShardBits.ShardIdCount() - oldCount
  39. deltaDiskUsages := newDiskUsages()
  40. deltaDiskUsage := deltaDiskUsages.getOrCreateDisk(types.ToDiskType(string(d.Id())))
  41. deltaDiskUsage.ecShardCount = int64(delta)
  42. d.UpAdjustDiskUsageDelta(deltaDiskUsages)
  43. if existing.ShardBits.ShardIdCount() == 0 {
  44. delete(d.ecShards, s.VolumeId)
  45. }
  46. }
  47. }
  48. func (d *Disk) HasVolumesById(id needle.VolumeId) (hasVolumeId bool) {
  49. // check whether normal volumes has this volume id
  50. d.RLock()
  51. _, ok := d.volumes[id]
  52. if ok {
  53. hasVolumeId = true
  54. }
  55. d.RUnlock()
  56. if hasVolumeId {
  57. return
  58. }
  59. // check whether ec shards has this volume id
  60. d.ecShardsLock.RLock()
  61. _, ok = d.ecShards[id]
  62. if ok {
  63. hasVolumeId = true
  64. }
  65. d.ecShardsLock.RUnlock()
  66. return
  67. }