Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

Ability to toggle cache on/off #1706

Open
wants to merge 2 commits into
base: cache
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/main/java/com/iota/iri/controllers/ApproveeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public static ApproveeViewModel first(Tangle tangle) throws Exception {
@Override
public boolean store(Tangle tangle) throws Exception {
Cache<Indexable, ApproveeViewModel> cache = tangle.getCache(ApproveeViewModel.class);
if (cache == null) {
return true;
}
Comment on lines +100 to +102
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So store doesn't do anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

ApproveeViewModel approveeViewModel = cache.get(hash);
if (approveeViewModel != null) {
return true;
Expand Down Expand Up @@ -151,10 +154,12 @@ public ApproveeViewModel next(Tangle tangle) throws Exception {
*/
public static void cachePut(Tangle tangle, ApproveeViewModel approveeViewModel, Indexable hash) throws Exception {
Cache<Indexable, ApproveeViewModel> cache = tangle.getCache(ApproveeViewModel.class);
if (cache.getSize() == cache.getConfiguration().getMaxSize()) {
cacheEvict(tangle);
if (cache != null) {
if (cache.getSize() == cache.getConfiguration().getMaxSize()) {
cacheEvict(tangle);
}
cache.put(hash, approveeViewModel);
}
cache.put(hash, approveeViewModel);
}

/**
Expand All @@ -164,7 +169,10 @@ public static void cachePut(Tangle tangle, ApproveeViewModel approveeViewModel,
* @param hash Hash of item to evict
*/
public static void cacheDelete(Tangle tangle, Indexable hash) {
tangle.getCache(ApproveeViewModel.class).evict(hash);
Cache<Indexable, ApproveeViewModel> cache = tangle.getCache(ApproveeViewModel.class);
if (cache != null) {
cache.evict(hash);
}
}

/**
Expand All @@ -175,6 +183,9 @@ public static void cacheDelete(Tangle tangle, Indexable hash) {
*/
public static void cacheEvict(Tangle tangle) throws Exception {
Cache<Indexable, ApproveeViewModel> cache = tangle.getCache(ApproveeViewModel.class);
if (cache == null) {
return;
}
for (int i = 0; i < cache.getConfiguration().getEvictionCount(); i++) {
Indexable hash = cache.nextEvictionKey();
if (hash != null) {
Expand Down
36 changes: 31 additions & 5 deletions src/main/java/com/iota/iri/controllers/TransactionViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,11 @@ public static boolean mightExist(Tangle tangle, Hash hash) throws Exception {
* @throws Exception Thrown if there is an error determining if the transaction exists or not
*/
public static boolean exists(Tangle tangle, Hash hash) throws Exception {
return tangle.getCache(TransactionViewModel.class).get(hash) != null || tangle.exists(Transaction.class, hash);
Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class);
if (cache != null) {
return cache.get(hash) != null || tangle.exists(Transaction.class, hash);
}
return tangle.exists(Transaction.class, hash);
}

/**
Expand Down Expand Up @@ -310,7 +314,13 @@ public void update(Tangle tangle, Snapshot initialSnapshot, String item) throws
return;
}

TransactionViewModel cachedTvm = tangle.getCache(TransactionViewModel.class).get(hash);
Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class);
if (cache == null) {
updateDB(tangle, transaction, hash, item);
return;
}

TransactionViewModel cachedTvm = cache.get(hash);
if (cachedTvm != null) {
this.isCacheEntryFresh = false;
}
Expand Down Expand Up @@ -442,8 +452,12 @@ public boolean store(Tangle tangle, Snapshot initialSnapshot) throws Exception {
// non-cached operations.
List<Pair<Indexable, Persistable>> batch = getSaveBatch();
cacheApprovees(tangle);
if (tangle.getCache(TransactionViewModel.class).get(hash) == null) {
cachePut(tangle, this, hash);

Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class);
if (cache != null) {
if (cache.get(hash) == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cachePut(tangle, this, hash);
}
}

if (tangle.exists(Transaction.class, hash)) {
Expand All @@ -455,6 +469,9 @@ public boolean store(Tangle tangle, Snapshot initialSnapshot) throws Exception {

private void cacheApprovees(Tangle tangle) throws Exception {
Cache<Indexable, ApproveeViewModel> approveeViewModelCache = tangle.getCache(ApproveeViewModel.class);
if (approveeViewModelCache == null) {
return;
}
ApproveeViewModel branchViewModel = ApproveeViewModel.load(tangle, getBranchTransactionHash());
branchViewModel.addHash(hash);
approveeViewModelCache.put(getBranchTransactionHash(), branchViewModel);
Expand Down Expand Up @@ -976,6 +993,9 @@ public String toString() {
*/
private static void cachePut(Tangle tangle, TransactionViewModel transactionViewModel, Hash hash) throws Exception {
Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class);
if (cache == null) {
return;
}
if (cache.getSize() == cache.getConfiguration().getMaxSize()) {
cacheEvict(tangle);
}
Expand All @@ -989,6 +1009,9 @@ private static void cachePut(Tangle tangle, TransactionViewModel transactionView
*/
public static void cacheEvict(Tangle tangle) throws Exception {
Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class);
if (cache == null) {
return;
}
for (int i = 0; i < cache.getConfiguration().getEvictionCount(); i++) {
Indexable hash = cache.nextEvictionKey();
if (hash != null) {
Expand All @@ -1009,7 +1032,10 @@ public static void cacheEvict(Tangle tangle) throws Exception {
* @param hash hash to evict
*/
private static void cacheDelete(Tangle tangle, Hash hash) {
tangle.getCache(TransactionViewModel.class).evict(hash);
Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class);
if (cache != null) {
cache.evict(hash);
}
}

/**
Expand Down
41 changes: 25 additions & 16 deletions src/main/java/com/iota/iri/storage/Tangle.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,13 @@
import com.iota.iri.controllers.TransactionViewModel;
import com.iota.iri.model.Hash;
import com.iota.iri.model.StateDiff;
import com.iota.iri.model.persistables.Address;
import com.iota.iri.model.persistables.Approvee;
import com.iota.iri.model.persistables.Bundle;
import com.iota.iri.model.persistables.Milestone;
import com.iota.iri.model.persistables.ObsoleteTag;
import com.iota.iri.model.persistables.Tag;
import com.iota.iri.model.persistables.Transaction;
import com.iota.iri.model.persistables.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import com.iota.iri.utils.Pair;
import com.iota.iri.zmq.MessageQueueProvider;

import javax.naming.OperationNotSupportedException;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -58,6 +45,7 @@ public class Tangle {
private final List<PersistenceProvider> persistenceProviders = new ArrayList<>();
private final List<MessageQueueProvider> messageQueueProviders = new ArrayList<>();
private CacheManager cacheManager;
private boolean isCacheEnabled = true;

public void addPersistenceProvider(PersistenceProvider provider) {
this.persistenceProviders.add(provider);
Expand Down Expand Up @@ -398,7 +386,28 @@ public void clearMetadata(Class<?> column) throws Exception {
* @return The cache with the specified type
*/
public <T> Cache<Indexable, T> getCache(Class<T> type){
return getCacheManager().getCache(type);
if (!isCacheEnabled) {
return null;
}
return getCacheManager().getCache(type);
}

/**
* Gets if caching is enabled or not.
*
* @return True if caching is enabled. Else, returns false.
*/
public boolean isCacheEnabled() {
return isCacheEnabled;
}

/**
* Sets caching on or off.
*
* @param cacheEnabled The value to set.
*/
public void setCacheEnabled(boolean cacheEnabled) {
isCacheEnabled = cacheEnabled;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
package com.iota.iri.service.tipselection.impl;

import com.iota.iri.TransactionTestUtils;
import com.iota.iri.cache.Cache;
import com.iota.iri.conf.MainnetConfig;
import com.iota.iri.conf.TipSelConfig;
import com.iota.iri.controllers.TransactionViewModel;
import com.iota.iri.model.Hash;
import com.iota.iri.service.ledger.LedgerService;
import com.iota.iri.service.snapshot.SnapshotProvider;
import com.iota.iri.service.snapshot.impl.SnapshotMockUtils;
import com.iota.iri.storage.Indexable;
import com.iota.iri.storage.Tangle;
import com.iota.iri.storage.rocksDB.RocksDBPersistenceProvider;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;

import org.junit.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import org.junit.rules.TemporaryFolder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import static com.iota.iri.TransactionTestUtils.getTransactionTritsWithTrunkAndBranch;
import static com.iota.iri.TransactionTestUtils.getTransactionHash;

import java.util.HashMap;
import java.util.HashSet;
import static com.iota.iri.TransactionTestUtils.getTransactionTritsWithTrunkAndBranch;

public class WalkValidatorImplTest {

Expand All @@ -54,7 +51,10 @@ public static void tearDown() throws Exception {

@After
public void clearCache(){
tangle.getCache(TransactionViewModel.class).clear();
Cache<Indexable, TransactionViewModel> cache = tangle.getCache(TransactionViewModel.class);
if (cache != null) {
cache.clear();
}
}

@BeforeClass
Expand Down