master_server.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. raftServerName = ms.Topo.HashicorpRaft.String()
  157. }
  158. ms.Topo.RaftServerAccessLock.Unlock()
  159. if ms.Topo.IsLeader() {
  160. glog.V(0).Infof("%s I am the leader!", raftServerName)
  161. } else {
  162. var raftServerLeader string
  163. ms.Topo.RaftServerAccessLock.RLock()
  164. if ms.Topo.RaftServer != nil {
  165. raftServerLeader = ms.Topo.RaftServer.Leader()
  166. } else if ms.Topo.HashicorpRaft != nil {
  167. raftServerName = ms.Topo.HashicorpRaft.String()
  168. raftServerLeaderAddr, _ := ms.Topo.HashicorpRaft.LeaderWithID()
  169. raftServerLeader = string(raftServerLeaderAddr)
  170. }
  171. ms.Topo.RaftServerAccessLock.RUnlock()
  172. glog.V(0).Infof("%s %s - is the leader.", raftServerName, raftServerLeader)
  173. }
  174. }
  175. func (ms *MasterServer) proxyToLeader(f http.HandlerFunc) http.HandlerFunc {
  176. return func(w http.ResponseWriter, r *http.Request) {
  177. if ms.Topo.IsLeader() {
  178. f(w, r)
  179. return
  180. }
  181. // get the current raft leader
  182. leaderAddr, _ := ms.Topo.MaybeLeader()
  183. raftServerLeader := leaderAddr.ToHttpAddress()
  184. if raftServerLeader == "" {
  185. f(w, r)
  186. return
  187. }
  188. ms.boundedLeaderChan <- 1
  189. defer func() { <-ms.boundedLeaderChan }()
  190. targetUrl, err := url.Parse("http://" + raftServerLeader)
  191. if err != nil {
  192. writeJsonError(w, r, http.StatusInternalServerError,
  193. fmt.Errorf("Leader URL http://%s Parse Error: %v", raftServerLeader, err))
  194. return
  195. }
  196. // proxy to leader
  197. glog.V(4).Infoln("proxying to leader", raftServerLeader)
  198. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  199. director := proxy.Director
  200. proxy.Director = func(req *http.Request) {
  201. actualHost, err := security.GetActualRemoteHost(req)
  202. if err == nil {
  203. req.Header.Set("HTTP_X_FORWARDED_FOR", actualHost)
  204. }
  205. director(req)
  206. }
  207. proxy.Transport = util_http.GetGlobalHttpClient().GetClientTransport()
  208. proxy.ServeHTTP(w, r)
  209. }
  210. }
  211. func (ms *MasterServer) startAdminScripts() {
  212. v := util.GetViper()
  213. adminScripts := v.GetString("master.maintenance.scripts")
  214. if adminScripts == "" {
  215. return
  216. }
  217. glog.V(0).Infof("adminScripts: %v", adminScripts)
  218. v.SetDefault("master.maintenance.sleep_minutes", 17)
  219. sleepMinutes := v.GetInt("master.maintenance.sleep_minutes")
  220. scriptLines := strings.Split(adminScripts, "\n")
  221. if !strings.Contains(adminScripts, "lock") {
  222. scriptLines = append(append([]string{}, "lock"), scriptLines...)
  223. scriptLines = append(scriptLines, "unlock")
  224. }
  225. masterAddress := string(ms.option.Master)
  226. var shellOptions shell.ShellOptions
  227. shellOptions.GrpcDialOption = security.LoadClientTLS(v, "grpc.master")
  228. shellOptions.Masters = &masterAddress
  229. shellOptions.Directory = "/"
  230. emptyFilerGroup := ""
  231. shellOptions.FilerGroup = &emptyFilerGroup
  232. commandEnv := shell.NewCommandEnv(&shellOptions)
  233. reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`)
  234. go commandEnv.MasterClient.KeepConnectedToMaster(context.Background())
  235. go func() {
  236. for {
  237. time.Sleep(time.Duration(sleepMinutes) * time.Minute)
  238. if ms.Topo.IsLeader() && ms.MasterClient.GetMaster(context.Background()) != "" {
  239. shellOptions.FilerAddress = ms.GetOneFiler(cluster.FilerGroupName(*shellOptions.FilerGroup))
  240. if shellOptions.FilerAddress == "" {
  241. continue
  242. }
  243. for _, line := range scriptLines {
  244. for _, c := range strings.Split(line, ";") {
  245. processEachCmd(reg, c, commandEnv)
  246. }
  247. }
  248. }
  249. }
  250. }()
  251. }
  252. func processEachCmd(reg *regexp.Regexp, line string, commandEnv *shell.CommandEnv) {
  253. cmds := reg.FindAllString(line, -1)
  254. if len(cmds) == 0 {
  255. return
  256. }
  257. args := make([]string, len(cmds[1:]))
  258. for i := range args {
  259. args[i] = strings.Trim(string(cmds[1+i]), "\"'")
  260. }
  261. cmd := cmds[0]
  262. for _, c := range shell.Commands {
  263. if c.Name() == cmd {
  264. glog.V(0).Infof("executing: %s %v", cmd, args)
  265. if err := c.Do(args, commandEnv, os.Stdout); err != nil {
  266. glog.V(0).Infof("error: %v", err)
  267. }
  268. }
  269. }
  270. }
  271. func (ms *MasterServer) createSequencer(option *MasterOption) sequence.Sequencer {
  272. var seq sequence.Sequencer
  273. v := util.GetViper()
  274. seqType := strings.ToLower(v.GetString(SequencerType))
  275. glog.V(1).Infof("[%s] : [%s]", SequencerType, seqType)
  276. switch strings.ToLower(seqType) {
  277. case "snowflake":
  278. var err error
  279. snowflakeId := v.GetInt(SequencerSnowflakeId)
  280. seq, err = sequence.NewSnowflakeSequencer(string(option.Master), snowflakeId)
  281. if err != nil {
  282. glog.Error(err)
  283. seq = nil
  284. }
  285. case "raft":
  286. fallthrough
  287. default:
  288. seq = sequence.NewMemorySequencer()
  289. }
  290. return seq
  291. }
  292. func (ms *MasterServer) OnPeerUpdate(update *master_pb.ClusterNodeUpdate, startFrom time.Time) {
  293. ms.Topo.RaftServerAccessLock.RLock()
  294. defer ms.Topo.RaftServerAccessLock.RUnlock()
  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. if ms.Topo.HashicorpRaft.State() != hashicorpRaft.Leader {
  302. return
  303. }
  304. if update.IsAdd {
  305. raftServerFound := false
  306. for _, server := range ms.Topo.HashicorpRaft.GetConfiguration().Configuration().Servers {
  307. if string(server.ID) == peerName {
  308. raftServerFound = true
  309. }
  310. }
  311. if !raftServerFound {
  312. glog.V(0).Infof("adding new raft server: %s", peerName)
  313. ms.Topo.HashicorpRaft.AddVoter(
  314. hashicorpRaft.ServerID(peerName),
  315. hashicorpRaft.ServerAddress(peerAddress.ToGrpcAddress()), 0, 0)
  316. }
  317. } else {
  318. pb.WithMasterClient(false, peerAddress, ms.grpcDialOption, true, func(client master_pb.SeaweedClient) error {
  319. ctx, cancel := context.WithTimeout(context.TODO(), 15*time.Second)
  320. defer cancel()
  321. if _, err := client.Ping(ctx, &master_pb.PingRequest{Target: string(peerAddress), TargetType: cluster.MasterType}); err != nil {
  322. glog.V(0).Infof("master %s didn't respond to pings. remove raft server", peerName)
  323. if err := ms.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  324. _, err := client.RaftRemoveServer(context.Background(), &master_pb.RaftRemoveServerRequest{
  325. Id: peerName,
  326. Force: false,
  327. })
  328. return err
  329. }); err != nil {
  330. glog.Warningf("failed removing old raft server: %v", err)
  331. return err
  332. }
  333. } else {
  334. glog.V(0).Infof("master %s successfully responded to ping", peerName)
  335. }
  336. return nil
  337. })
  338. }
  339. }
  340. func (ms *MasterServer) Shutdown() {
  341. if ms.Topo == nil || ms.Topo.HashicorpRaft == nil {
  342. return
  343. }
  344. if ms.Topo.HashicorpRaft.State() == hashicorpRaft.Leader {
  345. ms.Topo.HashicorpRaft.LeadershipTransfer()
  346. }
  347. ms.Topo.HashicorpRaft.Shutdown()
  348. }