-
Notifications
You must be signed in to change notification settings - Fork 88
[RHCLOUD-35790] Re-add remote cache event deduplication #4287
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: master
Are you sure you want to change the base?
Changes from 6 commits
2fc9638
e8a0c2c
0e68ccc
98f7a3c
7f7384f
a96da1b
501dd7a
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 |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package com.redhat.cloud.notifications.events; | ||
|
|
||
| import com.redhat.cloud.notifications.config.EngineConfig; | ||
| import io.quarkus.logging.Log; | ||
| import io.vertx.mutiny.core.Vertx; | ||
| import io.vertx.mutiny.redis.client.Redis; | ||
| import io.vertx.mutiny.redis.client.RedisAPI; | ||
| import io.vertx.redis.client.RedisOptions; | ||
| import jakarta.annotation.PostConstruct; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.inject.Inject; | ||
| import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.LocalDateTime; | ||
| import java.time.ZoneOffset; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| /** Stores and retrieves data from remote cache (i.e. Valkey). */ | ||
| @ApplicationScoped | ||
| public class ValkeyService { | ||
|
|
||
| private static final String EVENT_DEDUPLICATION_KEY = "engine:event-deduplication"; | ||
| private static final String NOT_USED = ""; | ||
|
|
||
| @ConfigProperty(name = "valkey-service.ttl", defaultValue = "PT24H") | ||
| Duration ttl; | ||
|
|
||
| @ConfigProperty(name = "quarkus.redis.hosts", defaultValue = "") | ||
| Optional<String> valkeyHost; | ||
|
|
||
| @ConfigProperty(name = "quarkus.redis.password", defaultValue = "") | ||
| Optional<String> valkeyPassword; | ||
|
|
||
| @Inject | ||
| EngineConfig config; | ||
|
|
||
| @Inject | ||
| Vertx vertx; | ||
|
|
||
| /** The underlying client connecting to Valkey. */ | ||
| private Redis valkeyClient; | ||
|
|
||
| /** Implementation of the Redis/Valkey API, using {@link #valkeyClient} */ | ||
| private RedisAPI valkey; | ||
|
|
||
| @PostConstruct | ||
| void initialize() { | ||
| if (config.isInMemoryDbEnabled()) { | ||
| if (valkeyHost.isEmpty() || valkeyHost.get().isEmpty()) { | ||
| Log.warn("In-memory DB enabled, but Valkey connection string was not provided"); | ||
| } else { | ||
| RedisOptions valkeyOptions = new RedisOptions().setConnectionString(valkeyHost.get()); | ||
| valkeyPassword.ifPresent(valkeyOptions::setPassword); | ||
|
|
||
| this.valkeyClient = Redis.createClient(vertx, valkeyOptions); | ||
| this.valkey = RedisAPI.api(this.valkeyClient); | ||
| } | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Verifies that the event has not been previously processed. The format of saved keys is | ||
| * {@code engine:event-deduplication:<event_type>:<deduplication_key>}. | ||
| * | ||
| * @param eventId only used for debugging | ||
| * @see com.redhat.cloud.notifications.events.deduplication.EventDeduplicator EventDeduplicator | ||
| */ | ||
| public boolean isNewEvent(UUID eventTypeId, String deduplicationKey, LocalDateTime deleteAfter, UUID eventId) { | ||
| String key = String.format("%s:%s:%s", EVENT_DEDUPLICATION_KEY, eventTypeId, deduplicationKey); | ||
| String deleteAfterIso = deleteAfter.format(DateTimeFormatter.ISO_DATE_TIME); | ||
|
|
||
| boolean isNew = valkey.setnxAndAwait(key, deleteAfterIso).toBoolean(); | ||
| if (isNew) { | ||
| boolean expireSet = valkey.expireatAndAwait(List.of( | ||
| key, | ||
| String.valueOf(deleteAfter.toEpochSecond(ZoneOffset.UTC)) | ||
| )).toBoolean(); | ||
|
|
||
| if (!expireSet) { | ||
| // dedup key may include private information, so other fields are used | ||
| Log.warnf("unable to set expiry for Valkey event deduplication [event_type_id=%s, event_id=%s, delete_after=%s]", | ||
| eventTypeId, eventId, deleteAfterIso); | ||
| } | ||
| } | ||
|
|
||
| return isNew; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| package com.redhat.cloud.notifications.events.deduplication; | ||
|
|
||
| import com.redhat.cloud.notifications.config.EngineConfig; | ||
| import com.redhat.cloud.notifications.events.ValkeyService; | ||
| import com.redhat.cloud.notifications.models.Event; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.inject.Inject; | ||
| import jakarta.persistence.EntityManager; | ||
| import jakarta.transaction.Transactional; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| @ApplicationScoped | ||
| public class EventDeduplicator { | ||
|
|
@@ -21,6 +25,12 @@ public class EventDeduplicator { | |
| @Inject | ||
| SubscriptionsDeduplicationConfig subscriptionsDeduplicationConfig; | ||
|
|
||
| @Inject | ||
| EngineConfig engineConfig; | ||
|
|
||
| @Inject | ||
| ValkeyService valkeyService; | ||
|
|
||
| private EventDeduplicationConfig getEventDeduplicationConfig(Event event) { | ||
| return switch (event.getEventType().getApplication().getBundle().getName()) { | ||
| case SUBSCRIPTION_SERVICES_BUNDLE -> | ||
|
|
@@ -43,15 +53,22 @@ public boolean isNew(Event event) { | |
| return true; | ||
| } | ||
|
|
||
| String sql = "INSERT INTO event_deduplication(event_type_id, deduplication_key, delete_after) " + | ||
| "VALUES (:eventTypeId, :deduplicationKey, :deleteAfter) " + | ||
| "ON CONFLICT (event_type_id, deduplication_key) DO NOTHING"; | ||
| UUID eventTypeId = event.getEventType().getId(); | ||
| LocalDateTime deleteAfter = eventDeduplicationConfig.getDeleteAfter(event); | ||
|
|
||
| if (engineConfig.isInMemoryDbEnabled() && engineConfig.isValkeyEventDeduplicatorEnabled()) { | ||
|
Contributor
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. We need to "feed" Valkey for 15 days before using it for dedup check. |
||
| return valkeyService.isNewEvent(eventTypeId, deduplicationKey.get(), deleteAfter, event.getId()); | ||
|
Comment on lines
+59
to
+60
Contributor
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. Make the Valkey dedup path rollback-safe.
🤖 Prompt for AI Agents
Member
Author
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. Not applicable, Redis does not support transaction rollbacks. Unless there's a way to implement this manually and we actually do need it. Edit: To the reviewer, please add your thoughts on the bot's suggestion, I'm leaning no.
Contributor
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.
Manual compensation is technically achievable in Quarkus without Redis transactions — you can register a That said, this compensation approach has its own failure mode: if the The practical question is: how often do DB transaction rollbacks occur after Happy to defer to your judgment on whether this risk level is acceptable given your system's retry behavior.
Contributor
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. All that code is wrapped into a large try/catch. I think we could delete the Valkey key in the catch block.
Contributor
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.
|
||
| } else { | ||
| String sql = "INSERT INTO event_deduplication(event_type_id, deduplication_key, delete_after) " + | ||
| "VALUES (:eventTypeId, :deduplicationKey, :deleteAfter) " + | ||
| "ON CONFLICT (event_type_id, deduplication_key) DO NOTHING"; | ||
|
|
||
| int rowCount = entityManager.createNativeQuery(sql) | ||
| .setParameter("eventTypeId", event.getEventType().getId()) | ||
| .setParameter("deduplicationKey", deduplicationKey.get()) | ||
| .setParameter("deleteAfter", eventDeduplicationConfig.getDeleteAfter(event)) | ||
| .executeUpdate(); | ||
| return rowCount > 0; | ||
| int rowCount = entityManager.createNativeQuery(sql) | ||
| .setParameter("eventTypeId", eventTypeId) | ||
| .setParameter("deduplicationKey", deduplicationKey.get()) | ||
| .setParameter("deleteAfter", deleteAfter) | ||
| .executeUpdate(); | ||
| return rowCount > 0; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.