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

[mqtt][HA] ConcurrentHashMap to avoid ConcurrentModificationException #8239

Merged
merged 2 commits into from
Aug 22, 2020

Conversation

bodiroga
Copy link

@bodiroga bodiroga commented Aug 1, 2020

Hi @jochen314!

I've been able to fix the ConcurrentModifiedException errors using a ConcurrentHashMap for the 'results' of the discovery process.

What do you think?

Sadly the problem described in #7716 (comment) is still present 😞

Fixes #7661

Signed-off-by: Aitor Iturrioz [email protected]

@bodiroga bodiroga requested a review from davidgraeff as a code owner August 1, 2020 14:09
@TravisBuddy
Copy link

Travis tests have failed

Hey @bodiroga,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

@@ -65,7 +65,7 @@
private final Logger logger = LoggerFactory.getLogger(HomeAssistantDiscovery.class);
protected final Map<String, Set<HaID>> componentsPerThingID = new TreeMap<>();
protected final Map<String, ThingUID> thingIDPerTopic = new TreeMap<>();
protected final Map<String, DiscoveryResult> results = new TreeMap<>();
protected final ConcurrentHashMap<String, DiscoveryResult> results = new ConcurrentHashMap<>();
Copy link
Member

Choose a reason for hiding this comment

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

Keep the Map in the declaration. But another suggestion:

Why don‘t you create an inner class

public static class HADiscoveryResult {
    public final Set<HaId> components = new HashSet<>();
    public @Nullable DiscoveryResult discoveryResult;
}

and use one Map<ThingUID, HADiscoveryResult> instead of the two sets you have now? Makes it easier to be thread safe late when clearing the Map.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @J-N-K!

I have changed the results declaration type, but I would prefer not to change the rest of the implementation, as it works fine. Do you think it's necessary?

@TravisBuddy
Copy link

Travis tests have failed

Hey @bodiroga,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

Signed-off-by: Aitor Iturrioz <[email protected]>
@bodiroga
Copy link
Author

Hi @J-N-K!

Sorry to bother you, but is there any option to get this merged for OH 2.5.8? And also: should I pay attention to Travis?

Thank you!

@Hilbrand Hilbrand requested a review from J-N-K August 21, 2020 13:51
@J-N-K
Copy link
Member

J-N-K commented Aug 22, 2020

Travis fails in disabled itest

@J-N-K J-N-K merged commit 448e5cf into openhab:2.5.x Aug 22, 2020
taboneclayton pushed a commit to taboneclayton/openhab-addons that referenced this pull request Aug 23, 2020
…openhab#8239)

* [mqtt][homeassistant] Use ConcurrentHashMap to avoid ConcurrentModificationException

Signed-off-by: Aitor Iturrioz <[email protected]>
Signed-off-by: Clayton Tabone <[email protected]>
@jochen314
Copy link
Contributor

I do not think, that this will fix #7661
We would need to make componentsPerThingID a ConcurrentHashMap and also the Set, which we put into it:

Set<HaID> components = componentsPerThingID.computeIfAbsent(thingID, key -> ConcurrentHashMap.newKeySet());

When we are at it, we should make thingIDPerTopic a ConcurrentHashMap to be sure.

The changes to results should not be needed, as all access is synchonized.

@bodiroga
Copy link
Author

I do not think, that this will fix #7661
We would need to make componentsPerThingID a ConcurrentHashMap and also the Set, which we put into it:

Set<HaID> components = componentsPerThingID.computeIfAbsent(thingID, key -> ConcurrentHashMap.newKeySet());

When we are at it, we should make thingIDPerTopic a ConcurrentHashMap to be sure.

The changes to results should not be needed, as all access is synchonized.

Hi @jochen314!

As you guys know more than me, I don't know if your proposed changes are really required or not, all I can say is that with the PR merged, I haven't seeing the error again. Was I the only one with that error message?

@jochen314
Copy link
Contributor

I see. I did not notice, that you had my second change (line 168) already in.
This reduces the change of a CME by a lot.
But there still is a chance for one, because componentsPerThingID is not thread safe

@bodiroga
Copy link
Author

So it is just a matter of changing componentsPerThingID and thingIDPerTopic to ConcurrentHashMaps? Nothing more? I can test it and provide a PR if you want!

@bodiroga bodiroga deleted the fix-7661 branch August 28, 2020 14:33
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
…openhab#8239)

* [mqtt][homeassistant] Use ConcurrentHashMap to avoid ConcurrentModificationException

Signed-off-by: Aitor Iturrioz <[email protected]>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
…openhab#8239)

* [mqtt][homeassistant] Use ConcurrentHashMap to avoid ConcurrentModificationException

Signed-off-by: Aitor Iturrioz <[email protected]>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
…openhab#8239)

* [mqtt][homeassistant] Use ConcurrentHashMap to avoid ConcurrentModificationException

Signed-off-by: Aitor Iturrioz <[email protected]>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
…openhab#8239)

* [mqtt][homeassistant] Use ConcurrentHashMap to avoid ConcurrentModificationException

Signed-off-by: Aitor Iturrioz <[email protected]>
DaanMeijer pushed a commit to DaanMeijer/openhab-addons that referenced this pull request Sep 1, 2020
…openhab#8239)

* [mqtt][homeassistant] Use ConcurrentHashMap to avoid ConcurrentModificationException

Signed-off-by: Aitor Iturrioz <[email protected]>
Signed-off-by: Daan Meijer <[email protected]>
CSchlipp pushed a commit to CSchlipp/openhab-addons that referenced this pull request Sep 12, 2020
…openhab#8239)

* [mqtt][homeassistant] Use ConcurrentHashMap to avoid ConcurrentModificationException

Signed-off-by: Aitor Iturrioz <[email protected]>
markus7017 pushed a commit to markus7017/openhab-addons that referenced this pull request Sep 19, 2020
…openhab#8239)

* [mqtt][homeassistant] Use ConcurrentHashMap to avoid ConcurrentModificationException

Signed-off-by: Aitor Iturrioz <[email protected]>
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.

[mqtt.homeassistant] ConcurrentModificationException in console/log
4 participants