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.

557 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. for _, dn := range vl.vid2location[vid].list {
  202. if v, getError := dn.GetVolumesById(vid); getError == nil {
  203. if v.ReadOnly {
  204. return false
  205. }
  206. }
  207. }
  208. return true
  209. }
  210. func (vl *VolumeLayout) isOversized(v *storage.VolumeInfo) bool {
  211. return uint64(v.Size) >= vl.volumeSizeLimit
  212. }
  213. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  214. return !vl.isOversized(v) &&
  215. v.Version == needle.CurrentVersion &&
  216. !v.ReadOnly
  217. }
  218. func (vl *VolumeLayout) isEmpty() bool {
  219. vl.accessLock.RLock()
  220. defer vl.accessLock.RUnlock()
  221. return len(vl.vid2location) == 0
  222. }
  223. func (vl *VolumeLayout) Lookup(vid needle.VolumeId) []*DataNode {
  224. vl.accessLock.RLock()
  225. defer vl.accessLock.RUnlock()
  226. if location := vl.vid2location[vid]; location != nil {
  227. return location.list
  228. }
  229. return nil
  230. }
  231. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  232. vl.accessLock.RLock()
  233. defer vl.accessLock.RUnlock()
  234. for _, location := range vl.vid2location {
  235. nodes = append(nodes, location.list...)
  236. }
  237. return
  238. }
  239. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (vid needle.VolumeId, counter uint64, locationList *VolumeLocationList, shouldGrow bool, err error) {
  240. vl.accessLock.RLock()
  241. defer vl.accessLock.RUnlock()
  242. lenWriters := len(vl.writables)
  243. if lenWriters <= 0 {
  244. //glog.V(0).Infoln("No more writable volumes!")
  245. shouldGrow = true
  246. return 0, 0, nil, shouldGrow, errors.New("No more writable volumes!")
  247. }
  248. if option.DataCenter == "" && option.Rack == "" && option.DataNode == "" {
  249. vid := vl.writables[rand.Intn(lenWriters)]
  250. locationList = vl.vid2location[vid]
  251. if locationList != nil && locationList.Length() > 0 {
  252. // check whether picked file is close to full
  253. dn := locationList.Head()
  254. info, _ := dn.GetVolumesById(vid)
  255. if float64(info.Size) > float64(vl.volumeSizeLimit)*option.Threshold() {
  256. shouldGrow = true
  257. }
  258. return vid, count, locationList.Copy(), shouldGrow, nil
  259. }
  260. return 0, 0, nil, shouldGrow, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  261. }
  262. // clone vl.writables
  263. writables := make([]needle.VolumeId, len(vl.writables))
  264. copy(writables, vl.writables)
  265. // randomize the writables
  266. rand.Shuffle(len(writables), func(i, j int) {
  267. writables[i], writables[j] = writables[j], writables[i]
  268. })
  269. for _, writableVolumeId := range writables {
  270. volumeLocationList := vl.vid2location[writableVolumeId]
  271. for _, dn := range volumeLocationList.list {
  272. if option.DataCenter != "" && dn.GetDataCenter().Id() != NodeId(option.DataCenter) {
  273. continue
  274. }
  275. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  276. continue
  277. }
  278. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  279. continue
  280. }
  281. vid, locationList = writableVolumeId, volumeLocationList.Copy()
  282. // check whether picked file is close to full
  283. info, _ := dn.GetVolumesById(writableVolumeId)
  284. if float64(info.Size) > float64(vl.volumeSizeLimit)*option.Threshold() {
  285. shouldGrow = true
  286. }
  287. counter = count
  288. return
  289. }
  290. }
  291. return vid, count, locationList, true, fmt.Errorf("No writable volumes in DataCenter:%v Rack:%v DataNode:%v", option.DataCenter, option.Rack, option.DataNode)
  292. }
  293. func (vl *VolumeLayout) HasGrowRequest() bool {
  294. return atomic.LoadInt32(&vl.growRequestCount) > 0
  295. }
  296. func (vl *VolumeLayout) AddGrowRequest() {
  297. atomic.AddInt32(&vl.growRequestCount, 1)
  298. }
  299. func (vl *VolumeLayout) DoneGrowRequest() {
  300. atomic.AddInt32(&vl.growRequestCount, -1)
  301. }
  302. func (vl *VolumeLayout) ShouldGrowVolumes(option *VolumeGrowOption) bool {
  303. total, active, crowded := vl.GetActiveVolumeCount(option)
  304. stats.MasterVolumeLayout.WithLabelValues(option.Collection, option.ReplicaPlacement.String(), "total").Set(float64(total))
  305. stats.MasterVolumeLayout.WithLabelValues(option.Collection, option.ReplicaPlacement.String(), "active").Set(float64(active))
  306. stats.MasterVolumeLayout.WithLabelValues(option.Collection, option.ReplicaPlacement.String(), "crowded").Set(float64(crowded))
  307. //glog.V(0).Infof("active volume: %d, high usage volume: %d\n", active, high)
  308. return active <= crowded
  309. }
  310. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) (total, active, crowded int) {
  311. vl.accessLock.RLock()
  312. defer vl.accessLock.RUnlock()
  313. if option.DataCenter == "" {
  314. return len(vl.writables), len(vl.writables), len(vl.crowded)
  315. }
  316. total = len(vl.writables)
  317. for _, v := range vl.writables {
  318. for _, dn := range vl.vid2location[v].list {
  319. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  320. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  321. continue
  322. }
  323. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  324. continue
  325. }
  326. active++
  327. info, _ := dn.GetVolumesById(v)
  328. if float64(info.Size) > float64(vl.volumeSizeLimit)*option.Threshold() {
  329. crowded++
  330. }
  331. }
  332. }
  333. }
  334. return
  335. }
  336. func (vl *VolumeLayout) removeFromWritable(vid needle.VolumeId) bool {
  337. toDeleteIndex := -1
  338. for k, id := range vl.writables {
  339. if id == vid {
  340. toDeleteIndex = k
  341. break
  342. }
  343. }
  344. if toDeleteIndex >= 0 {
  345. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  346. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  347. vl.removeFromCrowded(vid)
  348. return true
  349. }
  350. return false
  351. }
  352. func (vl *VolumeLayout) setVolumeWritable(vid needle.VolumeId) bool {
  353. for _, v := range vl.writables {
  354. if v == vid {
  355. return false
  356. }
  357. }
  358. glog.V(0).Infoln("Volume", vid, "becomes writable")
  359. vl.writables = append(vl.writables, vid)
  360. return true
  361. }
  362. func (vl *VolumeLayout) SetVolumeReadOnly(dn *DataNode, vid needle.VolumeId) bool {
  363. vl.accessLock.Lock()
  364. defer vl.accessLock.Unlock()
  365. if _, ok := vl.vid2location[vid]; ok {
  366. vl.readonlyVolumes.Add(vid, dn)
  367. return vl.removeFromWritable(vid)
  368. }
  369. return true
  370. }
  371. func (vl *VolumeLayout) SetVolumeWritable(dn *DataNode, vid needle.VolumeId) bool {
  372. vl.accessLock.Lock()
  373. defer vl.accessLock.Unlock()
  374. if _, ok := vl.vid2location[vid]; ok {
  375. vl.readonlyVolumes.Remove(vid, dn)
  376. }
  377. if vl.enoughCopies(vid) {
  378. return vl.setVolumeWritable(vid)
  379. }
  380. return false
  381. }
  382. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid needle.VolumeId) bool {
  383. vl.accessLock.Lock()
  384. defer vl.accessLock.Unlock()
  385. if location, ok := vl.vid2location[vid]; ok {
  386. if location.Remove(dn) {
  387. vl.readonlyVolumes.Remove(vid, dn)
  388. vl.oversizedVolumes.Remove(vid, dn)
  389. if location.Length() < vl.rp.GetCopyCount() {
  390. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  391. return vl.removeFromWritable(vid)
  392. }
  393. }
  394. }
  395. return false
  396. }
  397. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid needle.VolumeId, isReadOnly, isFullCapacity bool) bool {
  398. vl.accessLock.Lock()
  399. defer vl.accessLock.Unlock()
  400. vInfo, err := dn.GetVolumesById(vid)
  401. if err != nil {
  402. return false
  403. }
  404. vl.vid2location[vid].Set(dn)
  405. if vInfo.ReadOnly || isReadOnly || isFullCapacity {
  406. return false
  407. }
  408. if vl.enoughCopies(vid) {
  409. return vl.setVolumeWritable(vid)
  410. }
  411. return false
  412. }
  413. func (vl *VolumeLayout) enoughCopies(vid needle.VolumeId) bool {
  414. locations := vl.vid2location[vid].Length()
  415. desired := vl.rp.GetCopyCount()
  416. return locations == desired || (vl.replicationAsMin && locations > desired)
  417. }
  418. func (vl *VolumeLayout) SetVolumeCapacityFull(vid needle.VolumeId) bool {
  419. vl.accessLock.Lock()
  420. defer vl.accessLock.Unlock()
  421. wasWritable := vl.removeFromWritable(vid)
  422. if wasWritable {
  423. glog.V(0).Infof("Volume %d reaches full capacity.", vid)
  424. }
  425. return wasWritable
  426. }
  427. func (vl *VolumeLayout) removeFromCrowded(vid needle.VolumeId) {
  428. delete(vl.crowded, vid)
  429. }
  430. func (vl *VolumeLayout) setVolumeCrowded(vid needle.VolumeId) {
  431. if _, ok := vl.crowded[vid]; !ok {
  432. vl.crowded[vid] = struct{}{}
  433. glog.V(0).Infoln("Volume", vid, "becomes crowded")
  434. }
  435. }
  436. func (vl *VolumeLayout) SetVolumeCrowded(vid needle.VolumeId) {
  437. // since delete is guarded by accessLock.Lock(),
  438. // and is always called in sequential order,
  439. // RLock() should be safe enough
  440. vl.accessLock.RLock()
  441. defer vl.accessLock.RUnlock()
  442. for _, v := range vl.writables {
  443. if v == vid {
  444. vl.setVolumeCrowded(vid)
  445. break
  446. }
  447. }
  448. }
  449. type VolumeLayoutInfo struct {
  450. Replication string `json:"replication"`
  451. TTL string `json:"ttl"`
  452. Writables []needle.VolumeId `json:"writables"`
  453. Collection string `json:"collection"`
  454. DiskType string `json:"diskType"`
  455. }
  456. func (vl *VolumeLayout) ToInfo() (info VolumeLayoutInfo) {
  457. info.Replication = vl.rp.String()
  458. info.TTL = vl.ttl.String()
  459. info.Writables = vl.writables
  460. info.DiskType = vl.diskType.ReadableString()
  461. //m["locations"] = vl.vid2location
  462. return
  463. }
  464. func (vl *VolumeLayout) Stats() *VolumeLayoutStats {
  465. vl.accessLock.RLock()
  466. defer vl.accessLock.RUnlock()
  467. ret := &VolumeLayoutStats{}
  468. freshThreshold := time.Now().Unix() - 60
  469. for vid, vll := range vl.vid2location {
  470. size, fileCount := vll.Stats(vid, freshThreshold)
  471. ret.FileCount += uint64(fileCount)
  472. ret.UsedSize += size * uint64(vll.Length())
  473. if vl.readonlyVolumes.IsTrue(vid) {
  474. ret.TotalSize += size * uint64(vll.Length())
  475. } else {
  476. ret.TotalSize += vl.volumeSizeLimit * uint64(vll.Length())
  477. }
  478. }
  479. return ret
  480. }