Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Raptor transfer cache in parallel #6326

Open
wants to merge 6 commits into
base: dev-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.IntStream;
import org.opentripplanner.framework.application.OTPFeature;
import org.opentripplanner.raptor.api.model.RaptorTransfer;
import org.opentripplanner.routing.api.request.StreetMode;
import org.opentripplanner.street.search.request.StreetSearchRequest;

public class RaptorTransferIndex {

private enum RequestSource {
SETUP,
REQUEST_SCOPE,
}

private final List<RaptorTransfer>[] forwardTransfers;

private final List<RaptorTransfer>[] reversedTransfers;
Expand All @@ -24,19 +31,47 @@ public RaptorTransferIndex(
this.reversedTransfers = reversedTransfers.stream().map(List::copyOf).toArray(List[]::new);
}

public static RaptorTransferIndex create(
/**
* Create an index for a route request configured in router-config.json
*/
public static RaptorTransferIndex createInitialSetup(
List<List<Transfer>> transfersByStopIndex,
StreetSearchRequest request
) {
return create(transfersByStopIndex, request, RequestSource.SETUP);
}

/**
* Create an index for a route request originated from the client
*/
public static RaptorTransferIndex createRequestScope(
List<List<Transfer>> transfersByStopIndex,
StreetSearchRequest request
) {
return create(transfersByStopIndex, request, RequestSource.REQUEST_SCOPE);
}

private static RaptorTransferIndex create(
List<List<Transfer>> transfersByStopIndex,
StreetSearchRequest request,
RequestSource requestSource
) {
var forwardTransfers = new ArrayList<List<RaptorTransfer>>(transfersByStopIndex.size());
var reversedTransfers = new ArrayList<List<RaptorTransfer>>(transfersByStopIndex.size());
StreetMode mode = request.mode();

for (int i = 0; i < transfersByStopIndex.size(); i++) {
forwardTransfers.add(new ArrayList<>());
reversedTransfers.add(new ArrayList<>());
}

for (int fromStop = 0; fromStop < transfersByStopIndex.size(); fromStop++) {
var stopIndices = IntStream.range(0, transfersByStopIndex.size());
// we want to always parallelize the cache building during the startup
// and only parallelize during runtime requests if the feature flag is on
if (requestSource == RequestSource.SETUP || OTPFeature.ParallelRouting.isOn()) {
stopIndices = stopIndices.parallel();
}
stopIndices.forEach(fromStop -> {
// The transfers are filtered so that there is only one possible directional transfer
// for a stop pair.
var transfers = transfersByStopIndex
Expand All @@ -49,15 +84,18 @@ public static RaptorTransferIndex create(
)
.values();

forwardTransfers.add(new ArrayList<>(transfers));
// forwardTransfers is not modified here, and no two threads will access the same element
// in it, so this is still thread safe.
forwardTransfers.get(fromStop).addAll(transfers);
});

for (RaptorTransfer forwardTransfer : transfers) {
for (int fromStop = 0; fromStop < transfersByStopIndex.size(); fromStop++) {
for (var forwardTransfer : forwardTransfers.get(fromStop)) {
reversedTransfers
.get(forwardTransfer.stop())
.add(DefaultRaptorTransfer.reverseOf(fromStop, forwardTransfer));
}
}

return new RaptorTransferIndex(forwardTransfers, reversedTransfers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public LoadingCache<CacheKey, RaptorTransferIndex> getTransferCache() {

public void put(List<List<Transfer>> transfersByStopIndex, RouteRequest request) {
final CacheKey cacheKey = new CacheKey(transfersByStopIndex, request);
final RaptorTransferIndex raptorTransferIndex = RaptorTransferIndex.create(
final RaptorTransferIndex raptorTransferIndex = RaptorTransferIndex.createInitialSetup(
transfersByStopIndex,
cacheKey.request
);
Expand All @@ -58,7 +58,10 @@ private CacheLoader<CacheKey, RaptorTransferIndex> cacheLoader() {
@Override
public RaptorTransferIndex load(CacheKey cacheKey) {
LOG.info("Adding runtime request to cache: {}", cacheKey.options);
return RaptorTransferIndex.create(cacheKey.transfersByStopIndex, cacheKey.request);
return RaptorTransferIndex.createRequestScope(
cacheKey.transfersByStopIndex,
cacheKey.request
);
}
};
}
Expand Down
Loading