Skip to content

Commit

Permalink
validate snapshot cache size metric in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ceekay committed Apr 4, 2024
1 parent c399b44 commit 08150fe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,9 @@ public void decNumSnapshotDeleted() {
numSnapshotDeleted.incr(-1);
}

public int getNumSnapshotCacheSize() {
return numSnapshotCacheSize.value();
}
public void incNumSnapshotCacheSize() {
numSnapshotCacheSize.incr();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1829,6 +1829,7 @@ private void updateActiveSnapshotMetrics()
}
}
}

metrics.setNumSnapshotActive(activeGauge);
metrics.setNumSnapshotDeleted(deletedGauge);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ void tearDown() {
@DisplayName("get()")
void testGet() throws IOException {
final UUID dbKey1 = UUID.randomUUID();
assertEquals(0, omMetrics.getNumSnapshotCacheSize());
ReferenceCounted<OmSnapshot> omSnapshot = snapshotCache.get(dbKey1);
assertNotNull(omSnapshot);
assertNotNull(omSnapshot.get());
assertInstanceOf(OmSnapshot.class, omSnapshot.get());
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());
}

@Test
Expand All @@ -105,12 +107,14 @@ void testGetTwice() throws IOException {
ReferenceCounted<OmSnapshot> omSnapshot1 = snapshotCache.get(dbKey1);
assertNotNull(omSnapshot1);
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());

ReferenceCounted<OmSnapshot> omSnapshot1again = snapshotCache.get(dbKey1);
// Should be the same instance
assertEquals(omSnapshot1, omSnapshot1again);
assertEquals(omSnapshot1.get(), omSnapshot1again.get());
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());
}

@Test
Expand All @@ -121,10 +125,12 @@ void testReleaseByDbKey() throws IOException {
assertNotNull(omSnapshot1);
assertNotNull(omSnapshot1.get());
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());

snapshotCache.release(dbKey1);
// Entry will not be immediately evicted
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());
}

@Test
Expand All @@ -134,13 +140,16 @@ void testInvalidate() throws IOException {
ReferenceCounted<OmSnapshot> omSnapshot = snapshotCache.get(dbKey1);
assertNotNull(omSnapshot);
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());

snapshotCache.release(dbKey1);
// Entry will not be immediately evicted
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());

snapshotCache.invalidate(dbKey1);
assertEquals(0, snapshotCache.size());
assertEquals(0, omMetrics.getNumSnapshotCacheSize());
}

@Test
Expand All @@ -150,28 +159,34 @@ void testInvalidateAll() throws IOException {
ReferenceCounted<OmSnapshot> omSnapshot1 = snapshotCache.get(dbKey1);
assertNotNull(omSnapshot1);
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());

final UUID dbKey2 = UUID.randomUUID();
ReferenceCounted<OmSnapshot> omSnapshot2 = snapshotCache.get(dbKey2);
assertNotNull(omSnapshot2);
assertEquals(2, snapshotCache.size());
assertEquals(2, omMetrics.getNumSnapshotCacheSize());
// Should be difference omSnapshot instances
assertNotEquals(omSnapshot1, omSnapshot2);

final UUID dbKey3 = UUID.randomUUID();
ReferenceCounted<OmSnapshot> omSnapshot3 = snapshotCache.get(dbKey3);
assertNotNull(omSnapshot3);
assertEquals(3, snapshotCache.size());
assertEquals(3, omMetrics.getNumSnapshotCacheSize());

snapshotCache.release(dbKey1);
// Entry will not be immediately evicted
assertEquals(3, snapshotCache.size());
assertEquals(3, omMetrics.getNumSnapshotCacheSize());

snapshotCache.invalidate(dbKey1);
assertEquals(2, snapshotCache.size());
assertEquals(2, omMetrics.getNumSnapshotCacheSize());

snapshotCache.invalidateAll();
assertEquals(0, snapshotCache.size());
assertEquals(0, omMetrics.getNumSnapshotCacheSize());
}

private void assertEntryExistence(UUID key, boolean shouldExist) {
Expand All @@ -195,26 +210,33 @@ void testEviction1() throws IOException {
final UUID dbKey1 = UUID.randomUUID();
snapshotCache.get(dbKey1);
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());
snapshotCache.release(dbKey1);
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());

final UUID dbKey2 = UUID.randomUUID();
snapshotCache.get(dbKey2);
assertEquals(2, snapshotCache.size());
assertEquals(2, omMetrics.getNumSnapshotCacheSize());
snapshotCache.release(dbKey2);
assertEquals(2, snapshotCache.size());
assertEquals(2, omMetrics.getNumSnapshotCacheSize());

