/
TachyonWorker.java
359 lines (312 loc) · 13 KB
/
TachyonWorker.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Licensed to the University of California, Berkeley 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 tachyon.worker;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Throwables;
import tachyon.Constants;
import tachyon.UnderFileSystem;
import tachyon.UnderFileSystemHdfs;
import tachyon.Users;
import tachyon.Version;
import tachyon.conf.TachyonConf;
import tachyon.thrift.Command;
import tachyon.thrift.NetAddress;
import tachyon.thrift.WorkerService;
import tachyon.util.CommonUtils;
import tachyon.util.NetworkUtils;
import tachyon.util.ThreadFactoryUtils;
import tachyon.web.UIWebServer;
import tachyon.web.WorkerUIWebServer;
/**
* Entry point for a worker daemon.
*/
public class TachyonWorker implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);
/**
* Create a new TachyonWorker
*
* @param tachyonConf The instance of {@link tachyon.conf.TachyonConf} to used by Worker.
* @return The new TachyonWorker
*/
public static synchronized TachyonWorker createWorker(TachyonConf tachyonConf) {
String masterHostname = tachyonConf.get(Constants.MASTER_HOSTNAME,
NetworkUtils.getLocalHostName(tachyonConf));
int masterPort = tachyonConf.getInt(Constants.MASTER_PORT, Constants.DEFAULT_MASTER_PORT);
String workerHostName = NetworkUtils.getLocalHostName(tachyonConf);
int workerPort = tachyonConf.getInt(Constants.WORKER_PORT, Constants.DEFAULT_WORKER_PORT);
int dataPort =
tachyonConf.getInt(Constants.WORKER_DATA_PORT, Constants.DEFAULT_WORKER_DATA_SERVER_PORT);
int minWorkerThreads =
tachyonConf.getInt(Constants.WORKER_MIN_WORKER_THREADS, Runtime.getRuntime()
.availableProcessors());
int maxWorkerThreads =
tachyonConf.getInt(Constants.WORKER_MAX_WORKER_THREADS,
Constants.DEFAULT_WORKER_MAX_WORKER_THREADS);
return new TachyonWorker(new InetSocketAddress(masterHostname, masterPort),
new InetSocketAddress(workerHostName, workerPort), dataPort, minWorkerThreads,
maxWorkerThreads, tachyonConf);
}
private static void setMasterAddress(String masterAddress, TachyonConf conf) {
if (masterAddress == null) {
return;
}
String masterHostnameConf = conf.get(Constants.MASTER_HOSTNAME,
NetworkUtils.getLocalHostName(conf));
String masterPortConf = conf.get(Constants.MASTER_PORT, Constants.DEFAULT_MASTER_PORT + "");
String[] address = masterAddress.split(":");
String masterHostname = address[0];
if (!masterHostnameConf.equals(masterHostname)) {
LOG.warn("Master host in configuration ({}) is different from the command line ({}).",
masterHostnameConf, masterHostname);
conf.set(Constants.MASTER_HOSTNAME, masterHostname);
}
String masterPort = masterPortConf;
if (address.length > 1) {
masterPort = address[1];
if (!masterPortConf.equals(masterPort)) {
LOG.warn("Master port in configuration ({}) is different from the command line ({}).",
masterPortConf, masterPort);
conf.set(Constants.MASTER_PORT, masterPort);
}
}
return;
}
public static void main(String[] args) throws UnknownHostException {
if (args.length > 1) {
LOG.info("Usage: java -cp target/tachyon-" + Version.VERSION + "-jar-with-dependencies.jar "
+ "tachyon.Worker [<MasterHost:Port>]");
System.exit(-1);
}
TachyonConf tachyonConf = new TachyonConf();
if (args.length == 1) {
setMasterAddress(args[0], tachyonConf);
}
String resolvedWorkerHost = NetworkUtils.getLocalHostName(tachyonConf);
LOG.info("Resolved local TachyonWorker host to " + resolvedWorkerHost);
try {
TachyonWorker worker = TachyonWorker.createWorker(tachyonConf);
worker.start();
} catch (Exception e) {
LOG.error("Uncaught exception terminating worker", e);
System.exit(-1);
}
}
private final InetSocketAddress mMasterAddress;
private final NetAddress mWorkerAddress;
private final UIWebServer mWebServer;
private final int mWebPort;
private TServer mServer;
private TServerSocket mServerTServerSocket;
private final WorkerStorage mWorkerStorage;
private final WorkerServiceHandler mWorkerServiceHandler;
private final DataServer mDataServer;
private final Thread mHeartbeatThread;
private volatile boolean mStop = false;
private final int mPort;
private final int mDataPort;
private final ExecutorService mExecutorService = Executors.newFixedThreadPool(1,
ThreadFactoryUtils.daemon("heartbeat-worker-%d"));
private final TachyonConf mTachyonConf;
/**
* @param masterAddress The TachyonMaster's address.
* @param workerAddress This TachyonWorker's address.
* @param dataPort This TachyonWorker's data server's port
* @param minWorkerThreads The min number of worker threads used in TThreadPoolServer
* @param maxWorkerThreads The max number of worker threads used in TThreadPoolServer
* @param tachyonConf The {@link TachyonConf} instance for configuration properties
*/
private TachyonWorker(InetSocketAddress masterAddress, InetSocketAddress workerAddress,
int dataPort, int minWorkerThreads, int maxWorkerThreads, TachyonConf tachyonConf) {
TachyonConf.assertValidPort(masterAddress, tachyonConf);
TachyonConf.assertValidPort(workerAddress, tachyonConf);
TachyonConf.assertValidPort(dataPort, tachyonConf);
mTachyonConf = tachyonConf;
mMasterAddress = masterAddress;
mWorkerStorage = new WorkerStorage(mMasterAddress, mExecutorService, mTachyonConf);
mWorkerServiceHandler = new WorkerServiceHandler(mWorkerStorage);
mWebPort = mTachyonConf.getInt(Constants.WORKER_WEB_PORT, Constants.DEFAULT_WORKER_WEB_PORT);
// Extract the port from the generated socket.
// When running tests, its great to use port '0' so the system will figure out what port to use
// (any random free port).
// In a production or any real deployment setup, port '0' should not be used as it will make
// deployment more complicated.
InetSocketAddress dataAddress = new InetSocketAddress(workerAddress.getHostName(), dataPort);
BlocksLocker blockLocker = new BlocksLocker(mWorkerStorage, Users.DATASERVER_USER_ID);
mDataServer = DataServer.Factory.createDataServer(dataAddress, blockLocker, mTachyonConf);
mDataPort = mDataServer.getPort();
mHeartbeatThread = new Thread(this);
try {
LOG.info("Tachyon Worker version " + Version.VERSION + " tries to start @ " + workerAddress);
WorkerService.Processor<WorkerServiceHandler> processor =
new WorkerService.Processor<WorkerServiceHandler>(mWorkerServiceHandler);
mServerTServerSocket = new TServerSocket(workerAddress);
mPort = NetworkUtils.getPort(mServerTServerSocket);
mServer =
new TThreadPoolServer(new TThreadPoolServer.Args(mServerTServerSocket)
.minWorkerThreads(minWorkerThreads).maxWorkerThreads(maxWorkerThreads)
.processor(processor).transportFactory(new TFramedTransport.Factory())
.protocolFactory(new TBinaryProtocol.Factory(true, true)));
} catch (TTransportException e) {
LOG.error(e.getMessage(), e);
throw Throwables.propagate(e);
}
mWorkerAddress =
new NetAddress(workerAddress.getAddress().getCanonicalHostName(), mPort, mDataPort);
mWorkerStorage.initialize(mWorkerAddress);
mWebServer =
new WorkerUIWebServer("Tachyon Worker", new InetSocketAddress(workerAddress.getHostName(),
mWebPort), mWorkerStorage, mTachyonConf);
}
/**
* Gets the data port of the worker. For unit tests only.
*/
public int getDataPort() {
return mDataPort;
}
/**
* Gets the metadata port of the worker. For unit tests only.
*/
public int getMetaPort() {
return mPort;
}
/**
* Gets the underlying {@link tachyon.conf.TachyonConf} instance for the Worker.
*/
public TachyonConf getTachyonConf() {
return mTachyonConf;
}
/**
* Get the worker server handler class. This is for unit test only.
*
* @return the WorkerServiceHandler
*/
WorkerServiceHandler getWorkerServiceHandler() {
return mWorkerServiceHandler;
}
private void login() throws IOException {
String workerKeytabFile = mTachyonConf.get(Constants.WORKER_KEYTAB_KEY, null);
String workerPrincipal = mTachyonConf.get(Constants.WORKER_PRINCIPAL_KEY, null);
if (workerKeytabFile == null || workerPrincipal == null) {
return;
}
String ufsAddress = mTachyonConf.get(Constants.UNDERFS_ADDRESS, "localhost/underfs");
UnderFileSystem ufs = UnderFileSystem.get(ufsAddress, mTachyonConf);
if (ufs instanceof UnderFileSystemHdfs) {
((UnderFileSystemHdfs) ufs)
.login(Constants.WORKER_KEYTAB_KEY, workerKeytabFile, Constants.WORKER_PRINCIPAL_KEY,
workerPrincipal, NetworkUtils.getFqdnHost(mWorkerAddress));
}
}
@Override
public void run() {
long lastHeartbeatMs = System.currentTimeMillis();
Command cmd = null;
while (!mStop) {
long diff = System.currentTimeMillis() - lastHeartbeatMs;
int hbIntervalMs =
mTachyonConf
.getInt(Constants.WORKER_TO_MASTER_HEARTBEAT_INTERVAL_MS, Constants.SECOND_MS);
if (diff < hbIntervalMs) {
LOG.debug("Heartbeat process takes {} ms.", diff);
CommonUtils.sleepMs(LOG, hbIntervalMs - diff);
} else {
LOG.error("Heartbeat process takes " + diff + " ms.");
}
try {
cmd = mWorkerStorage.heartbeat();
lastHeartbeatMs = System.currentTimeMillis();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
mWorkerStorage.resetMasterClient();
CommonUtils.sleepMs(LOG, Constants.SECOND_MS);
cmd = null;
int heartbeatTimeout =
mTachyonConf.getInt(Constants.WORKER_HEARTBEAT_TIMEOUT_MS, 10 * Constants.SECOND_MS);
if (System.currentTimeMillis() - lastHeartbeatMs >= heartbeatTimeout) {
throw new RuntimeException("Heartbeat timeout "
+ (System.currentTimeMillis() - lastHeartbeatMs) + "ms");
}
}
if (cmd != null) {
switch (cmd.mCommandType) {
case Unknown:
LOG.error("Unknown command: " + cmd);
break;
case Nothing:
LOG.debug("Nothing command: {}", cmd);
break;
case Register:
LOG.info("Register command: " + cmd);
mWorkerStorage.register();
break;
case Free:
mWorkerStorage.freeBlocks(cmd.mData);
LOG.info("Free command: " + cmd);
break;
case Delete:
LOG.info("Delete command: " + cmd);
break;
default:
throw new RuntimeException("Un-recognized command from master " + cmd.toString());
}
}
mWorkerStorage.checkStatus();
}
}
/**
* Start the data server thread and heartbeat thread of this TachyonWorker.
*/
public void start() throws IOException {
login();
mHeartbeatThread.start();
mWebServer.startWebServer();
LOG.info("The worker server started @ " + mWorkerAddress);
mServer.serve();
LOG.info("The worker server ends @ " + mWorkerAddress);
}
/**
* Stop this TachyonWorker. Stop all the threads belong to this TachyonWorker.
*
* @throws IOException
* @throws InterruptedException
*/
public void stop() throws IOException, InterruptedException {
mStop = true;
mWorkerStorage.stop();
mDataServer.close();
mServer.stop();
mServerTServerSocket.close();
mExecutorService.shutdown();
while (!mDataServer.isClosed() || mServer.isServing() || mHeartbeatThread.isAlive()) {
// TODO The reason to stop and close again is due to some issues in Thrift.
mServer.stop();
mServerTServerSocket.close();
CommonUtils.sleepMs(null, 100);
}
mHeartbeatThread.join();
}
}