Concurrency issue
Hello everybody!
I am currently evaluating Chronicle-Map as an off heap cache solution for our SSP. For now i am using read-only caches to simplify the integration. I have a scheduled job that creates, persists and uploads the map-file to a distributed storage for our core instances to pick up and load the memory mapped file off heap.
- Job runs jdk-8u181
- Core instances run OpenJDK 11 on OpenJ9 (https://adoptopenjdk.net/releases.html?variant=openjdk11&jvmVariant=openj9), default gc, with
-XX:MaxDirectMemorySize=500m - The same value interface is used on both systems.
public interface TheValueInterface extends BytesMarshallable, Copyable<TheValueInterface> {... - BytesMarshallable methods are not overwritten
- Map creation:
ChronicleMapBuilder builder = ChronicleMapBuilder
.of(getKeyClass(), getValueClass())
.name(dataFileName)
.entries(data.size());
addAllToChronicleMap(builder, data, extractKey);
protected void addAllToChronicleMap(ChronicleMapBuilder builder, List<?> data, Function<Object,Object> extractKey) {
try (ChronicleMap<Object, Object> map = builder.createPersistedTo(localOffHeapFile)) {
for (Object item : data) {
try {
map.putIfAbsent(
getKeyClass().cast(extractKey.apply(item)),
getValueClass().cast(item)
);
} catch (Exception e) {
logger.error("Failed to insert item with key: {}", extractKey.apply(item));
throw new JobExecutionException(e);
}
}
} catch (Exception e) {
logger.error("Failed to persist ChronicalMap for {}", getValueClass().getName());
throw new JobExecutionException(e);
} finally {
gzipFile(localOffHeapFile.toPath());
}
}
- Core instance uses vertX + rxJava for multi thread request processing, where a request is processed by only one thread of the vertX thread pool
- hashCode, equals...
@Override
public boolean equals(Object theValueInterface) {
return theValueInterface != null && theValueInterface.hashCode() == this.hashCode();
}
@Override
public int hashCode() {
return id;
}
@Override
public String toString() {
return String.valueOf(id);
}
@Override //to allow easy data exchange: onHeapPoint.copyFrom(offHeapPoint)
public void copyFrom(TheValueInterface from) {...
- How we access the map:
// MyCollection holds the chronicle map and last_updated time
private AtomicReference<MyCollection> referenceToMy = new AtomicReference<>();
public Optional<MyWrapper> getIfExists(String hash) {
return getsCachedCollection()
.map(MyCollection::getMyHashLookup)
.map(getMyHashLookup -> getMyHashLookup
.getUsing(hash.toUpperCase(), Values.newNativeReference(MyValueInterface.class)))
.map(MyWrapper::new);
}
private Optional<MyCollection> getsCachedCollection() {
return Optional.ofNullable(referenceToMy.get());
}
During prod tests we saw that system metrics show a better gc performance and a more flattened out cpu usage, but unfortunately we also got complains from our customers. The cached data was messed up.
We could reproduce the issue locally by increasing the concurrent load of our application while using only 3 specific requests that would fetch just 3 objects from the chronicle map. The data was messed up between threads again. We saw only the entries of the 3 distinct request, which means that the map is returning the right object but than somehow mixes them between threads.
The issue We fetch the objects from the chronicle map and than reference string attributes from getters of the value interface to another class. During the request processing we compare those values to the value interface getter again. At this point we see the attributes (string values) are not equal for all performed request. The value interface suddenly returns the to the key corresponding object/attributes but what it returned before was the value from one of the other requests. This happens only when we increase the load. Could it be that ChronicleMap is trying to reuse objects by changing the references only and that is messing up the data than? Does ChronicleMaps use a thread pool or smth similar internally when fetching value interfaces and/or their attributes?
@leventov any feedback?
@reshadde can you reproduce with a unit test that you can share please?
Closing due to no response from the TS - feel free to reopen
Released in Chronicle-Map-3.20.2, BOM-2.20.17