Skip to content

Commit

Permalink
JT-78303: Add tests and config docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-zatsepin committed Jan 17, 2024
1 parent f232207 commit 1068da6
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright ${inceptionYear} - ${year} ${owner}
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jetbrains.exodus.entitystore

import org.junit.Assert.assertEquals
import org.junit.Test


class EntityIterableCacheAdapterTest {

@Test
fun `should create weighted cache`() {
// Given
// Use default config values
val config = PersistentEntityStoreConfig()
val maxCacheWeight = config.entityIterableCacheWeight

// When
val adapter = EntityIterableCacheAdapter.create(config)

// Then
assertEquals(maxCacheWeight, adapter.cache.size())
}

@Test
fun `should create sized cache for backward compatibility`() {
// Given
System.setProperty(PersistentEntityStoreConfig.ENTITY_ITERABLE_CACHE_SIZE, "100")
val config = PersistentEntityStoreConfig()

// When
val adapter = EntityIterableCacheAdapter.create(config)

// Then
assertEquals(100, adapter.cache.size())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public PersistentEntityStoreConfig setMutable(boolean isMutable) {
* <p>Mutable at runtime: no
*
* @see #CACHING_DISABLED
* @deprecated use {@linkplain #ENTITY_ITERABLE_CACHE_MEMORY_PERCENTAGE} instead.
* @deprecated use weighted case in conjunction with {@linkplain #ENTITY_ITERABLE_CACHE_MEMORY_PERCENTAGE} instead.
*/
public static final String ENTITY_ITERABLE_CACHE_SIZE = "exodus.entityStore.entityIterableCache.size";

Expand All @@ -243,14 +243,20 @@ public PersistentEntityStoreConfig setMutable(boolean isMutable) {
public static final String ENTITY_ITERABLE_DEFERRED_CACHE_SIZE = "exodus.entityStore.entityIterableCache.deferred.size";

/**
* Specifies the percentage (from 0 to 100) of heap memory used for cache.
* Specifies the percentage (from 0 to 100) of heap memory used for entity iterable cache.
* This value is used to calculate max cache weight.
* The final formula for maxCacheWeight is: maxMemory * memoryPercentage / 100 / entityWeight.
* <p>Mutable at runtime: no
*
* @see #ENTITY_ITERABLE_CACHE_ENTITY_WEIGHT
*/
public static final String ENTITY_ITERABLE_CACHE_MEMORY_PERCENTAGE = "exodus.entityStore.entityIterableCache.memoryPercentage";

/**
* Specifies the size of a single entity key stored in cached in bytes.
* <p>Mutable at runtime: no
*
* @see #ENTITY_ITERABLE_CACHE_MEMORY_PERCENTAGE
*/
public static final String ENTITY_ITERABLE_CACHE_ENTITY_WEIGHT = "exodus.entityStore.entityIterableCache.weightCoefficient";

Expand Down Expand Up @@ -703,7 +709,10 @@ public int getEntityIterableCacheEntityWeight() {
}

public long getEntityIterableCacheWeight() {
return (Runtime.getRuntime().maxMemory() * getEntityIterableCacheMemoryPercentage() / 100) / getEntityIterableCacheEntityWeight();
long maxMemory = Runtime.getRuntime().maxMemory();
long percentage = getEntityIterableCacheMemoryPercentage();
long entityWeight = getEntityIterableCacheEntityWeight();
return (maxMemory * percentage) / (100 * entityWeight);
}

public PersistentEntityStoreConfig setEntityIterableCacheSize(final int size) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright ${inceptionYear} - ${year} ${owner}
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jetbrains.exodus.entitystore

import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

class PersistentEntityStoreCacheConfigTest {

@Test
fun `should return default max cache weight`() {
// Given
val config = PersistentEntityStoreConfig()

// When
val maxCacheWeight = config.entityIterableCacheWeight

// Then
val maxMemory = Runtime.getRuntime().maxMemory()
assertTrue(maxCacheWeight in 1..<maxMemory)
}

@Test
fun `should calculate max cache weight from params`() {
// Given
System.setProperty(PersistentEntityStoreConfig.ENTITY_ITERABLE_CACHE_MEMORY_PERCENTAGE, "50")
System.setProperty(PersistentEntityStoreConfig.ENTITY_ITERABLE_CACHE_ENTITY_WEIGHT, "8")
val config = PersistentEntityStoreConfig()

// When
val maxCacheWeight = config.entityIterableCacheWeight

// Then
val maxMemory = Runtime.getRuntime().maxMemory()
assertEquals(maxCacheWeight, (maxMemory * 50) / (100 * 8))
}
}

0 comments on commit 1068da6

Please sign in to comment.