seaweedfs/weed-fs/src/cmd/weed/master.go

165 lines
5.2 KiB
Go
Raw Normal View History

package main
import (
2012-09-04 02:18:02 +00:00
"encoding/json"
"errors"
2012-09-04 02:18:02 +00:00
"log"
"net/http"
"pkg/replication"
2012-09-04 02:18:02 +00:00
"pkg/storage"
"pkg/topology"
"runtime"
2012-09-04 02:18:02 +00:00
"strconv"
"strings"
2012-09-28 16:13:17 +00:00
"time"
)
func init() {
2012-09-04 02:18:02 +00:00
cmdMaster.Run = runMaster // break init cycle
IsDebug = cmdMaster.Flag.Bool("debug", false, "enable debug mode")
}
var cmdMaster = &Command{
2012-09-04 02:18:02 +00:00
UsageLine: "master -port=9333",
Short: "start a master server",
Long: `start a master server to provide volume=>location mapping service
and sequence number of file ids
`,
}
var (
2012-09-04 02:18:02 +00:00
mport = cmdMaster.Flag.Int("port", 9333, "http listen port")
metaFolder = cmdMaster.Flag.String("mdir", "/tmp", "data directory to store mappings")
volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 32*1024, "Default Volume Size in MegaBytes")
2012-09-04 03:40:38 +00:00
mpulse = cmdMaster.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
2012-09-26 08:55:56 +00:00
confFile = cmdMaster.Flag.String("conf", "/etc/weedfs/weedfs.conf", "xml configuration file")
2012-09-30 09:20:33 +00:00
defaultRepType = cmdMaster.Flag.String("defaultReplicationType", "000", "Default replication type if not specified.")
2012-09-28 17:21:06 +00:00
mReadTimeout = cmdMaster.Flag.Int("readTimeout", 5, "connection read timeout in seconds")
mMaxCpu = cmdVolume.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
)
var topo *topology.Topology
var vg *replication.VolumeGrowth
func dirLookupHandler(w http.ResponseWriter, r *http.Request) {
2012-09-04 02:18:02 +00:00
vid := r.FormValue("volumeId")
commaSep := strings.Index(vid, ",")
if commaSep > 0 {
vid = vid[0:commaSep]
}
2012-09-25 23:05:31 +00:00
volumeId, err := storage.NewVolumeId(vid)
if err == nil {
machines := topo.Lookup(volumeId)
if machines != nil {
ret := []map[string]string{}
for _, dn := range *machines {
2012-09-28 17:21:06 +00:00
ret = append(ret, map[string]string{"url": dn.Url(), "publicUrl": dn.PublicUrl})
2012-09-25 23:05:31 +00:00
}
writeJson(w, r, map[string]interface{}{"locations": ret})
} else {
writeJson(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
}
2012-09-04 02:18:02 +00:00
}
}
func dirAssignHandler(w http.ResponseWriter, r *http.Request) {
c, e := strconv.Atoi(r.FormValue("count"))
if e != nil {
c = 1
}
repType := r.FormValue("replication")
if repType == "" {
repType = *defaultRepType
}
2012-09-30 09:20:33 +00:00
rt, err := storage.NewReplicationTypeFromString(repType)
if err != nil {
writeJson(w, r, map[string]string{"error": err.Error()})
return
}
if topo.GetVolumeLayout(rt).GetActiveVolumeCount() <= 0 {
if topo.FreeSpace() <= 0 {
writeJson(w, r, map[string]string{"error": "No free volumes left!"})
2012-09-26 21:28:46 +00:00
return
} else {
vg.GrowByType(rt, topo)
}
}
fid, count, dn, err := topo.PickForWrite(rt, c)
if err == nil {
2012-09-28 17:21:06 +00:00
writeJson(w, r, map[string]interface{}{"fid": fid, "url": dn.Url(), "publicUrl": dn.PublicUrl, "count": count})
} else {
writeJson(w, r, map[string]string{"error": err.Error()})
}
}
func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
2012-09-28 17:21:06 +00:00
ip := r.FormValue("ip")
if ip == "" {
ip = r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")]
}
port, _ := strconv.Atoi(r.FormValue("port"))
maxVolumeCount, _ := strconv.Atoi(r.FormValue("maxVolumeCount"))
2012-09-04 02:18:02 +00:00
s := r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")+1] + r.FormValue("port")
publicUrl := r.FormValue("publicUrl")
volumes := new([]storage.VolumeInfo)
json.Unmarshal([]byte(r.FormValue("volumes")), volumes)
2012-09-27 19:17:27 +00:00
debug(s, "volumes", r.FormValue("volumes"))
topo.RegisterVolumes(*volumes, ip, port, publicUrl, maxVolumeCount)
}
func dirStatusHandler(w http.ResponseWriter, r *http.Request) {
m := make(map[string]interface{})
m["Version"] = VERSION
m["Topology"] = topo.ToMap()
2012-09-30 09:20:33 +00:00
writeJson(w, r, m)
}
func volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
count := 0
2012-09-30 09:20:33 +00:00
rt, err := storage.NewReplicationTypeFromString(r.FormValue("replication"))
if err == nil {
if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
if topo.FreeSpace() < count*rt.GetCopyCount() {
err = errors.New("Only " + strconv.Itoa(topo.FreeSpace()) + " volumes left! Not enough for " + strconv.Itoa(count*rt.GetCopyCount()))
} else {
count, err = vg.GrowByCountAndType(count, rt, topo)
}
}
}
if err != nil {
writeJson(w, r, map[string]string{"error": err.Error()})
} else {
writeJson(w, r, map[string]interface{}{"count": count})
}
}
func runMaster(cmd *Command, args []string) bool {
if *mMaxCpu < 1 {
*mMaxCpu = runtime.NumCPU()
}
runtime.GOMAXPROCS(*mMaxCpu)
topo = topology.NewTopology("topo", *confFile, *metaFolder, "weed", uint64(*volumeSizeLimitMB)*1024*1024, *mpulse)
vg = replication.NewDefaultVolumeGrowth()
2012-09-04 02:18:02 +00:00
log.Println("Volume Size Limit is", *volumeSizeLimitMB, "MB")
http.HandleFunc("/dir/assign", dirAssignHandler)
http.HandleFunc("/dir/lookup", dirLookupHandler)
http.HandleFunc("/dir/join", dirJoinHandler)
http.HandleFunc("/dir/status", dirStatusHandler)
http.HandleFunc("/vol/grow", volumeGrowHandler)
topo.StartRefreshWritableVolumes()
2012-09-04 04:31:13 +00:00
log.Println("Start Weed Master", VERSION, "at port", strconv.Itoa(*mport))
2012-09-28 17:21:06 +00:00
srv := &http.Server{
Addr: ":" + strconv.Itoa(*mport),
Handler: http.DefaultServeMux,
ReadTimeout: time.Duration(*mReadTimeout) * time.Second,
}
e := srv.ListenAndServe()
2012-09-04 02:18:02 +00:00
if e != nil {
2012-09-26 06:28:16 +00:00
log.Fatalf("Fail to start:%s", e.Error())
2012-09-04 02:18:02 +00:00
}
return true
}