2012-08-07 16:29:22 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-09-04 10:18:02 +08:00
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"pkg/directory"
|
|
|
|
"pkg/storage"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2012-09-04 11:40:38 +08:00
|
|
|
"time"
|
2012-08-07 16:29:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2012-09-04 10:18:02 +08:00
|
|
|
cmdMaster.Run = runMaster // break init cycle
|
|
|
|
IsDebug = cmdMaster.Flag.Bool("debug", false, "enable debug mode")
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2012-08-07 16:29:22 +08:00
|
|
|
and sequence number of file ids
|
|
|
|
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2012-09-04 10:18:02 +08:00
|
|
|
mport = cmdMaster.Flag.Int("port", 9333, "http listen port")
|
|
|
|
metaFolder = cmdMaster.Flag.String("mdir", "/tmp", "data directory to store mappings")
|
|
|
|
capacity = cmdMaster.Flag.Int("capacity", 100, "maximum number of volumes to hold")
|
|
|
|
mapper *directory.Mapper
|
|
|
|
volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 32*1024, "Default Volume Size in MegaBytes")
|
2012-09-04 11:40:38 +08:00
|
|
|
mpulse = cmdMaster.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
|
2012-08-07 16:29:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func dirLookupHandler(w http.ResponseWriter, r *http.Request) {
|
2012-09-04 10:18:02 +08:00
|
|
|
vid := r.FormValue("volumeId")
|
|
|
|
commaSep := strings.Index(vid, ",")
|
|
|
|
if commaSep > 0 {
|
|
|
|
vid = vid[0:commaSep]
|
|
|
|
}
|
|
|
|
volumeId, _ := storage.NewVolumeId(vid)
|
2012-09-04 15:26:38 +08:00
|
|
|
machines, e := mapper.Get(volumeId)
|
2012-09-04 10:18:02 +08:00
|
|
|
if e == nil {
|
2012-09-04 15:34:28 +08:00
|
|
|
ret:= []map[string]string{}
|
2012-09-04 15:26:38 +08:00
|
|
|
for _, machine := range machines {
|
|
|
|
ret = append(ret,map[string]string{"url": machine.Url, "publicUrl": machine.PublicUrl})
|
|
|
|
}
|
|
|
|
writeJson(w, r, ret)
|
2012-09-04 10:18:02 +08:00
|
|
|
} else {
|
|
|
|
log.Println("Invalid volume id", volumeId)
|
2012-09-04 15:26:38 +08:00
|
|
|
writeJson(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. " + e.Error()})
|
2012-09-04 10:18:02 +08:00
|
|
|
}
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
|
|
|
func dirAssignHandler(w http.ResponseWriter, r *http.Request) {
|
2012-09-04 10:18:02 +08:00
|
|
|
c := r.FormValue("count")
|
|
|
|
fid, count, machine, err := mapper.PickForWrite(c)
|
|
|
|
if err == nil {
|
2012-09-04 10:36:35 +08:00
|
|
|
writeJson(w, r, map[string]interface{}{"fid": fid, "url": machine.Url, "publicUrl": machine.PublicUrl, "count": count})
|
2012-09-04 10:18:02 +08:00
|
|
|
} else {
|
2012-09-04 12:31:13 +08:00
|
|
|
writeJson(w, r, map[string]string{"error": err.Error()})
|
2012-09-04 10:18:02 +08:00
|
|
|
}
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
|
|
|
func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
|
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)
|
|
|
|
json.Unmarshal([]byte(r.FormValue("volumes")), volumes)
|
|
|
|
if *IsDebug {
|
|
|
|
log.Println(s, "volumes", r.FormValue("volumes"))
|
|
|
|
}
|
2012-09-04 11:40:38 +08:00
|
|
|
mapper.Add(directory.NewMachine(s, publicUrl, *volumes, time.Now().Unix()))
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
|
|
|
func dirStatusHandler(w http.ResponseWriter, r *http.Request) {
|
2012-09-04 10:18:02 +08:00
|
|
|
writeJson(w, r, mapper)
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func runMaster(cmd *Command, args []string) bool {
|
2012-09-04 10:18:02 +08:00
|
|
|
log.Println("Volume Size Limit is", *volumeSizeLimitMB, "MB")
|
2012-09-04 11:40:38 +08:00
|
|
|
mapper = directory.NewMapper(*metaFolder, "directory", uint64(*volumeSizeLimitMB)*1024*1024, *mpulse)
|
2012-09-04 10:18:02 +08:00
|
|
|
http.HandleFunc("/dir/assign", dirAssignHandler)
|
|
|
|
http.HandleFunc("/dir/lookup", dirLookupHandler)
|
|
|
|
http.HandleFunc("/dir/join", dirJoinHandler)
|
|
|
|
http.HandleFunc("/dir/status", dirStatusHandler)
|
2012-08-07 16:29:22 +08:00
|
|
|
|
2012-09-04 12:31:13 +08:00
|
|
|
mapper.StartRefreshWritableVolumes()
|
|
|
|
|
2012-09-04 10:18:02 +08:00
|
|
|
log.Println("Start directory service at http://127.0.0.1:" + strconv.Itoa(*mport))
|
|
|
|
e := http.ListenAndServe(":"+strconv.Itoa(*mport), nil)
|
|
|
|
if e != nil {
|
|
|
|
log.Fatal("Fail to start:", e)
|
|
|
|
}
|
|
|
|
return true
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|