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.

234 lines
5.7 KiB

6 years ago
6 years ago
6 years ago
9 years ago
9 years ago
6 years ago
9 years ago
6 years ago
  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. return dn.doAddOrUpdateVolume(v)
  41. }
  42. func (dn *DataNode) doAddOrUpdateVolume(v storage.VolumeInfo) (isNew, isChangedRO bool) {
  43. if oldV, ok := dn.volumes[v.Id]; !ok {
  44. dn.volumes[v.Id] = v
  45. if v.DiskType == storage.SsdType {
  46. dn.UpAdjustSsdVolumeCountDelta(1)
  47. } else {
  48. dn.UpAdjustVolumeCountDelta(1)
  49. }
  50. if v.IsRemote() {
  51. dn.UpAdjustRemoteVolumeCountDelta(1)
  52. }
  53. if !v.ReadOnly {
  54. dn.UpAdjustActiveVolumeCountDelta(1)
  55. }
  56. dn.UpAdjustMaxVolumeId(v.Id)
  57. isNew = true
  58. } else {
  59. if oldV.IsRemote() != v.IsRemote() {
  60. if v.IsRemote() {
  61. dn.UpAdjustRemoteVolumeCountDelta(1)
  62. }
  63. if oldV.IsRemote() {
  64. dn.UpAdjustRemoteVolumeCountDelta(-1)
  65. }
  66. }
  67. isChangedRO = dn.volumes[v.Id].ReadOnly != v.ReadOnly
  68. dn.volumes[v.Id] = v
  69. }
  70. return
  71. }
  72. func (dn *DataNode) UpdateVolumes(actualVolumes []storage.VolumeInfo) (newVolumes, deletedVolumes, changeRO []storage.VolumeInfo) {
  73. actualVolumeMap := make(map[needle.VolumeId]storage.VolumeInfo)
  74. for _, v := range actualVolumes {
  75. actualVolumeMap[v.Id] = v
  76. }
  77. dn.Lock()
  78. defer dn.Unlock()
  79. for vid, v := range dn.volumes {
  80. if _, ok := actualVolumeMap[vid]; !ok {
  81. glog.V(0).Infoln("Deleting volume id:", vid)
  82. delete(dn.volumes, vid)
  83. deletedVolumes = append(deletedVolumes, v)
  84. if v.DiskType == storage.SsdType {
  85. dn.UpAdjustSsdVolumeCountDelta(-1)
  86. } else {
  87. dn.UpAdjustVolumeCountDelta(-1)
  88. }
  89. if v.IsRemote() {
  90. dn.UpAdjustRemoteVolumeCountDelta(-1)
  91. }
  92. if !v.ReadOnly {
  93. dn.UpAdjustActiveVolumeCountDelta(-1)
  94. }
  95. }
  96. }
  97. for _, v := range actualVolumes {
  98. isNew, isChangedRO := dn.doAddOrUpdateVolume(v)
  99. if isNew {
  100. newVolumes = append(newVolumes, v)
  101. }
  102. if isChangedRO {
  103. changeRO = append(changeRO, v)
  104. }
  105. }
  106. return
  107. }
  108. func (dn *DataNode) DeltaUpdateVolumes(newVolumes, deletedVolumes []storage.VolumeInfo) {
  109. dn.Lock()
  110. defer dn.Unlock()
  111. for _, v := range deletedVolumes {
  112. delete(dn.volumes, v.Id)
  113. if v.DiskType == storage.SsdType {
  114. dn.UpAdjustSsdVolumeCountDelta(-1)
  115. } else {
  116. dn.UpAdjustVolumeCountDelta(-1)
  117. }
  118. if v.IsRemote() {
  119. dn.UpAdjustRemoteVolumeCountDelta(-1)
  120. }
  121. if !v.ReadOnly {
  122. dn.UpAdjustActiveVolumeCountDelta(-1)
  123. }
  124. }
  125. for _, v := range newVolumes {
  126. dn.doAddOrUpdateVolume(v)
  127. }
  128. return
  129. }
  130. func (dn *DataNode) GetVolumes() (ret []storage.VolumeInfo) {
  131. dn.RLock()
  132. for _, v := range dn.volumes {
  133. ret = append(ret, v)
  134. }
  135. dn.RUnlock()
  136. return ret
  137. }
  138. func (dn *DataNode) GetVolumesById(id needle.VolumeId) (storage.VolumeInfo, error) {
  139. dn.RLock()
  140. defer dn.RUnlock()
  141. vInfo, ok := dn.volumes[id]
  142. if ok {
  143. return vInfo, nil
  144. } else {
  145. return storage.VolumeInfo{}, fmt.Errorf("volumeInfo not found")
  146. }
  147. }
  148. func (dn *DataNode) GetDataCenter() *DataCenter {
  149. return dn.Parent().Parent().(*NodeImpl).value.(*DataCenter)
  150. }
  151. func (dn *DataNode) GetRack() *Rack {
  152. return dn.Parent().(*NodeImpl).value.(*Rack)
  153. }
  154. func (dn *DataNode) GetTopology() *Topology {
  155. p := dn.Parent()
  156. for p.Parent() != nil {
  157. p = p.Parent()
  158. }
  159. t := p.(*Topology)
  160. return t
  161. }
  162. func (dn *DataNode) MatchLocation(ip string, port int) bool {
  163. return dn.Ip == ip && dn.Port == port
  164. }
  165. func (dn *DataNode) Url() string {
  166. return dn.Ip + ":" + strconv.Itoa(dn.Port)
  167. }
  168. func (dn *DataNode) ToMap() interface{} {
  169. ret := make(map[string]interface{})
  170. ret["Url"] = dn.Url()
  171. ret["Volumes"] = dn.GetVolumeCount()
  172. ret["VolumeIds"] = dn.GetVolumeIds()
  173. ret["EcShards"] = dn.GetEcShardCount()
  174. ret["Max"] = dn.GetMaxVolumeCount()
  175. ret["MaxSsd"] = dn.GetMaxSsdVolumeCount()
  176. ret["Free"] = dn.FreeSpace()
  177. ret["PublicUrl"] = dn.PublicUrl
  178. return ret
  179. }
  180. func (dn *DataNode) ToDataNodeInfo() *master_pb.DataNodeInfo {
  181. m := &master_pb.DataNodeInfo{
  182. Id: string(dn.Id()),
  183. VolumeCount: uint64(dn.GetVolumeCount()),
  184. MaxVolumeCount: uint64(dn.GetMaxVolumeCount()),
  185. MaxSsdVolumeCount: uint64(dn.GetMaxSsdVolumeCount()),
  186. SsdVolumeCount: uint64(dn.GetSsdVolumeCount()),
  187. FreeVolumeCount: uint64(dn.FreeSpace()),
  188. ActiveVolumeCount: uint64(dn.GetActiveVolumeCount()),
  189. RemoteVolumeCount: uint64(dn.GetRemoteVolumeCount()),
  190. }
  191. for _, v := range dn.GetVolumes() {
  192. m.VolumeInfos = append(m.VolumeInfos, v.ToVolumeInformationMessage())
  193. }
  194. for _, ecv := range dn.GetEcShards() {
  195. m.EcShardInfos = append(m.EcShardInfos, ecv.ToVolumeEcShardInformationMessage())
  196. }
  197. return m
  198. }
  199. // GetVolumeIds returns the human readable volume ids limited to count of max 100.
  200. func (dn *DataNode) GetVolumeIds() string {
  201. dn.RLock()
  202. defer dn.RUnlock()
  203. ids := make([]int, 0, len(dn.volumes))
  204. for k := range dn.volumes {
  205. ids = append(ids, int(k))
  206. }
  207. return util.HumanReadableIntsMax(100, ids...)
  208. }