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.

289 lines
12 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/seaweedfs/go/glog"
  12. "github.com/chrislusf/seaweedfs/go/storage"
  13. "github.com/chrislusf/seaweedfs/go/util"
  14. "github.com/chrislusf/seaweedfs/go/weed/weed_server"
  15. "github.com/gorilla/mux"
  16. )
  17. type ServerOptions struct {
  18. cpuprofile *string
  19. }
  20. var (
  21. serverOptions ServerOptions
  22. filerOptions FilerOptions
  23. )
  24. func init() {
  25. cmdServer.Run = runServer // break init cycle
  26. }
  27. var cmdServer = &Command{
  28. UsageLine: "server -port=8080 -dir=/tmp -volume.max=5 -ip=server_name",
  29. Short: "start a server, including volume server, and automatically elect a master server",
  30. Long: `start both a volume server to provide storage spaces
  31. and a master server to provide volume=>location mapping service and sequence number of file ids
  32. This is provided as a convenient way to start both volume server and master server.
  33. The servers are exactly the same as starting them separately.
  34. So other volume servers can use this embedded master server also.
  35. Optionally, one filer server can be started. Logically, filer servers should not be in a cluster.
  36. They run with meta data on disk, not shared. So each filer server is different.
  37. `,
  38. }
  39. var (
  40. serverIp = cmdServer.Flag.String("ip", "localhost", "ip or server name")
  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. volumeIndexType = cmdServer.Flag.String("volume.index", "memory", "Choose [memory|leveldb|boltdb] mode for memory~performance balance.")
  61. volumeFixJpgOrientation = cmdServer.Flag.Bool("volume.images.fix.orientation", true, "Adjust jpg orientation when uploading.")
  62. volumeServerPublicUrl = cmdServer.Flag.String("volume.publicUrl", "", "publicly accessible address")
  63. isStartingFiler = cmdServer.Flag.Bool("filer", false, "whether to start filer")
  64. serverWhiteList []string
  65. )
  66. func init() {
  67. serverOptions.cpuprofile = cmdServer.Flag.String("cpuprofile", "", "cpu profile output file")
  68. filerOptions.master = cmdServer.Flag.String("filer.master", "", "default to current master server")
  69. filerOptions.collection = cmdServer.Flag.String("filer.collection", "", "all data will be stored in this collection")
  70. filerOptions.port = cmdServer.Flag.Int("filer.port", 8888, "filer server http listen port")
  71. filerOptions.dir = cmdServer.Flag.String("filer.dir", "", "directory to store meta data, default to a 'filer' sub directory of what -mdir is specified")
  72. filerOptions.defaultReplicaPlacement = cmdServer.Flag.String("filer.defaultReplicaPlacement", "", "Default replication type if not specified during runtime.")
  73. filerOptions.redirectOnRead = cmdServer.Flag.Bool("filer.redirectOnRead", false, "whether proxy or redirect to volume server during file GET request")
  74. filerOptions.disableDirListing = cmdServer.Flag.Bool("filer.disableDirListing", false, "turn off directory listing")
  75. filerOptions.cassandra_server = cmdServer.Flag.String("filer.cassandra.server", "", "host[:port] of the cassandra server")
  76. filerOptions.cassandra_keyspace = cmdServer.Flag.String("filer.cassandra.keyspace", "seaweed", "keyspace of the cassandra server")
  77. filerOptions.redis_server = cmdServer.Flag.String("filer.redis.server", "", "host:port of the redis server, e.g., 127.0.0.1:6379")
  78. filerOptions.redis_database = cmdServer.Flag.Int("filer.redis.database", 0, "the database on the redis server")
  79. }
  80. func runServer(cmd *Command, args []string) bool {
  81. filerOptions.secretKey = serverSecureKey
  82. if *serverOptions.cpuprofile != "" {
  83. f, err := os.Create(*serverOptions.cpuprofile)
  84. if err != nil {
  85. glog.Fatal(err)
  86. }
  87. pprof.StartCPUProfile(f)
  88. defer pprof.StopCPUProfile()
  89. }
  90. if *filerOptions.redirectOnRead {
  91. *isStartingFiler = true
  92. }
  93. *filerOptions.master = *serverIp + ":" + strconv.Itoa(*masterPort)
  94. if *filerOptions.defaultReplicaPlacement == "" {
  95. *filerOptions.defaultReplicaPlacement = *masterDefaultReplicaPlacement
  96. }
  97. if *volumePublicPort == 0 {
  98. *volumePublicPort = *volumePort
  99. }
  100. if *serverMaxCpu < 1 {
  101. *serverMaxCpu = runtime.NumCPU()
  102. }
  103. runtime.GOMAXPROCS(*serverMaxCpu)
  104. folders := strings.Split(*volumeDataFolders, ",")
  105. maxCountStrings := strings.Split(*volumeMaxDataVolumeCounts, ",")
  106. var maxCounts []int
  107. for _, maxString := range maxCountStrings {
  108. if max, e := strconv.Atoi(maxString); e == nil {
  109. maxCounts = append(maxCounts, max)
  110. } else {
  111. glog.Fatalf("The max specified in -max not a valid number %s", maxString)
  112. }
  113. }
  114. if len(folders) != len(maxCounts) {
  115. glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
  116. }
  117. for _, folder := range folders {
  118. if err := util.TestFolderWritable(folder); err != nil {
  119. glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
  120. }
  121. }
  122. if *masterMetaFolder == "" {
  123. *masterMetaFolder = folders[0]
  124. }
  125. if *isStartingFiler {
  126. if *filerOptions.dir == "" {
  127. *filerOptions.dir = *masterMetaFolder + "/filer"
  128. os.MkdirAll(*filerOptions.dir, 0700)
  129. }
  130. if err := util.TestFolderWritable(*filerOptions.dir); err != nil {
  131. glog.Fatalf("Check Mapping Meta Folder (-filer.dir=\"%s\") Writable: %s", *filerOptions.dir, err)
  132. }
  133. }
  134. if err := util.TestFolderWritable(*masterMetaFolder); err != nil {
  135. glog.Fatalf("Check Meta Folder (-mdir=\"%s\") Writable: %s", *masterMetaFolder, err)
  136. }
  137. if *serverWhiteListOption != "" {
  138. serverWhiteList = strings.Split(*serverWhiteListOption, ",")
  139. }
  140. if *isStartingFiler {
  141. go func() {
  142. r := http.NewServeMux()
  143. _, nfs_err := weed_server.NewFilerServer(r, *filerOptions.port, *filerOptions.master, *filerOptions.dir, *filerOptions.collection,
  144. *filerOptions.defaultReplicaPlacement,
  145. *filerOptions.redirectOnRead, *filerOptions.disableDirListing,
  146. *filerOptions.secretKey,
  147. "", "",
  148. "", 0,
  149. )
  150. if nfs_err != nil {
  151. glog.Fatalf("Filer startup error: %v", nfs_err)
  152. }
  153. glog.V(0).Infoln("Start Seaweed Filer", util.VERSION, "at port", strconv.Itoa(*filerOptions.port))
  154. filerListener, e := util.NewListener(
  155. ":"+strconv.Itoa(*filerOptions.port),
  156. time.Duration(10)*time.Second,
  157. )
  158. if e != nil {
  159. glog.Fatalf("Filer listener error: %v", e)
  160. }
  161. if e := http.Serve(filerListener, r); e != nil {
  162. glog.Fatalf("Filer Fail to serve: %v", e)
  163. }
  164. }()
  165. }
  166. var raftWaitForMaster sync.WaitGroup
  167. var volumeWait sync.WaitGroup
  168. raftWaitForMaster.Add(1)
  169. volumeWait.Add(1)
  170. go func() {
  171. r := mux.NewRouter()
  172. ms := weed_server.NewMasterServer(r, *masterPort, *masterMetaFolder,
  173. *masterVolumeSizeLimitMB, *volumePulse, *masterConfFile, *masterDefaultReplicaPlacement, *serverGarbageThreshold,
  174. serverWhiteList, *serverSecureKey,
  175. )
  176. glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*masterPort))
  177. masterListener, e := util.NewListener(*serverBindIp+":"+strconv.Itoa(*masterPort), time.Duration(*serverTimeout)*time.Second)
  178. if e != nil {
  179. glog.Fatalf("Master startup error: %v", e)
  180. }
  181. go func() {
  182. raftWaitForMaster.Wait()
  183. time.Sleep(100 * time.Millisecond)
  184. myAddress := *serverIp + ":" + strconv.Itoa(*masterPort)
  185. var peers []string
  186. if *serverPeers != "" {
  187. peers = strings.Split(*serverPeers, ",")
  188. }
  189. raftServer := weed_server.NewRaftServer(r, peers, myAddress, *masterMetaFolder, ms.Topo, *volumePulse)
  190. ms.SetRaftServer(raftServer)
  191. volumeWait.Done()
  192. }()
  193. raftWaitForMaster.Done()
  194. if e := http.Serve(masterListener, r); e != nil {
  195. glog.Fatalf("Master Fail to serve:%s", e.Error())
  196. }
  197. }()
  198. volumeWait.Wait()
  199. time.Sleep(100 * time.Millisecond)
  200. if *volumePublicPort == 0 {
  201. *volumePublicPort = *volumePort
  202. }
  203. if *volumeServerPublicUrl == "" {
  204. *volumeServerPublicUrl = *serverIp + ":" + strconv.Itoa(*volumePublicPort)
  205. }
  206. isSeperatedPublicPort := *volumePublicPort != *volumePort
  207. volumeMux := http.NewServeMux()
  208. publicVolumeMux := volumeMux
  209. if isSeperatedPublicPort {
  210. publicVolumeMux = http.NewServeMux()
  211. }
  212. volumeNeedleMapKind := storage.NeedleMapInMemory
  213. switch *volumeIndexType {
  214. case "leveldb":
  215. volumeNeedleMapKind = storage.NeedleMapLevelDb
  216. case "boltdb":
  217. volumeNeedleMapKind = storage.NeedleMapBoltDb
  218. }
  219. volumeServer := weed_server.NewVolumeServer(volumeMux, publicVolumeMux,
  220. *serverIp, *volumePort, *volumeServerPublicUrl,
  221. folders, maxCounts,
  222. volumeNeedleMapKind,
  223. *serverIp+":"+strconv.Itoa(*masterPort), *volumePulse, *serverDataCenter, *serverRack,
  224. serverWhiteList, *volumeFixJpgOrientation,
  225. )
  226. glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*volumePort))
  227. volumeListener, eListen := util.NewListener(
  228. *serverBindIp+":"+strconv.Itoa(*volumePort),
  229. time.Duration(*serverTimeout)*time.Second,
  230. )
  231. if eListen != nil {
  232. glog.Fatalf("Volume server listener error: %v", eListen)
  233. }
  234. if isSeperatedPublicPort {
  235. publicListeningAddress := *serverIp + ":" + strconv.Itoa(*volumePublicPort)
  236. glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "public at", publicListeningAddress)
  237. publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*serverTimeout)*time.Second)
  238. if e != nil {
  239. glog.Fatalf("Volume server listener error:%v", e)
  240. }
  241. go func() {
  242. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  243. glog.Fatalf("Volume server fail to serve public: %v", e)
  244. }
  245. }()
  246. }
  247. OnInterrupt(func() {
  248. volumeServer.Shutdown()
  249. pprof.StopCPUProfile()
  250. })
  251. if e := http.Serve(volumeListener, volumeMux); e != nil {
  252. glog.Fatalf("Volume server fail to serve:%v", e)
  253. }
  254. return true
  255. }