Skip to content

Send RESET event upon window expiry#13874

Open
PasanT9 wants to merge 2 commits into
wso2:masterfrom
PasanT9:mem-fix-master
Open

Send RESET event upon window expiry#13874
PasanT9 wants to merge 2 commits into
wso2:masterfrom
PasanT9:mem-fix-master

Conversation

@PasanT9

@PasanT9 PasanT9 commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

$subject
Ref: wso2/api-manager#5064

@wso2-engineering wso2-engineering Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Agent Log Improvement Checklist

⚠️ Warning: AI-Generated Review Comments

  • The log-related comments and suggestions in this review were generated by an AI tool to assist with identifying potential improvements. Purpose of reviewing the code for log improvements is to improve the troubleshooting capabilities of our products.
  • Please make sure to manually review and validate all suggestions before applying any changes. Not every code suggestion would make sense or add value to our purpose. Therefore, you have the freedom to decide which of the suggestions are helpful.

✅ Before merging this pull request:

  • Review all AI-generated comments for accuracy and relevance.
  • Complete and verify the table below. We need your feedback to measure the accuracy of these suggestions and the value they add. If you are rejecting a certain code suggestion, please mention the reason briefly in the suggestion for us to capture it.
Comment Accepted (Y/N) Reason
#### Log Improvement Suggestion No: 1
#### Log Improvement Suggestion No: 2

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 792141be-a4e1-405b-9bd4-0744655ebac1

📥 Commits

Reviewing files that changed from the base of the PR and between 18782b4 and e01a083.

📒 Files selected for processing (1)
  • components/apimgt/org.wso2.carbon.apimgt.throttling.siddhi.extension/src/test/java/org/wso2/carbon/apimgt/throttling/siddhi/extension/ThrottleTimeBatchWindowTestCase.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • components/apimgt/org.wso2.carbon.apimgt.throttling.siddhi.extension/src/test/java/org/wso2/carbon/apimgt/throttling/siddhi/extension/ThrottleTimeBatchWindowTestCase.java

📝 Walkthrough

Walkthrough

ThrottleStreamProcessor.process() is modified to replace direct expired-event emission on window expiry with a RESET-type event. A resetEventChunk variable holds this event; when the window elapses, the method advances expireEventTime, clones the first retained expired event, converts it to RESET type with the updated timestamp, and forwards it to the next processor instead of the original expired event. Expired-event accumulation is constrained to populate only when empty, preserving at most the first event per window. Test assertions are updated to reflect zero removal events, and a new test validates aggregate reset behavior on window expiry.

Changes

ThrottleStreamProcessor RESET Event Emission

Layer / File(s) Summary
resetEventChunk declaration and window expiry handler
components/apimgt/org.wso2.carbon.apimgt.throttling.siddhi.extension/src/main/java/.../ThrottleStreamProcessor.java
Introduces resetEventChunk to hold the outgoing RESET event. On window expiry (when currentTime >= expireEventTime), advances expireEventTime, schedules the next notification, and conditionally clones the first stored expired event, converts it to RESET type with updated timestamp, then clears the expired-event chunk.
Expired-event accumulation constraint and RESET event forwarding
components/apimgt/org.wso2.carbon.apimgt.throttling.siddhi.extension/src/main/java/.../ThrottleStreamProcessor.java
Modifies expired-event accumulation to add to expiredEventChunk only when empty, ensuring at most the first expired event is retained per window. Forwards resetEventChunk to nextProcessor when populated on window expiry.
Test updates and window expiry reset validation
components/apimgt/org.wso2.carbon.apimgt.throttling.siddhi.extension/src/test/java/.../ThrottleTimeBatchWindowTestCase.java
Adds imports for List, CountDownLatch, CopyOnWriteArrayList, and TimeUnit. Updates existing test assertions in throttleTimeWindowBatchTest1 and throttleTimeWindowBatchTest2 to expect zero removal events. Introduces a new test method that collects inEvents across a throttling window expiry boundary and asserts post-expiry event group-by keys and aggregated count/sum values.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and directly describes the main change: sending a RESET event upon window expiry in the ThrottleStreamProcessor.
Description check ✅ Passed The description references the PR subject and issue number, which is related to the changeset's objective of sending RESET events upon window expiry.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@components/apimgt/org.wso2.carbon.apimgt.throttling.siddhi.extension/src/main/java/org/wso2/carbon/apimgt/throttling/siddhi/extension/ThrottleStreamProcessor.java`:
- Around line 168-181: The issue is that the old representative event in
expiredEventChunk is being cleared after new window events are added to it,
causing loss of the first event reference needed for the RESET event. To fix
this in the ThrottleStreamProcessor's process method, capture the first event
from expiredEventChunk before iterating through and adding new cloned stream
events (before the condition checking null == expiredEventChunk.getFirst()),
clear the expiredEventChunk before processing new events to separate old and new
data, and then after the iteration completes, use the captured old event to
construct and emit the RESET event via streamEventCloner.copyStreamEvent() and
streamEventChunk.add(resetEvent) instead of attempting to get it after clearing.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 99b02a12-a059-4d27-9527-9a3e5116640a

📥 Commits

Reviewing files that changed from the base of the PR and between 56a4da8 and 46e74f7.

📒 Files selected for processing (1)
  • components/apimgt/org.wso2.carbon.apimgt.throttling.siddhi.extension/src/main/java/org/wso2/carbon/apimgt/throttling/siddhi/extension/ThrottleStreamProcessor.java

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the custom Siddhi throttling timeBatch window implementation to emit a RESET event when a time window expires (per wso2/api-manager#5064), enabling downstream throttling aggregations to reset state on window boundaries.

Changes:

  • Renames the internal “send events” flag to sendResetEvent to reflect the new semantics.
  • Emits a StreamEvent.Type.RESET event on window expiry instead of forwarding an EXPIRED event.
  • Avoids storing more than one template event in expiredEventChunk (only the first is retained).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jun 15, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@components/apimgt/org.wso2.carbon.apimgt.throttling.siddhi.extension/src/test/java/org/wso2/carbon/apimgt/throttling/siddhi/extension/ThrottleTimeBatchWindowTestCase.java`:
- Around line 173-185: The test method does not guarantee that
executionPlanRuntime.shutdown() executes if an assertion fails before reaching
that line, potentially leaving the Siddhi runtime running. Wrap all the code
from executionPlanRuntime.start() through the assertion checks in a try block,
then place executionPlanRuntime.shutdown() in a finally block to ensure the
runtime is always properly shut down regardless of whether any assertion fails.
- Around line 156-180: The currentEvents ArrayList is written to asynchronously
by the QueryCallback.receive method on a callback thread while being read by the
main test thread at line 180, creating a race condition. Replace the plain
ArrayList with a thread-safe collection like CopyOnWriteArrayList to ensure
thread-safe access to events. Additionally, instead of relying on fixed sleep
durations (the Thread.sleep calls), implement a wait mechanism (such as using
CountDownLatch or a polling loop with timeout) to ensure the expected number of
events have arrived before attempting to access the currentEvents collection in
the firstEventAfterReset assignment. This will eliminate the flaky behavior
caused by timing-dependent assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 61d9a92b-cebf-4987-8d76-c31a4a2d96fe

📥 Commits

Reviewing files that changed from the base of the PR and between 1525fc0 and 18782b4.

📒 Files selected for processing (1)
  • components/apimgt/org.wso2.carbon.apimgt.throttling.siddhi.extension/src/test/java/org/wso2/carbon/apimgt/throttling/siddhi/extension/ThrottleTimeBatchWindowTestCase.java

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.

3 participants