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.

571 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. "fmt"
  4. "math/rand/v2"
  5. "sync"
  6. "sync/atomic"
  7. "time"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  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. NoWritableVolumes = "No writable volumes"
  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. growRequest atomic.Bool
  92. lastGrowCount atomic.Uint32
  93. rp *super_block.ReplicaPlacement
  94. ttl *needle.TTL
  95. diskType types.DiskType
  96. vid2location map[needle.VolumeId]*VolumeLocationList
  97. writables []needle.VolumeId // transient array of writable volume id
  98. crowded map[needle.VolumeId]struct{}
  99. readonlyVolumes *volumesBinaryState // readonly volumes
  100. oversizedVolumes *volumesBinaryState // oversized volumes
  101. vacuumedVolumes map[needle.VolumeId]time.Time
  102. volumeSizeLimit uint64
  103. replicationAsMin bool
  104. accessLock sync.RWMutex
  105. }
  106. type VolumeLayoutStats struct {
  107. TotalSize uint64
  108. UsedSize uint64
  109. FileCount uint64
  110. }
  111. func NewVolumeLayout(rp *super_block.ReplicaPlacement, ttl *needle.TTL, diskType types.DiskType, volumeSizeLimit uint64, replicationAsMin bool) *VolumeLayout {
  112. return &VolumeLayout{
  113. rp: rp,
  114. ttl: ttl,
  115. diskType: diskType,
  116. vid2location: make(map[needle.VolumeId]*VolumeLocationList),
  117. writables: *new([]needle.VolumeId),
  118. crowded: make(map[needle.VolumeId]struct{}),
  119. readonlyVolumes: NewVolumesBinaryState(readOnlyState, rp, ExistCopies()),
  120. oversizedVolumes: NewVolumesBinaryState(oversizedState, rp, ExistCopies()),
  121. vacuumedVolumes: make(map[needle.VolumeId]time.Time),
  122. volumeSizeLimit: volumeSizeLimit,
  123. replicationAsMin: replicationAsMin,
  124. }
  125. }
  126. func (vl *VolumeLayout) String() string {
  127. return fmt.Sprintf("rp:%v, ttl:%v, writables:%v, volumeSizeLimit:%v", vl.rp, vl.ttl, vl.writables, vl.volumeSizeLimit)
  128. }
  129. func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  130. vl.accessLock.Lock()
  131. defer vl.accessLock.Unlock()
  132. defer vl.rememberOversizedVolume(v, dn)
  133. if _, ok := vl.vid2location[v.Id]; !ok {
  134. vl.vid2location[v.Id] = NewVolumeLocationList()
  135. }
  136. vl.vid2location[v.Id].Set(dn)
  137. // 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())
  138. for _, dn := range vl.vid2location[v.Id].list {
  139. if vInfo, err := dn.GetVolumesById(v.Id); err == nil {
  140. if vInfo.ReadOnly {
  141. glog.V(1).Infof("vid %d removed from writable", v.Id)
  142. vl.removeFromWritable(v.Id)
  143. vl.readonlyVolumes.Add(v.Id, dn)
  144. return
  145. } else {
  146. vl.readonlyVolumes.Remove(v.Id, dn)
  147. }
  148. } else {
  149. glog.V(1).Infof("vid %d removed from writable", v.Id)
  150. vl.removeFromWritable(v.Id)
  151. vl.readonlyVolumes.Remove(v.Id, dn)
  152. return
  153. }
  154. }
  155. }
  156. func (vl *VolumeLayout) rememberOversizedVolume(v *storage.VolumeInfo, dn *DataNode) {
  157. if vl.isOversized(v) {
  158. vl.oversizedVolumes.Add(v.Id, dn)
  159. } else {
  160. vl.oversizedVolumes.Remove(v.Id, dn)
  161. }
  162. }
  163. func (vl *VolumeLayout) UnRegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  164. vl.accessLock.Lock()
  165. defer vl.accessLock.Unlock()
  166. // remove from vid2location map
  167. location, ok := vl.vid2location[v.Id]
  168. if !ok {
  169. return
  170. }
  171. if location.Remove(dn) {
  172. vl.readonlyVolumes.Remove(v.Id, dn)
  173. vl.oversizedVolumes.Remove(v.Id, dn)
  174. vl.ensureCorrectWritables(v.Id)
  175. if location.Length() == 0 {
  176. delete(vl.vid2location, v.Id)
  177. }
  178. }
  179. }
  180. func (vl *VolumeLayout) EnsureCorrectWritables(v *storage.VolumeInfo) {
  181. vl.accessLock.Lock()
  182. defer vl.accessLock.Unlock()
  183. vl.ensureCorrectWritables(v.Id)
  184. }
  185. func (vl *VolumeLayout) ensureCorrectWritables(vid needle.VolumeId) {
  186. isEnoughCopies := vl.enoughCopies(vid)
  187. isAllWritable := vl.isAllWritable(vid)
  188. isOversizedVolume := vl.oversizedVolumes.IsTrue(vid)
  189. if isEnoughCopies && isAllWritable && !isOversizedVolume {
  190. vl.setVolumeWritable(vid)
  191. } else {
  192. if !isEnoughCopies {
  193. glog.V(0).Infof("volume %d does not have enough copies", vid)
  194. }
  195. if !isAllWritable {
  196. glog.V(0).Infof("volume %d are not all writable", vid)
  197. }
  198. if isOversizedVolume {
  199. glog.V(1).Infof("volume %d are oversized", vid)
  200. }
  201. glog.V(0).Infof("volume %d remove from writable", vid)
  202. vl.removeFromWritable(vid)
  203. }
  204. }
  205. func (vl *VolumeLayout) isAllWritable(vid needle.VolumeId) bool {
  206. if location, ok := vl.vid2location[vid]; ok {
  207. for _, dn := range location.list {
  208. if v, getError := dn.GetVolumesById(vid); getError == nil {
  209. if v.ReadOnly {
  210. return false
  211. }
  212. }
  213. }
  214. } else {
  215. return false
  216. }
  217. return true
  218. }
  219. func (vl *VolumeLayout) isOversized(v *storage.VolumeInfo) bool {
  220. return uint64(v.Size) >= vl.volumeSizeLimit
  221. }
  222. func (vl *VolumeLayout) isCrowdedVolume(v *storage.VolumeInfo) bool {
  223. return float64(v.Size) > float64(vl.volumeSizeLimit)*VolumeGrowStrategy.Threshold
  224. }
  225. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  226. return !vl.isOversized(v) &&
  227. v.Version == needle.CurrentVersion &&
  228. !v.ReadOnly
  229. }
  230. func (vl *VolumeLayout) isEmpty() bool {
  231. vl.accessLock.RLock()
  232. defer vl.accessLock.RUnlock()
  233. return len(vl.vid2location) == 0
  234. }
  235. func (vl *VolumeLayout) Lookup(vid needle.VolumeId) []*DataNode {
  236. vl.accessLock.RLock()
  237. defer vl.accessLock.RUnlock()
  238. if location := vl.vid2location[vid]; location != nil {
  239. return location.list
  240. }
  241. return nil
  242. }
  243. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  244. vl.accessLock.RLock()
  245. defer vl.accessLock.RUnlock()
  246. for _, location := range vl.vid2location {
  247. nodes = append(nodes, location.list...)
  248. }
  249. return
  250. }
  251. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (vid needle.VolumeId, counter uint64, locationList *VolumeLocationList, shouldGrow bool, err error) {
  252. vl.accessLock.RLock()
  253. defer vl.accessLock.RUnlock()
  254. lenWriters := len(vl.writables)
  255. if lenWriters <= 0 {
  256. return 0, 0, nil, true, fmt.Errorf("%s", NoWritableVolumes)
  257. }
  258. if option.DataCenter == "" && option.Rack == "" && option.DataNode == "" {
  259. vid := vl.writables[rand.IntN(lenWriters)]
  260. locationList = vl.vid2location[vid]
  261. if locationList == nil || len(locationList.list) == 0 {
  262. return 0, 0, nil, false, fmt.Errorf("Strangely vid %s is on no machine!", vid.String())
  263. }
  264. return vid, count, locationList.Copy(), false, nil
  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, counter = writableVolumeId, volumeLocationList.Copy(), count
  286. return
  287. }
  288. }
  289. return vid, count, locationList, true, fmt.Errorf("%s in DataCenter:%v Rack:%v DataNode:%v", NoWritableVolumes, option.DataCenter, option.Rack, option.DataNode)
  290. }
  291. func (vl *VolumeLayout) HasGrowRequest() bool {
  292. return vl.growRequest.Load()
  293. }
  294. func (vl *VolumeLayout) AddGrowRequest() {
  295. vl.growRequest.Store(true)
  296. }
  297. func (vl *VolumeLayout) DoneGrowRequest() {
  298. vl.growRequest.Store(false)
  299. }
  300. func (vl *VolumeLayout) SetLastGrowCount(count uint32) {
  301. if vl.lastGrowCount.Load() != count && count != 0 {
  302. vl.lastGrowCount.Store(count)
  303. }
  304. }
  305. func (vl *VolumeLayout) GetLastGrowCount() uint32 {
  306. return vl.lastGrowCount.Load()
  307. }
  308. func (vl *VolumeLayout) ShouldGrowVolumes() bool {
  309. writable, crowded := vl.GetWritableVolumeCount()
  310. return writable <= crowded
  311. }
  312. func (vl *VolumeLayout) ShouldGrowVolumesByDcAndRack(writables *[]needle.VolumeId, dcId NodeId, rackId NodeId) bool {
  313. for _, v := range *writables {
  314. for _, dn := range vl.Lookup(v) {
  315. if dn.GetDataCenter().Id() == dcId && dn.GetRack().Id() == rackId {
  316. if info, err := dn.GetVolumesById(v); err == nil && !vl.isCrowdedVolume(&info) {
  317. return false
  318. }
  319. }
  320. }
  321. }
  322. return true
  323. }
  324. func (vl *VolumeLayout) GetWritableVolumeCount() (active, crowded int) {
  325. vl.accessLock.RLock()
  326. defer vl.accessLock.RUnlock()
  327. return len(vl.writables), len(vl.crowded)
  328. }
  329. func (vl *VolumeLayout) CloneWritableVolumes() (writables []needle.VolumeId) {
  330. vl.accessLock.RLock()
  331. writables = make([]needle.VolumeId, len(vl.writables))
  332. copy(writables, vl.writables)
  333. vl.accessLock.RUnlock()
  334. return writables
  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. vl.removeFromCrowded(vid)
  345. if toDeleteIndex >= 0 {
  346. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  347. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  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. if _, ok := vl.crowded[vid]; ok {
  429. glog.V(0).Infoln("Volume", vid, "becomes uncrowded")
  430. delete(vl.crowded, vid)
  431. }
  432. }
  433. func (vl *VolumeLayout) setVolumeCrowded(vid needle.VolumeId) {
  434. if _, ok := vl.crowded[vid]; !ok {
  435. vl.crowded[vid] = struct{}{}
  436. glog.V(0).Infoln("Volume", vid, "becomes crowded")
  437. }
  438. }
  439. func (vl *VolumeLayout) SetVolumeCrowded(vid needle.VolumeId) {
  440. // since delete is guarded by accessLock.Lock(),
  441. // and is always called in sequential order,
  442. // RLock() should be safe enough
  443. vl.accessLock.RLock()
  444. defer vl.accessLock.RUnlock()
  445. vl.setVolumeCrowded(vid)
  446. }
  447. type VolumeLayoutInfo struct {
  448. Replication string `json:"replication"`
  449. TTL string `json:"ttl"`
  450. Writables []needle.VolumeId `json:"writables"`
  451. Collection string `json:"collection"`
  452. DiskType string `json:"diskType"`
  453. }
  454. func (vl *VolumeLayout) ToInfo() (info VolumeLayoutInfo) {
  455. info.Replication = vl.rp.String()
  456. info.TTL = vl.ttl.String()
  457. info.Writables = vl.writables
  458. info.DiskType = vl.diskType.ReadableString()
  459. //m["locations"] = vl.vid2location
  460. return
  461. }
  462. func (vlc *VolumeLayoutCollection) ToVolumeGrowRequest() *master_pb.VolumeGrowRequest {
  463. return &master_pb.VolumeGrowRequest{
  464. Collection: vlc.Collection,
  465. Replication: vlc.VolumeLayout.rp.String(),
  466. Ttl: vlc.VolumeLayout.ttl.String(),
  467. DiskType: vlc.VolumeLayout.diskType.String(),
  468. }
  469. }
  470. func (vl *VolumeLayout) Stats() *VolumeLayoutStats {
  471. vl.accessLock.RLock()
  472. defer vl.accessLock.RUnlock()
  473. ret := &VolumeLayoutStats{}
  474. freshThreshold := time.Now().Unix() - 60
  475. for vid, vll := range vl.vid2location {
  476. size, fileCount := vll.Stats(vid, freshThreshold)
  477. ret.FileCount += uint64(fileCount)
  478. ret.UsedSize += size * uint64(vll.Length())
  479. if vl.readonlyVolumes.IsTrue(vid) {
  480. ret.TotalSize += size * uint64(vll.Length())
  481. } else {
  482. ret.TotalSize += vl.volumeSizeLimit * uint64(vll.Length())
  483. }
  484. }
  485. return ret
  486. }