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

fix: #102 update to guarantee thread-safe when handling failed pool offline data #104

Merged
merged 2 commits into from
Dec 13, 2023
Merged
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 @@ -61,7 +61,6 @@ public PoolOfflineDataStoringServiceImpl(
public void saveSuccessPoolOfflineData(List<PoolData> successPools) {
long startTime = System.currentTimeMillis();
log.info("Start saving success pool offline data");
List<PoolOfflineData> poolOfflineDataNeedSave = new ArrayList<>();

List<Long> poolIds = successPools.stream().map(PoolData::getPoolId).toList();
List<Long> poolMetadataRefIds = successPools.stream().map(PoolData::getMetadataRefId).toList();
Expand All @@ -84,7 +83,7 @@ public void saveSuccessPoolOfflineData(List<PoolData> successPools) {
.stream()
.collect(Collectors.toMap(PoolOfflineData::getPoolId, Function.identity()));

poolDataMap.entrySet().parallelStream().forEach(poolDataEntry -> {
List<PoolOfflineData> poolOfflineDataNeedSave = poolDataMap.entrySet().parallelStream().map(poolDataEntry -> {
Long poolId = poolDataEntry.getKey();
PoolData poolData = poolDataEntry.getValue();
PoolHash poolHash = poolHashMap.get(poolId);
Expand All @@ -96,9 +95,10 @@ public void saveSuccessPoolOfflineData(List<PoolData> successPools) {
if (poolOfflineDataSourceMap.containsKey(poolId)) {
poolOfflineData.setId(poolOfflineDataSourceMap.get(poolId).getId());
}
poolOfflineDataNeedSave.add(poolOfflineData);
}
});

return poolOfflineData;
}).filter(Objects::nonNull).collect(Collectors.toList());

poolOfflineDataRepository.saveAll(poolOfflineDataNeedSave);
log.info("Saved success pool offline data, count: {}, time taken: {} ms",
Expand All @@ -111,7 +111,6 @@ public void saveFailOfflineData(List<PoolData> failedPools) {
long startTime = System.currentTimeMillis();

log.info("Start saving fail pool offline data");
List<PoolOfflineFetchError> poolOfflineFetchErrorsNeedSave = new ArrayList<>();

List<Long> poolIds = failedPools.stream().map(PoolData::getPoolId).toList();
List<Long> poolMetadataRefIds = failedPools.stream().map(PoolData::getMetadataRefId).toList();
Expand All @@ -130,7 +129,8 @@ public void saveFailOfflineData(List<PoolData> failedPools) {
.stream()
.collect(Collectors.toMap(PoolMetadataRef::getId, Function.identity()));

poolDataMap.entrySet().parallelStream().forEach(poolDataEntry -> {
List<PoolOfflineFetchError> poolOfflineFetchErrorsNeedSave =
poolDataMap.entrySet().parallelStream().map(poolDataEntry -> {
Long poolId = poolDataEntry.getKey();
PoolData poolData = poolDataEntry.getValue();
PoolHash poolHash = poolHashMap.get(poolId);
Expand All @@ -153,8 +153,9 @@ public void saveFailOfflineData(List<PoolData> failedPools) {
.fetchTime(Timestamp.valueOf(LocalDateTime.now(ZoneOffset.UTC)))
.build();
}
poolOfflineFetchErrorsNeedSave.add(poolOfflineFetchError);
});

return poolOfflineFetchError;
}).collect(Collectors.toList());

poolOfflineFetchErrorRepository.saveAll(poolOfflineFetchErrorsNeedSave);
log.info("Saved success fail pool offline data, count: {}, time taken: {} ms",
Expand Down
Loading