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.

511 lines
13 KiB

6 years ago
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
6 years ago
6 years ago
3 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. return fmt.Sprintf("rp:%v, ttl:%v, writables:%v, volumeSizeLimit:%v", vl.rp, vl.ttl, vl.writables, vl.volumeSizeLimit)
  124. }
  125. func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  126. vl.accessLock.Lock()
  127. defer vl.accessLock.Unlock()
  128. defer vl.rememberOversizedVolume(v, dn)
  129. if _, ok := vl.vid2location[v.Id]; !ok {
  130. vl.vid2location[v.Id] = NewVolumeLocationList()
  131. }
  132. vl.vid2location[v.Id].Set(dn)
  133. // 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())
  134. for _, dn := range vl.vid2location[v.Id].list {
  135. if vInfo, err := dn.GetVolumesById(v.Id); err == nil {
  136. if vInfo.ReadOnly {
  137. glog.V(1).Infof("vid %d removed from writable", v.Id)
  138. vl.removeFromWritable(v.Id)
  139. vl.readonlyVolumes.Add(v.Id, dn)
  140. return
  141. } else {
  142. vl.readonlyVolumes.Remove(v.Id, dn)
  143. }
  144. } else {
  145. glog.V(1).Infof("vid %d removed from writable", v.Id)
  146. vl.removeFromWritable(v.Id)
  147. vl.readonlyVolumes.Remove(v.Id, dn)
  148. return
  149. }
  150. }
  151. }
  152. func (vl *VolumeLayout) rememberOversizedVolume(v *storage.VolumeInfo, dn *DataNode) {
  153. if vl.isOversized(v) {
  154. vl.oversizedVolumes.Add(v.Id, dn)
  155. } else {
  156. vl.oversizedVolumes.Remove(v.Id, dn)
  157. }
  158. }
  159. func (vl *VolumeLayout) UnRegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  160. vl.accessLock.Lock()
  161. defer vl.accessLock.Unlock()
  162. // remove from vid2location map
  163. location, ok := vl.vid2location[v.Id]
  164. if !ok {
  165. return
  166. }
  167. if location.Remove(dn) {
  168. vl.readonlyVolumes.Remove(v.Id, dn)
  169. vl.oversizedVolumes.Remove(v.Id, dn)
  170. vl.ensureCorrectWritables(v.Id)
  171. if location.Length() == 0 {
  172. delete(vl.vid2location, v.Id)
  173. }
  174. }
  175. }
  176. func (vl *VolumeLayout) EnsureCorrectWritables(v *storage.VolumeInfo) {
  177. vl.accessLock.Lock()
  178. defer vl.accessLock.Unlock()
  179. vl.ensureCorrectWritables(v.Id)
  180. }
  181. func (vl *VolumeLayout) ensureCorrectWritables(vid needle.VolumeId) {
  182. if vl.enoughCopies(vid) && vl.isAllWritable(vid) {
  183. if !vl.oversizedVolumes.IsTrue(vid) {
  184. vl.setVolumeWritable(vid)
  185. }
  186. } else {
  187. if !vl.enoughCopies(vid) {
  188. glog.V(0).Infof("volume %d does not have enough copies", vid)
  189. }
  190. if !vl.isAllWritable(vid) {
  191. glog.V(0).Infof("volume %d are not all writable", vid)
  192. }
  193. glog.V(0).Infof("volume %d remove from writable", vid)
  194. vl.removeFromWritable(vid)
  195. }
  196. }
  197. func (vl *VolumeLayout) isAllWritable(vid needle.VolumeId) bool {
  198. for _, dn := range vl.vid2location[vid].list {
  199. if v, getError := dn.GetVolumesById(vid); getError == nil {
  200. if v.ReadOnly {
  201. return false
  202. }
  203. }
  204. }
  205. return true
  206. }
  207. func (vl *VolumeLayout) isOversized(v *storage.VolumeInfo) bool {
  208. return uint64(v.Size) >= vl.volumeSizeLimit
  209. }
  210. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  211. return !vl.isOversized(v) &&
  212. v.Version == needle.CurrentVersion &&
  213. !v.ReadOnly
  214. }
  215. func (vl *VolumeLayout) isEmpty() bool {
  216. vl.accessLock.RLock()
  217. defer vl.accessLock.RUnlock()
  218. return len(vl.vid2location) == 0
  219. }
  220. func (vl *VolumeLayout) Lookup(vid needle.VolumeId) []*DataNode {
  221. vl.accessLock.RLock()
  222. defer vl.accessLock.RUnlock()
  223. if location := vl.vid2location[vid]; location != nil {
  224. return location.list
  225. }
  226. return nil
  227. }
  228. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  229. vl.accessLock.RLock()
  230. defer vl.accessLock.RUnlock()
  231. for _, location := range vl.vid2location {
  232. nodes = append(nodes, location.list...)
  233. }
  234. return
  235. }
  236. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (*needle.VolumeId, uint64, *VolumeLocationList, error) {
  237. vl.accessLock.RLock()
  238. defer vl.accessLock.RUnlock()
  239. lenWriters := len(vl.writables)
  240. if lenWriters <= 0 {
  241. //glog.V(0).Infoln("No more writable volumes!")
  242. return nil, 0, nil, errors.New("No more writable volumes!")
  243. }
  244. if option.DataCenter == "" && option.Rack == "" && option.DataNode == "" {
  245. vid := vl.writables[rand.Intn(lenWriters)]
  246. locationList := vl.vid2location[vid]
  247. if locationList != nil {
  248. return &vid, count, locationList, nil
  249. }
  250. return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  251. }
  252. var vid needle.VolumeId
  253. var locationList *VolumeLocationList
  254. counter := 0
  255. for _, v := range vl.writables {
  256. volumeLocationList := vl.vid2location[v]
  257. for _, dn := range volumeLocationList.list {
  258. if option.DataCenter != "" && dn.GetDataCenter().Id() != NodeId(option.DataCenter) {
  259. continue
  260. }
  261. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  262. continue
  263. }
  264. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  265. continue
  266. }
  267. counter++
  268. if rand.Intn(counter) < 1 {
  269. vid, locationList = v, volumeLocationList.Copy()
  270. }
  271. }
  272. }
  273. return &vid, count, locationList, nil
  274. }
  275. func (vl *VolumeLayout) HasGrowRequest() bool {
  276. if vl.growRequestCount > 0 && vl.growRequestTime.Add(time.Minute).After(time.Now()) {
  277. return true
  278. }
  279. return false
  280. }
  281. func (vl *VolumeLayout) AddGrowRequest() {
  282. vl.growRequestTime = time.Now()
  283. vl.growRequestCount++
  284. }
  285. func (vl *VolumeLayout) DoneGrowRequest() {
  286. vl.growRequestTime = time.Unix(0, 0)
  287. vl.growRequestCount = 0
  288. }
  289. func (vl *VolumeLayout) ShouldGrowVolumes(option *VolumeGrowOption) bool {
  290. active, crowded := vl.GetActiveVolumeCount(option)
  291. //glog.V(0).Infof("active volume: %d, high usage volume: %d\n", active, high)
  292. return active <= crowded
  293. }
  294. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) (active, crowded int) {
  295. vl.accessLock.RLock()
  296. defer vl.accessLock.RUnlock()
  297. if option.DataCenter == "" {
  298. return len(vl.writables), len(vl.crowded)
  299. }
  300. for _, v := range vl.writables {
  301. for _, dn := range vl.vid2location[v].list {
  302. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  303. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  304. continue
  305. }
  306. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  307. continue
  308. }
  309. active++
  310. info, _ := dn.GetVolumesById(v)
  311. if float64(info.Size) > float64(vl.volumeSizeLimit)*option.Threshold() {
  312. crowded++
  313. }
  314. }
  315. }
  316. }
  317. return
  318. }
  319. func (vl *VolumeLayout) removeFromWritable(vid needle.VolumeId) bool {
  320. toDeleteIndex := -1
  321. for k, id := range vl.writables {
  322. if id == vid {
  323. toDeleteIndex = k
  324. break
  325. }
  326. }
  327. if toDeleteIndex >= 0 {
  328. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  329. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  330. vl.removeFromCrowded(vid)
  331. return true
  332. }
  333. return false
  334. }
  335. func (vl *VolumeLayout) setVolumeWritable(vid needle.VolumeId) bool {
  336. for _, v := range vl.writables {
  337. if v == vid {
  338. return false
  339. }
  340. }
  341. glog.V(0).Infoln("Volume", vid, "becomes writable")
  342. vl.writables = append(vl.writables, vid)
  343. return true
  344. }
  345. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid needle.VolumeId) bool {
  346. vl.accessLock.Lock()
  347. defer vl.accessLock.Unlock()
  348. if location, ok := vl.vid2location[vid]; ok {
  349. if location.Remove(dn) {
  350. vl.readonlyVolumes.Remove(vid, dn)
  351. vl.oversizedVolumes.Remove(vid, dn)
  352. if location.Length() < vl.rp.GetCopyCount() {
  353. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  354. return vl.removeFromWritable(vid)
  355. }
  356. }
  357. }
  358. return false
  359. }
  360. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid needle.VolumeId, isReadOnly bool) bool {
  361. vl.accessLock.Lock()
  362. defer vl.accessLock.Unlock()
  363. vInfo, err := dn.GetVolumesById(vid)
  364. if err != nil {
  365. return false
  366. }
  367. vl.vid2location[vid].Set(dn)
  368. if vInfo.ReadOnly || isReadOnly {
  369. return false
  370. }
  371. if vl.enoughCopies(vid) {
  372. return vl.setVolumeWritable(vid)
  373. }
  374. return false
  375. }
  376. func (vl *VolumeLayout) enoughCopies(vid needle.VolumeId) bool {
  377. locations := vl.vid2location[vid].Length()
  378. desired := vl.rp.GetCopyCount()
  379. return locations == desired || (vl.replicationAsMin && locations > desired)
  380. }
  381. func (vl *VolumeLayout) SetVolumeCapacityFull(vid needle.VolumeId) bool {
  382. vl.accessLock.Lock()
  383. defer vl.accessLock.Unlock()
  384. wasWritable := vl.removeFromWritable(vid)
  385. if wasWritable {
  386. glog.V(0).Infof("Volume %d reaches full capacity.", vid)
  387. }
  388. return wasWritable
  389. }
  390. func (vl *VolumeLayout) removeFromCrowded(vid needle.VolumeId) {
  391. delete(vl.crowded, vid)
  392. }
  393. func (vl *VolumeLayout) setVolumeCrowded(vid needle.VolumeId) {
  394. if _, ok := vl.crowded[vid]; !ok {
  395. vl.crowded[vid] = struct{}{}
  396. glog.V(0).Infoln("Volume", vid, "becomes crowded")
  397. }
  398. }
  399. func (vl *VolumeLayout) SetVolumeCrowded(vid needle.VolumeId) {
  400. // since delete is guarded by accessLock.Lock(),
  401. // and is always called in sequential order,
  402. // RLock() should be safe enough
  403. vl.accessLock.RLock()
  404. defer vl.accessLock.RUnlock()
  405. for _, v := range vl.writables {
  406. if v == vid {
  407. vl.setVolumeCrowded(vid)
  408. break
  409. }
  410. }
  411. }
  412. type VolumeLayoutInfo struct {
  413. Replication string `json:"replication"`
  414. TTL string `json:"ttl"`
  415. Writables []needle.VolumeId `json:"writables"`
  416. Collection string `json:"collection"`
  417. }
  418. func (vl *VolumeLayout) ToInfo() (info VolumeLayoutInfo) {
  419. info.Replication = vl.rp.String()
  420. info.TTL = vl.ttl.String()
  421. info.Writables = vl.writables
  422. //m["locations"] = vl.vid2location
  423. return
  424. }
  425. func (vl *VolumeLayout) Stats() *VolumeLayoutStats {
  426. vl.accessLock.RLock()
  427. defer vl.accessLock.RUnlock()
  428. ret := &VolumeLayoutStats{}
  429. freshThreshold := time.Now().Unix() - 60
  430. for vid, vll := range vl.vid2location {
  431. size, fileCount := vll.Stats(vid, freshThreshold)
  432. ret.FileCount += uint64(fileCount)
  433. ret.UsedSize += size * uint64(vll.Length())
  434. if vl.readonlyVolumes.IsTrue(vid) {
  435. ret.TotalSize += size * uint64(vll.Length())
  436. } else {
  437. ret.TotalSize += vl.volumeSizeLimit * uint64(vll.Length())
  438. }
  439. }
  440. return ret
  441. }