Skip to content

Commit

Permalink
fixed various serialization issues relating to silent ehcache put() a…
Browse files Browse the repository at this point in the history
…nd get() failures
  • Loading branch information
Peter Alfonsi committed Oct 13, 2023
1 parent 66915b6 commit f985e63
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 21 deletions.
2 changes: 1 addition & 1 deletion server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ dependencies {
api "jakarta.annotation:jakarta.annotation-api:${versions.jakarta_annotation}"

// ehcache
api "org.ehcache:ehcache:3.10.8"
api "org.ehcache:ehcache:3.10.8" // using 3.3.1 rather than 3.10.8 to test if dependency conflicts are causing my issues
api "org.slf4j:slf4j-api:1.7.36"

testImplementation(project(":test:framework")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 java.io.Serializable;
import java.util.Objects;

public class DummySerializableKey implements Serializable {
private Integer i;
private String s;
public DummySerializableKey(Integer i, String s) {
this.i = i;
this.s = s;
}

public int getI() {
return i;
}
public String getS() {
return s;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof DummySerializableKey)) {
return false;
}
DummySerializableKey other = (DummySerializableKey) o;
return Objects.equals(this.i, other.i) && this.s.equals(other.s);
}
@Override
public final int hashCode() {
int result = 11;
if (i != null) {
result = 31 * result + i.hashCode();
}
if (s != null) {
result = 31 * result + s.hashCode();
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void onEvent(CacheEvent<? extends K, ? extends V> event) {
} else if (oldValue != null && newValue == null) {
counter.dec();
} else {
int j;
int j; // breakpoint
}

// handle creating a RemovalReason, unless eventType is CREATED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class IndicesRequestCacheTests extends OpenSearchTestCase {
Expand Down Expand Up @@ -194,17 +196,58 @@ public void onRemoval(RemovalNotification<Integer, String> notification) {
.defaultPool("default", 0, 4)
.build();

PersistentCacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.using(statsService) // https://stackoverflow.com/questions/40453859/how-to-get-ehcache-3-1-statistics
.using(threadConfig)
.with(CacheManagerBuilder.persistence(EhcacheDiskCachingTier.DISK_CACHE_FP))
.withCache(cacheAlias, CacheConfigurationBuilder.newCacheConfigurationBuilder(
IndicesRequestCache.Key.class, String.class, ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.MB, false))
.withService(listenerConfig) // stackoverflow shows .add(), but IDE says this is deprecated. idk
).build(true);
Cache<IndicesRequestCache.Key, String> cache = cacheManager.getCache(cacheAlias, IndicesRequestCache.Key.class, String.class);
PersistentCacheManager cacheManager;

boolean doIntCache = false;

if (doIntCache) {
cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.using(statsService) // https://stackoverflow.com/questions/40453859/how-to-get-ehcache-3-1-statistics
.using(threadConfig)
.with(CacheManagerBuilder.persistence(EhcacheDiskCachingTier.DISK_CACHE_FP))
.withCache(cacheAlias, CacheConfigurationBuilder.newCacheConfigurationBuilder(
Integer.class, String.class, ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.MB, false))
.withService(listenerConfig)
).build(true);
Cache<Integer, String> integerCache = cacheManager.getCache(cacheAlias, Integer.class, String.class);

integerCache.put(0, "blorp");
System.out.println("Counter value = " + count.count());
String res = integerCache.get(0);
System.out.println("Got result " + res);
System.out.println("Counter value = " + count.count());
} else {
cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.using(statsService) // https://stackoverflow.com/questions/40453859/how-to-get-ehcache-3-1-statistics
.using(threadConfig)
.with(CacheManagerBuilder.persistence(EhcacheDiskCachingTier.DISK_CACHE_FP))
.withCache(cacheAlias, CacheConfigurationBuilder.newCacheConfigurationBuilder(
DummySerializableKey.class, String.class, ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.MB, false))
.withService(listenerConfig)
).build(true);
Cache<DummySerializableKey, String> cache = cacheManager.getCache(cacheAlias, DummySerializableKey.class, String.class);

DummySerializableKey key = new DummySerializableKey(Integer.valueOf(0), "blah");
cache.put(key, "blorp");
System.out.println("Counter value = " + count.count());
String res = cache.get(key);
System.out.println("Got result " + res);
System.out.println("Counter value = " + count.count());
TierStatistics ts = statsService.getCacheStatistics(cacheAlias).getTierStatistics().get("Disk");
System.out.println("self-reported count = " + ts.getMappings());
System.out.println("self-reported misses = " + ts.getMisses());
System.out.println("self-reported hits = " + ts.getHits());

List<Cache.Entry<DummySerializableKey, String>> foos = new ArrayList<>();
for(Cache.Entry<DummySerializableKey, String> entry : cache) {
foos.add(entry);
}
int j = 0;
j++;
System.out.println(j);
}

Directory dir = newDirectory();
/*Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig());
writer.addDocument(newDoc(0, "foo"));
DirectoryReader reader = OpenSearchDirectoryReader.wrap(DirectoryReader.open(writer), new ShardId("foo", "bar", 1));
Expand All @@ -216,19 +259,12 @@ public void onRemoval(RemovalNotification<Integer, String> notification) {
IndicesRequestCache.Key[] keys = new IndicesRequestCache.Key[9];
TermQueryBuilder termQuery = new TermQueryBuilder("id", "0");
BytesReference termBytes = XContentHelper.toXContent(termQuery, MediaTypeRegistry.JSON, false);
IndicesRequestCache.Key key = new IndicesRequestCache.Key(entity, reader.getReaderCacheHelper().getKey(), termBytes);

cache.put(key, "blorp");
System.out.println("Counter value = " + count.count());
String res = cache.get(key);
System.out.println("Got result " + res);
IndicesRequestCache.Key key = new IndicesRequestCache.Key(entity, reader.getReaderCacheHelper().getKey(), termBytes);*/

System.out.println("Counter value = " + count.count());
//System.out.println("Hits = " + statsService.getCacheStatistics(cacheAlias).getTierStatistics().get("Disk").getHits());

cacheManager.removeCache(cacheAlias);
cacheManager.close();
IOUtils.close(reader, writer, dir);
//IOUtils.close(reader, writer, dir);
}

public void testSpillover() throws Exception {
Expand Down

0 comments on commit f985e63

Please sign in to comment.