use one-liner debug(messages)

This commit is contained in:
Chris Lu 2012-09-27 12:17:27 -07:00
parent 2fd6b65a9e
commit 5cb38f9ea7
5 changed files with 25 additions and 52 deletions

View file

@ -51,14 +51,10 @@ func runFix(cmd *Command, args []string) bool {
nm := storage.NewNeedleMap(indexFile) nm := storage.NewNeedleMap(indexFile)
offset := uint32(storage.SuperBlockSize) offset := uint32(storage.SuperBlockSize)
for n != nil { for n != nil {
if *IsDebug { debug("key", n.Id, "volume offset", offset, "data_size", n.Size, "length", length)
log.Println("key", n.Id, "volume offset", offset, "data_size", n.Size, "length", length)
}
if n.Size > 0 { if n.Size > 0 {
count, pe := nm.Put(n.Id, offset/8, n.Size) count, pe := nm.Put(n.Id, offset/8, n.Size)
if *IsDebug { debug("saved", count, "with error", pe)
log.Println("saved", count, "with error", pe)
}
} }
offset += length offset += length
n, length = storage.ReadNeedle(dataFile) n, length = storage.ReadNeedle(dataFile)

View file

@ -100,9 +100,7 @@ func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
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 { debug(s, "volumes", r.FormValue("volumes"))
log.Println(s, "volumes", r.FormValue("volumes"))
}
topo.RegisterVolumes(*volumes, ip, port, publicUrl, maxVolumeCount) topo.RegisterVolumes(*volumes, ip, port, publicUrl, maxVolumeCount)
} }

View file

@ -43,9 +43,7 @@ func assign(count int) (*AssignResult, error) {
values.Add("count", strconv.Itoa(count)) values.Add("count", strconv.Itoa(count))
values.Add("replication", *uploadReplication) values.Add("replication", *uploadReplication)
jsonBlob, err := util.Post("http://"+*server+"/dir/assign", values) jsonBlob, err := util.Post("http://"+*server+"/dir/assign", values)
if *IsDebug { debug("assign result :", string(jsonBlob))
fmt.Println("assign result :", string(jsonBlob))
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -61,14 +59,10 @@ func assign(count int) (*AssignResult, error) {
} }
func upload(filename string, server string, fid string) (int, error) { func upload(filename string, server string, fid string) (int, error) {
if *IsDebug { debug("Start uploading file:", filename)
fmt.Println("Start uploading file:", filename)
}
fh, err := os.Open(filename) fh, err := os.Open(filename)
if err != nil { if err != nil {
if *IsDebug { debug("Failed to open file:", filename)
fmt.Println("Failed to open file:", filename)
}
return 0, err return 0, err
} }
ret, e := operation.Upload("http://"+server+"/"+fid, filename, fh) ret, e := operation.Upload("http://"+server+"/"+fid, filename, fh)

View file

@ -49,9 +49,7 @@ func assignVolumeHandler(w http.ResponseWriter, r *http.Request) {
} else { } else {
writeJson(w, r, map[string]string{"error": err.Error()}) writeJson(w, r, map[string]string{"error": err.Error()})
} }
if *IsDebug { debug("volume =", r.FormValue("volume"), ", replicationType =", r.FormValue("replicationType"), ", error =", err)
log.Println("volume =", r.FormValue("volume"), ", replicationType =", r.FormValue("replicationType"), ", error =", err)
}
} }
func storeHandler(w http.ResponseWriter, r *http.Request) { func storeHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
@ -68,40 +66,28 @@ func GetHandler(w http.ResponseWriter, r *http.Request) {
vid, fid, ext := parseURLPath(r.URL.Path) vid, fid, ext := parseURLPath(r.URL.Path)
volumeId, err := storage.NewVolumeId(vid) volumeId, err := storage.NewVolumeId(vid)
if err != nil { if err != nil {
if *IsDebug { debug("parsing error:", err, r.URL.Path)
log.Println("parsing error:", err, r.URL.Path)
}
return return
} }
n.ParsePath(fid) n.ParsePath(fid)
if *IsDebug { debug("volume", volumeId, "reading", n)
log.Println("volume", volumeId, "reading", n)
}
if !store.HasVolume(volumeId) { if !store.HasVolume(volumeId) {
lookupResult, err := operation.Lookup(*masterNode, volumeId) lookupResult, err := operation.Lookup(*masterNode, volumeId)
if *IsDebug { debug("volume", volumeId, "found on", lookupResult, "error", err)
log.Println("volume", volumeId, "found on", lookupResult, "error", err)
}
if err == nil { if err == nil {
http.Redirect(w, r, "http://"+lookupResult.Locations[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently) http.Redirect(w, r, "http://"+lookupResult.Locations[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
} else { } else {
if *IsDebug { debug("lookup error:", err, r.URL.Path)
log.Println("lookup error:", err, r.URL.Path)
}
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
} }
return return
} }
cookie := n.Cookie cookie := n.Cookie
count, e := store.Read(volumeId, n) count, e := store.Read(volumeId, n)
if *IsDebug { debug("read bytes", count, "error", e)
log.Println("read bytes", count, "error", e)
}
if e != nil || count <= 0 { if e != nil || count <= 0 {
if *IsDebug { debug("read error:", e, r.URL.Path)
log.Println("read error:", e, r.URL.Path)
}
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
} }
@ -171,9 +157,7 @@ func DeleteHandler(w http.ResponseWriter, r *http.Request) {
volumeId, _ := storage.NewVolumeId(vid) volumeId, _ := storage.NewVolumeId(vid)
n.ParsePath(fid) n.ParsePath(fid)
if *IsDebug { debug("deleting", n)
log.Println("deleting", n)
}
cookie := n.Cookie cookie := n.Cookie
count, ok := store.Read(volumeId, n) count, ok := store.Read(volumeId, n)
@ -213,6 +197,7 @@ func DeleteHandler(w http.ResponseWriter, r *http.Request) {
m["size"] = uint32(count) m["size"] = uint32(count)
writeJson(w, r, m) writeJson(w, r, m)
} }
func parseURLPath(path string) (vid, fid, ext string) { func parseURLPath(path string) (vid, fid, ext string) {
sepIndex := strings.LastIndex(path, "/") sepIndex := strings.LastIndex(path, "/")
@ -234,23 +219,17 @@ func parseURLPath(path string) (vid, fid, ext string) {
return return
} }
type distributedFunction func(location operation.Location) bool func distributedOperation(volumeId storage.VolumeId, op func(location operation.Location) bool) bool {
func distributedOperation(volumeId storage.VolumeId, op distributedFunction) bool {
if lookupResult, lookupErr := operation.Lookup(*masterNode, volumeId); lookupErr == nil { if lookupResult, lookupErr := operation.Lookup(*masterNode, volumeId); lookupErr == nil {
length := 0 length := 0
sem := make(chan int, len(lookupResult.Locations))
selfUrl := (*ip + ":" + strconv.Itoa(*vport)) selfUrl := (*ip + ":" + strconv.Itoa(*vport))
results := make(chan bool) results := make(chan bool)
for _, location := range lookupResult.Locations { for _, location := range lookupResult.Locations {
if location.Url != selfUrl { if location.Url != selfUrl {
sem <- 1
length++ length++
go func(op distributedFunction, location operation.Location, sem chan int, results chan bool) { go func(location operation.Location, results chan bool) {
ret := op(location) results <- op(location)
<-sem }(location, results)
results <- ret
}(op, location, sem, results)
} }
} }
ret := true ret := true

View file

@ -184,3 +184,9 @@ func writeJson(w http.ResponseWriter, r *http.Request, obj interface{}) {
w.Write([]uint8(")")) w.Write([]uint8(")"))
} }
} }
func debug(params ...interface{}){
if *IsDebug {
fmt.Println(params)
}
}