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.

344 lines
9.8 KiB

6 years ago
6 years ago
10 years ago
3 years ago
3 years ago
4 years ago
2 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 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
7 years ago
3 years ago
3 years ago
  1. package topology
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "math/rand"
  7. "sync"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/pb"
  10. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  11. backoff "github.com/cenkalti/backoff/v4"
  12. hashicorpRaft "github.com/hashicorp/raft"
  13. "github.com/seaweedfs/raft"
  14. "github.com/seaweedfs/seaweedfs/weed/glog"
  15. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  16. "github.com/seaweedfs/seaweedfs/weed/sequence"
  17. "github.com/seaweedfs/seaweedfs/weed/storage"
  18. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  19. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  20. "github.com/seaweedfs/seaweedfs/weed/util"
  21. )
  22. type Topology struct {
  23. vacuumLockCounter int64
  24. NodeImpl
  25. collectionMap *util.ConcurrentReadMap
  26. ecShardMap map[needle.VolumeId]*EcShardLocations
  27. ecShardMapLock sync.RWMutex
  28. pulse int64
  29. volumeSizeLimit uint64
  30. replicationAsMin bool
  31. isDisableVacuum bool
  32. Sequence sequence.Sequencer
  33. chanFullVolumes chan storage.VolumeInfo
  34. chanCrowdedVolumes chan storage.VolumeInfo
  35. Configuration *Configuration
  36. RaftServer raft.Server
  37. RaftServerAccessLock sync.RWMutex
  38. HashicorpRaft *hashicorpRaft.Raft
  39. UuidAccessLock sync.RWMutex
  40. UuidMap map[string][]string
  41. }
  42. func NewTopology(id string, seq sequence.Sequencer, volumeSizeLimit uint64, pulse int, replicationAsMin bool) *Topology {
  43. t := &Topology{}
  44. t.id = NodeId(id)
  45. t.nodeType = "Topology"
  46. t.NodeImpl.value = t
  47. t.diskUsages = newDiskUsages()
  48. t.children = make(map[NodeId]Node)
  49. t.collectionMap = util.NewConcurrentReadMap()
  50. t.ecShardMap = make(map[needle.VolumeId]*EcShardLocations)
  51. t.pulse = int64(pulse)
  52. t.volumeSizeLimit = volumeSizeLimit
  53. t.replicationAsMin = replicationAsMin
  54. t.Sequence = seq
  55. t.chanFullVolumes = make(chan storage.VolumeInfo)
  56. t.chanCrowdedVolumes = make(chan storage.VolumeInfo)
  57. t.Configuration = &Configuration{}
  58. return t
  59. }
  60. func (t *Topology) IsLeader() bool {
  61. t.RaftServerAccessLock.RLock()
  62. defer t.RaftServerAccessLock.RUnlock()
  63. if t.RaftServer != nil {
  64. if t.RaftServer.State() == raft.Leader {
  65. return true
  66. }
  67. if leader, err := t.Leader(); err == nil {
  68. if pb.ServerAddress(t.RaftServer.Name()) == leader {
  69. return true
  70. }
  71. }
  72. } else if t.HashicorpRaft != nil {
  73. if t.HashicorpRaft.State() == hashicorpRaft.Leader {
  74. return true
  75. }
  76. }
  77. return false
  78. }
  79. func (t *Topology) Leader() (l pb.ServerAddress, err error) {
  80. exponentialBackoff := backoff.NewExponentialBackOff()
  81. exponentialBackoff.InitialInterval = 100 * time.Millisecond
  82. exponentialBackoff.MaxElapsedTime = 20 * time.Second
  83. return backoff.RetryWithData(t.MaybeLeader, exponentialBackoff)
  84. }
  85. func (t *Topology) MaybeLeader() (l pb.ServerAddress, err error) {
  86. t.RaftServerAccessLock.RLock()
  87. defer t.RaftServerAccessLock.RUnlock()
  88. if t.RaftServer != nil {
  89. l = pb.ServerAddress(t.RaftServer.Leader())
  90. } else if t.HashicorpRaft != nil {
  91. l = pb.ServerAddress(t.HashicorpRaft.Leader())
  92. } else {
  93. err = errors.New("Raft Server not ready yet!")
  94. }
  95. return
  96. }
  97. func (t *Topology) Lookup(collection string, vid needle.VolumeId) (dataNodes []*DataNode) {
  98. // maybe an issue if lots of collections?
  99. if collection == "" {
  100. for _, c := range t.collectionMap.Items() {
  101. if list := c.(*Collection).Lookup(vid); list != nil {
  102. return list
  103. }
  104. }
  105. } else {
  106. if c, ok := t.collectionMap.Find(collection); ok {
  107. return c.(*Collection).Lookup(vid)
  108. }
  109. }
  110. if locations, found := t.LookupEcShards(vid); found {
  111. for _, loc := range locations.Locations {
  112. dataNodes = append(dataNodes, loc...)
  113. }
  114. return dataNodes
  115. }
  116. return nil
  117. }
  118. func (t *Topology) NextVolumeId() (needle.VolumeId, error) {
  119. vid := t.GetMaxVolumeId()
  120. next := vid.Next()
  121. t.RaftServerAccessLock.RLock()
  122. defer t.RaftServerAccessLock.RUnlock()
  123. if t.RaftServer != nil {
  124. if _, err := t.RaftServer.Do(NewMaxVolumeIdCommand(next)); err != nil {
  125. return 0, err
  126. }
  127. } else if t.HashicorpRaft != nil {
  128. b, err := json.Marshal(NewMaxVolumeIdCommand(next))
  129. if err != nil {
  130. return 0, fmt.Errorf("failed marshal NewMaxVolumeIdCommand: %+v", err)
  131. }
  132. if future := t.HashicorpRaft.Apply(b, time.Second); future.Error() != nil {
  133. return 0, future.Error()
  134. }
  135. }
  136. return next, nil
  137. }
  138. // deprecated
  139. func (t *Topology) HasWritableVolume(option *VolumeGrowOption) bool {
  140. vl := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  141. active, _ := vl.GetActiveVolumeCount(option)
  142. return active > 0
  143. }
  144. func (t *Topology) PickForWrite(count uint64, option *VolumeGrowOption) (string, uint64, *VolumeLocationList, error) {
  145. vid, count, datanodes, err := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType).PickForWrite(count, option)
  146. if err != nil {
  147. return "", 0, nil, fmt.Errorf("failed to find writable volumes for collection:%s replication:%s ttl:%s error: %v", option.Collection, option.ReplicaPlacement.String(), option.Ttl.String(), err)
  148. }
  149. if datanodes.Length() == 0 {
  150. return "", 0, nil, fmt.Errorf("no writable volumes available for collection:%s replication:%s ttl:%s", option.Collection, option.ReplicaPlacement.String(), option.Ttl.String())
  151. }
  152. fileId := t.Sequence.NextFileId(count)
  153. return needle.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes, nil
  154. }
  155. func (t *Topology) GetVolumeLayout(collectionName string, rp *super_block.ReplicaPlacement, ttl *needle.TTL, diskType types.DiskType) *VolumeLayout {
  156. return t.collectionMap.Get(collectionName, func() interface{} {
  157. return NewCollection(collectionName, t.volumeSizeLimit, t.replicationAsMin)
  158. }).(*Collection).GetOrCreateVolumeLayout(rp, ttl, diskType)
  159. }
  160. func (t *Topology) ListCollections(includeNormalVolumes, includeEcVolumes bool) (ret []string) {
  161. mapOfCollections := make(map[string]bool)
  162. for _, c := range t.collectionMap.Items() {
  163. mapOfCollections[c.(*Collection).Name] = true
  164. }
  165. if includeEcVolumes {
  166. t.ecShardMapLock.RLock()
  167. for _, ecVolumeLocation := range t.ecShardMap {
  168. mapOfCollections[ecVolumeLocation.Collection] = true
  169. }
  170. t.ecShardMapLock.RUnlock()
  171. }
  172. for k := range mapOfCollections {
  173. ret = append(ret, k)
  174. }
  175. return ret
  176. }
  177. func (t *Topology) FindCollection(collectionName string) (*Collection, bool) {
  178. c, hasCollection := t.collectionMap.Find(collectionName)
  179. if !hasCollection {
  180. return nil, false
  181. }
  182. return c.(*Collection), hasCollection
  183. }
  184. func (t *Topology) DeleteCollection(collectionName string) {
  185. t.collectionMap.Delete(collectionName)
  186. }
  187. func (t *Topology) DeleteLayout(collectionName string, rp *super_block.ReplicaPlacement, ttl *needle.TTL, diskType types.DiskType) {
  188. collection, found := t.FindCollection(collectionName)
  189. if !found {
  190. return
  191. }
  192. collection.DeleteVolumeLayout(rp, ttl, diskType)
  193. if len(collection.storageType2VolumeLayout.Items()) == 0 {
  194. t.DeleteCollection(collectionName)
  195. }
  196. }
  197. func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  198. diskType := types.ToDiskType(v.DiskType)
  199. vl := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl, diskType)
  200. vl.RegisterVolume(&v, dn)
  201. vl.EnsureCorrectWritables(&v)
  202. }
  203. func (t *Topology) UnRegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  204. glog.Infof("removing volume info: %+v from %v", v, dn.id)
  205. diskType := types.ToDiskType(v.DiskType)
  206. volumeLayout := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl, diskType)
  207. volumeLayout.UnRegisterVolume(&v, dn)
  208. if volumeLayout.isEmpty() {
  209. t.DeleteLayout(v.Collection, v.ReplicaPlacement, v.Ttl, diskType)
  210. }
  211. }
  212. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  213. t.Lock()
  214. defer t.Unlock()
  215. for _, c := range t.children {
  216. dc := c.(*DataCenter)
  217. if string(dc.Id()) == dcName {
  218. return dc
  219. }
  220. }
  221. dc := NewDataCenter(dcName)
  222. t.doLinkChildNode(dc)
  223. return dc
  224. }
  225. func (t *Topology) SyncDataNodeRegistration(volumes []*master_pb.VolumeInformationMessage, dn *DataNode) (newVolumes, deletedVolumes []storage.VolumeInfo) {
  226. // convert into in memory struct storage.VolumeInfo
  227. var volumeInfos []storage.VolumeInfo
  228. for _, v := range volumes {
  229. if vi, err := storage.NewVolumeInfo(v); err == nil {
  230. volumeInfos = append(volumeInfos, vi)
  231. } else {
  232. glog.V(0).Infof("Fail to convert joined volume information: %v", err)
  233. }
  234. }
  235. // find out the delta volumes
  236. var changedVolumes []storage.VolumeInfo
  237. newVolumes, deletedVolumes, changedVolumes = dn.UpdateVolumes(volumeInfos)
  238. for _, v := range newVolumes {
  239. t.RegisterVolumeLayout(v, dn)
  240. }
  241. for _, v := range deletedVolumes {
  242. t.UnRegisterVolumeLayout(v, dn)
  243. }
  244. for _, v := range changedVolumes {
  245. diskType := types.ToDiskType(v.DiskType)
  246. vl := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl, diskType)
  247. vl.EnsureCorrectWritables(&v)
  248. }
  249. return
  250. }
  251. func (t *Topology) IncrementalSyncDataNodeRegistration(newVolumes, deletedVolumes []*master_pb.VolumeShortInformationMessage, dn *DataNode) {
  252. var newVis, oldVis []storage.VolumeInfo
  253. for _, v := range newVolumes {
  254. vi, err := storage.NewVolumeInfoFromShort(v)
  255. if err != nil {
  256. glog.V(0).Infof("NewVolumeInfoFromShort %v: %v", v, err)
  257. continue
  258. }
  259. newVis = append(newVis, vi)
  260. }
  261. for _, v := range deletedVolumes {
  262. vi, err := storage.NewVolumeInfoFromShort(v)
  263. if err != nil {
  264. glog.V(0).Infof("NewVolumeInfoFromShort %v: %v", v, err)
  265. continue
  266. }
  267. oldVis = append(oldVis, vi)
  268. }
  269. dn.DeltaUpdateVolumes(newVis, oldVis)
  270. for _, vi := range newVis {
  271. t.RegisterVolumeLayout(vi, dn)
  272. }
  273. for _, vi := range oldVis {
  274. t.UnRegisterVolumeLayout(vi, dn)
  275. }
  276. return
  277. }
  278. func (t *Topology) DataNodeRegistration(dcName, rackName string, dn *DataNode) {
  279. if dn.Parent() != nil {
  280. return
  281. }
  282. // registration to topo
  283. dc := t.GetOrCreateDataCenter(dcName)
  284. rack := dc.GetOrCreateRack(rackName)
  285. rack.LinkChildNode(dn)
  286. glog.Infof("[%s] reLink To topo ", dn.Id())
  287. }
  288. func (t *Topology) DisableVacuum() {
  289. glog.V(0).Infof("DisableVacuum")
  290. t.isDisableVacuum = true
  291. }
  292. func (t *Topology) EnableVacuum() {
  293. glog.V(0).Infof("EnableVacuum")
  294. t.isDisableVacuum = false
  295. }