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.

563 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
6 years ago
6 years ago
6 years ago
6 years ago
  1. package topology
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/stats"
  6. "math/rand"
  7. "sync"
  8. "sync/atomic"
  9. "time"
  10. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  11. "github.com/seaweedfs/seaweedfs/weed/glog"
  12. "github.com/seaweedfs/seaweedfs/weed/storage"
  13. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  14. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  15. )
  16. type copyState int
  17. const (
  18. noCopies copyState = 0 + iota
  19. insufficientCopies
  20. enoughCopies
  21. )
  22. type volumeState string
  23. const (
  24. readOnlyState volumeState = "ReadOnly"
  25. oversizedState = "Oversized"
  26. crowdedState = "Crowded"
  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. growRequestCount int32
  92. rp *super_block.ReplicaPlacement
  93. ttl *needle.TTL
  94. diskType types.DiskType
  95. vid2location map[needle.VolumeId]*VolumeLocationList
  96. writables []needle.VolumeId // transient array of writable volume id
  97. crowded map[needle.VolumeId]struct{}
  98. readonlyVolumes *volumesBinaryState // readonly volumes
  99. oversizedVolumes *volumesBinaryState // oversized volumes
  100. vacuumedVolumes map[needle.VolumeId]time.Time
  101. volumeSizeLimit uint64
  102. replicationAsMin bool
  103. accessLock sync.RWMutex
  104. }
  105. type VolumeLayoutStats struct {
  106. TotalSize uint64
  107. UsedSize uint64
  108. FileCount uint64
  109. }
  110. func NewVolumeLayout(rp *super_block.ReplicaPlacement, ttl *needle.TTL, diskType types.DiskType, volumeSizeLimit uint64, replicationAsMin bool) *VolumeLayout {
  111. return &VolumeLayout{
  112. rp: rp,
  113. ttl: ttl,
  114. diskType: diskType,
  115. vid2location: make(map[needle.VolumeId]*VolumeLocationList),
  116. writables: *new([]needle.VolumeId),
  117. crowded: make(map[needle.VolumeId]struct{}),
  118. readonlyVolumes: NewVolumesBinaryState(readOnlyState, rp, ExistCopies()),
  119. oversizedVolumes: NewVolumesBinaryState(oversizedState, rp, ExistCopies()),
  120. vacuumedVolumes: make(map[needle.VolumeId]time.Time),
  121. volumeSizeLimit: volumeSizeLimit,
  122. replicationAsMin: replicationAsMin,
  123. }
  124. }
  125. func (vl *VolumeLayout) String() string {
  126. return fmt.Sprintf("rp:%v, ttl:%v, writables:%v, volumeSizeLimit:%v", vl.rp, vl.ttl, vl.writables, vl.volumeSizeLimit)
  127. }
  128. func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  129. vl.accessLock.Lock()
  130. defer vl.accessLock.Unlock()
  131. defer vl.rememberOversizedVolume(v, dn)
  132. if _, ok := vl.vid2location[v.Id]; !ok {
  133. vl.vid2location[v.Id] = NewVolumeLocationList()
  134. }
  135. vl.vid2location[v.Id].Set(dn)
  136. // 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())
  137. for _, dn := range vl.vid2location[v.Id].list {
  138. if vInfo, err := dn.GetVolumesById(v.Id); err == nil {
  139. if vInfo.ReadOnly {
  140. glog.V(1).Infof("vid %d removed from writable", v.Id)
  141. vl.removeFromWritable(v.Id)
  142. vl.readonlyVolumes.Add(v.Id, dn)
  143. return
  144. } else {
  145. vl.readonlyVolumes.Remove(v.Id, dn)
  146. }
  147. } else {
  148. glog.V(1).Infof("vid %d removed from writable", v.Id)
  149. vl.removeFromWritable(v.Id)
  150. vl.readonlyVolumes.Remove(v.Id, dn)
  151. return
  152. }
  153. }
  154. }
  155. func (vl *VolumeLayout) rememberOversizedVolume(v *storage.VolumeInfo, dn *DataNode) {
  156. if vl.isOversized(v) {
  157. vl.oversizedVolumes.Add(v.Id, dn)
  158. } else {
  159. vl.oversizedVolumes.Remove(v.Id, dn)
  160. }
  161. }
  162. func (vl *VolumeLayout) UnRegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  163. vl.accessLock.Lock()
  164. defer vl.accessLock.Unlock()
  165. // remove from vid2location map
  166. location, ok := vl.vid2location[v.Id]
  167. if !ok {
  168. return
  169. }
  170. if location.Remove(dn) {
  171. vl.readonlyVolumes.Remove(v.Id, dn)
  172. vl.oversizedVolumes.Remove(v.Id, dn)
  173. vl.ensureCorrectWritables(v.Id)
  174. if location.Length() == 0 {
  175. delete(vl.vid2location, v.Id)
  176. }
  177. }
  178. }
  179. func (vl *VolumeLayout) EnsureCorrectWritables(v *storage.VolumeInfo) {
  180. vl.accessLock.Lock()
  181. defer vl.accessLock.Unlock()
  182. vl.ensureCorrectWritables(v.Id)
  183. }
  184. func (vl *VolumeLayout) ensureCorrectWritables(vid needle.VolumeId) {
  185. if vl.enoughCopies(vid) && vl.isAllWritable(vid) {
  186. if !vl.oversizedVolumes.IsTrue(vid) {
  187. vl.setVolumeWritable(vid)
  188. }
  189. } else {
  190. if !vl.enoughCopies(vid) {
  191. glog.V(0).Infof("volume %d does not have enough copies", vid)
  192. }
  193. if !vl.isAllWritable(vid) {
  194. glog.V(0).Infof("volume %d are not all writable", vid)
  195. }
  196. glog.V(0).Infof("volume %d remove from writable", vid)
  197. vl.removeFromWritable(vid)
  198. }
  199. }
  200. func (vl *VolumeLayout) isAllWritable(vid needle.VolumeId) bool {
  201. if location, ok := vl.vid2location[vid]; ok {
  202. for _, dn := range location.list {
  203. if v, getError := dn.GetVolumesById(vid); getError == nil {
  204. if v.ReadOnly {
  205. return false
  206. }
  207. }
  208. }
  209. } else {
  210. return false
  211. }
  212. return true
  213. }
  214. func (vl *VolumeLayout) isOversized(v *storage.VolumeInfo) bool {
  215. return uint64(v.Size) >= vl.volumeSizeLimit
  216. }
  217. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  218. return !vl.isOversized(v) &&
  219. v.Version == needle.CurrentVersion &&
  220. !v.ReadOnly
  221. }
  222. func (vl *VolumeLayout) isEmpty() bool {
  223. vl.accessLock.RLock()
  224. defer vl.accessLock.RUnlock()
  225. return len(vl.vid2location) == 0
  226. }
  227. func (vl *VolumeLayout) Lookup(vid needle.VolumeId) []*DataNode {
  228. vl.accessLock.RLock()
  229. defer vl.accessLock.RUnlock()
  230. if location := vl.vid2location[vid]; location != nil {
  231. return location.list
  232. }
  233. return nil
  234. }
  235. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  236. vl.accessLock.RLock()
  237. defer vl.accessLock.RUnlock()
  238. for _, location := range vl.vid2location {
  239. nodes = append(nodes, location.list...)
  240. }
  241. return
  242. }
  243. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (vid needle.VolumeId, counter uint64, locationList *VolumeLocationList, shouldGrow bool, err error) {
  244. vl.accessLock.RLock()
  245. defer vl.accessLock.RUnlock()
  246. lenWriters := len(vl.writables)
  247. if lenWriters <= 0 {
  248. //glog.V(0).Infoln("No more writable volumes!")
  249. shouldGrow = true
  250. return 0, 0, nil, shouldGrow, errors.New("No more writable volumes!")
  251. }
  252. if option.DataCenter == "" && option.Rack == "" && option.DataNode == "" {
  253. vid := vl.writables[rand.Intn(lenWriters)]
  254. locationList = vl.vid2location[vid]
  255. if locationList != nil && locationList.Length() > 0 {
  256. // check whether picked file is close to full
  257. dn := locationList.Head()
  258. info, _ := dn.GetVolumesById(vid)
  259. if float64(info.Size) > float64(vl.volumeSizeLimit)*option.Threshold() {
  260. shouldGrow = true
  261. }
  262. return vid, count, locationList.Copy(), shouldGrow, nil
  263. }
  264. return 0, 0, nil, shouldGrow, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  265. }
  266. // clone vl.writables
  267. writables := make([]needle.VolumeId, len(vl.writables))
  268. copy(writables, vl.writables)
  269. // randomize the writables
  270. rand.Shuffle(len(writables), func(i, j int) {
  271. writables[i], writables[j] = writables[j], writables[i]
  272. })
  273. for _, writableVolumeId := range writables {
  274. volumeLocationList := vl.vid2location[writableVolumeId]
  275. for _, dn := range volumeLocationList.list {
  276. if option.DataCenter != "" && dn.GetDataCenter().Id() != NodeId(option.DataCenter) {
  277. continue
  278. }
  279. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  280. continue
  281. }
  282. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  283. continue
  284. }
  285. vid, locationList = writableVolumeId, volumeLocationList.Copy()
  286. // check whether picked file is close to full
  287. info, _ := dn.GetVolumesById(writableVolumeId)
  288. if float64(info.Size) > float64(vl.volumeSizeLimit)*option.Threshold() {
  289. shouldGrow = true
  290. }
  291. counter = count
  292. return
  293. }
  294. }
  295. return vid, count, locationList, true, fmt.Errorf("No writable volumes in DataCenter:%v Rack:%v DataNode:%v", option.DataCenter, option.Rack, option.DataNode)
  296. }
  297. func (vl *VolumeLayout) HasGrowRequest() bool {
  298. return atomic.LoadInt32(&vl.growRequestCount) > 0
  299. }
  300. func (vl *VolumeLayout) AddGrowRequest() {
  301. atomic.AddInt32(&vl.growRequestCount, 1)
  302. }
  303. func (vl *VolumeLayout) DoneGrowRequest() {
  304. atomic.AddInt32(&vl.growRequestCount, -1)
  305. }
  306. func (vl *VolumeLayout) ShouldGrowVolumes(option *VolumeGrowOption) bool {
  307. total, active, crowded := vl.GetActiveVolumeCount(option)
  308. stats.MasterVolumeLayout.WithLabelValues(option.Collection, option.ReplicaPlacement.String(), "total").Set(float64(total))
  309. stats.MasterVolumeLayout.WithLabelValues(option.Collection, option.ReplicaPlacement.String(), "active").Set(float64(active))
  310. stats.MasterVolumeLayout.WithLabelValues(option.Collection, option.ReplicaPlacement.String(), "crowded").Set(float64(crowded))
  311. //glog.V(0).Infof("active volume: %d, high usage volume: %d\n", active, high)
  312. return active <= crowded
  313. }
  314. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) (total, active, crowded int) {
  315. vl.accessLock.RLock()
  316. defer vl.accessLock.RUnlock()
  317. if option.DataCenter == "" {
  318. return len(vl.writables), len(vl.writables), len(vl.crowded)
  319. }
  320. total = len(vl.writables)
  321. threshold := option.Threshold()
  322. for _, v := range vl.writables {
  323. for _, dn := range vl.vid2location[v].list {
  324. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  325. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  326. continue
  327. }
  328. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  329. continue
  330. }
  331. active++
  332. info, _ := dn.GetVolumesById(v)
  333. if float64(info.Size) > float64(vl.volumeSizeLimit)*threshold {
  334. crowded++
  335. }
  336. }
  337. }
  338. }
  339. return
  340. }
  341. func (vl *VolumeLayout) removeFromWritable(vid needle.VolumeId) bool {
  342. toDeleteIndex := -1
  343. for k, id := range vl.writables {
  344. if id == vid {
  345. toDeleteIndex = k
  346. break
  347. }
  348. }
  349. if toDeleteIndex >= 0 {
  350. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  351. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  352. vl.removeFromCrowded(vid)
  353. return true
  354. }
  355. return false
  356. }
  357. func (vl *VolumeLayout) setVolumeWritable(vid needle.VolumeId) bool {
  358. for _, v := range vl.writables {
  359. if v == vid {
  360. return false
  361. }
  362. }
  363. glog.V(0).Infoln("Volume", vid, "becomes writable")
  364. vl.writables = append(vl.writables, vid)
  365. return true
  366. }
  367. func (vl *VolumeLayout) SetVolumeReadOnly(dn *DataNode, vid needle.VolumeId) bool {
  368. vl.accessLock.Lock()
  369. defer vl.accessLock.Unlock()
  370. if _, ok := vl.vid2location[vid]; ok {
  371. vl.readonlyVolumes.Add(vid, dn)
  372. return vl.removeFromWritable(vid)
  373. }
  374. return true
  375. }
  376. func (vl *VolumeLayout) SetVolumeWritable(dn *DataNode, vid needle.VolumeId) bool {
  377. vl.accessLock.Lock()
  378. defer vl.accessLock.Unlock()
  379. if _, ok := vl.vid2location[vid]; ok {
  380. vl.readonlyVolumes.Remove(vid, dn)
  381. }
  382. if vl.enoughCopies(vid) {
  383. return vl.setVolumeWritable(vid)
  384. }
  385. return false
  386. }
  387. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid needle.VolumeId) bool {
  388. vl.accessLock.Lock()
  389. defer vl.accessLock.Unlock()
  390. if location, ok := vl.vid2location[vid]; ok {
  391. if location.Remove(dn) {
  392. vl.readonlyVolumes.Remove(vid, dn)
  393. vl.oversizedVolumes.Remove(vid, dn)
  394. if location.Length() < vl.rp.GetCopyCount() {
  395. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  396. return vl.removeFromWritable(vid)
  397. }
  398. }
  399. }
  400. return false
  401. }
  402. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid needle.VolumeId, isReadOnly, isFullCapacity bool) bool {
  403. vl.accessLock.Lock()
  404. defer vl.accessLock.Unlock()
  405. vInfo, err := dn.GetVolumesById(vid)
  406. if err != nil {
  407. return false
  408. }
  409. vl.vid2location[vid].Set(dn)
  410. if vInfo.ReadOnly || isReadOnly || isFullCapacity {
  411. return false
  412. }
  413. if vl.enoughCopies(vid) {
  414. return vl.setVolumeWritable(vid)
  415. }
  416. return false
  417. }
  418. func (vl *VolumeLayout) enoughCopies(vid needle.VolumeId) bool {
  419. locations := vl.vid2location[vid].Length()
  420. desired := vl.rp.GetCopyCount()
  421. return locations == desired || (vl.replicationAsMin && locations > desired)
  422. }
  423. func (vl *VolumeLayout) SetVolumeCapacityFull(vid needle.VolumeId) bool {
  424. vl.accessLock.Lock()
  425. defer vl.accessLock.Unlock()
  426. wasWritable := vl.removeFromWritable(vid)
  427. if wasWritable {
  428. glog.V(0).Infof("Volume %d reaches full capacity.", vid)
  429. }
  430. return wasWritable
  431. }
  432. func (vl *VolumeLayout) removeFromCrowded(vid needle.VolumeId) {
  433. delete(vl.crowded, vid)
  434. }
  435. func (vl *VolumeLayout) setVolumeCrowded(vid needle.VolumeId) {
  436. if _, ok := vl.crowded[vid]; !ok {
  437. vl.crowded[vid] = struct{}{}
  438. glog.V(0).Infoln("Volume", vid, "becomes crowded")
  439. }
  440. }
  441. func (vl *VolumeLayout) SetVolumeCrowded(vid needle.VolumeId) {
  442. // since delete is guarded by accessLock.Lock(),
  443. // and is always called in sequential order,
  444. // RLock() should be safe enough
  445. vl.accessLock.RLock()
  446. defer vl.accessLock.RUnlock()
  447. for _, v := range vl.writables {
  448. if v == vid {
  449. vl.setVolumeCrowded(vid)
  450. break
  451. }
  452. }
  453. }
  454. type VolumeLayoutInfo struct {
  455. Replication string `json:"replication"`
  456. TTL string `json:"ttl"`
  457. Writables []needle.VolumeId `json:"writables"`
  458. Collection string `json:"collection"`
  459. DiskType string `json:"diskType"`
  460. }
  461. func (vl *VolumeLayout) ToInfo() (info VolumeLayoutInfo) {
  462. info.Replication = vl.rp.String()
  463. info.TTL = vl.ttl.String()
  464. info.Writables = vl.writables
  465. info.DiskType = vl.diskType.ReadableString()
  466. //m["locations"] = vl.vid2location
  467. return
  468. }
  469. func (vl *VolumeLayout) Stats() *VolumeLayoutStats {
  470. vl.accessLock.RLock()
  471. defer vl.accessLock.RUnlock()
  472. ret := &VolumeLayoutStats{}
  473. freshThreshold := time.Now().Unix() - 60
  474. for vid, vll := range vl.vid2location {
  475. size, fileCount := vll.Stats(vid, freshThreshold)
  476. ret.FileCount += uint64(fileCount)
  477. ret.UsedSize += size * uint64(vll.Length())
  478. if vl.readonlyVolumes.IsTrue(vid) {
  479. ret.TotalSize += size * uint64(vll.Length())
  480. } else {
  481. ret.TotalSize += vl.volumeSizeLimit * uint64(vll.Length())
  482. }
  483. }
  484. return ret
  485. }