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

Fix bug in migrateThingType to not call managed thing provider directly #3162

Merged
merged 2 commits into from
Dec 10, 2022

Conversation

Hilbrand
Copy link
Member

The migrateThingType call the managed thing provider directly instead of checking if it's actually a managed thing. This check is done in the thingUpdated method. Changed the code so it makes the same call as in thingUpdated.

This bug was trigged by a change in the systeminfo binding: openhab/openhab-addons#13708.

I've refactored the thingUpdated into a private method that is also called from migrateThingType as thingUpdated performs the same update method as was done in migrateThingType when the is managed. I've kept the lock and doPrivileged in the private method. I'm not sure if that is needed, but it seems to make sense as it's a guard in the updateThing, so why not in migrateThingType.

The `migrateThingType` call the managed thing provider directly instead of checking if it's actually a managed thing.
This check is done in the `thingUpdated` method. Changed the code so it makes the same call as in `thingUpdated`.

Signed-off-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
@lolodomo
Copy link
Contributor

Thank you a lot @Hilbrand .
We need that PR merged before the next 3.4 milestone is released.

J-N-K
J-N-K previously requested changes Nov 15, 2022
Copy link
Member

@J-N-K J-N-K left a comment

Choose a reason for hiding this comment

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

Nice catch. I have left some comments. Also please check why the integration tests fail.

@@ -472,6 +447,35 @@ protected synchronized void deactivate(ComponentContext componentContext) {
readyService.unregisterTracker(this);
}

private void thingUpdated(final Thing thing) {
thingUpdatedLock.add(thing.getUID());
AccessController.doPrivileged((PrivilegedAction<@Nullable Void>) () -> {
Copy link
Member

Choose a reason for hiding this comment

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

I think that the AccessController can be removed, it is deprecated for removal in Java 17 anyway and I know of no code that would depend on it in openHAB. But we should probably clean-up all code that uses the SecurityManager after we switched to Java 17 at the end of this year.

Comment on lines -273 to -288
if (provider instanceof ManagedProvider) {
@SuppressWarnings("unchecked")
ManagedProvider<Thing, ThingUID> managedProvider = (ManagedProvider<Thing, ThingUID>) provider;
managedProvider.update(thing);
} else {
logger.debug("Only updating thing {} in the registry because provider {} is not managed.",
thing.getUID().getAsString(), provider);
Thing oldThing = thingRegistry.get(thing.getUID());
if (oldThing == null) {
throw new IllegalArgumentException(
MessageFormat.format("Cannot update thing {0} because it is not known to the registry",
thing.getUID().getAsString()));
}
thingRegistry.updated(provider, oldThing, thing);
}
return null;
Copy link
Member

Choose a reason for hiding this comment

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

In fact this contradicts the description in the ThingHandlerCallback interface:

    /**
     * Informs about an update of the whole thing.
     *
     * @param thing thing that was updated (must not be null)
     * @throws IllegalStateException if the {@link Thing} is read-only.
     */
    void thingUpdated(Thing thing);

However, if we strictly follow the interface description this makes any dynamic properties or updates to file-based things impossible. IMO the interface desciption should be updated to reflect the current code.

I also wonder why we check if the thing exists. I would be very astonished if we got a provider from the ThingRegistry when the thing itself is not in the registry. I think this was introduced in #1134 to prevent the null-error for Registry.updated. It would probably be better to check if the thing is present in the registry before we try to get the provider and fail early if it is not in the registry (the registry fails silently if you try to update a thing that is not in the registry).

@mherwege
Copy link
Contributor

Thank you for analyzing this and solving this. For the record, I believe this migrateThingType method was introduced for the ZWave binding. It is also used in the Sonos, Shelly and Miio bindings.

@J-N-K
Copy link
Member

J-N-K commented Nov 15, 2022

It's also used in 3rd party add-ons.

@Hilbrand
Copy link
Member Author

Hilbrand commented Nov 16, 2022

@J-N-K the itests do seem to fail due to this change. But debugging these tests is a really horrible, so it will take me some time. Do you know how to enable logging in the test so it shows? The only log I get is from the iteat themselves, but not from the core code.

@J-N-K
Copy link
Member

J-N-K commented Nov 19, 2022

I usually set the log messages I'm interested in to level ERROR, then they show up.

@wborn
Copy link
Member

wborn commented Nov 21, 2022

You can easily change the log level for all loggers here:

org.ops4j.pax.logging.DefaultServiceLog.level=WARN

@lolodomo
Copy link
Contributor

No chance to finish before the new milestone is generated later today?

@wborn wborn added bug An unexpected problem or unintended behavior of the Core awaiting feedback labels Nov 30, 2022
@J-N-K
Copy link
Member

J-N-K commented Dec 7, 2022

The tests failed because the thing handler was unregistered before migration but the registration of the new handler was missing.

@openhab/core-maintainers Can someone else review now that I contributed to this PR? Thanks.

Signed-off-by: Jan N. Klug <github@klug.nrw>
@J-N-K J-N-K force-pushed the fix-migrateThingTypeBug branch from 24d3629 to ebca363 Compare December 7, 2022 20:25
@J-N-K J-N-K dismissed their stale review December 7, 2022 20:29

added as commit

@andrewfg
Copy link
Contributor

andrewfg commented Dec 9, 2022

^
Polite reminder: can we try to get this into 3.4 RC :)

Copy link
Member

@wborn wborn left a comment

Choose a reason for hiding this comment

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

LGTM and thank you both!

@wborn wborn merged commit 207c4a0 into openhab:main Dec 10, 2022
@wborn wborn added this to the 3.4 milestone Dec 10, 2022
@Hilbrand Hilbrand deleted the fix-migrateThingTypeBug branch December 10, 2022 09:30
splatch pushed a commit to ConnectorIO/copybara-hab-core that referenced this pull request Jul 12, 2023
…ly (openhab#3162)

The `migrateThingType` call the managed thing provider directly instead of checking if it's actually a managed thing.
This check is done in the `thingUpdated` method. Changed the code so it makes the same call as in `thingUpdated`.

Also-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
GitOrigin-RevId: 207c4a0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or unintended behavior of the Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants