-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
OutStreamOptions.java
492 lines (447 loc) · 13.3 KB
/
OutStreamOptions.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
/*
* 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.options;
import alluxio.annotation.PublicApi;
import alluxio.client.AlluxioStorageType;
import alluxio.client.UnderStorageType;
import alluxio.client.WriteType;
import alluxio.client.block.policy.BlockLocationPolicy;
import alluxio.client.block.policy.SpecificHostPolicy;
import alluxio.client.file.FileSystemContext;
import alluxio.conf.AlluxioConfiguration;
import alluxio.conf.PropertyKey;
import alluxio.grpc.CreateFilePOptions;
import alluxio.grpc.FileSystemMasterCommonPOptions;
import alluxio.security.authorization.AccessControlList;
import alluxio.security.authorization.Mode;
import alluxio.util.CommonUtils;
import alluxio.util.FileSystemOptionsUtils;
import alluxio.util.IdUtils;
import alluxio.util.ModeUtils;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import java.io.IOException;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Method options for writing a file.
*/
@PublicApi
@NotThreadSafe
public final class OutStreamOptions {
private FileSystemMasterCommonPOptions mCommonOptions;
private long mBlockSizeBytes;
private BlockLocationPolicy mLocationPolicy;
private int mWriteTier;
private WriteType mWriteType;
private String mOwner;
private String mGroup;
private Mode mMode;
private AccessControlList mAcl;
private long mPersistenceWaitTime;
private int mReplicationDurable;
private int mReplicationMax;
private int mReplicationMin;
private String mUfsPath;
private long mMountId;
private String mMediumType;
/**
* @param context Alluxio client context
* @param alluxioConf the Alluxio configuration
* @return the default {@link OutStreamOptions}
*/
public static OutStreamOptions defaults(FileSystemContext context,
AlluxioConfiguration alluxioConf) {
return new OutStreamOptions(context, alluxioConf);
}
/**
* @param context Alluxio client context
* @return the default {@link OutStreamOptions}
*/
public static OutStreamOptions defaults(FileSystemContext context) {
return new OutStreamOptions(context, context.getClusterConf());
}
/**
* Creates an {@link OutStreamOptions} instance from given options.
*
* @param options CreateFile options
* @param context Alluxio client context
* @param alluxioConf the Alluxio configuration
* @throws Exception if {@link BlockLocationPolicy} can't be loaded
*/
public OutStreamOptions(CreateFilePOptions options, FileSystemContext context,
AlluxioConfiguration alluxioConf) {
this(context, alluxioConf);
if (options.hasCommonOptions()) {
mCommonOptions = mCommonOptions.toBuilder().mergeFrom(options.getCommonOptions()).build();
}
if (options.hasBlockSizeBytes()) {
mBlockSizeBytes = options.getBlockSizeBytes();
}
if (options.hasMode()) {
mMode = Mode.fromProto(options.getMode());
}
if (options.hasPersistenceWaitTime()) {
mPersistenceWaitTime = options.getPersistenceWaitTime();
}
if (options.hasReplicationDurable()) {
mReplicationDurable = options.getReplicationDurable();
}
if (options.hasReplicationMin()) {
mReplicationMin = options.getReplicationMin();
}
if (options.hasReplicationMax()) {
mReplicationMax = options.getReplicationMax();
}
if (options.hasWriteTier()) {
mWriteTier = options.getWriteTier();
}
if (options.hasWriteType()) {
mWriteType = WriteType.fromProto(options.getWriteType());
}
if (options.hasWorkerLocation()) {
int port = options.getWorkerLocation().getRpcPort();
mLocationPolicy = new SpecificHostPolicy(
options.getWorkerLocation().getHost(), port == 0 ? null : port);
}
}
private OutStreamOptions(FileSystemContext context, AlluxioConfiguration alluxioConf) {
mCommonOptions = FileSystemOptionsUtils.commonDefaults(alluxioConf);
mBlockSizeBytes = alluxioConf.getBytes(PropertyKey.USER_BLOCK_SIZE_BYTES_DEFAULT);
mLocationPolicy = context.getWriteBlockLocationPolicy(alluxioConf);
mWriteTier = alluxioConf.getInt(PropertyKey.USER_FILE_WRITE_TIER_DEFAULT);
mWriteType = alluxioConf.getEnum(PropertyKey.USER_FILE_WRITE_TYPE_DEFAULT, WriteType.class);
try {
mOwner = context.getClientContext().getUserState().getUser().getName();
mGroup = CommonUtils.getPrimaryGroupName(mOwner, context.getClusterConf());
} catch (IOException e) {
mOwner = "";
mGroup = "";
}
mMode = ModeUtils.applyFileUMask(Mode.defaults(), alluxioConf
.getString(PropertyKey.SECURITY_AUTHORIZATION_PERMISSION_UMASK));
mMountId = IdUtils.INVALID_MOUNT_ID;
mPersistenceWaitTime = alluxioConf.getMs(PropertyKey.USER_FILE_PERSISTENCE_INITIAL_WAIT_TIME);
mReplicationDurable = alluxioConf.getInt(PropertyKey.USER_FILE_REPLICATION_DURABLE);
mReplicationMax = alluxioConf.getInt(PropertyKey.USER_FILE_REPLICATION_MAX);
mReplicationMin = alluxioConf.getInt(PropertyKey.USER_FILE_REPLICATION_MIN);
mMediumType = "";
if (alluxioConf.isSet(PropertyKey.USER_FILE_TARGET_MEDIA)) {
mMediumType = alluxioConf.getString(PropertyKey.USER_FILE_TARGET_MEDIA);
}
}
/**
* @return the write medium type
*/
public String getMediumType() {
return mMediumType;
}
/**
* @return the acl
*/
public AccessControlList getAcl() {
return mAcl;
}
/**
* @return the block size
*/
public long getBlockSizeBytes() {
return mBlockSizeBytes;
}
/**
* @return the file write location policy
*/
public BlockLocationPolicy getLocationPolicy() {
return mLocationPolicy;
}
/**
* @return the Alluxio storage type
*/
public AlluxioStorageType getAlluxioStorageType() {
return mWriteType.getAlluxioStorageType();
}
/**
* @return the common options
*/
public FileSystemMasterCommonPOptions getCommonOptions() {
return mCommonOptions;
}
/**
* @return the under storage type
*/
public UnderStorageType getUnderStorageType() {
return mWriteType.getUnderStorageType();
}
/**
* @return the owner
*/
public String getOwner() {
return mOwner;
}
/**
* @return the group
*/
public String getGroup() {
return mGroup;
}
/**
* @return the mode
*/
public Mode getMode() {
return mMode;
}
/**
* @return the persistence initial wait time
*/
public long getPersistenceWaitTime() {
return mPersistenceWaitTime;
}
/**
* @return the number of block replication for durable write
*/
public int getReplicationDurable() {
return mReplicationDurable;
}
/**
* @return the maximum number of block replication
*/
public int getReplicationMax() {
return mReplicationMax;
}
/**
* @return the minimum number of block replication
*/
public int getReplicationMin() {
return mReplicationMin;
}
/**
* @return the mount id
*/
public long getMountId() {
return mMountId;
}
/**
* @return the ufs path
*/
public String getUfsPath() {
return mUfsPath;
}
/**
* @return the write tier
*/
public int getWriteTier() {
return mWriteTier;
}
/**
* @return the write type
*/
public WriteType getWriteType() {
return mWriteType;
}
/**
* Set the write medium type of the file.
*
* @param mediumType write medium type
* @return the updated options object
*/
public OutStreamOptions setMediumType(String mediumType) {
mMediumType = mediumType;
return this;
}
/**
* Sets the acl of the file.
*
* @param acl the acl to use
* @return the updated options object
*/
public OutStreamOptions setAcl(AccessControlList acl) {
mAcl = acl;
return this;
}
/**
* Sets the size of the block in bytes.
*
* @param blockSizeBytes the block size to use
* @return the updated options object
*/
public OutStreamOptions setBlockSizeBytes(long blockSizeBytes) {
mBlockSizeBytes = blockSizeBytes;
return this;
}
/**
* @param locationPolicy the file write location policy
* @return the updated options object
*/
public OutStreamOptions setLocationPolicy(BlockLocationPolicy locationPolicy) {
mLocationPolicy = locationPolicy;
return this;
}
/**
* Sets the write tier.
*
* @param writeTier the write tier to use for this operation
* @return the updated options object
*/
public OutStreamOptions setWriteTier(int writeTier) {
mWriteTier = writeTier;
return this;
}
/**
* Sets the {@link WriteType}.
*
* @param writeType the {@link WriteType} to use for this operation. This will override both the
* {@link AlluxioStorageType} and {@link UnderStorageType}.
* @return the updated options object
*/
public OutStreamOptions setWriteType(WriteType writeType) {
mWriteType = writeType;
return this;
}
/**
* @param mountId the mount id
* @return the updated options object
*/
public OutStreamOptions setMountId(long mountId) {
mMountId = mountId;
return this;
}
/**
* @param ufsPath the ufs path
* @return the updated options object
*/
public OutStreamOptions setUfsPath(String ufsPath) {
mUfsPath = ufsPath;
return this;
}
/**
* @param owner the owner to set
* @return the updated object
*/
public OutStreamOptions setOwner(String owner) {
mOwner = owner;
return this;
}
/**
* @param group the group to set
* @return the updated object
*/
public OutStreamOptions setGroup(String group) {
mGroup = group;
return this;
}
/**
* @param persistenceWaitTime the persistence initial wait time
* @return the updated options object
*/
public OutStreamOptions setPersistenceWaitTime(long persistenceWaitTime) {
mPersistenceWaitTime = persistenceWaitTime;
return this;
}
/**
* @param replicationDurable the number of block replication for durable write
* @return the updated options object
*/
public OutStreamOptions setReplicationDurable(int replicationDurable) {
mReplicationDurable = replicationDurable;
return this;
}
/**
* @param replicationMax the maximum number of block replication
* @return the updated options object
*/
public OutStreamOptions setReplicationMax(int replicationMax) {
mReplicationMax = replicationMax;
return this;
}
/**
* @param replicationMin the minimum number of block replication
* @return the updated options object
*/
public OutStreamOptions setReplicationMin(int replicationMin) {
mReplicationMin = replicationMin;
return this;
}
/**
* @param mode the permission
* @return the updated options object
*/
public OutStreamOptions setMode(Mode mode) {
mMode = mode;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof OutStreamOptions)) {
return false;
}
OutStreamOptions that = (OutStreamOptions) o;
return Objects.equal(mAcl, that.mAcl)
&& Objects.equal(mBlockSizeBytes, that.mBlockSizeBytes)
&& Objects.equal(mCommonOptions, that.mCommonOptions)
&& Objects.equal(mGroup, that.mGroup)
&& Objects.equal(mLocationPolicy, that.mLocationPolicy)
&& Objects.equal(mMediumType, that.mMediumType)
&& Objects.equal(mMode, that.mMode)
&& Objects.equal(mMountId, that.mMountId)
&& Objects.equal(mOwner, that.mOwner)
&& Objects.equal(mPersistenceWaitTime, that.mPersistenceWaitTime)
&& Objects.equal(mReplicationDurable, that.mReplicationDurable)
&& Objects.equal(mReplicationMax, that.mReplicationMax)
&& Objects.equal(mReplicationMin, that.mReplicationMin)
&& Objects.equal(mUfsPath, that.mUfsPath)
&& Objects.equal(mWriteTier, that.mWriteTier)
&& Objects.equal(mWriteType, that.mWriteType);
}
@Override
public int hashCode() {
return Objects.hashCode(
mAcl,
mBlockSizeBytes,
mCommonOptions,
mGroup,
mLocationPolicy,
mMediumType,
mMode,
mMountId,
mOwner,
mPersistenceWaitTime,
mReplicationDurable,
mReplicationMax,
mReplicationMin,
mUfsPath,
mWriteTier,
mWriteType
);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("acl", mAcl)
.add("blockSizeBytes", mBlockSizeBytes)
.add("commonOptions", mCommonOptions)
.add("group", mGroup)
.add("locationPolicy", mLocationPolicy)
.add("mediumType", mMediumType)
.add("mode", mMode)
.add("mountId", mMountId)
.add("owner", mOwner)
.add("ufsPath", mUfsPath)
.add("writeTier", mWriteTier)
.add("writeType", mWriteType)
.add("persistenceWaitTime", mPersistenceWaitTime)
.add("replicationDurable", mReplicationDurable)
.add("replicationMax", mReplicationMax)
.add("replicationMin", mReplicationMin)
.toString();
}
}