-
Notifications
You must be signed in to change notification settings - Fork 0
No longer block the tomcat thread when waiting for geodata (servlet 3.0+ AsyncContext) #162
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ | |
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.collect.Streams; | ||
import com.powsybl.commons.exceptions.UncheckedInterruptedException; | ||
import com.powsybl.iidm.network.*; | ||
import com.powsybl.iidm.network.extensions.Coordinate; | ||
import com.powsybl.iidm.network.extensions.SubstationPosition; | ||
|
@@ -29,7 +28,6 @@ | |
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.*; | ||
import java.util.Map.Entry; | ||
|
@@ -546,48 +544,38 @@ private Pair<Substation, Substation> getSubstations(Identifiable<?> identifiable | |
}; | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<SubstationGeoData> getSubstationsData(Network network, Set<Country> countrySet, List<String> substationIds) { | ||
CompletableFuture<List<SubstationGeoData>> substationGeoDataFuture = geoDataExecutionService.supplyAsync(() -> { | ||
if (substationIds != null) { | ||
if (!countrySet.isEmpty()) { | ||
LOGGER.warn("Countries will not be taken into account to filter substation position."); | ||
public CompletableFuture<List<SubstationGeoData>> getSubstationsData(Network network, Set<Country> countrySet, List<String> substationIds) { | ||
return geoDataExecutionService.supplyAsync(() -> { | ||
try { | ||
if (substationIds != null) { | ||
if (!countrySet.isEmpty()) { | ||
LOGGER.warn("Countries will not be taken into account to filter substation position."); | ||
} | ||
return getSubstationsByIds(network, new HashSet<>(substationIds)); | ||
} else { | ||
return getSubstationsByCountries(network, countrySet); | ||
} | ||
return getSubstationsByIds(network, new HashSet<>(substationIds)); | ||
} else { | ||
return getSubstationsByCountries(network, countrySet); | ||
} catch (Exception e) { | ||
throw new GeoDataException(FAILED_SUBSTATIONS_LOADING, e); | ||
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. Does it behave the same to throw inside or outside the async ? 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. yes it's the same. And there are unit tests that verify that from an http client point of view, the thrown exception inside the completablefuture is correctly used by the servlet container asynchronously to return the associated error http reponse to the client |
||
} | ||
}); | ||
try { | ||
return substationGeoDataFuture.get(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new UncheckedInterruptedException(e); | ||
} catch (Exception e) { | ||
throw new GeoDataException(FAILED_SUBSTATIONS_LOADING, e); | ||
} | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<LineGeoData> getLinesData(Network network, Set<Country> countrySet, List<String> lineIds) { | ||
CompletableFuture<List<LineGeoData>> lineGeoDataFuture = geoDataExecutionService.supplyAsync(() -> { | ||
if (lineIds != null) { | ||
if (!countrySet.isEmpty()) { | ||
LOGGER.warn("Countries will not be taken into account to filter line position."); | ||
public CompletableFuture<List<LineGeoData>> getLinesData(Network network, Set<Country> countrySet, List<String> lineIds) { | ||
return geoDataExecutionService.supplyAsync(() -> { | ||
try { | ||
if (lineIds != null) { | ||
if (!countrySet.isEmpty()) { | ||
LOGGER.warn("Countries will not be taken into account to filter line position."); | ||
} | ||
return getLinesByIds(network, new HashSet<>(lineIds)); | ||
} else { | ||
return getLinesByCountries(network, countrySet); | ||
} | ||
return getLinesByIds(network, new HashSet<>(lineIds)); | ||
} else { | ||
return getLinesByCountries(network, countrySet); | ||
} catch (Exception e) { | ||
throw new GeoDataException(FAILED_LINES_LOADING, e); | ||
} | ||
}); | ||
try { | ||
return lineGeoDataFuture.get(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new UncheckedInterruptedException(e); | ||
} catch (Exception e) { | ||
throw new GeoDataException(FAILED_LINES_LOADING, e); | ||
} | ||
} | ||
|
||
List<LineGeoData> getLinesByIds(Network network, Set<String> linesIds) { | ||
|
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.
Shouldn't we move this Transactional somewhere else ?
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.
I suggest in a separate PR ?
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.
(also in this case it looks like all the computation is just a single repository call to the standard find method so it should be equivalent I think.. not 100% sure from the top of my head though. Also not sure what is the official code to do transactions in a separate thread.. probably it's just the same but I would need to double check..)