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.

418 lines
14 KiB

6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
9 months ago
5 years ago
3 years ago
6 years ago
9 months ago
6 years ago
6 years ago
6 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/http/httputil"
  7. "net/url"
  8. "os"
  9. "regexp"
  10. "strings"
  11. "sync"
  12. "time"
  13. "github.com/seaweedfs/seaweedfs/weed/stats"
  14. "github.com/seaweedfs/seaweedfs/weed/cluster"
  15. "github.com/seaweedfs/seaweedfs/weed/pb"
  16. "github.com/gorilla/mux"
  17. hashicorpRaft "github.com/hashicorp/raft"
  18. "github.com/seaweedfs/raft"
  19. "google.golang.org/grpc"
  20. "github.com/seaweedfs/seaweedfs/weed/glog"
  21. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  22. "github.com/seaweedfs/seaweedfs/weed/security"
  23. "github.com/seaweedfs/seaweedfs/weed/sequence"
  24. "github.com/seaweedfs/seaweedfs/weed/shell"
  25. "github.com/seaweedfs/seaweedfs/weed/topology"
  26. "github.com/seaweedfs/seaweedfs/weed/util"
  27. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  28. util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
  29. )
  30. const (
  31. SequencerType = "master.sequencer.type"
  32. SequencerSnowflakeId = "master.sequencer.sequencer_snowflake_id"
  33. )
  34. type MasterOption struct {
  35. Master pb.ServerAddress
  36. MetaFolder string
  37. VolumeSizeLimitMB uint32
  38. VolumePreallocate bool
  39. // PulseSeconds int
  40. DefaultReplicaPlacement string
  41. GarbageThreshold float64
  42. WhiteList []string
  43. DisableHttp bool
  44. MetricsAddress string
  45. MetricsIntervalSec int
  46. IsFollower bool
  47. }
  48. type MasterServer struct {
  49. master_pb.UnimplementedSeaweedServer
  50. option *MasterOption
  51. guard *security.Guard
  52. preallocateSize int64
  53. Topo *topology.Topology
  54. vg *topology.VolumeGrowth
  55. volumeGrowthRequestChan chan *topology.VolumeGrowRequest
  56. boundedLeaderChan chan int
  57. // notifying clients
  58. clientChansLock sync.RWMutex
  59. clientChans map[string]chan *master_pb.KeepConnectedResponse
  60. grpcDialOption grpc.DialOption
  61. MasterClient *wdclient.MasterClient
  62. adminLocks *AdminLocks
  63. Cluster *cluster.Cluster
  64. }
  65. func NewMasterServer(r *mux.Router, option *MasterOption, peers map[string]pb.ServerAddress) *MasterServer {
  66. v := util.GetViper()
  67. signingKey := v.GetString("jwt.signing.key")
  68. v.SetDefault("jwt.signing.expires_after_seconds", 10)
  69. expiresAfterSec := v.GetInt("jwt.signing.expires_after_seconds")
  70. readSigningKey := v.GetString("jwt.signing.read.key")
  71. v.SetDefault("jwt.signing.read.expires_after_seconds", 60)
  72. readExpiresAfterSec := v.GetInt("jwt.signing.read.expires_after_seconds")
  73. v.SetDefault("master.replication.treat_replication_as_minimums", false)
  74. replicationAsMin := v.GetBool("master.replication.treat_replication_as_minimums")
  75. v.SetDefault("master.volume_growth.copy_1", topology.VolumeGrowStrategy.Copy1Count)
  76. v.SetDefault("master.volume_growth.copy_2", topology.VolumeGrowStrategy.Copy2Count)
  77. v.SetDefault("master.volume_growth.copy_3", topology.VolumeGrowStrategy.Copy3Count)
  78. v.SetDefault("master.volume_growth.copy_other", topology.VolumeGrowStrategy.CopyOtherCount)
  79. v.SetDefault("master.volume_growth.threshold", topology.VolumeGrowStrategy.Threshold)
  80. topology.VolumeGrowStrategy.Copy1Count = v.GetUint32("master.volume_growth.copy_1")
  81. topology.VolumeGrowStrategy.Copy2Count = v.GetUint32("master.volume_growth.copy_2")
  82. topology.VolumeGrowStrategy.Copy3Count = v.GetUint32("master.volume_growth.copy_3")
  83. topology.VolumeGrowStrategy.CopyOtherCount = v.GetUint32("master.volume_growth.copy_other")
  84. topology.VolumeGrowStrategy.Threshold = v.GetFloat64("master.volume_growth.threshold")
  85. var preallocateSize int64
  86. if option.VolumePreallocate {
  87. preallocateSize = int64(option.VolumeSizeLimitMB) * (1 << 20)
  88. }
  89. grpcDialOption := security.LoadClientTLS(v, "grpc.master")
  90. ms := &MasterServer{
  91. option: option,
  92. preallocateSize: preallocateSize,
  93. volumeGrowthRequestChan: make(chan *topology.VolumeGrowRequest, 1<<6),
  94. clientChans: make(map[string]chan *master_pb.KeepConnectedResponse),
  95. grpcDialOption: grpcDialOption,
  96. MasterClient: wdclient.NewMasterClient(grpcDialOption, "", cluster.MasterType, option.Master, "", "", *pb.NewServiceDiscoveryFromMap(peers)),
  97. adminLocks: NewAdminLocks(),
  98. Cluster: cluster.NewCluster(),
  99. }
  100. ms.boundedLeaderChan = make(chan int, 16)
  101. ms.MasterClient.SetOnPeerUpdateFn(ms.OnPeerUpdate)
  102. seq := ms.createSequencer(option)
  103. if nil == seq {
  104. glog.Fatalf("create sequencer failed.")
  105. }
  106. ms.Topo = topology.NewTopology("topo", seq, uint64(ms.option.VolumeSizeLimitMB)*1024*1024, 5, replicationAsMin)
  107. ms.vg = topology.NewDefaultVolumeGrowth()
  108. glog.V(0).Infoln("Volume Size Limit is", ms.option.VolumeSizeLimitMB, "MB")
  109. ms.guard = security.NewGuard(ms.option.WhiteList, signingKey, expiresAfterSec, readSigningKey, readExpiresAfterSec)
  110. handleStaticResources2(r)
  111. r.HandleFunc("/", ms.proxyToLeader(ms.uiStatusHandler))
  112. r.HandleFunc("/ui/index.html", ms.uiStatusHandler)
  113. if !ms.option.DisableHttp {
  114. r.HandleFunc("/dir/assign", ms.proxyToLeader(ms.guard.WhiteList(ms.dirAssignHandler)))
  115. r.HandleFunc("/dir/lookup", ms.guard.WhiteList(ms.dirLookupHandler))
  116. r.HandleFunc("/dir/status", ms.proxyToLeader(ms.guard.WhiteList(ms.dirStatusHandler)))
  117. r.HandleFunc("/col/delete", ms.proxyToLeader(ms.guard.WhiteList(ms.collectionDeleteHandler)))
  118. r.HandleFunc("/vol/grow", ms.proxyToLeader(ms.guard.WhiteList(ms.volumeGrowHandler)))
  119. r.HandleFunc("/vol/status", ms.proxyToLeader(ms.guard.WhiteList(ms.volumeStatusHandler)))
  120. r.HandleFunc("/vol/vacuum", ms.proxyToLeader(ms.guard.WhiteList(ms.volumeVacuumHandler)))
  121. r.HandleFunc("/submit", ms.guard.WhiteList(ms.submitFromMasterServerHandler))
  122. /*
  123. r.HandleFunc("/stats/health", ms.guard.WhiteList(statsHealthHandler))
  124. r.HandleFunc("/stats/counter", ms.guard.WhiteList(statsCounterHandler))
  125. r.HandleFunc("/stats/memory", ms.guard.WhiteList(statsMemoryHandler))
  126. */
  127. r.HandleFunc("/{fileId}", ms.redirectHandler)
  128. }
  129. ms.Topo.StartRefreshWritableVolumes(
  130. ms.grpcDialOption,
  131. ms.option.GarbageThreshold,
  132. topology.VolumeGrowStrategy.Threshold,
  133. ms.preallocateSize,
  134. )
  135. ms.ProcessGrowRequest()
  136. if !option.IsFollower {
  137. ms.startAdminScripts()
  138. }
  139. return ms
  140. }
  141. func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) {
  142. var raftServerName string
  143. ms.Topo.RaftServerAccessLock.Lock()
  144. if raftServer.raftServer != nil {
  145. ms.Topo.RaftServer = raftServer.raftServer
  146. ms.Topo.RaftServer.AddEventListener(raft.LeaderChangeEventType, func(e raft.Event) {
  147. glog.V(0).Infof("leader change event: %+v => %+v", e.PrevValue(), e.Value())
  148. stats.MasterLeaderChangeCounter.WithLabelValues(fmt.Sprintf("%+v", e.Value())).Inc()
  149. if ms.Topo.RaftServer.Leader() != "" {
  150. glog.V(0).Infof("[%s] %s becomes leader.", ms.Topo.RaftServer.Name(), ms.Topo.RaftServer.Leader())
  151. }
  152. })
  153. raftServerName = fmt.Sprintf("[%s]", ms.Topo.RaftServer.Name())
  154. } else if raftServer.RaftHashicorp != nil {
  155. ms.Topo.HashicorpRaft = raftServer.RaftHashicorp
  156. leaderCh := raftServer.RaftHashicorp.LeaderCh()
  157. prevLeader, _ := ms.Topo.HashicorpRaft.LeaderWithID()
  158. raftServerName = ms.Topo.HashicorpRaft.String()
  159. go func() {
  160. for {
  161. select {
  162. case isLeader := <-leaderCh:
  163. ms.Topo.RaftServerAccessLock.RLock()
  164. leader, _ := ms.Topo.HashicorpRaft.LeaderWithID()
  165. ms.Topo.RaftServerAccessLock.RUnlock()
  166. glog.V(0).Infof("is leader %+v change event: %+v => %+v", isLeader, prevLeader, leader)
  167. stats.MasterLeaderChangeCounter.WithLabelValues(fmt.Sprintf("%+v", leader)).Inc()
  168. prevLeader = leader
  169. }
  170. }
  171. }()
  172. }
  173. ms.Topo.RaftServerAccessLock.Unlock()
  174. if ms.Topo.IsLeader() {
  175. glog.V(0).Infof("%s I am the leader!", raftServerName)
  176. } else {
  177. var raftServerLeader string
  178. ms.Topo.RaftServerAccessLock.RLock()
  179. if ms.Topo.RaftServer != nil {
  180. raftServerLeader = ms.Topo.RaftServer.Leader()
  181. } else if ms.Topo.HashicorpRaft != nil {
  182. raftServerName = ms.Topo.HashicorpRaft.String()
  183. raftServerLeaderAddr, _ := ms.Topo.HashicorpRaft.LeaderWithID()
  184. raftServerLeader = string(raftServerLeaderAddr)
  185. }
  186. ms.Topo.RaftServerAccessLock.RUnlock()
  187. glog.V(0).Infof("%s %s - is the leader.", raftServerName, raftServerLeader)
  188. }
  189. }
  190. func (ms *MasterServer) proxyToLeader(f http.HandlerFunc) http.HandlerFunc {
  191. return func(w http.ResponseWriter, r *http.Request) {
  192. if ms.Topo.IsLeader() {
  193. f(w, r)
  194. return
  195. }
  196. // get the current raft leader
  197. leaderAddr, _ := ms.Topo.MaybeLeader()
  198. raftServerLeader := leaderAddr.ToHttpAddress()
  199. if raftServerLeader == "" {
  200. f(w, r)
  201. return
  202. }
  203. ms.boundedLeaderChan <- 1
  204. defer func() { <-ms.boundedLeaderChan }()
  205. targetUrl, err := url.Parse("http://" + raftServerLeader)
  206. if err != nil {
  207. writeJsonError(w, r, http.StatusInternalServerError,
  208. fmt.Errorf("Leader URL http://%s Parse Error: %v", raftServerLeader, err))
  209. return
  210. }
  211. // proxy to leader
  212. glog.V(4).Infoln("proxying to leader", raftServerLeader)
  213. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  214. director := proxy.Director
  215. proxy.Director = func(req *http.Request) {
  216. actualHost, err := security.GetActualRemoteHost(req)
  217. if err == nil {
  218. req.Header.Set("HTTP_X_FORWARDED_FOR", actualHost)
  219. }
  220. director(req)
  221. }
  222. proxy.Transport = util_http.GetGlobalHttpClient().GetClientTransport()
  223. proxy.ServeHTTP(w, r)
  224. }
  225. }
  226. func (ms *MasterServer) startAdminScripts() {
  227. v := util.GetViper()
  228. adminScripts := v.GetString("master.maintenance.scripts")
  229. if adminScripts == "" {
  230. return
  231. }
  232. glog.V(0).Infof("adminScripts: %v", adminScripts)
  233. v.SetDefault("master.maintenance.sleep_minutes", 17)
  234. sleepMinutes := v.GetInt("master.maintenance.sleep_minutes")
  235. scriptLines := strings.Split(adminScripts, "\n")
  236. if !strings.Contains(adminScripts, "lock") {
  237. scriptLines = append(append([]string{}, "lock"), scriptLines...)
  238. scriptLines = append(scriptLines, "unlock")
  239. }
  240. masterAddress := string(ms.option.Master)
  241. var shellOptions shell.ShellOptions
  242. shellOptions.GrpcDialOption = security.LoadClientTLS(v, "grpc.master")
  243. shellOptions.Masters = &masterAddress
  244. shellOptions.Directory = "/"
  245. emptyFilerGroup := ""
  246. shellOptions.FilerGroup = &emptyFilerGroup
  247. commandEnv := shell.NewCommandEnv(&shellOptions)
  248. reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`)
  249. go commandEnv.MasterClient.KeepConnectedToMaster(context.Background())
  250. go func() {
  251. for {
  252. time.Sleep(time.Duration(sleepMinutes) * time.Minute)
  253. if ms.Topo.IsLeader() && ms.MasterClient.GetMaster(context.Background()) != "" {
  254. shellOptions.FilerAddress = ms.GetOneFiler(cluster.FilerGroupName(*shellOptions.FilerGroup))
  255. if shellOptions.FilerAddress == "" {
  256. continue
  257. }
  258. for _, line := range scriptLines {
  259. for _, c := range strings.Split(line, ";") {
  260. processEachCmd(reg, c, commandEnv)
  261. }
  262. }
  263. }
  264. }
  265. }()
  266. }
  267. func processEachCmd(reg *regexp.Regexp, line string, commandEnv *shell.CommandEnv) {
  268. cmds := reg.FindAllString(line, -1)
  269. if len(cmds) == 0 {
  270. return
  271. }
  272. args := make([]string, len(cmds[1:]))
  273. for i := range args {
  274. args[i] = strings.Trim(string(cmds[1+i]), "\"'")
  275. }
  276. cmd := cmds[0]
  277. for _, c := range shell.Commands {
  278. if c.Name() == cmd {
  279. glog.V(0).Infof("executing: %s %v", cmd, args)
  280. if err := c.Do(args, commandEnv, os.Stdout); err != nil {
  281. glog.V(0).Infof("error: %v", err)
  282. }
  283. }
  284. }
  285. }
  286. func (ms *MasterServer) createSequencer(option *MasterOption) sequence.Sequencer {
  287. var seq sequence.Sequencer
  288. v := util.GetViper()
  289. seqType := strings.ToLower(v.GetString(SequencerType))
  290. glog.V(1).Infof("[%s] : [%s]", SequencerType, seqType)
  291. switch strings.ToLower(seqType) {
  292. case "snowflake":
  293. var err error
  294. snowflakeId := v.GetInt(SequencerSnowflakeId)
  295. seq, err = sequence.NewSnowflakeSequencer(string(option.Master), snowflakeId)
  296. if err != nil {
  297. glog.Error(err)
  298. seq = nil
  299. }
  300. case "raft":
  301. fallthrough
  302. default:
  303. seq = sequence.NewMemorySequencer()
  304. }
  305. return seq
  306. }
  307. func (ms *MasterServer) OnPeerUpdate(update *master_pb.ClusterNodeUpdate, startFrom time.Time) {
  308. ms.Topo.RaftServerAccessLock.RLock()
  309. defer ms.Topo.RaftServerAccessLock.RUnlock()
  310. if update.NodeType != cluster.MasterType || ms.Topo.HashicorpRaft == nil {
  311. return
  312. }
  313. glog.V(4).Infof("OnPeerUpdate: %+v", update)
  314. peerAddress := pb.ServerAddress(update.Address)
  315. peerName := string(peerAddress)
  316. if ms.Topo.HashicorpRaft.State() != hashicorpRaft.Leader {
  317. return
  318. }
  319. if update.IsAdd {
  320. raftServerFound := false
  321. for _, server := range ms.Topo.HashicorpRaft.GetConfiguration().Configuration().Servers {
  322. if string(server.ID) == peerName {
  323. raftServerFound = true
  324. }
  325. }
  326. if !raftServerFound {
  327. glog.V(0).Infof("adding new raft server: %s", peerName)
  328. ms.Topo.HashicorpRaft.AddVoter(
  329. hashicorpRaft.ServerID(peerName),
  330. hashicorpRaft.ServerAddress(peerAddress.ToGrpcAddress()), 0, 0)
  331. }
  332. } else {
  333. pb.WithMasterClient(false, peerAddress, ms.grpcDialOption, true, func(client master_pb.SeaweedClient) error {
  334. ctx, cancel := context.WithTimeout(context.TODO(), 15*time.Second)
  335. defer cancel()
  336. if _, err := client.Ping(ctx, &master_pb.PingRequest{Target: string(peerAddress), TargetType: cluster.MasterType}); err != nil {
  337. glog.V(0).Infof("master %s didn't respond to pings. remove raft server", peerName)
  338. if err := ms.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  339. _, err := client.RaftRemoveServer(context.Background(), &master_pb.RaftRemoveServerRequest{
  340. Id: peerName,
  341. Force: false,
  342. })
  343. return err
  344. }); err != nil {
  345. glog.Warningf("failed removing old raft server: %v", err)
  346. return err
  347. }
  348. } else {
  349. glog.V(0).Infof("master %s successfully responded to ping", peerName)
  350. }
  351. return nil
  352. })
  353. }
  354. }
  355. func (ms *MasterServer) Shutdown() {
  356. if ms.Topo == nil || ms.Topo.HashicorpRaft == nil {
  357. return
  358. }
  359. if ms.Topo.HashicorpRaft.State() == hashicorpRaft.Leader {
  360. ms.Topo.HashicorpRaft.LeadershipTransfer()
  361. }
  362. ms.Topo.HashicorpRaft.Shutdown()
  363. }