Replace synchronized with ConcurrentHashMap in Http2StreamChannelBootstrap (#9848)

Motivation:

97361fa2c8 replace synchronized with ConcurrentHashMap in *Bootstrap classes but missed to do the same for the Http2 variant.

Modifications:

- Use ConcurrentHashMap
- Simplify code in *Bootstrap classes

Result:

Less contention
This commit is contained in:
Norman Maurer 2019-12-06 10:59:55 +01:00
parent 4a5712f8d9
commit 254732b43c
4 changed files with 37 additions and 45 deletions

View File

@ -34,15 +34,19 @@ import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.nio.channels.ClosedChannelException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@UnstableApi
public final class Http2StreamChannelBootstrap {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(Http2StreamChannelBootstrap.class);
@SuppressWarnings("unchecked")
private static final Map.Entry<ChannelOption<?>, Object>[] EMPTY_OPTION_ARRAY = new Map.Entry[0];
@SuppressWarnings("unchecked")
private static final Map.Entry<AttributeKey<?>, Object>[] EMPTY_ATTRIBUTE_ARRAY = new Map.Entry[0];
private final Map<ChannelOption<?>, Object> options = new LinkedHashMap<>();
private final Map<AttributeKey<?>, Object> attrs = new LinkedHashMap<>();
private final Map<ChannelOption<?>, Object> options = new ConcurrentHashMap<>();
private final Map<AttributeKey<?>, Object> attrs = new ConcurrentHashMap<>();
private final Channel channel;
private volatile ChannelHandler handler;
@ -61,13 +65,9 @@ public final class Http2StreamChannelBootstrap {
public <T> Http2StreamChannelBootstrap option(ChannelOption<T> option, T value) {
requireNonNull(option, "option");
if (value == null) {
synchronized (options) {
options.remove(option);
}
options.remove(option);
} else {
synchronized (options) {
options.put(option, value);
}
options.put(option, value);
}
return this;
}
@ -80,13 +80,9 @@ public final class Http2StreamChannelBootstrap {
public <T> Http2StreamChannelBootstrap attr(AttributeKey<T> key, T value) {
requireNonNull(key, "key");
if (value == null) {
synchronized (attrs) {
attrs.remove(key);
}
attrs.remove(key);
} else {
synchronized (attrs) {
attrs.put(key, value);
}
attrs.put(key, value);
}
return this;
}
@ -193,36 +189,29 @@ public final class Http2StreamChannelBootstrap {
});
}
@SuppressWarnings("unchecked")
private void init(Channel channel) {
ChannelPipeline p = channel.pipeline();
ChannelHandler handler = this.handler;
if (handler != null) {
p.addLast(handler);
}
synchronized (options) {
setChannelOptions(channel, options);
}
synchronized (attrs) {
for (Map.Entry<AttributeKey<?>, Object> e: attrs.entrySet()) {
channel.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());
}
}
setChannelOptions(channel, options.entrySet().toArray(EMPTY_OPTION_ARRAY));
setAttributes(channel, attrs.entrySet().toArray(EMPTY_ATTRIBUTE_ARRAY));
}
private static void setChannelOptions(
Channel channel, Map<ChannelOption<?>, Object> options) {
for (Map.Entry<ChannelOption<?>, Object> e: options.entrySet()) {
Channel channel, Map.Entry<ChannelOption<?>, Object>[] options) {
for (Map.Entry<ChannelOption<?>, Object> e: options) {
setChannelOption(channel, e.getKey(), e.getValue());
}
}
@SuppressWarnings("unchecked")
private static void setChannelOption(
Channel channel, ChannelOption<?> option, Object value) {
try {
if (!channel.config().setOption((ChannelOption<Object>) option, value)) {
@SuppressWarnings("unchecked")
ChannelOption<Object> opt = (ChannelOption<Object>) option;
if (!channel.config().setOption(opt, value)) {
logger.warn("Unknown channel option '{}' for channel '{}'", option, channel);
}
} catch (Throwable t) {
@ -230,4 +219,13 @@ public final class Http2StreamChannelBootstrap {
"Failed to set channel option '{}' with value '{}' for channel '{}'", option, value, channel, t);
}
}
private static void setAttributes(
Channel channel, Map.Entry<AttributeKey<?>, Object>[] options) {
for (Map.Entry<AttributeKey<?>, Object> e: options) {
@SuppressWarnings("unchecked")
AttributeKey<Object> key = (AttributeKey<Object>) e.getKey();
channel.attr(key).set(e.getValue());
}
}
}

View File

@ -48,6 +48,10 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C, F>, C extends Channel, F>
implements Cloneable {
@SuppressWarnings("unchecked")
static final Map.Entry<ChannelOption<?>, Object>[] EMPTY_OPTION_ARRAY = new Map.Entry[0];
@SuppressWarnings("unchecked")
static final Map.Entry<AttributeKey<?>, Object>[] EMPTY_ATTRIBUTE_ARRAY = new Map.Entry[0];
volatile EventLoopGroup group;
private volatile SocketAddress localAddress;
@ -353,16 +357,6 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C, F>, C
}
}
@SuppressWarnings("unchecked")
static Map.Entry<AttributeKey<?>, Object>[] newAttrArray(int size) {
return new Map.Entry[size];
}
@SuppressWarnings("unchecked")
static Map.Entry<ChannelOption<?>, Object>[] newOptionArray(int size) {
return new Map.Entry[size];
}
@SuppressWarnings("unchecked")
private static void setChannelOption(
Channel channel, ChannelOption<?> option, Object value, InternalLogger logger) {

View File

@ -277,8 +277,8 @@ public class Bootstrap extends AbstractBootstrap<Bootstrap, Channel, ChannelFact
ChannelPipeline p = channel.pipeline();
p.addLast(config.handler());
setChannelOptions(channel, options0().entrySet().toArray(newOptionArray(0)), logger);
setAttributes(channel, attrs0().entrySet().toArray(newAttrArray(0)));
setChannelOptions(channel, options0().entrySet().toArray(EMPTY_OPTION_ARRAY), logger);
setAttributes(channel, attrs0().entrySet().toArray(EMPTY_ATTRIBUTE_ARRAY));
return promise.setSuccess();
}

View File

@ -160,15 +160,15 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerCh
@Override
ChannelFuture init(Channel channel) {
final ChannelPromise promise = channel.newPromise();
setChannelOptions(channel, options0().entrySet().toArray(newOptionArray(0)), logger);
setAttributes(channel, attrs0().entrySet().toArray(newAttrArray(0)));
setChannelOptions(channel, options0().entrySet().toArray(EMPTY_OPTION_ARRAY), logger);
setAttributes(channel, attrs0().entrySet().toArray(EMPTY_ATTRIBUTE_ARRAY));
ChannelPipeline p = channel.pipeline();
final ChannelHandler currentChildHandler = childHandler;
final Entry<ChannelOption<?>, Object>[] currentChildOptions =
childOptions.entrySet().toArray(newOptionArray(0));
final Entry<AttributeKey<?>, Object>[] currentChildAttrs = childAttrs.entrySet().toArray(newAttrArray(0));
childOptions.entrySet().toArray(EMPTY_OPTION_ARRAY);
final Entry<AttributeKey<?>, Object>[] currentChildAttrs = childAttrs.entrySet().toArray(EMPTY_ATTRIBUTE_ARRAY);
p.addLast(new ChannelInitializer<Channel>() {
@Override