From f4350c9c232d59a876210ecf305cd2868569d68b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harri=20Sm=C3=A5tt?= Date: Sun, 15 Oct 2023 11:14:41 +0300 Subject: [PATCH] [#3562] Migrate to Quarkus JDBC implementation --- .../base/jdbc/config/JdbcProperties.java | 23 +++++++------------ .../store/device/AbstractDeviceStore.java | 4 ++-- .../store/device/TableManagementStore.java | 14 ++++++----- .../jdbc/store/tenant/ManagementStore.java | 8 +++---- services/device-registry-base/pom.xml | 6 ++++- services/device-registry-jdbc/pom.xml | 14 ++++++++++- .../src/main/resources/application.properties | 3 +++ 7 files changed, 43 insertions(+), 29 deletions(-) diff --git a/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/config/JdbcProperties.java b/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/config/JdbcProperties.java index 4f0b067fa5..f4b0bd453d 100644 --- a/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/config/JdbcProperties.java +++ b/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/config/JdbcProperties.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2022 Contributors to the Eclipse Foundation + * Copyright (c) 2020, 2023 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -24,6 +24,7 @@ import io.vertx.core.Vertx; import io.vertx.core.json.JsonObject; import io.vertx.ext.jdbc.JDBCClient; +import io.vertx.ext.jdbc.spi.impl.AgroalCPDataSourceProvider; /** * Configuration properties for a JDBC service. @@ -115,28 +116,20 @@ public void setTableName(final String tableName) { public static JDBCClient dataSource(final Vertx vertx, final JdbcProperties dataSourceProperties) { final JsonObject config = new JsonObject() - .put("url", dataSourceProperties.getUrl()) - .put("user", dataSourceProperties.getUsername()); - - // password is added later, after logging - - if (dataSourceProperties.getDriverClass() != null) { - config.put("driver_class", dataSourceProperties.getDriverClass()); - } - if (dataSourceProperties.getMaximumPoolSize() != null) { - config.put("max_pool_size", dataSourceProperties.getMaximumPoolSize()); - } + .put("dataSourceImplementation", "AGROAL") + .put("jdbcUrl", dataSourceProperties.getUrl()) + .put("principal", dataSourceProperties.getUsername()) + .put("maxSize", dataSourceProperties.getMaximumPoolSize()); log.info("Creating new SQL client: {} - table: {}", config, dataSourceProperties.getTableName()); // put password after logging - config - .put("password", dataSourceProperties.getPassword()); + config.put("credential", dataSourceProperties.getPassword()); // create new client - return JDBCClient.create(vertx, config); + return JDBCClient.create(vertx, new AgroalCPDataSourceProvider().init(config)); } diff --git a/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/device/AbstractDeviceStore.java b/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/device/AbstractDeviceStore.java index 91363f447b..7db6a3c195 100644 --- a/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/device/AbstractDeviceStore.java +++ b/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/device/AbstractDeviceStore.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2022 Contributors to the Eclipse Foundation + * Copyright (c) 2020, 2023 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -124,7 +124,7 @@ protected Future read(final SQLOperations operations, final DeviceKey return expanded .trace(this.tracer, spanContext) - .query(this.client); + .query(operations); } diff --git a/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/device/TableManagementStore.java b/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/device/TableManagementStore.java index e1a27ded42..a2fe911c72 100644 --- a/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/device/TableManagementStore.java +++ b/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/device/TableManagementStore.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2022 Contributors to the Eclipse Foundation + * Copyright (c) 2020, 2023 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -60,6 +60,7 @@ import io.vertx.ext.jdbc.JDBCClient; import io.vertx.ext.sql.ResultSet; import io.vertx.ext.sql.SQLConnection; +import io.vertx.ext.sql.SQLOperations; import io.vertx.ext.sql.UpdateResult; /** @@ -337,14 +338,14 @@ public Future> createDevice( log.debug("createDevice - statement: {}", expanded); - return getDeviceCount(key.getTenantId(), span.context(), this.countDevicesOfTenantStatement, null, null) + return getDeviceCount(connection, key.getTenantId(), span.context(), this.countDevicesOfTenantStatement, null, null) .compose(currentDeviceCount -> tenant.checkDeviceLimitReached( key.getTenantId(), currentDeviceCount, globalDevicesPerTenantLimit)) .compose(ok -> expanded .trace(this.tracer, context) - .update(this.client) + .update(connection) .recover(SQL::translateException)) .compose(x -> createGroups(connection, key, new HashSet<>(device.getMemberOf()), context)); @@ -658,6 +659,7 @@ public Future dropTenant(final String tenantId, final SpanContext /** * Gets the number of devices that are registered for a tenant. * + * @param operations The SQL operations instance to use. * @param tenantId The tenant to count devices for. * @param spanContext The span to contribute to. * @param countStatement The count statement to use. @@ -666,7 +668,7 @@ public Future dropTenant(final String tenantId, final SpanContext * @return A future tracking the outcome of the operation. * @throws NullPointerException if tenant is {@code null}. */ - public Future getDeviceCount(final String tenantId, final SpanContext spanContext, final Statement countStatement, final String field, final String value) { + public Future getDeviceCount(final SQLOperations operations, final String tenantId, final SpanContext spanContext, final Statement countStatement, final String field, final String value) { Objects.requireNonNull(tenantId); @@ -684,7 +686,7 @@ public Future getDeviceCount(final String tenantId, final SpanContext s return expanded .trace(this.tracer, span.context()) - .query(this.client) + .query(operations) .map(r -> { final var entries = r.getRows(true); switch (entries.size()) { @@ -958,7 +960,7 @@ public Future> findDevices(final String tenantId, fin .withTag(TracingHelper.TAG_TENANT_ID, tenantId) .start(); - final Future deviceCountFuture = getDeviceCount(tenantId, span.context(), countStatement, field, value); + final Future deviceCountFuture = getDeviceCount(this.client, tenantId, span.context(), countStatement, field, value); return deviceCountFuture .compose(count -> expanded.trace(this.tracer, span.context()).query(this.client)) diff --git a/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/tenant/ManagementStore.java b/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/tenant/ManagementStore.java index 5d2c4d8f6b..0e7baa72c8 100644 --- a/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/tenant/ManagementStore.java +++ b/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/tenant/ManagementStore.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2022 Contributors to the Eclipse Foundation + * Copyright (c) 2020, 2023 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -221,7 +221,7 @@ public Future> create(final String tenantId, final Tenant tenant log.debug("create - statement: {}", expanded); return expanded .trace(this.tracer, span.context()) - .update(this.client) + .update(connection) .recover(SQL::translateException) // insert all trust anchors @@ -445,13 +445,13 @@ protected Future updateJsonField( // execute update final var result = expanded .trace(this.tracer, span.context()) - .update(this.client); + .update(operations); // process result, check optimistic lock return checkOptimisticLock( result, span, resourceVersion, - checkSpan -> readTenantEntryById(this.client, tenantId, checkSpan.context())); + checkSpan -> readTenantEntryById(operations, tenantId, checkSpan.context())); } /** diff --git a/services/device-registry-base/pom.xml b/services/device-registry-base/pom.xml index 6197ceedef..e1229d7434 100644 --- a/services/device-registry-base/pom.xml +++ b/services/device-registry-base/pom.xml @@ -1,6 +1,6 @@ diff --git a/services/device-registry-jdbc/pom.xml b/services/device-registry-jdbc/pom.xml index 990c2d1c62..6a8d90a6e5 100644 --- a/services/device-registry-jdbc/pom.xml +++ b/services/device-registry-jdbc/pom.xml @@ -1,6 +1,6 @@ + + io.quarkus + quarkus-jdbc-postgresql + + + io.quarkus + quarkus-jdbc-h2 + com.h2database h2 diff --git a/services/device-registry-jdbc/src/main/resources/application.properties b/services/device-registry-jdbc/src/main/resources/application.properties index 35eb4801a1..7be05aa117 100644 --- a/services/device-registry-jdbc/src/main/resources/application.properties +++ b/services/device-registry-jdbc/src/main/resources/application.properties @@ -3,3 +3,6 @@ quarkus.jackson.accept-case-insensitive-enums=true # fail deserialization of JSON objects sent by clients if they contain unexpected content quarkus.jackson.fail-on-unknown-properties=true +# enable h2 and postgres extensions +quarkus.datasource.h2.db-kind=h2 +quarkus.datasource.pg.db-kind=pg