This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
NettyMapOutputHttpServer.java
143 lines (132 loc) · 5.42 KB
/
NettyMapOutputHttpServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* 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.hadoop.http;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelException;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
/**
* Create a Netty server to only answer map output http requests. HttpServer is
* used to handle all other requests.
*/
public class NettyMapOutputHttpServer {
/** Maximum thread pool size key */
public static final String MAXIMUM_THREAD_POOL_SIZE =
"mapred.task.tracker.netty.maxThreadPoolSize";
/** Default maximum thread pool size (same as jetty) */
public static final int DEFAULT_MAXIMUM_THREAD_POOL_SIZE = 254;
/** Class logger */
private static final Log LOG =
LogFactory.getLog(NettyMapOutputHttpServer.class);
private ChannelFactory channelFactory;
/** Port that this server runs on */
private int port;
/** Accepted channels */
private final ChannelGroup accepted = new DefaultChannelGroup();
/** Maximum bind attempts */
private final int DEFAULT_BIND_ATTEMPT_MAX = 50;
/** Worker thread pool (if implemented as a ThreadPoolExecutor) */
private ThreadPoolExecutor workerThreadPool = null;
/**
* Create a status server on the given port.
* The jsp scripts are taken from src/webapps/<name>.
* @param name The name of the server
*
* @param startingPort The port to use on the server (or start
* at if binding fails). If 0, use an let the server pick the port.
*/
public NettyMapOutputHttpServer(int startingPort) throws IOException {
this.port = startingPort;
}
public synchronized void init(Configuration conf) {
ThreadFactory bossFactory = new ThreadFactoryBuilder()
.setNameFormat("ShuffleHandler Netty Boss #%d")
.build();
ThreadFactory workerFactory = new ThreadFactoryBuilder()
.setNameFormat("ShuffleHandler Netty Worker #%d")
.build();
int maximumPoolSize = conf.getInt(MAXIMUM_THREAD_POOL_SIZE,
DEFAULT_MAXIMUM_THREAD_POOL_SIZE);
try {
workerThreadPool =
(ThreadPoolExecutor) Executors.newCachedThreadPool(workerFactory);
workerThreadPool.setMaximumPoolSize(maximumPoolSize);
} catch (ClassCastException e) {
LOG.warn("Netty worker thread pool is not of type ThreadPoolExecutor", e);
}
LOG.info("Netty starting up with a maximum of " + maximumPoolSize +
" worker threads");
channelFactory = new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(bossFactory),
workerThreadPool, maximumPoolSize);
}
public synchronized int start(ChannelPipelineFactory pipelineFactory) {
ServerBootstrap bootstrap = new ServerBootstrap(channelFactory);
bootstrap.setPipelineFactory(pipelineFactory);
// Try to bind to a port. If the port is 0, netty will select a port.
int bindAttempt = 0;
while (bindAttempt < DEFAULT_BIND_ATTEMPT_MAX) {
try {
InetSocketAddress address = new InetSocketAddress(port);
Channel ch = bootstrap.bind(address);
accepted.add(ch);
port = ((InetSocketAddress) ch.getLocalAddress()).getPort();
break;
} catch (ChannelException e) {
LOG.warn("start: Likely failed to bind on attempt " +
bindAttempt + " to port " + port, e);
// Only increment the port number when set by the user
if (port != 0) {
++port;
}
++bindAttempt;
}
}
LOG.info(this.getClass() + " is listening on port " + port);
return port;
}
public synchronized void stop() {
accepted.close().awaitUninterruptibly(10, TimeUnit.SECONDS);
ServerBootstrap bootstrap = new ServerBootstrap(channelFactory);
bootstrap.releaseExternalResources();
}
/**
* Get the worker thread pool for metrics.
*
* @return Worker thread pool or null if the thread pool is not a
* ThreadPoolExcecutor.
*/
public ThreadPoolExecutor getWorkerThreadPool() {
return workerThreadPool;
}
}