reformatting

This commit is contained in:
Chris Lu 2012-09-03 19:18:02 -07:00
parent 09542d82b4
commit fbe828e486
2 changed files with 66 additions and 73 deletions

View file

@ -1,87 +1,87 @@
package main package main
import ( import (
"pkg/directory" "encoding/json"
"encoding/json" "log"
"log" "net/http"
"net/http" "pkg/directory"
"pkg/storage" "pkg/storage"
"strconv" "strconv"
"strings" "strings"
) )
func init() { func init() {
cmdMaster.Run = runMaster // break init cycle cmdMaster.Run = runMaster // break init cycle
IsDebug = cmdMaster.Flag.Bool("debug", false, "enable debug mode") IsDebug = cmdMaster.Flag.Bool("debug", false, "enable debug mode")
} }
var cmdMaster = &Command{ var cmdMaster = &Command{
UsageLine: "master -port=9333", UsageLine: "master -port=9333",
Short: "start a master server", Short: "start a master server",
Long: `start a master server to provide volume=>location mapping service Long: `start a master server to provide volume=>location mapping service
and sequence number of file ids and sequence number of file ids
`, `,
} }
var ( var (
mport = cmdMaster.Flag.Int("port", 9333, "http listen port") mport = cmdMaster.Flag.Int("port", 9333, "http listen port")
metaFolder = cmdMaster.Flag.String("mdir", "/tmp", "data directory to store mappings") metaFolder = cmdMaster.Flag.String("mdir", "/tmp", "data directory to store mappings")
capacity = cmdMaster.Flag.Int("capacity", 100, "maximum number of volumes to hold") capacity = cmdMaster.Flag.Int("capacity", 100, "maximum number of volumes to hold")
mapper *directory.Mapper mapper *directory.Mapper
volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 32*1024, "Default Volume Size in MegaBytes") volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 32*1024, "Default Volume Size in MegaBytes")
) )
func dirLookupHandler(w http.ResponseWriter, r *http.Request) { func dirLookupHandler(w http.ResponseWriter, r *http.Request) {
vid := r.FormValue("volumeId") vid := r.FormValue("volumeId")
commaSep := strings.Index(vid, ",") commaSep := strings.Index(vid, ",")
if commaSep > 0 { if commaSep > 0 {
vid = vid[0:commaSep] vid = vid[0:commaSep]
} }
volumeId, _ := storage.NewVolumeId(vid) volumeId, _ := storage.NewVolumeId(vid)
machine, e := mapper.Get(volumeId) machine, e := mapper.Get(volumeId)
if e == nil { if e == nil {
writeJson(w, r, machine.Server) writeJson(w, r, map[string]string{"url": machine.Url, "publicUrl": machine.PublicUrl})
} else { } else {
log.Println("Invalid volume id", volumeId) log.Println("Invalid volume id", volumeId)
writeJson(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found"}) writeJson(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found"})
} }
} }
func dirAssignHandler(w http.ResponseWriter, r *http.Request) { func dirAssignHandler(w http.ResponseWriter, r *http.Request) {
c:=r.FormValue("count") c := r.FormValue("count")
fid, count, machine, err := mapper.PickForWrite(c) fid, count, machine, err := mapper.PickForWrite(c)
if err == nil { if err == nil {
writeJson(w, r, map[string]string{"fid": fid, "url": machine.Url, "publicUrl":machine.PublicUrl, "count":strconv.Itoa(count)}) writeJson(w, r, map[string]string{"fid": fid, "url": machine.Url, "publicUrl": machine.PublicUrl, "count": strconv.Itoa(count)})
} else { } else {
writeJson(w, r, map[string]string{"error": err.Error()}) writeJson(w, r, map[string]string{"error": err.Error()})
} }
} }
func dirJoinHandler(w http.ResponseWriter, r *http.Request) { func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
s := r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")+1] + r.FormValue("port") s := r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")+1] + r.FormValue("port")
publicUrl := r.FormValue("publicUrl") publicUrl := r.FormValue("publicUrl")
volumes := new([]storage.VolumeInfo) volumes := new([]storage.VolumeInfo)
json.Unmarshal([]byte(r.FormValue("volumes")), volumes) json.Unmarshal([]byte(r.FormValue("volumes")), volumes)
if *IsDebug { if *IsDebug {
log.Println(s, "volumes", r.FormValue("volumes")) log.Println(s, "volumes", r.FormValue("volumes"))
} }
mapper.Add(*directory.NewMachine(s, publicUrl, *volumes)) mapper.Add(*directory.NewMachine(s, publicUrl, *volumes))
} }
func dirStatusHandler(w http.ResponseWriter, r *http.Request) { func dirStatusHandler(w http.ResponseWriter, r *http.Request) {
writeJson(w, r, mapper) writeJson(w, r, mapper)
} }
func runMaster(cmd *Command, args []string) bool { func runMaster(cmd *Command, args []string) bool {
log.Println("Volume Size Limit is", *volumeSizeLimitMB, "MB") log.Println("Volume Size Limit is", *volumeSizeLimitMB, "MB")
mapper = directory.NewMapper(*metaFolder, "directory", uint64(*volumeSizeLimitMB)*1024*1024) mapper = directory.NewMapper(*metaFolder, "directory", uint64(*volumeSizeLimitMB)*1024*1024)
http.HandleFunc("/dir/assign", dirAssignHandler) http.HandleFunc("/dir/assign", dirAssignHandler)
http.HandleFunc("/dir/lookup", dirLookupHandler) http.HandleFunc("/dir/lookup", dirLookupHandler)
http.HandleFunc("/dir/join", dirJoinHandler) http.HandleFunc("/dir/join", dirJoinHandler)
http.HandleFunc("/dir/status", dirStatusHandler) http.HandleFunc("/dir/status", dirStatusHandler)
log.Println("Start directory service at http://127.0.0.1:" + strconv.Itoa(*mport)) log.Println("Start directory service at http://127.0.0.1:" + strconv.Itoa(*mport))
e := http.ListenAndServe(":"+strconv.Itoa(*mport), nil) e := http.ListenAndServe(":"+strconv.Itoa(*mport), nil)
if e != nil { if e != nil {
log.Fatal("Fail to start:", e) log.Fatal("Fail to start:", e)
} }
return true return true
} }

