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.

395 lines
12 KiB

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