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.

388 lines
12 KiB

7 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
9 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. } else if ms.Topo.RaftServer != nil && ms.Topo.RaftServer.Leader() != "" {
  183. ms.boundedLeaderChan <- 1
  184. defer func() { <-ms.boundedLeaderChan }()
  185. targetUrl, err := url.Parse("http://" + ms.Topo.RaftServer.Leader())
  186. if err != nil {
  187. writeJsonError(w, r, http.StatusInternalServerError,
  188. fmt.Errorf("Leader URL http://%s Parse Error: %v", ms.Topo.RaftServer.Leader(), err))
  189. return
  190. }
  191. glog.V(4).Infoln("proxying to leader", ms.Topo.RaftServer.Leader())
  192. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  193. director := proxy.Director
  194. proxy.Director = func(req *http.Request) {
  195. actualHost, err := security.GetActualRemoteHost(req)
  196. if err == nil {
  197. req.Header.Set("HTTP_X_FORWARDED_FOR", actualHost)
  198. }
  199. director(req)
  200. }
  201. proxy.Transport = util.Transport
  202. proxy.ServeHTTP(w, r)
  203. } else {
  204. // handle requests locally
  205. f(w, r)
  206. }
  207. }
  208. }
  209. func (ms *MasterServer) startAdminScripts() {
  210. v := util.GetViper()
  211. adminScripts := v.GetString("master.maintenance.scripts")
  212. if adminScripts == "" {
  213. return
  214. }
  215. glog.V(0).Infof("adminScripts: %v", adminScripts)
  216. v.SetDefault("master.maintenance.sleep_minutes", 17)
  217. sleepMinutes := v.GetInt("master.maintenance.sleep_minutes")
  218. scriptLines := strings.Split(adminScripts, "\n")
  219. if !strings.Contains(adminScripts, "lock") {
  220. scriptLines = append(append([]string{}, "lock"), scriptLines...)
  221. scriptLines = append(scriptLines, "unlock")
  222. }
  223. masterAddress := string(ms.option.Master)
  224. var shellOptions shell.ShellOptions
  225. shellOptions.GrpcDialOption = security.LoadClientTLS(v, "grpc.master")
  226. shellOptions.Masters = &masterAddress
  227. shellOptions.Directory = "/"
  228. commandEnv := shell.NewCommandEnv(&shellOptions)
  229. reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`)
  230. go commandEnv.MasterClient.KeepConnectedToMaster()
  231. go func() {
  232. commandEnv.MasterClient.WaitUntilConnected()
  233. for {
  234. time.Sleep(time.Duration(sleepMinutes) * time.Minute)
  235. if ms.Topo.IsLeader() {
  236. shellOptions.FilerAddress = ms.GetOneFiler()
  237. if shellOptions.FilerAddress == "" {
  238. continue
  239. }
  240. for _, line := range scriptLines {
  241. for _, c := range strings.Split(line, ";") {
  242. processEachCmd(reg, c, commandEnv)
  243. }
  244. }
  245. }
  246. }
  247. }()
  248. }
  249. func processEachCmd(reg *regexp.Regexp, line string, commandEnv *shell.CommandEnv) {
  250. cmds := reg.FindAllString(line, -1)
  251. if len(cmds) == 0 {
  252. return
  253. }
  254. args := make([]string, len(cmds[1:]))
  255. for i := range args {
  256. args[i] = strings.Trim(string(cmds[1+i]), "\"'")
  257. }
  258. cmd := strings.ToLower(cmds[0])
  259. for _, c := range shell.Commands {
  260. if c.Name() == cmd {
  261. glog.V(0).Infof("executing: %s %v", cmd, args)
  262. if err := c.Do(args, commandEnv, os.Stdout); err != nil {
  263. glog.V(0).Infof("error: %v", err)
  264. }
  265. }
  266. }
  267. }
  268. func (ms *MasterServer) createSequencer(option *MasterOption) sequence.Sequencer {
  269. var seq sequence.Sequencer
  270. v := util.GetViper()
  271. seqType := strings.ToLower(v.GetString(SequencerType))
  272. glog.V(1).Infof("[%s] : [%s]", SequencerType, seqType)
  273. switch strings.ToLower(seqType) {
  274. case "snowflake":
  275. var err error
  276. snowflakeId := v.GetInt(SequencerSnowflakeId)
  277. seq, err = sequence.NewSnowflakeSequencer(string(option.Master), snowflakeId)
  278. if err != nil {
  279. glog.Error(err)
  280. seq = nil
  281. }
  282. default:
  283. seq = sequence.NewMemorySequencer()
  284. }
  285. return seq
  286. }
  287. func (ms *MasterServer) OnPeerUpdate(update *master_pb.ClusterNodeUpdate) {
  288. if update.NodeType != cluster.MasterType || ms.Topo.HashicorpRaft == nil {
  289. return
  290. }
  291. glog.V(4).Infof("OnPeerUpdate: %+v", update)
  292. peerAddress := pb.ServerAddress(update.Address)
  293. peerName := string(peerAddress)
  294. isLeader := ms.Topo.HashicorpRaft.State() == hashicorpRaft.Leader
  295. if update.IsAdd {
  296. if isLeader {
  297. raftServerFound := false
  298. for _, server := range ms.Topo.HashicorpRaft.GetConfiguration().Configuration().Servers {
  299. if string(server.ID) == peerName {
  300. raftServerFound = true
  301. }
  302. }
  303. if !raftServerFound {
  304. glog.V(0).Infof("adding new raft server: %s", peerName)
  305. ms.Topo.HashicorpRaft.AddVoter(
  306. hashicorpRaft.ServerID(peerName),
  307. hashicorpRaft.ServerAddress(peerAddress.ToGrpcAddress()), 0, 0)
  308. }
  309. }
  310. if ms.onPeerUpdatDoneCnExist {
  311. ms.onPeerUpdatDoneCn <- peerName
  312. }
  313. } else if isLeader {
  314. go func(peerName string) {
  315. for {
  316. select {
  317. case <-time.After(RaftServerRemovalTime):
  318. err := ms.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  319. _, err := client.RaftRemoveServer(context.Background(), &master_pb.RaftRemoveServerRequest{
  320. Id: peerName,
  321. Force: false,
  322. })
  323. return err
  324. })
  325. if err != nil {
  326. glog.Warningf("failed to removing old raft server %s: %v", peerName, err)
  327. }
  328. return
  329. case peerDone := <-ms.onPeerUpdatDoneCn:
  330. if peerName == peerDone {
  331. return
  332. }
  333. }
  334. }
  335. }(peerName)
  336. ms.onPeerUpdatDoneCnExist = true
  337. }
  338. }