correctly deleting a file

correctly setting volume file size limit

git-svn-id: https://weed-fs.googlecode.com/svn/trunk@42 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
chris.lu@gmail.com 2012-01-19 00:49:41 +00:00
parent 380953692b
commit 659bf1940f
5 changed files with 158 additions and 135 deletions

View file

@ -74,7 +74,8 @@ func writeJson(w http.ResponseWriter, r *http.Request, obj interface{}) {
func main() { func main() {
flag.Parse() flag.Parse()
mapper = directory.NewMapper(*metaFolder, "directory", uint32(*volumeSizeLimitMB*1024*1024)) log.Println("Volume Size Limit is", *volumeSizeLimitMB, "MB")
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)

View file

@ -1,157 +1,168 @@
package main package main
import ( import (
"storage" "storage"
"flag" "flag"
"fmt" "fmt"
"http" "http"
"json" "json"
"log" "log"
"mime" "mime"
"rand" "rand"
"strconv" "strconv"
"strings" "strings"
"time" "time"
) )
var ( var (
port = flag.Int("port", 8080, "http listen port") port = flag.Int("port", 8080, "http listen port")
chunkFolder = flag.String("dir", "/tmp", "data directory to store files") chunkFolder = flag.String("dir", "/tmp", "data directory to store files")
volumes = flag.String("volumes", "0,1-3,4", "comma-separated list of volume ids or range of ids") volumes = flag.String("volumes", "0,1-3,4", "comma-separated list of volume ids or range of ids")
publicUrl = flag.String("publicUrl", "localhost:8080", "public url to serve data read") publicUrl = flag.String("publicUrl", "localhost:8080", "public url to serve data read")
metaServer = flag.String("mserver", "localhost:9333", "master directory server to store mappings") metaServer = flag.String("mserver", "localhost:9333", "master directory server to store mappings")
IsDebug = flag.Bool("debug", false, "enable debug mode") IsDebug = flag.Bool("debug", false, "enable debug mode")
pulse = flag.Int("pulseSeconds", 5, "number of seconds between heartbeats") pulse = flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
store *storage.Store store *storage.Store
) )
func statusHandler(w http.ResponseWriter, r *http.Request) { func statusHandler(w http.ResponseWriter, r *http.Request) {
writeJson(w, r, store.Status()) writeJson(w, r, store.Status())
} }
func addVolumeHandler(w http.ResponseWriter, r *http.Request) { func addVolumeHandler(w http.ResponseWriter, r *http.Request) {
store.AddVolume(r.FormValue("volume")) store.AddVolume(r.FormValue("volume"))
writeJson(w, r, store.Status()) writeJson(w, r, store.Status())
} }
func storeHandler(w http.ResponseWriter, r *http.Request) { func storeHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
case "GET": case "GET":
GetHandler(w, r) GetHandler(w, r)
case "DELETE": case "DELETE":
DeleteHandler(w, r) DeleteHandler(w, r)
case "POST": case "POST":
PostHandler(w, r) PostHandler(w, r)
} }
} }
func GetHandler(w http.ResponseWriter, r *http.Request) { func GetHandler(w http.ResponseWriter, r *http.Request) {
n := new(storage.Needle) n := new(storage.Needle)
vid, fid, ext := parseURLPath(r.URL.Path) vid, fid, ext := parseURLPath(r.URL.Path)
volumeId, _ := strconv.Atoui64(vid) volumeId, _ := strconv.Atoui64(vid)
n.ParsePath(fid) n.ParsePath(fid)
if *IsDebug { if *IsDebug {
log.Println("volume", volumeId, "reading", n) log.Println("volume", volumeId, "reading", n)
} }
cookie := n.Cookie cookie := n.Cookie
count, e := store.Read(volumeId, n) count, e := store.Read(volumeId, n)
if *IsDebug { if *IsDebug {
log.Println("read bytes", count, "error", e) log.Println("read bytes", count, "error", e)
} }
if n.Cookie != cookie { if n.Cookie != cookie {
log.Println("request with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent()) log.Println("request with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
return return
} }
if ext != "" { if ext != "" {
w.Header().Set("Content-Type", mime.TypeByExtension(ext)) w.Header().Set("Content-Type", mime.TypeByExtension(ext))
} }
w.Write(n.Data) w.Write(n.Data)
} }
func PostHandler(w http.ResponseWriter, r *http.Request) { func PostHandler(w http.ResponseWriter, r *http.Request) {
vid, _, _ := parseURLPath(r.URL.Path) vid, _, _ := parseURLPath(r.URL.Path)
volumeId, e := strconv.Atoui64(vid) volumeId, e := strconv.Atoui64(vid)
if e != nil { if e != nil {
writeJson(w, r, e) writeJson(w, r, e)
} else { } else {
ret := store.Write(volumeId, storage.NewNeedle(r)) ret := store.Write(volumeId, storage.NewNeedle(r))
m := make(map[string]uint32) m := make(map[string]uint32)
m["size"] = ret m["size"] = ret
writeJson(w, r, m) writeJson(w, r, m)
} }
} }
func DeleteHandler(w http.ResponseWriter, r *http.Request) { func DeleteHandler(w http.ResponseWriter, r *http.Request) {
n := new(storage.Needle) n := new(storage.Needle)
vid, fid, _ := parseURLPath(r.URL.Path) vid, fid, _ := parseURLPath(r.URL.Path)
volumeId, _ := strconv.Atoui64(vid) volumeId, _ := strconv.Atoui64(vid)
n.ParsePath(fid) n.ParsePath(fid)
cookie := n.Cookie if *IsDebug {
count, _ := store.Read(volumeId, n) log.Println("deleting", n)
}
if n.Cookie != cookie { cookie := n.Cookie
log.Println("delete with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent()) count, ok := store.Read(volumeId, n)
return
}
n.Size = 0 if ok!=nil {
store.Write(volumeId, n) m := make(map[string]uint32)
m := make(map[string]uint32) m["size"] = 0
m["size"] = uint32(count) writeJson(w, r, m)
writeJson(w, r, m) return
}
if n.Cookie != cookie {
log.Println("delete with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
return
}
n.Size = 0
store.Delete(volumeId, n)
m := make(map[string]uint32)
m["size"] = uint32(count)
writeJson(w, r, m)
} }
func writeJson(w http.ResponseWriter, r *http.Request, obj interface{}) { func writeJson(w http.ResponseWriter, r *http.Request, obj interface{}) {
w.Header().Set("Content-Type", "application/javascript") w.Header().Set("Content-Type", "application/javascript")
bytes, _ := json.Marshal(obj) bytes, _ := json.Marshal(obj)
callback := r.FormValue("callback") callback := r.FormValue("callback")
if callback == "" { if callback == "" {
w.Write(bytes) w.Write(bytes)
} else { } else {
w.Write([]uint8(callback)) w.Write([]uint8(callback))
w.Write([]uint8("(")) w.Write([]uint8("("))
fmt.Fprint(w, string(bytes)) fmt.Fprint(w, string(bytes))
w.Write([]uint8(")")) w.Write([]uint8(")"))
} }
//log.Println("JSON Response", string(bytes)) //log.Println("JSON Response", string(bytes))
} }
func parseURLPath(path string) (vid, fid, ext string) { func parseURLPath(path string) (vid, fid, ext string) {
sepIndex := strings.LastIndex(path, "/") sepIndex := strings.LastIndex(path, "/")
commaIndex := strings.LastIndex(path[sepIndex:], ",") commaIndex := strings.LastIndex(path[sepIndex:], ",")
if commaIndex <= 0 { if commaIndex <= 0 {
log.Println("unknown file id", path[sepIndex+1:]) log.Println("unknown file id", path[sepIndex+1:])
return return
} }
dotIndex := strings.LastIndex(path[sepIndex:], ".") dotIndex := strings.LastIndex(path[sepIndex:], ".")
vid = path[sepIndex+1 : commaIndex] vid = path[sepIndex+1 : commaIndex]
fid = path[commaIndex+1:] fid = path[commaIndex+1:]
ext = "" ext = ""
if dotIndex > 0 { if dotIndex > 0 {
fid = path[commaIndex+1 : dotIndex] fid = path[commaIndex+1 : dotIndex]
ext = path[dotIndex+1:] ext = path[dotIndex+1:]
} }
return return
} }
func main() { func main() {
flag.Parse() flag.Parse()
//TODO: now default to 1G, this value should come from server? //TODO: now default to 1G, this value should come from server?
store = storage.NewStore(*port, *publicUrl, *chunkFolder, *volumes) store = storage.NewStore(*port, *publicUrl, *chunkFolder, *volumes)
defer store.Close() defer store.Close()
http.HandleFunc("/", storeHandler) http.HandleFunc("/", storeHandler)
http.HandleFunc("/status", statusHandler) http.HandleFunc("/status", statusHandler)
http.HandleFunc("/add_volume", addVolumeHandler) http.HandleFunc("/add_volume", addVolumeHandler)
go func() { go func() {
for { for {
store.Join(*metaServer) store.Join(*metaServer)
ns := int64(*pulse) * 1e9 ns := int64(*pulse) * 1e9
time.Sleep(ns + rand.Int63()%ns) time.Sleep(ns + rand.Int63()%ns)
} }
}() }()
log.Println("store joined at", *metaServer) log.Println("store joined at", *metaServer)
log.Println("Start storage service at http://127.0.0.1:" + strconv.Itoa(*port), "public url", *publicUrl) log.Println("Start storage service at http://127.0.0.1:"+strconv.Itoa(*port), "public url", *publicUrl)
e := http.ListenAndServe(":"+strconv.Itoa(*port), nil) e := http.ListenAndServe(":"+strconv.Itoa(*port), nil)
if e != nil { if e != nil {
log.Fatalf("Fail to start:", e.String()) log.Fatalf("Fail to start:", e.String())
} }
} }

View file

@ -36,14 +36,14 @@ type Mapper struct {
FileIdSequence uint64 FileIdSequence uint64
fileIdCounter uint64 fileIdCounter uint64
volumeSizeLimit uint32 volumeSizeLimit uint64
} }
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{Server: MachineInfo{Url: server, PublicUrl: publicUrl}, Volumes: volumes}
} }
func NewMapper(dirname string, filename string, volumeSizeLimit uint32) (m *Mapper) { func NewMapper(dirname string, filename string, volumeSizeLimit uint64) (m *Mapper) {
m = &Mapper{dir: dirname, fileName: filename} m = &Mapper{dir: dirname, fileName: filename}
m.vid2machineId = make(map[uint32]int) m.vid2machineId = make(map[uint32]int)
m.volumeSizeLimit = volumeSizeLimit m.volumeSizeLimit = volumeSizeLimit
@ -96,6 +96,7 @@ func (m *Mapper) Get(vid uint32) (*Machine, os.Error) {
} }
func (m *Mapper) Add(machine Machine) { func (m *Mapper) Add(machine Machine) {
//check existing machine, linearly //check existing machine, linearly
log.Println("Adding machine", machine.Server.Url)
m.lock.Lock() m.lock.Lock()
foundExistingMachineId := -1 foundExistingMachineId := -1
for index, entry := range m.Machines { for index, entry := range m.Machines {
@ -115,14 +116,13 @@ func (m *Mapper) Add(machine Machine) {
//add to vid2machineId map, and Writers array //add to vid2machineId map, and Writers array
for _, v := range machine.Volumes { for _, v := range machine.Volumes {
//log.Println("Setting volume", v.Id, "to", machine.Server.Url)
m.vid2machineId[v.Id] = machineId + 1 //use base 1 indexed, to detect not found cases m.vid2machineId[v.Id] = machineId + 1 //use base 1 indexed, to detect not found cases
} }
//setting Writers, copy-on-write because of possible updating //setting Writers, copy-on-write because of possible updating, this needs some future work!
var writers []uint32 var writers []uint32
for _, machine_entry := range m.Machines { for _, machine_entry := range m.Machines {
for _, v := range machine_entry.Volumes { for _, v := range machine_entry.Volumes {
if v.Size < int64(m.volumeSizeLimit) { if uint64(v.Size) < m.volumeSizeLimit {
writers = append(writers, v.Id) writers = append(writers, v.Id)
} }
} }

View file

@ -92,7 +92,10 @@ func (s *Store) Close() {
} }
} }
func (s *Store) Write(i uint64, n *Needle) uint32 { func (s *Store) Write(i uint64, n *Needle) uint32 {
return s.volumes[i].write(n) return s.volumes[i].write(n)
}
func (s *Store) Delete(i uint64, n *Needle) uint32 {
return s.volumes[i].delete(n)
} }
func (s *Store) Read(i uint64, n *Needle) (int, os.Error) { func (s *Store) Read(i uint64, n *Needle) (int, os.Error) {
return s.volumes[i].read(n) return s.volumes[i].read(n)

View file

@ -9,7 +9,7 @@ import (
) )
const ( const (
SuperBlockSize = 8 SuperBlockSize = 8
) )
type Volume struct { type Volume struct {
@ -69,6 +69,19 @@ func (v *Volume) write(n *Needle) uint32 {
} }
return ret return ret
} }
func (v *Volume) delete(n *Needle) uint32 {
v.accessLock.Lock()
defer v.accessLock.Unlock()
nv, ok := v.nm.Get(n.Key)
//log.Println("key", n.Key, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
if ok {
v.nm.Delete(n.Key)
v.dataFile.Seek(int64(nv.Offset*8), 0)
n.Append(v.dataFile)
return nv.Size
}
return 0
}
func (v *Volume) read(n *Needle) (int, os.Error) { func (v *Volume) read(n *Needle) (int, os.Error) {
v.accessLock.Lock() v.accessLock.Lock()
defer v.accessLock.Unlock() defer v.accessLock.Unlock()
@ -79,8 +92,3 @@ func (v *Volume) read(n *Needle) (int, os.Error) {
} }
return -1, os.EOF return -1, os.EOF
} }
func (v *Volume) delete(n *Needle) {
v.accessLock.Lock()
defer v.accessLock.Unlock()
v.nm.Delete(n.Key)
}