Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* 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, 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions hadoop-hdds/common/src/main/resources/ozone-default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down