🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

KNOX-1997: Adding changes to buffer messages from backend in onMessag… - #143

Merged
moresandeep merged 10 commits into
apache:masterfrom
goelrajat:KNOX-1997
Nov 13, 2019
Merged

moresandeep merged 10 commits into
apache:masterfrom
goelrajat:KNOX-1997

Conversation

@goelrajat

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Adding changes to buffer messages from backend in onMessageText() if frontend session related data structures have not been setup i.e. remote is null. Message buffer will be flushed when remote is set by other thread executing onWebSocketConnect() API. To synchronize reading/flushing buffer, added a lock

How was this patch tested?

Race condition was easily reproducible with our service UI and backedn when integrated with Knox. After this change, I no longer see the NullPointerException. Since this is a race condition, its hard to add a unit test case for it. In my case, the race condition was getting hit when service backend used to send a data frame immediately after opening a websocket connection but Knox hadn't setup the connection related data structures. So I added a test case for scenario where send a message to frontend while opening a websocket.

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.

So I don't know this code, but it seems weird to me that getRemote returns null at all. It looks like the remote null check is fixing the symptom not the root cause. Is it possible to make getRemote block and never return null? I think that would avoid having to buffer messages at all?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What I observed that remote was being created in other thread which was executing onWebSocketConnect() while this current thread received a message. This might be an limitation with Jetty itself, check: signalapp/Signal-Desktop#1840.

I checked jetty examples and even there the server is using locks before accessing remote, as mentioned in the above link.

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.

I prefer to avoid locks if they can be avoided. Instead of buffering can we wait for a configurable amount of time to see if the remote object is created before quitting ? This should not be an issue IMO since this should be on a different thread than other connections.

@goelrajat goelrajat Sep 9, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried adding a fixed delay of few seconds in onMessageText() API but I was still seeing the issue in some cases. So adding a fixed configurable delay will not work. I aslo want to avoid locks as they incur a performance overhead. IF you have any suggestions on how can locks be avoided, let me know. Thanks.

@risdenk
risdenk requested a review from moresandeep September 6, 2019 14:40

@moresandeep moresandeep 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.

Thanks @goelrajat, the patch is on track, we requested few changes. The concerning one is about Locks, if we can avoid them that would be great. there are callback functions, not sure of we can leverage them but it would be good to try them out ! Thanks again for the patch and for finding and troubleshooting it !

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.

I don't like a locks here, can they be avoided, e.g. by using configurable wait in public void onMessageText(String message, Object session) { not sure if this is optimal, this is the first thing that came to my mind.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried adding a fixed delay of few seconds but I was still seeing the issue in some cases. So delay is unpredictable.

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.

Lock in a callback seems like a bad idea, especially the lock is dependent on the creation of the front end session. Is there a way to avoid locks ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I could not think of any other way, haven't worked much with Jetty. If you can provide any pointers, I will be happy to try things out.

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.

I prefer to avoid locks if they can be avoided. Instead of buffering can we wait for a configurable amount of time to see if the remote object is created before quitting ? This should not be an issue IMO since this should be on a different thread than other connections.

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.

