From dcaf1796fe0e41e39ac5c9602cb190cf150e7849 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 16 Nov 2016 07:09:57 -0800 Subject: [PATCH] add option to enable caching --- weed/command/server.go | 2 ++ weed/command/volume.go | 3 +++ weed/server/volume_server.go | 4 +++- weed/storage/needle_byte_cache.go | 21 +++++++++++++-------- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/weed/command/server.go b/weed/command/server.go index eed7dcae4..027ba191d 100644 --- a/weed/command/server.go +++ b/weed/command/server.go @@ -72,6 +72,7 @@ var ( volumeFixJpgOrientation = cmdServer.Flag.Bool("volume.images.fix.orientation", true, "Adjust jpg orientation when uploading.") volumeReadRedirect = cmdServer.Flag.Bool("volume.read.redirect", true, "Redirect moved or non-local volumes.") volumeServerPublicUrl = cmdServer.Flag.String("volume.publicUrl", "", "publicly accessible address") + volumeEnableBytesCache = cmdServer.Flag.Bool("volume.cache.enable", false, "direct cache instead of OS cache, cost more memory.") isStartingFiler = cmdServer.Flag.Bool("filer", false, "whether to start filer") serverWhiteList []string @@ -259,6 +260,7 @@ func runServer(cmd *Command, args []string) bool { volumeNeedleMapKind, *serverIp+":"+strconv.Itoa(*masterPort), *volumePulse, *serverDataCenter, *serverRack, serverWhiteList, *volumeFixJpgOrientation, *volumeReadRedirect, + *volumeEnableBytesCache, ) glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*volumePort)) diff --git a/weed/command/volume.go b/weed/command/volume.go index 21369cbe9..ba498b8e4 100644 --- a/weed/command/volume.go +++ b/weed/command/volume.go @@ -36,6 +36,7 @@ type VolumeServerOptions struct { indexType *string fixJpgOrientation *bool readRedirect *bool + enableBytesCache *bool } func init() { @@ -54,6 +55,7 @@ func init() { v.indexType = cmdVolume.Flag.String("index", "memory", "Choose [memory|leveldb|boltdb] mode for memory~performance balance.") v.fixJpgOrientation = cmdVolume.Flag.Bool("images.fix.orientation", true, "Adjust jpg orientation when uploading.") v.readRedirect = cmdVolume.Flag.Bool("read.redirect", true, "Redirect moved or non-local volumes.") + v.enableBytesCache = cmdVolume.Flag.Bool("cache.enable", false, "direct cache instead of OS cache, cost more memory.") } var cmdVolume = &Command{ @@ -132,6 +134,7 @@ func runVolume(cmd *Command, args []string) bool { *v.master, *v.pulseSeconds, *v.dataCenter, *v.rack, v.whiteList, *v.fixJpgOrientation, *v.readRedirect, + *v.enableBytesCache, ) listeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.port) diff --git a/weed/server/volume_server.go b/weed/server/volume_server.go index 79a4276b1..1a912a169 100644 --- a/weed/server/volume_server.go +++ b/weed/server/volume_server.go @@ -33,7 +33,8 @@ func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string, dataCenter string, rack string, whiteList []string, fixJpgOrientation bool, - readRedirect bool) *VolumeServer { + readRedirect bool, + enableBytesCache bool) *VolumeServer { vs := &VolumeServer{ pulseSeconds: pulseSeconds, dataCenter: dataCenter, @@ -44,6 +45,7 @@ func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string, } vs.SetMasterNode(masterNode) vs.store = storage.NewStore(port, ip, publicUrl, folders, maxCounts, vs.needleMapKind) + storage.EnableBytesCache = enableBytesCache vs.guard = security.NewGuard(whiteList, "") diff --git a/weed/storage/needle_byte_cache.go b/weed/storage/needle_byte_cache.go index ae35a48ba..dfc32bcbf 100644 --- a/weed/storage/needle_byte_cache.go +++ b/weed/storage/needle_byte_cache.go @@ -11,8 +11,9 @@ import ( ) var ( - bytesCache *lru.Cache - bytesPool *util.BytesPool + EnableBytesCache = true + bytesCache *lru.Cache + bytesPool *util.BytesPool ) /* @@ -48,11 +49,13 @@ func (block *Block) increaseReference() { func getBytesForFileBlock(r *os.File, offset int64, readSize int) (dataSlice []byte, block *Block, err error) { // check cache, return if found cacheKey := fmt.Sprintf("%d:%d:%d", r.Fd(), offset>>3, readSize) - if obj, found := bytesCache.Get(cacheKey); found { - block = obj.(*Block) - block.increaseReference() - dataSlice = block.Bytes[0:readSize] - return dataSlice, block, nil + if EnableBytesCache { + if obj, found := bytesCache.Get(cacheKey); found { + block = obj.(*Block) + block.increaseReference() + dataSlice = block.Bytes[0:readSize] + return dataSlice, block, nil + } } // get the []byte from pool @@ -61,7 +64,9 @@ func getBytesForFileBlock(r *os.File, offset int64, readSize int) (dataSlice []b block = &Block{Bytes: b, refCount: 2} dataSlice = block.Bytes[0:readSize] _, err = r.ReadAt(dataSlice, offset) - bytesCache.Add(cacheKey, block) + if EnableBytesCache { + bytesCache.Add(cacheKey, block) + } return dataSlice, block, err }