From 168076fb5cbcc926cfa9c8aac6da82e8d31d1bea Mon Sep 17 00:00:00 2001 From: Sagar Upadhyaya Date: Thu, 26 Oct 2023 14:42:08 -0700 Subject: [PATCH] Added javadoc for new files/packages Signed-off-by: Sagar Upadhyaya --- .../common/cache/RemovalNotification.java | 2 +- .../cache/tier}/CachingTier.java | 2 +- .../cache/tier}/DiskCachingTier.java | 2 +- .../cache/tier}/OnHeapCachingTier.java | 2 +- .../cache/tier}/OpenSearchOnHeapCache.java | 26 ++++++--- .../cache/tier}/TierType.java | 5 +- .../cache/tier}/TieredCacheEventListener.java | 7 ++- .../cache/tier}/TieredCacheLoader.java | 7 ++- .../cache/tier}/TieredCacheService.java | 2 +- .../TieredCacheSpilloverStrategyService.java | 25 ++++---- .../common/cache/tier/package-info.java | 10 ++++ .../cache/request/ShardRequestCache.java | 2 +- .../AbstractIndexShardCacheEntity.java | 1 + .../indices/DummyDiskCachingTier.java | 58 ------------------- .../indices/IndicesRequestCache.java | 9 ++- ...redCacheSpilloverStrategyServiceTests.java | 2 +- .../indices/IndicesServiceCloseTests.java | 1 + 17 files changed, 73 insertions(+), 90 deletions(-) rename server/src/main/java/org/opensearch/{indices => common/cache/tier}/CachingTier.java (96%) rename server/src/main/java/org/opensearch/{indices => common/cache/tier}/DiskCachingTier.java (91%) rename server/src/main/java/org/opensearch/{indices => common/cache/tier}/OnHeapCachingTier.java (91%) rename server/src/main/java/org/opensearch/{indices => common/cache/tier}/OpenSearchOnHeapCache.java (80%) rename server/src/main/java/org/opensearch/{indices => common/cache/tier}/TierType.java (77%) rename server/src/main/java/org/opensearch/{indices => common/cache/tier}/TieredCacheEventListener.java (76%) rename server/src/main/java/org/opensearch/{indices => common/cache/tier}/TieredCacheLoader.java (66%) rename server/src/main/java/org/opensearch/{indices => common/cache/tier}/TieredCacheService.java (94%) rename server/src/main/java/org/opensearch/{indices => common/cache/tier}/TieredCacheSpilloverStrategyService.java (92%) create mode 100644 server/src/main/java/org/opensearch/common/cache/tier/package-info.java delete mode 100644 server/src/main/java/org/opensearch/indices/DummyDiskCachingTier.java rename server/src/test/java/org/opensearch/{indices => common/cache/tier}/TieredCacheSpilloverStrategyServiceTests.java (99%) diff --git a/server/src/main/java/org/opensearch/common/cache/RemovalNotification.java b/server/src/main/java/org/opensearch/common/cache/RemovalNotification.java index 71e240064c6ae..2152c4917b62d 100644 --- a/server/src/main/java/org/opensearch/common/cache/RemovalNotification.java +++ b/server/src/main/java/org/opensearch/common/cache/RemovalNotification.java @@ -32,7 +32,7 @@ package org.opensearch.common.cache; -import org.opensearch.indices.TierType; +import org.opensearch.common.cache.tier.TierType; /** * Notification when an element is removed from the cache diff --git a/server/src/main/java/org/opensearch/indices/CachingTier.java b/server/src/main/java/org/opensearch/common/cache/tier/CachingTier.java similarity index 96% rename from server/src/main/java/org/opensearch/indices/CachingTier.java rename to server/src/main/java/org/opensearch/common/cache/tier/CachingTier.java index 7bf7294467114..48fd5deadc111 100644 --- a/server/src/main/java/org/opensearch/indices/CachingTier.java +++ b/server/src/main/java/org/opensearch/common/cache/tier/CachingTier.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.indices; +package org.opensearch.common.cache.tier; import org.opensearch.common.cache.RemovalListener; diff --git a/server/src/main/java/org/opensearch/indices/DiskCachingTier.java b/server/src/main/java/org/opensearch/common/cache/tier/DiskCachingTier.java similarity index 91% rename from server/src/main/java/org/opensearch/indices/DiskCachingTier.java rename to server/src/main/java/org/opensearch/common/cache/tier/DiskCachingTier.java index abc8952c0b8e2..4db71b6378a02 100644 --- a/server/src/main/java/org/opensearch/indices/DiskCachingTier.java +++ b/server/src/main/java/org/opensearch/common/cache/tier/DiskCachingTier.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.indices; +package org.opensearch.common.cache.tier; /** * This is specific to disk caching tier and can be used to add methods which are specific to disk tier. diff --git a/server/src/main/java/org/opensearch/indices/OnHeapCachingTier.java b/server/src/main/java/org/opensearch/common/cache/tier/OnHeapCachingTier.java similarity index 91% rename from server/src/main/java/org/opensearch/indices/OnHeapCachingTier.java rename to server/src/main/java/org/opensearch/common/cache/tier/OnHeapCachingTier.java index f7d68c27c1904..127fa6ee8e6b3 100644 --- a/server/src/main/java/org/opensearch/indices/OnHeapCachingTier.java +++ b/server/src/main/java/org/opensearch/common/cache/tier/OnHeapCachingTier.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.indices; +package org.opensearch.common.cache.tier; /** * This is specific to onHeap caching tier and can be used to add methods which are specific to this tier. diff --git a/server/src/main/java/org/opensearch/indices/OpenSearchOnHeapCache.java b/server/src/main/java/org/opensearch/common/cache/tier/OpenSearchOnHeapCache.java similarity index 80% rename from server/src/main/java/org/opensearch/indices/OpenSearchOnHeapCache.java rename to server/src/main/java/org/opensearch/common/cache/tier/OpenSearchOnHeapCache.java index 189b741ed1696..22d2f769507cf 100644 --- a/server/src/main/java/org/opensearch/indices/OpenSearchOnHeapCache.java +++ b/server/src/main/java/org/opensearch/common/cache/tier/OpenSearchOnHeapCache.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.indices; +package org.opensearch.common.cache.tier; import org.opensearch.common.cache.Cache; import org.opensearch.common.cache.CacheBuilder; @@ -14,21 +14,28 @@ import org.opensearch.common.cache.RemovalNotification; import org.opensearch.common.unit.TimeValue; +import java.util.Objects; import java.util.concurrent.ExecutionException; import java.util.function.ToLongBiFunction; +/** + * This variant of on-heap cache uses OpenSearch custom cache implementation. + * @param Type of key + * @param Type of value + */ public class OpenSearchOnHeapCache implements OnHeapCachingTier, RemovalListener { private final Cache cache; private RemovalListener removalListener; - private OpenSearchOnHeapCache(long maxWeightInBytes, ToLongBiFunction weigher, TimeValue expireAfterAcess) { + private OpenSearchOnHeapCache(Builder builder) { + Objects.requireNonNull(builder.weigher); CacheBuilder cacheBuilder = CacheBuilder.builder() - .setMaximumWeight(maxWeightInBytes) - .weigher(weigher) + .setMaximumWeight(builder.maxWeightInBytes) + .weigher(builder.weigher) .removalListener(this); - if (expireAfterAcess != null) { - cacheBuilder.setExpireAfterAccess(expireAfterAcess); + if (builder.expireAfterAcess != null) { + cacheBuilder.setExpireAfterAccess(builder.expireAfterAcess); } cache = cacheBuilder.build(); } @@ -93,6 +100,11 @@ public void onRemoval(RemovalNotification notification) { removalListener.onRemoval(notification); } + /** + * Builder object + * @param Type of key + * @param Type of value + */ public static class Builder { private long maxWeightInBytes; @@ -118,7 +130,7 @@ public Builder setExpireAfterAccess(TimeValue expireAfterAcess) { } public OpenSearchOnHeapCache build() { - return new OpenSearchOnHeapCache(maxWeightInBytes, weigher, expireAfterAcess); + return new OpenSearchOnHeapCache(this); } } } diff --git a/server/src/main/java/org/opensearch/indices/TierType.java b/server/src/main/java/org/opensearch/common/cache/tier/TierType.java similarity index 77% rename from server/src/main/java/org/opensearch/indices/TierType.java rename to server/src/main/java/org/opensearch/common/cache/tier/TierType.java index 9a286fd26151b..ca61b636c1dda 100644 --- a/server/src/main/java/org/opensearch/indices/TierType.java +++ b/server/src/main/java/org/opensearch/common/cache/tier/TierType.java @@ -6,8 +6,11 @@ * compatible open source license. */ -package org.opensearch.indices; +package org.opensearch.common.cache.tier; +/** + * Tier types in cache. + */ public enum TierType { ON_HEAP, diff --git a/server/src/main/java/org/opensearch/indices/TieredCacheEventListener.java b/server/src/main/java/org/opensearch/common/cache/tier/TieredCacheEventListener.java similarity index 76% rename from server/src/main/java/org/opensearch/indices/TieredCacheEventListener.java rename to server/src/main/java/org/opensearch/common/cache/tier/TieredCacheEventListener.java index 084ac5a57e0d3..05b59bf16b282 100644 --- a/server/src/main/java/org/opensearch/indices/TieredCacheEventListener.java +++ b/server/src/main/java/org/opensearch/common/cache/tier/TieredCacheEventListener.java @@ -6,10 +6,15 @@ * compatible open source license. */ -package org.opensearch.indices; +package org.opensearch.common.cache.tier; import org.opensearch.common.cache.RemovalNotification; +/** + * This can be used to listen to tiered caching events + * @param Type of key + * @param Type of value + */ public interface TieredCacheEventListener { void onMiss(K key, TierType tierType); diff --git a/server/src/main/java/org/opensearch/indices/TieredCacheLoader.java b/server/src/main/java/org/opensearch/common/cache/tier/TieredCacheLoader.java similarity index 66% rename from server/src/main/java/org/opensearch/indices/TieredCacheLoader.java rename to server/src/main/java/org/opensearch/common/cache/tier/TieredCacheLoader.java index f6bb1a74e973e..d720feade0609 100644 --- a/server/src/main/java/org/opensearch/indices/TieredCacheLoader.java +++ b/server/src/main/java/org/opensearch/common/cache/tier/TieredCacheLoader.java @@ -6,8 +6,13 @@ * compatible open source license. */ -package org.opensearch.indices; +package org.opensearch.common.cache.tier; +/** + * Used to load value in tiered cache if not present. + * @param Type of key + * @param Type of value + */ public interface TieredCacheLoader { V load(K key) throws Exception; diff --git a/server/src/main/java/org/opensearch/indices/TieredCacheService.java b/server/src/main/java/org/opensearch/common/cache/tier/TieredCacheService.java similarity index 94% rename from server/src/main/java/org/opensearch/indices/TieredCacheService.java rename to server/src/main/java/org/opensearch/common/cache/tier/TieredCacheService.java index 59e5e0e00b6c1..31d58510206f0 100644 --- a/server/src/main/java/org/opensearch/indices/TieredCacheService.java +++ b/server/src/main/java/org/opensearch/common/cache/tier/TieredCacheService.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.indices; +package org.opensearch.common.cache.tier; import java.util.Optional; diff --git a/server/src/main/java/org/opensearch/indices/TieredCacheSpilloverStrategyService.java b/server/src/main/java/org/opensearch/common/cache/tier/TieredCacheSpilloverStrategyService.java similarity index 92% rename from server/src/main/java/org/opensearch/indices/TieredCacheSpilloverStrategyService.java rename to server/src/main/java/org/opensearch/common/cache/tier/TieredCacheSpilloverStrategyService.java index 7799170a1ede9..c148145274546 100644 --- a/server/src/main/java/org/opensearch/indices/TieredCacheSpilloverStrategyService.java +++ b/server/src/main/java/org/opensearch/common/cache/tier/TieredCacheSpilloverStrategyService.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.indices; +package org.opensearch.common.cache.tier; import org.opensearch.common.cache.RemovalListener; import org.opensearch.common.cache.RemovalNotification; @@ -39,14 +39,10 @@ public class TieredCacheSpilloverStrategyService implements TieredCacheSer */ private final List> cachingTierList; - private TieredCacheSpilloverStrategyService( - OnHeapCachingTier onHeapCachingTier, - DiskCachingTier diskCachingTier, - TieredCacheEventListener tieredCacheEventListener - ) { - this.onHeapCachingTier = Objects.requireNonNull(onHeapCachingTier); - this.diskCachingTier = Optional.ofNullable(diskCachingTier); - this.tieredCacheEventListener = Objects.requireNonNull(tieredCacheEventListener); + private TieredCacheSpilloverStrategyService(Builder builder) { + this.onHeapCachingTier = Objects.requireNonNull(builder.onHeapCachingTier); + this.diskCachingTier = Optional.ofNullable(builder.diskCachingTier); + this.tieredCacheEventListener = Objects.requireNonNull(builder.tieredCacheEventListener); this.cachingTierList = this.diskCachingTier.map(diskTier -> Arrays.asList(onHeapCachingTier, diskTier)) .orElse(List.of(onHeapCachingTier)); setRemovalListeners(); @@ -197,6 +193,11 @@ public static class CacheValue { } } + /** + * Builder object + * @param Type of key + * @param Type of value + */ public static class Builder { private OnHeapCachingTier onHeapCachingTier; private DiskCachingTier diskCachingTier; @@ -220,11 +221,7 @@ public Builder setTieredCacheEventListener(TieredCacheEventListener } public TieredCacheSpilloverStrategyService build() { - return new TieredCacheSpilloverStrategyService( - this.onHeapCachingTier, - this.diskCachingTier, - this.tieredCacheEventListener - ); + return new TieredCacheSpilloverStrategyService(this); } } diff --git a/server/src/main/java/org/opensearch/common/cache/tier/package-info.java b/server/src/main/java/org/opensearch/common/cache/tier/package-info.java new file mode 100644 index 0000000000000..7ad81dbe3073c --- /dev/null +++ b/server/src/main/java/org/opensearch/common/cache/tier/package-info.java @@ -0,0 +1,10 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/** Base package for cache tier support. */ +package org.opensearch.common.cache.tier; diff --git a/server/src/main/java/org/opensearch/index/cache/request/ShardRequestCache.java b/server/src/main/java/org/opensearch/index/cache/request/ShardRequestCache.java index 1beef5217355f..efad437804bef 100644 --- a/server/src/main/java/org/opensearch/index/cache/request/ShardRequestCache.java +++ b/server/src/main/java/org/opensearch/index/cache/request/ShardRequestCache.java @@ -33,9 +33,9 @@ package org.opensearch.index.cache.request; import org.apache.lucene.util.Accountable; +import org.opensearch.common.cache.tier.TierType; import org.opensearch.common.metrics.CounterMetric; import org.opensearch.core.common.bytes.BytesReference; -import org.opensearch.indices.TierType; import java.util.EnumMap; diff --git a/server/src/main/java/org/opensearch/indices/AbstractIndexShardCacheEntity.java b/server/src/main/java/org/opensearch/indices/AbstractIndexShardCacheEntity.java index 2eef16df2bb9a..d9c256b4b4a94 100644 --- a/server/src/main/java/org/opensearch/indices/AbstractIndexShardCacheEntity.java +++ b/server/src/main/java/org/opensearch/indices/AbstractIndexShardCacheEntity.java @@ -34,6 +34,7 @@ import org.opensearch.common.cache.RemovalNotification; import org.opensearch.common.cache.RemovalReason; +import org.opensearch.common.cache.tier.TierType; import org.opensearch.core.common.bytes.BytesReference; import org.opensearch.index.cache.request.ShardRequestCache; import org.opensearch.index.shard.IndexShard; diff --git a/server/src/main/java/org/opensearch/indices/DummyDiskCachingTier.java b/server/src/main/java/org/opensearch/indices/DummyDiskCachingTier.java deleted file mode 100644 index 26a78b6c61920..0000000000000 --- a/server/src/main/java/org/opensearch/indices/DummyDiskCachingTier.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -package org.opensearch.indices; - -import org.opensearch.common.cache.RemovalListener; - -import java.util.Collections; - -public class DummyDiskCachingTier implements CachingTier { - - @Override - public V get(K key) { - return null; - } - - @Override - public void put(K key, V value) {} - - @Override - public V computeIfAbsent(K key, TieredCacheLoader loader) throws Exception { - return null; - } - - @Override - public void invalidate(K key) {} - - @Override - public V compute(K key, TieredCacheLoader loader) throws Exception { - return null; - } - - @Override - public void setRemovalListener(RemovalListener removalListener) {} - - @Override - public void invalidateAll() {} - - @Override - public Iterable keys() { - return Collections::emptyIterator; - } - - @Override - public int count() { - return 0; - } - - @Override - public TierType getTierType() { - return TierType.DISK; - } -} diff --git a/server/src/main/java/org/opensearch/indices/IndicesRequestCache.java b/server/src/main/java/org/opensearch/indices/IndicesRequestCache.java index 6781d3fa0eb2a..c5dcaa7f28811 100644 --- a/server/src/main/java/org/opensearch/indices/IndicesRequestCache.java +++ b/server/src/main/java/org/opensearch/indices/IndicesRequestCache.java @@ -40,6 +40,13 @@ import org.apache.lucene.util.RamUsageEstimator; import org.opensearch.common.CheckedSupplier; import org.opensearch.common.cache.RemovalNotification; +import org.opensearch.common.cache.tier.OnHeapCachingTier; +import org.opensearch.common.cache.tier.OpenSearchOnHeapCache; +import org.opensearch.common.cache.tier.TierType; +import org.opensearch.common.cache.tier.TieredCacheEventListener; +import org.opensearch.common.cache.tier.TieredCacheLoader; +import org.opensearch.common.cache.tier.TieredCacheService; +import org.opensearch.common.cache.tier.TieredCacheSpilloverStrategyService; import org.opensearch.common.lucene.index.OpenSearchDirectoryReader; import org.opensearch.common.settings.Setting; import org.opensearch.common.settings.Setting.Property; @@ -190,7 +197,7 @@ void invalidate(CacheEntity cacheEntity, DirectoryReader reader, BytesReference * * @opensearch.internal */ - private static class Loader implements org.opensearch.indices.TieredCacheLoader { + private static class Loader implements TieredCacheLoader { private final CacheEntity entity; private final CheckedSupplier loader; diff --git a/server/src/test/java/org/opensearch/indices/TieredCacheSpilloverStrategyServiceTests.java b/server/src/test/java/org/opensearch/common/cache/tier/TieredCacheSpilloverStrategyServiceTests.java similarity index 99% rename from server/src/test/java/org/opensearch/indices/TieredCacheSpilloverStrategyServiceTests.java rename to server/src/test/java/org/opensearch/common/cache/tier/TieredCacheSpilloverStrategyServiceTests.java index 4c4c7f195ba31..3cd08df649f72 100644 --- a/server/src/test/java/org/opensearch/indices/TieredCacheSpilloverStrategyServiceTests.java +++ b/server/src/test/java/org/opensearch/common/cache/tier/TieredCacheSpilloverStrategyServiceTests.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.indices; +package org.opensearch.common.cache.tier; import org.opensearch.common.cache.RemovalListener; import org.opensearch.common.cache.RemovalNotification; diff --git a/server/src/test/java/org/opensearch/indices/IndicesServiceCloseTests.java b/server/src/test/java/org/opensearch/indices/IndicesServiceCloseTests.java index 2fe1f87f5d828..38ff7ba8cef21 100644 --- a/server/src/test/java/org/opensearch/indices/IndicesServiceCloseTests.java +++ b/server/src/test/java/org/opensearch/indices/IndicesServiceCloseTests.java @@ -37,6 +37,7 @@ import org.opensearch.cluster.ClusterName; import org.opensearch.cluster.routing.allocation.DiskThresholdSettings; import org.opensearch.common.cache.RemovalNotification; +import org.opensearch.common.cache.tier.TierType; import org.opensearch.common.settings.Settings; import org.opensearch.common.util.concurrent.OpenSearchExecutors; import org.opensearch.core.common.bytes.BytesArray;