data_node.go 5.2 KB

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