seaweedfs/go/weed/master.go

305 lines
10 KiB
Go
Raw Normal View History

package main
import (
"bytes"
"code.google.com/p/weed-fs/go/glog"
"code.google.com/p/weed-fs/go/operation"
2013-02-27 14:54:22 +08:00
"code.google.com/p/weed-fs/go/replication"
"code.google.com/p/weed-fs/go/sequence"
2013-02-27 14:54:22 +08:00
"code.google.com/p/weed-fs/go/storage"
"code.google.com/p/weed-fs/go/topology"
2012-09-04 10:18:02 +08:00
"encoding/json"
"errors"
2013-12-02 06:41:47 +08:00
"github.com/gorilla/mux"
2012-09-04 10:18:02 +08:00
"net/http"
"os"
"path"
"runtime"
2012-09-04 10:18:02 +08:00
"strconv"
"strings"
"sync"
2012-09-29 00:13:17 +08:00
"time"
)
func init() {
2012-09-04 10:18:02 +08:00
cmdMaster.Run = runMaster // break init cycle
2013-01-20 11:49:57 +08:00
cmdMaster.IsDebug = cmdMaster.Flag.Bool("debug", false, "enable debug mode")
}
var cmdMaster = &Command{
2012-09-04 10:18:02 +08: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 (
mport = cmdMaster.Flag.Int("port", 9333, "http listen port")
metaFolder = cmdMaster.Flag.String("mdir", os.TempDir(), "data directory to store mappings")
volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 32*1024, "Default Volume Size in MegaBytes")
mpulse = cmdMaster.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
confFile = cmdMaster.Flag.String("conf", "/etc/weedfs/weedfs.conf", "xml configuration file")
defaultRepType = cmdMaster.Flag.String("defaultReplicationType", "000", "Default replication type if not specified.")
mReadTimeout = cmdMaster.Flag.Int("readTimeout", 3, "connection read timeout in seconds")
mMaxCpu = cmdMaster.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
garbageThreshold = cmdMaster.Flag.String("garbageThreshold", "0.3", "threshold to vacuum and reclaim spaces")
masterWhiteListOption = cmdMaster.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
//etcdCluster = cmdMaster.Flag.String("etcd", "", "comma separated etcd urls, e.g., http://localhost:4001, See github.com/coreos/go-etcd/etcd")
masterWhiteList []string
)
var topo *topology.Topology
var vg *replication.VolumeGrowth
var vgLock sync.Mutex
func dirLookupHandler(w http.ResponseWriter, r *http.Request) {
2012-09-04 10:18:02 +08:00
vid := r.FormValue("volumeId")
2013-11-12 18:21:22 +08:00
collection := r.FormValue("collection") //optional, but can be faster if too many collections
2012-09-04 10:18:02 +08:00
commaSep := strings.Index(vid, ",")
if commaSep > 0 {
vid = vid[0:commaSep]
}
2012-09-26 07:05:31 +08:00
volumeId, err := storage.NewVolumeId(vid)
if err == nil {
2013-11-12 18:21:22 +08:00
machines := topo.Lookup(collection, volumeId)
2012-09-26 07:05:31 +08:00
if machines != nil {
ret := []map[string]string{}
for _, dn := range machines {
2012-09-29 01:21:06 +08:00
ret = append(ret, map[string]string{"url": dn.Url(), "publicUrl": dn.PublicUrl})
2012-09-26 07:05:31 +08:00
}
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, map[string]interface{}{"locations": ret})
2012-09-26 07:05:31 +08:00
} else {
w.WriteHeader(http.StatusNotFound)
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
}
} else {
w.WriteHeader(http.StatusNotAcceptable)
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, map[string]string{"error": "unknown volumeId format " + vid})
2012-09-04 10:18:02 +08: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
}
2013-11-12 18:21:22 +08:00
collection := r.FormValue("collection")
dataCenter := r.FormValue("dataCenter")
2012-09-30 17:20:33 +08:00
rt, err := storage.NewReplicationTypeFromString(repType)
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
return
}
2013-11-12 18:21:22 +08:00
if topo.GetVolumeLayout(collection, rt).GetActiveVolumeCount(dataCenter) <= 0 {
if topo.FreeSpace() <= 0 {
w.WriteHeader(http.StatusNotFound)
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, map[string]string{"error": "No free volumes left!"})
2012-09-27 05:28:46 +08:00
return
} else {
vgLock.Lock()
defer vgLock.Unlock()
2013-11-12 18:21:22 +08:00
if topo.GetVolumeLayout(collection, rt).GetActiveVolumeCount(dataCenter) <= 0 {
if _, err = vg.AutomaticGrowByType(collection, rt, dataCenter, topo); err != nil {
writeJsonQuiet(w, r, map[string]string{"error": "Cannot grow volume group! " + err.Error()})
return
}
2013-02-27 14:54:22 +08:00
}
}
}
2013-11-12 18:21:22 +08:00
fid, count, dn, err := topo.PickForWrite(collection, rt, c, dataCenter)
if err == nil {
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, map[string]interface{}{"fid": fid, "url": dn.Url(), "publicUrl": dn.PublicUrl, "count": count})
} else {
w.WriteHeader(http.StatusNotAcceptable)
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
}
}
func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
init := r.FormValue("init") == "true"
2012-09-29 01:21:06 +08: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 10:18:02 +08:00
s := r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")+1] + r.FormValue("port")
publicUrl := r.FormValue("publicUrl")
volumes := new([]storage.VolumeInfo)
2013-02-27 14:54:22 +08:00
if err := json.Unmarshal([]byte(r.FormValue("volumes")), volumes); err != nil {
writeJsonQuiet(w, r, map[string]string{"error": "Cannot unmarshal \"volumes\": " + err.Error()})
return
}
2012-09-28 03:17:27 +08:00
debug(s, "volumes", r.FormValue("volumes"))
topo.RegisterVolumes(init, *volumes, ip, port, publicUrl, maxVolumeCount, r.FormValue("dataCenter"), r.FormValue("rack"))
m := make(map[string]interface{})
m["VolumeSizeLimit"] = uint64(*volumeSizeLimitMB) * 1024 * 1024
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, m)
}
func dirStatusHandler(w http.ResponseWriter, r *http.Request) {
m := make(map[string]interface{})
m["Version"] = VERSION
m["Topology"] = topo.ToMap()
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, m)
}
2012-11-07 17:51:43 +08:00
func volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
gcThreshold := r.FormValue("garbageThreshold")
if gcThreshold == "" {
gcThreshold = *garbageThreshold
}
debug("garbageThreshold =", gcThreshold)
topo.Vacuum(gcThreshold)
dirStatusHandler(w, r)
2012-11-07 17:51:43 +08:00
}
func volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
count := 0
2012-09-30 17:20:33 +08: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 {
2013-11-12 18:21:22 +08:00
count, err = vg.GrowByCountAndType(count, r.FormValue("collection"), rt, r.FormValue("dataCneter"), topo)
}
} else {
err = errors.New("parameter count is not found")
}
}
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, map[string]string{"error": "parameter replication " + err.Error()})
} else {
w.WriteHeader(http.StatusNotAcceptable)
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, map[string]interface{}{"count": count})
}
}
2012-10-10 16:10:05 +08:00
func volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
m := make(map[string]interface{})
m["Version"] = VERSION
m["Volumes"] = topo.ToVolumeMap()
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, m)
2012-10-10 16:10:05 +08:00
}
func redirectHandler(w http.ResponseWriter, r *http.Request) {
vid, _, _, _, _ := parseURLPath(r.URL.Path)
volumeId, err := storage.NewVolumeId(vid)
if err != nil {
debug("parsing error:", err, r.URL.Path)
return
}
2013-11-12 18:21:22 +08:00
machines := topo.Lookup("", volumeId)
if machines != nil && len(machines) > 0 {
http.Redirect(w, r, "http://"+machines[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
} else {
w.WriteHeader(http.StatusNotFound)
2013-02-27 14:54:22 +08:00
writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
}
}
func submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
submitForClientHandler(w, r, "localhost:"+strconv.Itoa(*mport))
}
func runMaster(cmd *Command, args []string) bool {
if *mMaxCpu < 1 {
*mMaxCpu = runtime.NumCPU()
}
runtime.GOMAXPROCS(*mMaxCpu)
if *masterWhiteListOption != "" {
masterWhiteList = strings.Split(*masterWhiteListOption, ",")
}
var seq sequence.Sequencer
//if len(*etcdCluster) == 0 {
seq = sequence.NewFileSequencer(path.Join(*metaFolder, "weed.seq"))
//} else {
// seq = sequence.NewEtcdSequencer(*etcdCluster)
//}
2013-02-27 14:54:22 +08:00
var e error
if topo, e = topology.NewTopology("topo", *confFile, seq,
2013-02-27 14:54:22 +08:00
uint64(*volumeSizeLimitMB)*1024*1024, *mpulse); e != nil {
glog.Fatalf("cannot create topology:%s", e)
2013-02-27 14:54:22 +08:00
}
vg = replication.NewDefaultVolumeGrowth()
glog.V(0).Infoln("Volume Size Limit is", *volumeSizeLimitMB, "MB")
2013-12-02 06:41:47 +08:00
r := mux.NewRouter()
r.HandleFunc("/dir/assign", secure(masterWhiteList, dirAssignHandler))
r.HandleFunc("/dir/lookup", secure(masterWhiteList, dirLookupHandler))
r.HandleFunc("/dir/join", secure(masterWhiteList, dirJoinHandler))
r.HandleFunc("/dir/status", secure(masterWhiteList, dirStatusHandler))
r.HandleFunc("/vol/grow", secure(masterWhiteList, volumeGrowHandler))
r.HandleFunc("/vol/status", secure(masterWhiteList, volumeStatusHandler))
r.HandleFunc("/vol/vacuum", secure(masterWhiteList, volumeVacuumHandler))
r.HandleFunc("/submit", secure(masterWhiteList, submitFromMasterServerHandler))
r.HandleFunc("/", redirectHandler)
topo.StartRefreshWritableVolumes(*garbageThreshold)
2012-09-04 12:31:13 +08:00
glog.V(0).Infoln("Start Weed Master", VERSION, "at port", strconv.Itoa(*mport))
2012-09-29 01:21:06 +08:00
srv := &http.Server{
Addr: ":" + strconv.Itoa(*mport),
2013-12-02 06:41:47 +08:00
Handler: r,
2012-09-29 01:21:06 +08:00
ReadTimeout: time.Duration(*mReadTimeout) * time.Second,
}
2013-02-27 14:54:22 +08:00
e = srv.ListenAndServe()
2012-09-04 10:18:02 +08:00
if e != nil {
glog.Fatalf("Fail to start:%s", e)
2012-09-04 10:18:02 +08:00
}
return true
}
func submitForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
m := make(map[string]interface{})
if r.Method != "POST" {
m["error"] = "Only submit via POST!"
writeJsonQuiet(w, r, m)
return
}
debug("parsing upload file...")
fname, data, mimeType, isGzipped, lastModified, pe := storage.ParseUpload(r)
if pe != nil {
writeJsonError(w, r, pe)
return
}
debug("assigning file id for", fname)
assignResult, ae := operation.Assign(masterUrl, 1, r.FormValue("replication"))
if ae != nil {
writeJsonError(w, r, ae)
return
}
url := "http://" + assignResult.PublicUrl + "/" + assignResult.Fid
if lastModified != 0 {
url = url + "?ts=" + strconv.FormatUint(lastModified, 10)
}
debug("upload file to store", url)
uploadResult, err := operation.Upload(url, fname, bytes.NewReader(data), isGzipped, mimeType)
if err != nil {
writeJsonError(w, r, err)
return
}
m["fileName"] = fname
m["fid"] = assignResult.Fid
m["fileUrl"] = assignResult.PublicUrl + "/" + assignResult.Fid
m["size"] = uploadResult.Size
writeJsonQuiet(w, r, m)
return
}