2012-08-07 08:29:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-08-13 16:31:19 +00:00
|
|
|
"bytes"
|
|
|
|
"code.google.com/p/weed-fs/go/glog"
|
|
|
|
"code.google.com/p/weed-fs/go/operation"
|
2013-02-27 06:54:22 +00:00
|
|
|
"code.google.com/p/weed-fs/go/replication"
|
2013-11-10 09:31:50 +00:00
|
|
|
"code.google.com/p/weed-fs/go/sequence"
|
2013-02-27 06:54:22 +00:00
|
|
|
"code.google.com/p/weed-fs/go/storage"
|
|
|
|
"code.google.com/p/weed-fs/go/topology"
|
2012-09-04 02:18:02 +00:00
|
|
|
"encoding/json"
|
2012-09-17 08:48:09 +00:00
|
|
|
"errors"
|
2012-09-04 02:18:02 +00:00
|
|
|
"net/http"
|
2013-08-28 17:39:15 +00:00
|
|
|
"os"
|
2013-11-10 09:31:50 +00:00
|
|
|
"path"
|
2012-10-06 17:50:52 +00:00
|
|
|
"runtime"
|
2012-09-04 02:18:02 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2013-10-29 19:48:31 +00:00
|
|
|
"sync"
|
2012-09-28 16:13:17 +00:00
|
|
|
"time"
|
2012-08-07 08:29:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2012-09-04 02:18:02 +00:00
|
|
|
cmdMaster.Run = runMaster // break init cycle
|
2013-01-20 03:49:57 +00:00
|
|
|
cmdMaster.IsDebug = cmdMaster.Flag.Bool("debug", false, "enable debug mode")
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2012-08-07 08:29:22 +00:00
|
|
|
and sequence number of file ids
|
|
|
|
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2013-08-13 16:31:19 +00:00
|
|
|
mport = cmdMaster.Flag.Int("port", 9333, "http listen port")
|
2013-08-28 17:39:15 +00:00
|
|
|
metaFolder = cmdMaster.Flag.String("mdir", os.TempDir(), "data directory to store mappings")
|
2013-08-13 16:31:19 +00:00
|
|
|
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.")
|
2013-11-10 09:31:50 +00:00
|
|
|
etcdCluster = cmdMaster.Flag.String("etcd", "", "comma separated etcd urls, e.g., http://localhost:4001, See github.com/coreos/go-etcd/etcd")
|
2013-08-13 16:31:19 +00:00
|
|
|
|
|
|
|
masterWhiteList []string
|
2012-08-07 08:29:22 +00:00
|
|
|
)
|
|
|
|
|
2012-09-17 00:31:15 +00:00
|
|
|
var topo *topology.Topology
|
|
|
|
var vg *replication.VolumeGrowth
|
2013-10-29 19:48:31 +00:00
|
|
|
var vgLock sync.Mutex
|
2012-09-17 00:31:15 +00:00
|
|
|
|
2012-08-07 08:29:22 +00:00
|
|
|
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{}
|
2013-01-04 09:02:52 +00:00
|
|
|
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
|
|
|
}
|
2013-02-27 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, map[string]interface{}{"locations": ret})
|
2012-09-25 23:05:31 +00:00
|
|
|
} else {
|
2012-10-10 04:06:24 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2013-02-27 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
|
2012-09-17 00:31:15 +00:00
|
|
|
}
|
2012-10-10 04:06:24 +00:00
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNotAcceptable)
|
2013-02-27 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": "unknown volumeId format " + vid})
|
2012-09-04 02:18:02 +00:00
|
|
|
}
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
2012-09-24 09:01:53 +00:00
|
|
|
|
2012-08-07 08:29:22 +00:00
|
|
|
func dirAssignHandler(w http.ResponseWriter, r *http.Request) {
|
2012-09-24 09:01:53 +00:00
|
|
|
c, e := strconv.Atoi(r.FormValue("count"))
|
|
|
|
if e != nil {
|
|
|
|
c = 1
|
|
|
|
}
|
2012-09-23 22:45:26 +00:00
|
|
|
repType := r.FormValue("replication")
|
2012-09-24 09:01:53 +00:00
|
|
|
if repType == "" {
|
|
|
|
repType = *defaultRepType
|
2012-09-23 22:45:26 +00:00
|
|
|
}
|
2013-06-20 01:10:38 +00:00
|
|
|
dataCenter := r.FormValue("dataCenter")
|
2012-09-30 09:20:33 +00:00
|
|
|
rt, err := storage.NewReplicationTypeFromString(repType)
|
2012-09-17 08:48:09 +00:00
|
|
|
if err != nil {
|
2012-10-10 04:06:24 +00:00
|
|
|
w.WriteHeader(http.StatusNotAcceptable)
|
2013-02-27 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
|
2012-09-17 08:48:09 +00:00
|
|
|
return
|
2012-09-17 06:18:47 +00:00
|
|
|
}
|
2013-06-20 01:10:38 +00:00
|
|
|
|
|
|
|
if topo.GetVolumeLayout(rt).GetActiveVolumeCount(dataCenter) <= 0 {
|
2012-09-17 00:31:15 +00:00
|
|
|
if topo.FreeSpace() <= 0 {
|
2012-10-10 04:06:24 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2013-02-27 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": "No free volumes left!"})
|
2012-09-26 21:28:46 +00:00
|
|
|
return
|
2012-09-17 00:31:15 +00:00
|
|
|
} else {
|
2013-10-29 19:48:31 +00:00
|
|
|
vgLock.Lock()
|
|
|
|
defer vgLock.Unlock()
|
|
|
|
if topo.GetVolumeLayout(rt).GetActiveVolumeCount(dataCenter) <= 0 {
|
|
|
|
if _, err = vg.AutomaticGrowByType(rt, dataCenter, topo); err != nil {
|
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": "Cannot grow volume group! " + err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2013-02-27 06:54:22 +00:00
|
|
|
}
|
2012-09-17 00:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-20 01:10:38 +00:00
|
|
|
fid, count, dn, err := topo.PickForWrite(rt, c, dataCenter)
|
2012-09-17 00:31:15 +00:00
|
|
|
if err == nil {
|
2013-02-27 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, map[string]interface{}{"fid": fid, "url": dn.Url(), "publicUrl": dn.PublicUrl, "count": count})
|
2012-09-17 00:31:15 +00:00
|
|
|
} else {
|
2012-10-10 04:06:24 +00:00
|
|
|
w.WriteHeader(http.StatusNotAcceptable)
|
2013-02-27 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
|
2012-09-17 00:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-24 09:01:53 +00:00
|
|
|
|
2012-08-07 08:29:22 +00:00
|
|
|
func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
|
2013-01-04 09:02:52 +00:00
|
|
|
init := r.FormValue("init") == "true"
|
2012-09-28 17:21:06 +00:00
|
|
|
ip := r.FormValue("ip")
|
|
|
|
if ip == "" {
|
|
|
|
ip = r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")]
|
|
|
|
}
|
2013-01-17 08:14:58 +00:00
|
|
|
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)
|
2013-02-27 06:54:22 +00: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-27 19:17:27 +00:00
|
|
|
debug(s, "volumes", r.FormValue("volumes"))
|
2013-06-20 01:10:38 +00:00
|
|
|
topo.RegisterVolumes(init, *volumes, ip, port, publicUrl, maxVolumeCount, r.FormValue("dataCenter"), r.FormValue("rack"))
|
2013-01-04 09:02:52 +00:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["VolumeSizeLimit"] = uint64(*volumeSizeLimitMB) * 1024 * 1024
|
2013-02-27 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, m)
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
2012-09-24 09:01:53 +00:00
|
|
|
|
2012-09-23 22:45:26 +00:00
|
|
|
func dirStatusHandler(w http.ResponseWriter, r *http.Request) {
|
2012-10-06 17:50:52 +00:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["Version"] = VERSION
|
|
|
|
m["Topology"] = topo.ToMap()
|
2013-02-27 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, m)
|
2012-09-17 00:31:15 +00:00
|
|
|
}
|
2012-09-24 09:01:53 +00:00
|
|
|
|
2012-11-07 09:51:43 +00:00
|
|
|
func volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
|
2013-01-21 03:44:23 +00:00
|
|
|
gcThreshold := r.FormValue("garbageThreshold")
|
|
|
|
if gcThreshold == "" {
|
|
|
|
gcThreshold = *garbageThreshold
|
2012-11-24 01:31:54 +00:00
|
|
|
}
|
2013-01-21 03:44:23 +00:00
|
|
|
debug("garbageThreshold =", gcThreshold)
|
|
|
|
topo.Vacuum(gcThreshold)
|
|
|
|
dirStatusHandler(w, r)
|
2012-11-07 09:51:43 +00:00
|
|
|
}
|
|
|
|
|
2012-09-17 00:31:15 +00:00
|
|
|
func volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
|
2012-09-17 08:48:09 +00:00
|
|
|
count := 0
|
2012-09-30 09:20:33 +00:00
|
|
|
rt, err := storage.NewReplicationTypeFromString(r.FormValue("replication"))
|
2012-09-17 08:48:09 +00:00
|
|
|
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-06-20 01:10:38 +00:00
|
|
|
count, err = vg.GrowByCountAndType(count, rt, r.FormValue("dataCneter"), topo)
|
2012-09-17 08:48:09 +00:00
|
|
|
}
|
2012-11-24 01:31:54 +00:00
|
|
|
} else {
|
|
|
|
err = errors.New("parameter count is not found")
|
2012-09-17 08:48:09 +00:00
|
|
|
}
|
2012-09-17 06:18:47 +00:00
|
|
|
}
|
2012-09-17 00:31:15 +00:00
|
|
|
if err != nil {
|
2012-10-10 04:06:24 +00:00
|
|
|
w.WriteHeader(http.StatusNotAcceptable)
|
2013-02-27 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": "parameter replication " + err.Error()})
|
2012-09-17 00:31:15 +00:00
|
|
|
} else {
|
2012-11-24 01:31:54 +00:00
|
|
|
w.WriteHeader(http.StatusNotAcceptable)
|
2013-02-27 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, map[string]interface{}{"count": count})
|
2012-09-17 00:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-07 08:29:22 +00:00
|
|
|
|
2012-10-10 08:10:05 +00:00
|
|
|
func volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
|
2012-11-24 01:31:54 +00:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["Version"] = VERSION
|
|
|
|
m["Volumes"] = topo.ToVolumeMap()
|
2013-02-27 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, m)
|
2012-10-10 08:10:05 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:52 +00:00
|
|
|
func redirectHandler(w http.ResponseWriter, r *http.Request) {
|
2013-09-02 06:58:21 +00:00
|
|
|
vid, _, _, _, _ := parseURLPath(r.URL.Path)
|
2013-01-04 09:02:52 +00:00
|
|
|
volumeId, err := storage.NewVolumeId(vid)
|
|
|
|
if err != nil {
|
|
|
|
debug("parsing error:", err, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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 06:54:22 +00:00
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
|
2013-01-04 09:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 20:23:10 +00:00
|
|
|
func submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
submitForClientHandler(w, r, "localhost:"+strconv.Itoa(*mport))
|
|
|
|
}
|
|
|
|
|
2012-08-07 08:29:22 +00:00
|
|
|
func runMaster(cmd *Command, args []string) bool {
|
2012-10-06 17:50:52 +00:00
|
|
|
if *mMaxCpu < 1 {
|
|
|
|
*mMaxCpu = runtime.NumCPU()
|
|
|
|
}
|
|
|
|
runtime.GOMAXPROCS(*mMaxCpu)
|
2013-08-13 16:31:19 +00:00
|
|
|
if *masterWhiteListOption != "" {
|
|
|
|
masterWhiteList = strings.Split(*masterWhiteListOption, ",")
|
|
|
|
}
|
2013-11-10 09:31:50 +00:00
|
|
|
var seq sequence.Sequencer
|
|
|
|
if len(*etcdCluster) == 0 {
|
|
|
|
seq = sequence.NewFileSequencer(path.Join(*metaFolder, "weed.seq"))
|
|
|
|
} else {
|
|
|
|
seq = sequence.NewEtcdSequencer(*etcdCluster)
|
|
|
|
}
|
2013-02-27 06:54:22 +00:00
|
|
|
var e error
|
2013-11-10 09:31:50 +00:00
|
|
|
if topo, e = topology.NewTopology("topo", *confFile, seq,
|
2013-02-27 06:54:22 +00:00
|
|
|
uint64(*volumeSizeLimitMB)*1024*1024, *mpulse); e != nil {
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.Fatalf("cannot create topology:%s", e)
|
2013-02-27 06:54:22 +00:00
|
|
|
}
|
2012-09-17 00:31:15 +00:00
|
|
|
vg = replication.NewDefaultVolumeGrowth()
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.V(0).Infoln("Volume Size Limit is", *volumeSizeLimitMB, "MB")
|
2013-08-13 16:31:19 +00:00
|
|
|
http.HandleFunc("/dir/assign", secure(masterWhiteList, dirAssignHandler))
|
|
|
|
http.HandleFunc("/dir/lookup", secure(masterWhiteList, dirLookupHandler))
|
|
|
|
http.HandleFunc("/dir/join", secure(masterWhiteList, dirJoinHandler))
|
|
|
|
http.HandleFunc("/dir/status", secure(masterWhiteList, dirStatusHandler))
|
|
|
|
http.HandleFunc("/vol/grow", secure(masterWhiteList, volumeGrowHandler))
|
|
|
|
http.HandleFunc("/vol/status", secure(masterWhiteList, volumeStatusHandler))
|
|
|
|
http.HandleFunc("/vol/vacuum", secure(masterWhiteList, volumeVacuumHandler))
|
|
|
|
|
|
|
|
http.HandleFunc("/submit", secure(masterWhiteList, submitFromMasterServerHandler))
|
2013-01-04 09:02:52 +00:00
|
|
|
http.HandleFunc("/", redirectHandler)
|
2012-08-07 08:29:22 +00:00
|
|
|
|
2012-11-24 01:31:54 +00:00
|
|
|
topo.StartRefreshWritableVolumes(*garbageThreshold)
|
2012-09-04 04:31:13 +00:00
|
|
|
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.V(0).Infoln("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,
|
|
|
|
}
|
2013-02-27 06:54:22 +00:00
|
|
|
e = srv.ListenAndServe()
|
2012-09-04 02:18:02 +00:00
|
|
|
if e != nil {
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.Fatalf("Fail to start:%s", e)
|
2012-09-04 02:18:02 +00:00
|
|
|
}
|
|
|
|
return true
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
2013-08-06 20:23:10 +00:00
|
|
|
|
|
|
|
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)
|
2013-08-14 06:26:51 +00:00
|
|
|
assignResult, ae := operation.Assign(masterUrl, 1, r.FormValue("replication"))
|
2013-08-06 20:23:10 +00:00
|
|
|
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
|
|
|
|
}
|