Skip to content

Commit

Permalink
Merge branch 'dont-persist-pruning-state' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Gal Rogozinski committed Feb 13, 2019
2 parents 0a76c9b + 53e9669 commit a14e1f5
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 97 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/iota/iri/Iota.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ private void injectDependencies() throws SnapshotException, TransactionPruningEx
ledgerService.init(tangle, snapshotProvider, snapshotService, milestoneService, spentAddressesService,
bundleValidator);
if (transactionPruner != null) {
transactionPruner.init(tangle, snapshotProvider, spentAddressesService, tipsViewModel, configuration)
.restoreState();
transactionPruner.init(tangle, snapshotProvider, spentAddressesService, tipsViewModel, configuration);
}
transactionRequesterWorker.init(tangle, transactionRequester, tipsViewModel, node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,4 @@ public interface TransactionPruner {
*/
void processJobs() throws TransactionPruningException;

/**
* This method saves the current state of the {@link TransactionPruner}, so it can later be restored by
* {@link #restoreState()}.
*
* It is used to maintain the state between IRI restarts and pick up pruning where it stopped when IRI shut down.
*
* @throws TransactionPruningException if anything goes wrong while saving the current state
*/
void saveState() throws TransactionPruningException;

/**
* Restores the state of the {@link TransactionPruner} after being saved before by {@link #saveState()}.
*
* It is used to keep the state between IRI restarts and pick up pruning where it stopped when IRI shut down.
*
* @throws TransactionPruningException if anything goes wrong while restoring the state
*/
void restoreState() throws TransactionPruningException;

/**
* This method removes all queued jobs and resets the state of the {@link TransactionPruner}. It can for example be
* used to cleanup after tests.
*
* @throws TransactionPruningException if anything goes wrong while clearing the jobs
* */
void clear() throws TransactionPruningException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ public void addJob(TransactionPrunerJob job) throws TransactionPruningException
// this call is "unchecked" to a "raw" JobQueue and it is intended since the matching JobQueue is defined by the
// registered job types
getJobQueue(job.getClass()).addJob(job);

saveState();
}

/**
Expand All @@ -184,65 +182,17 @@ public void processJobs() throws TransactionPruningException {
}
}

/**
* {@inheritDoc}
*
* We incorporate a background job that periodically saves the state rather than doing it "live", to reduce the cost
* of this operation. While this can theoretically lead to a situation where the saved state is not 100% correct and
* the latest changes get lost (if IRI crashes or gets restarted before the new changes could be persisted), the
* impact is marginal because it only leads to some floating "zombie" transactions that will stay in the database.
* This will be "solved" once we persist the changes in the database instead of a file on the hard disk. For now the
* trade off between faster processing times and leaving some garbage is reasonable.
*/
@Override
public void saveState() {
persistRequested = true;
}

/**
* {@inheritDoc}
*
* It reads the state by parsing the state file and passing it into the registered parsers for each job type.
*
* Every line holds a job entry that starts with the fully qualified class name of the job followed by a ";" and the
* serialized representation of the job.
*/
@Override
public void restoreState() throws TransactionPruningException {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new BufferedInputStream(new FileInputStream(getStateFile())))
)) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(";", 2);
if (parts.length >= 2) {
JobParser jobParser = jobParsers.get(parts[0]);
if (jobParser == null) {
throw new TransactionPruningException("could not determine a parser for cleanup job of type " + parts[0]);
}

addJob(jobParser.parse(parts[1]));
}
}
} catch (IOException e) {
if (getStateFile().exists()) {
throw new TransactionPruningException("could not read the state file", e);
}
}
}

/**
* {@inheritDoc}
* This method removes all queued jobs and resets the state of the {@link TransactionPruner}. It can for example be
* used to cleanup after tests.
*
* It cycles through all registered {@link JobQueue}s and clears them before persisting the state.
*/
@Override
public void clear() throws TransactionPruningException {
void clear() {
for (JobQueue jobQueue : jobQueues.values()) {
jobQueue.clear();
}

saveStateNow();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,17 @@ public void clear() {
public void processJobs() throws TransactionPruningException {
MilestonePrunerJob currentJob;
while (!Thread.currentThread().isInterrupted() && (currentJob = jobs.peek()) != null) {
try {
currentJob.process();
currentJob.process();

youngestFullyCleanedMilestoneIndex = currentJob.getTargetIndex();
youngestFullyCleanedMilestoneIndex = currentJob.getTargetIndex();

// we always leave the last job in the queue to be able to "serialize" the queue status and allow
// to skip already processed milestones even when IRI restarts
if (jobs.size() == 1) {
break;
}

jobs.poll();
} finally {
transactionPruner.saveState();
// we always leave the last job in the queue to be able to "serialize" the queue status and allow
// to skip already processed milestones even when IRI restarts
if (jobs.size() == 1) {
break;
}

jobs.poll();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ public void processJobs() throws TransactionPruningException {
}

throw e;
} finally {
transactionPruner.saveState();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ public void process() throws TransactionPruningException {
setStatus(TransactionPrunerJobStatus.DONE);
}
}

getTransactionPruner().saveState();
}
} catch (TransactionPruningException e) {
setStatus(TransactionPrunerJobStatus.FAILED);
Expand Down

0 comments on commit a14e1f5

Please sign in to comment.