Revert HDFS-6788, bad merge.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1615239 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew Wang 2014-08-01 21:45:38 +00:00
parent 511234c828
commit 2777f777e9
2 changed files with 113 additions and 189 deletions

View File

@ -97,9 +97,6 @@ Release 2.6.0 - UNRELEASED
HDFS-6802. Some tests in TestDFSClientFailover are missing @Test HDFS-6802. Some tests in TestDFSClientFailover are missing @Test
annotation. (Akira Ajisaka via wang) annotation. (Akira Ajisaka via wang)
HDFS-6788. Improve synchronization in BPOfferService with read write lock.
(Yongjun Zhang via wang)
OPTIMIZATIONS OPTIMIZATIONS
HDFS-6690. Deduplicate xattr names in memory. (wang) HDFS-6690. Deduplicate xattr names in memory. (wang)

View File

@ -21,7 +21,6 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.ha.HAServiceProtocol.HAServiceState; import org.apache.hadoop.ha.HAServiceProtocol.HAServiceState;
@ -39,8 +38,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/** /**
* One instance per block-pool/namespace on the DN, which handles the * One instance per block-pool/namespace on the DN, which handles the
@ -94,28 +91,6 @@ class BPOfferService {
*/ */
private long lastActiveClaimTxId = -1; private long lastActiveClaimTxId = -1;
private final ReentrantReadWriteLock mReadWriteLock =
new ReentrantReadWriteLock();
private final Lock mReadLock = mReadWriteLock.readLock();
private final Lock mWriteLock = mReadWriteLock.writeLock();
// utility methods to acquire and release read lock and write lock
void readLock() {
mReadLock.lock();
}
void readUnlock() {
mReadLock.unlock();
}
void writeLock() {
mWriteLock.lock();
}
void writeUnlock() {
mWriteLock.unlock();
}
BPOfferService(List<InetSocketAddress> nnAddrs, DataNode dn) { BPOfferService(List<InetSocketAddress> nnAddrs, DataNode dn) {
Preconditions.checkArgument(!nnAddrs.isEmpty(), Preconditions.checkArgument(!nnAddrs.isEmpty(),
"Must pass at least one NN."); "Must pass at least one NN.");
@ -160,19 +135,14 @@ class BPOfferService {
} }
return false; return false;
} }
String getBlockPoolId() { synchronized String getBlockPoolId() {
readLock(); if (bpNSInfo != null) {
try { return bpNSInfo.getBlockPoolID();
if (bpNSInfo != null) { } else {
return bpNSInfo.getBlockPoolID(); LOG.warn("Block pool ID needed, but service not yet registered with NN",
} else { new Exception("trace"));
LOG.warn("Block pool ID needed, but service not yet registered with NN", return null;
new Exception("trace"));
return null;
}
} finally {
readUnlock();
} }
} }
@ -180,37 +150,27 @@ class BPOfferService {
return getNamespaceInfo() != null; return getNamespaceInfo() != null;
} }
NamespaceInfo getNamespaceInfo() { synchronized NamespaceInfo getNamespaceInfo() {
readLock(); return bpNSInfo;
try {
return bpNSInfo;
} finally {
readUnlock();
}
} }
@Override @Override
public String toString() { public synchronized String toString() {
readLock(); if (bpNSInfo == null) {
try { // If we haven't yet connected to our NN, we don't yet know our
if (bpNSInfo == null) { // own block pool ID.
// If we haven't yet connected to our NN, we don't yet know our // If _none_ of the block pools have connected yet, we don't even
// own block pool ID. // know the DatanodeID ID of this DN.
// If _none_ of the block pools have connected yet, we don't even String datanodeUuid = dn.getDatanodeUuid();
// know the DatanodeID ID of this DN.
String datanodeUuid = dn.getDatanodeUuid();
if (datanodeUuid == null || datanodeUuid.isEmpty()) { if (datanodeUuid == null || datanodeUuid.isEmpty()) {
datanodeUuid = "unassigned"; datanodeUuid = "unassigned";
}
return "Block pool <registering> (Datanode Uuid " + datanodeUuid + ")";
} else {
return "Block pool " + getBlockPoolId() +
" (Datanode Uuid " + dn.getDatanodeUuid() +
")";
} }
} finally { return "Block pool <registering> (Datanode Uuid " + datanodeUuid + ")";
readUnlock(); } else {
return "Block pool " + getBlockPoolId() +
" (Datanode Uuid " + dn.getDatanodeUuid() +
")";
} }
} }
@ -306,37 +266,32 @@ class BPOfferService {
* verifies that this namespace matches (eg to prevent a misconfiguration * verifies that this namespace matches (eg to prevent a misconfiguration
* where a StandbyNode from a different cluster is specified) * where a StandbyNode from a different cluster is specified)
*/ */
void verifyAndSetNamespaceInfo(NamespaceInfo nsInfo) throws IOException { synchronized void verifyAndSetNamespaceInfo(NamespaceInfo nsInfo) throws IOException {
writeLock(); if (this.bpNSInfo == null) {
try { this.bpNSInfo = nsInfo;
if (this.bpNSInfo == null) { boolean success = false;
this.bpNSInfo = nsInfo;
boolean success = false;
// Now that we know the namespace ID, etc, we can pass this to the DN. // Now that we know the namespace ID, etc, we can pass this to the DN.
// The DN can now initialize its local storage if we are the // The DN can now initialize its local storage if we are the
// first BP to handshake, etc. // first BP to handshake, etc.
try { try {
dn.initBlockPool(this); dn.initBlockPool(this);
success = true; success = true;
} finally { } finally {
if (!success) { if (!success) {
// The datanode failed to initialize the BP. We need to reset // The datanode failed to initialize the BP. We need to reset
// the namespace info so that other BPService actors still have // the namespace info so that other BPService actors still have
// a chance to set it, and re-initialize the datanode. // a chance to set it, and re-initialize the datanode.
this.bpNSInfo = null; this.bpNSInfo = null;
}
} }
} else {
checkNSEquality(bpNSInfo.getBlockPoolID(), nsInfo.getBlockPoolID(),
"Blockpool ID");
checkNSEquality(bpNSInfo.getNamespaceID(), nsInfo.getNamespaceID(),
"Namespace ID");
checkNSEquality(bpNSInfo.getClusterID(), nsInfo.getClusterID(),
"Cluster ID");
} }
} finally { } else {
writeUnlock(); checkNSEquality(bpNSInfo.getBlockPoolID(), nsInfo.getBlockPoolID(),
"Blockpool ID");
checkNSEquality(bpNSInfo.getNamespaceID(), nsInfo.getNamespaceID(),
"Namespace ID");
checkNSEquality(bpNSInfo.getClusterID(), nsInfo.getClusterID(),
"Cluster ID");
} }
} }
@ -345,27 +300,22 @@ class BPOfferService {
* NN, it calls this function to verify that the NN it connected to * NN, it calls this function to verify that the NN it connected to
* is consistent with other NNs serving the block-pool. * is consistent with other NNs serving the block-pool.
*/ */
void registrationSucceeded(BPServiceActor bpServiceActor, synchronized void registrationSucceeded(BPServiceActor bpServiceActor,
DatanodeRegistration reg) throws IOException { DatanodeRegistration reg) throws IOException {
writeLock(); if (bpRegistration != null) {
try { checkNSEquality(bpRegistration.getStorageInfo().getNamespaceID(),
if (bpRegistration != null) { reg.getStorageInfo().getNamespaceID(), "namespace ID");
checkNSEquality(bpRegistration.getStorageInfo().getNamespaceID(), checkNSEquality(bpRegistration.getStorageInfo().getClusterID(),
reg.getStorageInfo().getNamespaceID(), "namespace ID"); reg.getStorageInfo().getClusterID(), "cluster ID");
checkNSEquality(bpRegistration.getStorageInfo().getClusterID(), } else {
reg.getStorageInfo().getClusterID(), "cluster ID"); bpRegistration = reg;
} else { }
bpRegistration = reg;
} dn.bpRegistrationSucceeded(bpRegistration, getBlockPoolId());
// Add the initial block token secret keys to the DN's secret manager.
dn.bpRegistrationSucceeded(bpRegistration, getBlockPoolId()); if (dn.isBlockTokenEnabled) {
// Add the initial block token secret keys to the DN's secret manager. dn.blockPoolTokenSecretManager.addKeys(getBlockPoolId(),
if (dn.isBlockTokenEnabled) { reg.getExportedKeys());
dn.blockPoolTokenSecretManager.addKeys(getBlockPoolId(),
reg.getExportedKeys());
}
} finally {
writeUnlock();
} }
} }
@ -383,35 +333,25 @@ class BPOfferService {
} }
} }
DatanodeRegistration createRegistration() { synchronized DatanodeRegistration createRegistration() {
writeLock(); Preconditions.checkState(bpNSInfo != null,
try { "getRegistration() can only be called after initial handshake");
Preconditions.checkState(bpNSInfo != null, return dn.createBPRegistration(bpNSInfo);
"getRegistration() can only be called after initial handshake");
return dn.createBPRegistration(bpNSInfo);
} finally {
writeUnlock();
}
} }
/** /**
* Called when an actor shuts down. If this is the last actor * Called when an actor shuts down. If this is the last actor
* to shut down, shuts down the whole blockpool in the DN. * to shut down, shuts down the whole blockpool in the DN.
*/ */
void shutdownActor(BPServiceActor actor) { synchronized void shutdownActor(BPServiceActor actor) {
writeLock(); if (bpServiceToActive == actor) {
try { bpServiceToActive = null;
if (bpServiceToActive == actor) { }
bpServiceToActive = null;
}
bpServices.remove(actor); bpServices.remove(actor);
if (bpServices.isEmpty()) { if (bpServices.isEmpty()) {
dn.shutdownBlockPool(this); dn.shutdownBlockPool(this);
}
} finally {
writeUnlock();
} }
} }
@ -453,16 +393,11 @@ class BPOfferService {
* @return a proxy to the active NN, or null if the BPOS has not * @return a proxy to the active NN, or null if the BPOS has not
* acknowledged any NN as active yet. * acknowledged any NN as active yet.
*/ */
DatanodeProtocolClientSideTranslatorPB getActiveNN() { synchronized DatanodeProtocolClientSideTranslatorPB getActiveNN() {
readLock(); if (bpServiceToActive != null) {
try { return bpServiceToActive.bpNamenode;
if (bpServiceToActive != null) { } else {
return bpServiceToActive.bpNamenode; return null;
} else {
return null;
}
} finally {
readUnlock();
} }
} }
@ -490,50 +425,45 @@ class BPOfferService {
* @param actor the actor which received the heartbeat * @param actor the actor which received the heartbeat
* @param nnHaState the HA-related heartbeat contents * @param nnHaState the HA-related heartbeat contents
*/ */
void updateActorStatesFromHeartbeat( synchronized void updateActorStatesFromHeartbeat(
BPServiceActor actor, BPServiceActor actor,
NNHAStatusHeartbeat nnHaState) { NNHAStatusHeartbeat nnHaState) {
writeLock(); final long txid = nnHaState.getTxId();
try {
final long txid = nnHaState.getTxId(); final boolean nnClaimsActive =
nnHaState.getState() == HAServiceState.ACTIVE;
final boolean nnClaimsActive = final boolean bposThinksActive = bpServiceToActive == actor;
nnHaState.getState() == HAServiceState.ACTIVE; final boolean isMoreRecentClaim = txid > lastActiveClaimTxId;
final boolean bposThinksActive = bpServiceToActive == actor;
final boolean isMoreRecentClaim = txid > lastActiveClaimTxId; if (nnClaimsActive && !bposThinksActive) {
LOG.info("Namenode " + actor + " trying to claim ACTIVE state with " +
if (nnClaimsActive && !bposThinksActive) { "txid=" + txid);
LOG.info("Namenode " + actor + " trying to claim ACTIVE state with " + if (!isMoreRecentClaim) {
"txid=" + txid); // Split-brain scenario - an NN is trying to claim active
if (!isMoreRecentClaim) { // state when a different NN has already claimed it with a higher
// Split-brain scenario - an NN is trying to claim active // txid.
// state when a different NN has already claimed it with a higher LOG.warn("NN " + actor + " tried to claim ACTIVE state at txid=" +
// txid. txid + " but there was already a more recent claim at txid=" +
LOG.warn("NN " + actor + " tried to claim ACTIVE state at txid=" + lastActiveClaimTxId);
txid + " but there was already a more recent claim at txid=" + return;
lastActiveClaimTxId); } else {
return; if (bpServiceToActive == null) {
LOG.info("Acknowledging ACTIVE Namenode " + actor);
} else { } else {
if (bpServiceToActive == null) { LOG.info("Namenode " + actor + " taking over ACTIVE state from " +
LOG.info("Acknowledging ACTIVE Namenode " + actor); bpServiceToActive + " at higher txid=" + txid);
} else {
LOG.info("Namenode " + actor + " taking over ACTIVE state from " +
bpServiceToActive + " at higher txid=" + txid);
}
bpServiceToActive = actor;
} }
} else if (!nnClaimsActive && bposThinksActive) { bpServiceToActive = actor;
LOG.info("Namenode " + actor + " relinquishing ACTIVE state with " +
"txid=" + nnHaState.getTxId());
bpServiceToActive = null;
} }
} else if (!nnClaimsActive && bposThinksActive) {
if (bpServiceToActive == actor) { LOG.info("Namenode " + actor + " relinquishing ACTIVE state with " +
assert txid >= lastActiveClaimTxId; "txid=" + nnHaState.getTxId());
lastActiveClaimTxId = txid; bpServiceToActive = null;
} }
} finally {
writeUnlock(); if (bpServiceToActive == actor) {
assert txid >= lastActiveClaimTxId;
lastActiveClaimTxId = txid;
} }
} }
@ -604,14 +534,11 @@ class BPOfferService {
actor.reRegister(); actor.reRegister();
return true; return true;
} }
writeLock(); synchronized (this) {
try {
if (actor == bpServiceToActive) { if (actor == bpServiceToActive) {
return processCommandFromActive(cmd, actor); return processCommandFromActive(cmd, actor);
} else { } else {
return processCommandFromStandby(cmd, actor); return processCommandFromStandby(cmd, actor);
} finally {
writeUnlock();
} }
} }
} }