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.

374 lines
11 KiB

6 years ago
6 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) IsChildLocked() (bool, error) {
  61. if t.IsLocked() {
  62. return true, errors.New("topology is locked")
  63. }
  64. for _, dcNode := range t.Children() {
  65. if dcNode.IsLocked() {
  66. return true, fmt.Errorf("topology child %s is locked", dcNode.String())
  67. }
  68. for _, rackNode := range dcNode.Children() {
  69. if rackNode.IsLocked() {
  70. return true, fmt.Errorf("dc %s child %s is locked", dcNode.String(), rackNode.String())
  71. }
  72. for _, dataNode := range rackNode.Children() {
  73. if dataNode.IsLocked() {
  74. return true, fmt.Errorf("rack %s child %s is locked", rackNode.String(), dataNode.Id())
  75. }
  76. }
  77. }
  78. }
  79. return false, nil
  80. }
  81. func (t *Topology) IsLeader() bool {
  82. t.RaftServerAccessLock.RLock()
  83. defer t.RaftServerAccessLock.RUnlock()
  84. if t.RaftServer != nil {
  85. if t.RaftServer.State() == raft.Leader {
  86. return true
  87. }
  88. if leader, err := t.Leader(); err == nil {
  89. if pb.ServerAddress(t.RaftServer.Name()) == leader {
  90. return true
  91. }
  92. }
  93. } else if t.HashicorpRaft != nil {
  94. if t.HashicorpRaft.State() == hashicorpRaft.Leader {
  95. return true
  96. }
  97. }
  98. return false
  99. }
  100. func (t *Topology) Leader() (l pb.ServerAddress, err error) {
  101. exponentialBackoff := backoff.NewExponentialBackOff()
  102. exponentialBackoff.InitialInterval = 100 * time.Millisecond
  103. exponentialBackoff.MaxElapsedTime = 20 * time.Second
  104. leaderNotSelected := errors.New("leader not selected yet")
  105. l, err = backoff.RetryWithData(
  106. func() (l pb.ServerAddress, err error) {
  107. l, err = t.MaybeLeader()
  108. if err == nil && l == "" {
  109. err = leaderNotSelected
  110. }
  111. return l, err
  112. },
  113. exponentialBackoff)
  114. if err == leaderNotSelected {
  115. l = ""
  116. }
  117. return l, err
  118. }
  119. func (t *Topology) MaybeLeader() (l pb.ServerAddress, err error) {
  120. t.RaftServerAccessLock.RLock()
  121. defer t.RaftServerAccessLock.RUnlock()
  122. if t.RaftServer != nil {
  123. l = pb.ServerAddress(t.RaftServer.Leader())
  124. } else if t.HashicorpRaft != nil {
  125. l = pb.ServerAddress(t.HashicorpRaft.Leader())
  126. } else {
  127. err = errors.New("Raft Server not ready yet!")
  128. }
  129. return
  130. }
  131. func (t *Topology) Lookup(collection string, vid needle.VolumeId) (dataNodes []*DataNode) {
  132. // maybe an issue if lots of collections?
  133. if collection == "" {
  134. for _, c := range t.collectionMap.Items() {
  135. if list := c.(*Collection).Lookup(vid); list != nil {
  136. return list
  137. }
  138. }
  139. } else {
  140. if c, ok := t.collectionMap.Find(collection); ok {
  141. return c.(*Collection).Lookup(vid)
  142. }
  143. }
  144. if locations, found := t.LookupEcShards(vid); found {
  145. for _, loc := range locations.Locations {
  146. dataNodes = append(dataNodes, loc...)
  147. }
  148. return dataNodes
  149. }
  150. return nil
  151. }
  152. func (t *Topology) NextVolumeId() (needle.VolumeId, error) {
  153. vid := t.GetMaxVolumeId()
  154. next := vid.Next()
  155. t.RaftServerAccessLock.RLock()
  156. defer t.RaftServerAccessLock.RUnlock()
  157. if t.RaftServer != nil {
  158. if _, err := t.RaftServer.Do(NewMaxVolumeIdCommand(next)); err != nil {
  159. return 0, err
  160. }
  161. } else if t.HashicorpRaft != nil {
  162. b, err := json.Marshal(NewMaxVolumeIdCommand(next))
  163. if err != nil {
  164. return 0, fmt.Errorf("failed marshal NewMaxVolumeIdCommand: %+v", err)
  165. }
  166. if future := t.HashicorpRaft.Apply(b, time.Second); future.Error() != nil {
  167. return 0, future.Error()
  168. }
  169. }
  170. return next, nil
  171. }
  172. func (t *Topology) PickForWrite(requestedCount uint64, option *VolumeGrowOption, volumeLayout *VolumeLayout) (fileId string, count uint64, volumeLocationList *VolumeLocationList, shouldGrow bool, err error) {
  173. var vid needle.VolumeId
  174. vid, count, volumeLocationList, shouldGrow, err = volumeLayout.PickForWrite(requestedCount, option)
  175. if err != nil {
  176. return "", 0, nil, shouldGrow, 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)
  177. }
  178. if volumeLocationList.Length() == 0 {
  179. return "", 0, nil, shouldGrow, fmt.Errorf("no writable volumes available for collection:%s replication:%s ttl:%s", option.Collection, option.ReplicaPlacement.String(), option.Ttl.String())
  180. }
  181. nextFileId := t.Sequence.NextFileId(requestedCount)
  182. fileId = needle.NewFileId(vid, nextFileId, rand.Uint32()).String()
  183. return fileId, count, volumeLocationList, shouldGrow, nil
  184. }
  185. func (t *Topology) GetVolumeLayout(collectionName string, rp *super_block.ReplicaPlacement, ttl *needle.TTL, diskType types.DiskType) *VolumeLayout {
  186. return t.collectionMap.Get(collectionName, func() interface{} {
  187. return NewCollection(collectionName, t.volumeSizeLimit, t.replicationAsMin)
  188. }).(*Collection).GetOrCreateVolumeLayout(rp, ttl, diskType)
  189. }
  190. func (t *Topology) ListCollections(includeNormalVolumes, includeEcVolumes bool) (ret []string) {
  191. mapOfCollections := make(map[string]bool)
  192. for _, c := range t.collectionMap.Items() {
  193. mapOfCollections[c.(*Collection).Name] = true
  194. }
  195. if includeEcVolumes {
  196. t.ecShardMapLock.RLock()
  197. for _, ecVolumeLocation := range t.ecShardMap {
  198. mapOfCollections[ecVolumeLocation.Collection] = true
  199. }
  200. t.ecShardMapLock.RUnlock()
  201. }
  202. for k := range mapOfCollections {
  203. ret = append(ret, k)
  204. }
  205. return ret
  206. }
  207. func (t *Topology) FindCollection(collectionName string) (*Collection, bool) {
  208. c, hasCollection := t.collectionMap.Find(collectionName)
  209. if !hasCollection {
  210. return nil, false
  211. }
  212. return c.(*Collection), hasCollection
  213. }
  214. func (t *Topology) DeleteCollection(collectionName string) {
  215. t.collectionMap.Delete(collectionName)
  216. }
  217. func (t *Topology) DeleteLayout(collectionName string, rp *super_block.ReplicaPlacement, ttl *needle.TTL, diskType types.DiskType) {
  218. collection, found := t.FindCollection(collectionName)
  219. if !found {
  220. return
  221. }
  222. collection.DeleteVolumeLayout(rp, ttl, diskType)
  223. if len(collection.storageType2VolumeLayout.Items()) == 0 {
  224. t.DeleteCollection(collectionName)
  225. }
  226. }
  227. func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  228. diskType := types.ToDiskType(v.DiskType)
  229. vl := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl, diskType)
  230. vl.RegisterVolume(&v, dn)
  231. vl.EnsureCorrectWritables(&v)
  232. }
  233. func (t *Topology) UnRegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  234. glog.Infof("removing volume info: %+v from %v", v, dn.id)
  235. diskType := types.ToDiskType(v.DiskType)
  236. volumeLayout := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl, diskType)
  237. volumeLayout.UnRegisterVolume(&v, dn)
  238. if volumeLayout.isEmpty() {
  239. t.DeleteLayout(v.Collection, v.ReplicaPlacement, v.Ttl, diskType)
  240. }
  241. }
  242. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  243. t.Lock()
  244. defer t.Unlock()
  245. for _, c := range t.children {
  246. dc := c.(*DataCenter)
  247. if string(dc.Id()) == dcName {
  248. return dc
  249. }
  250. }
  251. dc := NewDataCenter(dcName)
  252. t.doLinkChildNode(dc)
  253. return dc
  254. }
  255. func (t *Topology) SyncDataNodeRegistration(volumes []*master_pb.VolumeInformationMessage, dn *DataNode) (newVolumes, deletedVolumes []storage.VolumeInfo) {
  256. // convert into in memory struct storage.VolumeInfo
  257. var volumeInfos []storage.VolumeInfo
  258. for _, v := range volumes {
  259. if vi, err := storage.NewVolumeInfo(v); err == nil {
  260. volumeInfos = append(volumeInfos, vi)
  261. } else {
  262. glog.V(0).Infof("Fail to convert joined volume information: %v", err)
  263. }
  264. }
  265. // find out the delta volumes
  266. var changedVolumes []storage.VolumeInfo
  267. newVolumes, deletedVolumes, changedVolumes = dn.UpdateVolumes(volumeInfos)
  268. for _, v := range newVolumes {
  269. t.RegisterVolumeLayout(v, dn)
  270. }
  271. for _, v := range deletedVolumes {
  272. t.UnRegisterVolumeLayout(v, dn)
  273. }
  274. for _, v := range changedVolumes {
  275. diskType := types.ToDiskType(v.DiskType)
  276. vl := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl, diskType)
  277. vl.EnsureCorrectWritables(&v)
  278. }
  279. return
  280. }
  281. func (t *Topology) IncrementalSyncDataNodeRegistration(newVolumes, deletedVolumes []*master_pb.VolumeShortInformationMessage, dn *DataNode) {
  282. var newVis, oldVis []storage.VolumeInfo
  283. for _, v := range newVolumes {
  284. vi, err := storage.NewVolumeInfoFromShort(v)
  285. if err != nil {
  286. glog.V(0).Infof("NewVolumeInfoFromShort %v: %v", v, err)
  287. continue
  288. }
  289. newVis = append(newVis, vi)
  290. }
  291. for _, v := range deletedVolumes {
  292. vi, err := storage.NewVolumeInfoFromShort(v)
  293. if err != nil {
  294. glog.V(0).Infof("NewVolumeInfoFromShort %v: %v", v, err)
  295. continue
  296. }
  297. oldVis = append(oldVis, vi)
  298. }
  299. dn.DeltaUpdateVolumes(newVis, oldVis)
  300. for _, vi := range newVis {
  301. t.RegisterVolumeLayout(vi, dn)
  302. }
  303. for _, vi := range oldVis {
  304. t.UnRegisterVolumeLayout(vi, dn)
  305. }
  306. return
  307. }
  308. func (t *Topology) DataNodeRegistration(dcName, rackName string, dn *DataNode) {
  309. if dn.Parent() != nil {
  310. return
  311. }
  312. // registration to topo
  313. dc := t.GetOrCreateDataCenter(dcName)
  314. rack := dc.GetOrCreateRack(rackName)
  315. rack.LinkChildNode(dn)
  316. glog.Infof("[%s] reLink To topo ", dn.Id())
  317. }
  318. func (t *Topology) DisableVacuum() {
  319. glog.V(0).Infof("DisableVacuum")
  320. t.isDisableVacuum = true
  321. }
  322. func (t *Topology) EnableVacuum() {
  323. glog.V(0).Infof("EnableVacuum")
  324. t.isDisableVacuum = false
  325. }