data_node.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package topology
  2. import (
  3. "fmt"
  4. "strconv"
  5. "sync"
  6. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  7. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/storage"
  11. )
  12. type DataNode struct {
  13. NodeImpl
  14. volumes map[needle.VolumeId]storage.VolumeInfo
  15. Ip string
  16. Port int
  17. PublicUrl string
  18. LastSeen int64 // unix time in seconds
  19. ecShards map[needle.VolumeId]*erasure_coding.EcVolumeInfo
  20. ecShardsLock sync.RWMutex
  21. }
  22. func NewDataNode(id string) *DataNode {
  23. s := &DataNode{}
  24. s.id = NodeId(id)
  25. s.nodeType = "DataNode"
  26. s.volumes = make(map[needle.VolumeId]storage.VolumeInfo)
  27. s.ecShards = make(map[needle.VolumeId]*erasure_coding.EcVolumeInfo)
  28. s.NodeImpl.value = s
  29. return s
  30. }
  31. func (dn *DataNode) String() string {
  32. dn.RLock()
  33. defer dn.RUnlock()
  34. return fmt.Sprintf("Node:%s, volumes:%v, Ip:%s, Port:%d, PublicUrl:%s", dn.NodeImpl.String(), dn.volumes, dn.Ip, dn.Port, dn.PublicUrl)
  35. }
  36. func (dn *DataNode) AddOrUpdateVolume(v storage.VolumeInfo) (isNew bool) {
  37. dn.Lock()
  38. defer dn.Unlock()
  39. if oldV, ok := dn.volumes[v.Id]; !ok {
  40. dn.volumes[v.Id] = v
  41. dn.UpAdjustVolumeCountDelta(1)
  42. if v.IsRemote() {
  43. dn.UpAdjustRemoteVolumeCountDelta(1)
  44. }
  45. if !v.ReadOnly {
  46. dn.UpAdjustActiveVolumeCountDelta(1)
  47. }
  48. dn.UpAdjustMaxVolumeId(v.Id)
  49. isNew = true
  50. } else {
  51. if oldV.IsRemote() != v.IsRemote() {
  52. if v.IsRemote() {
  53. dn.UpAdjustRemoteVolumeCountDelta(1)
  54. }
  55. if oldV.IsRemote() {
  56. dn.UpAdjustRemoteVolumeCountDelta(-1)
  57. }
  58. }
  59. dn.volumes[v.Id] = v
  60. }
  61. return
  62. }
  63. func (dn *DataNode) UpdateVolumes(actualVolumes []storage.VolumeInfo) (newVolumes, deletedVolumes []storage.VolumeInfo) {
  64. actualVolumeMap := make(map[needle.VolumeId]storage.VolumeInfo)
  65. for _, v := range actualVolumes {
  66. actualVolumeMap[v.Id] = v
  67. }
  68. dn.Lock()
  69. for vid, v := range dn.volumes {
  70. if _, ok := actualVolumeMap[vid]; !ok {
  71. glog.V(0).Infoln("Deleting volume id:", vid)
  72. delete(dn.volumes, vid)
  73. deletedVolumes = append(deletedVolumes, v)
  74. dn.UpAdjustVolumeCountDelta(-1)
  75. if v.IsRemote() {
  76. dn.UpAdjustRemoteVolumeCountDelta(-1)
  77. }
  78. if !v.ReadOnly {
  79. dn.UpAdjustActiveVolumeCountDelta(-1)
  80. }
  81. }
  82. }
  83. dn.Unlock()
  84. for _, v := range actualVolumes {
  85. isNew := dn.AddOrUpdateVolume(v)
  86. if isNew {
  87. newVolumes = append(newVolumes, v)
  88. }
  89. }
  90. return
  91. }
  92. func (dn *DataNode) DeltaUpdateVolumes(newlVolumes, deletedVolumes []storage.VolumeInfo) {
  93. dn.Lock()
  94. for _, v := range deletedVolumes {
  95. delete(dn.volumes, v.Id)
  96. dn.UpAdjustVolumeCountDelta(-1)
  97. if v.IsRemote() {
  98. dn.UpAdjustRemoteVolumeCountDelta(-1)
  99. }
  100. if !v.ReadOnly {
  101. dn.UpAdjustActiveVolumeCountDelta(-1)
  102. }
  103. }
  104. dn.Unlock()
  105. for _, v := range newlVolumes {
  106. dn.AddOrUpdateVolume(v)
  107. }
  108. return
  109. }
  110. func (dn *DataNode) GetVolumes() (ret []storage.VolumeInfo) {
  111. dn.RLock()
  112. for _, v := range dn.volumes {
  113. ret = append(ret, v)
  114. }
  115. dn.RUnlock()
  116. return ret
  117. }
  118. func (dn *DataNode) GetVolumesById(id needle.VolumeId) (storage.VolumeInfo, error) {
  119. dn.RLock()
  120. defer dn.RUnlock()
  121. vInfo, ok := dn.volumes[id]
  122. if ok {
  123. return vInfo, nil
  124. } else {
  125. return storage.VolumeInfo{}, fmt.Errorf("volumeInfo not found")
  126. }
  127. }
  128. func (dn *DataNode) GetDataCenter() *DataCenter {
  129. return dn.Parent().Parent().(*NodeImpl).value.(*DataCenter)
  130. }
  131. func (dn *DataNode) GetRack() *Rack {
  132. return dn.Parent().(*NodeImpl).value.(*Rack)
  133. }
  134. func (dn *DataNode) GetTopology() *Topology {
  135. p := dn.Parent()
  136. for p.Parent() != nil {
  137. p = p.Parent()
  138. }
  139. t := p.(*Topology)
  140. return t
  141. }
  142. func (dn *DataNode) MatchLocation(ip string, port int) bool {
  143. return dn.Ip == ip && dn.Port == port
  144. }
  145. func (dn *DataNode) Url() string {
  146. return dn.Ip + ":" + strconv.Itoa(dn.Port)
  147. }
  148. func (dn *DataNode) ToMap() interface{} {
  149. ret := make(map[string]interface{})
  150. ret["Url"] = dn.Url()
  151. ret["Volumes"] = dn.GetVolumeCount()
  152. ret["EcShards"] = dn.GetEcShardCount()
  153. ret["Max"] = dn.GetMaxVolumeCount()
  154. ret["Free"] = dn.FreeSpace()
  155. ret["PublicUrl"] = dn.PublicUrl
  156. return ret
  157. }
  158. func (dn *DataNode) ToDataNodeInfo() *master_pb.DataNodeInfo {
  159. m := &master_pb.DataNodeInfo{
  160. Id: string(dn.Id()),
  161. VolumeCount: uint64(dn.GetVolumeCount()),
  162. MaxVolumeCount: uint64(dn.GetMaxVolumeCount()),
  163. FreeVolumeCount: uint64(dn.FreeSpace()),
  164. ActiveVolumeCount: uint64(dn.GetActiveVolumeCount()),
  165. RemoteVolumeCount: uint64(dn.GetRemoteVolumeCount()),
  166. }
  167. for _, v := range dn.GetVolumes() {
  168. m.VolumeInfos = append(m.VolumeInfos, v.ToVolumeInformationMessage())
  169. }
  170. for _, ecv := range dn.GetEcShards() {
  171. m.EcShardInfos = append(m.EcShardInfos, ecv.ToVolumeEcShardInformationMessage())
  172. }
  173. return m
  174. }