final UUID dbKey3 = UUID.randomUUID();
snapshotCache.get(dbKey3);
assertEquals(3, snapshotCache.size());
assertEquals(3, omMetrics.getNumSnapshotCacheSize());
snapshotCache.release(dbKey3);
assertEquals(3, snapshotCache.size());
assertEquals(3, omMetrics.getNumSnapshotCacheSize());

final UUID dbKey4 = UUID.randomUUID();
snapshotCache.get(dbKey4);
// dbKey1, dbKey2 and dbKey3 would have been evicted by the end of the last get() because
// those were release()d.
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());
assertEntryExistence(dbKey1, false);
}

Expand All @@ -225,25 +247,30 @@ void testEviction2() throws IOException {
final UUID dbKey1 = UUID.randomUUID();
snapshotCache.get(dbKey1);
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());

final UUID dbKey2 = UUID.randomUUID();
snapshotCache.get(dbKey2);
assertEquals(2, snapshotCache.size());
assertEquals(2, omMetrics.getNumSnapshotCacheSize());

final UUID dbKey3 = UUID.randomUUID();
snapshotCache.get(dbKey3);
assertEquals(3, snapshotCache.size());
assertEquals(3, omMetrics.getNumSnapshotCacheSize());

final UUID dbKey4 = UUID.randomUUID();
snapshotCache.get(dbKey4);
// dbKey1 would not have been evicted because it is not release()d
assertEquals(4, snapshotCache.size());
assertEquals(4, omMetrics.getNumSnapshotCacheSize());
assertEntryExistence(dbKey1, true);

// Releasing dbKey2 at this point should immediately trigger its eviction
// because the cache size exceeded the soft limit
snapshotCache.release(dbKey2);
assertEquals(3, snapshotCache.size());
assertEquals(3, omMetrics.getNumSnapshotCacheSize());
assertEntryExistence(dbKey2, false);
assertEntryExistence(dbKey1, true);
}
Expand All @@ -256,41 +283,50 @@ void testEviction3WithClose() throws IOException {
try (ReferenceCounted<OmSnapshot> rcOmSnapshot = snapshotCache.get(dbKey1)) {
assertEquals(1L, rcOmSnapshot.getTotalRefCount());
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());
}
// ref count should have been decreased because it would be close()d
// upon exiting try-with-resources.
assertEquals(0L, snapshotCache.getDbMap().get(dbKey1).getTotalRefCount());
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());

final UUID dbKey2 = UUID.randomUUID();
try (ReferenceCounted<OmSnapshot> rcOmSnapshot = snapshotCache.get(dbKey2)) {
assertEquals(1L, rcOmSnapshot.getTotalRefCount());
assertEquals(2, snapshotCache.size());
assertEquals(2, omMetrics.getNumSnapshotCacheSize());
// Get dbKey2 entry a second time
try (ReferenceCounted<OmSnapshot> rcOmSnapshot2 = snapshotCache.get(dbKey2)) {
assertEquals(2L, rcOmSnapshot.getTotalRefCount());
assertEquals(2L, rcOmSnapshot2.getTotalRefCount());
assertEquals(2, snapshotCache.size());
assertEquals(2, omMetrics.getNumSnapshotCacheSize());
}
assertEquals(1L, rcOmSnapshot.getTotalRefCount());
}
assertEquals(0L, snapshotCache.getDbMap().get(dbKey2).getTotalRefCount());
assertEquals(2, snapshotCache.size());
assertEquals(2, omMetrics.getNumSnapshotCacheSize());

final UUID dbKey3 = UUID.randomUUID();
try (ReferenceCounted<OmSnapshot> rcOmSnapshot = snapshotCache.get(dbKey3)) {
assertEquals(1L, rcOmSnapshot.getTotalRefCount());
assertEquals(3, snapshotCache.size());
assertEquals(3, omMetrics.getNumSnapshotCacheSize());
}
assertEquals(0L, snapshotCache.getDbMap().get(dbKey3).getTotalRefCount());
assertEquals(3, snapshotCache.size());
assertEquals(3, omMetrics.getNumSnapshotCacheSize());

final UUID dbKey4 = UUID.randomUUID();
try (ReferenceCounted<OmSnapshot> rcOmSnapshot = snapshotCache.get(dbKey4)) {
assertEquals(1L, rcOmSnapshot.getTotalRefCount());
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());
}
assertEquals(0L, snapshotCache.getDbMap().get(dbKey4).getTotalRefCount());
assertEquals(1, snapshotCache.size());
assertEquals(1, omMetrics.getNumSnapshotCacheSize());
}
}

0 comments on commit 08150fe

Please sign in to comment.