From 9a5f9a2e295efbdcb143d03ceda320ae484534af Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 24 Oct 2019 10:17:07 -0400 Subject: Handle DB error codes in distributed locking The commons library wraps the SQLExceptions within its own SQLException, so changed the code to simply look for a cause that's SQLTransientException, eliminating the need to check specific error codes. Deleted the error code property now that it is no longer needed. Also updated the distributed locking properties to include examples. Issue-ID: POLICY-2113 Signed-off-by: Jim Hahn Change-Id: If46e85a81cfc952e561174fea670df81efb8309a --- .../config/feature-distributed-locking.properties | 7 +++- .../locking/DistributedLockManager.java | 12 ++++--- .../locking/DistributedLockProperties.java | 41 ---------------------- 3 files changed, 13 insertions(+), 47 deletions(-) (limited to 'feature-distributed-locking/src/main') diff --git a/feature-distributed-locking/src/main/feature/config/feature-distributed-locking.properties b/feature-distributed-locking/src/main/feature/config/feature-distributed-locking.properties index c609c8f9..9acea3c0 100644 --- a/feature-distributed-locking/src/main/feature/config/feature-distributed-locking.properties +++ b/feature-distributed-locking/src/main/feature/config/feature-distributed-locking.properties @@ -19,7 +19,12 @@ ### #Database properties -javax.persistence.jdbc.driver= org.mariadb.jdbc.Driver +javax.persistence.jdbc.driver=org.mariadb.jdbc.Driver javax.persistence.jdbc.url=jdbc:mariadb://${env:SQL_HOST}:3306/pooling javax.persistence.jdbc.user=${env:SQL_USER} javax.persistence.jdbc.password=${env:SQL_PASSWORD} + +# default property values are commented out +#distributed.locking.expire.check.seconds=900 +#distributed.locking.retry.seconds=60 +#distributed.locking.max.retries=2 diff --git a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java index 523c0d93..7ee786b9 100644 --- a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java +++ b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java @@ -24,6 +24,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.SQLTransientException; import java.util.HashSet; import java.util.Map; import java.util.Properties; @@ -77,8 +78,7 @@ import org.slf4j.LoggerFactory; * instance. * */ -public class DistributedLockManager - implements PolicyResourceLockManager, PolicyEngineFeatureApi { +public class DistributedLockManager implements PolicyResourceLockManager, PolicyEngineFeatureApi { private static final Logger logger = LoggerFactory.getLogger(DistributedLockManager.class); @@ -396,6 +396,8 @@ public class DistributedLockManager * Distributed Lock implementation. */ public static class DistributedLock extends LockImpl { + private static final String SQL_FAILED_MSG = "request failed for lock: {}"; + private static final long serialVersionUID = 1L; /** @@ -702,9 +704,9 @@ public class DistributedLockManager req.run(); } catch (SQLException e) { - logger.warn("request failed for lock: {}", this, e); + logger.warn(SQL_FAILED_MSG, this, e); - if (feature.featProps.isTransient(e.getErrorCode())) { + if (e.getCause() instanceof SQLTransientException) { // retry the request a little later rescheduleRequest(req); } else { @@ -712,7 +714,7 @@ public class DistributedLockManager } } catch (RuntimeException e) { - logger.warn("request failed for lock: {}", this, e); + logger.warn(SQL_FAILED_MSG, this, e); removeFromMap(); } } diff --git a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockProperties.java b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockProperties.java index f470c8e2..fff19447 100644 --- a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockProperties.java +++ b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockProperties.java @@ -20,10 +20,7 @@ package org.onap.policy.distributed.locking; -import java.util.Collections; -import java.util.HashSet; import java.util.Properties; -import java.util.Set; import lombok.Getter; import lombok.Setter; import org.onap.policy.common.utils.properties.BeanConfigurator; @@ -40,7 +37,6 @@ public class DistributedLockProperties { public static final String DB_URL = "javax.persistence.jdbc.url"; public static final String DB_USER = "javax.persistence.jdbc.user"; public static final String DB_PASS = "javax.persistence.jdbc.password"; - public static final String TRANSIENT_ERROR_CODES = PREFIX + "transient.error.codes"; public static final String EXPIRE_CHECK_SEC = PREFIX + "expire.check.seconds"; public static final String RETRY_SEC = PREFIX + "retry.seconds"; public static final String MAX_RETRIES = PREFIX + "max.retries"; @@ -69,16 +65,6 @@ public class DistributedLockProperties { @Property(name = DB_PASS) private String dbPwd; - /** - * Vendor-specific error codes that are "transient", meaning they may go away if the - * command is repeated (e.g., connection issue), as opposed to something like a syntax - * error or a duplicate key. - */ - @Property(name = TRANSIENT_ERROR_CODES) - private String errorCodeStrings; - - private final Set transientErrorCodes; - /** * Time, in seconds, to wait between checks for expired locks. */ @@ -105,32 +91,5 @@ public class DistributedLockProperties { */ public DistributedLockProperties(Properties props) throws PropertyException { new BeanConfigurator().configureFromProperties(this, props); - - Set set = new HashSet<>(); - for (String text : errorCodeStrings.split(",")) { - text = text.trim(); - if (text.isEmpty()) { - continue; - } - - try { - set.add(Integer.valueOf(text)); - - } catch (NumberFormatException e) { - throw new PropertyException(TRANSIENT_ERROR_CODES, "errorCodeStrings", e); - } - } - - transientErrorCodes = Collections.unmodifiableSet(set); - } - - /** - * Determines if an error is transient. - * - * @param errorCode error code to check - * @return {@code true} if the error is transient, {@code false} otherwise - */ - public boolean isTransient(int errorCode) { - return transientErrorCodes.contains(errorCode); } } -- cgit 1.2.3-korg