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

Don't use scheduler to execute command threads #393

Merged
merged 2 commits into from Feb 25, 2019

Conversation

cdjackson
Copy link
Contributor

This uses a thread to execute the command rather than the scheduler which is not suitable for real-time commands.
Closes #392
Signed-off-by: Chris Jackson chris@cd-jackson.com

@hsudbrock
Copy link
Contributor

It would be really interesting to understand why these huge delays that you mention in #392 occur (it still sounds somewhat implausible to me, but I also don't have a good argument why the delays should not be caused by the scheduler provided by the BaseThingHandler).

I think the change in this PR is OK, but would it be OK for you to change to using a local scheduler, as you suggested in the discussion in #392? (I also think that using a scheduler might be the better solution, to avoid the overhead of thread creation on systems where many commands need to be handled by the ZigBee binding).

@cdjackson
Copy link
Contributor Author

It would be really interesting to understand why these huge delays that you mention in #392

I agree, but I've no way to find this out. I can only assume that with the 19 bindings that are running, something is taking a lot of time, or scheduling a lot of tasks, or... However, as the system is not mine, I have no way to find out.

What I can say is that the quick fix to use the thread solved the problem and commands were implemented quickly.

I will update to use a local scheduler.

@hsudbrock
Copy link
Contributor

What I can say is that the quick fix to use the thread solved the problem and commands were implemented quickly.

That sounds good :)

@cdjackson
Copy link
Contributor Author

I've updated this PR but haven't had the chance to test it - will do so in the weekend if necessary.

@@ -129,6 +131,8 @@

private boolean firmwareUpdateInProgress = false;

private ExecutorService commandScheduler = Executors.newCachedThreadPool();
Copy link
Contributor

Choose a reason for hiding this comment

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

With this change, a thread pool will be created for each zigbee thing handler instance. Shouldn't we rather use a shared pool for all zigbee thing handler instances, as follows (where the ThreadPoolManager is from package org.eclipse.smarthome.core.common), wdyt?

private ExecutorService commandScheduler = ThreadPoolManager.getPool("zigbee-thinghandler-commands");

As a side note, a colleague of mine mentioned that the thread pools returned by newCachedThreadPool turned out to be problematic in an OSGi setup, because they caused class loader leaks at least in our setting (but I don't know the details about those issues).

@cdjackson
Copy link
Contributor Author

cdjackson commented Feb 19, 2019 via email

@hsudbrock
Copy link
Contributor

I see your point to avoid dependencies between things (in particular, as it is probably dependencies between bindings via the BaseThingHandler scheduler that caused the issue leading to this PR).

I still think we should use a shared pool at least for all ZIgBee things, because I fear problems due to too many threads on slow hardware with many paired ZigBee devices if we don't share the pool.

@cdjackson
Copy link
Contributor Author

cdjackson commented Feb 19, 2019 via email

@hsudbrock
Copy link
Contributor

The advantage of the cached scheduler is only that threads are reused (at least that’s what I thought?)

I think so as well. But that means that if there are currently no threads used by a thing handler, I think that the pool for that handler will not immediately remove the unused threads from the pool, they will still be in the pool to be reused when needed. (Some pools will reduce the number of threads after some timeout.) That is, if each thing handler has its own pool as currently implemented, a system with, say, 50 ZigBee devices will have 50 pools with a total of 50*(average number of idle hreads per pool) threads on the system, which can be a lot for not-so-powerful systems. In contrast, when using one pool for all ZigBee thing handlers, there will be less idle threads in that single pool.

Or do I make a thinking error here?

@cdjackson
Copy link
Contributor Author

I'm not sure I'm convinced that it's a big issue, but on the other hand, you're right that it will use less threads. I'll change it, and if we find there are performance issues, we can always update again (although if there are, further investigation would probably be in order ;) ).

Signed-off-by: Chris Jackson <chris@cd-jackson.com>
Copy link
Contributor

@hsudbrock hsudbrock left a comment

Choose a reason for hiding this comment

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

lgtm, thanks Chris!

@hsudbrock hsudbrock merged commit b6d5e61 into openhab:master Feb 25, 2019
@cdjackson cdjackson added this to the 2.5 milestone Dec 8, 2019
@cdjackson cdjackson deleted the 392-command-execution branch April 5, 2020 15:20
@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/hue-binding-for-api-v2-3-4-0-0-4-0-0-0/146799/15

@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/hue-binding-for-api-v2-3-4-0-0-4-0-0-0/146799/30

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.

Extremely slow execution of commands
3 participants