seaweedfs/go/weed/volume.go

93 lines
3.3 KiB
Go
Raw Normal View History

package main
import (
"code.google.com/p/weed-fs/go/glog"
"code.google.com/p/weed-fs/go/util"
"code.google.com/p/weed-fs/go/weed/weed_server"
2012-08-24 05:46:54 +00:00
"net/http"
"os"
"runtime"
2012-08-24 05:46:54 +00:00
"strconv"
"strings"
"time"
)
func init() {
2012-08-24 05:46:54 +00:00
cmdVolume.Run = runVolume // break init cycle
}
var cmdVolume = &Command{
2012-09-26 08:55:56 +00:00
UsageLine: "volume -port=8080 -dir=/tmp -max=5 -ip=server_name -mserver=localhost:9333",
2012-08-24 05:46:54 +00:00
Short: "start a volume server",
Long: `start a volume server to provide storage spaces
`,
}
var (
2013-08-13 16:22:06 +00:00
vport = cmdVolume.Flag.Int("port", 8080, "http listen port")
volumeFolders = cmdVolume.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
2013-08-13 16:22:06 +00:00
maxVolumeCounts = cmdVolume.Flag.String("max", "7", "maximum numbers of volumes, count[,count]...")
ip = cmdVolume.Flag.String("ip", "localhost", "ip or server name")
publicUrl = cmdVolume.Flag.String("publicUrl", "", "Publicly accessible <ip|server_name>:<port>")
masterNode = cmdVolume.Flag.String("mserver", "localhost:9333", "master server location")
2014-02-05 19:47:26 +00:00
vpulse = cmdVolume.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats, must be smaller than or equal to the master's setting")
2014-03-12 22:05:07 +00:00
vReadTimeout = cmdVolume.Flag.Int("readTimeout", 30, "connection read timeout in seconds. Increase this if uploading large files.")
2013-08-13 16:22:06 +00:00
vMaxCpu = cmdVolume.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name")
volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
2013-08-13 16:22:06 +00:00
volumeWhiteList []string
)
func runVolume(cmd *Command, args []string) bool {
2012-11-07 09:51:43 +00:00
if *vMaxCpu < 1 {
*vMaxCpu = runtime.NumCPU()
}
runtime.GOMAXPROCS(*vMaxCpu)
folders := strings.Split(*volumeFolders, ",")
maxCountStrings := strings.Split(*maxVolumeCounts, ",")
maxCounts := make([]int, 0)
for _, maxString := range maxCountStrings {
if max, e := strconv.Atoi(maxString); e == nil {
maxCounts = append(maxCounts, max)
} else {
glog.Fatalf("The max specified in -max not a valid number %s", max)
}
}
if len(folders) != len(maxCounts) {
glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
}
for _, folder := range folders {
if err := util.TestFolderWritable(folder); err != nil {
glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
}
}
2012-09-26 10:27:10 +00:00
2012-09-26 09:29:16 +00:00
if *publicUrl == "" {
2012-09-26 10:27:10 +00:00
*publicUrl = *ip + ":" + strconv.Itoa(*vport)
2012-09-26 09:29:16 +00:00
}
2013-08-13 16:22:06 +00:00
if *volumeWhiteListOption != "" {
volumeWhiteList = strings.Split(*volumeWhiteListOption, ",")
2013-08-13 06:48:10 +00:00
}
r := http.NewServeMux()
2012-08-24 05:46:54 +00:00
weed_server.NewVolumeServer(r, VERSION, *ip, *vport, *publicUrl, folders, maxCounts,
*masterNode, *vpulse, *dataCenter, *rack, volumeWhiteList,
)
2012-08-24 05:46:54 +00:00
glog.V(0).Infoln("Start Weed volume server", VERSION, "at http://"+*ip+":"+strconv.Itoa(*vport))
2012-09-28 16:13:17 +00:00
srv := &http.Server{
Addr: *ip + ":" + strconv.Itoa(*vport),
2013-12-01 22:41:47 +00:00
Handler: r,
2012-09-28 17:21:06 +00:00
ReadTimeout: (time.Duration(*vReadTimeout) * time.Second),
}
2012-09-28 16:13:17 +00:00
e := srv.ListenAndServe()
2012-08-24 05:46:54 +00:00
if e != nil {
glog.Fatalf("Fail to start:%s", e.Error())
2012-08-24 05:46:54 +00:00
}
return true
}