-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
FileSystemMasterClient.java
340 lines (315 loc) · 11 KB
/
FileSystemMasterClient.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
/*
* 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.AbstractMasterClient;
import alluxio.AlluxioURI;
import alluxio.Configuration;
import alluxio.Constants;
import alluxio.client.file.options.CompleteFileOptions;
import alluxio.client.file.options.CreateDirectoryOptions;
import alluxio.client.file.options.CreateFileOptions;
import alluxio.client.file.options.DeleteOptions;
import alluxio.client.file.options.FreeOptions;
import alluxio.client.file.options.LoadMetadataOptions;
import alluxio.client.file.options.MountOptions;
import alluxio.client.file.options.SetAttributeOptions;
import alluxio.exception.AlluxioException;
import alluxio.thrift.AlluxioService;
import alluxio.thrift.AlluxioTException;
import alluxio.thrift.FileSystemMasterClientService;
import alluxio.wire.ThriftUtils;
import org.apache.thrift.TException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.concurrent.ThreadSafe;
/**
* A wrapper for the thrift client to interact with the file system master, used by alluxio clients.
*
* Since thrift clients are not thread safe, this class is a wrapper to provide thread safety, and
* to provide retries.
*/
@ThreadSafe
public final class FileSystemMasterClient extends AbstractMasterClient {
private FileSystemMasterClientService.Client mClient = null;
/**
* Creates a new file system master client.
*
* @param masterAddress the master address
* @param configuration the Alluxio configuration
*/
public FileSystemMasterClient(InetSocketAddress masterAddress, Configuration configuration) {
super(masterAddress, configuration);
}
@Override
protected AlluxioService.Client getClient() {
return mClient;
}
@Override
protected String getServiceName() {
return Constants.FILE_SYSTEM_MASTER_CLIENT_SERVICE_NAME;
}
@Override
protected long getServiceVersion() {
return Constants.FILE_SYSTEM_MASTER_CLIENT_SERVICE_VERSION;
}
@Override
protected void afterConnect() throws IOException {
mClient = new FileSystemMasterClientService.Client(mProtocol);
}
/**
* Creates a new directory.
*
* @param path the directory path
* @param options method options
* @throws IOException if an I/O error occurs
* @throws AlluxioException if an Alluxio error occurs
*/
public synchronized void createDirectory(final AlluxioURI path,
final CreateDirectoryOptions options) throws IOException, AlluxioException {
retryRPC(new RpcCallableThrowsAlluxioTException<Void>() {
@Override
public Void call() throws AlluxioTException, TException {
mClient.createDirectory(path.getPath(), options.toThrift());
return null;
}
});
}
/**
* Creates a new file.
*
* @param path the file path
* @param options method options
* @throws IOException if an I/O error occurs
* @throws AlluxioException if an Alluxio error occurs
*/
public synchronized void createFile(final AlluxioURI path, final CreateFileOptions options)
throws IOException, AlluxioException {
retryRPC(new RpcCallableThrowsAlluxioTException<Void>() {
@Override
public Void call() throws AlluxioTException, TException {
mClient.createFile(path.getPath(), options.toThrift());
return null;
}
});
}
/**
* Marks a file as completed.
*
* @param path the file path
* @param options the method options
* @throws IOException if an I/O error occurs
* @throws AlluxioException if an Alluxio error occurs
*/
public synchronized void completeFile(final AlluxioURI path, final CompleteFileOptions options)
throws IOException, AlluxioException {
retryRPC(new RpcCallableThrowsAlluxioTException<Void>() {
@Override
public Void call() throws AlluxioTException, TException {
mClient.completeFile(path.getPath(), options.toThrift());
return null;
}
});
}
/**
* Deletes a file or a directory.
*
* @param path the path to delete
* @param options method options
* @throws IOException if an I/O error occurs
* @throws AlluxioException if an Alluxio error occurs
*/
public synchronized void delete(final AlluxioURI path, final DeleteOptions options)
throws IOException, AlluxioException {
retryRPC(new RpcCallableThrowsAlluxioTException<Void>() {
@Override
public Void call() throws AlluxioTException, TException {
mClient.remove(path.getPath(), options.isRecursive());
return null;
}
});
}
/**
* Frees a file.
*
* @param path the path to free
* @param options method options
* @throws IOException if an I/O error occurs
* @throws AlluxioException if an Alluxio error occurs
*/
public synchronized void free(final AlluxioURI path, final FreeOptions options)
throws IOException, AlluxioException {
retryRPC(new RpcCallableThrowsAlluxioTException<Void>() {
@Override
public Void call() throws AlluxioTException, TException {
mClient.free(path.getPath(), options.isRecursive());
return null;
}
});
}
/**
* @param path the file path
* @return the file info for the given file id
* @throws IOException if an I/O error occurs
* @throws AlluxioException if an Alluxio error occurs
*/
public synchronized URIStatus getStatus(final AlluxioURI path) throws IOException,
AlluxioException {
return retryRPC(new RpcCallableThrowsAlluxioTException<URIStatus>() {
@Override
public URIStatus call() throws AlluxioTException, TException {
return new URIStatus(ThriftUtils.fromThrift(mClient.getStatus(path.getPath())));
}
});
}
/**
* @param path the file path
* @return the next blockId for the file
* @throws IOException if an I/O error occurs
* @throws AlluxioException if an Alluxio error occurs
*/
public synchronized long getNewBlockIdForFile(final AlluxioURI path)
throws IOException, AlluxioException {
return retryRPC(new RpcCallableThrowsAlluxioTException<Long>() {
@Override
public Long call() throws AlluxioTException, TException {
return mClient.getNewBlockIdForFile(path.getPath());
}
});
}
/**
* @param path the path to list
* @return the list of file information for the given path
* @throws IOException if an I/O error occurs
* @throws AlluxioException if an Alluxio error occurs
*/
public synchronized List<URIStatus> listStatus(final AlluxioURI path)
throws IOException, AlluxioException {
return retryRPC(new RpcCallableThrowsAlluxioTException<List<URIStatus>>() {
@Override
public List<URIStatus> call() throws AlluxioTException, TException {
List<URIStatus> result = new ArrayList<URIStatus>();
for (alluxio.thrift.FileInfo fileInfo : mClient.listStatus(path.getPath())) {
result.add(new URIStatus(ThriftUtils.fromThrift(fileInfo)));
}
return result;
}
});
}
/**
* Loads the metadata of a file from the under file system.
*
* @param path the path of the file to load metadata for
* @param options method options
* @throws AlluxioException if an Alluxio error occurs
* @throws IOException if an I/O error occurs
*/
public synchronized void loadMetadata(final AlluxioURI path,
final LoadMetadataOptions options) throws IOException, AlluxioException {
retryRPC(new RpcCallableThrowsAlluxioTException<Long>() {
@Override
public Long call() throws AlluxioTException, TException {
return mClient.loadMetadata(path.toString(), options.toThrift());
}
});
}
/**
* Mounts the given UFS path under the given Alluxio path.
*
* @param alluxioPath the Alluxio path
* @param ufsPath the UFS path
* @param options mount options
* @throws AlluxioException if an Alluxio error occurs
* @throws IOException an I/O error occurs
*/
public synchronized void mount(final AlluxioURI alluxioPath, final AlluxioURI ufsPath,
final MountOptions options)
throws AlluxioException, IOException {
retryRPC(new RpcCallableThrowsAlluxioTException<Void>() {
@Override
public Void call() throws AlluxioTException, TException {
mClient.mount(alluxioPath.toString(), ufsPath.toString(), options.toThrift());
return null;
}
});
}
/**
* Renames a file or a directory.
*
* @param src the path to rename
* @param dst new file path
* @throws IOException if an I/O error occurs
* @throws AlluxioException if an Alluxio error occurs
*/
public synchronized void rename(final AlluxioURI src, final AlluxioURI dst)
throws IOException, AlluxioException {
retryRPC(new RpcCallableThrowsAlluxioTException<Void>() {
@Override
public Void call() throws AlluxioTException, TException {
mClient.rename(src.getPath(), dst.getPath());
return null;
}
});
}
/**
* Sets the file or directory attributes.
*
* @param path the file or directory path
* @param options the file or directory attribute options to be set
* @throws IOException if an I/O error occurs
* @throws AlluxioException if an Alluxio error occurs
*/
public synchronized void setAttribute(final AlluxioURI path, final SetAttributeOptions options)
throws IOException, AlluxioException {
retryRPC(new RpcCallableThrowsAlluxioTException<Void>() {
@Override
public Void call() throws AlluxioTException, TException {
mClient.setAttribute(path.getPath(), options.toThrift());
return null;
}
});
}
/**
* Schedules the async persistence of the given file.
*
* @param path the file path
* @throws AlluxioException if an Alluxio error occurs
* @throws IOException if an I/O error occurs
*/
public synchronized void scheduleAsyncPersist(final AlluxioURI path)
throws AlluxioException, IOException {
retryRPC(new RpcCallableThrowsAlluxioTException<Void>() {
@Override
public Void call() throws AlluxioTException, TException {
mClient.scheduleAsyncPersist(path.getPath());
return null;
}
});
}
/**
* Unmounts the given Alluxio path.
*
* @param alluxioPath the Alluxio path
* @throws AlluxioException if an Alluxio error occurs
* @throws IOException an I/O error occurs
*/
public synchronized void unmount(final AlluxioURI alluxioPath)
throws AlluxioException, IOException {
retryRPC(new RpcCallableThrowsAlluxioTException<Void>() {
@Override
public Void call() throws AlluxioTException, TException {
mClient.unmount(alluxioPath.toString());
return null;
}
});
}
}