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

[MongoDB Persistence] Fix error 500 and various improvements #10584

Merged
merged 23 commits into from
May 9, 2021

Conversation

boomer41
Copy link
Contributor

@boomer41 boomer41 commented Apr 26, 2021

[MongoDB Persistence] Bugfixes and various improvements

I noticed the MongoDB persistence driver had several issues, with the biggest one being the completely broken query support.
Additionally, connection recovery to the database server was not working, either.
Also, the indices were reordered to improve performance.
Last but not least some code style changed were introduced.
Please see the commits for detailed changes. I split the changes in small commits for easier review.

The changes were tested in my local openHAB snapshot instance, running in docker.

Prebuilt module:
mongodb-persistence.zip

Fixes #10574

boomer41 added 16 commits April 26, 2021 12:20
Currently, the implementation throws a RuntimeException.
This fails horribly in query() and store(), because the exception is never caught and connection reestablishment won't happen.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
The correct database name is "MongoDB" without a space.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
…ed()

When the connection was established successfully once, and the server goes down (e. g. a restart of the DB server), the connection still seems to be OK.
This would throw an exception later on, which is not caught and connection reestablishment won't happen.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
… database.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
…ure fails.

When, for example, the creation of the index fails (disk full, ...), the connection is still there, but the collection isn't created with it's appropriate indices.
Thus, disconnect when the procedure fails to ensure no database connection remains valid.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
Create the shared collection with its indices when collectionPerItem is disabled, not enabled.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
Preparation for synchronization.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
This allows to easify implementing necessary multithreading locks.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
Though the MongoDB driver is thread-safe, we still need to synchronize connection establishment/closing/...

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
…utes.

The given attributes should not be NotNullByDefault, because some may be null, too.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
…Client.

The mongo always succeeds in creating the connection.
We have to actually force it to test the connection to try to connect to the server.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
Issues:
- Second should have been getEndDate, not getBeginDate
- Send a Date object as the other version wasn't serializable.
- EndDate would have overridden beginDate.

Fixes openhab#10574

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
@boomer41 boomer41 requested a review from a team as a code owner April 26, 2021 10:33
…ver.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
The order (Item, Timestamp) makes much more sense and should be better for performance.
All queries are always done with an item, and the dates are optional.
Also, the database server can now execute a range query for the date.

Existing indices are not dropped for backwards compatibility with other systems analyzing the data.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
@boomer41
Copy link
Contributor Author

boomer41 commented Apr 27, 2021

CI failure seems unrelated to my changes? Looks like Maven can't find a dependency for the plclogo binding.
The run before my last commit went through successfully, though.

@wborn wborn added rebuild Triggers Jenkins PR build and removed rebuild Triggers Jenkins PR build labels Apr 28, 2021
@boomer41 boomer41 force-pushed the 10574-mongodb-persistence branch 2 times, most recently from c7cf63c to f4bbd6f Compare April 30, 2021 21:07
boomer41 added 4 commits May 1, 2021 00:03
Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
This was removed in 1f8e452 by mistake.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
The new method throws the real underlying exception instead of a generic one. This allows for detailed logging.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
… tool.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
@boomer41 boomer41 force-pushed the 10574-mongodb-persistence branch from f4bbd6f to 4170805 Compare April 30, 2021 22:04
@boomer41
Copy link
Contributor Author

boomer41 commented May 1, 2021

PR should be ready for review. By now I should have spotted most issues as I use the driver in production here.

Copy link
Member

@fwolter fwolter 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!

@boomer41 Can you check if this fixes also #10230?

@boomer41
Copy link
Contributor Author

boomer41 commented May 9, 2021

@boomer41 Can you check if this fixes also #10230?

Yes, this is the same issue as I opened. That'ss exactly the reason i looked into it ;)

@cweitkamp already linked this issue to this PR.

@fwolter fwolter changed the title [MongoDB Persistence] Bugfixes and various improvements [MongoDB Persistence] Fix error 500 and various improvements May 9, 2021
@fwolter fwolter merged commit d674814 into openhab:main May 9, 2021
@fwolter fwolter added the bug An unexpected problem or unintended behavior of an add-on label May 9, 2021
@fwolter fwolter added this to the 3.1 milestone May 9, 2021
@fwolter
Copy link
Member

