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)
offset := uint32(storage.SuperBlockSize)
for n != nil {
if *IsDebug {
log.Println("key", n.Id, "volume offset", offset, "data_size", n.Size, "length", length)
}
debug("key", n.Id, "volume offset", offset, "data_size", n.Size, "length", length)
if n.Size > 0 {
count, pe := nm.Put(n.Id, offset/8, n.Size)
if *IsDebug {
log.Println("saved", count, "with error", pe)
}
debug("saved", count, "with error", pe)
}
offset += length
n, length = storage.ReadNeedle(dataFile)

View file

@ -100,9 +100,7 @@ func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
publicUrl := r.FormValue("publicUrl")
volumes := new([]storage.VolumeInfo)
json.Unmarshal([]byte(r.FormValue("volumes")), volumes)
if *IsDebug {
log.Println(s, "volumes", r.FormValue("volumes"))
}
debug(s, "volumes", r.FormValue("volumes"))
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("replication", *uploadReplication)
jsonBlob, err := util.Post("http://"+*server+"/dir/assign", values)
if *IsDebug {
fmt.Println("assign result :", string(jsonBlob))
}
debug("assign result :", string(jsonBlob))
if err != nil {
return nil, err
}
@ -61,14 +59,10 @@ func assign(count int) (*AssignResult, error) {
}
func upload(filename string, server string, fid string) (int, error) {
if *IsDebug {
fmt.Println("Start uploading file:", filename)
}
debug("Start uploading file:", filename)
fh, err := os.Open(filename)
if err != nil {
if *IsDebug {
fmt.Println("Failed to open file:", filename)
}
debug("Failed to open file:", filename)
return 0, err
}
ret, e := operation.Upload("http://"+server+"/"+fid, filename, fh)

View file

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

View file

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