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.

274 lines
11 KiB

11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package main
  2. import (
  3. "net/http"
  4. "os"
  5. "runtime"
  6. "runtime/pprof"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "time"
  11. "github.com/chrislusf/weed-fs/go/glog"
  12. "github.com/chrislusf/weed-fs/go/util"
  13. "github.com/chrislusf/weed-fs/go/weed/weed_server"
  14. "github.com/gorilla/mux"
  15. )
  16. type ServerOptions struct {
  17. cpuprofile *string
  18. }
  19. var (
  20. serverOptions ServerOptions
  21. filerOptions FilerOptions
  22. )
  23. func init() {
  24. cmdServer.Run = runServer // break init cycle
  25. }
  26. var cmdServer = &Command{
  27. UsageLine: "server -port=8080 -dir=/tmp -volume.max=5 -ip=server_name",
  28. Short: "start a server, including volume server, and automatically elect a master server",
  29. Long: `start both a volume server to provide storage spaces
  30. and a master server to provide volume=>location mapping service and sequence number of file ids
  31. This is provided as a convenient way to start both volume server and master server.
  32. The servers are exactly the same as starting them separately.
  33. So other volume servers can use this embedded master server also.
  34. Optionally, one filer server can be started. Logically, filer servers should not be in a cluster.
  35. They run with meta data on disk, not shared. So each filer server is different.
  36. `,
  37. }
  38. var (
  39. serverIp = cmdServer.Flag.String("ip", "localhost", "ip or server name")
  40. serverPublicUrl = cmdServer.Flag.String("publicUrl", "", "publicly accessible address")
  41. serverBindIp = cmdServer.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  42. serverMaxCpu = cmdServer.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  43. serverTimeout = cmdServer.Flag.Int("idleTimeout", 10, "connection idle seconds")
  44. serverDataCenter = cmdServer.Flag.String("dataCenter", "", "current volume server's data center name")
  45. serverRack = cmdServer.Flag.String("rack", "", "current volume server's rack name")
  46. serverWhiteListOption = cmdServer.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  47. serverPeers = cmdServer.Flag.String("master.peers", "", "other master nodes in comma separated ip:masterPort list")
  48. serverSecureKey = cmdServer.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)")
  49. serverGarbageThreshold = cmdServer.Flag.String("garbageThreshold", "0.3", "threshold to vacuum and reclaim spaces")
  50. masterPort = cmdServer.Flag.Int("master.port", 9333, "master server http listen port")
  51. masterMetaFolder = cmdServer.Flag.String("master.dir", "", "data directory to store meta data, default to same as -dir specified")
  52. masterVolumeSizeLimitMB = cmdServer.Flag.Uint("master.volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  53. masterConfFile = cmdServer.Flag.String("master.conf", "/etc/weedfs/weedfs.conf", "xml configuration file")
  54. masterDefaultReplicaPlacement = cmdServer.Flag.String("master.defaultReplicaPlacement", "000", "Default replication type if not specified.")
  55. volumePort = cmdServer.Flag.Int("volume.port", 8080, "volume server http listen port")
  56. volumePublicPort = cmdServer.Flag.Int("volume.port.public", 0, "volume server public port")
  57. volumeDataFolders = cmdServer.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
  58. volumeMaxDataVolumeCounts = cmdServer.Flag.String("volume.max", "7", "maximum numbers of volumes, count[,count]...")
  59. volumePulse = cmdServer.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  60. volumeFixJpgOrientation = cmdServer.Flag.Bool("volume.images.fix.orientation", true, "Adjust jpg orientation when uploading.")
  61. isStartingFiler = cmdServer.Flag.Bool("filer", false, "whether to start filer")
  62. serverWhiteList []string
  63. )
  64. func init() {
  65. serverOptions.cpuprofile = cmdServer.Flag.String("cpuprofile", "", "cpu profile output file")
  66. filerOptions.master = cmdServer.Flag.String("filer.master", "", "default to current master server")
  67. filerOptions.collection = cmdServer.Flag.String("filer.collection", "", "all data will be stored in this collection")
  68. filerOptions.port = cmdServer.Flag.Int("filer.port", 8888, "filer server http listen port")
  69. filerOptions.dir = cmdServer.Flag.String("filer.dir", "", "directory to store meta data, default to a 'filer' sub directory of what -mdir is specified")
  70. filerOptions.defaultReplicaPlacement = cmdServer.Flag.String("filer.defaultReplicaPlacement", "", "Default replication type if not specified during runtime.")
  71. filerOptions.redirectOnRead = cmdServer.Flag.Bool("filer.redirectOnRead", false, "whether proxy or redirect to volume server during file GET request")
  72. filerOptions.cassandra_server = cmdServer.Flag.String("filer.cassandra.server", "", "host[:port] of the cassandra server")
  73. filerOptions.cassandra_keyspace = cmdServer.Flag.String("filer.cassandra.keyspace", "seaweed", "keyspace of the cassandra server")
  74. filerOptions.redis_server = cmdServer.Flag.String("filer.redis.server", "", "host:port of the redis server, e.g., 127.0.0.1:6379")
  75. filerOptions.redis_database = cmdServer.Flag.Int("filer.redis.database", 0, "the database on the redis server")
  76. }
  77. func runServer(cmd *Command, args []string) bool {
  78. filerOptions.secretKey = serverSecureKey
  79. if *serverOptions.cpuprofile != "" {
  80. f, err := os.Create(*serverOptions.cpuprofile)
  81. if err != nil {
  82. glog.Fatal(err)
  83. }
  84. pprof.StartCPUProfile(f)
  85. defer pprof.StopCPUProfile()
  86. }
  87. if *filerOptions.redirectOnRead {
  88. *isStartingFiler = true
  89. }
  90. *filerOptions.master = *serverIp + ":" + strconv.Itoa(*masterPort)
  91. if *filerOptions.defaultReplicaPlacement == "" {
  92. *filerOptions.defaultReplicaPlacement = *masterDefaultReplicaPlacement
  93. }
  94. if *volumePublicPort == 0 {
  95. *volumePublicPort = *volumePort
  96. }
  97. if *serverMaxCpu < 1 {
  98. *serverMaxCpu = runtime.NumCPU()
  99. }
  100. runtime.GOMAXPROCS(*serverMaxCpu)
  101. folders := strings.Split(*volumeDataFolders, ",")
  102. maxCountStrings := strings.Split(*volumeMaxDataVolumeCounts, ",")
  103. maxCounts := make([]int, 0)
  104. for _, maxString := range maxCountStrings {
  105. if max, e := strconv.Atoi(maxString); e == nil {
  106. maxCounts = append(maxCounts, max)
  107. } else {
  108. glog.Fatalf("The max specified in -max not a valid number %s", maxString)
  109. }
  110. }
  111. if len(folders) != len(maxCounts) {
  112. glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
  113. }
  114. for _, folder := range folders {
  115. if err := util.TestFolderWritable(folder); err != nil {
  116. glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
  117. }
  118. }
  119. if *masterMetaFolder == "" {
  120. *masterMetaFolder = folders[0]
  121. }
  122. if *isStartingFiler {
  123. if *filerOptions.dir == "" {
  124. *filerOptions.dir = *masterMetaFolder + "/filer"
  125. os.MkdirAll(*filerOptions.dir, 0700)
  126. }
  127. if err := util.TestFolderWritable(*filerOptions.dir); err != nil {
  128. glog.Fatalf("Check Mapping Meta Folder (-filer.dir=\"%s\") Writable: %s", *filerOptions.dir, err)
  129. }
  130. }
  131. if err := util.TestFolderWritable(*masterMetaFolder); err != nil {
  132. glog.Fatalf("Check Meta Folder (-mdir=\"%s\") Writable: %s", *masterMetaFolder, err)
  133. }
  134. if *serverWhiteListOption != "" {
  135. serverWhiteList = strings.Split(*serverWhiteListOption, ",")
  136. }
  137. if *isStartingFiler {
  138. go func() {
  139. r := http.NewServeMux()
  140. _, nfs_err := weed_server.NewFilerServer(r, *filerOptions.port, *filerOptions.master, *filerOptions.dir, *filerOptions.collection,
  141. *filerOptions.defaultReplicaPlacement, *filerOptions.redirectOnRead,
  142. *filerOptions.secretKey,
  143. "", "",
  144. "", 0,
  145. )
  146. if nfs_err != nil {
  147. glog.Fatalf("Filer startup error: %v", nfs_err)
  148. }
  149. glog.V(0).Infoln("Start Seaweed Filer", util.VERSION, "at port", strconv.Itoa(*filerOptions.port))
  150. filerListener, e := util.NewListener(
  151. ":"+strconv.Itoa(*filerOptions.port),
  152. time.Duration(10)*time.Second,
  153. )
  154. if e != nil {
  155. glog.Fatalf("Filer listener error: %v", e)
  156. }
  157. if e := http.Serve(filerListener, r); e != nil {
  158. glog.Fatalf("Filer Fail to serve: %v", e)
  159. }
  160. }()
  161. }
  162. var raftWaitForMaster sync.WaitGroup
  163. var volumeWait sync.WaitGroup
  164. raftWaitForMaster.Add(1)
  165. volumeWait.Add(1)
  166. go func() {
  167. r := mux.NewRouter()
  168. ms := weed_server.NewMasterServer(r, *masterPort, *masterMetaFolder,
  169. *masterVolumeSizeLimitMB, *volumePulse, *masterConfFile, *masterDefaultReplicaPlacement, *serverGarbageThreshold,
  170. serverWhiteList, *serverSecureKey,
  171. )
  172. glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*masterPort))
  173. masterListener, e := util.NewListener(*serverBindIp+":"+strconv.Itoa(*masterPort), time.Duration(*serverTimeout)*time.Second)
  174. if e != nil {
  175. glog.Fatalf("Master startup error: %v", e)
  176. }
  177. go func() {
  178. raftWaitForMaster.Wait()
  179. time.Sleep(100 * time.Millisecond)
  180. myAddress := *serverIp + ":" + strconv.Itoa(*masterPort)
  181. var peers []string
  182. if *serverPeers != "" {
  183. peers = strings.Split(*serverPeers, ",")
  184. }
  185. raftServer := weed_server.NewRaftServer(r, peers, myAddress, *masterMetaFolder, ms.Topo, *volumePulse)
  186. ms.SetRaftServer(raftServer)
  187. volumeWait.Done()
  188. }()
  189. raftWaitForMaster.Done()
  190. if e := http.Serve(masterListener, r); e != nil {
  191. glog.Fatalf("Master Fail to serve:%s", e.Error())
  192. }
  193. }()
  194. volumeWait.Wait()
  195. time.Sleep(100 * time.Millisecond)
  196. if *volumePublicPort == 0 {
  197. *volumePublicPort = *volumePort
  198. }
  199. isSeperatedPublicPort := *volumePublicPort != *volumePort
  200. volumeMux := http.NewServeMux()
  201. publicVolumeMux := volumeMux
  202. if isSeperatedPublicPort {
  203. publicVolumeMux = http.NewServeMux()
  204. }
  205. volumeServer := weed_server.NewVolumeServer(volumeMux, publicVolumeMux,
  206. *serverIp, *volumePort, *serverPublicUrl,
  207. folders, maxCounts,
  208. *serverIp+":"+strconv.Itoa(*masterPort), *volumePulse, *serverDataCenter, *serverRack,
  209. serverWhiteList, *volumeFixJpgOrientation,
  210. )
  211. glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*volumePort))
  212. volumeListener, eListen := util.NewListener(
  213. *serverBindIp+":"+strconv.Itoa(*volumePort),
  214. time.Duration(*serverTimeout)*time.Second,
  215. )
  216. if eListen != nil {
  217. glog.Fatalf("Volume server listener error: %v", eListen)
  218. }
  219. if isSeperatedPublicPort {
  220. publicListeningAddress := *serverIp + ":" + strconv.Itoa(*volumePublicPort)
  221. glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "public at", publicListeningAddress)
  222. publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*serverTimeout)*time.Second)
  223. if e != nil {
  224. glog.Fatalf("Volume server listener error:%v", e)
  225. }
  226. go func() {
  227. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  228. glog.Fatalf("Volume server fail to serve public: %v", e)
  229. }
  230. }()
  231. }
  232. OnInterrupt(func() {
  233. volumeServer.Shutdown()
  234. pprof.StopCPUProfile()
  235. })
  236. if e := http.Serve(volumeListener, volumeMux); e != nil {
  237. glog.Fatalf("Volume server fail to serve:%v", e)
  238. }
  239. return true
  240. }