-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
559b3e0
commit 184eca1
Showing
5 changed files
with
95 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/it/gov/pagopa/onboarding/citizen/configuration/RedissonConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package it.gov.pagopa.onboarding.citizen.configuration; | ||
|
||
import org.redisson.Redisson; | ||
import org.redisson.api.RedissonReactiveClient; | ||
import org.redisson.config.Config; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class RedissonConfig { | ||
|
||
@Value("${redis.url}") | ||
private String redisUrl; | ||
@Bean | ||
public RedissonReactiveClient redissonClient() { | ||
Config config = new Config(); | ||
config.useSingleServer().setAddress(redisUrl); | ||
return Redisson.create(config).reactive(); | ||
} | ||
} |
77 changes: 41 additions & 36 deletions
77
src/main/java/it/gov/pagopa/onboarding/citizen/service/BloomFilterServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,57 @@ | ||
package it.gov.pagopa.onboarding.citizen.service; | ||
|
||
|
||
import com.azure.cosmos.implementation.guava25.hash.BloomFilter; | ||
import com.azure.cosmos.implementation.guava25.hash.Funnels; | ||
import it.gov.pagopa.onboarding.citizen.repository.CitizenRepository; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.redisson.api.RLock; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.nio.charset.StandardCharsets; | ||
import org.redisson.api.RBloomFilter; | ||
import org.redisson.api.RedissonClient; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@Service | ||
@Slf4j | ||
public class BloomFilterServiceImpl implements BloomFilterService{ | ||
|
||
private final CitizenRepository citizenRepository; | ||
|
||
private BloomFilter<String> bloomFilter; | ||
|
||
public BloomFilterServiceImpl(CitizenRepository citizenRepository) { | ||
this.citizenRepository = citizenRepository; | ||
public class BloomFilterServiceImpl implements BloomFilterService { | ||
|
||
private static final String REDDIS_BF_NAME = "emd-bloom-fiter"; | ||
private static final String REDIS_LOCK_NAME = "startup-task-lock"; | ||
|
||
RBloomFilter<String> bloomFilter ; | ||
public BloomFilterServiceImpl(RedissonClient redissonClient) { | ||
RLock lock = redissonClient.getLock(REDIS_LOCK_NAME); | ||
|
||
try { | ||
if (lock.tryLock(0, TimeUnit.SECONDS)) { | ||
try { | ||
this.bloomFilter = initializeBloomFilter(redissonClient); | ||
// carico dati dal db | ||
log.info("[BLOOM-FILTER-SERVICE] Inizializzazione bloom filter eseguita"); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} else { | ||
this.bloomFilter = redissonClient.getBloomFilter(REDDIS_BF_NAME); | ||
log.info("BLOOM-FILTER-SERVICE] Un'altra replica sta già eseguendo l'inizializzazione bloom filter "); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException("Errore durante l'acquisizione del lock.", e); | ||
} | ||
} | ||
|
||
|
||
@PostConstruct | ||
public void initializeBloomFilter() { | ||
bloomFilter = BloomFilter.create(Funnels.stringFunnel(StandardCharsets.UTF_8), 1000000, 0.01); | ||
|
||
citizenRepository.findAll() | ||
.doOnNext(citizenConsent -> | ||
bloomFilter.put(citizenConsent.getFiscalCode())) | ||
.doOnComplete(() -> log.info("Bloom filter initialized")) | ||
.subscribe(); | ||
private RBloomFilter<String> initializeBloomFilter(RedissonClient redissonClient) { | ||
RBloomFilter<String> filter = redissonClient.getBloomFilter(REDDIS_BF_NAME); | ||
filter.tryInit(1000000L, 0.01); | ||
return filter; | ||
} | ||
@Override | ||
|
||
public boolean mightContain(String fiscalCode) { | ||
return bloomFilter.mightContain(fiscalCode); | ||
public void add(String value) { | ||
bloomFilter.add(value); | ||
} | ||
|
||
|
||
@Scheduled(fixedRate = 3600000) | ||
public void update() { | ||
this.initializeBloomFilter(); | ||
public boolean mightContain(String value) { | ||
return bloomFilter.contains(value); | ||
} | ||
|
||
public void add(String fiscalCode){ | ||
bloomFilter.put(fiscalCode); | ||
} | ||
} | ||
//Valutare re-inizializzaione temporizata per rimuovere eventuali elementi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters