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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NC-2236] Parallel Block importer (#774)
ParallelImportChainSegmentTask, an explicitly parallel re-implementation of PipelinedImportChainSegmentTask. Data is passed between stages via BlockingQueues. Pipeline stages are implemented in AbstractPipelinePeerTask and the parent task assembles and initiates the pipeline execution. Other changes to support this: * Move ethTaskTimer to abstract root * Don't use deterministic scheduler for downloader tests, this depends on explicit parallelism * Change download segment size = 200 * Increase timeout in recoversFromSyncTargetDisconnect, the chain downloader may stall for 10 seconds looking for an alternative target. * Use a blocking queue instead of fixed wait period for the test peers
- Loading branch information
Showing
22 changed files
with
765 additions
and
45 deletions.
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
105 changes: 105 additions & 0 deletions
105
...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,105 @@ | ||
/* | ||
* 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 shuttingDown = 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 (shuttingDown && 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 void shutdown() { | ||
this.shuttingDown = true; | ||
} | ||
|
||
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.