-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BlockWorker.java
348 lines (308 loc) · 12.5 KB
/
BlockWorker.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
/*
* 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.block;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.thrift.protocol.TBinaryProtocol;
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.Sessions;
import tachyon.client.WorkerBlockMasterClient;
import tachyon.client.WorkerFileSystemMasterClient;
import tachyon.conf.TachyonConf;
import tachyon.metrics.MetricsSystem;
import tachyon.thrift.NetAddress;
import tachyon.thrift.WorkerService;
import tachyon.util.CommonUtils;
import tachyon.util.ThreadFactoryUtils;
import tachyon.util.io.PathUtils;
import tachyon.util.network.NetworkAddressUtils;
import tachyon.util.network.NetworkAddressUtils.ServiceType;
import tachyon.web.UIWebServer;
import tachyon.web.WorkerUIWebServer;
import tachyon.worker.DataServer;
import tachyon.worker.WorkerContext;
import tachyon.worker.WorkerSource;
/**
* The class is responsible for managing all top level components of the Block Worker, including:
*
* Servers: BlockServiceHandler (RPC Server), BlockDataServer (Data Server)
*
* Periodic Threads: BlockMasterSync (Worker to Master continuous communication)
*
* Logic: BlockDataManager (Logic for all block related storage operations)
*/
public final class BlockWorker {
private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);
/** Runnable responsible for heartbeating and registration with master. */
private final BlockMasterSync mBlockMasterSync;
/** Runnable responsible for fetching pinlist from master. */
private final PinListSync mPinListSync;
/** Runnable responsible for clean up potential zombie sessions. */
private final SessionCleaner mSessionCleanerThread;
/** Logic for handling RPC requests. */
private final BlockServiceHandler mServiceHandler;
/** Logic for managing block store and under file system store. */
private final BlockDataManager mBlockDataManager;
/** Server for data requests and responses. */
private final DataServer mDataServer;
/** Client for all block master communication */
private final WorkerBlockMasterClient mBlockMasterClient;
/** Client for all file system master communication */
private final WorkerFileSystemMasterClient mFileSystemMasterClient;
/** The executor service for the master client thread */
private final ExecutorService mMasterClientExecutorService;
/** Threadpool for the master sync */
private final ExecutorService mSyncExecutorService;
/** Net address of this worker */
private final NetAddress mWorkerNetAddress;
/** Configuration object */
private final TachyonConf mTachyonConf;
/** Server socket for thrift */
private final TServerSocket mThriftServerSocket;
/** RPC local port for thrift */
private final int mPort;
/** Thread pool for thrift */
private final TThreadPoolServer mThriftServer;
/** Worker start time in milliseconds */
private final long mStartTimeMs;
/** Worker Web UI server */
private final UIWebServer mWebServer;
/** Worker metrics system */
private MetricsSystem mWorkerMetricsSystem;
/** Space reserver for the block data manager */
private SpaceReserver mSpaceReserver = null;
/**
* @return the worker service handler
*/
public BlockServiceHandler getWorkerServiceHandler() {
return mServiceHandler;
}
/**
* @return the worker RPC service bind host
*/
public String getRPCBindHost() {
return NetworkAddressUtils.getThriftSocket(mThriftServerSocket).getLocalSocketAddress()
.toString();
}
/**
* @return the worker RPC service port
*/
public int getRPCLocalPort() {
return mPort;
}
/**
* @return the worker data service bind host
*/
public String getDataBindHost() {
return mDataServer.getBindHost();
}
/**
* @return the worker data service port
*/
public int getDataLocalPort() {
return mDataServer.getPort();
}
/**
* @return the worker web service bind host
*/
public String getWebBindHost() {
return mWebServer.getBindHost();
}
/**
* @return the worker web service port
*/
public int getWebLocalPort() {
return mWebServer.getLocalPort();
}
/**
* Creates a Tachyon Block Worker.
*
* @throws IOException for other exceptions
*/
public BlockWorker() throws IOException {
mTachyonConf = WorkerContext.getConf();
mStartTimeMs = System.currentTimeMillis();
// Setup MasterClientBase along with its heartbeat ExecutorService
mMasterClientExecutorService =
Executors.newFixedThreadPool(1,
ThreadFactoryUtils.build("worker-client-heartbeat-%d", true));
mBlockMasterClient =
new WorkerBlockMasterClient(NetworkAddressUtils.getConnectAddress(ServiceType.MASTER_RPC,
mTachyonConf), mMasterClientExecutorService, mTachyonConf);
mFileSystemMasterClient = new WorkerFileSystemMasterClient(
NetworkAddressUtils.getConnectAddress(ServiceType.MASTER_RPC, mTachyonConf),
mMasterClientExecutorService, mTachyonConf);
// Set up BlockDataManager
WorkerSource workerSource = new WorkerSource();
mBlockDataManager =
new BlockDataManager(workerSource, mBlockMasterClient, mFileSystemMasterClient,
new TieredBlockStore());
// Setup metrics collection
mWorkerMetricsSystem = new MetricsSystem("worker", mTachyonConf);
workerSource.registerGauges(mBlockDataManager);
mWorkerMetricsSystem.registerSource(workerSource);
// Set up DataServer
mDataServer =
DataServer.Factory.createDataServer(
NetworkAddressUtils.getBindAddress(ServiceType.WORKER_DATA, mTachyonConf),
mBlockDataManager, mTachyonConf);
// reset data server port
mTachyonConf.set(Constants.WORKER_DATA_PORT, Integer.toString(mDataServer.getPort()));
// Setup RPC Server
mServiceHandler = new BlockServiceHandler(mBlockDataManager);
mThriftServerSocket = createThriftServerSocket();
mPort = NetworkAddressUtils.getThriftPort(mThriftServerSocket);
// reset worker RPC port
mTachyonConf.set(Constants.WORKER_PORT, Integer.toString(mPort));
mThriftServer = createThriftServer();
mWorkerNetAddress =
new NetAddress(NetworkAddressUtils.getConnectHost(ServiceType.WORKER_RPC, mTachyonConf),
mPort, mDataServer.getPort());
// Set up web server
mWebServer =
new WorkerUIWebServer(ServiceType.WORKER_WEB, NetworkAddressUtils.getBindAddress(
ServiceType.WORKER_WEB, mTachyonConf), mBlockDataManager,
NetworkAddressUtils.getConnectAddress(ServiceType.WORKER_RPC, mTachyonConf),
mStartTimeMs, mTachyonConf);
// Setup Worker to Master Syncer
// We create four threads for two syncers, one cleaner and one asynchronous evictor:
// mBlockMasterSync, mPinListSync, mSessionCleanerThread, mSpaceReserver
mSyncExecutorService =
Executors.newFixedThreadPool(4, ThreadFactoryUtils.build("worker-heartbeat-%d", true));
mBlockMasterSync = new BlockMasterSync(mBlockDataManager, mWorkerNetAddress,
mBlockMasterClient);
// Get the worker id
// TODO(calvin): Do this at TachyonWorker.
mBlockMasterSync.setWorkerId();
// Setup PinListSyncer
mPinListSync = new PinListSync(mBlockDataManager, mFileSystemMasterClient);
// Setup session cleaner
mSessionCleanerThread = new SessionCleaner(mBlockDataManager);
// Setup space reserver
if (mTachyonConf.getBoolean(Constants.WORKER_SPACE_RESERVER_ENABLE)) {
mSpaceReserver = new SpaceReserver(mBlockDataManager);
}
// Setup session metadata mapping
// TODO(calvin): Have a top level register that gets the worker id.
long workerId = mBlockMasterSync.getWorkerId();
String ufsWorkerFolder = mTachyonConf.get(Constants.UNDERFS_WORKERS_FOLDER);
Sessions sessions = new Sessions(PathUtils.concatPath(ufsWorkerFolder, workerId), mTachyonConf);
// Give BlockDataManager a pointer to the session metadata mapping
// TODO(calvin): Fix this hack when we have a top level register.
mBlockDataManager.setSessions(sessions);
mBlockDataManager.setWorkerId(workerId);
}
/**
* Gets this worker's {@link tachyon.thrift.NetAddress}, which is the worker's hostname, rpc
* server port, and data server port
*
* @return the worker's net address
*/
public NetAddress getWorkerNetAddress() {
return mWorkerNetAddress;
}
/**
* Runs the block worker. The thread calling this will be blocked until the thrift server shuts
* down.
*/
public void process() {
mWorkerMetricsSystem.start();
// Add the metrics servlet to the web server, this must be done after the metrics system starts
mWebServer.addHandler(mWorkerMetricsSystem.getServletHandler());
mSyncExecutorService.submit(mBlockMasterSync);
// Start the pinlist syncer to perform the periodical fetching
mSyncExecutorService.submit(mPinListSync);
// Start the session cleanup checker to perform the periodical checking
mSyncExecutorService.submit(mSessionCleanerThread);
// Start the space reserver
if (mSpaceReserver != null) {
mSyncExecutorService.submit(mSpaceReserver);
}
mWebServer.startWebServer();
mThriftServer.serve();
}
/**
* Stops the block worker. This method should only be called to terminate the worker.
*
* @throws IOException if the data server fails to close.
*/
public void stop() throws IOException {
mDataServer.close();
mThriftServer.stop();
mThriftServerSocket.close();
mBlockMasterSync.stop();
mPinListSync.stop();
mSessionCleanerThread.stop();
mBlockMasterClient.close();
mFileSystemMasterClient.close();
mMasterClientExecutorService.shutdown();
mSyncExecutorService.shutdown();
mWorkerMetricsSystem.stop();
if (mSpaceReserver != null) {
mSpaceReserver.stop();
}
try {
mWebServer.shutdownWebServer();
} catch (Exception e) {
LOG.error("Failed to stop web server", e);
}
mBlockDataManager.stop();
while (!mDataServer.isClosed() || mThriftServer.isServing()) {
// TODO(calvin): The reason to stop and close again is due to some issues in Thrift.
mDataServer.close();
mThriftServer.stop();
mThriftServerSocket.close();
CommonUtils.sleepMs(100);
}
}
/**
* Helper method to create a {@link org.apache.thrift.server.TThreadPoolServer} for handling
* incoming RPC requests.
*
* @return a thrift server
*/
private TThreadPoolServer createThriftServer() {
int minWorkerThreads = mTachyonConf.getInt(Constants.WORKER_MIN_WORKER_THREADS);
int maxWorkerThreads = mTachyonConf.getInt(Constants.WORKER_MAX_WORKER_THREADS);
WorkerService.Processor<BlockServiceHandler> processor =
new WorkerService.Processor<BlockServiceHandler>(mServiceHandler);
return new TThreadPoolServer(new TThreadPoolServer.Args(mThriftServerSocket)
.minWorkerThreads(minWorkerThreads).maxWorkerThreads(maxWorkerThreads).processor(processor)
.transportFactory(new TFramedTransport.Factory())
.protocolFactory(new TBinaryProtocol.Factory(true, true)));
}
/**
* Helper method to create a {@link org.apache.thrift.transport.TServerSocket} for the RPC server
*
* @return a thrift server socket
*/
private TServerSocket createThriftServerSocket() {
try {
return new TServerSocket(NetworkAddressUtils.getBindAddress(ServiceType.WORKER_RPC,
mTachyonConf));
} catch (TTransportException tte) {
LOG.error(tte.getMessage(), tte);
throw Throwables.propagate(tte);
}
}
}