You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ro...@apache.org on 2018/05/23 16:34:07 UTC

[incubator-trafficcontrol] 01/04: Add HitCount to cache object and cleanup of HTTP table in cacheinsepctor

This is an automated email from the ASF dual-hosted git repository.

rob pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git

commit 784198d10e5bb41ff50d336850119b1034a6b6ae
Author: Jan van Doorn <ja...@comcast.com>
AuthorDate: Mon May 7 09:28:30 2018 -0600

    Add HitCount to cache object and cleanup of HTTP table in cacheinsepctor
---
 grove/cache/retryinggetter.go       | 1 +
 grove/cacheobj/cacheobj.go          | 2 ++
 grove/diskcache/diskcache.go        | 4 ++--
 grove/memcache/memcache.go          | 1 +
 grove/plugin/http_cacheinspector.go | 7 ++++---
 5 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/grove/cache/retryinggetter.go b/grove/cache/retryinggetter.go
index 12a0cf8..ee60017 100644
--- a/grove/cache/retryinggetter.go
+++ b/grove/cache/retryinggetter.go
@@ -187,6 +187,7 @@ func GetAndCache(
 				RespRespTime:     respRespTime,
 				LastModified:     revalidateObj.LastModified,
 				Size:             revalidateObj.Size,
+				HitCount:         revalidateObj.HitCount, // no need to +1 here, the cache Get did that
 			}
 		}
 		cache.Add(cacheKey, obj) // TODO store pointer?
diff --git a/grove/cacheobj/cacheobj.go b/grove/cacheobj/cacheobj.go
index 3e6b27f..c05888f 100644
--- a/grove/cacheobj/cacheobj.go
+++ b/grove/cacheobj/cacheobj.go
@@ -34,6 +34,7 @@ type CacheObj struct {
 	RespRespTime     time.Time // the origin server's Date time when the object was sent
 	LastModified     time.Time // the origin LastModified if it exists, or Date if it doesn't
 	Size             uint64
+	HitCount         uint64 // the number of times this object was hit
 }
 
 // ComputeSize computes the size of the given CacheObj. This computation is expensive, as the headers must be iterated over. Thus, the size should be computed once and stored, not computed on-the-fly for every new request for the cached object.
@@ -55,6 +56,7 @@ func New(reqHeader http.Header, bytes []byte, code int, originCode int, proxyURL
 		ReqRespTime:      reqRespTime,
 		RespRespTime:     respRespTime,
 		LastModified:     lastModified,
+		HitCount:         1,
 	}
 	// copyHeader(reqHeader, &obj.reqHeaders)
 	// copyHeader(respHeader, &obj.respHeaders)
diff --git a/grove/diskcache/diskcache.go b/grove/diskcache/diskcache.go
index ae9ce35..a8e1b30 100644
--- a/grove/diskcache/diskcache.go
+++ b/grove/diskcache/diskcache.go
@@ -152,16 +152,16 @@ func (c *DiskCache) gc(cacheSizeBytes uint64) {
 func (c *DiskCache) Get(key string) (*cacheobj.CacheObj, bool) {
 	val, found := c.Peek(key)
 	if found {
-		// TODO JvD check to see if val.Size is good to use here.
 		c.lru.Add(key, val.Size) // TODO directly call c.ll.MoveToFront
 		log.Debugln("DiskCache.Get getting '" + key + "' from cache and updating LRU")
+		atomic.AddUint64(&val.HitCount, 1)
 		return val, true
 	}
 	return nil, false
 
 }
 
-// Peek takes a key, and returns its value, and whether it was found, without changing the lru-ness or hit-count
+// Peek takes a key, and returns its value, and whether it was found, without changing the lru-ness or hitcount
 func (c *DiskCache) Peek(key string) (*cacheobj.CacheObj, bool) {
 	log.Debugln("DiskCache.Get key '" + key + "'")
 	valBytes := []byte(nil)
diff --git a/grove/memcache/memcache.go b/grove/memcache/memcache.go
index 2cd29ce..29d0217 100644
--- a/grove/memcache/memcache.go
+++ b/grove/memcache/memcache.go
@@ -52,6 +52,7 @@ func (c *MemCache) Get(key string) (*cacheobj.CacheObj, bool) {
 	obj, ok := c.cache[key]
 	if ok {
 		c.lru.Add(key, obj.Size) // TODO directly call c.ll.MoveToFront
+		atomic.AddUint64(&obj.HitCount, 1)
 	}
 	c.cacheM.RUnlock()
 	return obj, ok
diff --git a/grove/plugin/http_cacheinspector.go b/grove/plugin/http_cacheinspector.go
index 23b40d1..ee792b4 100644
--- a/grove/plugin/http_cacheinspector.go
+++ b/grove/plugin/http_cacheinspector.go
@@ -119,6 +119,7 @@ func cacheinspect(icfg interface{}, d OnRequestData) bool {
 			w.Write([]byte(fmt.Sprintf("  ReqRespTime:                  %v\n", cacheObject.ReqRespTime)))
 			w.Write([]byte(fmt.Sprintf("  RespRespTime:                 %v\n", cacheObject.RespRespTime)))
 			w.Write([]byte(fmt.Sprintf("  LastModified:                 %v\n", cacheObject.LastModified)))
+			w.Write([]byte(fmt.Sprintf("  HitCount:                     %v\n", cacheObject.HitCount)))
 		} else {
 			w.Write([]byte("Not Found"))
 		}
@@ -169,7 +170,7 @@ func cacheinspect(icfg interface{}, d OnRequestData) bool {
 				w.Write([]byte(fmt.Sprintf("showing only first %d and last %d:\n\n", head, tail)))
 			}
 
-			w.Write([]byte(fmt.Sprintf("<b>     #\t\tCode\tSize\tAge\t\t\tKey</b>\n")))
+			w.Write([]byte(fmt.Sprintf("<b>            #    Code      Size                   Age    HitCount      Key</b>\n")))
 			for i, key := range keys {
 				if (doSearch && !strings.Contains(key, searchArr[0])) || !doSearch && (i > tail && i < len(keys)-head) {
 					continue
@@ -177,8 +178,8 @@ func cacheinspect(icfg interface{}, d OnRequestData) bool {
 
 				cacheObject, _ := d.Stats.CachePeek(key, cName)
 				age := time.Now().Sub(cacheObject.ReqRespTime)
-				w.Write([]byte(fmt.Sprintf("     %05d\t%d\t%s\t%-20v\t<a href=\"http://%s%s?key=%s&cache=%s\">%s</a>\n",
-					i, cacheObject.Code, bytefmt.ByteSize(cacheObject.Size), age, req.Host, CacheStatsEndpoint, url.QueryEscape(key), cName, key)))
+				w.Write([]byte(fmt.Sprintf("     %8d%8d%10s%22v%12d      <a href=\"http://%s%s?key=%s&cache=%s\">%s</a>\n",
+					i, cacheObject.Code, bytefmt.ByteSize(cacheObject.Size), age, cacheObject.HitCount, req.Host, CacheStatsEndpoint, url.QueryEscape(key), cName, key)))
 			}
 
 		}

-- 
To stop receiving notification emails like this one, please contact
rob@apache.org.