pvary commented on code in PR #10484:
URL: https://github.com/apache/iceberg/pull/10484#discussion_r1724712629


##########
flink/v1.19/flink/src/main/java/org/apache/iceberg/flink/maintenance/operator/JdbcLockFactory.java:
##########
@@ -0,0 +1,246 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.flink.maintenance.operator;
+
+import java.io.IOException;
+import java.sql.DatabaseMetaData;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLNonTransientConnectionException;
+import java.sql.SQLTimeoutException;
+import java.sql.SQLTransientConnectionException;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.iceberg.jdbc.JdbcClientPool;
+import org.apache.iceberg.jdbc.UncheckedInterruptedException;
+import org.apache.iceberg.jdbc.UncheckedSQLException;
+import 
org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.util.PropertyUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class JdbcLockFactory implements TriggerLockFactory {
+  private static final Logger LOG = 
LoggerFactory.getLogger(JdbcLockFactory.class);
+  private static final String INIT_LOCK_TABLES_PROPERTY = 
"maintenance.lock.jdbc.init-lock-tables";
+  private static final String LOCK_TABLE_NAME = "maintenance_lock";
+  private static final int MAX_PREFIX_LENGTH = 100;
+  private static final String CREATE_LOCK_TABLE_SQL =
+      "CREATE TABLE "
+          + LOCK_TABLE_NAME
+          + "(LOCK_TYPE CHAR(1) NOT NULL, ISSUER VARCHAR("
+          + MAX_PREFIX_LENGTH
+          + ") NOT NULL, LOCK_ID CHAR(36) NOT NULL, PRIMARY KEY (LOCK_TYPE, 
ISSUER))";
+  private static final String CREATE_LOCK_SQL =
+      "INSERT INTO " + LOCK_TABLE_NAME + " (LOCK_TYPE, ISSUER, LOCK_ID) VALUES 
(?, ?, ?)";
+  private static final String GET_LOCK_SQL =
+      "SELECT LOCK_ID FROM " + LOCK_TABLE_NAME + " WHERE LOCK_TYPE=? AND 
ISSUER=?";
+  private static final String DELETE_LOCK_SQL =
+      "DELETE FROM " + LOCK_TABLE_NAME + " WHERE LOCK_TYPE=? AND ISSUER=? AND 
LOCK_ID=?";
+
+  private final String uri;
+  private final String issuer;
+  private final Map<String, String> properties;
+  private transient JdbcClientPool pool;
+
+  public JdbcLockFactory(String uri, String issuer, Map<String, String> 
properties) {
+    Preconditions.checkNotNull(uri, "JDBC connection URI is required");
+    Preconditions.checkArgument(
+        issuer.length() < MAX_PREFIX_LENGTH,
+        "Invalid prefix length: issuer should be shorter than %s",
+        MAX_PREFIX_LENGTH);
+    this.uri = uri;
+    this.issuer = issuer;
+    this.properties = properties;
+  }
+
+  @Override
+  public void open() {
+    this.pool = new JdbcClientPool(1, uri, properties);
+
+    if (PropertyUtil.propertyAsBoolean(properties, INIT_LOCK_TABLES_PROPERTY, 
false)) {
+      initializeLockTables();
+    }
+  }
+
+  /** Only used in testing to share the jdbc pool */
+  @VisibleForTesting
+  void open(JdbcLockFactory other) {
+    this.pool = other.pool;
+  }
+
+  @Override
+  public Lock createLock() {
+    return new Lock(pool, issuer, Type.MAINTENANCE);
+  }
+
+  @Override
+  public Lock createRecoveryLock() {
+    return new Lock(pool, issuer, Type.RECOVERY);
+  }
+
+  @Override
+  public void close() throws IOException {
+    pool.close();
+  }
+
+  private void initializeLockTables() {
+    LOG.trace("Creating database tables (if missing) to store table 
maintenance locks");
+    try {
+      pool.run(
+          conn -> {
+            DatabaseMetaData dbMeta = conn.getMetaData();
+            ResultSet tableExists =
+                dbMeta.getTables(
+                    null /* catalog name */,
+                    null /* schemaPattern */,
+                    LOCK_TABLE_NAME /* tableNamePattern */,
+                    null /* types */);
+            if (tableExists.next()) {
+              return true;
+            }
+
+            LOG.debug("Creating table {} to store iceberg catalog tables", 
LOCK_TABLE_NAME);
+            return conn.prepareStatement(CREATE_LOCK_TABLE_SQL).execute();

Review Comment:
   I don't see when this could be a failure and there would be no exception 
thrown. We could change the return null instead of the output of the execute, 
as we need to return something from the `run` method anyway, but I don't see a 
point adding more code here than neccessary.
   
   WDYT? 



##########
flink/v1.19/flink/src/main/java/org/apache/iceberg/flink/maintenance/operator/JdbcLockFactory.java:
##########
@@ -0,0 +1,246 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.flink.maintenance.operator;
+
+import java.io.IOException;
+import java.sql.DatabaseMetaData;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLNonTransientConnectionException;
+import java.sql.SQLTimeoutException;
+import java.sql.SQLTransientConnectionException;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.iceberg.jdbc.JdbcClientPool;
+import org.apache.iceberg.jdbc.UncheckedInterruptedException;
+import org.apache.iceberg.jdbc.UncheckedSQLException;
+import 
org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.util.PropertyUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class JdbcLockFactory implements TriggerLockFactory {
+  private static final Logger LOG = 
LoggerFactory.getLogger(JdbcLockFactory.class);
+  private static final String INIT_LOCK_TABLES_PROPERTY = 
"maintenance.lock.jdbc.init-lock-tables";
+  private static final String LOCK_TABLE_NAME = "maintenance_lock";
+  private static final int MAX_PREFIX_LENGTH = 100;
+  private static final String CREATE_LOCK_TABLE_SQL =
+      "CREATE TABLE "
+          + LOCK_TABLE_NAME
+          + "(LOCK_TYPE CHAR(1) NOT NULL, ISSUER VARCHAR("
+          + MAX_PREFIX_LENGTH
+          + ") NOT NULL, LOCK_ID CHAR(36) NOT NULL, PRIMARY KEY (LOCK_TYPE, 
ISSUER))";
+  private static final String CREATE_LOCK_SQL =
+      "INSERT INTO " + LOCK_TABLE_NAME + " (LOCK_TYPE, ISSUER, LOCK_ID) VALUES 
(?, ?, ?)";
+  private static final String GET_LOCK_SQL =
+      "SELECT LOCK_ID FROM " + LOCK_TABLE_NAME + " WHERE LOCK_TYPE=? AND 
ISSUER=?";
+  private static final String DELETE_LOCK_SQL =
+      "DELETE FROM " + LOCK_TABLE_NAME + " WHERE LOCK_TYPE=? AND ISSUER=? AND 
LOCK_ID=?";
+
+  private final String uri;
+  private final String issuer;
+  private final Map<String, String> properties;
+  private transient JdbcClientPool pool;
+
+  public JdbcLockFactory(String uri, String issuer, Map<String, String> 
properties) {
+    Preconditions.checkNotNull(uri, "JDBC connection URI is required");
+    Preconditions.checkArgument(
+        issuer.length() < MAX_PREFIX_LENGTH,
+        "Invalid prefix length: issuer should be shorter than %s",
+        MAX_PREFIX_LENGTH);
+    this.uri = uri;
+    this.issuer = issuer;
+    this.properties = properties;
+  }
+
+  @Override
+  public void open() {
+    this.pool = new JdbcClientPool(1, uri, properties);
+
+    if (PropertyUtil.propertyAsBoolean(properties, INIT_LOCK_TABLES_PROPERTY, 
false)) {
+      initializeLockTables();
+    }
+  }
+
+  /** Only used in testing to share the jdbc pool */
+  @VisibleForTesting
+  void open(JdbcLockFactory other) {
+    this.pool = other.pool;
+  }
+
+  @Override
+  public Lock createLock() {
+    return new Lock(pool, issuer, Type.MAINTENANCE);
+  }
+
+  @Override
+  public Lock createRecoveryLock() {
+    return new Lock(pool, issuer, Type.RECOVERY);
+  }
+
+  @Override
+  public void close() throws IOException {
+    pool.close();
+  }
+
+  private void initializeLockTables() {
+    LOG.trace("Creating database tables (if missing) to store table 
maintenance locks");
+    try {
+      pool.run(
+          conn -> {
+            DatabaseMetaData dbMeta = conn.getMetaData();
+            ResultSet tableExists =
+                dbMeta.getTables(
+                    null /* catalog name */,
+                    null /* schemaPattern */,
+                    LOCK_TABLE_NAME /* tableNamePattern */,
+                    null /* types */);
+            if (tableExists.next()) {
+              return true;
+            }
+
+            LOG.debug("Creating table {} to store iceberg catalog tables", 
LOCK_TABLE_NAME);
+            return conn.prepareStatement(CREATE_LOCK_TABLE_SQL).execute();

Review Comment:
   I don't see when this could be a failure and there would be no exception 
thrown. We could change the return null instead of the output of the execute, 
as we need to return something from the `run` method anyway, but I don't see a 
point adding more code here than necessary.
   
   WDYT? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to