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.

318 lines
9.1 KiB

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