Skip to content
Closed
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 @@ -44,6 +44,7 @@
import org.apache.spark.network.server.TransportServerBootstrap;
import org.apache.spark.network.util.IOMode;
import org.apache.spark.network.util.NettyUtils;
import org.apache.spark.network.util.NettyLogger;
import org.apache.spark.network.util.TransportConf;
import org.apache.spark.network.util.TransportFrameDecoder;

Expand All @@ -64,6 +65,7 @@
public class TransportContext implements Closeable {
private static final Logger logger = LoggerFactory.getLogger(TransportContext.class);

private static final NettyLogger nettyLogger = new NettyLogger();
private final TransportConf conf;
private final RpcHandler rpcHandler;
private final boolean closeIdleConnections;
Expand Down Expand Up @@ -187,7 +189,11 @@ public TransportChannelHandler initializePipeline(
RpcHandler channelRpcHandler) {
try {
TransportChannelHandler channelHandler = createChannelHandler(channel, channelRpcHandler);
ChannelPipeline pipeline = channel.pipeline()
ChannelPipeline pipeline = channel.pipeline();
if (nettyLogger.getLoggingHandler() != null) {
pipeline.addLast("loggingHandler", nettyLogger.getLoggingHandler());
}
pipeline
.addLast("encoder", ENCODER)
.addLast(TransportFrameDecoder.HANDLER_NAME, NettyUtils.createFrameDecoder())
.addLast("decoder", DECODER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,10 @@ public boolean release(int decrement) {
}
return super.release(decrement);
}

@Override
public String toString() {
return "MessageWithHeader [headerLength: " + headerLength + ", bodyLength: " + bodyLength + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.network.util;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.logging.LogLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NettyLogger {
private static final Logger logger = LoggerFactory.getLogger(NettyLogger.class);

/** A Netty LoggingHandler which does not dump the message contents. */
private static class NoContentLoggingHandler extends LoggingHandler {

NoContentLoggingHandler(Class<?> clazz, LogLevel level) {
super(clazz, level);
}

protected String format(ChannelHandlerContext ctx, String eventName, Object arg) {
if (arg instanceof ByteBuf) {
return format(ctx, eventName) + " " + ((ByteBuf) arg).readableBytes() + "B";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: Add a space between value and "B" (here and below) ?

Copy link
Contributor Author

@attilapiros attilapiros Sep 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (no space) is consistent with Netty's own logging.
You can see it in the TRACE output in the PR description.
But I copy it for you (the last line is the proof):

         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 00 00 00 00 00 00 00 42 06 00 00 00 35 2f 6a 61 |.......B....5/ja|
|00000010| 72 73 2f 6f 72 69 67 69 6e 61 6c 2d 73 70 61 72 |rs/original-spar|
|00000020| 6b 2d 65 78 61 6d 70 6c 65 73 5f 32 2e 31 32 2d |k-examples_2.12-|
|00000030| 33 2e 33 2e 30 2d 53 4e 41 50 53 48 4f 54 2e 6a |3.3.0-SNAPSHOT.j|
|00000040| 61 72                                           |ar              |
+--------+-------------------------------------------------+----------------+
21/09/10 15:29:14 TRACE NettyLogger: [id: 0xf1d25786, L:/172.30.64.219:61045 - R:/172.30.64.219:61044] FLUSH
21/09/10 15:29:14 TRACE NettyLogger: [id: 0x362fc693, L:/172.30.64.219:61044 - R:/172.30.64.219:61045] READ: 66B
   

} else if (arg instanceof ByteBufHolder) {
return format(ctx, eventName) + " " +
((ByteBufHolder) arg).content().readableBytes() + "B";
} else {
return super.format(ctx, eventName, arg);
}
}
}

private final LoggingHandler loggingHandler;

public NettyLogger() {
if (logger.isTraceEnabled()) {
loggingHandler = new LoggingHandler(NettyLogger.class, LogLevel.TRACE);
} else if (logger.isDebugEnabled()) {
loggingHandler = new NoContentLoggingHandler(NettyLogger.class, LogLevel.DEBUG);
} else {
loggingHandler = null;
}
}

public LoggingHandler getLoggingHandler() {
return loggingHandler;
}
}