Skip to content

Enhance event handling #407

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

Merged
merged 5 commits into from
Apr 28, 2021
Merged

Conversation

lburgazzoli
Copy link
Collaborator

@lburgazzoli lburgazzoli commented Apr 21, 2021

This PR includes:

  • add lifecycle hooks to EventSource (Add lifecycle hooks to EventSource #368)
  • review DefaultEventSourceManager instance variable modifiers
  • make DefaultEventSourceManager::registerEventSource final as it is invoked by the constructor
  • DefaultEventSourceManager use containsKey instead of null check to determine if an event source is already registerd
  • add lifecycle hooks to the operator so i.e. watchers can be properly closed

Fixes #368

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
…k to determine if an event source is already registerd
@lburgazzoli lburgazzoli force-pushed the github-368 branch 2 times, most recently from 4498d40 to f3d2ade Compare April 22, 2021 08:52
@lburgazzoli lburgazzoli marked this pull request as ready for review April 22, 2021 13:04
@metacosm metacosm self-requested a review April 27, 2021 16:21
@metacosm metacosm self-assigned this Apr 27, 2021
@@ -155,7 +180,8 @@ public void onClose(WatcherException e) {
if (e.isHttpGone()) {
log.warn("Received error for watch, will try to reconnect.", e);
try {
registerWatch();
close();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we really close all the watches? I guess there's no other option since we can't really know which watcher caused the exception…

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, the issue was that in case of an error on of a single watcher, calling registerWatch would re-register all the watcher, potentially you could end up in duplicated watchers.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That was another thing I wondered about (using a List for the watchers as opposed to a Set but I'm not sure we have consistent equals/hashCode implementations for the Watch implementations)…

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes we need a way to improve how watchers are handled, I may try to have a look once this is done

}

@Override
public <T extends EventSource> void registerEventSource(String name, T eventSource) {
public final void registerEventSource(String name, EventSource eventSource) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why did you make this method final?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

because it is invoked by the constructor so either the class is marked as final or this method need to be final to avoid issues in classes extending DefaultEventSourceManager and overriding registerEventSource

Copy link
Collaborator

@metacosm metacosm Apr 27, 2021

Choose a reason for hiding this comment

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

Ah yes. I remember hearing about this but I have never run into such issues in practice… Have you?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, got bitten hard by it 😂 that's why I notice them

@metacosm metacosm requested review from csviri and adam-sandor April 27, 2021 19:24
@@ -17,9 +17,8 @@
private static final Logger log = LoggerFactory.getLogger(DefaultEventSourceManager.class);

private final ReentrantLock lock = new ReentrantLock();
private Map<String, EventSource> eventSources = new ConcurrentHashMap<>();
private CustomResourceEventSource customResourceEventSource;
Copy link
Collaborator

@csviri csviri Apr 28, 2021

Choose a reason for hiding this comment

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

Do you see it correctly that this has been CustomResourceEventSource is moved to the Operator? Why is that?
The intention was that this class will own all the custom resources related to a controller.
But are there particular reasons to move it?
The downside is that if we want to have other events,between event sources and controllers vs event source, but also access access CustomResourceEventSource (where we thinking to move the caching for example) will not be accessible in this manager.

Copy link
Collaborator Author

@lburgazzoli lburgazzoli Apr 28, 2021

Choose a reason for hiding this comment

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

The reason was that the method:

public void registerCustomResourceEventSource(
      CustomResourceEventSource customResourceEventSource) {
    this.customResourceEventSource = customResourceEventSource;
    this.customResourceEventSource.addedToEventManager();
}

Was not really meaningful as yes the customResourceEventSource was assigned to an instance of the DefaultEventManager but was never accesses by the manager.

A better option would have been either to use registerEventSource as any other event source or pass the customResourceEventSource as a constructor parameter to make it clear that the DefaultEventManager requires it whereas as today, it looks like an unrelated method.

The event handler for the CustomResourceEventSource was also set outside the event manager directly in the operator which is quite confusing.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see, ok, yes this part definetelly needs improvements and refactorings. (Just like the others :) )
As I mentioned I would like to keep the event sources in one place, I think the constructor would make sense.
CustomResourceEventSource currently has quite special position, it's required always as an event source, so constructor is good ide also do express this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that I will probably make some other improvements after this, getting back to work on #392

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@csviri yep I think I got the reasoning and I do agree but for the sake of the lifecycle it was quite confusing so I opted to make at little bit more clear (i.e to have set-up / tear-down on the same side, in this case on the Operator). If we move it on the contructor then we can move its set-up/tear-down as part of the event manager.

@lburgazzoli
Copy link
Collaborator Author

@csviri @metacosm thx for the review

@metacosm metacosm merged commit 566e062 into operator-framework:master Apr 28, 2021
@metacosm
Copy link
Collaborator

Thank you!

@lburgazzoli lburgazzoli deleted the github-368 branch April 28, 2021 09:05
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.

Add lifecycle hooks to EventSource
3 participants