Skip to content
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

[#5112] feat(core): support pre event for event listener systems #5110

Merged
merged 6 commits into from
Oct 15, 2024

Conversation

FANNG1
Copy link
Contributor

@FANNG1 FANNG1 commented Oct 11, 2024

What changes were proposed in this pull request?

support pre event for event listener systems

  1. add new PreEvent to represent Pre event and only SYNC event listeners could process PreEvent
  2. keep Event as post event to keep compatibility.
  3. EventBus dispatch event to corresponding event listeners.

Why are the changes needed?

Fix: #5112

Does this PR introduce any user-facing change?

no

How was this patch tested?

add UT

@FANNG1 FANNG1 marked this pull request as draft October 11, 2024 10:02
@FANNG1 FANNG1 changed the title [SIP] support pre event [SIP] support pre event for event listener systems Oct 11, 2024
@FANNG1 FANNG1 marked this pull request as ready for review October 12, 2024 01:58
@FANNG1 FANNG1 requested a review from jerryshao October 12, 2024 01:59
@FANNG1 FANNG1 changed the title [SIP] support pre event for event listener systems [#5112] feat(core): support pre event for event listener systems Oct 12, 2024
@FANNG1 FANNG1 force-pushed the pre_event branch 2 times, most recently from 9740528 to 269e7c6 Compare October 14, 2024 09:05
*
* <p>This method provides a hook for pre-operation event processing, any changes you make to the
* resources during the event will be reflected in the operations that follow. You must set mode
* to SYNC to process pre events.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to set SYNC to support pre events?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

@@ -35,25 +37,45 @@ public class EventBus {
// which are meant for synchronous event listening, or AsyncQueueListener, designed for
// asynchronous event processing.
private final List<EventListenerPlugin> postEventListeners;
// Only contain sync EventListenerPlugins.
private final List<EventListenerPlugin> preEventListeners;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to have 2 listeners for pre and post event separately?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

@FANNG1
Copy link
Contributor Author

FANNG1 commented Oct 14, 2024

@jerryshao please help to review again

*/
void onPostEvent(Event event) throws RuntimeException;
default void onPostEvent(Event postEvent) throws RuntimeException {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we change to default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

after supporting pre event, the user who only interest on pre event, could only implement onPreEvent interface, no need to implement onPostEvent interface

* Handles events generated after the completion of an operation. Implementers are responsible for
* processing these events, which may involve additional logic to respond to the operation
* outcomes.
* Handles events generated after the completion of an operation.
Copy link
Contributor

Choose a reason for hiding this comment

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

"Handle post-events..."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

default void onPostEvent(Event postEvent) throws RuntimeException {}

/**
* Handles pre events generated before the operation.
Copy link
Contributor

Choose a reason for hiding this comment

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

"Handle pre-events..."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

Comment on lines 80 to 85
private void dispatchPostEvent(Event postEvent) {
eventListeners.forEach(postEventListener -> postEventListener.onPostEvent(postEvent));
}

private void dispatchPreEvent(PreEvent preEvent) throws ForbiddenException {
eventListeners.forEach(preEventListener -> preEventListener.onPreEvent(preEvent));
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, we don't differentiate pre or post event listeners, the code here is a little confusing. We can change to:

  private void dispatchPostEvent(Event postEvent) {
    eventListeners.forEach(listener -> listener.onPostEvent(postEvent));
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 86 to 90
LOG.warn(
"Event listener {} process pre event {} failed, will skip the operation.",
listenerName,
preEvent.getClass().getSimpleName(),
e);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can extract the common part here to simplify the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

* Handles pre events generated before the operation.
*
* <p>This method handles pre-operation events in SYNC or ASYNC mode, any changes to resources in
* the event will inflect the subsequent operations.
Copy link
Contributor

Choose a reason for hiding this comment

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

"will affect..."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 118 to 120
* @throws ForbiddenException The operation will be skipped if and only if throwing {@code
* org.apache.gravitino.exceptions.ForbiddenException} and the event listener is SYNC mode,
* the exception will be ignored in other conditions.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please explain about the exception here, how you handle this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

@jerryshao jerryshao merged commit e9acd15 into apache:main Oct 15, 2024
26 checks passed
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.

[Improvement] Add pre event to event listeners
2 participants