1. ensure replicated file has the same timestamp

2. upload can specify modified time by &ts=...
3. correctly return code 304
This commit is contained in:
Chris Lu 2013-07-10 00:25:14 -07:00
parent 53b6831f73
commit 4c200acd7d
5 changed files with 20 additions and 9 deletions

View file

@ -4,10 +4,12 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"path/filepath"
_ "fmt" _ "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"mime"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
) )
@ -29,7 +31,8 @@ func Upload(uploadUrl string, filename string, reader io.Reader) (*UploadResult,
log.Println("error copying data", err) log.Println("error copying data", err)
return nil, err return nil, err
} }
content_type := body_writer.FormDataContentType() content_type := mime.TypeByExtension(filepath.Ext(filename))
content_type := body_writer.FormDataContentType()
if err = body_writer.Close(); err != nil { if err = body_writer.Close(); err != nil {
log.Println("error closing body", err) log.Println("error closing body", err)
return nil, err return nil, err

View file

@ -23,9 +23,9 @@ func ReplicatedWrite(masterNode string, s *storage.Store, volumeId storage.Volum
needToReplicate = s.GetVolume(volumeId).NeedToReplicate() needToReplicate = s.GetVolume(volumeId).NeedToReplicate()
} }
if needToReplicate { //send to other replica locations if needToReplicate { //send to other replica locations
if r.FormValue("type") != "standard" { if r.FormValue("type") != "replicate" {
if !distributedOperation(masterNode, s, volumeId, func(location operation.Location) bool { if !distributedOperation(masterNode, s, volumeId, func(location operation.Location) bool {
_, err := operation.Upload("http://"+location.Url+r.URL.Path+"?type=standard", string(needle.Name), bytes.NewReader(needle.Data)) _, err := operation.Upload("http://"+location.Url+r.URL.Path+"?type=replicate&ts="+strconv.FormatUint(needle.LastModified,10), string(needle.Name), bytes.NewReader(needle.Data))
return err == nil return err == nil
}) { }) {
ret = 0 ret = 0
@ -39,7 +39,7 @@ func ReplicatedWrite(masterNode string, s *storage.Store, volumeId storage.Volum
volumeId.String() + ": " + err.Error() volumeId.String() + ": " + err.Error()
} else { } else {
distributedOperation(masterNode, s, volumeId, func(location operation.Location) bool { distributedOperation(masterNode, s, volumeId, func(location operation.Location) bool {
return nil == operation.Delete("http://"+location.Url+r.URL.Path+"?type=standard") return nil == operation.Delete("http://"+location.Url+r.URL.Path+"?type=replicate")
}) })
} }
} }
@ -59,9 +59,9 @@ func ReplicatedDelete(masterNode string, store *storage.Store, volumeId storage.
needToReplicate = store.GetVolume(volumeId).NeedToReplicate() needToReplicate = store.GetVolume(volumeId).NeedToReplicate()
} }
if needToReplicate { //send to other replica locations if needToReplicate { //send to other replica locations
if r.FormValue("type") != "standard" { if r.FormValue("type") != "replicate" {
if !distributedOperation(masterNode, store, volumeId, func(location operation.Location) bool { if !distributedOperation(masterNode, store, volumeId, func(location operation.Location) bool {
return nil == operation.Delete("http://"+location.Url+r.URL.Path+"?type=standard") return nil == operation.Delete("http://"+location.Url+r.URL.Path+"?type=replicate")
}) { }) {
ret = 0 ret = 0
} }

View file

@ -90,7 +90,10 @@ func NewNeedle(r *http.Request) (n *Needle, e error) {
} }
n.SetHasName() n.SetHasName()
} }
n.LastModified = uint64(time.Now().Unix()) var parseError error
if n.LastModified, parseError = strconv.ParseUint(r.FormValue("ts"), 10, 64); parseError != nil {
n.LastModified = uint64(time.Now().Unix())
}
n.SetHasLastModifiedDate() n.SetHasLastModifiedDate()
n.Data = data n.Data = data

View file

@ -66,7 +66,12 @@ func upload(filename string, server string, fid string) (int, error) {
debug("Failed to open file:", filename) debug("Failed to open file:", filename)
return 0, err return 0, err
} }
ret, e := operation.Upload("http://"+server+"/"+fid, path.Base(filename), fh) fi, fiErr := fh.Stat()
if fiErr!=nil {
debug("Failed to stat file:", filename)
return 0, fiErr
}
ret, e := operation.Upload("http://"+server+"/"+fid+"?ts="+strconv.Itoa(int(fi.ModTime().Unix()) ), path.Base(filename), fh)
if e != nil { if e != nil {
return 0, e return 0, e
} }

View file

@ -149,7 +149,7 @@ func GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool)
w.Header().Set("Last-Modified", time.Unix(int64(n.LastModified), 0).UTC().Format(http.TimeFormat)) w.Header().Set("Last-Modified", time.Unix(int64(n.LastModified), 0).UTC().Format(http.TimeFormat))
if r.Header.Get("If-Modified-Since") != "" { if r.Header.Get("If-Modified-Since") != "" {
if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil { if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
if t.Unix() <= int64(n.LastModified) { if t.Unix() >= int64(n.LastModified) {
w.WriteHeader(http.StatusNotModified) w.WriteHeader(http.StatusNotModified)
return return
} }