fwolter commented May 9, 2021

Thanks!

@boomer41 boomer41 deleted the 10574-mongodb-persistence branch May 9, 2021 19:21
themillhousegroup pushed a commit to themillhousegroup/openhab2-addons that referenced this pull request May 10, 2021
…#10584)

* [MongoDB Persistence] Rename connectToDatabase to tryConnectToDatabase.

Currently, the implementation throws a RuntimeException.
This fails horribly in query() and store(), because the exception is never caught and connection reestablishment won't happen.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix JavaDoc

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix Bundle name

The correct database name is "MongoDB" without a space.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Also check for connection validity in isConnected()

When the connection was established successfully once, and the server goes down (e. g. a restart of the DB server), the connection still seems to be OK.
This would throw an exception later on, which is not caught and connection reestablishment won't happen.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Disconnect from database before reconnecting to database.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Disconnect from database when connection procedure fails.

When, for example, the creation of the index fails (disk full, ...), the connection is still there, but the collection isn't created with it's appropriate indices.
Thus, disconnect when the procedure fails to ensure no database connection remains valid.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix logic

Create the shared collection with its indices when collectionPerItem is disabled, not enabled.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Access database via getDatabase()

Preparation for synchronization.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove global mongoCollection variable.

This allows to easify implementing necessary multithreading locks.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Codestyle improvements.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Move isConnected-Check into tryConnectToDatabase.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Synchronize all connection handling methods.

Though the MongoDB driver is thread-safe, we still need to synchronize connection establishment/closing/...

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove all unnecessary @NonNullByDefault attributes.

The given attributes should not be NotNullByDefault, because some may be null, too.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove duplicate logging.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Test the db connection after creating the MongoClient.

The mongo always succeeds in creating the connection.
We have to actually force it to test the connection to try to connect to the server.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix date query.

Issues:
- Second should have been getEndDate, not getBeginDate
- Send a Date object as the other version wasn't serializable.
- EndDate would have overridden beginDate.

Fixes openhab#10574

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add debug log for MongoDB query sent to the server.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Reorder index on collections.

The order (Item, Timestamp) makes much more sense and should be better for performance.
All queries are always done with an item, and the dates are optional.
Also, the database server can now execute a range query for the date.

Existing indices are not dropped for backwards compatibility with other systems analyzing the data.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add @author JavaDoc as per developer guidelines.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Improve logging

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add @NonNullByDefault

This was removed in 1f8e452 by mistake.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Do not use isConnected() to test the connection.

The new method throws the real underlying exception instead of a generic one. This allows for detailed logging.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove useless @inheritdoc as per code analysis tool.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
Signed-off-by: John Marshall <john.marshall.au@gmail.com>
computergeek1507 pushed a commit to computergeek1507/openhab-addons that referenced this pull request Jul 13, 2021
…#10584)

* [MongoDB Persistence] Rename connectToDatabase to tryConnectToDatabase.

Currently, the implementation throws a RuntimeException.
This fails horribly in query() and store(), because the exception is never caught and connection reestablishment won't happen.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix JavaDoc

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix Bundle name

The correct database name is "MongoDB" without a space.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Also check for connection validity in isConnected()

When the connection was established successfully once, and the server goes down (e. g. a restart of the DB server), the connection still seems to be OK.
This would throw an exception later on, which is not caught and connection reestablishment won't happen.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Disconnect from database before reconnecting to database.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Disconnect from database when connection procedure fails.

When, for example, the creation of the index fails (disk full, ...), the connection is still there, but the collection isn't created with it's appropriate indices.
Thus, disconnect when the procedure fails to ensure no database connection remains valid.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix logic

Create the shared collection with its indices when collectionPerItem is disabled, not enabled.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Access database via getDatabase()

Preparation for synchronization.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove global mongoCollection variable.

This allows to easify implementing necessary multithreading locks.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Codestyle improvements.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Move isConnected-Check into tryConnectToDatabase.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Synchronize all connection handling methods.

Though the MongoDB driver is thread-safe, we still need to synchronize connection establishment/closing/...

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove all unnecessary @NonNullByDefault attributes.

The given attributes should not be NotNullByDefault, because some may be null, too.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove duplicate logging.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Test the db connection after creating the MongoClient.

