-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
FileSystemContext.java
79 lines (69 loc) · 2.66 KB
/
FileSystemContext.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
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the “License”). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.client.file;
import alluxio.client.ClientContext;
import alluxio.client.block.AlluxioBlockStore;
import javax.annotation.concurrent.ThreadSafe;
/**
* A shared context in each client JVM for common file master client functionality such as a pool of
* master clients. Any remote clients will be created and destroyed on a per use basis.
* <p>
* NOTE: The context maintains a pool of file system master clients that is already thread-safe.
* Synchronizing {@link FileSystemContext} methods could lead to deadlock: thread A attempts to
* acquire a client when there are no clients left in the pool and blocks holding a lock on the
* {@link FileSystemContext}, when thread B attempts to release a client it owns it is unable to do
* so, because thread A holds the lock on {@link FileSystemContext}.
*/
@ThreadSafe
public enum FileSystemContext {
INSTANCE;
private FileSystemMasterClientPool mFileSystemMasterClientPool;
private final AlluxioBlockStore mAlluxioBlockStore;
/**
* Creates a new file stream context.
*/
FileSystemContext() {
mFileSystemMasterClientPool =
new FileSystemMasterClientPool(ClientContext.getMasterAddress());
mAlluxioBlockStore = AlluxioBlockStore.get();
}
/**
* Acquires a block master client from the block master client pool.
*
* @return the acquired block master client
*/
public FileSystemMasterClient acquireMasterClient() {
return mFileSystemMasterClientPool.acquire();
}
/**
* Releases a block master client into the block master client pool.
*
* @param masterClient a block master client to release
*/
public void releaseMasterClient(FileSystemMasterClient masterClient) {
mFileSystemMasterClientPool.release(masterClient);
}
/**
* @return the Alluxio block store
*/
public AlluxioBlockStore getAlluxioBlockStore() {
return mAlluxioBlockStore;
}
/**
* Re-initializes the Block Store context. This method should only be used in
* {@link ClientContext}.
*/
public void reset() {
mFileSystemMasterClientPool.close();
mFileSystemMasterClientPool =
new FileSystemMasterClientPool(ClientContext.getMasterAddress());
}
}