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.

385 lines
12 KiB

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