From 44945106c46a078c8299cb0b70b9f3a0eaa49916 Mon Sep 17 00:00:00 2001 From: Sotatek-HuyLe3a Date: Tue, 12 Dec 2023 12:24:40 +0700 Subject: [PATCH 1/2] fix: #102 update to guarantee thread-safe when handling failed pool offline data --- .../service/impl/PoolOfflineDataStoringServiceImpl.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/components/scheduler/src/main/java/org/cardanofoundation/ledgersync/scheduler/service/impl/PoolOfflineDataStoringServiceImpl.java b/components/scheduler/src/main/java/org/cardanofoundation/ledgersync/scheduler/service/impl/PoolOfflineDataStoringServiceImpl.java index 34806de1..e2f11842 100644 --- a/components/scheduler/src/main/java/org/cardanofoundation/ledgersync/scheduler/service/impl/PoolOfflineDataStoringServiceImpl.java +++ b/components/scheduler/src/main/java/org/cardanofoundation/ledgersync/scheduler/service/impl/PoolOfflineDataStoringServiceImpl.java @@ -111,7 +111,6 @@ public void saveFailOfflineData(List failedPools) { long startTime = System.currentTimeMillis(); log.info("Start saving fail pool offline data"); - List poolOfflineFetchErrorsNeedSave = new ArrayList<>(); List poolIds = failedPools.stream().map(PoolData::getPoolId).toList(); List poolMetadataRefIds = failedPools.stream().map(PoolData::getMetadataRefId).toList(); @@ -130,7 +129,8 @@ public void saveFailOfflineData(List failedPools) { .stream() .collect(Collectors.toMap(PoolMetadataRef::getId, Function.identity())); - poolDataMap.entrySet().parallelStream().forEach(poolDataEntry -> { + List poolOfflineFetchErrorsNeedSave = + poolDataMap.entrySet().parallelStream().map(poolDataEntry -> { Long poolId = poolDataEntry.getKey(); PoolData poolData = poolDataEntry.getValue(); PoolHash poolHash = poolHashMap.get(poolId); @@ -153,8 +153,9 @@ public void saveFailOfflineData(List 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", From 198af465de1161d4dd520667b2a13a8b6ab30507 Mon Sep 17 00:00:00 2001 From: Sotatek-HuyLe3a Date: Wed, 13 Dec 2023 07:45:58 +0700 Subject: [PATCH 2/2] fix: #102 update to guarantee thread-safe when handling success pool offline data --- .../service/impl/PoolOfflineDataStoringServiceImpl.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/scheduler/src/main/java/org/cardanofoundation/ledgersync/scheduler/service/impl/PoolOfflineDataStoringServiceImpl.java b/components/scheduler/src/main/java/org/cardanofoundation/ledgersync/scheduler/service/impl/PoolOfflineDataStoringServiceImpl.java index e2f11842..9b898c5f 100644 --- a/components/scheduler/src/main/java/org/cardanofoundation/ledgersync/scheduler/service/impl/PoolOfflineDataStoringServiceImpl.java +++ b/components/scheduler/src/main/java/org/cardanofoundation/ledgersync/scheduler/service/impl/PoolOfflineDataStoringServiceImpl.java @@ -61,7 +61,6 @@ public PoolOfflineDataStoringServiceImpl( public void saveSuccessPoolOfflineData(List successPools) { long startTime = System.currentTimeMillis(); log.info("Start saving success pool offline data"); - List poolOfflineDataNeedSave = new ArrayList<>(); List poolIds = successPools.stream().map(PoolData::getPoolId).toList(); List poolMetadataRefIds = successPools.stream().map(PoolData::getMetadataRefId).toList(); @@ -84,7 +83,7 @@ public void saveSuccessPoolOfflineData(List successPools) { .stream() .collect(Collectors.toMap(PoolOfflineData::getPoolId, Function.identity())); - poolDataMap.entrySet().parallelStream().forEach(poolDataEntry -> { + List poolOfflineDataNeedSave = poolDataMap.entrySet().parallelStream().map(poolDataEntry -> { Long poolId = poolDataEntry.getKey(); PoolData poolData = poolDataEntry.getValue(); PoolHash poolHash = poolHashMap.get(poolId); @@ -96,9 +95,10 @@ public void saveSuccessPoolOfflineData(List 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",