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

Conversation

Sotatek-HuyLe3a
Copy link
Collaborator

#102
This error occurs in: (PoolOfflineDataStoringServiceImpl.java)

 poolDataMap.entrySet().parallelStream().forEach(poolDataEntry -> {
            Long poolId = poolDataEntry.getKey();
            PoolData poolData = poolDataEntry.getValue();
            PoolHash poolHash = poolHashMap.get(poolId);
            PoolMetadataRef poolMetadataRef = poolMetadataRefMap.get(poolData.getMetadataRefId());
            // if pool offline data exist then update
            PoolOfflineFetchError poolOfflineFetchError = poolOfflineFetchErrorRepository
                    .findByPoolHashAndPoolMetadataRef(poolHash, poolMetadataRef);

            // if pool offline fetch error exist then update
            if (!Objects.isNull(poolOfflineFetchError)) {
                poolOfflineFetchError.setFetchTime(Timestamp.valueOf(LocalDateTime.now(ZoneOffset.UTC)));
                poolOfflineFetchError.setRetryCount(poolOfflineFetchError.getRetryCount() + 1);
                poolOfflineFetchError.setFetchError(poolData.getErrorMessage());
            } else {
                poolOfflineFetchError = PoolOfflineFetchError.builder()
                        .poolHash(poolHash)
                        .poolMetadataRef(poolMetadataRef)
                        .fetchError(poolData.getErrorMessage())
                        .retryCount(BigInteger.ONE.intValue())
                        .fetchTime(Timestamp.valueOf(LocalDateTime.now(ZoneOffset.UTC)))
                        .build();
            }
            poolOfflineFetchErrorsNeedSave.add(poolOfflineFetchError);
        });

It's not thread safe when using parallel stream with forEach, and add elements into a ArrayList.
Solution: use Stream map() function and Collector interface instead.

@Sotatek-HuyLe3a Sotatek-HuyLe3a merged commit cbcb79d into develop Dec 13, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants