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.

259 lines
7.1 KiB

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