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.

553 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
6 years ago
6 years ago
6 years ago
6 years ago
  1. package topology
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  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. )
  27. type stateIndicator func(copyState) bool
  28. func ExistCopies() stateIndicator {
  29. return func(state copyState) bool { return state != noCopies }
  30. }
  31. func NoCopies() stateIndicator {
  32. return func(state copyState) bool { return state == noCopies }
  33. }
  34. type volumesBinaryState struct {
  35. rp *super_block.ReplicaPlacement
  36. name volumeState // the name for volume state (eg. "Readonly", "Oversized")
  37. indicator stateIndicator // indicate whether the volumes should be marked as `name`
  38. copyMap map[needle.VolumeId]*VolumeLocationList
  39. }
  40. func NewVolumesBinaryState(name volumeState, rp *super_block.ReplicaPlacement, indicator stateIndicator) *volumesBinaryState {
  41. return &volumesBinaryState{
  42. rp: rp,
  43. name: name,
  44. indicator: indicator,
  45. copyMap: make(map[needle.VolumeId]*VolumeLocationList),
  46. }
  47. }
  48. func (v *volumesBinaryState) Dump() (res []uint32) {
  49. for vid, list := range v.copyMap {
  50. if v.indicator(v.copyState(list)) {
  51. res = append(res, uint32(vid))
  52. }
  53. }
  54. return
  55. }
  56. func (v *volumesBinaryState) IsTrue(vid needle.VolumeId) bool {
  57. list, _ := v.copyMap[vid]
  58. return v.indicator(v.copyState(list))
  59. }
  60. func (v *volumesBinaryState) Add(vid needle.VolumeId, dn *DataNode) {
  61. list, _ := v.copyMap[vid]
  62. if list != nil {
  63. list.Set(dn)
  64. return
  65. }
  66. list = NewVolumeLocationList()
  67. list.Set(dn)
  68. v.copyMap[vid] = list
  69. }
  70. func (v *volumesBinaryState) Remove(vid needle.VolumeId, dn *DataNode) {
  71. list, _ := v.copyMap[vid]
  72. if list != nil {
  73. list.Remove(dn)
  74. if list.Length() == 0 {
  75. delete(v.copyMap, vid)
  76. }
  77. }
  78. }
  79. func (v *volumesBinaryState) copyState(list *VolumeLocationList) copyState {
  80. if list == nil {
  81. return noCopies
  82. }
  83. if list.Length() < v.rp.GetCopyCount() {
  84. return insufficientCopies
  85. }
  86. return enoughCopies
  87. }
  88. // mapping from volume to its locations, inverted from server to volume
  89. type VolumeLayout struct {
  90. growRequestCount int32
  91. rp *super_block.ReplicaPlacement
  92. ttl *needle.TTL
  93. diskType types.DiskType
  94. vid2location map[needle.VolumeId]*VolumeLocationList
  95. writables []needle.VolumeId // transient array of writable volume id
  96. crowded map[needle.VolumeId]struct{}
  97. readonlyVolumes *volumesBinaryState // readonly volumes
  98. oversizedVolumes *volumesBinaryState // oversized volumes
  99. vacuumedVolumes map[needle.VolumeId]time.Time
  100. volumeSizeLimit uint64
  101. replicationAsMin bool
  102. accessLock sync.RWMutex
  103. }
  104. type VolumeLayoutStats struct {
  105. TotalSize uint64
  106. UsedSize uint64
  107. FileCount uint64
  108. }
  109. func NewVolumeLayout(rp *super_block.ReplicaPlacement, ttl *needle.TTL, diskType types.DiskType, volumeSizeLimit uint64, replicationAsMin bool) *VolumeLayout {
  110. return &VolumeLayout{
  111. rp: rp,
  112. ttl: ttl,
  113. diskType: diskType,
  114. vid2location: make(map[needle.VolumeId]*VolumeLocationList),
  115. writables: *new([]needle.VolumeId),
  116. crowded: make(map[needle.VolumeId]struct{}),
  117. readonlyVolumes: NewVolumesBinaryState(readOnlyState, rp, ExistCopies()),
  118. oversizedVolumes: NewVolumesBinaryState(oversizedState, rp, ExistCopies()),
  119. vacuumedVolumes: make(map[needle.VolumeId]time.Time),
  120. volumeSizeLimit: volumeSizeLimit,
  121. replicationAsMin: replicationAsMin,
  122. }
  123. }
  124. func (vl *VolumeLayout) String() string {
  125. return fmt.Sprintf("rp:%v, ttl:%v, writables:%v, volumeSizeLimit:%v", vl.rp, vl.ttl, 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. if !vl.enoughCopies(vid) {
  190. glog.V(0).Infof("volume %d does not have enough copies", vid)
  191. }
  192. if !vl.isAllWritable(vid) {
  193. glog.V(0).Infof("volume %d are not all writable", vid)
  194. }
  195. glog.V(0).Infof("volume %d remove from writable", vid)
  196. vl.removeFromWritable(vid)
  197. }
  198. }
  199. func (vl *VolumeLayout) isAllWritable(vid needle.VolumeId) bool {
  200. for _, dn := range vl.vid2location[vid].list {
  201. if v, getError := dn.GetVolumesById(vid); getError == nil {
  202. if v.ReadOnly {
  203. return false
  204. }
  205. }
  206. }
  207. return true
  208. }
  209. func (vl *VolumeLayout) isOversized(v *storage.VolumeInfo) bool {
  210. return uint64(v.Size) >= vl.volumeSizeLimit
  211. }
  212. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  213. return !vl.isOversized(v) &&
  214. v.Version == needle.CurrentVersion &&
  215. !v.ReadOnly
  216. }
  217. func (vl *VolumeLayout) isEmpty() bool {
  218. vl.accessLock.RLock()
  219. defer vl.accessLock.RUnlock()
  220. return len(vl.vid2location) == 0
  221. }
  222. func (vl *VolumeLayout) Lookup(vid needle.VolumeId) []*DataNode {
  223. vl.accessLock.RLock()
  224. defer vl.accessLock.RUnlock()
  225. if location := vl.vid2location[vid]; location != nil {
  226. return location.list
  227. }
  228. return nil
  229. }
  230. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  231. vl.accessLock.RLock()
  232. defer vl.accessLock.RUnlock()
  233. for _, location := range vl.vid2location {
  234. nodes = append(nodes, location.list...)
  235. }
  236. return
  237. }
  238. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (vid needle.VolumeId, counter uint64, locationList *VolumeLocationList, shouldGrow bool, err error) {
  239. vl.accessLock.RLock()
  240. defer vl.accessLock.RUnlock()
  241. lenWriters := len(vl.writables)
  242. if lenWriters <= 0 {
  243. //glog.V(0).Infoln("No more writable volumes!")
  244. shouldGrow = true
  245. return 0, 0, nil, shouldGrow, errors.New("No more writable volumes!")
  246. }
  247. if option.DataCenter == "" && option.Rack == "" && option.DataNode == "" {
  248. vid := vl.writables[rand.Intn(lenWriters)]
  249. locationList = vl.vid2location[vid]
  250. if locationList != nil && locationList.Length() > 0 {
  251. // check whether picked file is close to full
  252. dn := locationList.Head()
  253. info, _ := dn.GetVolumesById(vid)
  254. if float64(info.Size) > float64(vl.volumeSizeLimit)*option.Threshold() {
  255. shouldGrow = true
  256. }
  257. return vid, count, locationList, shouldGrow, nil
  258. }
  259. return 0, 0, nil, shouldGrow, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  260. }
  261. // clone vl.writables
  262. writables := make([]needle.VolumeId, len(vl.writables))
  263. copy(writables, vl.writables)
  264. // randomize the writables
  265. rand.Shuffle(len(writables), func(i, j int) {
  266. writables[i], writables[j] = writables[j], writables[i]
  267. })
  268. for _, writableVolumeId := range writables {
  269. volumeLocationList := vl.vid2location[writableVolumeId]
  270. for _, dn := range volumeLocationList.list {
  271. if option.DataCenter != "" && dn.GetDataCenter().Id() != NodeId(option.DataCenter) {
  272. continue
  273. }
  274. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  275. continue
  276. }
  277. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  278. continue
  279. }
  280. vid, locationList = writableVolumeId, volumeLocationList.Copy()
  281. // check whether picked file is close to full
  282. info, _ := dn.GetVolumesById(writableVolumeId)
  283. if float64(info.Size) > float64(vl.volumeSizeLimit)*option.Threshold() {
  284. shouldGrow = true
  285. }
  286. counter = count
  287. return
  288. }
  289. }
  290. return vid, count, locationList, shouldGrow, fmt.Errorf("No writable volumes in DataCenter:%v Rack:%v DataNode:%v", option.DataCenter, option.Rack, option.DataNode)
  291. }
  292. func (vl *VolumeLayout) HasGrowRequest() bool {
  293. return atomic.LoadInt32(&vl.growRequestCount) > 0
  294. }
  295. func (vl *VolumeLayout) AddGrowRequest() {
  296. atomic.AddInt32(&vl.growRequestCount, 1)
  297. }
  298. func (vl *VolumeLayout) DoneGrowRequest() {
  299. atomic.AddInt32(&vl.growRequestCount, -1)
  300. }
  301. func (vl *VolumeLayout) ShouldGrowVolumes(option *VolumeGrowOption) bool {
  302. active, crowded := vl.GetActiveVolumeCount(option)
  303. //glog.V(0).Infof("active volume: %d, high usage volume: %d\n", active, high)
  304. return active <= crowded
  305. }
  306. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) (active, crowded int) {
  307. vl.accessLock.RLock()
  308. defer vl.accessLock.RUnlock()
  309. if option.DataCenter == "" {
  310. return len(vl.writables), len(vl.crowded)
  311. }
  312. for _, v := range vl.writables {
  313. for _, dn := range vl.vid2location[v].list {
  314. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  315. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  316. continue
  317. }
  318. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  319. continue
  320. }
  321. active++
  322. info, _ := dn.GetVolumesById(v)
  323. if float64(info.Size) > float64(vl.volumeSizeLimit)*option.Threshold() {
  324. crowded++
  325. }
  326. }
  327. }
  328. }
  329. return
  330. }
  331. func (vl *VolumeLayout) removeFromWritable(vid needle.VolumeId) bool {
  332. toDeleteIndex := -1
  333. for k, id := range vl.writables {
  334. if id == vid {
  335. toDeleteIndex = k
  336. break
  337. }
  338. }
  339. if toDeleteIndex >= 0 {
  340. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  341. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  342. vl.removeFromCrowded(vid)
  343. return true
  344. }
  345. return false
  346. }
  347. func (vl *VolumeLayout) setVolumeWritable(vid needle.VolumeId) bool {
  348. for _, v := range vl.writables {
  349. if v == vid {
  350. return false
  351. }
  352. }
  353. glog.V(0).Infoln("Volume", vid, "becomes writable")
  354. vl.writables = append(vl.writables, vid)
  355. return true
  356. }
  357. func (vl *VolumeLayout) SetVolumeReadOnly(dn *DataNode, vid needle.VolumeId) bool {
  358. vl.accessLock.Lock()
  359. defer vl.accessLock.Unlock()
  360. if _, ok := vl.vid2location[vid]; ok {
  361. vl.readonlyVolumes.Add(vid, dn)
  362. return vl.removeFromWritable(vid)
  363. }
  364. return true
  365. }
  366. func (vl *VolumeLayout) SetVolumeWritable(dn *DataNode, vid needle.VolumeId) bool {
  367. vl.accessLock.Lock()
  368. defer vl.accessLock.Unlock()
  369. if _, ok := vl.vid2location[vid]; ok {
  370. vl.readonlyVolumes.Remove(vid, dn)
  371. }
  372. if vl.enoughCopies(vid) {
  373. return vl.setVolumeWritable(vid)
  374. }
  375. return false
  376. }
  377. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid needle.VolumeId) bool {
  378. vl.accessLock.Lock()
  379. defer vl.accessLock.Unlock()
  380. if location, ok := vl.vid2location[vid]; ok {
  381. if location.Remove(dn) {
  382. vl.readonlyVolumes.Remove(vid, dn)
  383. vl.oversizedVolumes.Remove(vid, dn)
  384. if location.Length() < vl.rp.GetCopyCount() {
  385. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  386. return vl.removeFromWritable(vid)
  387. }
  388. }
  389. }
  390. return false
  391. }
  392. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid needle.VolumeId, isReadOnly, isFullCapacity bool) bool {
  393. vl.accessLock.Lock()
  394. defer vl.accessLock.Unlock()
  395. vInfo, err := dn.GetVolumesById(vid)
  396. if err != nil {
  397. return false
  398. }
  399. vl.vid2location[vid].Set(dn)
  400. if vInfo.ReadOnly || isReadOnly || isFullCapacity {
  401. return false
  402. }
  403. if vl.enoughCopies(vid) {
  404. return vl.setVolumeWritable(vid)
  405. }
  406. return false
  407. }
  408. func (vl *VolumeLayout) enoughCopies(vid needle.VolumeId) bool {
  409. locations := vl.vid2location[vid].Length()
  410. desired := vl.rp.GetCopyCount()
  411. return locations == desired || (vl.replicationAsMin && locations > desired)
  412. }
  413. func (vl *VolumeLayout) SetVolumeCapacityFull(vid needle.VolumeId) bool {
  414. vl.accessLock.Lock()
  415. defer vl.accessLock.Unlock()
  416. wasWritable := vl.removeFromWritable(vid)
  417. if wasWritable {
  418. glog.V(0).Infof("Volume %d reaches full capacity.", vid)
  419. }
  420. return wasWritable
  421. }
  422. func (vl *VolumeLayout) removeFromCrowded(vid needle.VolumeId) {
  423. delete(vl.crowded, vid)
  424. }
  425. func (vl *VolumeLayout) setVolumeCrowded(vid needle.VolumeId) {
  426. if _, ok := vl.crowded[vid]; !ok {
  427. vl.crowded[vid] = struct{}{}
  428. glog.V(0).Infoln("Volume", vid, "becomes crowded")
  429. }
  430. }
  431. func (vl *VolumeLayout) SetVolumeCrowded(vid needle.VolumeId) {
  432. // since delete is guarded by accessLock.Lock(),
  433. // and is always called in sequential order,
  434. // RLock() should be safe enough
  435. vl.accessLock.RLock()
  436. defer vl.accessLock.RUnlock()
  437. for _, v := range vl.writables {
  438. if v == vid {
  439. vl.setVolumeCrowded(vid)
  440. break
  441. }
  442. }
  443. }
  444. type VolumeLayoutInfo struct {
  445. Replication string `json:"replication"`
  446. TTL string `json:"ttl"`
  447. Writables []needle.VolumeId `json:"writables"`
  448. Collection string `json:"collection"`
  449. DiskType string `json:"diskType"`
  450. }
  451. func (vl *VolumeLayout) ToInfo() (info VolumeLayoutInfo) {
  452. info.Replication = vl.rp.String()
  453. info.TTL = vl.ttl.String()
  454. info.Writables = vl.writables
  455. info.DiskType = vl.diskType.ReadableString()
  456. //m["locations"] = vl.vid2location
  457. return
  458. }
  459. func (vl *VolumeLayout) Stats() *VolumeLayoutStats {
  460. vl.accessLock.RLock()
  461. defer vl.accessLock.RUnlock()
  462. ret := &VolumeLayoutStats{}
  463. freshThreshold := time.Now().Unix() - 60
  464. for vid, vll := range vl.vid2location {
  465. size, fileCount := vll.Stats(vid, freshThreshold)
  466. ret.FileCount += uint64(fileCount)
  467. ret.UsedSize += size * uint64(vll.Length())
  468. if vl.readonlyVolumes.IsTrue(vid) {
  469. ret.TotalSize += size * uint64(vll.Length())
  470. } else {
  471. ret.TotalSize += vl.volumeSizeLimit * uint64(vll.Length())
  472. }
  473. }
  474. return ret
  475. }