The mongo always succeeds in creating the connection.
We have to actually force it to test the connection to try to connect to the server.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix date query.

Issues:
- Second should have been getEndDate, not getBeginDate
- Send a Date object as the other version wasn't serializable.
- EndDate would have overridden beginDate.

Fixes openhab#10574

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add debug log for MongoDB query sent to the server.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Reorder index on collections.

The order (Item, Timestamp) makes much more sense and should be better for performance.
All queries are always done with an item, and the dates are optional.
Also, the database server can now execute a range query for the date.

Existing indices are not dropped for backwards compatibility with other systems analyzing the data.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add @author JavaDoc as per developer guidelines.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Improve logging

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add @NonNullByDefault

This was removed in 1f8e452 by mistake.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Do not use isConnected() to test the connection.

The new method throws the real underlying exception instead of a generic one. This allows for detailed logging.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove useless @inheritdoc as per code analysis tool.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
frederictobiasc pushed a commit to frederictobiasc/openhab-addons that referenced this pull request Oct 26, 2021
…#10584)

* [MongoDB Persistence] Rename connectToDatabase to tryConnectToDatabase.

Currently, the implementation throws a RuntimeException.
This fails horribly in query() and store(), because the exception is never caught and connection reestablishment won't happen.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix JavaDoc

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix Bundle name

The correct database name is "MongoDB" without a space.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Also check for connection validity in isConnected()

When the connection was established successfully once, and the server goes down (e. g. a restart of the DB server), the connection still seems to be OK.
This would throw an exception later on, which is not caught and connection reestablishment won't happen.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Disconnect from database before reconnecting to database.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Disconnect from database when connection procedure fails.

When, for example, the creation of the index fails (disk full, ...), the connection is still there, but the collection isn't created with it's appropriate indices.
Thus, disconnect when the procedure fails to ensure no database connection remains valid.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix logic

Create the shared collection with its indices when collectionPerItem is disabled, not enabled.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Access database via getDatabase()

Preparation for synchronization.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove global mongoCollection variable.

This allows to easify implementing necessary multithreading locks.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Codestyle improvements.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Move isConnected-Check into tryConnectToDatabase.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Synchronize all connection handling methods.

Though the MongoDB driver is thread-safe, we still need to synchronize connection establishment/closing/...

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove all unnecessary @NonNullByDefault attributes.

The given attributes should not be NotNullByDefault, because some may be null, too.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove duplicate logging.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Test the db connection after creating the MongoClient.

The mongo always succeeds in creating the connection.
We have to actually force it to test the connection to try to connect to the server.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix date query.

Issues:
- Second should have been getEndDate, not getBeginDate
- Send a Date object as the other version wasn't serializable.
- EndDate would have overridden beginDate.

Fixes openhab#10574

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add debug log for MongoDB query sent to the server.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Reorder index on collections.

The order (Item, Timestamp) makes much more sense and should be better for performance.
All queries are always done with an item, and the dates are optional.
Also, the database server can now execute a range query for the date.

Existing indices are not dropped for backwards compatibility with other systems analyzing the data.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add @author JavaDoc as per developer guidelines.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Improve logging

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add @NonNullByDefault

This was removed in 1f8e452 by mistake.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Do not use isConnected() to test the connection.

The new method throws the real underlying exception instead of a generic one. This allows for detailed logging.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove useless @inheritdoc as per code analysis tool.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
thinkingstone pushed a commit to thinkingstone/openhab-addons that referenced this pull request Nov 7, 2021
…#10584)

* [MongoDB Persistence] Rename connectToDatabase to tryConnectToDatabase.

Currently, the implementation throws a RuntimeException.
This fails horribly in query() and store(), because the exception is never caught and connection reestablishment won't happen.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix JavaDoc

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix Bundle name

The correct database name is "MongoDB" without a space.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Also check for connection validity in isConnected()

When the connection was established successfully once, and the server goes down (e. g. a restart of the DB server), the connection still seems to be OK.
This would throw an exception later on, which is not caught and connection reestablishment won't happen.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Disconnect from database before reconnecting to database.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Disconnect from database when connection procedure fails.

