Skip to content

Commit c000115

Browse files
steveloughranKeeProMise
authored andcommitted
HADOOP-19161. S3A: option "fs.s3a.performance.flags" to take list of performance flags (apache#6789)
1. Configuration adds new method `getEnumSet()` to get a set of enum values from a configuration string. <E extends Enum<E>> EnumSet<E> getEnumSet(String key, Class<E> enumClass, boolean ignoreUnknown) Whitespace is ignored, case is ignored and the value "*" is mapped to "all values of the enum". If "ignoreUnknown" is true then when parsing, unknown values are ignored. This is recommended for forward compatiblity with later versions. 2. This support is implemented in org.apache.hadoop.fs.s3a.impl.ConfigurationHelper -it can be used elsewhere in the hadoop codebase. 3. A new private FlagSet class in hadoop common manages a set of enum flags. It implements StreamCapabilities and can be probed for a specific option being set (with a prefix) S3A adds an option fs.s3a.performance.flags which builds a FlagSet with enum type PerformanceFlagEnum * which initially contains {Create, Delete, Mkdir, Open} * the existing fs.s3a.create.performance option sets the flag "Create". * tests which configure fs.s3a.create.performance MUST clear fs.s3a.performance.flags in test setup. Future performance flags are planned, with different levels of safety and/or backwards compatibility. Contributed by Steve Loughran
1 parent 91f0fc5 commit c000115

File tree

21 files changed

+1350
-60
lines changed

21 files changed

+1350
-60
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/Configuration.java

+22
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.Arrays;
5050
import java.util.Collection;
5151
import java.util.Collections;
52+
import java.util.EnumSet;
5253
import java.util.Enumeration;
5354
import java.util.HashMap;
5455
import java.util.HashSet;
@@ -99,6 +100,7 @@
99100
import org.apache.hadoop.security.alias.CredentialProvider.CredentialEntry;
100101
import org.apache.hadoop.security.alias.CredentialProviderFactory;
101102
import org.apache.hadoop.thirdparty.com.google.common.base.Strings;
103+
import org.apache.hadoop.util.ConfigurationHelper;
102104
import org.apache.hadoop.util.Preconditions;
103105
import org.apache.hadoop.util.ReflectionUtils;
104106
import org.apache.hadoop.util.StringInterner;
@@ -1786,6 +1788,26 @@ public <T extends Enum<T>> T getEnum(String name, T defaultValue) {
17861788
: Enum.valueOf(defaultValue.getDeclaringClass(), val);
17871789
}
17881790

1791+
/**
1792+
* Build an enumset from a comma separated list of values.
1793+
* Case independent.
1794+
* Special handling of "*" meaning: all values.
1795+
* @param key key to look for
1796+
* @param enumClass class of enum
1797+
* @param ignoreUnknown should unknown values raise an exception?
1798+
* @return a mutable set of the identified enum values declared in the configuration
1799+
* @param <E> enumeration type
1800+
* @throws IllegalArgumentException if one of the entries was unknown and ignoreUnknown is false,
1801+
* or there are two entries in the enum which differ only by case.
1802+
*/
1803+
public <E extends Enum<E>> EnumSet<E> getEnumSet(
1804+
final String key,
1805+
final Class<E> enumClass,
1806+
final boolean ignoreUnknown) throws IllegalArgumentException {
1807+
final String value = get(key, "");
1808+
return ConfigurationHelper.parseEnumSet(key, value, enumClass, ignoreUnknown);
1809+
}
1810+
17891811
enum ParsedTimeDuration {
17901812
NS {
17911813
TimeUnit unit() { return TimeUnit.NANOSECONDS; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.fs.impl;
20+
21+
import java.util.Arrays;
22+
import java.util.EnumSet;
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.Objects;
26+
import java.util.concurrent.atomic.AtomicBoolean;
27+
import java.util.stream.Collectors;
28+
import javax.annotation.Nullable;
29+
30+
import org.apache.hadoop.conf.Configuration;
31+
import org.apache.hadoop.fs.StreamCapabilities;
32+
import org.apache.hadoop.util.ConfigurationHelper;
33+
import org.apache.hadoop.util.Preconditions;
34+
35+
import static java.util.Objects.requireNonNull;
36+
import static org.apache.hadoop.util.ConfigurationHelper.mapEnumNamesToValues;
37+
38+
/**
39+
* A set of flags, constructed from a configuration option or from a string,
40+
* with the semantics of
41+
* {@link ConfigurationHelper#parseEnumSet(String, String, Class, boolean)}
42+
* and implementing {@link StreamCapabilities}.
43+
* <p>
44+
* Thread safety: there is no synchronization on a mutable {@code FlagSet}.
45+
* Once declared immutable, flags cannot be changed, so they
46+
* becomes implicitly thread-safe.
47+
*/
48+
public final class FlagSet<E extends Enum<E>> implements StreamCapabilities {
49+
50+
/**
51+
* Class of the enum.
52+
* Used for duplicating the flags as java type erasure
53+
* loses this information otherwise.
54+
*/
55+
private final Class<E> enumClass;
56+
57+
/**
58+
* Prefix for path capabilities probe.
59+
*/
60+
private final String prefix;
61+
62+
/**
63+
* Set of flags.
64+
*/
65+
private final EnumSet<E> flags;
66+
67+
/**
68+
* Is the set immutable?
69+
*/
70+
private final AtomicBoolean immutable = new AtomicBoolean(false);
71+
72+
/**
73+
* Mapping of prefixed flag names to enum values.
74+
*/
75+
private final Map<String, E> namesToValues;
76+
77+
/**
78+
* Create a FlagSet.
79+
* @param enumClass class of enum
80+
* @param prefix prefix (with trailing ".") for path capabilities probe
81+
* @param flags flags. A copy of these are made.
82+
*/
83+
private FlagSet(final Class<E> enumClass,
84+
final String prefix,
85+
@Nullable final EnumSet<E> flags) {
86+
this.enumClass = requireNonNull(enumClass, "null enumClass");
87+
this.prefix = requireNonNull(prefix, "null prefix");
88+
this.flags = flags != null
89+
? EnumSet.copyOf(flags)
90+
: EnumSet.noneOf(enumClass);
91+
this.namesToValues = mapEnumNamesToValues(prefix, enumClass);
92+
}
93+
94+
/**
95+
* Get a copy of the flags.
96+
* <p>
97+
* This is immutable.
98+
* @return the flags.
99+
*/
100+
public EnumSet<E> flags() {
101+
return EnumSet.copyOf(flags);
102+
}
103+
104+
/**
105+
* Probe for the FlagSet being empty.
106+
* @return true if there are no flags set.
107+
*/
108+
public boolean isEmpty() {
109+
return flags.isEmpty();
110+
}
111+
112+
/**
113+
* Is a flag enabled?
114+
* @param flag flag to check
115+
* @return true if it is in the set of enabled flags.
116+
*/
117+
public boolean enabled(final E flag) {
118+
return flags.contains(flag);
119+
}
120+
121+
/**
122+
* Check for mutability before any mutating operation.
123+
* @throws IllegalStateException if the set is still mutable
124+
*/
125+
private void checkMutable() {
126+
Preconditions.checkState(!immutable.get(),
127+
"FlagSet is immutable");
128+
}
129+
130+
/**
131+
* Enable a flag.
132+
* @param flag flag to enable.
133+
*/
134+
public void enable(final E flag) {
135+
checkMutable();
136+
flags.add(flag);
137+
}
138+
139+
/**
140+
* Disable a flag.
141+
* @param flag flag to disable
142+
*/
143+
public void disable(final E flag) {
144+
checkMutable();
145+
flags.remove(flag);
146+
}
147+
148+
/**
149+
* Set a flag to the chosen value.
150+
* @param flag flag
151+
* @param state true to enable, false to disable.
152+
*/
153+
public void set(final E flag, boolean state) {
154+
if (state) {
155+
enable(flag);
156+
} else {
157+
disable(flag);
158+
}
159+
}
160+
161+
/**
162+
* Is a flag enabled?
163+
* @param capability string to query the stream support for.
164+
* @return true if the capability maps to an enum value and
165+
* that value is set.
166+
*/
167+
@Override
168+
public boolean hasCapability(final String capability) {
169+
final E e = namesToValues.get(capability);
170+
return e != null && enabled(e);
171+
}
172+
173+
/**
174+
* Make immutable; no-op if already set.
175+
*/
176+
public void makeImmutable() {
177+
immutable.set(true);
178+
}
179+
180+
/**
181+
* Is the FlagSet immutable?
182+
* @return true iff the FlagSet is immutable.
183+
*/
184+
public boolean isImmutable() {
185+
return immutable.get();
186+
}
187+
188+
/**
189+
* Get the enum class.
190+
* @return the enum class.
191+
*/
192+
public Class<E> getEnumClass() {
193+
return enumClass;
194+
}
195+
196+
@Override
197+
public String toString() {
198+
return "{" +
199+
(flags.stream()
200+
.map(Enum::name)
201+
.collect(Collectors.joining(", ")))
202+
+ "}";
203+
}
204+
205+
/**
206+
* Generate the list of capabilities.
207+
* @return a possibly empty list.
208+
*/
209+
public List<String> pathCapabilities() {
210+
return namesToValues.keySet().stream()
211+
.filter(this::hasCapability)
212+
.collect(Collectors.toList());
213+
}
214+
215+
/**
216+
* Equality is based on the value of {@link #enumClass} and
217+
* {@link #prefix} and the contents of the set, which must match.
218+
* <p>
219+
* The immutability flag is not considered, nor is the
220+
* {@link #namesToValues} map, though as that is generated from
221+
* the enumeration and prefix, it is implicitly equal if the prefix
222+
* and enumClass fields are equal.
223+
* @param o other object
224+
* @return true iff the equality condition is met.
225+
*/
226+
@Override
227+
public boolean equals(final Object o) {
228+
if (this == o) {
229+
return true;
230+
}
231+
if (o == null || getClass() != o.getClass()) {
232+
return false;
233+
}
234+
FlagSet<?> flagSet = (FlagSet<?>) o;
235+
return Objects.equals(enumClass, flagSet.enumClass)
236+
&& Objects.equals(prefix, flagSet.prefix)
237+
&& Objects.equals(flags, flagSet.flags);
238+
}
239+
240+
/**
241+
* Hash code is based on the flags.
242+
* @return a hash code.
243+
*/
244+
@Override
245+
public int hashCode() {
246+
return Objects.hashCode(flags);
247+
}
248+
249+
/**
250+
* Create a copy of the FlagSet.
251+
* @return a new mutable instance with a separate copy of the flags
252+
*/
253+
public FlagSet<E> copy() {
254+
return new FlagSet<>(enumClass, prefix, flags);
255+
}
256+
257+
/**
258+
* Convert to a string which can be then set in a configuration.
259+
* This is effectively a marshalled form of the flags.
260+
* @return a comma separated list of flag names.
261+
*/
262+
public String toConfigurationString() {
263+
return flags.stream()
264+
.map(Enum::name)
265+
.collect(Collectors.joining(", "));
266+
}
267+
268+
/**
269+
* Create a FlagSet.
270+
* @param enumClass class of enum
271+
* @param prefix prefix (with trailing ".") for path capabilities probe
272+
* @param flags flags
273+
* @param <E> enum type
274+
* @return a mutable FlagSet
275+
*/
276+
public static <E extends Enum<E>> FlagSet<E> createFlagSet(
277+
final Class<E> enumClass,
278+
final String prefix,
279+
final EnumSet<E> flags) {
280+
return new FlagSet<>(enumClass, prefix, flags);
281+
}
282+
283+
/**
284+
* Create a FlagSet from a list of enum values.
285+
* @param enumClass class of enum
286+
* @param prefix prefix (with trailing ".") for path capabilities probe
287+
* @param enabled varags list of flags to enable.
288+
* @param <E> enum type
289+
* @return a mutable FlagSet
290+
*/
291+
@SafeVarargs
292+
public static <E extends Enum<E>> FlagSet<E> createFlagSet(
293+
final Class<E> enumClass,
294+
final String prefix,
295+
final E... enabled) {
296+
final FlagSet<E> flagSet = new FlagSet<>(enumClass, prefix, null);
297+
Arrays.stream(enabled).forEach(flag -> {
298+
if (flag != null) {
299+
flagSet.enable(flag);
300+
}
301+
});
302+
return flagSet;
303+
}
304+
305+
/**
306+
* Build a FlagSet from a comma separated list of values.
307+
* Case independent.
308+
* Special handling of "*" meaning: all values.
309+
* @param enumClass class of enum
310+
* @param conf configuration
311+
* @param key key to look for
312+
* @param ignoreUnknown should unknown values raise an exception?
313+
* @param <E> enumeration type
314+
* @return a mutable FlagSet
315+
* @throws IllegalArgumentException if one of the entries was unknown and ignoreUnknown is false,
316+
* or there are two entries in the enum which differ only by case.
317+
*/
318+
public static <E extends Enum<E>> FlagSet<E> buildFlagSet(
319+
final Class<E> enumClass,
320+
final Configuration conf,
321+
final String key,
322+
final boolean ignoreUnknown) {
323+
final EnumSet<E> flags = conf.getEnumSet(key, enumClass, ignoreUnknown);
324+
return createFlagSet(enumClass, key + ".", flags);
325+
}
326+
327+
}

0 commit comments

Comments
 (0)