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.

552 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. return
  287. }
  288. }
  289. return vid, count, locationList, shouldGrow, fmt.Errorf("No writable volumes in DataCenter:%v Rack:%v DataNode:%v", option.DataCenter, option.Rack, option.DataNode)
  290. }
  291. func (vl *VolumeLayout) HasGrowRequest() bool {
  292. return atomic.LoadInt32(&vl.growRequestCount) > 0
  293. }
  294. func (vl *VolumeLayout) AddGrowRequest() {
  295. atomic.AddInt32(&vl.growRequestCount, 1)
  296. }
  297. func (vl *VolumeLayout) DoneGrowRequest() {
  298. atomic.AddInt32(&vl.growRequestCount, -1)
  299. }
  300. func (vl *VolumeLayout) ShouldGrowVolumes(option *VolumeGrowOption) bool {
  301. active, crowded := vl.GetActiveVolumeCount(option)
  302. //glog.V(0).Infof("active volume: %d, high usage volume: %d\n", active, high)
  303. return active <= crowded
  304. }
  305. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) (active, crowded int) {
  306. vl.accessLock.RLock()
  307. defer vl.accessLock.RUnlock()
  308. if option.DataCenter == "" {
  309. return len(vl.writables), len(vl.crowded)
  310. }
  311. for _, v := range vl.writables {
  312. for _, dn := range vl.vid2location[v].list {
  313. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  314. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  315. continue
  316. }
  317. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  318. continue
  319. }
  320. active++
  321. info, _ := dn.GetVolumesById(v)
  322. if float64(info.Size) > float64(vl.volumeSizeLimit)*option.Threshold() {
  323. crowded++
  324. }
  325. }
  326. }
  327. }
  328. return
  329. }
  330. func (vl *VolumeLayout) removeFromWritable(vid needle.VolumeId) bool {
  331. toDeleteIndex := -1
  332. for k, id := range vl.writables {
  333. if id == vid {
  334. toDeleteIndex = k
  335. break
  336. }
  337. }
  338. if toDeleteIndex >= 0 {
  339. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  340. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  341. vl.removeFromCrowded(vid)
  342. return true
  343. }
  344. return false
  345. }
  346. func (vl *VolumeLayout) setVolumeWritable(vid needle.VolumeId) bool {
  347. for _, v := range vl.writables {
  348. if v == vid {
  349. return false
  350. }
  351. }
  352. glog.V(0).Infoln("Volume", vid, "becomes writable")
  353. vl.writables = append(vl.writables, vid)
  354. return true
  355. }
  356. func (vl *VolumeLayout) SetVolumeReadOnly(dn *DataNode, vid needle.VolumeId) bool {
  357. vl.accessLock.Lock()
  358. defer vl.accessLock.Unlock()
  359. if _, ok := vl.vid2location[vid]; ok {
  360. vl.readonlyVolumes.Add(vid, dn)
  361. return vl.removeFromWritable(vid)
  362. }
  363. return true
  364. }
  365. func (vl *VolumeLayout) SetVolumeWritable(dn *DataNode, vid needle.VolumeId) bool {
  366. vl.accessLock.Lock()
  367. defer vl.accessLock.Unlock()
  368. if _, ok := vl.vid2location[vid]; ok {
  369. vl.readonlyVolumes.Remove(vid, dn)
  370. }
  371. if vl.enoughCopies(vid) {
  372. return vl.setVolumeWritable(vid)
  373. }
  374. return false
  375. }
  376. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid needle.VolumeId) bool {
  377. vl.accessLock.Lock()
  378. defer vl.accessLock.Unlock()
  379. if location, ok := vl.vid2location[vid]; ok {
  380. if location.Remove(dn) {
  381. vl.readonlyVolumes.Remove(vid, dn)
  382. vl.oversizedVolumes.Remove(vid, dn)
  383. if location.Length() < vl.rp.GetCopyCount() {
  384. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  385. return vl.removeFromWritable(vid)
  386. }
  387. }
  388. }
  389. return false
  390. }
  391. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid needle.VolumeId, isReadOnly, isFullCapacity bool) bool {
  392. vl.accessLock.Lock()
  393. defer vl.accessLock.Unlock()
  394. vInfo, err := dn.GetVolumesById(vid)
  395. if err != nil {
  396. return false
  397. }
  398. vl.vid2location[vid].Set(dn)
  399. if vInfo.ReadOnly || isReadOnly || isFullCapacity {
  400. return false
  401. }
  402. if vl.enoughCopies(vid) {
  403. return vl.setVolumeWritable(vid)
  404. }
  405. return false
  406. }
  407. func (vl *VolumeLayout) enoughCopies(vid needle.VolumeId) bool {
  408. locations := vl.vid2location[vid].Length()
  409. desired := vl.rp.GetCopyCount()
  410. return locations == desired || (vl.replicationAsMin && locations > desired)
  411. }
  412. func (vl *VolumeLayout) SetVolumeCapacityFull(vid needle.VolumeId) bool {
  413. vl.accessLock.Lock()
  414. defer vl.accessLock.Unlock()
  415. wasWritable := vl.removeFromWritable(vid)
  416. if wasWritable {
  417. glog.V(0).Infof("Volume %d reaches full capacity.", vid)
  418. }
  419. return wasWritable
  420. }
  421. func (vl *VolumeLayout) removeFromCrowded(vid needle.VolumeId) {
  422. delete(vl.crowded, vid)
  423. }
  424. func (vl *VolumeLayout) setVolumeCrowded(vid needle.VolumeId) {
  425. if _, ok := vl.crowded[vid]; !ok {
  426. vl.crowded[vid] = struct{}{}
  427. glog.V(0).Infoln("Volume", vid, "becomes crowded")
  428. }
  429. }
  430. func (vl *VolumeLayout) SetVolumeCrowded(vid needle.VolumeId) {
  431. // since delete is guarded by accessLock.Lock(),
  432. // and is always called in sequential order,
  433. // RLock() should be safe enough
  434. vl.accessLock.RLock()
  435. defer vl.accessLock.RUnlock()
  436. for _, v := range vl.writables {
  437. if v == vid {
  438. vl.setVolumeCrowded(vid)
  439. break
  440. }
  441. }
  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) Stats() *VolumeLayoutStats {
  459. vl.accessLock.RLock()
  460. defer vl.accessLock.RUnlock()
  461. ret := &VolumeLayoutStats{}
  462. freshThreshold := time.Now().Unix() - 60
  463. for vid, vll := range vl.vid2location {
  464. size, fileCount := vll.Stats(vid, freshThreshold)
  465. ret.FileCount += uint64(fileCount)
  466. ret.UsedSize += size * uint64(vll.Length())
  467. if vl.readonlyVolumes.IsTrue(vid) {
  468. ret.TotalSize += size * uint64(vll.Length())
  469. } else {
  470. ret.TotalSize += vl.volumeSizeLimit * uint64(vll.Length())
  471. }
  472. }
  473. return ret
  474. }