Skip to content

Commit

Permalink
[#3562] Migrate to Quarkus JDBC implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
harism committed Oct 15, 2023
1 parent 6757e40 commit f4350c9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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));

}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -124,7 +124,7 @@ protected Future<ResultSet> read(final SQLOperations operations, final DeviceKey

return expanded
.trace(this.tracer, spanContext)
.query(this.client);
.query(operations);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -337,14 +338,14 @@ public Future<Versioned<Void>> 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));
Expand Down Expand Up @@ -658,6 +659,7 @@ public Future<UpdateResult> 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.
Expand All @@ -666,7 +668,7 @@ public Future<UpdateResult> dropTenant(final String tenantId, final SpanContext
* @return A future tracking the outcome of the operation.
* @throws NullPointerException if tenant is {@code null}.
*/
public Future<Integer> getDeviceCount(final String tenantId, final SpanContext spanContext, final Statement countStatement, final String field, final String value) {
public Future<Integer> getDeviceCount(final SQLOperations operations, final String tenantId, final SpanContext spanContext, final Statement countStatement, final String field, final String value) {

Objects.requireNonNull(tenantId);

Expand All @@ -684,7 +686,7 @@ public Future<Integer> 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()) {
Expand Down Expand Up @@ -958,7 +960,7 @@ public Future<SearchResult<DeviceWithId>> findDevices(final String tenantId, fin
.withTag(TracingHelper.TAG_TENANT_ID, tenantId)
.start();

final Future<Integer> deviceCountFuture = getDeviceCount(tenantId, span.context(), countStatement, field, value);
final Future<Integer> deviceCountFuture = getDeviceCount(this.client, tenantId, span.context(), countStatement, field, value);

return deviceCountFuture
.compose(count -> expanded.trace(this.tracer, span.context()).query(this.client))
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -221,7 +221,7 @@ public Future<Versioned<Void>> 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
Expand Down Expand Up @@ -445,13 +445,13 @@ protected Future<UpdateResult> 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()));
}

/**
Expand Down
6 changes: 5 additions & 1 deletion services/device-registry-base/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
Expand Down Expand Up @@ -101,6 +101,10 @@
<artifactId>cryptvault</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal</artifactId>
</dependency>

<!-- testing -->
<dependency>
Expand Down
14 changes: 13 additions & 1 deletion services/device-registry-jdbc/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2022 Contributors to the Eclipse Foundation
Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
See the NOTICE file(s) distributed with this work for additional
information regarding copyright ownership.
Expand Down Expand Up @@ -62,8 +62,20 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-kafka-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal</artifactId>
</dependency>

<!-- JDBC drivers -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f4350c9

Please sign in to comment.