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.

562 lines
15 KiB

6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
adding locking to avoid nil VolumeLocationList fix panic: runtime error: invalid memory address or nil pointer dereference Oct 22 00:53:44 bedb-master1 weed[8055]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x17658da] Oct 22 00:53:44 bedb-master1 weed[8055]: goroutine 310 [running]: Oct 22 00:53:44 bedb-master1 weed[8055]: github.com/chrislusf/seaweedfs/weed/topology.(*VolumeLocationList).Length(...) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/seaweedfs/weed/topology/volume_location_list.go:35 Oct 22 00:53:44 bedb-master1 weed[8055]: github.com/chrislusf/seaweedfs/weed/topology.(*VolumeLayout).enoughCopies(...) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/seaweedfs/weed/topology/volume_layout.go:376 Oct 22 00:53:44 bedb-master1 weed[8055]: github.com/chrislusf/seaweedfs/weed/topology.(*VolumeLayout).ensureCorrectWritables(0xc000111d50, 0xc000b55438) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/seaweedfs/weed/topology/volume_layout.go:202 +0x5a Oct 22 00:53:44 bedb-master1 weed[8055]: github.com/chrislusf/seaweedfs/weed/topology.(*Topology).SyncDataNodeRegistration(0xc00042ac60, 0xc001454d30, 0x1, 0x1, 0xc0005fc000, 0xc00135de40, 0x4, 0xc00135de50, 0x10, 0x10d, ...) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/seaweedfs/weed/topology/topology.go:224 +0x616 Oct 22 00:53:44 bedb-master1 weed[8055]: github.com/chrislusf/seaweedfs/weed/server.(*MasterServer).SendHeartbeat(0xc000162700, 0x23b97c0, 0xc000ae2c90, 0x0, 0x0) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/seaweedfs/weed/server/master_grpc_server.go:106 +0x325 Oct 22 00:53:44 bedb-master1 weed[8055]: github.com/chrislusf/seaweedfs/weed/pb/master_pb._Seaweed_SendHeartbeat_Handler(0x1f8e7c0, 0xc000162700, 0x23b0a60, 0xc00024b440, 0x3172c38, 0xc000ab7100) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/seaweedfs/weed/pb/master_pb/master.pb.go:4250 +0xad Oct 22 00:53:44 bedb-master1 weed[8055]: google.golang.org/grpc.(*Server).processStreamingRPC(0xc0001f31e0, 0x23bb800, 0xc000ac5500, 0xc000ab7100, 0xc0001fea80, 0x311fec0, 0x0, 0x0, 0x0) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1329 +0xcd8 Oct 22 00:53:44 bedb-master1 weed[8055]: google.golang.org/grpc.(*Server).handleStream(0xc0001f31e0, 0x23bb800, 0xc000ac5500, 0xc000ab7100, 0x0) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1409 +0xc5c Oct 22 00:53:44 bedb-master1 weed[8055]: google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc0001ce8b0, 0xc0001f31e0, 0x23bb800, 0xc000ac5500, 0xc000ab7100) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:746 +0xa5 Oct 22 00:53:44 bedb-master1 weed[8055]: created by google.golang.org/grpc.(*Server).serveStreams.func1 Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:744 +0xa5 Oct 22 00:53:44 bedb-master1 systemd[1]: weedmaster.service: Main process exited, code=exited, status=2/INVALIDARGUMENT Oct 22 00:53:44 bedb-master1 systemd[1]: weedmaster.service: Failed with result 'exit-code'.
4 years ago
adding locking to avoid nil VolumeLocationList fix panic: runtime error: invalid memory address or nil pointer dereference Oct 22 00:53:44 bedb-master1 weed[8055]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x17658da] Oct 22 00:53:44 bedb-master1 weed[8055]: goroutine 310 [running]: Oct 22 00:53:44 bedb-master1 weed[8055]: github.com/chrislusf/seaweedfs/weed/topology.(*VolumeLocationList).Length(...) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/seaweedfs/weed/topology/volume_location_list.go:35 Oct 22 00:53:44 bedb-master1 weed[8055]: github.com/chrislusf/seaweedfs/weed/topology.(*VolumeLayout).enoughCopies(...) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/seaweedfs/weed/topology/volume_layout.go:376 Oct 22 00:53:44 bedb-master1 weed[8055]: github.com/chrislusf/seaweedfs/weed/topology.(*VolumeLayout).ensureCorrectWritables(0xc000111d50, 0xc000b55438) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/seaweedfs/weed/topology/volume_layout.go:202 +0x5a Oct 22 00:53:44 bedb-master1 weed[8055]: github.com/chrislusf/seaweedfs/weed/topology.(*Topology).SyncDataNodeRegistration(0xc00042ac60, 0xc001454d30, 0x1, 0x1, 0xc0005fc000, 0xc00135de40, 0x4, 0xc00135de50, 0x10, 0x10d, ...) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/seaweedfs/weed/topology/topology.go:224 +0x616 Oct 22 00:53:44 bedb-master1 weed[8055]: github.com/chrislusf/seaweedfs/weed/server.(*MasterServer).SendHeartbeat(0xc000162700, 0x23b97c0, 0xc000ae2c90, 0x0, 0x0) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/seaweedfs/weed/server/master_grpc_server.go:106 +0x325 Oct 22 00:53:44 bedb-master1 weed[8055]: github.com/chrislusf/seaweedfs/weed/pb/master_pb._Seaweed_SendHeartbeat_Handler(0x1f8e7c0, 0xc000162700, 0x23b0a60, 0xc00024b440, 0x3172c38, 0xc000ab7100) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/seaweedfs/weed/pb/master_pb/master.pb.go:4250 +0xad Oct 22 00:53:44 bedb-master1 weed[8055]: google.golang.org/grpc.(*Server).processStreamingRPC(0xc0001f31e0, 0x23bb800, 0xc000ac5500, 0xc000ab7100, 0xc0001fea80, 0x311fec0, 0x0, 0x0, 0x0) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1329 +0xcd8 Oct 22 00:53:44 bedb-master1 weed[8055]: google.golang.org/grpc.(*Server).handleStream(0xc0001f31e0, 0x23bb800, 0xc000ac5500, 0xc000ab7100, 0x0) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1409 +0xc5c Oct 22 00:53:44 bedb-master1 weed[8055]: google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc0001ce8b0, 0xc0001f31e0, 0x23bb800, 0xc000ac5500, 0xc000ab7100) Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:746 +0xa5 Oct 22 00:53:44 bedb-master1 weed[8055]: created by google.golang.org/grpc.(*Server).serveStreams.func1 Oct 22 00:53:44 bedb-master1 weed[8055]: #011/root/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:744 +0xa5 Oct 22 00:53:44 bedb-master1 systemd[1]: weedmaster.service: Main process exited, code=exited, status=2/INVALIDARGUMENT Oct 22 00:53:44 bedb-master1 systemd[1]: weedmaster.service: Failed with result 'exit-code'.
4 years ago
6 years ago
6 years ago
3 years ago
3 years ago
7 months ago
6 years ago
6 years ago
6 years ago
6 years ago
5 months ago
  1. package topology
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/stats"
  5. "math/rand/v2"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/storage"
  12. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  13. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  14. )
  15. type copyState int
  16. const (
  17. noCopies copyState = 0 + iota
  18. insufficientCopies
  19. enoughCopies
  20. )
  21. type volumeState string
  22. const (
  23. readOnlyState volumeState = "ReadOnly"
  24. oversizedState = "Oversized"
  25. crowdedState = "Crowded"
  26. noWritableVolumes = "No writable volumes"
  27. )
  28. type stateIndicator func(copyState) bool
  29. func ExistCopies() stateIndicator {
  30. return func(state copyState) bool { return state != noCopies }
  31. }
  32. func NoCopies() stateIndicator {
  33. return func(state copyState) bool { return state == noCopies }
  34. }
  35. type volumesBinaryState struct {
  36. rp *super_block.ReplicaPlacement
  37. name volumeState // the name for volume state (eg. "Readonly", "Oversized")
  38. indicator stateIndicator // indicate whether the volumes should be marked as `name`
  39. copyMap map[needle.VolumeId]*VolumeLocationList
  40. }
  41. func NewVolumesBinaryState(name volumeState, rp *super_block.ReplicaPlacement, indicator stateIndicator) *volumesBinaryState {
  42. return &volumesBinaryState{
  43. rp: rp,
  44. name: name,
  45. indicator: indicator,
  46. copyMap: make(map[needle.VolumeId]*VolumeLocationList),
  47. }
  48. }
  49. func (v *volumesBinaryState) Dump() (res []uint32) {
  50. for vid, list := range v.copyMap {
  51. if v.indicator(v.copyState(list)) {
  52. res = append(res, uint32(vid))
  53. }
  54. }
  55. return
  56. }
  57. func (v *volumesBinaryState) IsTrue(vid needle.VolumeId) bool {
  58. list, _ := v.copyMap[vid]
  59. return v.indicator(v.copyState(list))
  60. }
  61. func (v *volumesBinaryState) Add(vid needle.VolumeId, dn *DataNode) {
  62. list, _ := v.copyMap[vid]
  63. if list != nil {
  64. list.Set(dn)
  65. return
  66. }
  67. list = NewVolumeLocationList()
  68. list.Set(dn)
  69. v.copyMap[vid] = list
  70. }
  71. func (v *volumesBinaryState) Remove(vid needle.VolumeId, dn *DataNode) {
  72. list, _ := v.copyMap[vid]
  73. if list != nil {
  74. list.Remove(dn)
  75. if list.Length() == 0 {
  76. delete(v.copyMap, vid)
  77. }
  78. }
  79. }
  80. func (v *volumesBinaryState) copyState(list *VolumeLocationList) copyState {
  81. if list == nil {
  82. return noCopies
  83. }
  84. if list.Length() < v.rp.GetCopyCount() {
  85. return insufficientCopies
  86. }
  87. return enoughCopies
  88. }
  89. // mapping from volume to its locations, inverted from server to volume
  90. type VolumeLayout struct {
  91. growRequest atomic.Bool
  92. lastGrowCount atomic.Uint32
  93. rp *super_block.ReplicaPlacement
  94. ttl *needle.TTL
  95. diskType types.DiskType
  96. vid2location map[needle.VolumeId]*VolumeLocationList
  97. writables []needle.VolumeId // transient array of writable volume id
  98. crowded map[needle.VolumeId]struct{}
  99. readonlyVolumes *volumesBinaryState // readonly volumes
  100. oversizedVolumes *volumesBinaryState // oversized volumes
  101. vacuumedVolumes map[needle.VolumeId]time.Time
  102. volumeSizeLimit uint64
  103. replicationAsMin bool
  104. accessLock sync.RWMutex
  105. }
  106. type VolumeLayoutStats struct {
  107. TotalSize uint64
  108. UsedSize uint64
  109. FileCount uint64
  110. }
  111. func NewVolumeLayout(rp *super_block.ReplicaPlacement, ttl *needle.TTL, diskType types.DiskType, volumeSizeLimit uint64, replicationAsMin bool) *VolumeLayout {
  112. return &VolumeLayout{
  113. rp: rp,
  114. ttl: ttl,
  115. diskType: diskType,
  116. vid2location: make(map[needle.VolumeId]*VolumeLocationList),
  117. writables: *new([]needle.VolumeId),
  118. crowded: make(map[needle.VolumeId]struct{}),
  119. readonlyVolumes: NewVolumesBinaryState(readOnlyState, rp, ExistCopies()),
  120. oversizedVolumes: NewVolumesBinaryState(oversizedState, rp, ExistCopies()),
  121. vacuumedVolumes: make(map[needle.VolumeId]time.Time),
  122. volumeSizeLimit: volumeSizeLimit,
  123. replicationAsMin: replicationAsMin,
  124. }
  125. }
  126. func (vl *VolumeLayout) String() string {
  127. return fmt.Sprintf("rp:%v, ttl:%v, writables:%v, volumeSizeLimit:%v", vl.rp, vl.ttl, vl.writables, vl.volumeSizeLimit)
  128. }
  129. func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  130. vl.accessLock.Lock()
  131. defer vl.accessLock.Unlock()
  132. defer vl.rememberOversizedVolume(v, dn)
  133. if _, ok := vl.vid2location[v.Id]; !ok {
  134. vl.vid2location[v.Id] = NewVolumeLocationList()
  135. }
  136. vl.vid2location[v.Id].Set(dn)
  137. // glog.V(4).Infof("volume %d added to %s len %d copy %d", v.Id, dn.Id(), vl.vid2location[v.Id].Length(), v.ReplicaPlacement.GetCopyCount())
  138. for _, dn := range vl.vid2location[v.Id].list {
  139. if vInfo, err := dn.GetVolumesById(v.Id); err == nil {
  140. if vInfo.ReadOnly {
  141. glog.V(1).Infof("vid %d removed from writable", v.Id)
  142. vl.removeFromWritable(v.Id)
  143. vl.readonlyVolumes.Add(v.Id, dn)
  144. return
  145. } else {
  146. vl.readonlyVolumes.Remove(v.Id, dn)
  147. }
  148. } else {
  149. glog.V(1).Infof("vid %d removed from writable", v.Id)
  150. vl.removeFromWritable(v.Id)
  151. vl.readonlyVolumes.Remove(v.Id, dn)
  152. return
  153. }
  154. }
  155. }
  156. func (vl *VolumeLayout) rememberOversizedVolume(v *storage.VolumeInfo, dn *DataNode) {
  157. if vl.isOversized(v) {
  158. vl.oversizedVolumes.Add(v.Id, dn)
  159. } else {
  160. vl.oversizedVolumes.Remove(v.Id, dn)
  161. }
  162. }
  163. func (vl *VolumeLayout) UnRegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  164. vl.accessLock.Lock()
  165. defer vl.accessLock.Unlock()
  166. // remove from vid2location map
  167. location, ok := vl.vid2location[v.Id]
  168. if !ok {
  169. return
  170. }
  171. if location.Remove(dn) {
  172. vl.readonlyVolumes.Remove(v.Id, dn)
  173. vl.oversizedVolumes.Remove(v.Id, dn)
  174. vl.ensureCorrectWritables(v.Id)
  175. if location.Length() == 0 {
  176. delete(vl.vid2location, v.Id)
  177. }
  178. }
  179. }
  180. func (vl *VolumeLayout) EnsureCorrectWritables(v *storage.VolumeInfo) {
  181. vl.accessLock.Lock()
  182. defer vl.accessLock.Unlock()
  183. vl.ensureCorrectWritables(v.Id)
  184. }
  185. func (vl *VolumeLayout) ensureCorrectWritables(vid needle.VolumeId) {
  186. if vl.enoughCopies(vid) && vl.isAllWritable(vid) {
  187. if !vl.oversizedVolumes.IsTrue(vid) {
  188. vl.setVolumeWritable(vid)
  189. }
  190. } else {
  191. if !vl.enoughCopies(vid) {
  192. glog.V(0).Infof("volume %d does not have enough copies", vid)
  193. }
  194. if !vl.isAllWritable(vid) {
  195. glog.V(0).Infof("volume %d are not all writable", vid)
  196. }
  197. glog.V(0).Infof("volume %d remove from writable", vid)
  198. vl.removeFromWritable(vid)
  199. }
  200. }
  201. func (vl *VolumeLayout) isAllWritable(vid needle.VolumeId) bool {
  202. if location, ok := vl.vid2location[vid]; ok {
  203. for _, dn := range location.list {
  204. if v, getError := dn.GetVolumesById(vid); getError == nil {
  205. if v.ReadOnly {
  206. return false
  207. }
  208. }
  209. }
  210. } else {
  211. return false
  212. }
  213. return true
  214. }
  215. func (vl *VolumeLayout) isOversized(v *storage.VolumeInfo) bool {
  216. return uint64(v.Size) >= vl.volumeSizeLimit
  217. }
  218. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  219. return !vl.isOversized(v) &&
  220. v.Version == needle.CurrentVersion &&
  221. !v.ReadOnly
  222. }
  223. func (vl *VolumeLayout) isEmpty() bool {
  224. vl.accessLock.RLock()
  225. defer vl.accessLock.RUnlock()
  226. return len(vl.vid2location) == 0
  227. }
  228. func (vl *VolumeLayout) Lookup(vid needle.VolumeId) []*DataNode {
  229. vl.accessLock.RLock()
  230. defer vl.accessLock.RUnlock()
  231. if location := vl.vid2location[vid]; location != nil {
  232. return location.list
  233. }
  234. return nil
  235. }
  236. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  237. vl.accessLock.RLock()
  238. defer vl.accessLock.RUnlock()
  239. for _, location := range vl.vid2location {
  240. nodes = append(nodes, location.list...)
  241. }
  242. return
  243. }
  244. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (vid needle.VolumeId, counter uint64, locationList *VolumeLocationList, shouldGrow bool, err error) {
  245. vl.accessLock.RLock()
  246. defer vl.accessLock.RUnlock()
  247. lenWriters := len(vl.writables)
  248. if lenWriters <= 0 {
  249. return 0, 0, nil, true, fmt.Errorf("%s in volume layout", noWritableVolumes)
  250. }
  251. if option.DataCenter == "" && option.Rack == "" && option.DataNode == "" {
  252. vid := vl.writables[rand.IntN(lenWriters)]
  253. locationList = vl.vid2location[vid]
  254. if locationList == nil || len(locationList.list) == 0 {
  255. return 0, 0, nil, false, fmt.Errorf("Strangely vid %s is on no machine!", vid.String())
  256. }
  257. return vid, count, locationList.Copy(), false, nil
  258. }
  259. // clone vl.writables
  260. writables := make([]needle.VolumeId, len(vl.writables))
  261. copy(writables, vl.writables)
  262. // randomize the writables
  263. rand.Shuffle(len(writables), func(i, j int) {
  264. writables[i], writables[j] = writables[j], writables[i]
  265. })
  266. for _, writableVolumeId := range writables {
  267. volumeLocationList := vl.vid2location[writableVolumeId]
  268. for _, dn := range volumeLocationList.list {
  269. if option.DataCenter != "" && dn.GetDataCenter().Id() != NodeId(option.DataCenter) {
  270. continue
  271. }
  272. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  273. continue
  274. }
  275. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  276. continue
  277. }
  278. vid, locationList, counter = writableVolumeId, volumeLocationList.Copy(), count
  279. return
  280. }
  281. }
  282. return vid, count, locationList, true, fmt.Errorf("%s in DataCenter:%v Rack:%v DataNode:%v", noWritableVolumes, option.DataCenter, option.Rack, option.DataNode)
  283. }
  284. func (vl *VolumeLayout) HasGrowRequest() bool {
  285. return vl.growRequest.Load()
  286. }
  287. func (vl *VolumeLayout) AddGrowRequest() {
  288. vl.growRequest.Store(true)
  289. }
  290. func (vl *VolumeLayout) DoneGrowRequest() {
  291. vl.growRequest.Store(false)
  292. }
  293. func (vl *VolumeLayout) SetLastGrowCount(count uint32) {
  294. if vl.lastGrowCount.Load() != count && count != 0 {
  295. vl.lastGrowCount.Store(count)
  296. }
  297. }
  298. func (vl *VolumeLayout) GetLastGrowCount() uint32 {
  299. return vl.lastGrowCount.Load()
  300. }
  301. func (vl *VolumeLayout) ShouldGrowVolumes(option *VolumeGrowOption) bool {
  302. total, active, crowded := vl.GetActiveVolumeCount(option)
  303. stats.MasterVolumeLayout.WithLabelValues(option.Collection, option.DataCenter, "total").Set(float64(total))
  304. stats.MasterVolumeLayout.WithLabelValues(option.Collection, option.DataCenter, "active").Set(float64(active))
  305. stats.MasterVolumeLayout.WithLabelValues(option.Collection, option.DataCenter, "crowded").Set(float64(crowded))
  306. //glog.V(0).Infof("active volume: %d, high usage volume: %d\n", active, high)
  307. return active <= crowded
  308. }
  309. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) (total, active, crowded int) {
  310. vl.accessLock.RLock()
  311. defer vl.accessLock.RUnlock()
  312. if option.DataCenter == "" {
  313. return len(vl.writables), len(vl.writables), len(vl.crowded)
  314. }
  315. total = len(vl.writables)
  316. for _, v := range vl.writables {
  317. for _, dn := range vl.vid2location[v].list {
  318. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  319. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  320. continue
  321. }
  322. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  323. continue
  324. }
  325. active++
  326. info, _ := dn.GetVolumesById(v)
  327. if float64(info.Size) > float64(vl.volumeSizeLimit)*VolumeGrowStrategy.Threshold {
  328. crowded++
  329. }
  330. }
  331. }
  332. }
  333. return
  334. }
  335. func (vl *VolumeLayout) removeFromWritable(vid needle.VolumeId) bool {
  336. toDeleteIndex := -1
  337. for k, id := range vl.writables {
  338. if id == vid {
  339. toDeleteIndex = k
  340. break
  341. }
  342. }
  343. if toDeleteIndex >= 0 {
  344. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  345. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  346. vl.removeFromCrowded(vid)
  347. return true
  348. }
  349. return false
  350. }
  351. func (vl *VolumeLayout) setVolumeWritable(vid needle.VolumeId) bool {
  352. for _, v := range vl.writables {
  353. if v == vid {
  354. return false
  355. }
  356. }
  357. glog.V(0).Infoln("Volume", vid, "becomes writable")
  358. vl.writables = append(vl.writables, vid)
  359. return true
  360. }
  361. func (vl *VolumeLayout) SetVolumeReadOnly(dn *DataNode, vid needle.VolumeId) bool {
  362. vl.accessLock.Lock()
  363. defer vl.accessLock.Unlock()
  364. if _, ok := vl.vid2location[vid]; ok {
  365. vl.readonlyVolumes.Add(vid, dn)
  366. return vl.removeFromWritable(vid)
  367. }
  368. return true
  369. }
  370. func (vl *VolumeLayout) SetVolumeWritable(dn *DataNode, vid needle.VolumeId) bool {
  371. vl.accessLock.Lock()
  372. defer vl.accessLock.Unlock()
  373. if _, ok := vl.vid2location[vid]; ok {
  374. vl.readonlyVolumes.Remove(vid, dn)
  375. }
  376. if vl.enoughCopies(vid) {
  377. return vl.setVolumeWritable(vid)
  378. }
  379. return false
  380. }
  381. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid needle.VolumeId) bool {
  382. vl.accessLock.Lock()
  383. defer vl.accessLock.Unlock()
  384. if location, ok := vl.vid2location[vid]; ok {
  385. if location.Remove(dn) {
  386. vl.readonlyVolumes.Remove(vid, dn)
  387. vl.oversizedVolumes.Remove(vid, dn)
  388. if location.Length() < vl.rp.GetCopyCount() {
  389. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  390. return vl.removeFromWritable(vid)
  391. }
  392. }
  393. }
  394. return false
  395. }
  396. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid needle.VolumeId, isReadOnly, isFullCapacity bool) bool {
  397. vl.accessLock.Lock()
  398. defer vl.accessLock.Unlock()
  399. vInfo, err := dn.GetVolumesById(vid)
  400. if err != nil {
  401. return false
  402. }
  403. vl.vid2location[vid].Set(dn)
  404. if vInfo.ReadOnly || isReadOnly || isFullCapacity {
  405. return false
  406. }
  407. if vl.enoughCopies(vid) {
  408. return vl.setVolumeWritable(vid)
  409. }
  410. return false
  411. }
  412. func (vl *VolumeLayout) enoughCopies(vid needle.VolumeId) bool {
  413. locations := vl.vid2location[vid].Length()
  414. desired := vl.rp.GetCopyCount()
  415. return locations == desired || (vl.replicationAsMin && locations > desired)
  416. }
  417. func (vl *VolumeLayout) SetVolumeCapacityFull(vid needle.VolumeId) bool {
  418. vl.accessLock.Lock()
  419. defer vl.accessLock.Unlock()
  420. wasWritable := vl.removeFromWritable(vid)
  421. if wasWritable {
  422. glog.V(0).Infof("Volume %d reaches full capacity.", vid)
  423. }
  424. return wasWritable
  425. }
  426. func (vl *VolumeLayout) removeFromCrowded(vid needle.VolumeId) {
  427. delete(vl.crowded, vid)
  428. }
  429. func (vl *VolumeLayout) setVolumeCrowded(vid needle.VolumeId) {
  430. if _, ok := vl.crowded[vid]; !ok {
  431. vl.crowded[vid] = struct{}{}
  432. glog.V(0).Infoln("Volume", vid, "becomes crowded")
  433. }
  434. }
  435. func (vl *VolumeLayout) SetVolumeCrowded(vid needle.VolumeId) {
  436. // since delete is guarded by accessLock.Lock(),
  437. // and is always called in sequential order,
  438. // RLock() should be safe enough
  439. vl.accessLock.RLock()
  440. defer vl.accessLock.RUnlock()
  441. vl.setVolumeCrowded(vid)
  442. }
  443. type VolumeLayoutInfo struct {
  444. Replication string `json:"replication"`
  445. TTL string `json:"ttl"`
  446. Writables []needle.VolumeId `json:"writables"`
  447. Collection string `json:"collection"`
  448. DiskType string `json:"diskType"`
  449. }
  450. func (vl *VolumeLayout) ToInfo() (info VolumeLayoutInfo) {
  451. info.Replication = vl.rp.String()
  452. info.TTL = vl.ttl.String()
  453. info.Writables = vl.writables
  454. info.DiskType = vl.diskType.ReadableString()
  455. //m["locations"] = vl.vid2location
  456. return
  457. }
  458. func (vl *VolumeLayout) ToGrowOption() (option *VolumeGrowOption) {
  459. option = &VolumeGrowOption{}
  460. option.ReplicaPlacement = vl.rp
  461. option.Ttl = vl.ttl
  462. option.DiskType = vl.diskType
  463. return
  464. }
  465. func (vl *VolumeLayout) Stats() *VolumeLayoutStats {
  466. vl.accessLock.RLock()
  467. defer vl.accessLock.RUnlock()
  468. ret := &VolumeLayoutStats{}
  469. freshThreshold := time.Now().Unix() - 60
  470. for vid, vll := range vl.vid2location {
  471. size, fileCount := vll.Stats(vid, freshThreshold)
  472. ret.FileCount += uint64(fileCount)
  473. ret.UsedSize += size * uint64(vll.Length())
  474. if vl.readonlyVolumes.IsTrue(vid) {
  475. ret.TotalSize += size * uint64(vll.Length())
  476. } else {
  477. ret.TotalSize += vl.volumeSizeLimit * uint64(vll.Length())
  478. }
  479. }
  480. return ret
  481. }