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

@ -84,8 +84,19 @@ func DeleteHandler(w http.ResponseWriter, r *http.Request) {
volumeId, _ := strconv.Atoui64(vid) volumeId, _ := strconv.Atoui64(vid)
n.ParsePath(fid) n.ParsePath(fid)
if *IsDebug {
log.Println("deleting", n)
}
cookie := n.Cookie cookie := n.Cookie
count, _ := store.Read(volumeId, n) count, ok := store.Read(volumeId, n)
if ok!=nil {
m := make(map[string]uint32)
m["size"] = 0
writeJson(w, r, m)
return
}
if n.Cookie != cookie { if n.Cookie != cookie {
log.Println("delete with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent()) log.Println("delete with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
@ -93,7 +104,7 @@ func DeleteHandler(w http.ResponseWriter, r *http.Request) {
} }
n.Size = 0 n.Size = 0
store.Write(volumeId, n) store.Delete(volumeId, n)
m := make(map[string]uint32) m := make(map[string]uint32)
m["size"] = uint32(count) m["size"] = uint32(count)
writeJson(w, r, m) writeJson(w, r, m)
@ -148,7 +159,7 @@ func main() {
}() }()
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

@ -94,6 +94,9 @@ 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

@ -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)
}