Rename Priority to Source. This should make it more clear what is the intention of the usage.

Rename:
LOW to NETWORK
MID to PRIVATE_LOW_PRIO
HIGH to PRIVATE

Change delay of MID/PRIVATE_LOW_PRIO from 30 min to 2 hours (we had different datastores before using it, now its only real low prio stores)

Add comment to each enum
This commit is contained in:
chimp1984 2020-10-02 15:48:12 -05:00
parent 137b5e81d1
commit e9db7c6808
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3
27 changed files with 47 additions and 41 deletions

View File

@ -57,7 +57,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Responsible for reading persisted data and writing it on disk. We read usually only at start-up and keep data in RAM.
* We write all data which got a request for persistence at shut down at the very last moment when all other services
* are shut down, so allowing changes to the data in the very last moment. For critical data we set {@link Priority}
* are shut down, so allowing changes to the data in the very last moment. For critical data we set {@link Source}
* to HIGH which causes a timer to trigger a write to disk after 1 minute. We use that for not very frequently altered
* data and data which cannot be recovered from the network.
*
@ -94,7 +94,7 @@ public class PersistenceManager<T extends PersistableEnvelope> {
// For Priority.HIGH data we want to write to disk in any case to be on the safe side if we might have missed
// a requestPersistence call after an important state update. Those are usually rather small data stores.
// Otherwise we only persist if requestPersistence was called since the last persist call.
if (persistenceManager.priority.flushAtShutDown || persistenceManager.persistenceRequested) {
if (persistenceManager.SOURCE.flushAtShutDown || persistenceManager.persistenceRequested) {
// We don't know from which thread we are called so we map back to user thread when calling persistNow
UserThread.execute(() -> {
// We always get our completeHandler called even if exceptions happen. In case a file write fails
@ -124,10 +124,16 @@ public class PersistenceManager<T extends PersistableEnvelope> {
// Enum
///////////////////////////////////////////////////////////////////////////////////////////
public enum Priority {
LOW(1, TimeUnit.HOURS.toSeconds(1), false),
MID(4, TimeUnit.MINUTES.toSeconds(30), false),
HIGH(10, TimeUnit.SECONDS.toSeconds(30), true);
public enum Source {
// For data stores we received from the network and which could be rebuilt. We store only for avoiding too much network traffic.
NETWORK(1, TimeUnit.HOURS.toSeconds(1), false),
// For data stores which are created from private local data. This data could only be rebuilt from backup files.
PRIVATE(10, TimeUnit.SECONDS.toSeconds(30), true),
// For data stores which are created from private local data. Loss of that data would not have any critical consequences.
PRIVATE_LOW_PRIO(4, TimeUnit.HOURS.toSeconds(2), false);
@Getter
private final int numMaxBackupFiles;
@ -136,7 +142,7 @@ public class PersistenceManager<T extends PersistableEnvelope> {
@Getter
private final boolean flushAtShutDown;
Priority(int numMaxBackupFiles, long delayInSec, boolean flushAtShutDown) {
Source(int numMaxBackupFiles, long delayInSec, boolean flushAtShutDown) {
this.numMaxBackupFiles = numMaxBackupFiles;
this.delayInSec = delayInSec;
this.flushAtShutDown = flushAtShutDown;
@ -154,7 +160,7 @@ public class PersistenceManager<T extends PersistableEnvelope> {
private File storageFile;
private T persistable;
private String fileName;
private Priority priority = Priority.MID;
private Source SOURCE = SOURCE.PRIVATE_LOW_PRIO;
private Path usedTempFilePath;
private volatile boolean persistenceRequested;
@Nullable
@ -179,14 +185,14 @@ public class PersistenceManager<T extends PersistableEnvelope> {
// API
///////////////////////////////////////////////////////////////////////////////////////////
public void initialize(T persistable, Priority priority) {
this.initialize(persistable, persistable.getDefaultStorageFileName(), priority);
public void initialize(T persistable, Source SOURCE) {
this.initialize(persistable, persistable.getDefaultStorageFileName(), SOURCE);
}
public void initialize(T persistable, String fileName, Priority priority) {
public void initialize(T persistable, String fileName, Source SOURCE) {
this.persistable = persistable;
this.fileName = fileName;
this.priority = priority;
this.SOURCE = SOURCE;
storageFile = new File(dir, fileName);
ALL_PERSISTENCE_MANAGERS.put(fileName, this);
}
@ -262,7 +268,7 @@ public class PersistenceManager<T extends PersistableEnvelope> {
timer = UserThread.runPeriodically(() -> {
persistNow(null);
UserThread.execute(() -> timer = null);
}, priority.delayInSec, TimeUnit.SECONDS);
}, SOURCE.delayInSec, TimeUnit.SECONDS);
}
}
@ -293,7 +299,7 @@ public class PersistenceManager<T extends PersistableEnvelope> {
try {
// Before we write we backup existing file
FileUtil.rollingBackup(dir, fileName, priority.getNumMaxBackupFiles());
FileUtil.rollingBackup(dir, fileName, SOURCE.getNumMaxBackupFiles());
if (!dir.exists() && !dir.mkdir())
log.warn("make dir failed {}", fileName);
@ -364,7 +370,7 @@ public class PersistenceManager<T extends PersistableEnvelope> {
",\n dir=" + dir +
",\n storageFile=" + storageFile +
",\n persistable=" + persistable +
",\n priority=" + priority +
",\n priority=" + SOURCE +
",\n usedTempFilePath=" + usedTempFilePath +
",\n persistenceRequested=" + persistenceRequested +
"\n}";

View File

@ -54,7 +54,7 @@ public class SignedWitnessStorageService extends MapStoreService<SignedWitnessSt
@Override
protected void initializePersistenceManager() {
persistenceManager.initialize(store, PersistenceManager.Priority.LOW);
persistenceManager.initialize(store, PersistenceManager.Source.NETWORK);
}
@Override

View File

@ -54,7 +54,7 @@ public class AccountAgeWitnessStorageService extends MapStoreService<AccountAgeW
@Override
protected void initializePersistenceManager() {
persistenceManager.initialize(store, PersistenceManager.Priority.LOW);
persistenceManager.initialize(store, PersistenceManager.Source.NETWORK);
}
@Override

View File

@ -57,7 +57,7 @@ public final class AddressEntryList implements PersistableEnvelope, PersistedDat
public AddressEntryList(PersistenceManager<AddressEntryList> persistenceManager) {
this.persistenceManager = persistenceManager;
this.persistenceManager.initialize(this, PersistenceManager.Priority.HIGH);
this.persistenceManager.initialize(this, PersistenceManager.Source.PRIVATE);
}
@Override

View File

@ -73,7 +73,7 @@ public class BallotListService implements PersistedDataHost, DaoSetupService {
this.validatorProvider = validatorProvider;
this.persistenceManager = persistenceManager;
this.persistenceManager.initialize(ballotList, PersistenceManager.Priority.LOW);
this.persistenceManager.initialize(ballotList, PersistenceManager.Source.NETWORK);
}

View File

@ -136,7 +136,7 @@ public class MyBlindVoteListService implements PersistedDataHost, DaoStateListen
this.myVoteListService = myVoteListService;
this.myProposalListService = myProposalListService;
this.persistenceManager.initialize(myBlindVoteList, PersistenceManager.Priority.HIGH);
this.persistenceManager.initialize(myBlindVoteList, PersistenceManager.Source.PRIVATE);
numConnectedPeersListener = (observable, oldValue, newValue) -> maybeRePublishMyBlindVote();
}

View File

@ -65,7 +65,7 @@ public class BlindVoteStorageService extends MapStoreService<BlindVoteStore, Per
@Override
protected void initializePersistenceManager() {
persistenceManager.initialize(store, PersistenceManager.Priority.LOW);
persistenceManager.initialize(store, PersistenceManager.Source.NETWORK);
}
@Override

View File

@ -46,7 +46,7 @@ public class MyReputationListService implements PersistedDataHost, DaoSetupServi
@Inject
public MyReputationListService(PersistenceManager<MyReputationList> persistenceManager) {
this.persistenceManager = persistenceManager;
persistenceManager.initialize(myReputationList, PersistenceManager.Priority.HIGH);
persistenceManager.initialize(myReputationList, PersistenceManager.Source.PRIVATE);
}

View File

@ -60,7 +60,7 @@ public class MyVoteListService implements PersistedDataHost {
this.daoStateService = daoStateService;
this.persistenceManager = persistenceManager;
this.persistenceManager.initialize(myVoteList, PersistenceManager.Priority.HIGH);
this.persistenceManager.initialize(myVoteList, PersistenceManager.Source.PRIVATE);
}

View File

@ -46,7 +46,7 @@ public class MyProofOfBurnListService implements PersistedDataHost, DaoSetupServ
@Inject
public MyProofOfBurnListService(PersistenceManager<MyProofOfBurnList> persistenceManager) {
this.persistenceManager = persistenceManager;
persistenceManager.initialize(myProofOfBurnList, PersistenceManager.Priority.HIGH);
persistenceManager.initialize(myProofOfBurnList, PersistenceManager.Source.PRIVATE);
}

View File

@ -92,7 +92,7 @@ public class MyProposalListService implements PersistedDataHost, DaoStateListene
this.walletsManager = walletsManager;
this.persistenceManager = persistenceManager;
this.persistenceManager.initialize(myProposalList, PersistenceManager.Priority.HIGH);
this.persistenceManager.initialize(myProposalList, PersistenceManager.Source.PRIVATE);
signaturePubKey = pubKeyRing.getSignaturePubKey();

View File

@ -59,7 +59,7 @@ public class ProposalStorageService extends MapStoreService<ProposalStore, Persi
@Override
protected void initializePersistenceManager() {
persistenceManager.initialize(store, PersistenceManager.Priority.LOW);
persistenceManager.initialize(store, PersistenceManager.Source.NETWORK);
}
@Override

View File

@ -59,7 +59,7 @@ public class TempProposalStorageService extends MapStoreService<TempProposalStor
@Override
protected void initializePersistenceManager() {
persistenceManager.initialize(store, PersistenceManager.Priority.LOW);
persistenceManager.initialize(store, PersistenceManager.Source.NETWORK);
}
@Override

View File

@ -128,6 +128,6 @@ public class DaoStateStorageService extends StoreService<DaoStateStore> {
@Override
protected void initializePersistenceManager() {
persistenceManager.initialize(store, PersistenceManager.Priority.LOW);
persistenceManager.initialize(store, PersistenceManager.Source.NETWORK);
}
}

View File

@ -46,7 +46,7 @@ public class UnconfirmedBsqChangeOutputListService implements PersistedDataHost
public UnconfirmedBsqChangeOutputListService(PersistenceManager<UnconfirmedBsqChangeOutputList> persistenceManager) {
this.persistenceManager = persistenceManager;
this.persistenceManager.initialize(unconfirmedBsqChangeOutputList, PersistenceManager.Priority.HIGH);
this.persistenceManager.initialize(unconfirmedBsqChangeOutputList, PersistenceManager.Source.PRIVATE);
}

View File

@ -162,7 +162,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe
this.filterManager = filterManager;
this.persistenceManager = persistenceManager;
this.persistenceManager.initialize(openOffers, "OpenOffers", PersistenceManager.Priority.HIGH);
this.persistenceManager.initialize(openOffers, "OpenOffers", PersistenceManager.Source.PRIVATE);
}
@Override

View File

@ -63,7 +63,7 @@ public abstract class DisputeListService<T extends DisputeList<Dispute>> impleme
this.persistenceManager = persistenceManager;
disputeList = getConcreteDisputeList();
this.persistenceManager.initialize(disputeList, getFileName(), PersistenceManager.Priority.HIGH);
this.persistenceManager.initialize(disputeList, getFileName(), PersistenceManager.Source.PRIVATE);
}

View File

@ -178,7 +178,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
this.allowFaultyDelayedTxs = allowFaultyDelayedTxs;
this.persistenceManager = persistenceManager;
this.persistenceManager.initialize(tradableList, "PendingTrades", PersistenceManager.Priority.HIGH);
this.persistenceManager.initialize(tradableList, "PendingTrades", PersistenceManager.Source.PRIVATE);
p2PService.addDecryptedDirectMessageListener(this);

View File

@ -56,7 +56,7 @@ public class ClosedTradableManager implements PersistedDataHost {
this.dumpDelayedPayoutTx = dumpDelayedPayoutTx;
this.persistenceManager = persistenceManager;
this.persistenceManager.initialize(closedTradables, "ClosedTrades", PersistenceManager.Priority.HIGH);
this.persistenceManager.initialize(closedTradables, "ClosedTrades", PersistenceManager.Source.PRIVATE);
}
@Override

View File

@ -66,7 +66,7 @@ public class FailedTradesManager implements PersistedDataHost {
this.dumpDelayedPayoutTx = dumpDelayedPayoutTx;
this.persistenceManager = persistenceManager;
this.persistenceManager.initialize(failedTrades, "FailedTrades", PersistenceManager.Priority.HIGH);
this.persistenceManager.initialize(failedTrades, "FailedTrades", PersistenceManager.Source.PRIVATE);
}
@Override

View File

@ -55,7 +55,7 @@ public class TradeStatistics2StorageService extends MapStoreService<TradeStatist
@Override
protected void initializePersistenceManager() {
persistenceManager.initialize(store, PersistenceManager.Priority.LOW);
persistenceManager.initialize(store, PersistenceManager.Source.NETWORK);
}
@Override

View File

@ -265,7 +265,7 @@ public final class Preferences implements PersistedDataHost, BridgeAddressProvid
prefPayload.setSellScreenCurrencyCode(preferredTradeCurrency.getCode());
}
persistenceManager.initialize(prefPayload, PersistenceManager.Priority.HIGH);
persistenceManager.initialize(prefPayload, PersistenceManager.Source.PRIVATE);
// We don't want to pass Preferences to all popups where the don't show again checkbox is used, so we use
// that static lookup class to avoid static access to the Preferences directly.

View File

@ -95,7 +95,7 @@ public class User implements PersistedDataHost {
userPayload = persisted;
}
persistenceManager.initialize(userPayload, PersistenceManager.Priority.HIGH);
persistenceManager.initialize(userPayload, PersistenceManager.Source.PRIVATE);
checkNotNull(userPayload.getPaymentAccounts(), "userPayload.getPaymentAccounts() must not be null");
checkNotNull(userPayload.getAcceptedLanguageLocaleCodes(), "userPayload.getAcceptedLanguageLocaleCodes() must not be null");

View File

@ -75,7 +75,7 @@ public final class Navigation implements PersistedDataHost {
public Navigation(PersistenceManager<NavigationPath> persistenceManager) {
this.persistenceManager = persistenceManager;
persistenceManager.initialize(navigationPath, PersistenceManager.Priority.MID);
persistenceManager.initialize(navigationPath, PersistenceManager.Source.PRIVATE_LOW_PRIO);
}
@Override

View File

@ -215,7 +215,7 @@ public class GUIUtil {
if (!directory.isEmpty()) {
PersistenceManager<PersistableEnvelope> persistenceManager = new PersistenceManager<>(new File(directory), persistenceProtoResolver, corruptedStorageFileHandler);
PaymentAccountList paymentAccounts = new PaymentAccountList(accounts);
persistenceManager.initialize(paymentAccounts, fileName, PersistenceManager.Priority.HIGH);
persistenceManager.initialize(paymentAccounts, fileName, PersistenceManager.Source.PRIVATE_LOW_PRIO);
persistenceManager.persistNow(() -> {
persistenceManager.shutdown();
new Popup().feedback(Res.get("guiUtil.accountExport.savedToPath",

View File

@ -138,7 +138,7 @@ public class PeerManager implements ConnectionListener, PersistedDataHost {
this.clockWatcher = clockWatcher;
this.persistenceManager = persistenceManager;
this.persistenceManager.initialize(peerList, PersistenceManager.Priority.MID);
this.persistenceManager.initialize(peerList, PersistenceManager.Source.PRIVATE_LOW_PRIO);
this.networkNode.addConnectionListener(this);
setConnectionLimits(maxConnections);

View File

@ -167,7 +167,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
networkNode.addMessageListener(this);
networkNode.addConnectionListener(this);
this.persistenceManager.initialize(sequenceNumberMap, PersistenceManager.Priority.HIGH);
this.persistenceManager.initialize(sequenceNumberMap, PersistenceManager.Source.PRIVATE);
}
@Override