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

Introduce separate thread pool for establishing Initiator connections #255

Merged
merged 7 commits into from
Jun 8, 2020

Conversation

chrjohn
Copy link
Member

@chrjohn chrjohn commented Jan 7, 2020

Fixes #254

  • introduced separate thread pool with 3 threads for connection establishment
  • changed enabled flag in Session to volatile and removed synchronization from setEnabled/isEnabled since I could not find any good reason why it was synchronized
    ** volatile should be enough to ensure that all threads see the current state now that it is checked from distinct threads
  • removed synchronized from IoSessionInitiator.ConnectTask.run() since I could not find any good reason why it was synchronized

 * Fixes #254
 * introduced separate thread pool with 3 threads for connection establishment
 * changed `enabled` flag in `Session` to `volatile` and removed synchronization from `setEnabled`/`isEnabled` since I could not find any good reason why it was synchronized
 ** `volatile` should ensure that all threads should see the current state and prevents possible deadlocks now that flag is checked from distinct threads
 * removed `synchronized` from `IoSessionInitiator.ConnectTask.run()` since I could not find any good reason why it was synchronized
to test LGTM check
 - by default 3 threads are used
 - changed executor to be non-static, i.e. each Initiator instance has its own reconnect thread pool
  - (unless feature is disabled which will use the former behaviour)
  - to use the former behaviour (i.e. use "QFJ Timer" thread for both reconnections and calls to next()) you should pass 0 as number of reconnect threads
 - when passing 1 you will end up with a separate thread for reconnections; next() will still be called by "QFJ Timer" thread
@chrjohn chrjohn added this to the QFJ 2.2.0 milestone Apr 8, 2020
@chrjohn chrjohn added the please review asking for a review label Apr 20, 2020
super(settings, sessionFactory);
IoBuffer.setAllocator(new SimpleBufferAllocator());
IoBuffer.setUseDirectBuffer(false);
if (numReconnectThreads > 0) {
scheduledReconnectExecutor = Executors.newScheduledThreadPool(numReconnectThreads, new QFScheduledReconnectThreadFactory());
((ThreadPoolExecutor) scheduledReconnectExecutor).setMaximumPoolSize(numReconnectThreads);
Copy link
Member Author

Choose a reason for hiding this comment

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

Setting maximumPoolSize to initialPoolSize ensures that numReconnectThreads threads are available to service tasks. Otherwise new threads only get created when the task queue is full.

@@ -318,7 +318,7 @@ protected void startSessionTimer() {
if (shortLivedExecutor != null) {
timerTask = new DelegatingTask(timerTask, shortLivedExecutor);
}
sessionTimerFuture = scheduledExecutorService.scheduleAtFixedRate(timerTask, 0, 1000L,
sessionTimerFuture = SCHEDULED_EXECUTOR.scheduleAtFixedRate(timerTask, 0, 1000L,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not directly related to this change, but we probably don't want to queue up timer tasks if they take too long for some reason.

.scheduleWithFixedDelay(reconnectTask, 0, 1, TimeUnit.SECONDS);

Copy link
Member Author

Choose a reason for hiding this comment

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

The reconnectTasks timed out after a maximum of 2000ms. That was the reason to have a bigger pool to service these requests, see #254 .
What do you suggest to prevent piling up these tasks?

Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to IoSessionInitiator. As a rule of thumb scheduleWithFixedDelay is better in majority of cases than scheduleAtFixedRate. It respects the delay, but it only reschedules when the tasks finishes - this does not allow bursts if for some reason task is slow.

@chrjohn chrjohn removed the please review asking for a review label Jun 3, 2020
@chrjohn chrjohn merged commit c7c19a3 into master Jun 8, 2020
@chrjohn chrjohn deleted the connection-timeout-254 branch June 8, 2020 13:52
jli8000 pushed a commit to jli8000/quickfixj that referenced this pull request Sep 22, 2020
…quickfix-j#255)

 * Fixes quickfix-j#254
 * introduced separate thread pool with 3 threads for connection establishment
 * changed `enabled` flag in `Session` to `volatile` and removed synchronization from `setEnabled`/`isEnabled` since I could not find any good reason why it was synchronized
 ** `volatile` should ensure that all threads should see the current state and prevents possible deadlocks now that flag is checked from distinct threads
 * removed `synchronized` from `IoSessionInitiator.ConnectTask.run()` since I could not find any good reason why it was synchronized
* - prevent NPE on Logon in Session when not specifying messageFactory in
AbstractSessionConnectorBuilder
* - added possibility to specify number of reconnect threads via builder
 - by default 3 threads are used
 - changed executor to be non-static, i.e. each Initiator instance has its own reconnect thread pool
  - (unless feature is disabled which will use the former behaviour)
  - to use the former behaviour (i.e. use "QFJ Timer" thread for both reconnections and calls to next()) you should pass 0 as number of reconnect threads
 - when passing 1 you will end up with a separate thread for reconnections; next() will still be called by "QFJ Timer" thread
chrjohn added a commit that referenced this pull request Oct 19, 2020
 - see #254 / #255 for original issue
 - improved error message in AbstractSocketAcceptor/Initiator
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.

Connection timeout on one Session will block timer-related tasks on other Sessions (cf. QFJ-291)
3 participants