phet commented on code in PR #3715:
URL: https://github.com/apache/gobblin/pull/3715#discussion_r1263168291


##########
gobblin-runtime/src/main/java/org/apache/gobblin/runtime/api/MysqlMultiActiveLeaseArbiter.java:
##########
@@ -155,19 +163,26 @@ public MysqlMultiActiveLeaseArbiter(Config config) throws 
IOException {
     } catch (SQLException e) {
       throw new IOException("Table creation failure for " + 
leaseArbiterTableName, e);
     }
+    initializeConstantsTable();
+
+    log.info("MysqlMultiActiveLeaseArbiter initialized");
+  }
+
+  // Initialize Constants table if needed and insert row into it if one does 
not exist
+  private void initializeConstantsTable() throws IOException {
     String createConstantsStatement = 
String.format(CREATE_CONSTANTS_TABLE_STATEMENT, this.constantsTableName);
     withPreparedStatement(createConstantsStatement, createStatement -> 
createStatement.executeUpdate(), true);
 
-    int count = withPreparedStatement(String.format(GET_ROW_COUNT_STATEMENT, 
this.constantsTableName), getStatement -> {
+    Optional<Integer> count = 
withPreparedStatement(String.format(GET_ROW_COUNT_STATEMENT, 
this.constantsTableName), getStatement -> {
       ResultSet resultSet = getStatement.executeQuery();
       if (resultSet.next()) {
-        return resultSet.getInt(1);
+        return Optional.of(resultSet.getInt(1));
       }
-      return -1;
+      return Optional.absent();
     }, true);
 
     // Only insert epsilon and linger values from config if this table does 
not contain pre-existing values.
-    if (count == 0) {
+    if (count.isPresent() && count.get() == 0) {

Review Comment:
   with multiple participants, use of "check, then set" is error-prone.  e.g. 
two or more rows could be inserted.
   
   the algo requires there to only ever be *exactly one*.  as this is 
essential, add a primary key field that should only ever have one value (== 1). 
 every INSERT should hard-code that PK value.  to seamlessly handle both 
first-time init, or instead update when the config settings have changed, 
utilize `INSERT... ON DUPLICATE KEY UPDATE`.  see: 
https://stackoverflow.com/a/1361368
   
   *EVERY PARTICIPANT* would then "upsert" when it starts up, where the most 
recent value wins, clobbering the former.  even so, due to all participants 
having uniform config, in general, no actual overwriting happens with most 
updates.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to