volume_layout.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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)*VolumeGrowStrategy.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)*VolumeGrowStrategy.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. for _, v := range vl.writables {
  322. for _, dn := range vl.vid2location[v].list {
  323. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  324. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  325. continue
  326. }
  327. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  328. continue
  329. }
  330. active++
  331. info, _ := dn.GetVolumesById(v)
  332. if float64(info.Size) > float64(vl.volumeSizeLimit)* VolumeGrowStrategy.Threshold{
  333. crowded++
  334. }
  335. }
  336. }
  337. }
  338. return
  339. }
  340. func (vl *VolumeLayout) removeFromWritable(vid needle.VolumeId) bool {
  341. toDeleteIndex := -1
  342. for k, id := range vl.writables {
  343. if id == vid {
  344. toDeleteIndex = k
  345. break
  346. }
  347. }
  348. if toDeleteIndex >= 0 {
  349. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  350. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  351. vl.removeFromCrowded(vid)
  352. return true
  353. }
  354. return false
  355. }
  356. func (vl *VolumeLayout) setVolumeWritable(vid needle.VolumeId) bool {
  357. for _, v := range vl.writables {
  358. if v == vid {
  359. return false
  360. }
  361. }
  362. glog.V(0).Infoln("Volume", vid, "becomes writable")
  363. vl.writables = append(vl.writables, vid)
  364. return true
  365. }
  366. func (vl *VolumeLayout) SetVolumeReadOnly(dn *DataNode, vid needle.VolumeId) bool {
  367. vl.accessLock.Lock()
  368. defer vl.accessLock.Unlock()
  369. if _, ok := vl.vid2location[vid]; ok {
  370. vl.readonlyVolumes.Add(vid, dn)
  371. return vl.removeFromWritable(vid)
  372. }
  373. return true
  374. }
  375. func (vl *VolumeLayout) SetVolumeWritable(dn *DataNode, vid needle.VolumeId) bool {
  376. vl.accessLock.Lock()
  377. defer vl.accessLock.Unlock()
  378. if _, ok := vl.vid2location[vid]; ok {
  379. vl.readonlyVolumes.Remove(vid, dn)
  380. }
  381. if vl.enoughCopies(vid) {
  382. return vl.setVolumeWritable(vid)
  383. }
  384. return false
  385. }
  386. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid needle.VolumeId) bool {
  387. vl.accessLock.Lock()
  388. defer vl.accessLock.Unlock()
  389. if location, ok := vl.vid2location[vid]; ok {
  390. if location.Remove(dn) {
  391. vl.readonlyVolumes.Remove(vid, dn)
  392. vl.oversizedVolumes.Remove(vid, dn)
  393. if location.Length() < vl.rp.GetCopyCount() {
  394. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  395. return vl.removeFromWritable(vid)
  396. }
  397. }
  398. }
  399. return false
  400. }
  401. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid needle.VolumeId, isReadOnly, isFullCapacity bool) bool {
  402. vl.accessLock.Lock()
  403. defer vl.accessLock.Unlock()
  404. vInfo, err := dn.GetVolumesById(vid)
  405. if err != nil {
  406. return false
  407. }
  408. vl.vid2location[vid].Set(dn)
  409. if vInfo.ReadOnly || isReadOnly || isFullCapacity {
  410. return false
  411. }
  412. if vl.enoughCopies(vid) {
  413. return vl.setVolumeWritable(vid)
  414. }
  415. return false
  416. }
  417. func (vl *VolumeLayout) enoughCopies(vid needle.VolumeId) bool {
  418. locations := vl.vid2location[vid].Length()
  419. desired := vl.rp.GetCopyCount()
  420. return locations == desired || (vl.replicationAsMin && locations > desired)
  421. }
  422. func (vl *VolumeLayout) SetVolumeCapacityFull(vid needle.VolumeId) bool {
  423. vl.accessLock.Lock()
  424. defer vl.accessLock.Unlock()
  425. wasWritable := vl.removeFromWritable(vid)
  426. if wasWritable {
  427. glog.V(0).Infof("Volume %d reaches full capacity.", vid)
  428. }
  429. return wasWritable
  430. }
  431. func (vl *VolumeLayout) removeFromCrowded(vid needle.VolumeId) {
  432. delete(vl.crowded, vid)
  433. }
  434. func (vl *VolumeLayout) setVolumeCrowded(vid needle.VolumeId) {
  435. if _, ok := vl.crowded[vid]; !ok {
  436. vl.crowded[vid] = struct{}{}
  437. glog.V(0).Infoln("Volume", vid, "becomes crowded")
  438. }
  439. }
  440. func (vl *VolumeLayout) SetVolumeCrowded(vid needle.VolumeId) {
  441. // since delete is guarded by accessLock.Lock(),
  442. // and is always called in sequential order,
  443. // RLock() should be safe enough
  444. vl.accessLock.RLock()
  445. defer vl.accessLock.RUnlock()
  446. for _, v := range vl.writables {
  447. if v == vid {
  448. vl.setVolumeCrowded(vid)
  449. break
  450. }
  451. }
  452. }
  453. type VolumeLayoutInfo struct {
  454. Replication string `json:"replication"`
  455. TTL string `json:"ttl"`
  456. Writables []needle.VolumeId `json:"writables"`
  457. Collection string `json:"collection"`
  458. DiskType string `json:"diskType"`
  459. }
  460. func (vl *VolumeLayout) ToInfo() (info VolumeLayoutInfo) {
  461. info.Replication = vl.rp.String()
  462. info.TTL = vl.ttl.String()
  463. info.Writables = vl.writables
  464. info.DiskType = vl.diskType.ReadableString()
  465. //m["locations"] = vl.vid2location
  466. return
  467. }
  468. func (vl *VolumeLayout) Stats() *VolumeLayoutStats {
  469. vl.accessLock.RLock()
  470. defer vl.accessLock.RUnlock()
  471. ret := &VolumeLayoutStats{}
  472. freshThreshold := time.Now().Unix() - 60
  473. for vid, vll := range vl.vid2location {
  474. size, fileCount := vll.Stats(vid, freshThreshold)
  475. ret.FileCount += uint64(fileCount)
  476. ret.UsedSize += size * uint64(vll.Length())
  477. if vl.readonlyVolumes.IsTrue(vid) {
  478. ret.TotalSize += size * uint64(vll.Length())
  479. } else {
  480. ret.TotalSize += vl.volumeSizeLimit * uint64(vll.Length())
  481. }
  482. }
  483. return ret
  484. }