properly release memory

fix https://github.com/chrislusf/seaweedfs/issues/301
This commit is contained in:
Chris Lu 2016-05-13 00:19:57 -07:00
parent e639f1f889
commit f8b03c45ef
5 changed files with 7 additions and 7 deletions

View file

@ -66,8 +66,10 @@ func getBytesForFileBlock(r *os.File, offset int64, readSize int) (dataSlice []b
} }
func (n *Needle) ReleaseMemory() { func (n *Needle) ReleaseMemory() {
if n.rawBlock != nil {
n.rawBlock.decreaseReference() n.rawBlock.decreaseReference()
} }
}
func ReleaseBytes(b []byte) { func ReleaseBytes(b []byte) {
bytesPool.Put(b) bytesPool.Put(b)
} }

View file

@ -144,10 +144,10 @@ func ReadNeedleBlob(r *os.File, offset int64, size uint32) (dataSlice []byte, bl
func (n *Needle) ReadData(r *os.File, offset int64, size uint32, version Version) (err error) { func (n *Needle) ReadData(r *os.File, offset int64, size uint32, version Version) (err error) {
bytes, block, err := ReadNeedleBlob(r, offset, size) bytes, block, err := ReadNeedleBlob(r, offset, size)
n.rawBlock = block
if err != nil { if err != nil {
return err return err
} }
n.rawBlock = block
n.ParseNeedleHeader(bytes) n.ParseNeedleHeader(bytes)
if n.Size != size { if n.Size != size {
return fmt.Errorf("File Entry Not Found. Needle %d Memory %d", n.Size, size) return fmt.Errorf("File Entry Not Found. Needle %d Memory %d", n.Size, size)

View file

@ -159,11 +159,11 @@ func (v *Volume) isFileUnchanged(n *Needle) bool {
if ok && nv.Offset > 0 { if ok && nv.Offset > 0 {
oldNeedle := new(Needle) oldNeedle := new(Needle)
err := oldNeedle.ReadData(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version()) err := oldNeedle.ReadData(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version())
defer oldNeedle.ReleaseMemory()
if err != nil { if err != nil {
glog.V(0).Infof("Failed to check updated file %v", err) glog.V(0).Infof("Failed to check updated file %v", err)
return false return false
} }
defer oldNeedle.ReleaseMemory()
if oldNeedle.Checksum == n.Checksum && bytes.Equal(oldNeedle.Data, n.Data) { if oldNeedle.Checksum == n.Checksum && bytes.Equal(oldNeedle.Data, n.Data) {
n.DataSize = oldNeedle.DataSize n.DataSize = oldNeedle.DataSize
return true return true
@ -289,7 +289,6 @@ func (v *Volume) readNeedle(n *Needle) (int, error) {
} }
err := n.ReadData(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version()) err := n.ReadData(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version())
if err != nil { if err != nil {
n.ReleaseMemory()
return 0, err return 0, err
} }
bytesRead := len(n.Data) bytesRead := len(n.Data)

View file

@ -66,12 +66,12 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
cookie := n.Cookie cookie := n.Cookie
count, e := vs.store.ReadVolumeNeedle(volumeId, n) count, e := vs.store.ReadVolumeNeedle(volumeId, n)
glog.V(4).Infoln("read bytes", count, "error", e) glog.V(4).Infoln("read bytes", count, "error", e)
defer n.ReleaseMemory()
if e != nil || count <= 0 { if e != nil || count <= 0 {
glog.V(0).Infoln("read error:", e, r.URL.Path) glog.V(0).Infoln("read error:", e, r.URL.Path)
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
} }
defer n.ReleaseMemory()
if n.Cookie != cookie { if n.Cookie != cookie {
glog.V(0).Infoln("request", r.URL.Path, "with unmaching cookie seen:", cookie, "expected:", n.Cookie, "from", r.RemoteAddr, "agent", r.UserAgent()) glog.V(0).Infoln("request", r.URL.Path, "with unmaching cookie seen:", cookie, "expected:", n.Cookie, "from", r.RemoteAddr, "agent", r.UserAgent())
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)

View file

@ -56,13 +56,13 @@ func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
cookie := n.Cookie cookie := n.Cookie
_, ok := vs.store.ReadVolumeNeedle(volumeId, n) _, ok := vs.store.ReadVolumeNeedle(volumeId, n)
defer n.ReleaseMemory()
if ok != nil { if ok != nil {
m := make(map[string]uint32) m := make(map[string]uint32)
m["size"] = 0 m["size"] = 0
writeJsonQuiet(w, r, http.StatusNotFound, m) writeJsonQuiet(w, r, http.StatusNotFound, m)
return return
} }
defer n.ReleaseMemory()
if n.Cookie != cookie { if n.Cookie != cookie {
glog.V(0).Infoln("delete", r.URL.Path, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent()) glog.V(0).Infoln("delete", r.URL.Path, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
@ -122,7 +122,6 @@ func (vs *VolumeServer) batchDeleteHandler(w http.ResponseWriter, r *http.Reques
Status: http.StatusNotFound, Status: http.StatusNotFound,
Error: err.Error(), Error: err.Error(),
}) })
n.ReleaseMemory()
continue continue
} }