View file

@ -10,17 +10,10 @@ import (
"sync" "sync"
) )
const (
FileIdSaveInterval = 10000
)
type MachineInfo struct {
Url string //<server name/ip>[:port]
PublicUrl string
}
type Machine struct { type Machine struct {
Server MachineInfo
Volumes []storage.VolumeInfo Volumes []storage.VolumeInfo
Url string //<server name/ip>[:port]
PublicUrl string
} }
type Mapper struct { type Mapper struct {
@ -35,7 +28,7 @@ type Mapper struct {
} }
func NewMachine(server, publicUrl string, volumes []storage.VolumeInfo) *Machine { func NewMachine(server, publicUrl string, volumes []storage.VolumeInfo) *Machine {
return &Machine{Server: MachineInfo{Url: server, PublicUrl: publicUrl}, Volumes: volumes} return &Machine{Url: server, PublicUrl: publicUrl, Volumes: volumes}
} }
func NewMapper(dirname string, filename string, volumeSizeLimit uint64) (m *Mapper) { func NewMapper(dirname string, filename string, volumeSizeLimit uint64) (m *Mapper) {
@ -49,7 +42,7 @@ func NewMapper(dirname string, filename string, volumeSizeLimit uint64) (m *Mapp
return return
} }
func (m *Mapper) PickForWrite(c string) (string, int, *MachineInfo, error) { func (m *Mapper) PickForWrite(c string) (string, int, *Machine, error) {
len_writers := len(m.Writers) len_writers := len(m.Writers)
if len_writers <= 0 { if len_writers <= 0 {
log.Println("No more writable volumes!") log.Println("No more writable volumes!")
@ -61,11 +54,11 @@ func (m *Mapper) PickForWrite(c string) (string, int, *MachineInfo, error) {
machine := m.Machines[machine_id-1] machine := m.Machines[machine_id-1]
fileId, count := m.sequence.NextFileId(util.ParseInt(c, 1)) fileId, count := m.sequence.NextFileId(util.ParseInt(c, 1))
if count == 0 { if count == 0 {
return "", 0, &m.Machines[rand.Intn(len(m.Machines))].Server, errors.New("Strange count:" + c) return "", 0, m.Machines[rand.Intn(len(m.Machines))], errors.New("Strange count:" + c)
} }
return NewFileId(vid, fileId, rand.Uint32()).String(), count, &machine.Server, nil return NewFileId(vid, fileId, rand.Uint32()).String(), count, machine, nil
} }
return "", 0, &m.Machines[rand.Intn(len(m.Machines))].Server, errors.New("Strangely vid " + vid.String() + " is on no machine!") return "", 0, m.Machines[rand.Intn(len(m.Machines))], errors.New("Strangely vid " + vid.String() + " is on no machine!")
} }
func (m *Mapper) Get(vid storage.VolumeId) (*Machine, error) { func (m *Mapper) Get(vid storage.VolumeId) (*Machine, error) {
machineId := m.vid2machineId[vid] machineId := m.vid2machineId[vid]
@ -80,7 +73,7 @@ func (m *Mapper) Add(machine Machine) {
m.volumeLock.Lock() m.volumeLock.Lock()
foundExistingMachineId := -1 foundExistingMachineId := -1
for index, entry := range m.Machines { for index, entry := range m.Machines {
if machine.Server.Url == entry.Server.Url { if machine.Url == entry.Url {
foundExistingMachineId = index foundExistingMachineId = index
break break
} }