-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce dedupe for outbound events (#148)
* Update application.template with dedupe config details Signed-off-by: Nithin <nithin.pankaj@walmart.com> * Enable configurations for Dedupe and Producer idempotence Signed-off-by: Nithin <nithin.pankaj@walmart.com> * Service and impl for RecencyTransactionContext Signed-off-by: Nithin <nithin.pankaj@walmart.com> * Setting transaction context prior to submitting proposal Signed-off-by: Nithin <nithin.pankaj@walmart.com> * Validate for duplicate events prior to kafka submission Signed-off-by: Nithin <nithin.pankaj@walmart.com> * Support optional Transaction Id filter for event replay Signed-off-by: Nithin <nithin.pankaj@walmart.com> * Add dependency for Guava utils Signed-off-by: Nithin <nithin.pankaj@walmart.com> * Test fix for producer idempotence Signed-off-by: Nithin <nithin.pankaj@walmart.com> --------- Signed-off-by: Nithin <nithin.pankaj@walmart.com>
- Loading branch information
1 parent
cb8a11c
commit 96b353f
Showing
18 changed files
with
203 additions
and
18 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
47 changes: 47 additions & 0 deletions
47
src/main/java/hlf/java/rest/client/config/EventDedupeConfig.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,47 @@ | ||
package hlf.java.rest.client.config; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import hlf.java.rest.client.service.RecencyTransactionContext; | ||
import hlf.java.rest.client.service.impl.DefaultCacheBasedRecencyTransactionContext; | ||
import hlf.java.rest.client.service.impl.NoOpRecencyTransactionContext; | ||
import java.util.concurrent.TimeUnit; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Data | ||
@Configuration | ||
@ConfigurationProperties(prefix = "dedupe") | ||
@Slf4j | ||
public class EventDedupeConfig { | ||
|
||
private boolean enable; | ||
private int recencyWindowSize; | ||
private int recencyWindowExpiryInMinutes; | ||
|
||
@Bean | ||
public RecencyTransactionContext recencyTransactionContext() { | ||
|
||
if (!this.isEnable()) { | ||
log.info( | ||
"Dedupe config is disabled. Events wont be validated prior to submission to publisher topic"); | ||
return new NoOpRecencyTransactionContext(); | ||
} | ||
|
||
Cache<String, Object> recencyCache = | ||
CacheBuilder.newBuilder() | ||
.maximumSize(this.getRecencyWindowSize()) | ||
.expireAfterAccess(this.getRecencyWindowExpiryInMinutes(), TimeUnit.MINUTES) | ||
.build(); | ||
|
||
log.info( | ||
"Enabling recency check with cache size {} and TTL {} minutes", | ||
recencyWindowSize, | ||
recencyWindowExpiryInMinutes); | ||
|
||
return new DefaultCacheBasedRecencyTransactionContext(recencyCache); | ||
} | ||
} |
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
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
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
8 changes: 8 additions & 0 deletions
8
src/main/java/hlf/java/rest/client/service/RecencyTransactionContext.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,8 @@ | ||
package hlf.java.rest.client.service; | ||
|
||
public interface RecencyTransactionContext { | ||
|
||
void setTransactionContext(String transactionId); | ||
|
||
boolean validateAndRemoveTransactionContext(String transactionId); | ||
} |
29 changes: 29 additions & 0 deletions
29
...in/java/hlf/java/rest/client/service/impl/DefaultCacheBasedRecencyTransactionContext.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,29 @@ | ||
package hlf.java.rest.client.service.impl; | ||
|
||
import com.google.common.cache.Cache; | ||
import hlf.java.rest.client.service.RecencyTransactionContext; | ||
|
||
public class DefaultCacheBasedRecencyTransactionContext implements RecencyTransactionContext { | ||
|
||
private Cache<String, Object> recencyCache; | ||
|
||
public DefaultCacheBasedRecencyTransactionContext(Cache<String, Object> recencyCache) { | ||
this.recencyCache = recencyCache; | ||
} | ||
|
||
@Override | ||
public void setTransactionContext(String transactionId) { | ||
recencyCache.put(transactionId, 1); | ||
} | ||
|
||
@Override | ||
public boolean validateAndRemoveTransactionContext(String transactionId) { | ||
synchronized (this) { | ||
if (recencyCache.getIfPresent(transactionId) == null) { | ||
return false; | ||
} | ||
recencyCache.invalidate(transactionId); | ||
return true; | ||
} | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
src/main/java/hlf/java/rest/client/service/impl/NoOpRecencyTransactionContext.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,15 @@ | ||
package hlf.java.rest.client.service.impl; | ||
|
||
import hlf.java.rest.client.service.RecencyTransactionContext; | ||
|
||
public class NoOpRecencyTransactionContext implements RecencyTransactionContext { | ||
@Override | ||
public void setTransactionContext(String transactionId) { | ||
// NO-OP | ||
} | ||
|
||
@Override | ||
public boolean validateAndRemoveTransactionContext(String transactionId) { | ||
return true; | ||
} | ||
} |
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
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
Oops, something went wrong.