Skip to content

Commit

Permalink
[PIE-2303] Automatic log bloom caching - Remove usage of pending file. (
Browse files Browse the repository at this point in the history
hyperledger#407)

* Don't use pending file.

Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net>

* Don't use pending file.

Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net>
(cherry picked from commit c38152a)
Signed-off-by: Danno Ferrin <danno.ferrin@gmail.com>
  • Loading branch information
AbdelStark authored and shemnon committed Feb 19, 2020
1 parent 21d2d79 commit 6868ce4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Optional;
import java.util.OptionalLong;

import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -51,13 +52,15 @@ public void start() {
(event, __) -> {
if (event.isNewCanonicalHead()) {
transactionLogBloomCacher.cacheLogsBloomForBlockHeader(
event.getBlock().getHeader());
event.getBlock().getHeader(), Optional.empty(), true);
}
}));
chainReorgSubscriptionId =
OptionalLong.of(
blockchain.observeChainReorg(
(header, __) -> transactionLogBloomCacher.cacheLogsBloomForBlockHeader(header)));
(header, __) ->
transactionLogBloomCacher.cacheLogsBloomForBlockHeader(
header, Optional.empty(), true)));

transactionLogBloomCacher
.getScheduler()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
Expand All @@ -49,7 +48,6 @@ public class TransactionLogBloomCacher {

public static final int BLOCKS_PER_BLOOM_CACHE = 100_000;
private static final int BLOOM_BITS_LENGTH = 256;
public static final String PENDING = "pending";
private final Map<Long, Boolean> cachedSegments;

private final Lock submissionLock = new ReentrantLock();
Expand Down Expand Up @@ -95,22 +93,16 @@ public CachingStatus generateLogBloomCache(final long start, final long stop) {
LOG.error("Cache directory '{}' does not exist and could not be made.", cacheDir);
return cachingStatus;
}

final File pendingFile = calculateCacheFileName(PENDING, cacheDir);
for (long blockNum = start; blockNum < stop; blockNum += BLOCKS_PER_BLOOM_CACHE) {
LOG.info("Caching segment at {}", blockNum);
try (final FileOutputStream fos = new FileOutputStream(pendingFile)) {
final long blockCount = fillCacheFile(blockNum, blockNum + BLOCKS_PER_BLOOM_CACHE, fos);
if (blockCount == BLOCKS_PER_BLOOM_CACHE) {
Files.move(
pendingFile.toPath(),
calculateCacheFileName(blockNum, cacheDir).toPath(),
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE);
} else {
LOG.info("Partial segment at {}, only {} blocks cached", blockNum, blockCount);
break;
}
final File cacheFile = calculateCacheFileName(blockNum, cacheDir);
blockchain
.getBlockHeader(blockNum)
.ifPresent(
blockHeader ->
cacheLogsBloomForBlockHeader(blockHeader, Optional.of(cacheFile), false));
try (final FileOutputStream fos = new FileOutputStream(cacheFile)) {
fillCacheFile(blockNum, blockNum + BLOCKS_PER_BLOOM_CACHE, fos);
}
}
} catch (final Exception e) {
Expand All @@ -122,7 +114,7 @@ public CachingStatus generateLogBloomCache(final long start, final long stop) {
return cachingStatus;
}

private long fillCacheFile(
private void fillCacheFile(
final long startBlock, final long stopBlock, final FileOutputStream fos) throws IOException {
long blockNum = startBlock;
while (blockNum < stopBlock) {
Expand All @@ -134,15 +126,19 @@ private long fillCacheFile(
cachingStatus.currentBlock = blockNum;
blockNum++;
}
return blockNum - startBlock;
}

public void cacheLogsBloomForBlockHeader(final BlockHeader blockHeader) {
public void cacheLogsBloomForBlockHeader(
final BlockHeader blockHeader,
final Optional<File> reusedCacheFile,
final boolean ensureChecks) {
try {
final long blockNumber = blockHeader.getNumber();
LOG.debug("Caching logs bloom for block {}.", "0x" + Long.toHexString(blockNumber));
ensurePreviousSegmentsArePresent(blockNumber);
final File cacheFile = calculateCacheFileName(blockNumber, cacheDir);
if (ensureChecks) {
ensurePreviousSegmentsArePresent(blockNumber);
}
final File cacheFile = reusedCacheFile.orElse(calculateCacheFileName(blockNumber, cacheDir));
if (!cacheFile.exists()) {
Files.createFile(cacheFile.toPath());
}
Expand Down

0 comments on commit 6868ce4

Please sign in to comment.