NIFI-1002: Added WebSocket support.#1184
Conversation
|
Hello NiFi developers, This PR contains a new set of WebSocket support components. At least there're two WebSocket processors available on GitHub already, nifi-websocket by @xmlking , and nifi-websockets-bundle by @acesir. I appreciate these projects providing WebSocket support for NiFi, and wanted to merge the capability to Apache NiFi code base. I referred these existing projects, and add few functionalities and Unit Test codes. Features:
It may look more complex, so for the description purpose, I wrote a blog post about how it works, and how it's designed. Please check this post as well, NiFi WebSocket Support. Any comment or advice would be appreciated, thanks in advance! |
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public abstract class AbstractWebSocketProcessor extends AbstractSessionFactoryProcessor { |
There was a problem hiding this comment.
Is there a reason why this class extends from _ AbstractSessionFactoryProcessor_ and not AbstractProcessor? Just trying to figure out the need for _ onTrigger()_ implementation that is identical to the one in AbstractProcessor.
There was a problem hiding this comment.
@olegz The reason why it extends AbstractSessionFactoryProcessor is to capture `processSessionFactory instance. So that it can create a session at onStopped() to flush incoming WebSocket messages those only exist on memory. I followed the pattern of AbstractMQTTProcessor.
|
|
||
| static List<PropertyDescriptor> getAbstractPropertyDescriptors(){ | ||
| final List<PropertyDescriptor> descriptors = new ArrayList<>(); | ||
| return descriptors; |
There was a problem hiding this comment.
I am not sure I understand this code
- For each call you create a new List
- It's always empty
Could you please clarify?
There was a problem hiding this comment.
@olegz As you suspected, it's not required, and it's there simply because I ported the basic code structure from MQTT processors. This method was used for sharing common property descriptors. However, since there's no such properties for now, it can be removed. I will remove this.
| } | ||
|
|
||
| protected ComponentLog logger; | ||
| protected ProcessSessionFactory processSessionFactory; |
There was a problem hiding this comment.
Since there is a potential for the above variables to be accessed by different thread, there are thread visibility concerns. Consider making them volatile.
There was a problem hiding this comment.
Added volatile keyword to these variables.
|
@olegz Thanks for taking time to review! I added a commit to address your feedback. Also, I've added |
| servletHandler.addServletWithMapping(JettyWebSocketServlet.class, "/*"); | ||
|
|
||
| // Need to specify classloader, otherwise since the callstack doesn't have any nifi specific class, so it can't use nar. | ||
| try (NarCloseable closeable = NarCloseable.withComponentNarLoader(WebSocketServerFactory.class)) { |
There was a problem hiding this comment.
This no longer compiles after recent work on dynamic class loading by @bbende
There was a problem hiding this comment.
Thanks Oleg, I removed the use of NarCloseable and confirmed that JettyWebSocketServer works without this now.
| .defaultValue("10000") | ||
| .build(); | ||
|
|
||
| private volatile LinkedBlockingQueue<WebSocketMessage> incomingMessageQueue; |
There was a problem hiding this comment.
This particular issue keeps coming up. Basically the pattern of maintaining an internal queue for messages may lead to OOM exception as well as lost data if processor crashes while the queue still has data. This is due to the fact that you are essentially integrating with external scheduling mechanism
One way of mitigating this is to rely on such mechanism for distributing messages while essentially ignoring our schedule. You can look at the example of it in SMTP processor
While may not be ideal, it certainly paves a simpler path for future improvement when we get a chance to define an Event-driver Processor abstraction as a first class component of NiFi.
There was a problem hiding this comment.
Thank you for bringing this up. I had been worrying about this queue, too. To make it simpler, I removed the intermediate queue, and let incoming messages to be transferred to NiFi relationships.
- Reflecting review comments - Added displayName to peroperty descriptors
0794b0d to
112fa70
Compare
- Reflecting review comments:
- Removed unnecessary use of NarCloseable.withComponentNarLoader.
- Removed intermediate on memory queue to make it simpler and more
robust. Received messages in WebSocket layer now will be transferred
to downstream relationships directly.
|
@olegz Thanks again for the great review! I rebased the PR and done additional refactoring based on your feedback. Please let me know if you prefer it to be squashed for further review process. |
|
Koji, this is great and I'll be looking at it shortly with hopes of merging it some time today. |
|
@ijokarumawak at this point I am going to say Great work! I am sure in month to come there will be things ti improve as people start using it , but LGTM for now. Will merge shortly! |
|
@olegz Thank you very much! I hope it will be helpful for users and willing to improve it over time! |
NIFI-1002: Added WebSocket support. - Reflecting review comments - Added displayName to peroperty descriptors NIFI-1002: Added WebSocket support. This closes apache#1184 - Reflecting review comments: - Removed unnecessary use of NarCloseable.withComponentNarLoader. - Removed intermediate on memory queue to make it simpler and more robust. Received messages in WebSocket layer now will be transferred to downstream relationships directly.
NIFI-1002: Added WebSocket support. - Reflecting review comments - Added displayName to peroperty descriptors NIFI-1002: Added WebSocket support. This closes apache#1184 - Reflecting review comments: - Removed unnecessary use of NarCloseable.withComponentNarLoader. - Removed intermediate on memory queue to make it simpler and more robust. Received messages in WebSocket layer now will be transferred to downstream relationships directly.
Thank you for submitting a contribution to Apache NiFi.
In order to streamline the review of the contribution we ask you
to ensure the following steps have been taken:
For all changes:
Is there a JIRA ticket associated with this PR? Is it referenced
in the commit message?
Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
Has your PR been rebased against the latest commit within the target branch (typically master)?
Is your initial contribution a single, squashed commit?
For code changes:
For documentation related changes:
Note:
Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.