This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
[NC-2236] Parallel Block importer #774
Merged
Merged
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7a6adc1
Parallel Block importer
shemnon e29f79c
Move ethTaskTimer to abstract root
shemnon 5718db5
spotless and errorprone
shemnon fa800aa
Merge branch 'master' of github.com:PegaSysEng/pantheon into parallel…
shemnon 1817e87
hack to get us past unit tests.
shemnon bbb4aec
merge from main
shemnon 2941970
bug fixes
shemnon 082549c
* remove CompletableFuture.allOf and wait on each. .allOf(...) doesn't
shemnon 7cf5790
Merge branch 'master' of github.com:PegaSysEng/pantheon into parallel…
shemnon 494b9df
spotless
shemnon d7bb3d4
increase timeout in recoversFromSyncTargetDisconnect, the chain downl…
shemnon 727a24d
merge
shemnon dc65fd8
un-wedge the tests.
shemnon f8a0ca0
spotless
shemnon 0c7a625
Use a blocking queue instead of fixed wait period.
ajsutton 6db15d3
Merge pull request #1 from ajsutton/parallel_blocks
shemnon 6126116
review updates.
shemnon 9a559cf
drop semi-colon
shemnon c85f36f
Merge branch 'master' of github.com:PegaSysEng/pantheon into parallel…
shemnon f9a4694
restore missing header
shemnon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...h/src/main/java/tech/pegasys/pantheon/ethereum/eth/manager/AbstractPipelinedPeerTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.eth.manager; | ||
|
||
import tech.pegasys.pantheon.metrics.LabelledMetric; | ||
import tech.pegasys.pantheon.metrics.OperationTimer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public abstract class AbstractPipelinedPeerTask<I, O> extends AbstractPeerTask<List<O>> { | ||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
static final int TIMEOUT_MS = 1000; | ||
|
||
private BlockingQueue<I> inboundQueue; | ||
private BlockingQueue<O> outboundQueue; | ||
private List<O> results; | ||
|
||
private boolean lameDuckMode = false; | ||
private AtomicReference<Throwable> processingException = new AtomicReference<>(null); | ||
|
||
protected AbstractPipelinedPeerTask( | ||
final BlockingQueue<I> inboundQueue, | ||
final int outboundBacklogSize, | ||
final EthContext ethContext, | ||
final LabelledMetric<OperationTimer> ethTasksTimer) { | ||
super(ethContext, ethTasksTimer); | ||
this.inboundQueue = inboundQueue; | ||
outboundQueue = new LinkedBlockingQueue<>(outboundBacklogSize); | ||
results = new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
protected void executeTaskWithPeer(final EthPeer peer) { | ||
Optional<I> previousInput = Optional.empty(); | ||
while (!isDone() && processingException.get() == null) { | ||
if (lameDuckMode && inboundQueue.isEmpty()) { | ||
break; | ||
} | ||
final I input; | ||
try { | ||
input = inboundQueue.poll(TIMEOUT_MS, TimeUnit.MILLISECONDS); | ||
if (input == null) { | ||
// timed out waiting for a result | ||
continue; | ||
} | ||
} catch (final InterruptedException e) { | ||
// this is expected | ||
continue; | ||
} | ||
final Optional<O> output = processStep(input, previousInput, peer); | ||
output.ifPresent( | ||
o -> { | ||
try { | ||
outboundQueue.put(o); | ||
} catch (final InterruptedException e) { | ||
processingException.compareAndSet(null, e); | ||
} | ||
results.add(o); | ||
}); | ||
previousInput = Optional.of(input); | ||
} | ||
if (processingException.get() == null) { | ||
result.get().complete(new PeerTaskResult<>(peer, results)); | ||
} else { | ||
result.get().completeExceptionally(processingException.get()); | ||
} | ||
} | ||
|
||
public BlockingQueue<O> getOutboundQueue() { | ||
return outboundQueue; | ||
} | ||
|
||
public boolean isLameDuckMode() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be unused so can be removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
return lameDuckMode; | ||
} | ||
|
||
public void setLameDuckMode(final boolean lameDuckMode) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming-wise this seems quite similar to |
||
this.lameDuckMode = lameDuckMode; | ||
} | ||
|
||
protected void failExceptionally(final Throwable t) { | ||
LOG.error("Task Failure", t); | ||
processingException.compareAndSet(null, t); | ||
result.get().completeExceptionally(t); | ||
cancel(); | ||
} | ||
|
||
protected abstract Optional<O> processStep(I input, Optional<I> previousInput, EthPeer peer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/BlockHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.eth.sync; | ||
|
||
import tech.pegasys.pantheon.ethereum.core.BlockHeader; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public interface BlockHandler<B> { | ||
CompletableFuture<List<B>> downloadBlocks(final List<BlockHeader> headers); | ||
|
||
CompletableFuture<List<B>> validateAndImportBlocks(final List<B> blocks); | ||
|
||
long extractBlockNumber(final B block); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...c/main/java/tech/pegasys/pantheon/ethereum/eth/sync/tasks/ParallelDownloadBodiesTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.eth.sync.tasks; | ||
|
||
import tech.pegasys.pantheon.ethereum.core.BlockHeader; | ||
import tech.pegasys.pantheon.ethereum.eth.manager.AbstractPipelinedPeerTask; | ||
import tech.pegasys.pantheon.ethereum.eth.manager.EthContext; | ||
import tech.pegasys.pantheon.ethereum.eth.manager.EthPeer; | ||
import tech.pegasys.pantheon.ethereum.eth.sync.BlockHandler; | ||
import tech.pegasys.pantheon.metrics.LabelledMetric; | ||
import tech.pegasys.pantheon.metrics.OperationTimer; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class ParallelDownloadBodiesTask<B> | ||
extends AbstractPipelinedPeerTask<List<BlockHeader>, List<B>> { | ||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
private final BlockHandler<B> blockHandler; | ||
|
||
ParallelDownloadBodiesTask( | ||
final BlockHandler<B> blockHandler, | ||
final BlockingQueue<List<BlockHeader>> inboundQueue, | ||
final int outboundBacklogSize, | ||
final EthContext ethContext, | ||
final LabelledMetric<OperationTimer> ethTasksTimer) { | ||
super(inboundQueue, outboundBacklogSize, ethContext, ethTasksTimer); | ||
|
||
this.blockHandler = blockHandler; | ||
} | ||
|
||
@Override | ||
protected Optional<List<B>> processStep( | ||
final List<BlockHeader> headers, | ||
final Optional<List<BlockHeader>> previousHeaders, | ||
final EthPeer peer) { | ||
LOG.trace( | ||
"Downloading bodies {} to {}", | ||
headers.get(0).getNumber(), | ||
headers.get(headers.size() - 1).getNumber()); | ||
try { | ||
final List<B> blocks = blockHandler.downloadBlocks(headers).get(); | ||
LOG.debug( | ||
"Downloaded bodies {} to {}", | ||
headers.get(0).getNumber(), | ||
headers.get(headers.size() - 1).getNumber()); | ||
return Optional.of(blocks); | ||
} catch (final InterruptedException | ExecutionException e) { | ||
failExceptionally(e); | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we name this something a little more descriptive? Maybe just
stopWhenInboundQueueEmpty
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lame duck is google server speak, keep processing but accept no new connections and then stop when you're done. Like a Lame Duck session in the US congress. But this is Java,
shutdown
is better for the method andshuttingDown
for the var.