This should probably go inside of try{ in case getRemote() throws an exception.

Comment thread gateway-spi/src/main/java/org/apache/knox/gateway/config/GatewayConfig.java Outdated
@goelrajat

Copy link
Copy Markdown
Contributor Author

Any updates on whether this PR can be merged or something more needs to be done here.

@risdenk

risdenk commented Sep 19, 2019

Copy link
Copy Markdown
Contributor

@goelrajat you might have to rebase your commit on latest master to get Travis builds to pass. Just pushed a build fix a few minutes ago. I'll leave it to @smolnar82 and @moresandeep to review

@goelrajat

Copy link
Copy Markdown
Contributor Author

@risdenk Rebase has no impact:
bash-3.2$ git pull origin master
From https://github.com/goelrajat/knox

  • branch master -> FETCH_HEAD
    Already up to date.
    bash-3.2$ git rebase master
    Current branch KNOX-1997 is up to date.
    bash-3.2$

@smolnar82

Copy link
Copy Markdown
Contributor

https://github.com/goelrajat/knox

Your master is behind apache/knox by 21 commits:

Screen Shot 2019-09-20 at 1 36 33 PM

First, you need to pull changes from apache/master to your master then you can rebase.

@goelrajat

Copy link
Copy Markdown
Contributor Author

@smolnar82 Done. Thanks

@smolnar82 smolnar82 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.

LGTM

@smolnar82

Copy link
Copy Markdown
Contributor

One more thing: since you added a new configuration in gateway-site.xml it should be documented (even if it's optional). Could you please open another JIRA to modify the user guide?
Thanks!

@smolnar82

Copy link
Copy Markdown
Contributor

@goelrajat - tests are failing:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.218 s <<< FAILURE! - in org.apache.knox.gateway.websockets.WebsocketServerInitiatedMessageTest
[ERROR] testGatewayRewriteHttpEcho(org.apache.knox.gateway.websockets.WebsocketServerInitiatedMessageTest)  Time elapsed: 1.063 s  <<< ERROR!
java.util.concurrent.TimeoutException: Timed out reading message from queue
	at org.apache.knox.gateway.websockets.WebsocketServerInitiatedMessageTest.testGatewayRewriteHttpEcho(WebsocketServerInitiatedMessageTest.java:166)```

@goelrajat

goelrajat commented Sep 20, 2019 •

Copy link
Copy Markdown
Contributor Author

@smolnar82 Test case is passing on my local build setup. However, I have increased timeout now and updated the test case. Hopefully, should work now.

Raised a ticket for documentation: https://issues.apache.org/jira/browse/KNOX-2019

@smolnar82

smolnar82 commented Sep 20, 2019 •

Copy link
Copy Markdown
Contributor

@goelrajat - tests are failing again:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /src/gateway-test-release-utils/src/main/java/org/apache/knox/gateway/GatewayTestConfig.java:[38,8] org.apache.knox.gateway.GatewayTestConfig is not abstract and does not override abstract method getWebsocketMaxWaitBufferCount() in org.apache.knox.gateway.config.GatewayConfig

Before you submit a new patch could you please run the following command locally:
mvn clean -T1C verify -Prelease,package

@risdenk risdenk 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.

@moresandeep / @smolnar82 any other comments about this?

@smolnar82

Copy link
Copy Markdown
Contributor

Nope; works for me now!

@risdenk risdenk self-assigned this Sep 23, 2019

@moresandeep moresandeep 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.

@goelrajat apologies for the delay, I think I am ok with the locks. I can't think of a better way to do it right now and don't want to hold up the patch for it. I requested few changes on the unit tests, once we have that the patch is good to go (I'll do some internal testing as well to make sure we don't break anything).
Thanks again !

@risdenk risdenk assigned moresandeep and unassigned risdenk Sep 25, 2019
@goelrajat

goelrajat commented Sep 27, 2019 •

Copy link
Copy Markdown
Contributor Author

Travis build failed due to failure in test case WebsocketServerInitiatedMessageTest, however the same test case is passing on my local setup. I tried multiple times but it works on my side.

bash-3.2$ mvn test -Dtest=org.apache.knox.gateway.websockets.WebsocketServerInitiatedMessageTest
[INFO] Running org.apache.knox.gateway.websockets.WebsocketServerInitiatedMessageTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.504 s - in org.apache.knox.gateway.websockets.WebsocketServerInitiatedMessageTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

Can someone please check?

rajat.goel added 6 commits September 27, 2019 14:44
…eText() if frontend session related data structures have not been setup i.e. remote is null. Message buffer will be flushed when remote is set by other thread executing onWebSocketConnect() API. To synchronise reading/flushing buffer, added a lock
@goelrajat

Copy link
Copy Markdown
Contributor Author

Test case changes have been done. Please check and merge.

@smolnar82

Copy link
Copy Markdown
Contributor

LGTM +1

@goelrajat

Copy link
Copy Markdown
Contributor Author

@moresandeep Please check and merge this one as well.

@goelrajat

goelrajat commented Oct 11, 2019 •

Copy link
Copy Markdown
Contributor Author

@moresandeep Can you please merge this PR? I want to raise another PR for KNOX-2004 and its changes are dependent on this PR.

@moresandeep

Copy link
Copy Markdown
Contributor

@moresandeep Can you please merge this PR? I want to raise another PR for KNOX-2004 and its changes are dependent on this PR.

@goelrajat I will get to it probably mid next week, since this is pretty involved and touches a bunch of things I would like to test it thoroughly.

@risdenk

risdenk commented Oct 30, 2019

Copy link
Copy Markdown
Contributor

@moresandeep any update here?

@moresandeep

Copy link
Copy Markdown
Contributor

Still underwater, hoping to find some time by the end of this week.

@goelrajat

Copy link
Copy Markdown
Contributor Author

Any updates on this one please? When can we merge this ? Its been idle for too long now.

@moresandeep moresandeep 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.

Looks good, sorry for the delay in reviewing.

@moresandeep
moresandeep merged commit 96587a4 into apache:master Nov 13, 2019
@goelrajat
goelrajat deleted the KNOX-1997 branch November 22, 2019 15:40
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.

4 participants