From 4850b3aa86970f7af8f528564f2573becbd8e434 Mon Sep 17 00:00:00 2001 From: Bharat Viswanadham Date: Thu, 10 Oct 2019 10:58:19 -0700 Subject: [PATCH] HDDS-2269. Provide config for fair/non-fair for OM RW Lock. (#1623) --- .../apache/hadoop/ozone/OzoneConfigKeys.java | 3 +++ .../apache/hadoop/ozone/lock/ActiveLock.java | 11 +++++++---- .../apache/hadoop/ozone/lock/LockManager.java | 19 ++++++++++++++++--- .../hadoop/ozone/lock/PooledLockFactory.java | 7 ++++++- .../src/main/resources/ozone-default.xml | 11 +++++++++++ .../ozone/om/lock/OzoneManagerLock.java | 7 ++++++- 6 files changed, 49 insertions(+), 9 deletions(-) diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConfigKeys.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConfigKeys.java index a3d1c4ab28..3f7d0b915d 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConfigKeys.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConfigKeys.java @@ -453,6 +453,9 @@ public final class OzoneConfigKeys { "ozone.network.topology.aware.read"; public static final boolean OZONE_NETWORK_TOPOLOGY_AWARE_READ_DEFAULT = false; + public static final String OZONE_MANAGER_FAIR_LOCK = "ozone.om.lock.fair"; + public static final boolean OZONE_MANAGER_FAIR_LOCK_DEFAULT = false; + /** * There is no need to instantiate this class. */ diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/lock/ActiveLock.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/lock/ActiveLock.java index 49efad05fe..95dfd6c393 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/lock/ActiveLock.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/lock/ActiveLock.java @@ -31,9 +31,12 @@ public final class ActiveLock { /** * Use ActiveLock#newInstance to create instance. + * + * @param fairness - if true the lock uses a fair ordering policy, else + * non-fair ordering. */ - private ActiveLock() { - this.lock = new ReentrantReadWriteLock(); + private ActiveLock(boolean fairness) { + this.lock = new ReentrantReadWriteLock(fairness); this.count = new AtomicInteger(0); } @@ -42,8 +45,8 @@ private ActiveLock() { * * @return new ActiveLock */ - public static ActiveLock newInstance() { - return new ActiveLock(); + public static ActiveLock newInstance(boolean fairness) { + return new ActiveLock(fairness); } /** diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/lock/LockManager.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/lock/LockManager.java index 670d4d1637..3c2b5d4a39 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/lock/LockManager.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/lock/LockManager.java @@ -37,18 +37,31 @@ public class LockManager { private static final Logger LOG = LoggerFactory.getLogger(LockManager.class); private final Map activeLocks = new ConcurrentHashMap<>(); - private final GenericObjectPool lockPool = - new GenericObjectPool<>(new PooledLockFactory()); + private final GenericObjectPool lockPool; + + /** + * Creates new LockManager instance with the given Configuration.and uses + * non-fair mode for locks. + * + * @param conf Configuration object + */ + public LockManager(final Configuration conf) { + this(conf, false); + } + /** * Creates new LockManager instance with the given Configuration. * * @param conf Configuration object + * @param fair - true to use fair lock ordering, else non-fair lock ordering. */ - public LockManager(final Configuration conf) { + public LockManager(final Configuration conf, boolean fair) { final int maxPoolSize = conf.getInt( HddsConfigKeys.HDDS_LOCK_MAX_CONCURRENCY, HddsConfigKeys.HDDS_LOCK_MAX_CONCURRENCY_DEFAULT); + lockPool = + new GenericObjectPool<>(new PooledLockFactory(fair)); lockPool.setMaxTotal(maxPoolSize); } diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/lock/PooledLockFactory.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/lock/PooledLockFactory.java index 4c24ef74b2..1e3ba05a3a 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/lock/PooledLockFactory.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/lock/PooledLockFactory.java @@ -26,9 +26,14 @@ */ public class PooledLockFactory extends BasePooledObjectFactory { + private boolean fairness; + + PooledLockFactory(boolean fair) { + this.fairness = fair; + } @Override public ActiveLock create() throws Exception { - return ActiveLock.newInstance(); + return ActiveLock.newInstance(fairness); } @Override diff --git a/hadoop-hdds/common/src/main/resources/ozone-default.xml b/hadoop-hdds/common/src/main/resources/ozone-default.xml index 31bc65240d..b0a59fa209 100644 --- a/hadoop-hdds/common/src/main/resources/ozone-default.xml +++ b/hadoop-hdds/common/src/main/resources/ozone-default.xml @@ -1529,6 +1529,17 @@ + + ozone.om.lock.fair + false + If this is true, the Ozone Manager lock will be used in Fair + mode, which will schedule threads in the order received/queued. If this is + false, uses non-fair ordering. See + java.util.concurrent.locks.ReentrantReadWriteLock + for more information on fair/non-fair locks. + + + ozone.om.ratis.enable false diff --git a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java index a97a26c90b..31f0924462 100644 --- a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java +++ b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java @@ -29,6 +29,9 @@ import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.ozone.lock.LockManager; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_MANAGER_FAIR_LOCK_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_MANAGER_FAIR_LOCK; + /** * Provides different locks to handle concurrency in OzoneMaster. * We also maintain lock hierarchy, based on the weight. @@ -89,7 +92,9 @@ public class OzoneManagerLock { * @param conf Configuration object */ public OzoneManagerLock(Configuration conf) { - manager = new LockManager<>(conf); + boolean fair = conf.getBoolean(OZONE_MANAGER_FAIR_LOCK, + OZONE_MANAGER_FAIR_LOCK_DEFAULT); + manager = new LockManager<>(conf, fair); } /**