Introduce a default liveness-interval value when config is not present#27
Introduce a default liveness-interval value when config is not present#27ayeshLK merged 3 commits intoballerina-platform:mainfrom
Conversation
📝 WalkthroughWalkthroughThe Listener class constants are refactored to reduce public exposure. Four configuration-related keys and one config key are changed from public to private visibility, while retaining public access for shared state keys. A default liveness interval constant is introduced with conditional logic to apply defaults when configuration is missing. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 3❌ Failed checks (2 warnings, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
⚔️ Resolve merge conflicts (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@native/src/main/java/io/ballerina/lib/cdc/Listener.java`:
- Around line 167-175: The livenessInterval assignment in Listener (the block
reading LIVENESS_INTERVAL_CONFIG_KEY from config) must validate that
config.get(LIVENESS_INTERVAL_CONFIG_KEY) is non-null and an instance of BDecimal
before casting, and reject non-positive values; change the logic that sets
livenessInterval to first fetch Object val =
config.get(LIVENESS_INTERVAL_CONFIG_KEY), if val == null use
DEFAULT_LIVENESS_INTERVAL_MILLIS, else if val instanceof BDecimal convert via
((BDecimal) val).decimalValue().multiply(BigDecimal.valueOf(1000)).longValue()
and if the resulting long <= 0 throw an IllegalArgumentException (or
Ballerina-specific error) with a clear message referencing
LIVENESS_INTERVAL_CONFIG_KEY, otherwise assign it, and if val is not a BDecimal
throw a similar informative error instead of allowing a ClassCastException.
| Long livenessInterval; | ||
| if (config.containsKey(LIVENESS_INTERVAL_CONFIG_KEY)) { | ||
| livenessInterval = ((BDecimal) config.get(LIVENESS_INTERVAL_CONFIG_KEY)) | ||
| .decimalValue() | ||
| .multiply(BigDecimal.valueOf(1000)) | ||
| .longValue(); | ||
| } else { | ||
| livenessInterval = DEFAULT_LIVENESS_INTERVAL_MILLIS; | ||
| } |
There was a problem hiding this comment.
Validate livenessInterval value before casting.
If the key exists but is null or not a BDecimal, this will throw at runtime. Also consider rejecting non‑positive values to avoid immediate liveness failures.
✅ Suggested guard with explicit error
- Long livenessInterval;
- if (config.containsKey(LIVENESS_INTERVAL_CONFIG_KEY)) {
- livenessInterval = ((BDecimal) config.get(LIVENESS_INTERVAL_CONFIG_KEY))
- .decimalValue()
- .multiply(BigDecimal.valueOf(1000))
- .longValue();
- } else {
- livenessInterval = DEFAULT_LIVENESS_INTERVAL_MILLIS;
- }
+ Long livenessInterval = DEFAULT_LIVENESS_INTERVAL_MILLIS;
+ if (config.containsKey(LIVENESS_INTERVAL_CONFIG_KEY)) {
+ Object rawInterval = config.get(LIVENESS_INTERVAL_CONFIG_KEY);
+ if (!(rawInterval instanceof BDecimal)) {
+ return createCdcError("'livenessInterval' must be a decimal number");
+ }
+ long computedMillis = ((BDecimal) rawInterval).decimalValue()
+ .multiply(BigDecimal.valueOf(1000))
+ .longValue();
+ if (computedMillis <= 0) {
+ return createCdcError("'livenessInterval' must be > 0");
+ }
+ livenessInterval = computedMillis;
+ }🤖 Prompt for AI Agents
In `@native/src/main/java/io/ballerina/lib/cdc/Listener.java` around lines 167 -
175, The livenessInterval assignment in Listener (the block reading
LIVENESS_INTERVAL_CONFIG_KEY from config) must validate that
config.get(LIVENESS_INTERVAL_CONFIG_KEY) is non-null and an instance of BDecimal
before casting, and reject non-positive values; change the logic that sets
livenessInterval to first fetch Object val =
config.get(LIVENESS_INTERVAL_CONFIG_KEY), if val == null use
DEFAULT_LIVENESS_INTERVAL_MILLIS, else if val instanceof BDecimal convert via
((BDecimal) val).decimalValue().multiply(BigDecimal.valueOf(1000)).longValue()
and if the resulting long <= 0 throw an IllegalArgumentException (or
Ballerina-specific error) with a clear message referencing
LIVENESS_INTERVAL_CONFIG_KEY, otherwise assign it, and if val is not a BDecimal
throw a similar informative error instead of allowing a ClassCastException.
Codecov Report❌ Patch coverage is
❌ Your project status has failed because the head coverage (76.56%) is below the target coverage (80.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #27 +/- ##
============================================
- Coverage 76.70% 76.56% -0.15%
Complexity 224 224
============================================
Files 30 30
Lines 1082 1084 +2
Branches 169 170 +1
============================================
Hits 830 830
- Misses 175 176 +1
- Partials 77 78 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Purpose
Part of: ballerina-platform/ballerina-library#8589
Summary
This PR introduces a default liveness-interval value for the CDC listener when configuration is not explicitly provided, improving operational resilience and reducing the need for mandatory configuration.
Changes
Modified:
native/src/main/java/io/ballerina/lib/cdc/Listener.javaImpact
The CDC listener now has sensible built-in defaults for liveness checks, ensuring consistent heartbeat behavior without requiring explicit configuration. This improves the usability and reliability of the module in typical deployment scenarios.