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.

496 lines
13 KiB

6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 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
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package topology
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/storage/types"
  6. "math/rand"
  7. "sync"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/storage"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  13. )
  14. type copyState int
  15. const (
  16. noCopies copyState = 0 + iota
  17. insufficientCopies
  18. enoughCopies
  19. )
  20. type volumeState string
  21. const (
  22. readOnlyState volumeState = "ReadOnly"
  23. oversizedState = "Oversized"
  24. crowdedState = "Crowded"
  25. )
  26. type stateIndicator func(copyState) bool
  27. func ExistCopies() stateIndicator {
  28. return func(state copyState) bool { return state != noCopies }
  29. }
  30. func NoCopies() stateIndicator {
  31. return func(state copyState) bool { return state == noCopies }
  32. }
  33. type volumesBinaryState struct {
  34. rp *super_block.ReplicaPlacement
  35. name volumeState // the name for volume state (eg. "Readonly", "Oversized")
  36. indicator stateIndicator // indicate whether the volumes should be marked as `name`
  37. copyMap map[needle.VolumeId]*VolumeLocationList
  38. }
  39. func NewVolumesBinaryState(name volumeState, rp *super_block.ReplicaPlacement, indicator stateIndicator) *volumesBinaryState {
  40. return &volumesBinaryState{
  41. rp: rp,
  42. name: name,
  43. indicator: indicator,
  44. copyMap: make(map[needle.VolumeId]*VolumeLocationList),
  45. }
  46. }
  47. func (v *volumesBinaryState) Dump() (res []uint32) {
  48. for vid, list := range v.copyMap {
  49. if v.indicator(v.copyState(list)) {
  50. res = append(res, uint32(vid))
  51. }
  52. }
  53. return
  54. }
  55. func (v *volumesBinaryState) IsTrue(vid needle.VolumeId) bool {
  56. list, _ := v.copyMap[vid]
  57. return v.indicator(v.copyState(list))
  58. }
  59. func (v *volumesBinaryState) Add(vid needle.VolumeId, dn *DataNode) {
  60. list, _ := v.copyMap[vid]
  61. if list != nil {
  62. list.Set(dn)
  63. return
  64. }
  65. list = NewVolumeLocationList()
  66. list.Set(dn)
  67. v.copyMap[vid] = list
  68. }
  69. func (v *volumesBinaryState) Remove(vid needle.VolumeId, dn *DataNode) {
  70. list, _ := v.copyMap[vid]
  71. if list != nil {
  72. list.Remove(dn)
  73. if list.Length() == 0 {
  74. delete(v.copyMap, vid)
  75. }
  76. }
  77. }
  78. func (v *volumesBinaryState) copyState(list *VolumeLocationList) copyState {
  79. if list == nil {
  80. return noCopies
  81. }
  82. if list.Length() < v.rp.GetCopyCount() {
  83. return insufficientCopies
  84. }
  85. return enoughCopies
  86. }
  87. // mapping from volume to its locations, inverted from server to volume
  88. type VolumeLayout struct {
  89. rp *super_block.ReplicaPlacement
  90. ttl *needle.TTL
  91. diskType types.DiskType
  92. vid2location map[needle.VolumeId]*VolumeLocationList
  93. writables []needle.VolumeId // transient array of writable volume id
  94. crowded map[needle.VolumeId]struct{}
  95. readonlyVolumes *volumesBinaryState // readonly volumes
  96. oversizedVolumes *volumesBinaryState // oversized volumes
  97. volumeSizeLimit uint64
  98. replicationAsMin bool
  99. accessLock sync.RWMutex
  100. growRequestCount int
  101. growRequestTime time.Time
  102. }
  103. type VolumeLayoutStats struct {
  104. TotalSize uint64
  105. UsedSize uint64
  106. FileCount uint64
  107. }
  108. func NewVolumeLayout(rp *super_block.ReplicaPlacement, ttl *needle.TTL, diskType types.DiskType, volumeSizeLimit uint64, replicationAsMin bool) *VolumeLayout {
  109. return &VolumeLayout{
  110. rp: rp,
  111. ttl: ttl,
  112. diskType: diskType,
  113. vid2location: make(map[needle.VolumeId]*VolumeLocationList),
  114. writables: *new([]needle.VolumeId),
  115. crowded: make(map[needle.VolumeId]struct{}),
  116. readonlyVolumes: NewVolumesBinaryState(readOnlyState, rp, ExistCopies()),
  117. oversizedVolumes: NewVolumesBinaryState(oversizedState, rp, ExistCopies()),
  118. volumeSizeLimit: volumeSizeLimit,
  119. replicationAsMin: replicationAsMin,
  120. }
  121. }
  122. func (vl *VolumeLayout) String() string {
  123. vl.accessLock.RLock()
  124. defer vl.accessLock.RUnlock()
  125. return fmt.Sprintf("rp:%v, ttl:%v, vid2location:%v, writables:%v, volumeSizeLimit:%v", vl.rp, vl.ttl, vl.vid2location, vl.writables, vl.volumeSizeLimit)
  126. }
  127. func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  128. vl.accessLock.Lock()
  129. defer vl.accessLock.Unlock()
  130. defer vl.rememberOversizedVolume(v, dn)
  131. if _, ok := vl.vid2location[v.Id]; !ok {
  132. vl.vid2location[v.Id] = NewVolumeLocationList()
  133. }
  134. vl.vid2location[v.Id].Set(dn)
  135. // 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())
  136. for _, dn := range vl.vid2location[v.Id].list {
  137. if vInfo, err := dn.GetVolumesById(v.Id); err == nil {
  138. if vInfo.ReadOnly {
  139. glog.V(1).Infof("vid %d removed from writable", v.Id)
  140. vl.removeFromWritable(v.Id)
  141. vl.readonlyVolumes.Add(v.Id, dn)
  142. return
  143. } else {
  144. vl.readonlyVolumes.Remove(v.Id, dn)
  145. }
  146. } else {
  147. glog.V(1).Infof("vid %d removed from writable", v.Id)
  148. vl.removeFromWritable(v.Id)
  149. vl.readonlyVolumes.Remove(v.Id, dn)
  150. return
  151. }
  152. }
  153. }
  154. func (vl *VolumeLayout) rememberOversizedVolume(v *storage.VolumeInfo, dn *DataNode) {
  155. if vl.isOversized(v) {
  156. vl.oversizedVolumes.Add(v.Id, dn)
  157. } else {
  158. vl.oversizedVolumes.Remove(v.Id, dn)
  159. }
  160. }
  161. func (vl *VolumeLayout) UnRegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  162. vl.accessLock.Lock()
  163. defer vl.accessLock.Unlock()
  164. // remove from vid2location map
  165. location, ok := vl.vid2location[v.Id]
  166. if !ok {
  167. return
  168. }
  169. if location.Remove(dn) {
  170. vl.readonlyVolumes.Remove(v.Id, dn)
  171. vl.oversizedVolumes.Remove(v.Id, dn)
  172. vl.ensureCorrectWritables(v.Id)
  173. if location.Length() == 0 {
  174. delete(vl.vid2location, v.Id)
  175. }
  176. }
  177. }
  178. func (vl *VolumeLayout) EnsureCorrectWritables(v *storage.VolumeInfo) {
  179. vl.accessLock.Lock()
  180. defer vl.accessLock.Unlock()
  181. vl.ensureCorrectWritables(v.Id)
  182. }
  183. func (vl *VolumeLayout) ensureCorrectWritables(vid needle.VolumeId) {
  184. if vl.enoughCopies(vid) && vl.isAllWritable(vid) {
  185. if !vl.oversizedVolumes.IsTrue(vid) {
  186. vl.setVolumeWritable(vid)
  187. }
  188. } else {
  189. vl.removeFromWritable(vid)
  190. }
  191. }
  192. func (vl *VolumeLayout) isAllWritable(vid needle.VolumeId) bool {
  193. for _, dn := range vl.vid2location[vid].list {
  194. if v, getError := dn.GetVolumesById(vid); getError == nil {
  195. if v.ReadOnly {
  196. return false
  197. }
  198. }
  199. }
  200. return true
  201. }
  202. func (vl *VolumeLayout) isOversized(v *storage.VolumeInfo) bool {
  203. return uint64(v.Size) >= vl.volumeSizeLimit
  204. }
  205. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  206. return !vl.isOversized(v) &&
  207. v.Version == needle.CurrentVersion &&
  208. !v.ReadOnly
  209. }
  210. func (vl *VolumeLayout) isEmpty() bool {
  211. vl.accessLock.RLock()
  212. defer vl.accessLock.RUnlock()
  213. return len(vl.vid2location) == 0
  214. }
  215. func (vl *VolumeLayout) Lookup(vid needle.VolumeId) []*DataNode {
  216. vl.accessLock.RLock()
  217. defer vl.accessLock.RUnlock()
  218. if location := vl.vid2location[vid]; location != nil {
  219. return location.list
  220. }
  221. return nil
  222. }
  223. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  224. vl.accessLock.RLock()
  225. defer vl.accessLock.RUnlock()
  226. for _, location := range vl.vid2location {
  227. nodes = append(nodes, location.list...)
  228. }
  229. return
  230. }
  231. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (*needle.VolumeId, uint64, *VolumeLocationList, error) {
  232. vl.accessLock.RLock()
  233. defer vl.accessLock.RUnlock()
  234. lenWriters := len(vl.writables)
  235. if lenWriters <= 0 {
  236. //glog.V(0).Infoln("No more writable volumes!")
  237. return nil, 0, nil, errors.New("No more writable volumes!")
  238. }
  239. if option.DataCenter == "" {
  240. vid := vl.writables[rand.Intn(lenWriters)]
  241. locationList := vl.vid2location[vid]
  242. if locationList != nil {
  243. return &vid, count, locationList, nil
  244. }
  245. return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  246. }
  247. var vid needle.VolumeId
  248. var locationList *VolumeLocationList
  249. counter := 0
  250. for _, v := range vl.writables {
  251. volumeLocationList := vl.vid2location[v]
  252. for _, dn := range volumeLocationList.list {
  253. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  254. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  255. continue
  256. }
  257. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  258. continue
  259. }
  260. counter++
  261. if rand.Intn(counter) < 1 {
  262. vid, locationList = v, volumeLocationList.Copy()
  263. }
  264. }
  265. }
  266. }
  267. return &vid, count, locationList, nil
  268. }
  269. func (vl *VolumeLayout) HasGrowRequest() bool {
  270. if vl.growRequestCount > 0 && vl.growRequestTime.Add(time.Minute).After(time.Now()) {
  271. return true
  272. }
  273. return false
  274. }
  275. func (vl *VolumeLayout) AddGrowRequest() {
  276. vl.growRequestTime = time.Now()
  277. vl.growRequestCount++
  278. }
  279. func (vl *VolumeLayout) DoneGrowRequest() {
  280. vl.growRequestTime = time.Unix(0,0)
  281. vl.growRequestCount = 0
  282. }
  283. func (vl *VolumeLayout) ShouldGrowVolumes(option *VolumeGrowOption) bool {
  284. active, crowded := vl.GetActiveVolumeCount(option)
  285. //glog.V(0).Infof("active volume: %d, high usage volume: %d\n", active, high)
  286. return active <= crowded
  287. }
  288. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) (active, crowded int) {
  289. vl.accessLock.RLock()
  290. defer vl.accessLock.RUnlock()
  291. if option.DataCenter == "" {
  292. return len(vl.writables), len(vl.crowded)
  293. }
  294. for _, v := range vl.writables {
  295. for _, dn := range vl.vid2location[v].list {
  296. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  297. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  298. continue
  299. }
  300. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  301. continue
  302. }
  303. active++
  304. info, _ := dn.GetVolumesById(v)
  305. if float64(info.Size) > float64(vl.volumeSizeLimit)*option.Threshold() {
  306. crowded++
  307. }
  308. }
  309. }
  310. }
  311. return
  312. }
  313. func (vl *VolumeLayout) removeFromWritable(vid needle.VolumeId) bool {
  314. toDeleteIndex := -1
  315. for k, id := range vl.writables {
  316. if id == vid {
  317. toDeleteIndex = k
  318. break
  319. }
  320. }
  321. if toDeleteIndex >= 0 {
  322. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  323. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  324. vl.removeFromCrowded(vid)
  325. return true
  326. }
  327. return false
  328. }
  329. func (vl *VolumeLayout) setVolumeWritable(vid needle.VolumeId) bool {
  330. for _, v := range vl.writables {
  331. if v == vid {
  332. return false
  333. }
  334. }
  335. glog.V(0).Infoln("Volume", vid, "becomes writable")
  336. vl.writables = append(vl.writables, vid)
  337. return true
  338. }
  339. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid needle.VolumeId) bool {
  340. vl.accessLock.Lock()
  341. defer vl.accessLock.Unlock()
  342. if location, ok := vl.vid2location[vid]; ok {
  343. if location.Remove(dn) {
  344. vl.readonlyVolumes.Remove(vid, dn)
  345. vl.oversizedVolumes.Remove(vid, dn)
  346. if location.Length() < vl.rp.GetCopyCount() {
  347. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  348. return vl.removeFromWritable(vid)
  349. }
  350. }
  351. }
  352. return false
  353. }
  354. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid needle.VolumeId, isReadOnly bool) bool {
  355. vl.accessLock.Lock()
  356. defer vl.accessLock.Unlock()
  357. vInfo, err := dn.GetVolumesById(vid)
  358. if err != nil {
  359. return false
  360. }
  361. vl.vid2location[vid].Set(dn)
  362. if vInfo.ReadOnly || isReadOnly {
  363. return false
  364. }
  365. if vl.enoughCopies(vid) {
  366. return vl.setVolumeWritable(vid)
  367. }
  368. return false
  369. }
  370. func (vl *VolumeLayout) enoughCopies(vid needle.VolumeId) bool {
  371. locations := vl.vid2location[vid].Length()
  372. desired := vl.rp.GetCopyCount()
  373. return locations == desired || (vl.replicationAsMin && locations > desired)
  374. }
  375. func (vl *VolumeLayout) SetVolumeCapacityFull(vid needle.VolumeId) bool {
  376. vl.accessLock.Lock()
  377. defer vl.accessLock.Unlock()
  378. // glog.V(0).Infoln("Volume", vid, "reaches full capacity.")
  379. return vl.removeFromWritable(vid)
  380. }
  381. func (vl *VolumeLayout) removeFromCrowded(vid needle.VolumeId) {
  382. delete(vl.crowded, vid)
  383. }
  384. func (vl *VolumeLayout) setVolumeCrowded(vid needle.VolumeId) {
  385. if _, ok := vl.crowded[vid]; !ok {
  386. vl.crowded[vid] = struct{}{}
  387. glog.V(0).Infoln("Volume", vid, "becomes crowded")
  388. }
  389. }
  390. func (vl *VolumeLayout) SetVolumeCrowded(vid needle.VolumeId) {
  391. // since delete is guarded by accessLock.Lock(),
  392. // and is always called in sequential order,
  393. // RLock() should be safe enough
  394. vl.accessLock.RLock()
  395. defer vl.accessLock.RUnlock()
  396. for _, v := range vl.writables {
  397. if v == vid {
  398. vl.setVolumeCrowded(vid)
  399. break
  400. }
  401. }
  402. }
  403. func (vl *VolumeLayout) ToMap() map[string]interface{} {
  404. m := make(map[string]interface{})
  405. m["replication"] = vl.rp.String()
  406. m["ttl"] = vl.ttl.String()
  407. m["writables"] = vl.writables
  408. //m["locations"] = vl.vid2location
  409. return m
  410. }
  411. func (vl *VolumeLayout) Stats() *VolumeLayoutStats {
  412. vl.accessLock.RLock()
  413. defer vl.accessLock.RUnlock()
  414. ret := &VolumeLayoutStats{}
  415. freshThreshold := time.Now().Unix() - 60
  416. for vid, vll := range vl.vid2location {
  417. size, fileCount := vll.Stats(vid, freshThreshold)
  418. ret.FileCount += uint64(fileCount)
  419. ret.UsedSize += size
  420. if vl.readonlyVolumes.IsTrue(vid) {
  421. ret.TotalSize += size
  422. } else {
  423. ret.TotalSize += vl.volumeSizeLimit * uint64(vll.Length())
  424. }
  425. }
  426. return ret
  427. }