When, for example, the creation of the index fails (disk full, ...), the connection is still there, but the collection isn't created with it's appropriate indices.
Thus, disconnect when the procedure fails to ensure no database connection remains valid.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix logic

Create the shared collection with its indices when collectionPerItem is disabled, not enabled.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Access database via getDatabase()

Preparation for synchronization.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove global mongoCollection variable.

This allows to easify implementing necessary multithreading locks.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Codestyle improvements.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Move isConnected-Check into tryConnectToDatabase.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Synchronize all connection handling methods.

Though the MongoDB driver is thread-safe, we still need to synchronize connection establishment/closing/...

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove all unnecessary @NonNullByDefault attributes.

The given attributes should not be NotNullByDefault, because some may be null, too.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove duplicate logging.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Test the db connection after creating the MongoClient.

The mongo always succeeds in creating the connection.
We have to actually force it to test the connection to try to connect to the server.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix date query.

Issues:
- Second should have been getEndDate, not getBeginDate
- Send a Date object as the other version wasn't serializable.
- EndDate would have overridden beginDate.

Fixes openhab#10574

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add debug log for MongoDB query sent to the server.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Reorder index on collections.

The order (Item, Timestamp) makes much more sense and should be better for performance.
All queries are always done with an item, and the dates are optional.
Also, the database server can now execute a range query for the date.

Existing indices are not dropped for backwards compatibility with other systems analyzing the data.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add @author JavaDoc as per developer guidelines.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Improve logging

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add @NonNullByDefault

This was removed in 1f8e452 by mistake.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Do not use isConnected() to test the connection.

The new method throws the real underlying exception instead of a generic one. This allows for detailed logging.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove useless @inheritdoc as per code analysis tool.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
marcfischerboschio pushed a commit to bosch-io/openhab-addons that referenced this pull request May 5, 2022
…#10584)

* [MongoDB Persistence] Rename connectToDatabase to tryConnectToDatabase.

Currently, the implementation throws a RuntimeException.
This fails horribly in query() and store(), because the exception is never caught and connection reestablishment won't happen.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix JavaDoc

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix Bundle name

The correct database name is "MongoDB" without a space.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Also check for connection validity in isConnected()

When the connection was established successfully once, and the server goes down (e. g. a restart of the DB server), the connection still seems to be OK.
This would throw an exception later on, which is not caught and connection reestablishment won't happen.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Disconnect from database before reconnecting to database.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Disconnect from database when connection procedure fails.

When, for example, the creation of the index fails (disk full, ...), the connection is still there, but the collection isn't created with it's appropriate indices.
Thus, disconnect when the procedure fails to ensure no database connection remains valid.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix logic

Create the shared collection with its indices when collectionPerItem is disabled, not enabled.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Access database via getDatabase()

Preparation for synchronization.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove global mongoCollection variable.

This allows to easify implementing necessary multithreading locks.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Codestyle improvements.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Move isConnected-Check into tryConnectToDatabase.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Synchronize all connection handling methods.

Though the MongoDB driver is thread-safe, we still need to synchronize connection establishment/closing/...

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove all unnecessary @NonNullByDefault attributes.

The given attributes should not be NotNullByDefault, because some may be null, too.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove duplicate logging.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Test the db connection after creating the MongoClient.

The mongo always succeeds in creating the connection.
We have to actually force it to test the connection to try to connect to the server.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Fix date query.

Issues:
- Second should have been getEndDate, not getBeginDate
- Send a Date object as the other version wasn't serializable.
- EndDate would have overridden beginDate.

Fixes openhab#10574

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add debug log for MongoDB query sent to the server.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Reorder index on collections.

The order (Item, Timestamp) makes much more sense and should be better for performance.
All queries are always done with an item, and the dates are optional.
Also, the database server can now execute a range query for the date.

Existing indices are not dropped for backwards compatibility with other systems analyzing the data.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add @author JavaDoc as per developer guidelines.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Improve logging

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Add @NonNullByDefault

This was removed in 1f8e452 by mistake.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Do not use isConnected() to test the connection.

The new method throws the real underlying exception instead of a generic one. This allows for detailed logging.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>

* [MongoDB Persistence] Remove useless @inheritdoc as per code analysis tool.

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
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 an add-on
Projects
None yet
3 participants