-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
TachyonBlockStore.java
168 lines (153 loc) · 5.69 KB
/
TachyonBlockStore.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
/*
* 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.client.block;
import java.io.Closeable;
import java.io.IOException;
import com.google.common.base.Preconditions;
import tachyon.client.BlockMasterClient;
import tachyon.client.ClientContext;
import tachyon.client.ClientOptions;
import tachyon.thrift.BlockInfo;
import tachyon.thrift.NetAddress;
import tachyon.util.network.NetworkAddressUtils;
/**
* Tachyon Block Store client. This is an internal client for all block level operations in Tachyon.
* An instance of this class can be obtained via {@link TachyonBlockStore#get}. The methods in this
* class are completely opaque to user input (such as {@link ClientOptions}). This class is thread
* safe.
*/
public class TachyonBlockStore implements Closeable {
private static TachyonBlockStore sCachedClient = null;
/**
* @return a new instance of Tachyon block store
*/
public static synchronized TachyonBlockStore get() {
if (null == sCachedClient) {
sCachedClient = new TachyonBlockStore();
}
return sCachedClient;
}
private final BSContext mContext;
/**
* Creates a Tachyon block store.
*/
private TachyonBlockStore() {
mContext = BSContext.INSTANCE;
}
@Override
// TODO: Evaluate the necessity of this method
public synchronized void close() {
sCachedClient = null;
}
/**
* Gets the block info of a block, if it exists.
*
* @param blockId the blockId to obtain information about
* @return a FileBlockInfo containing the metadata of the block
* @throws IOException if the block does not exist
*/
public BlockInfo getInfo(long blockId) throws IOException {
BlockMasterClient masterClient = mContext.acquireMasterClient();
try {
return masterClient.getBlockInfo(blockId);
} finally {
mContext.releaseMasterClient(masterClient);
}
}
/**
* Gets a stream to read the data of a block. The stream is backed by Tachyon storage.
*
* @param blockId the block to read from
* @return a BlockInStream which can be used to read the data in a streaming fashion
* @throws IOException if the block does not exist
*/
public BlockInStream getInStream(long blockId) throws IOException {
BlockMasterClient masterClient = mContext.acquireMasterClient();
try {
// TODO: Fix this RPC
BlockInfo blockInfo = masterClient.getBlockInfo(blockId);
// TODO: Get location via a policy
if (blockInfo.locations.isEmpty()) {
// TODO: Maybe this shouldn't be an exception
throw new IOException("No block " + blockId + " is not available in Tachyon");
}
return BlockInStream.get(blockId, blockInfo.getLength(), blockInfo.locations.get(0)
.getWorkerAddress());
} finally {
mContext.releaseMasterClient(masterClient);
}
}
/**
* Gets a stream to write data to a block. The stream can only be backed by Tachyon storage.
*
* @param blockId the block to write
* @return a BlockOutStream which can be used to write data to the block in a streaming fashion
* @throws IOException if the block cannot be written
*/
public BufferedBlockOutStream getOutStream(long blockId, long blockSize, NetAddress location)
throws IOException {
if (blockSize == -1) {
BlockMasterClient blockMasterClient = mContext.acquireMasterClient();
try {
blockSize = blockMasterClient.getBlockInfo(blockId).getLength();
} finally {
mContext.releaseMasterClient(blockMasterClient);
}
}
// No specified location to write to
if (null == location) {
// Local client, attempt to do direct write to local storage
if (mContext.hasLocalWorker()) {
return new LocalBlockOutStream(blockId, blockSize);
}
// Client is not local or the data is not available on the local worker, use remote stream
return new RemoteBlockOutStream(blockId, blockSize);
}
// Location is local
if (NetworkAddressUtils.getLocalHostName(ClientContext.getConf()).equals(location.getMHost())) {
Preconditions.checkState(mContext.hasLocalWorker(), "Requested write location unavailable.");
return new LocalBlockOutStream(blockId, blockSize);
}
// TODO: Handle the case when a location is specified
return null;
}
/**
* Gets the total capacity of Tachyon's BlockStore.
*
* @return the capacity in bytes
* @throws IOException
*/
public long getCapacityBytes() throws IOException {
BlockMasterClient blockMasterClient = mContext.acquireMasterClient();
try {
return blockMasterClient.getCapacityBytes();
} finally {
mContext.releaseMasterClient(blockMasterClient);
}
}
/**
* Gets the used bytes of Tachyon's BlockStore.
*
* @throws IOException
*/
public long getUsedBytes() throws IOException {
BlockMasterClient blockMasterClient = mContext.acquireMasterClient();
try {
return blockMasterClient.getUsedBytes();
} finally {
mContext.releaseMasterClient(blockMasterClient);
}
}
}