partitions cache improvements

This commit is contained in:
Andrii Shvaika 2020-12-21 17:05:12 +02:00 committed by Andrew Shvayka
parent 0a2255d055
commit 2ea3b18738
2 changed files with 23 additions and 22 deletions

View File

@ -404,31 +404,32 @@ public class CassandraBaseTimeseriesDao extends CassandraAbstractAsyncDao implem
return doSavePartition(tenantId, entityId, key, ttl, partition);
} else {
CassandraPartitionCacheKey partitionSearchKey = new CassandraPartitionCacheKey(entityId, key, partition);
CompletableFuture<Boolean> hasInCacheFuture = cassandraTsPartitionsCache.has(partitionSearchKey);
if (hasInCacheFuture == null) {
return doSavePartitionWithCache(tenantId, entityId, key, partition, partitionSearchKey, ttl);
if (!cassandraTsPartitionsCache.has(partitionSearchKey)) {
ListenableFuture<Void> result = doSavePartition(tenantId, entityId, key, ttl, partition);
Futures.addCallback(result, new CacheCallback<>(partitionSearchKey), MoreExecutors.directExecutor());
return result;
} else {
long finalTtl = ttl;
SettableFuture<Void> futureResult = SettableFuture.create();
hasInCacheFuture.whenComplete((result, throwable) -> {
if (throwable != null) {
futureResult.setException(throwable);
} else if (result) {
futureResult.set(null);
} else {
futureResult.setFuture(doSavePartitionWithCache(tenantId, entityId, key, partition, partitionSearchKey, finalTtl));
}
});
return futureResult;
return Futures.immediateFuture(null);
}
}
}
private ListenableFuture<Void> doSavePartitionWithCache(TenantId tenantId, EntityId entityId, String key, long partition, CassandraPartitionCacheKey partitionSearchKey, long ttl) {
return Futures.transform(doSavePartition(tenantId, entityId, key, ttl, partition), input -> {
cassandraTsPartitionsCache.put(partitionSearchKey);
return input;
}, readResultsProcessingExecutor);
private class CacheCallback<Void> implements FutureCallback<Void> {
private final CassandraPartitionCacheKey key;
private CacheCallback(CassandraPartitionCacheKey key) {
this.key = key;
}
@Override
public void onSuccess(Void result) {
cassandraTsPartitionsCache.put(key);
}
@Override
public void onFailure(Throwable t) {
}
}
private ListenableFuture<Void> doSavePartition(TenantId tenantId, EntityId entityId, String key, long ttl, long partition) {

View File

@ -32,8 +32,8 @@ public class CassandraTsPartitionsCache {
});
}
public CompletableFuture<Boolean> has(CassandraPartitionCacheKey key) {
return partitionsCache.getIfPresent(key);
public boolean has(CassandraPartitionCacheKey key) {
return partitionsCache.getIfPresent(key) != null;
}
public void put(CassandraPartitionCacheKey key) {