Skip to content

Commit 90a89ce

Browse files
authored
Near cache expiry improvements (#102)
* Near cache expiry improvements * Minor optimization
1 parent 18248dd commit 90a89ce

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

.github/workflows/build.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ jobs:
7171
go get google.golang.org/grpc/cmd/[email protected]
7272
INCLUDE_LONG_RUNNING=true PROFILES=,-jakarta,javax COHERENCE_VERSION=22.06.9 make clean generate-proto build-test-images test-e2e-standalone
7373
74+
- name: Profile Near Cache
75+
shell: bash
76+
run: |
77+
make profile-near-cache
78+
7479
- uses: actions/upload-artifact@v4
7580
if: failure()
7681
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ shared/
4040
certs/
4141
release/
4242
etc/
43+
coherence/coherence.test

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ copyright: getcopyright ## Check copyright headers
161161
-X bin/ \
162162
-X ./test/test/utils.go \
163163
-X dependency-reduced-pom.xml \
164+
-X ./coherence/coherence.test \
164165
-X binaries/ \
165166
-X build/ \
166167
-X proto/ \
@@ -332,6 +333,15 @@ test-cluster-startup: $(BUILD_PROPS) ## Startup any test cluster members using d
332333
test-cluster-shutdown: ## Shutdown any test cluster members using docker-compose
333334
cd test/utils && $(DOCKER_COMPOSE) -f docker-compose-2-members.yaml down || true
334335

336+
# ----------------------------------------------------------------------------------------------------------------------
337+
# Run Near Cache Profile Tests
338+
# ----------------------------------------------------------------------------------------------------------------------
339+
.PHONY: profile-near-cache
340+
profile-near-cache: ## Run Near Cache Profile Tests
341+
cd coherence && go test -run=^TestNearCacheExpiry1 -cpuprofile=$(TEST_LOGS_DIR)/cpu.out -memprofile=$(TEST_LOGS_DIR)/mem.out
342+
echo top | go tool pprof $(TEST_LOGS_DIR)/cpu.out
343+
echo top | go tool pprof $(TEST_LOGS_DIR)/mem.out
344+
335345

336346
# ----------------------------------------------------------------------------------------------------------------------
337347
# Startup standalone coherence via java -jar

coherence/localcache.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,14 @@ func (l *localCacheImpl[K, V]) GetAll(keys []K) map[K]*V {
160160
l.expireEntries()
161161

162162
results := make(map[K]*V, 0)
163+
now := time.Now()
163164

164165
for _, key := range keys {
165166
v, ok := l.data[key]
166167
if ok {
167168
// have entry so add to the results
168169
results[key] = &v.value
169-
v.lastAccess = time.Now()
170+
v.lastAccess = now
170171
}
171172
}
172173

@@ -239,7 +240,7 @@ func (l *localCacheImpl[K, V]) expireEntries() {
239240

240241
// check for cache expiry
241242
for k, v := range l.data {
242-
if v.ttl > 0 && time.Since(v.insertTime) > v.ttl {
243+
if v.ttl > 0 && start.Sub(v.insertTime) > v.ttl {
243244
keysToDelete = append(keysToDelete, k)
244245
}
245246
}

coherence/localcache_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,37 @@ func TestBasicLocalCacheSizeCalculation(t *testing.T) {
300300
g.Expect(cache.cacheMemory).Should(gomega.Equal(int64(0)))
301301
}
302302

303+
type expiryResults struct {
304+
ttl time.Duration
305+
expiryTime time.Duration
306+
cacheExpires int64
307+
}
308+
309+
func TestNearCacheExpiry1(_ *testing.T) {
310+
var (
311+
results = make([]expiryResults, 0)
312+
ttl int64
313+
)
314+
315+
for ttl = 10; ttl < 1_000; ttl += 25 {
316+
results = append(results, localCacheExpiryTest(time.Duration(ttl)*time.Millisecond, 10_000))
317+
}
318+
319+
// output results
320+
for _, r := range results {
321+
fmt.Printf("ttl=%5v, cache expires=%5v, time spent expiring=%10v\n", r.ttl, r.cacheExpires, r.expiryTime)
322+
}
323+
}
324+
325+
func localCacheExpiryTest(ttl time.Duration, count int) expiryResults {
326+
cache := newLocalCache[int, string]("my-cache-high-unit3", withLocalCacheExpiry(ttl))
327+
for i := 1; i <= count; i++ {
328+
cache.Put(i, fmt.Sprintf("value-%v", i))
329+
}
330+
331+
return expiryResults{ttl: ttl, expiryTime: cache.GetCacheExpiresDuration(), cacheExpires: cache.GetCacheExpires()}
332+
}
333+
303334
func Sleep(seconds int) {
304335
time.Sleep(time.Duration(seconds) * time.Second)
305336
}

0 commit comments

Comments
 (0)