HDFS-9398. Make ByteArraryManager log message in one-line format. Contributed by Mingliang Liu

This commit is contained in:
Tsz-Wo Nicholas Sze 2015-11-07 22:21:28 +08:00
parent 1e0746e756
commit 9766fc988f
2 changed files with 54 additions and 12 deletions

View File

@ -36,6 +36,18 @@ import org.slf4j.LoggerFactory;
@InterfaceAudience.Private
public abstract class ByteArrayManager {
static final Logger LOG = LoggerFactory.getLogger(ByteArrayManager.class);
private static final ThreadLocal<StringBuilder> DEBUG_MESSAGE =
new ThreadLocal<StringBuilder>() {
protected StringBuilder initialValue() {
return new StringBuilder();
}
};
private static void logDebugMessage() {
final StringBuilder b = DEBUG_MESSAGE.get();
LOG.debug(b.toString());
b.setLength(0);
}
static final int MIN_ARRAY_LENGTH = 32;
static final byte[] EMPTY_BYTE_ARRAY = {};
@ -148,18 +160,27 @@ public abstract class ByteArrayManager {
* via the {@link FixedLengthManager#recycle(byte[])} method.
*/
synchronized byte[] allocate() throws InterruptedException {
LOG.debug(", {}", this);
if (LOG.isDebugEnabled()) {
DEBUG_MESSAGE.get().append(", ").append(this);
}
for(; numAllocated >= maxAllocated;) {
LOG.debug(": wait ...");
if (LOG.isDebugEnabled()) {
DEBUG_MESSAGE.get().append(": wait ...");
logDebugMessage();
}
wait();
LOG.debug("wake up: {}", this);
if (LOG.isDebugEnabled()) {
DEBUG_MESSAGE.get().append("wake up: ").append(this);
}
}
numAllocated++;
final byte[] array = freeQueue.poll();
LOG.debug(", recycled? {}", array != null);
if (LOG.isDebugEnabled()) {
DEBUG_MESSAGE.get().append(", recycled? ").append(array != null);
}
return array != null? array : new byte[byteArrayLength];
}
@ -173,7 +194,9 @@ public abstract class ByteArrayManager {
synchronized int recycle(byte[] array) {
Preconditions.checkNotNull(array);
Preconditions.checkArgument(array.length == byteArrayLength);
LOG.debug(", {}", this);
if (LOG.isDebugEnabled()) {
DEBUG_MESSAGE.get().append(", ").append(this);
}
notify();
numAllocated--;
@ -184,7 +207,9 @@ public abstract class ByteArrayManager {
}
if (freeQueue.size() < maxAllocated - numAllocated) {
LOG.debug(", freeQueue.offer");
if (LOG.isDebugEnabled()) {
DEBUG_MESSAGE.get().append(", freeQueue.offer");
}
freeQueue.offer(array);
}
return freeQueue.size();
@ -324,7 +349,9 @@ public abstract class ByteArrayManager {
public byte[] newByteArray(final int arrayLength)
throws InterruptedException {
Preconditions.checkArgument(arrayLength >= 0);
LOG.debug("allocate({})", arrayLength);
if (LOG.isDebugEnabled()) {
DEBUG_MESSAGE.get().append("allocate(").append(arrayLength).append(")");
}
final byte[] array;
if (arrayLength == 0) {
@ -338,12 +365,18 @@ public abstract class ByteArrayManager {
final FixedLengthManager manager =
managers.get(powerOfTwo, aboveThreshold);
LOG.debug(": count={}, {}Threshold", count,
aboveThreshold ? "above" : "below");
if (LOG.isDebugEnabled()) {
DEBUG_MESSAGE.get().append(": count=").append(count)
.append(aboveThreshold? ", aboveThreshold": ", belowThreshold");
}
array = manager != null? manager.allocate(): new byte[powerOfTwo];
}
LOG.debug(", return byte[{}]", array.length);
if (LOG.isDebugEnabled()) {
DEBUG_MESSAGE.get().append(", return byte[")
.append(array.length).append("]");
logDebugMessage();
}
return array;
}
@ -358,7 +391,10 @@ public abstract class ByteArrayManager {
@Override
public int release(final byte[] array) {
Preconditions.checkNotNull(array);
LOG.debug("recycle: array.length={}", array.length);
if (LOG.isDebugEnabled()) {
DEBUG_MESSAGE.get()
.append("recycle: array.length=").append(array.length);
}
final int freeQueueSize;
if (array.length == 0) {
@ -368,7 +404,10 @@ public abstract class ByteArrayManager {
freeQueueSize = manager == null? -1: manager.recycle(array);
}
LOG.debug(", freeQueueSize={}", freeQueueSize);
if (LOG.isDebugEnabled()) {
DEBUG_MESSAGE.get().append(", freeQueueSize=").append(freeQueueSize);
logDebugMessage();
}
return freeQueueSize;
}

View File

@ -788,6 +788,9 @@ Release 2.8.0 - UNRELEASED
HDFS-9379. Make NNThroughputBenchmark$BlockReportStats support more than 10
datanodes. (Mingliang Liu via Arpit Agarwal)
HDFS-9398. Make ByteArraryManager log message in one-line format.
(Mingliang Liu via szetszwo)
OPTIMIZATIONS
HDFS-8026. Trace FSOutputSummer#writeChecksumChunks rather than