From 2e5a1d79a4b5e019645d24d703d149e41d1f3726 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 11 Jun 2023 14:36:00 +0100 Subject: [PATCH] Switch back to official Postgres JDBC driver --- bukkit-legacy/build.gradle | 2 +- bukkit/build.gradle | 2 +- bungee/build.gradle | 2 +- common/build.gradle | 2 +- .../common/dependencies/Dependency.java | 17 ++--- .../dependencies/DependencyRegistry.java | 2 +- .../messaging/postgres/PostgresMessenger.java | 64 ++++++++++++------- .../hikari/PostgresConnectionFactory.java | 10 +-- fabric/build.gradle | 2 +- forge/build.gradle | 2 +- nukkit/build.gradle | 2 +- sponge/build.gradle | 2 +- standalone/build.gradle | 4 +- velocity/build.gradle | 2 +- 14 files changed, 59 insertions(+), 56 deletions(-) diff --git a/bukkit-legacy/build.gradle b/bukkit-legacy/build.gradle index 6c9853388..3e21ee1a6 100644 --- a/bukkit-legacy/build.gradle +++ b/bukkit-legacy/build.gradle @@ -29,7 +29,7 @@ shadowJar { relocate 'me.lucko.commodore', 'me.lucko.luckperms.lib.commodore' relocate 'org.mariadb.jdbc', 'me.lucko.luckperms.lib.mariadb' relocate 'com.mysql', 'me.lucko.luckperms.lib.mysql' - relocate 'com.impossibl', 'me.lucko.luckperms.lib.postgresql' + relocate 'org.postgresql', 'me.lucko.luckperms.lib.postgresql' relocate 'com.zaxxer.hikari', 'me.lucko.luckperms.lib.hikari' relocate 'com.mongodb', 'me.lucko.luckperms.lib.mongodb' relocate 'org.bson', 'me.lucko.luckperms.lib.bson' diff --git a/bukkit/build.gradle b/bukkit/build.gradle index d4e636c02..9569036d8 100644 --- a/bukkit/build.gradle +++ b/bukkit/build.gradle @@ -39,7 +39,7 @@ shadowJar { relocate 'me.lucko.commodore', 'me.lucko.luckperms.lib.commodore' relocate 'org.mariadb.jdbc', 'me.lucko.luckperms.lib.mariadb' relocate 'com.mysql', 'me.lucko.luckperms.lib.mysql' - relocate 'com.impossibl', 'me.lucko.luckperms.lib.postgresql' + relocate 'org.postgresql', 'me.lucko.luckperms.lib.postgresql' relocate 'com.zaxxer.hikari', 'me.lucko.luckperms.lib.hikari' relocate 'com.mongodb', 'me.lucko.luckperms.lib.mongodb' relocate 'org.bson', 'me.lucko.luckperms.lib.bson' diff --git a/bungee/build.gradle b/bungee/build.gradle index 188043578..6ea4a7cb5 100644 --- a/bungee/build.gradle +++ b/bungee/build.gradle @@ -31,7 +31,7 @@ shadowJar { relocate 'me.lucko.commodore', 'me.lucko.luckperms.lib.commodore' relocate 'org.mariadb.jdbc', 'me.lucko.luckperms.lib.mariadb' relocate 'com.mysql', 'me.lucko.luckperms.lib.mysql' - relocate 'com.impossibl', 'me.lucko.luckperms.lib.postgresql' + relocate 'org.postgresql', 'me.lucko.luckperms.lib.postgresql' relocate 'com.zaxxer.hikari', 'me.lucko.luckperms.lib.hikari' relocate 'com.mongodb', 'me.lucko.luckperms.lib.mongodb' relocate 'org.bson', 'me.lucko.luckperms.lib.bson' diff --git a/common/build.gradle b/common/build.gradle index a22461cef..5958da14a 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -78,6 +78,6 @@ dependencies { compileOnly 'io.nats:jnats:2.16.4' compileOnly 'com.rabbitmq:amqp-client:5.12.0' compileOnly 'org.mongodb:mongodb-driver-legacy:4.5.0' - compileOnly 'com.impossibl.pgjdbc-ng:pgjdbc-ng:0.8.9' + compileOnly 'org.postgresql:postgresql:42.6.0' compileOnly 'org.yaml:snakeyaml:1.28' } diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java index d58b5f75e..3ec7fa6b4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java +++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java @@ -153,18 +153,11 @@ public enum Dependency { Relocation.of("mysql", "com{}mysql") ), POSTGRESQL_DRIVER( - "com{}impossibl{}pgjdbc-ng", - "pgjdbc-ng", - "0.8.9", - "WEYMezl02Rot2n2ATs7NABcvL9ceQ/oLA/XPduIEaWA=", - Relocation.of("postgresql", "com{}impossibl") - ), - POSTGRESQL_DRIVER_SPY( - "com{}impossibl{}pgjdbc-ng", - "spy", - "0.8.9", - "72ZuhpMy/4EYJZuSjBjGI5NGgdWmOpwjDHW9ISnqso8=", - Relocation.of("postgresql", "com{}impossibl") + "org{}postgresql", + "postgresql", + "42.6.0", + "uBfGekDJQkn9WdTmhuMyftDT0/rkJrINoPHnVlLPxGE=", + Relocation.of("postgresql", "org{}postgresql") ), H2_DRIVER_LEGACY( "com.h2database", diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyRegistry.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyRegistry.java index 45b4e52a5..73b3b08e3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyRegistry.java +++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyRegistry.java @@ -55,7 +55,7 @@ public class DependencyRegistry { .putAll(StorageType.MONGODB, Dependency.MONGODB_DRIVER_CORE, Dependency.MONGODB_DRIVER_LEGACY, Dependency.MONGODB_DRIVER_SYNC, Dependency.MONGODB_DRIVER_BSON) .putAll(StorageType.MARIADB, Dependency.SLF4J_API, Dependency.SLF4J_SIMPLE, Dependency.HIKARI, Dependency.MARIADB_DRIVER) .putAll(StorageType.MYSQL, Dependency.SLF4J_API, Dependency.SLF4J_SIMPLE, Dependency.HIKARI, Dependency.MYSQL_DRIVER) - .putAll(StorageType.POSTGRESQL, Dependency.SLF4J_API, Dependency.SLF4J_SIMPLE, Dependency.HIKARI, Dependency.POSTGRESQL_DRIVER, Dependency.POSTGRESQL_DRIVER_SPY) + .putAll(StorageType.POSTGRESQL, Dependency.SLF4J_API, Dependency.SLF4J_SIMPLE, Dependency.HIKARI, Dependency.POSTGRESQL_DRIVER) .putAll(StorageType.SQLITE, Dependency.SQLITE_DRIVER) .putAll(StorageType.H2, Dependency.H2_DRIVER) .build(); diff --git a/common/src/main/java/me/lucko/luckperms/common/messaging/postgres/PostgresMessenger.java b/common/src/main/java/me/lucko/luckperms/common/messaging/postgres/PostgresMessenger.java index 1f73fdf54..a79a9b63f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/messaging/postgres/PostgresMessenger.java +++ b/common/src/main/java/me/lucko/luckperms/common/messaging/postgres/PostgresMessenger.java @@ -25,8 +25,6 @@ package me.lucko.luckperms.common.messaging.postgres; -import com.impossibl.postgres.api.jdbc.PGConnection; -import com.impossibl.postgres.api.jdbc.PGNotificationListener; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.scheduler.SchedulerTask; import me.lucko.luckperms.common.storage.implementation.sql.SqlStorage; @@ -34,12 +32,17 @@ import net.luckperms.api.messenger.IncomingMessageConsumer; import net.luckperms.api.messenger.Messenger; import net.luckperms.api.messenger.message.OutgoingMessage; import org.checkerframework.checker.nullness.qual.NonNull; +import org.postgresql.PGConnection; +import org.postgresql.PGNotification; +import org.postgresql.util.PSQLException; +import java.net.SocketException; +import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.Statement; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; /** * An implementation of {@link Messenger} using Postgres. @@ -68,7 +71,7 @@ public class PostgresMessenger implements Messenger { @Override public void sendOutgoingMessage(@NonNull OutgoingMessage outgoingMessage) { - try (PGConnection connection = this.sqlStorage.getConnectionFactory().getConnection().unwrap(PGConnection.class)) { + try (Connection connection = this.sqlStorage.getConnectionFactory().getConnection()) { try (PreparedStatement ps = connection.prepareStatement("SELECT pg_notify(?, ?)")) { ps.setString(1, CHANNEL); ps.setString(2, outgoingMessage.asEncodedString()); @@ -122,47 +125,60 @@ public class PostgresMessenger implements Messenger { } } - private class NotificationListener implements PGNotificationListener, AutoCloseable { - private final CountDownLatch latch = new CountDownLatch(1); - private final AtomicBoolean listening = new AtomicBoolean(false); + private class NotificationListener implements AutoCloseable { + private static final int RECEIVE_TIMEOUT_MILLIS = 1000; + + private final AtomicBoolean open = new AtomicBoolean(true); + private final AtomicReference listeningThread = new AtomicReference<>(); public void listenAndBind() { - try (PGConnection connection = PostgresMessenger.this.sqlStorage.getConnectionFactory().getConnection().unwrap(PGConnection.class)) { - connection.addNotificationListener(CHANNEL, this); - + try (Connection connection = PostgresMessenger.this.sqlStorage.getConnectionFactory().getConnection()) { try (Statement s = connection.createStatement()) { s.execute("LISTEN \"" + CHANNEL + "\""); } - this.listening.set(true); - this.latch.await(); + PGConnection pgConnection = connection.unwrap(PGConnection.class); + this.listeningThread.set(Thread.currentThread()); + + while (this.open.get()) { + PGNotification[] notifications = pgConnection.getNotifications(RECEIVE_TIMEOUT_MILLIS); + if (notifications != null) { + for (PGNotification notification : notifications) { + handleNotification(notification); + } + } + } + + } catch (PSQLException e) { + if (!(e.getCause() instanceof SocketException && e.getCause().getMessage().equals("Socket closed"))) { + e.printStackTrace(); + } } catch (Exception e) { e.printStackTrace(); } finally { - this.listening.set(false); + this.listeningThread.set(null); } } public boolean isListening() { - return this.listening.get(); + return this.listeningThread.get() != null; } - @Override - public void notification(int processId, String channelName, String payload) { - if (!CHANNEL.equals(channelName)) { + public void handleNotification(PGNotification notification) { + if (!CHANNEL.equals(notification.getName())) { return; } - PostgresMessenger.this.consumer.consumeIncomingMessageAsString(payload); - } - - @Override - public void closed() { - this.latch.countDown(); + PostgresMessenger.this.consumer.consumeIncomingMessageAsString(notification.getParameter()); } @Override public void close() { - this.latch.countDown(); + if (this.open.compareAndSet(true, false)) { + Thread thread = this.listeningThread.get(); + if (thread != null) { + thread.interrupt(); + } + } } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/PostgresConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/PostgresConnectionFactory.java index 6fcfb8eae..ec4d7ae15 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/PostgresConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/PostgresConnectionFactory.java @@ -48,9 +48,9 @@ public class PostgresConnectionFactory extends HikariConnectionFactory { @Override protected void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password) { - config.setDataSourceClassName("com.impossibl.postgres.jdbc.PGDataSource"); + config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource"); config.addDataSourceProperty("serverName", address); - config.addDataSourceProperty("portNumber", Integer.parseInt(port)); + config.addDataSourceProperty("portNumber", port); config.addDataSourceProperty("databaseName", databaseName); config.addDataSourceProperty("user", username); config.addDataSourceProperty("password", password); @@ -63,12 +63,6 @@ public class PostgresConnectionFactory extends HikariConnectionFactory { // remove the default config properties which don't exist for PostgreSQL properties.remove("useUnicode"); properties.remove("characterEncoding"); - - // socketTimeout -> networkTimeout - Object socketTimeout = properties.remove("socketTimeout"); - if (socketTimeout != null) { - properties.putIfAbsent("networkTimeout", Integer.parseInt(socketTimeout.toString())); - } } @Override diff --git a/fabric/build.gradle b/fabric/build.gradle index aa43338d8..f5b0c4e2c 100644 --- a/fabric/build.gradle +++ b/fabric/build.gradle @@ -61,7 +61,7 @@ shadowJar { relocate 'me.lucko.commodore', 'me.lucko.luckperms.lib.commodore' relocate 'org.mariadb.jdbc', 'me.lucko.luckperms.lib.mariadb' relocate 'com.mysql', 'me.lucko.luckperms.lib.mysql' - relocate 'com.impossibl', 'me.lucko.luckperms.lib.postgresql' + relocate 'org.postgresql', 'me.lucko.luckperms.lib.postgresql' relocate 'com.zaxxer.hikari', 'me.lucko.luckperms.lib.hikari' relocate 'com.mongodb', 'me.lucko.luckperms.lib.mongodb' relocate 'org.bson', 'me.lucko.luckperms.lib.bson' diff --git a/forge/build.gradle b/forge/build.gradle index 5fde91d68..8904a7b77 100644 --- a/forge/build.gradle +++ b/forge/build.gradle @@ -39,7 +39,7 @@ shadowJar { relocate 'me.lucko.commodore', 'me.lucko.luckperms.lib.commodore' relocate 'org.mariadb.jdbc', 'me.lucko.luckperms.lib.mariadb' relocate 'com.mysql', 'me.lucko.luckperms.lib.mysql' - relocate 'com.impossibl', 'me.lucko.luckperms.lib.postgresql' + relocate 'org.postgresql', 'me.lucko.luckperms.lib.postgresql' relocate 'com.zaxxer.hikari', 'me.lucko.luckperms.lib.hikari' relocate 'com.mongodb', 'me.lucko.luckperms.lib.mongodb' relocate 'org.bson', 'me.lucko.luckperms.lib.bson' diff --git a/nukkit/build.gradle b/nukkit/build.gradle index da926e01c..92c0233aa 100644 --- a/nukkit/build.gradle +++ b/nukkit/build.gradle @@ -29,7 +29,7 @@ shadowJar { relocate 'me.lucko.commodore', 'me.lucko.luckperms.lib.commodore' relocate 'org.mariadb.jdbc', 'me.lucko.luckperms.lib.mariadb' relocate 'com.mysql', 'me.lucko.luckperms.lib.mysql' - relocate 'com.impossibl', 'me.lucko.luckperms.lib.postgresql' + relocate 'org.postgresql', 'me.lucko.luckperms.lib.postgresql' relocate 'com.zaxxer.hikari', 'me.lucko.luckperms.lib.hikari' relocate 'com.mongodb', 'me.lucko.luckperms.lib.mongodb' relocate 'org.bson', 'me.lucko.luckperms.lib.bson' diff --git a/sponge/build.gradle b/sponge/build.gradle index c1b895243..f618eaff3 100644 --- a/sponge/build.gradle +++ b/sponge/build.gradle @@ -42,7 +42,7 @@ shadowJar { relocate 'me.lucko.commodore', 'me.lucko.luckperms.lib.commodore' relocate 'org.mariadb.jdbc', 'me.lucko.luckperms.lib.mariadb' relocate 'com.mysql', 'me.lucko.luckperms.lib.mysql' - relocate 'com.impossibl', 'me.lucko.luckperms.lib.postgresql' + relocate 'org.postgresql', 'me.lucko.luckperms.lib.postgresql' relocate 'com.zaxxer.hikari', 'me.lucko.luckperms.lib.hikari' relocate 'com.mongodb', 'me.lucko.luckperms.lib.mongodb' relocate 'org.bson', 'me.lucko.luckperms.lib.bson' diff --git a/standalone/build.gradle b/standalone/build.gradle index d5269e6fb..c44f0c862 100644 --- a/standalone/build.gradle +++ b/standalone/build.gradle @@ -32,7 +32,7 @@ dependencies { testImplementation 'redis.clients:jedis:3.5.2' testImplementation 'io.nats:jnats:2.16.4' testImplementation 'com.rabbitmq:amqp-client:5.12.0' - testImplementation 'com.impossibl.pgjdbc-ng:pgjdbc-ng:0.8.9' + testImplementation 'org.postgresql:postgresql:42.6.0' testImplementation 'com.h2database:h2:2.1.214' testImplementation 'mysql:mysql-connector-java:8.0.23' testImplementation 'org.mariadb.jdbc:mariadb-java-client:3.1.3' @@ -60,7 +60,7 @@ shadowJar { relocate 'me.lucko.commodore', 'me.lucko.luckperms.lib.commodore' relocate 'org.mariadb.jdbc', 'me.lucko.luckperms.lib.mariadb' relocate 'com.mysql', 'me.lucko.luckperms.lib.mysql' - relocate 'com.impossibl', 'me.lucko.luckperms.lib.postgresql' + relocate 'org.postgresql', 'me.lucko.luckperms.lib.postgresql' relocate 'com.zaxxer.hikari', 'me.lucko.luckperms.lib.hikari' relocate 'com.mongodb', 'me.lucko.luckperms.lib.mongodb' relocate 'org.bson', 'me.lucko.luckperms.lib.bson' diff --git a/velocity/build.gradle b/velocity/build.gradle index 8b3ed2938..e255e729f 100644 --- a/velocity/build.gradle +++ b/velocity/build.gradle @@ -36,7 +36,7 @@ shadowJar { relocate 'me.lucko.commodore', 'me.lucko.luckperms.lib.commodore' relocate 'org.mariadb.jdbc', 'me.lucko.luckperms.lib.mariadb' relocate 'com.mysql', 'me.lucko.luckperms.lib.mysql' - relocate 'com.impossibl', 'me.lucko.luckperms.lib.postgresql' + relocate 'org.postgresql', 'me.lucko.luckperms.lib.postgresql' relocate 'com.zaxxer.hikari', 'me.lucko.luckperms.lib.hikari' relocate 'com.mongodb', 'me.lucko.luckperms.lib.mongodb' relocate 'org.bson', 'me.lucko.luckperms.lib.bson'