HDDS-2269. Provide config for fair/non-fair for OM RW Lock. (#1623)

This commit is contained in:
Bharat Viswanadham 2019-10-10 10:58:19 -07:00 committed by Nanda kumar
parent effe6087a5
commit 4850b3aa86
6 changed files with 49 additions and 9 deletions

View File

@ -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.
*/

View File

@ -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);
}
/**

View File

@ -37,18 +37,31 @@ public class LockManager<R> {
private static final Logger LOG = LoggerFactory.getLogger(LockManager.class);
private final Map<R, ActiveLock> activeLocks = new ConcurrentHashMap<>();
private final GenericObjectPool<ActiveLock> lockPool =
new GenericObjectPool<>(new PooledLockFactory());
private final GenericObjectPool<ActiveLock> 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);
}

View File

@ -26,9 +26,14 @@
*/
public class PooledLockFactory extends BasePooledObjectFactory<ActiveLock> {
private boolean fairness;
PooledLockFactory(boolean fair) {
this.fairness = fair;
}
@Override
public ActiveLock create() throws Exception {
return ActiveLock.newInstance();
return ActiveLock.newInstance(fairness);
}
@Override

View File

@ -1529,6 +1529,17 @@
</description>
</property>
<property>
<name>ozone.om.lock.fair</name>
<value>false</value>
<description>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.
</description>
</property>
<property>
<name>ozone.om.ratis.enable</name>
<value>false</value>

View File

@ -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);
}
/**