/
TieredBlockStore.java
523 lines (480 loc) · 21.2 KB
/
TieredBlockStore.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
* 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.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
import com.google.common.io.Files;
import tachyon.Constants;
import tachyon.Pair;
import tachyon.conf.TachyonConf;
import tachyon.thrift.InvalidPathException;
import tachyon.util.CommonUtils;
import tachyon.worker.block.allocator.Allocator;
import tachyon.worker.block.allocator.AllocatorFactory;
import tachyon.worker.block.allocator.AllocatorType;
import tachyon.worker.block.evictor.EvictionPlan;
import tachyon.worker.block.evictor.Evictor;
import tachyon.worker.block.evictor.EvictorFactory;
import tachyon.worker.block.evictor.EvictorType;
import tachyon.worker.block.io.BlockReader;
import tachyon.worker.block.io.BlockWriter;
import tachyon.worker.block.io.LocalFileBlockReader;
import tachyon.worker.block.io.LocalFileBlockWriter;
import tachyon.worker.block.meta.BlockMeta;
import tachyon.worker.block.meta.StorageDir;
import tachyon.worker.block.meta.StorageTier;
import tachyon.worker.block.meta.TempBlockMeta;
/**
* This class represents an object store that manages all the blocks in the local tiered storage.
* This store exposes simple public APIs to operate blocks. Inside this store, it creates an
* Allocator to decide where to put a new block, an Evictor to decide where to evict a stale block,
* a BlockMetadataManager to maintain the status of the tiered storage, and a LockManager to
* coordinate read/write on the same block.
* <p>
* This class is thread-safe.
*/
// TODO: This atomicity comes with cost of heavy locking, improve locking by not guard eviction
// (and its IO performance) with heavy write lock (TACHYON-584)
// TODO: If a method requires certain locks being hold, validate it.
public class TieredBlockStore implements BlockStore {
private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);
private final TachyonConf mTachyonConf;
private final BlockMetadataManager mMetaManager;
private final BlockLockManager mLockManager;
private final Allocator mAllocator;
private final Evictor mEvictor;
private List<BlockStoreEventListener> mBlockStoreEventListeners =
new ArrayList<BlockStoreEventListener>();
/** A set of pinned inodes fetched from the master */
private final Set<Integer> mPinnedInodes = new HashSet<Integer>();
/**
* A read/write lock to ensure eviction is atomic w.r.t. other operations. An eviction may trigger
* a sequence of block remove and move and we want eviction to be atomic so no remove or move
* interleaves during eviction is working. The current workaround is to wrap single block
* operations like remove and move by read lock and wrap eviction operations by write lock that
* can wait for previous operations and block following operations.
*/
private final ReentrantReadWriteLock mEvictionLock = new ReentrantReadWriteLock();
public TieredBlockStore(TachyonConf tachyonConf) throws IOException {
mTachyonConf = Preconditions.checkNotNull(tachyonConf);
mMetaManager = BlockMetadataManager.newBlockMetadataManager(mTachyonConf);
mLockManager = new BlockLockManager(mMetaManager);
AllocatorType allocatorType =
mTachyonConf.getEnum(Constants.WORKER_ALLOCATE_STRATEGY_TYPE, AllocatorType.DEFAULT);
BlockMetadataManagerView initManagerView = new BlockMetadataManagerView(mMetaManager,
Collections.<Integer>emptySet(), Collections.<Long>emptySet());
mAllocator = AllocatorFactory.create(allocatorType, initManagerView);
if (mAllocator instanceof BlockStoreEventListener) {
registerBlockStoreEventListener((BlockStoreEventListener) mAllocator);
}
EvictorType evictorType =
mTachyonConf.getEnum(Constants.WORKER_EVICT_STRATEGY_TYPE, EvictorType.DEFAULT);
initManagerView = new BlockMetadataManagerView(mMetaManager,
Collections.<Integer>emptySet(), Collections.<Long>emptySet());
mEvictor = EvictorFactory.create(evictorType, initManagerView);
if (mEvictor instanceof BlockStoreEventListener) {
registerBlockStoreEventListener((BlockStoreEventListener) mEvictor);
}
}
@Override
public long lockBlock(long userId, long blockId) throws IOException {
return mLockManager.lockBlock(userId, blockId, BlockLockType.READ);
}
@Override
public void unlockBlock(long lockId) throws IOException {
mLockManager.unlockBlock(lockId);
}
@Override
public void unlockBlock(long userId, long blockId) throws IOException {
mLockManager.unlockBlock(userId, blockId);
}
@Override
public BlockWriter getBlockWriter(long userId, long blockId) throws IOException {
TempBlockMeta tempBlockMeta = mMetaManager.getTempBlockMeta(blockId);
return new LocalFileBlockWriter(tempBlockMeta);
}
@Override
public BlockReader getBlockReader(long userId, long blockId, long lockId) throws IOException {
mLockManager.validateLock(userId, blockId, lockId);
BlockMeta blockMeta = mMetaManager.getBlockMeta(blockId);
return new LocalFileBlockReader(blockMeta);
}
@Override
public TempBlockMeta createBlockMeta(long userId, long blockId, BlockStoreLocation location,
long initialBlockSize) throws IOException {
mEvictionLock.writeLock().lock();
try {
return createBlockMetaNoLock(userId, blockId, location, initialBlockSize);
} finally {
mEvictionLock.writeLock().unlock();
}
}
@Override
public BlockMeta getVolatileBlockMeta(long blockId) throws IOException {
return mMetaManager.getBlockMeta(blockId);
}
@Override
public BlockMeta getBlockMeta(long userId, long blockId, long lockId) throws IOException {
mLockManager.validateLock(userId, blockId, lockId);
return mMetaManager.getBlockMeta(blockId);
}
@Override
public void commitBlock(long userId, long blockId) throws IOException {
mEvictionLock.readLock().lock();
try {
TempBlockMeta tempBlockMeta = mMetaManager.getTempBlockMeta(blockId);
commitBlockNoLock(userId, blockId, tempBlockMeta);
// TODO: move listeners outside of the lock.
synchronized (mBlockStoreEventListeners) {
for (BlockStoreEventListener listener : mBlockStoreEventListeners) {
listener.onCommitBlock(userId, blockId, tempBlockMeta.getBlockLocation());
}
}
} finally {
mEvictionLock.readLock().unlock();
}
}
@Override
public void abortBlock(long userId, long blockId) throws IOException {
mEvictionLock.readLock().lock();
try {
abortBlockNoLock(userId, blockId);
synchronized (mBlockStoreEventListeners) {
for (BlockStoreEventListener listener : mBlockStoreEventListeners) {
listener.onAbortBlock(userId, blockId);
}
}
} finally {
mEvictionLock.readLock().unlock();
}
}
@Override
public void requestSpace(long userId, long blockId, long additionalBytes) throws IOException {
// TODO: Change the lock to read lock and only upgrade to write lock if necessary
mEvictionLock.writeLock().lock();
try {
TempBlockMeta tempBlockMeta = mMetaManager.getTempBlockMeta(blockId);
freeSpaceInternal(userId, additionalBytes, tempBlockMeta.getBlockLocation());
// Increase the size of this temp block
mMetaManager.resizeTempBlockMeta(tempBlockMeta, tempBlockMeta.getBlockSize()
+ additionalBytes);
} finally {
mEvictionLock.writeLock().unlock();
}
}
@Override
public void moveBlock(long userId, long blockId, BlockStoreLocation newLocation)
throws IOException {
mEvictionLock.writeLock().lock();
try {
long lockId = mLockManager.lockBlock(userId, blockId, BlockLockType.WRITE);
try {
BlockMeta blockMeta = mMetaManager.getBlockMeta(blockId);
BlockStoreLocation oldLocation = blockMeta.getBlockLocation();
// freeSpaceInternal ensures newLocation has enough space
freeSpaceInternal(userId, blockMeta.getBlockSize(), newLocation);
moveBlockNoLock(blockId, newLocation);
blockMeta = mMetaManager.getBlockMeta(blockId);
BlockStoreLocation actualNewLocation = blockMeta.getBlockLocation();
synchronized (mBlockStoreEventListeners) {
for (BlockStoreEventListener listener : mBlockStoreEventListeners) {
listener.onMoveBlockByClient(userId, blockId, oldLocation, actualNewLocation);
}
}
} finally {
mLockManager.unlockBlock(lockId);
}
} finally {
// If we fail to lock, the block is no longer in tiered store
mEvictionLock.writeLock().unlock();
}
}
@Override
public void removeBlock(long userId, long blockId) throws IOException {
mEvictionLock.readLock().lock();
try {
long lockId = mLockManager.lockBlock(userId, blockId, BlockLockType.WRITE);
try {
removeBlockNoLock(userId, blockId);
synchronized (mBlockStoreEventListeners) {
for (BlockStoreEventListener listener : mBlockStoreEventListeners) {
listener.onRemoveBlockByClient(userId, blockId);
}
}
} finally {
mLockManager.unlockBlock(lockId);
}
} finally {
// If we fail to lock, the block is no longer in tiered store
mEvictionLock.readLock().unlock();
}
}
@Override
public void accessBlock(long userId, long blockId) {
synchronized (mBlockStoreEventListeners) {
for (BlockStoreEventListener listener : mBlockStoreEventListeners) {
listener.onAccessBlock(userId, blockId);
}
}
}
@Override
public void freeSpace(long userId, long availableBytes, BlockStoreLocation location)
throws IOException {
mEvictionLock.writeLock().lock();
try {
freeSpaceInternal(userId, availableBytes, location);
} finally {
mEvictionLock.writeLock().unlock();
}
}
@Override
public void cleanupUser(long userId) throws IOException {
List<TempBlockMeta> tempBlocksToRemove = mMetaManager.getUserTempBlocks(userId);
List<Long> removedTempBlocks = new ArrayList<Long>(tempBlocksToRemove.size());
// TODO: fix the block removing below, there is possible risk condition when the client which
// is considered "dead" may still be using or committing this block.
// A user may have multiple temporary directories for temp blocks, in diffrent StorageTier
// and StorageDir.
for (TempBlockMeta tempBlockMeta : tempBlocksToRemove) {
String fileName = tempBlockMeta.getPath();
if (!new File(fileName).delete()) {
LOG.error("Error in cleanup userId {}: cannot delete file {}", userId, fileName);
} else {
removedTempBlocks.add(tempBlockMeta.getBlockId());
}
}
// Go through all the storage directories and delete the user folders which should be empty
for (StorageTier tier : mMetaManager.getTiers()) {
for (StorageDir dir : tier.getStorageDirs()) {
File userFolder = new File(CommonUtils.concatPath(dir.getDirPath(), userId));
if (userFolder.exists() && !userFolder.delete()) {
LOG.error("Failed to clean up user: {} with directory: {}", userId, userFolder.getPath());
}
}
}
// Release all locks the user is holding.
mLockManager.cleanupUser(userId);
// Delete the temporary metadata for the user.
mEvictionLock.readLock().lock();
mMetaManager.cleanupUserTempBlocks(userId, removedTempBlocks);
mEvictionLock.readLock().unlock();
}
@Override
public boolean hasBlockMeta(long blockId) {
return mMetaManager.hasBlockMeta(blockId);
}
@Override
public BlockStoreMeta getBlockStoreMeta() {
return mMetaManager.getBlockStoreMeta();
}
@Override
public void registerBlockStoreEventListener(BlockStoreEventListener listener) {
synchronized (mBlockStoreEventListeners) {
mBlockStoreEventListeners.add(listener);
}
}
// Create a temp block meta. This method requires eviction lock in READ mode.
private TempBlockMeta createBlockMetaNoLock(long userId, long blockId,
BlockStoreLocation location, long initialBlockSize) throws IOException {
if (mMetaManager.hasTempBlockMeta(blockId)) {
throw new IOException("Failed to create TempBlockMeta: blockId " + blockId + " exists");
}
if (mMetaManager.hasBlockMeta(blockId)) {
throw new IOException("Failed to create TempBlockMeta: blockId " + blockId + " committed");
}
TempBlockMeta tempBlock = mAllocator.allocateBlockWithView(
userId, blockId, initialBlockSize, location, getUpdatedView());
if (tempBlock == null) {
// Failed to allocate a temp block, let Evictor kick in to ensure sufficient space available.
freeSpaceInternal(userId, initialBlockSize, location);
tempBlock = mAllocator.allocateBlockWithView(
userId, blockId, initialBlockSize, location, getUpdatedView());
Preconditions.checkNotNull(tempBlock, "Cannot allocate block %s:", blockId);
}
// Add allocated temp block to metadata manager
mMetaManager.addTempBlockMeta(tempBlock);
return tempBlock;
}
// Commit a temp block. This method requires eviction lock in READ mode.
private void commitBlockNoLock(long userId, long blockId, TempBlockMeta tempBlockMeta)
throws IOException {
// TODO: share the condition checking among commitBlockNoLock and abortBlockNoLock in a helper
// function.
if (mMetaManager.hasBlockMeta(blockId)) {
throw new IOException("Failed to commit block " + blockId + ": block is committed");
}
// Check the userId is the owner of this temp block
long ownerUserId = tempBlockMeta.getUserId();
if (ownerUserId != userId) {
throw new IOException("Failed to commit temp block " + blockId + ": ownerUserId "
+ ownerUserId + " but userId " + userId);
}
String sourcePath = tempBlockMeta.getPath();
String destPath = tempBlockMeta.getCommitPath();
boolean renamed = new File(sourcePath).renameTo(new File(destPath));
if (!renamed) {
throw new IOException("Failed to commit temp block " + blockId + ": cannot rename from "
+ sourcePath + " to " + destPath);
}
mMetaManager.commitTempBlockMeta(tempBlockMeta);
}
// Abort a temp block. This method requires eviction lock in READ mode.
private void abortBlockNoLock(long userId, long blockId) throws IOException {
if (mMetaManager.hasBlockMeta(blockId)) {
throw new IOException("Failed to abort block " + blockId + ": block is committed");
}
TempBlockMeta tempBlockMeta = mMetaManager.getTempBlockMeta(blockId);
// Check the userId is the owner of this temp block
long ownerUserId = tempBlockMeta.getUserId();
if (ownerUserId != userId) {
throw new IOException("Failed to abort temp block " + blockId + ": ownerUserId "
+ ownerUserId + " but userId " + userId);
}
String path = tempBlockMeta.getPath();
if (!new File(path).delete()) {
throw new IOException("Failed to abort temp block " + blockId + ": cannot delete " + path);
}
mMetaManager.abortTempBlockMeta(tempBlockMeta);
}
/** Move a block. This method requires block lock in WRITE mode and eviction lock in READ mode */
private void moveBlockNoLock(long blockId, BlockStoreLocation newLocation) throws IOException {
if (mMetaManager.hasTempBlockMeta(blockId)) {
throw new IOException("Failed to move block " + blockId + ": block is uncommited");
}
BlockMeta blockMeta = mMetaManager.getBlockMeta(blockId);
// NOTE: since WRITE Eviction lock is acquired, we move metadata first before moving raw data.
mMetaManager.moveBlockMeta(blockMeta, newLocation);
BlockMeta newBlockMeta = mMetaManager.getBlockMeta(blockId);
String srcFilePath = blockMeta.getPath();
String dstFilePath = newBlockMeta.getPath();
// NOTE: Because this move can possibly across storage devices (e.g., from memory to SSD),
// renameTo may not work, use guava's move instead.
Files.move(new File(srcFilePath), new File(dstFilePath));
}
/**
* Remove a block. This method requires block lock in WRITE mode and eviction lock in READ mode.
*/
private void removeBlockNoLock(long userId, long blockId) throws IOException {
if (!mMetaManager.hasBlockMeta(blockId)) {
throw new IOException("Failed to remove block " + blockId + ": block is not found");
}
BlockMeta blockMeta = mMetaManager.getBlockMeta(blockId);
String filePath = blockMeta.getPath();
// Delete the data of the block on "disk"
if (!new File(filePath).delete()) {
throw new IOException("Failed to remove block " + blockId + ": cannot delete " + filePath);
}
mMetaManager.removeBlockMeta(blockMeta);
}
/**
* Get the most updated view with most recent information on pinned inodes,
* and currently locked blocks.
*
* @return BlockMetadataManagerView, a updated view with most recent infomation.
*/
private BlockMetadataManagerView getUpdatedView() {
// TODO: update the view object instead of creating new one every time
synchronized (mPinnedInodes) {
return new BlockMetadataManagerView(mMetaManager, mPinnedInodes,
mLockManager.getLockedBlocks());
}
}
/** This method must be guarded by WRITE lock of mEvictionLock */
private void freeSpaceInternal(long userId, long availableBytes, BlockStoreLocation location)
throws IOException {
EvictionPlan plan = mEvictor.freeSpaceWithView(availableBytes, location, getUpdatedView());
// Absent plan means failed to evict enough space.
if (null == plan) {
throw new IOException("Failed to free space: no eviction plan by evictor");
}
// 1. remove blocks to make room.
for (long blockId : plan.toEvict()) {
long lockId = mLockManager.lockBlock(userId, blockId, BlockLockType.WRITE);
try {
removeBlockNoLock(userId, blockId);
synchronized (mBlockStoreEventListeners) {
for (BlockStoreEventListener listener : mBlockStoreEventListeners) {
listener.onRemoveBlockByWorker(userId, blockId);
}
}
} catch (IOException e) {
throw new IOException("Failed to free space: cannot evict block " + blockId);
} finally {
mLockManager.unlockBlock(lockId);
}
}
// 2. transfer blocks among tiers.
// 2.1. group blocks move plan by the destination tier.
Map<Integer, Set<Pair<Long, BlockStoreLocation>>> blocksGroupedByDestTier =
new HashMap<Integer, Set<Pair<Long, BlockStoreLocation>>>();
for (Pair<Long, BlockStoreLocation> entry : plan.toMove()) {
int alias = entry.getSecond().tierAlias();
if (!blocksGroupedByDestTier.containsKey(alias)) {
blocksGroupedByDestTier.put(alias, new HashSet<Pair<Long, BlockStoreLocation>>());
}
blocksGroupedByDestTier.get(alias).add(entry);
}
// 2.2. sort tiers according in reversed order: bottom tier first and top tier last.
List<Integer> destTierAlias = new ArrayList<Integer>(blocksGroupedByDestTier.keySet());
Collections.sort(destTierAlias, Collections.reverseOrder());
// 2.3. move blocks in the order of their dest tiers.
for (int alias : destTierAlias) {
Set<Pair<Long, BlockStoreLocation>> toMove = blocksGroupedByDestTier.get(alias);
for (Pair<Long, BlockStoreLocation> entry : toMove) {
long blockId = entry.getFirst();
BlockStoreLocation newLocation = entry.getSecond();
BlockMeta blockMeta = mMetaManager.getBlockMeta(blockId);
BlockStoreLocation oldLocation = blockMeta.getBlockLocation();
long lockId = mLockManager.lockBlock(userId, blockId, BlockLockType.WRITE);
try {
moveBlockNoLock(blockId, newLocation);
synchronized (mBlockStoreEventListeners) {
for (BlockStoreEventListener listener : mBlockStoreEventListeners) {
listener.onMoveBlockByWorker(userId, blockId, oldLocation, newLocation);
}
}
} catch (IOException e) {
throw new IOException("Failed to free space: cannot move block " + blockId + " to "
+ newLocation);
} finally {
mLockManager.unlockBlock(lockId);
}
}
}
}
/**
* updates the pinned blocks
*
* @param inodes, a set of IDs inodes that are pinned
*/
@Override
public void updatePinnedInodes(Set<Integer> inodes) {
synchronized (mPinnedInodes) {
mPinnedInodes.clear();
mPinnedInodes.addAll(Preconditions.checkNotNull(inodes));
}
}
}