code
stringlengths
67
466k
docstring
stringlengths
1
13.2k
public static <T> T instantiateUserCode(TaskConfig config, ClassLoader cl, Class<? super T> superClass) { try { T stub = config.<T>getStubWrapper(cl).getUserCodeObject(superClass, cl); // check if the class is a subclass, if the check is required if (superClass != null && !superClass.isAssignableFrom(stub.getClass())) { throw new RuntimeException("The class '" + stub.getClass().getName() + "' is not a subclass of '" + superClass.getName() + "' as is required."); } return stub; } catch (ClassCastException ccex) { throw new RuntimeException("The UDF class is not a proper subclass of " + superClass.getName(), ccex); } }
Instantiates a user code class from is definition in the task configuration. The class is instantiated without arguments using the null-ary constructor. Instantiation will fail if this constructor does not exist or is not public. @param <T> The generic type of the user code class. @param config The task configuration containing the class description. @param cl The class loader to be used to load the class. @param superClass The super class that the user code class extends or implements, for type checking. @return An instance of the user code class.
public PythonDataStream close_with(PythonDataStream<? extends DataStream<PyObject>> feedback_stream) { ((IterativeStream<PyObject>) this.stream).closeWith(feedback_stream.stream); return feedback_stream; }
A thin wrapper layer over {@link IterativeStream#closeWith(org.apache.flink.streaming.api.datastream.DataStream)} <p>Please note that this function works with {@link PythonDataStream} and thus wherever a DataStream is mentioned in the above {@link IterativeStream#closeWith(org.apache.flink.streaming.api.datastream.DataStream)} description, the user may regard it as {@link PythonDataStream} . @param feedback_stream {@link PythonDataStream} that will be used as input to the iteration head. @return The feedback stream.
@PublicEvolving public void setResources(ResourceSpec minResources, ResourceSpec preferredResources) { this.minResources = minResources; this.preferredResources = preferredResources; }
Sets the minimum and preferred resources for this contract instance. The resource denotes how many memories and cpu cores of the user function will be consumed during the execution. @param minResources The minimum resource of this operator. @param preferredResources The preferred resource of this operator.
@SuppressWarnings("unchecked") public static <T> Operator<T> createUnionCascade(List<? extends Operator<T>> operators) { return createUnionCascade((Operator<T>[]) operators.toArray(new Operator[operators.size()])); }
Takes a list of operators and creates a cascade of unions of this inputs, if needed. If not needed (there was only one operator in the list), then that operator is returned. @param operators The operators. @return The single operator or a cascade of unions of the operators.
public static <T> Operator<T> createUnionCascade(Operator<T>... operators) { return createUnionCascade(null, operators); }
Takes a list of operators and creates a cascade of unions of this inputs, if needed. If not needed (there was only one operator in the list), then that operator is returned. @param operators The operators. @return The single operator or a cascade of unions of the operators.
protected void runAsyncWithoutFencing(Runnable runnable) { if (rpcServer instanceof FencedMainThreadExecutable) { ((FencedMainThreadExecutable) rpcServer).runAsyncWithoutFencing(runnable); } else { throw new RuntimeException("FencedRpcEndpoint has not been started with a FencedMainThreadExecutable RpcServer."); } }
Run the given runnable in the main thread of the RpcEndpoint without checking the fencing token. This allows to run operations outside of the fencing token scope. @param runnable to execute in the main thread of the rpc endpoint without checking the fencing token.
protected <V> CompletableFuture<V> callAsyncWithoutFencing(Callable<V> callable, Time timeout) { if (rpcServer instanceof FencedMainThreadExecutable) { return ((FencedMainThreadExecutable) rpcServer).callAsyncWithoutFencing(callable, timeout); } else { throw new RuntimeException("FencedRpcEndpoint has not been started with a FencedMainThreadExecutable RpcServer."); } }
Run the given callable in the main thread of the RpcEndpoint without checking the fencing token. This allows to run operations outside of the fencing token scope. @param callable to run in the main thread of the rpc endpoint without checkint the fencing token. @param timeout for the operation. @return Future containing the callable result.
public static org.apache.hadoop.fs.Path toHadoopPath(Path path) { return new org.apache.hadoop.fs.Path(path.toUri()); }
------------------------------------------------------------------------
static FileSystemKind getKindForScheme(String scheme) { scheme = scheme.toLowerCase(Locale.US); if (scheme.startsWith("s3") || scheme.startsWith("emr") || scheme.startsWith("oss")) { // the Amazon S3 storage or Aliyun OSS storage return FileSystemKind.OBJECT_STORE; } else if (scheme.startsWith("http") || scheme.startsWith("ftp")) { // file servers instead of file systems // they might actually be consistent, but we have no hard guarantees // currently to rely on that return FileSystemKind.OBJECT_STORE; } else { // the remainder should include hdfs, kosmos, ceph, ... // this also includes federated HDFS (viewfs). return FileSystemKind.FILE_SYSTEM; } }
Gets the kind of the file system from its scheme. <p>Implementation note: Initially, especially within the Flink 1.3.x line (in order to not break backwards compatibility), we must only label file systems as 'inconsistent' or as 'not proper filesystems' if we are sure about it. Otherwise, we cause regression for example in the performance and cleanup handling of checkpoints. For that reason, we initially mark some filesystems as 'eventually consistent' or as 'object stores', and leave the others as 'consistent file systems'.
private BT advanceAndRead(BT reuse) throws IOException { if (!partitions.hasNext()) { return null; } currentPartition = partitions.next(); currentPartition.setReadPosition(0); try { reuse = serializer.deserialize(reuse, currentPartition); } catch (EOFException e) { reuse = advanceAndRead(reuse); } return reuse; }
/* jump to the next partition and continue reading from that
void updateSummary(CompletedCheckpointStats completed) { stateSize.add(completed.getStateSize()); duration.add(completed.getEndToEndDuration()); alignmentBuffered.add(completed.getAlignmentBuffered()); }
Updates the summary with the given completed checkpoint. @param completed Completed checkpoint to update the summary with.
private int getPartitioningFanOutNoEstimates() { return Math.max(11, findSmallerPrime((int) Math.min(buildRowCount * avgRecordLen / (10 * segmentSize), MAX_NUM_PARTITIONS))); }
Gets the number of partitions to be used for an initial hash-table.
public MemorySegment getNextBuffer() { // check if the list directly offers memory int s = this.availableMemory.size(); if (s > 0) { return this.availableMemory.remove(s - 1); } // check if there are write behind buffers that actually are to be used for the hash table if (this.buildSpillRetBufferNumbers > 0) { // grab at least one, no matter what MemorySegment toReturn; try { toReturn = this.buildSpillReturnBuffers.take(); } catch (InterruptedException iex) { throw new RuntimeException("Hybrid Hash Join was interrupted while taking a buffer."); } this.buildSpillRetBufferNumbers--; // grab as many more buffers as are available directly MemorySegment currBuff; while (this.buildSpillRetBufferNumbers > 0 && (currBuff = this.buildSpillReturnBuffers.poll()) != null) { this.availableMemory.add(currBuff); this.buildSpillRetBufferNumbers--; } return toReturn; } else { if (reservedNumBuffers + allocatedFloatingNum >= maxNumBuffers) { //no more memory. return null; } else { int requestNum = Math.min(perRequestNumBuffers, maxNumBuffers - reservedNumBuffers - allocatedFloatingNum); //apply for much more memory. try { List<MemorySegment> allocates = memManager.allocatePages(owner, requestNum); this.availableMemory.addAll(allocates); allocatedFloatingNum += allocates.size(); allocates.clear(); LOG.info("{} allocate {} floating segments successfully!", owner, requestNum); } catch (MemoryAllocationException e) { LOG.warn("BinaryHashMap can't allocate {} floating pages, and now used {} pages", requestNum, reservedNumBuffers + allocatedFloatingNum, e); //can't allocate much more memory. return null; } if (this.availableMemory.size() > 0) { return this.availableMemory.remove(this.availableMemory.size() - 1); } else { return null; } } } }
Gets the next buffer to be used with the hash-table, either for an in-memory partition, or for the table buckets. This method returns <tt>null</tt>, if no more buffer is available. Spilling a partition may free new buffers then. @return The next buffer to be used by the hash-table, or null, if no buffer remains.
public MemorySegment[] getNextBuffers(int bufferSize) { MemorySegment[] memorySegments = new MemorySegment[bufferSize]; for (int i = 0; i < bufferSize; i++) { MemorySegment nextBuffer = getNextBuffer(); if (nextBuffer == null) { throw new RuntimeException("No enough buffers!"); } memorySegments[i] = nextBuffer; } return memorySegments; }
Bulk memory acquisition. NOTE: Failure to get memory will throw an exception.
public void ensureNumBuffersReturned(final int minRequiredAvailable) { if (minRequiredAvailable > this.availableMemory.size() + this.buildSpillRetBufferNumbers) { throw new IllegalArgumentException("More buffers requested available than totally available."); } try { while (this.availableMemory.size() < minRequiredAvailable) { this.availableMemory.add(this.buildSpillReturnBuffers.take()); this.buildSpillRetBufferNumbers--; } } catch (InterruptedException iex) { throw new RuntimeException("Hash Join was interrupted."); } }
This method makes sure that at least a certain number of memory segments is in the list of free segments. Free memory can be in the list of free segments, or in the return-queue where segments used to write behind are put. The number of segments that are in that return-queue, but are actually reclaimable is tracked. This method makes sure at least a certain number of buffers is reclaimed. @param minRequiredAvailable The minimum number of buffers that needs to be reclaimed.
public void close() { // make sure that we close only once if (!this.closed.compareAndSet(false, true)) { return; } // clear the current probe side channel, if there is one if (this.currentSpilledProbeSide != null) { try { this.currentSpilledProbeSide.getChannel().closeAndDelete(); } catch (Throwable t) { LOG.warn("Could not close and delete the temp file for the current spilled partition probe side.", t); } } // clear the memory in the partitions clearPartitions(); // return the write-behind buffers for (int i = 0; i < this.buildSpillRetBufferNumbers; i++) { try { this.availableMemory.add(this.buildSpillReturnBuffers.take()); } catch (InterruptedException iex) { throw new RuntimeException("Hashtable closing was interrupted"); } } this.buildSpillRetBufferNumbers = 0; }
Closes the hash table. This effectively releases all internal structures and closes all open files and removes them. The call to this method is valid both as a cleanup after the complete inputs were properly processed, and as an cancellation call, which cleans up all resources that are currently held by the hash join.
public void freeCurrent() { int beforeReleaseNum = availableMemory.size(); memManager.release(availableMemory); allocatedFloatingNum -= (beforeReleaseNum - availableMemory.size()); }
Free the memory not used.
public static int hash(int hashCode, int level) { final int rotation = level * 11; int code = Integer.rotateLeft(hashCode, rotation); return code >= 0 ? code : -(code + 1); }
The level parameter is needed so that we can have different hash functions when we recursively apply the partitioning, so that the working set eventually fits into memory.
public static void addDeprecations(DeprecationDelta[] deltas) { DeprecationContext prev, next; do { prev = deprecationContext.get(); next = new DeprecationContext(prev, deltas); } while (!deprecationContext.compareAndSet(prev, next)); }
Adds a set of deprecated keys to the global deprecations. This method is lockless. It works by means of creating a new DeprecationContext based on the old one, and then atomically swapping in the new context. If someone else updated the context in between us reading the old context and swapping in the new one, we try again until we win the race. @param deltas The deprecations to add.
@Deprecated public static void addDeprecation(String key, String[] newKeys, String customMessage) { addDeprecations(new DeprecationDelta[] { new DeprecationDelta(key, newKeys, customMessage) }); }
Adds the deprecated key to the global deprecation map. It does not override any existing entries in the deprecation map. This is to be used only by the developers in order to add deprecation of keys, and attempts to call this method after loading resources once, would lead to <tt>UnsupportedOperationException</tt> If a key is deprecated in favor of multiple keys, they are all treated as aliases of each other, and setting any one of them resets all the others to the new value. If you have multiple deprecation entries to add, it is more efficient to use #addDeprecations(DeprecationDelta[] deltas) instead. @param key @param newKeys @param customMessage @deprecated use {@link #addDeprecation(String key, String newKey, String customMessage)} instead
public static void addDeprecation(String key, String newKey, String customMessage) { addDeprecation(key, new String[] {newKey}, customMessage); }
Adds the deprecated key to the global deprecation map. It does not override any existing entries in the deprecation map. This is to be used only by the developers in order to add deprecation of keys, and attempts to call this method after loading resources once, would lead to <tt>UnsupportedOperationException</tt> If you have multiple deprecation entries to add, it is more efficient to use #addDeprecations(DeprecationDelta[] deltas) instead. @param key @param newKey @param customMessage
@Deprecated public static void addDeprecation(String key, String[] newKeys) { addDeprecation(key, newKeys, null); }
Adds the deprecated key to the global deprecation map when no custom message is provided. It does not override any existing entries in the deprecation map. This is to be used only by the developers in order to add deprecation of keys, and attempts to call this method after loading resources once, would lead to <tt>UnsupportedOperationException</tt> If a key is deprecated in favor of multiple keys, they are all treated as aliases of each other, and setting any one of them resets all the others to the new value. If you have multiple deprecation entries to add, it is more efficient to use #addDeprecations(DeprecationDelta[] deltas) instead. @param key Key that is to be deprecated @param newKeys list of keys that take up the values of deprecated key @deprecated use {@link #addDeprecation(String key, String newKey)} instead
public static void addDeprecation(String key, String newKey) { addDeprecation(key, new String[] {newKey}, null); }
Adds the deprecated key to the global deprecation map when no custom message is provided. It does not override any existing entries in the deprecation map. This is to be used only by the developers in order to add deprecation of keys, and attempts to call this method after loading resources once, would lead to <tt>UnsupportedOperationException</tt> If you have multiple deprecation entries to add, it is more efficient to use #addDeprecations(DeprecationDelta[] deltas) instead. @param key Key that is to be deprecated @param newKey key that takes up the value of deprecated key
public void setDeprecatedProperties() { DeprecationContext deprecations = deprecationContext.get(); Properties props = getProps(); Properties overlay = getOverlay(); for (Map.Entry<String, DeprecatedKeyInfo> entry : deprecations.getDeprecatedKeyMap().entrySet()) { String depKey = entry.getKey(); if (!overlay.contains(depKey)) { for (String newKey : entry.getValue().newKeys) { String val = overlay.getProperty(newKey); if (val != null) { props.setProperty(depKey, val); overlay.setProperty(depKey, val); break; } } } } }
Sets all deprecated properties that are not currently set but have a corresponding new property that is set. Useful for iterating the properties when all deprecated properties for currently set properties need to be present.
private String[] handleDeprecation(DeprecationContext deprecations, String name) { if (null != name) { name = name.trim(); } // Initialize the return value with requested name String[] names = new String[]{name}; // Deprecated keys are logged once and an updated names are returned DeprecatedKeyInfo keyInfo = deprecations.getDeprecatedKeyMap().get(name); if (keyInfo != null) { if (!keyInfo.getAndSetAccessed()) { logDeprecation(keyInfo.getWarningMessage(name)); } // Override return value for deprecated keys names = keyInfo.newKeys; } // If there are no overlay values we can return early Properties overlayProperties = getOverlay(); if (overlayProperties.isEmpty()) { return names; } // Update properties and overlays with reverse lookup values for (String n : names) { String deprecatedKey = deprecations.getReverseDeprecatedKeyMap().get(n); if (deprecatedKey != null && !overlayProperties.containsKey(n)) { String deprecatedValue = overlayProperties.getProperty(deprecatedKey); if (deprecatedValue != null) { getProps().setProperty(n, deprecatedValue); overlayProperties.setProperty(n, deprecatedValue); } } } return names; }
Checks for the presence of the property <code>name</code> in the deprecation map. Returns the first of the list of new keys if present in the deprecation map or the <code>name</code> itself. If the property is not presently set but the property map contains an entry for the deprecated key, the value of the deprecated key is set as the value for the provided property name. @param deprecations deprecation context @param name the property name @return the first property in the list of properties mapping the <code>name</code> or the <code>name</code> itself.
public static synchronized void reloadExistingConfigurations() { if (LOG.isDebugEnabled()) { LOG.debug("Reloading " + REGISTRY.keySet().size() + " existing configurations"); } for (Configuration conf : REGISTRY.keySet()) { conf.reloadConfiguration(); } }
Reload existing configuration instances.
public static synchronized void addDefaultResource(String name) { if(!defaultResources.contains(name)) { defaultResources.add(name); for(Configuration conf : REGISTRY.keySet()) { if(conf.loadDefaults) { conf.reloadConfiguration(); } } } }
Add a default resource. Resources are loaded in the order of the resources added. @param name file name. File should be present in the classpath.
private static int[] findSubVariable(String eval) { int[] result = {-1, -1}; int matchStart; int leftBrace; // scanning for a brace first because it's less frequent than $ // that can occur in nested class names // match_loop: for (matchStart = 1, leftBrace = eval.indexOf('{', matchStart); // minimum left brace position (follows '$') leftBrace > 0 // right brace of a smallest valid expression "${c}" && leftBrace + "{c".length() < eval.length(); leftBrace = eval.indexOf('{', matchStart)) { int matchedLen = 0; if (eval.charAt(leftBrace - 1) == '$') { int subStart = leftBrace + 1; // after '{' for (int i = subStart; i < eval.length(); i++) { switch (eval.charAt(i)) { case '}': if (matchedLen > 0) { // match result[SUB_START_IDX] = subStart; result[SUB_END_IDX] = subStart + matchedLen; break match_loop; } // fall through to skip 1 char case ' ': case '$': matchStart = i + 1; continue match_loop; default: matchedLen++; } } // scanned from "${" to the end of eval, and no reset via ' ', '$': // no match! break match_loop; } else { // not a start of a variable // matchStart = leftBrace + 1; } } return result; }
This is a manual implementation of the following regex "\\$\\{[^\\}\\$\u0020]+\\}". It can be 15x more efficient than a regex matcher as demonstrated by HADOOP-11506. This is noticeable with Hadoop apps building on the assumption Configuration#get is an O(1) hash table lookup, especially when the eval is a long string. @param eval a string that may contain variables requiring expansion. @return a 2-element int array res such that eval.substring(res[0], res[1]) is "var" for the left-most occurrence of ${var} in eval. If no variable is found -1, -1 is returned.
private String substituteVars(String expr) { if (expr == null) { return null; } String eval = expr; for(int s = 0; s < MAX_SUBST; s++) { final int[] varBounds = findSubVariable(eval); if (varBounds[SUB_START_IDX] == -1) { return eval; } final String var = eval.substring(varBounds[SUB_START_IDX], varBounds[SUB_END_IDX]); String val = null; if (!restrictSystemProps) { try { if (var.startsWith("env.") && 4 < var.length()) { String v = var.substring(4); int i = 0; for (; i < v.length(); i++) { char c = v.charAt(i); if (c == ':' && i < v.length() - 1 && v.charAt(i + 1) == '-') { val = getenv(v.substring(0, i)); if (val == null || val.length() == 0) { val = v.substring(i + 2); } break; } else if (c == '-') { val = getenv(v.substring(0, i)); if (val == null) { val = v.substring(i + 1); } break; } } if (i == v.length()) { val = getenv(v); } } else { val = getProperty(var); } } catch (SecurityException se) { LOG.warn("Unexpected SecurityException in Configuration", se); } } if (val == null) { val = getRaw(var); } if (val == null) { return eval; // return literal ${var}: var is unbound } final int dollar = varBounds[SUB_START_IDX] - "${".length(); final int afterRightBrace = varBounds[SUB_END_IDX] + "}".length(); final String refVar = eval.substring(dollar, afterRightBrace); // detect self-referential values if (val.contains(refVar)) { return expr; // return original expression if there is a loop } // substitute eval = eval.substring(0, dollar) + val + eval.substring(afterRightBrace); } throw new IllegalStateException("Variable substitution depth too large: " + MAX_SUBST + " " + expr); }
Attempts to repeatedly expand the value {@code expr} by replacing the left-most substring of the form "${var}" in the following precedence order <ol> <li>by the value of the environment variable "var" if defined</li> <li>by the value of the Java system property "var" if defined</li> <li>by the value of the configuration key "var" if defined</li> </ol> If var is unbounded the current state of expansion "prefix${var}suffix" is returned. <p> This function also detects self-referential substitutions, i.e. <pre> {@code foo.bar = ${foo.bar} } </pre> If a cycle is detected then the original expr is returned. Loops involving multiple substitutions are not detected. @param expr the literal value of a config key @return null if expr is null, otherwise the value resulting from expanding expr using the algorithm above. @throws IllegalArgumentException when more than {@link Configuration#MAX_SUBST} replacements are required
public String get(String name) { String[] names = handleDeprecation(deprecationContext.get(), name); String result = null; for(String n : names) { result = substituteVars(getProps().getProperty(n)); } return result; }
Get the value of the <code>name</code> property, <code>null</code> if no such property exists. If the key is deprecated, it returns the value of the first key which replaces the deprecated key and is not null. Values are processed for <a href="#VariableExpansion">variable expansion</a> before being returned. @param name the property name, will be trimmed before get value. @return the value of the <code>name</code> or its replacing property, or null if no such property exists.
@VisibleForTesting public boolean onlyKeyExists(String name) { String[] names = handleDeprecation(deprecationContext.get(), name); for(String n : names) { if ( getProps().getProperty(n,DEFAULT_STRING_CHECK) .equals(DEFAULT_STRING_CHECK) ) { return true; } } return false; }
Return existence of the <code>name</code> property, but only for names which have no valid value, usually non-existent or commented out in XML. @param name the property name @return true if the property <code>name</code> exists without value
public String getTrimmed(String name) { String value = get(name); if (null == value) { return null; } else { return value.trim(); } }
Get the value of the <code>name</code> property as a trimmed <code>String</code>, <code>null</code> if no such property exists. If the key is deprecated, it returns the value of the first key which replaces the deprecated key and is not null Values are processed for <a href="#VariableExpansion">variable expansion</a> before being returned. @param name the property name. @return the value of the <code>name</code> or its replacing property, or null if no such property exists.
public String getTrimmed(String name, String defaultValue) { String ret = getTrimmed(name); return ret == null ? defaultValue : ret; }
Get the value of the <code>name</code> property as a trimmed <code>String</code>, <code>defaultValue</code> if no such property exists. See @{Configuration#getTrimmed} for more details. @param name the property name. @param defaultValue the property default value. @return the value of the <code>name</code> or defaultValue if it is not set.
private String[] getAlternativeNames(String name) { String altNames[] = null; DeprecatedKeyInfo keyInfo = null; DeprecationContext cur = deprecationContext.get(); String depKey = cur.getReverseDeprecatedKeyMap().get(name); if(depKey != null) { keyInfo = cur.getDeprecatedKeyMap().get(depKey); if(keyInfo.newKeys.length > 0) { if(getProps().containsKey(depKey)) { //if deprecated key is previously set explicitly List<String> list = new ArrayList<String>(); list.addAll(Arrays.asList(keyInfo.newKeys)); list.add(depKey); altNames = list.toArray(new String[list.size()]); } else { altNames = keyInfo.newKeys; } } } return altNames; }
Returns alternative names (non-deprecated keys or previously-set deprecated keys) for a given non-deprecated key. If the given key is deprecated, return null. @param name property name. @return alternative names.
public synchronized void unset(String name) { String[] names = null; if (!isDeprecated(name)) { names = getAlternativeNames(name); if(names == null) { names = new String[]{name}; } } else { names = handleDeprecation(deprecationContext.get(), name); } for(String n: names) { getOverlay().remove(n); getProps().remove(n); } }
Unset a previously set property.
public int getInt(String name, int defaultValue) { String valueString = getTrimmed(name); if (valueString == null) return defaultValue; String hexString = getHexDigits(valueString); if (hexString != null) { return Integer.parseInt(hexString, 16); } return Integer.parseInt(valueString); }
Get the value of the <code>name</code> property as an <code>int</code>. If no such property exists, the provided default value is returned, or if the specified value is not a valid <code>int</code>, then an error is thrown. @param name property name. @param defaultValue default value. @throws NumberFormatException when the value is invalid @return property value as an <code>int</code>, or <code>defaultValue</code>.
public int[] getInts(String name) { String[] strings = getTrimmedStrings(name); int[] ints = new int[strings.length]; for (int i = 0; i < strings.length; i++) { ints[i] = Integer.parseInt(strings[i]); } return ints; }
Get the value of the <code>name</code> property as a set of comma-delimited <code>int</code> values. If no such property exists, an empty array is returned. @param name property name @return property value interpreted as an array of comma-delimited <code>int</code> values
public long getLong(String name, long defaultValue) { String valueString = getTrimmed(name); if (valueString == null) return defaultValue; String hexString = getHexDigits(valueString); if (hexString != null) { return Long.parseLong(hexString, 16); } return Long.parseLong(valueString); }
Get the value of the <code>name</code> property as a <code>long</code>. If no such property exists, the provided default value is returned, or if the specified value is not a valid <code>long</code>, then an error is thrown. @param name property name. @param defaultValue default value. @throws NumberFormatException when the value is invalid @return property value as a <code>long</code>, or <code>defaultValue</code>.
public long getLongBytes(String name, long defaultValue) { String valueString = getTrimmed(name); if (valueString == null) return defaultValue; return StringUtils.TraditionalBinaryPrefix.string2long(valueString); }
Get the value of the <code>name</code> property as a <code>long</code> or human readable format. If no such property exists, the provided default value is returned, or if the specified value is not a valid <code>long</code> or human readable format, then an error is thrown. You can use the following suffix (case insensitive): k(kilo), m(mega), g(giga), t(tera), p(peta), e(exa) @param name property name. @param defaultValue default value. @throws NumberFormatException when the value is invalid @return property value as a <code>long</code>, or <code>defaultValue</code>.
public double getDouble(String name, double defaultValue) { String valueString = getTrimmed(name); if (valueString == null) return defaultValue; return Double.parseDouble(valueString); }
Get the value of the <code>name</code> property as a <code>double</code>. If no such property exists, the provided default value is returned, or if the specified value is not a valid <code>double</code>, then an error is thrown. @param name property name. @param defaultValue default value. @throws NumberFormatException when the value is invalid @return property value as a <code>double</code>, or <code>defaultValue</code>.
public boolean getBoolean(String name, boolean defaultValue) { String valueString = getTrimmed(name); if (null == valueString || valueString.isEmpty()) { return defaultValue; } if (StringUtils.equalsIgnoreCase("true", valueString)) return true; else if (StringUtils.equalsIgnoreCase("false", valueString)) return false; else return defaultValue; }
Get the value of the <code>name</code> property as a <code>boolean</code>. If no such property is specified, or if the specified value is not a valid <code>boolean</code>, then <code>defaultValue</code> is returned. @param name property name. @param defaultValue default value. @return property value as a <code>boolean</code>, or <code>defaultValue</code>.
public <T extends Enum<T>> void setEnum(String name, T value) { set(name, value.toString()); }
Set the value of the <code>name</code> property to the given type. This is equivalent to <code>set(&lt;name&gt;, value.toString())</code>. @param name property name @param value new value
public void setTimeDuration(String name, long value, TimeUnit unit) { set(name, value + ParsedTimeDuration.unitFor(unit).suffix()); }
Set the value of <code>name</code> to the given time duration. This is equivalent to <code>set(&lt;name&gt;, value + &lt;time suffix&gt;)</code>. @param name Property name @param value Time duration @param unit Unit of time
public long getTimeDuration(String name, long defaultValue, TimeUnit unit) { String vStr = get(name); if (null == vStr) { return defaultValue; } else { return getTimeDurationHelper(name, vStr, unit); } }
Return time duration in the given time unit. Valid units are encoded in properties as suffixes: nanoseconds (ns), microseconds (us), milliseconds (ms), seconds (s), minutes (m), hours (h), and days (d). @param name Property name @param defaultValue Value returned if no mapping exists. @param unit Unit to convert the stored property, if it exists. @throws NumberFormatException If the property stripped of its unit is not a number
public long getTimeDurationHelper(String name, String vStr, TimeUnit unit) { vStr = vStr.trim(); vStr = StringUtils.toLowerCase(vStr); ParsedTimeDuration vUnit = ParsedTimeDuration.unitFor(vStr); if (null == vUnit) { logDeprecation("No unit for " + name + "(" + vStr + ") assuming " + unit); vUnit = ParsedTimeDuration.unitFor(unit); } else { vStr = vStr.substring(0, vStr.lastIndexOf(vUnit.suffix())); } long raw = Long.parseLong(vStr); long converted = unit.convert(raw, vUnit.unit()); if (vUnit.unit().convert(converted, unit) < raw) { logDeprecation("Possible loss of precision converting " + vStr + vUnit.suffix() + " to " + unit + " for " + name); } return converted; }
Return time duration in the given time unit. Valid units are encoded in properties as suffixes: nanoseconds (ns), microseconds (us), milliseconds (ms), seconds (s), minutes (m), hours (h), and days (d). @param name Property name @param vStr The string value with time unit suffix to be converted. @param unit Unit to convert the stored property, if it exists.
public double getStorageSize(String name, String defaultValue, StorageUnit targetUnit) { Preconditions.checkState(isNotBlank(name), "Key cannot be blank."); String vString = get(name); if (isBlank(vString)) { vString = defaultValue; } // Please note: There is a bit of subtlety here. If the user specifies // the default unit as "1GB", but the requested unit is MB, we will return // the format in MB even thought the default string is specified in GB. // Converts a string like "1GB" to to unit specified in targetUnit. StorageSize measure = StorageSize.parse(vString); return convertStorageUnit(measure.getValue(), measure.getUnit(), targetUnit); }
Gets the Storage Size from the config, or returns the defaultValue. The unit of return value is specified in target unit. @param name - Key Name @param defaultValue - Default Value -- e.g. 100MB @param targetUnit - The units that we want result to be in. @return double -- formatted in target Units
public void setStorageSize(String name, double value, StorageUnit unit) { set(name, value + unit.getShortName()); }
Sets Storage Size for the specified key. @param name - Key to set. @param value - The numeric value to set. @param unit - Storage Unit to be used.
private double convertStorageUnit(double value, StorageUnit sourceUnit, StorageUnit targetUnit) { double byteValue = sourceUnit.toBytes(value); return targetUnit.fromBytes(byteValue); }
convert the value from one storage unit to another. @param value - value @param sourceUnit - Source unit to convert from @param targetUnit - target unit. @return double.
public Pattern getPattern(String name, Pattern defaultValue) { String valString = get(name); if (null == valString || valString.isEmpty()) { return defaultValue; } try { return Pattern.compile(valString); } catch (PatternSyntaxException pse) { LOG.warn("Regular expression '" + valString + "' for property '" + name + "' not valid. Using default", pse); return defaultValue; } }
Get the value of the <code>name</code> property as a <code>Pattern</code>. If no such property is specified, or if the specified value is not a valid <code>Pattern</code>, then <code>DefaultValue</code> is returned. Note that the returned value is NOT trimmed by this method. @param name property name @param defaultValue default value @return property value as a compiled Pattern, or defaultValue
@InterfaceStability.Unstable public synchronized String[] getPropertySources(String name) { if (properties == null) { // If properties is null, it means a resource was newly added // but the props were cleared so as to load it upon future // requests. So lets force a load by asking a properties list. getProps(); } // Return a null right away if our properties still // haven't loaded or the resource mapping isn't defined if (properties == null || updatingResource == null) { return null; } else { String[] source = updatingResource.get(name); if(source == null) { return null; } else { return Arrays.copyOf(source, source.length); } } }
Gets information about why a property was set. Typically this is the path to the resource objects (file, URL, etc.) the property came from, but it can also indicate that it was set programmatically, or because of the command line. @param name - The property name to get the source of. @return null - If the property or its source wasn't found. Otherwise, returns a list of the sources of the resource. The older sources are the first ones in the list. So for example if a configuration is set from the command line, and then written out to a file that is read back in the first entry would indicate that it was set from the command line, while the second one would indicate the file that the new configuration was read in from.
public IntegerRanges getRange(String name, String defaultValue) { return new IntegerRanges(get(name, defaultValue)); }
Parse the given attribute as a set of integer ranges @param name the attribute name @param defaultValue the default value if it is not set @return a new set of ranges from the configured value
public Collection<String> getStringCollection(String name) { String valueString = get(name); return StringUtils.getStringCollection(valueString); }
Get the comma delimited values of the <code>name</code> property as a collection of <code>String</code>s. If no such property is specified then empty collection is returned. <p> This is an optimized version of {@link #getStrings(String)} @param name property name. @return property value as a collection of <code>String</code>s.
public Collection<String> getTrimmedStringCollection(String name) { String valueString = get(name); if (null == valueString) { Collection<String> empty = new ArrayList<String>(); return empty; } return StringUtils.getTrimmedStringCollection(valueString); }
Get the comma delimited values of the <code>name</code> property as a collection of <code>String</code>s, trimmed of the leading and trailing whitespace. If no such property is specified then empty <code>Collection</code> is returned. @param name property name. @return property value as a collection of <code>String</code>s, or empty <code>Collection</code>
public String[] getTrimmedStrings(String name) { String valueString = get(name); return StringUtils.getTrimmedStrings(valueString); }
Get the comma delimited values of the <code>name</code> property as an array of <code>String</code>s, trimmed of the leading and trailing whitespace. If no such property is specified then an empty array is returned. @param name property name. @return property value as an array of trimmed <code>String</code>s, or empty array.
public String[] getTrimmedStrings(String name, String... defaultValue) { String valueString = get(name); if (null == valueString) { return defaultValue; } else { return StringUtils.getTrimmedStrings(valueString); } }
Get the comma delimited values of the <code>name</code> property as an array of <code>String</code>s, trimmed of the leading and trailing whitespace. If no such property is specified then default value is returned. @param name property name. @param defaultValue The default value @return property value as an array of trimmed <code>String</code>s, or default value.
public void setStrings(String name, String... values) { set(name, StringUtils.arrayToString(values)); }
Set the array of string values for the <code>name</code> property as as comma delimited values. @param name property name. @param values The values
public char[] getPassword(String name) throws IOException { char[] pass = null; pass = getPasswordFromCredentialProviders(name); if (pass == null) { pass = getPasswordFromConfig(name); } return pass; }
Get the value for a known password configuration element. In order to enable the elimination of clear text passwords in config, this method attempts to resolve the property name as an alias through the CredentialProvider API and conditionally fallsback to config. @param name property name @return password
private CredentialEntry getCredentialEntry(CredentialProvider provider, String name) throws IOException { CredentialEntry entry = provider.getCredentialEntry(name); if (entry != null) { return entry; } // The old name is stored in the credential provider. String oldName = getDeprecatedKey(name); if (oldName != null) { entry = provider.getCredentialEntry(oldName); if (entry != null) { logDeprecationOnce(oldName, provider.toString()); return entry; } } // The name is deprecated. DeprecatedKeyInfo keyInfo = getDeprecatedKeyInfo(name); if (keyInfo != null && keyInfo.newKeys != null) { for (String newName : keyInfo.newKeys) { entry = provider.getCredentialEntry(newName); if (entry != null) { logDeprecationOnce(name, null); return entry; } } } return null; }
Get the credential entry by name from a credential provider. Handle key deprecation. @param provider a credential provider @param name alias of the credential @return the credential entry or null if not found
protected char[] getPasswordFromConfig(String name) { char[] pass = null; if (getBoolean(CredentialProvider.CLEAR_TEXT_FALLBACK, CommonConfigurationKeysPublic. HADOOP_SECURITY_CREDENTIAL_CLEAR_TEXT_FALLBACK_DEFAULT)) { String passStr = get(name); if (passStr != null) { pass = passStr.toCharArray(); } } return pass; }
Fallback to clear text passwords in configuration. @param name @return clear text password or null
public InetSocketAddress getSocketAddr( String hostProperty, String addressProperty, String defaultAddressValue, int defaultPort) { InetSocketAddress bindAddr = getSocketAddr( addressProperty, defaultAddressValue, defaultPort); final String host = get(hostProperty); if (host == null || host.isEmpty()) { return bindAddr; } return NetUtils.createSocketAddr( host, bindAddr.getPort(), hostProperty); }
Get the socket address for <code>hostProperty</code> as a <code>InetSocketAddress</code>. If <code>hostProperty</code> is <code>null</code>, <code>addressProperty</code> will be used. This is useful for cases where we want to differentiate between host bind address and address clients should use to establish connection. @param hostProperty bind host property name. @param addressProperty address property name. @param defaultAddressValue the default value @param defaultPort the default port @return InetSocketAddress
public InetSocketAddress getSocketAddr( String name, String defaultAddress, int defaultPort) { final String address = getTrimmed(name, defaultAddress); return NetUtils.createSocketAddr(address, defaultPort, name); }
Get the socket address for <code>name</code> property as a <code>InetSocketAddress</code>. @param name property name. @param defaultAddress the default value @param defaultPort the default port @return InetSocketAddress
public void setSocketAddr(String name, InetSocketAddress addr) { set(name, NetUtils.getHostPortString(addr)); }
Set the socket address for the <code>name</code> property as a <code>host:port</code>.
public InetSocketAddress updateConnectAddr( String hostProperty, String addressProperty, String defaultAddressValue, InetSocketAddress addr) { final String host = get(hostProperty); final String connectHostPort = getTrimmed(addressProperty, defaultAddressValue); if (host == null || host.isEmpty() || connectHostPort == null || connectHostPort.isEmpty()) { //not our case, fall back to original logic return updateConnectAddr(addressProperty, addr); } final String connectHost = connectHostPort.split(":")[0]; // Create connect address using client address hostname and server port. return updateConnectAddr(addressProperty, NetUtils.createSocketAddrForHost( connectHost, addr.getPort())); }
Set the socket address a client can use to connect for the <code>name</code> property as a <code>host:port</code>. The wildcard address is replaced with the local host's address. If the host and address properties are configured the host component of the address will be combined with the port component of the addr to generate the address. This is to allow optional control over which host name is used in multi-home bind-host cases where a host can have multiple names @param hostProperty the bind-host configuration name @param addressProperty the service address configuration name @param defaultAddressValue the service default address configuration value @param addr InetSocketAddress of the service listener @return InetSocketAddress for clients to connect
public InetSocketAddress updateConnectAddr(String name, InetSocketAddress addr) { final InetSocketAddress connectAddr = NetUtils.getConnectAddress(addr); setSocketAddr(name, connectAddr); return connectAddr; }
Set the socket address a client can use to connect for the <code>name</code> property as a <code>host:port</code>. The wildcard address is replaced with the local host's address. @param name property name. @param addr InetSocketAddress of a listener to store in the given property @return InetSocketAddress for clients to connect
public Class<?> getClassByName(String name) throws ClassNotFoundException { Class<?> ret = getClassByNameOrNull(name); if (ret == null) { throw new ClassNotFoundException("Class " + name + " not found"); } return ret; }
Load a class by name. @param name the class name. @return the class object. @throws ClassNotFoundException if the class is not found.
public Class<?> getClassByNameOrNull(String name) { Map<String, WeakReference<Class<?>>> map; synchronized (CACHE_CLASSES) { map = CACHE_CLASSES.get(classLoader); if (map == null) { map = Collections.synchronizedMap( new WeakHashMap<String, WeakReference<Class<?>>>()); CACHE_CLASSES.put(classLoader, map); } } Class<?> clazz = null; WeakReference<Class<?>> ref = map.get(name); if (ref != null) { clazz = ref.get(); } if (clazz == null) { try { clazz = Class.forName(name, true, classLoader); } catch (ClassNotFoundException e) { // Leave a marker that the class isn't found map.put(name, new WeakReference<Class<?>>(NEGATIVE_CACHE_SENTINEL)); return null; } // two putters can race here, but they'll put the same class map.put(name, new WeakReference<Class<?>>(clazz)); return clazz; } else if (clazz == NEGATIVE_CACHE_SENTINEL) { return null; // not found } else { // cache hit return clazz; } }
Load a class by name, returning null rather than throwing an exception if it couldn't be loaded. This is to avoid the overhead of creating an exception. @param name the class name @return the class object, or null if it could not be found.
public <U> Class<? extends U> getClass(String name, Class<? extends U> defaultValue, Class<U> xface) { try { Class<?> theClass = getClass(name, defaultValue); if (theClass != null && !xface.isAssignableFrom(theClass)) throw new RuntimeException(theClass+" not "+xface.getName()); else if (theClass != null) return theClass.asSubclass(xface); else return null; } catch (Exception e) { throw new RuntimeException(e); } }
Get the value of the <code>name</code> property as a <code>Class</code> implementing the interface specified by <code>xface</code>. If no such property is specified, then <code>defaultValue</code> is returned. An exception is thrown if the returned class does not implement the named interface. @param name the class name. @param defaultValue default value. @param xface the interface implemented by the named class. @return property value as a <code>Class</code>, or <code>defaultValue</code>.
@SuppressWarnings("unchecked") public <U> List<U> getInstances(String name, Class<U> xface) { List<U> ret = new ArrayList<U>(); Class<?>[] classes = getClasses(name); for (Class<?> cl: classes) { if (!xface.isAssignableFrom(cl)) { throw new RuntimeException(cl + " does not implement " + xface); } ret.add((U)ReflectionUtils.newInstance(cl, this)); } return ret; }
Get the value of the <code>name</code> property as a <code>List</code> of objects implementing the interface specified by <code>xface</code>. An exception is thrown if any of the classes does not exist, or if it does not implement the named interface. @param name the property name. @param xface the interface implemented by the classes named by <code>name</code>. @return a <code>List</code> of objects implementing <code>xface</code>.
public InputStream getConfResourceAsInputStream(String name) { try { URL url= getResource(name); if (url == null) { LOG.info(name + " not found"); return null; } else { LOG.info("found resource " + name + " at " + url); } return url.openStream(); } catch (Exception e) { return null; } }
Get an input stream attached to the configuration resource with the given <code>name</code>. @param name configuration resource name. @return an input stream attached to the resource.
public Reader getConfResourceAsReader(String name) { try { URL url= getResource(name); if (url == null) { LOG.info(name + " not found"); return null; } else { LOG.info("found resource " + name + " at " + url); } return new InputStreamReader(url.openStream(), Charsets.UTF_8); } catch (Exception e) { return null; } }
Get a {@link Reader} attached to the configuration resource with the given <code>name</code>. @param name configuration resource name. @return a reader attached to the resource.
public Set<String> getFinalParameters() { Set<String> setFinalParams = Collections.newSetFromMap( new ConcurrentHashMap<String, Boolean>()); setFinalParams.addAll(finalParameters); return setFinalParams; }
Get the set of parameters marked final. @return final parameter set.
public Map<String, String> getPropsWithPrefix(String confPrefix) { Properties props = getProps(); Enumeration e = props.propertyNames(); Map<String, String> configMap = new HashMap<>(); String name = null; while (e.hasMoreElements()) { name = (String) e.nextElement(); if (name.startsWith(confPrefix)) { String value = props.getProperty(name); name = name.substring(confPrefix.length()); configMap.put(name, value); } } return configMap; }
Constructs a mapping of configuration and includes all properties that start with the specified configuration prefix. Property names in the mapping are trimmed to remove the configuration prefix. @param confPrefix configuration prefix @return mapping of configuration properties with prefix stripped
private void checkForOverride(Properties properties, String name, String attr, String value) { String propertyValue = properties.getProperty(attr); if (propertyValue != null && !propertyValue.equals(value)) { LOG.warn(name + ":an attempt to override final parameter: " + attr + "; Ignoring."); } }
Print a warning if a property with a given name already exists with a different value
public void writeXml(String propertyName, Writer out) throws IOException, IllegalArgumentException { Document doc = asXmlDocument(propertyName); try { DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(out); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); // Important to not hold Configuration log while writing result, since // 'out' may be an HDFS stream which needs to lock this configuration // from another thread. transformer.transform(source, result); } catch (TransformerException te) { throw new IOException(te); } }
Write out the non-default properties in this configuration to the given {@link Writer}. <li> When property name is not empty and the property exists in the configuration, this method writes the property and its attributes to the {@link Writer}. </li> <p> <li> When property name is null or empty, this method writes all the configuration properties and their attributes to the {@link Writer}. </li> <p> <li> When property name is not empty but the property doesn't exist in the configuration, this method throws an {@link IllegalArgumentException}. </li> <p> @param out the writer to write to.
private synchronized Document asXmlDocument(String propertyName) throws IOException, IllegalArgumentException { Document doc; try { doc = DocumentBuilderFactory .newInstance() .newDocumentBuilder() .newDocument(); } catch (ParserConfigurationException pe) { throw new IOException(pe); } Element conf = doc.createElement("configuration"); doc.appendChild(conf); conf.appendChild(doc.createTextNode("\n")); handleDeprecation(); //ensure properties is set and deprecation is handled if(!Strings.isNullOrEmpty(propertyName)) { if (!properties.containsKey(propertyName)) { // given property not found, illegal argument throw new IllegalArgumentException("Property " + propertyName + " not found"); } else { // given property is found, write single property appendXMLProperty(doc, conf, propertyName); conf.appendChild(doc.createTextNode("\n")); } } else { // append all elements for (Enumeration<Object> e = properties.keys(); e.hasMoreElements();) { appendXMLProperty(doc, conf, (String)e.nextElement()); conf.appendChild(doc.createTextNode("\n")); } } return doc; }
Return the XML DOM corresponding to this Configuration.
private synchronized void appendXMLProperty(Document doc, Element conf, String propertyName) { // skip writing if given property name is empty or null if (!Strings.isNullOrEmpty(propertyName)) { String value = properties.getProperty(propertyName); if (value != null) { Element propNode = doc.createElement("property"); conf.appendChild(propNode); Element nameNode = doc.createElement("name"); nameNode.appendChild(doc.createTextNode(propertyName)); propNode.appendChild(nameNode); Element valueNode = doc.createElement("value"); valueNode.appendChild(doc.createTextNode( properties.getProperty(propertyName))); propNode.appendChild(valueNode); Element finalNode = doc.createElement("final"); finalNode.appendChild(doc.createTextNode( String.valueOf(finalParameters.contains(propertyName)))); propNode.appendChild(finalNode); if (updatingResource != null) { String[] sources = updatingResource.get(propertyName); if(sources != null) { for(String s : sources) { Element sourceNode = doc.createElement("source"); sourceNode.appendChild(doc.createTextNode(s)); propNode.appendChild(sourceNode); } } } } } }
Append a property with its attributes to a given {#link Document} if the property is found in configuration. @param doc @param conf @param propertyName
public static void dumpConfiguration(Configuration config, String propertyName, Writer out) throws IOException { if(Strings.isNullOrEmpty(propertyName)) { dumpConfiguration(config, out); } else if (Strings.isNullOrEmpty(config.get(propertyName))) { throw new IllegalArgumentException("Property " + propertyName + " not found"); } else { JsonFactory dumpFactory = new JsonFactory(); JsonGenerator dumpGenerator = dumpFactory.createGenerator(out); dumpGenerator.writeStartObject(); dumpGenerator.writeFieldName("property"); appendJSONProperty(dumpGenerator, config, propertyName, new ConfigRedactor(config)); dumpGenerator.writeEndObject(); dumpGenerator.flush(); } }
Writes properties and their attributes (final and resource) to the given {@link Writer}. <li> When propertyName is not empty, and the property exists in the configuration, the format of the output would be, <pre> { "property": { "key" : "key1", "value" : "value1", "isFinal" : "key1.isFinal", "resource" : "key1.resource" } } </pre> </li> <li> When propertyName is null or empty, it behaves same as {@link #dumpConfiguration(Configuration, Writer)}, the output would be, <pre> { "properties" : [ { key : "key1", value : "value1", isFinal : "key1.isFinal", resource : "key1.resource" }, { key : "key2", value : "value2", isFinal : "ke2.isFinal", resource : "key2.resource" } ] } </pre> </li> <li> When propertyName is not empty, and the property is not found in the configuration, this method will throw an {@link IllegalArgumentException}. </li> <p> @param config the configuration @param propertyName property name @param out the Writer to write to @throws IOException @throws IllegalArgumentException when property name is not empty and the property is not found in configuration
public static void dumpConfiguration(Configuration config, Writer out) throws IOException { JsonFactory dumpFactory = new JsonFactory(); JsonGenerator dumpGenerator = dumpFactory.createGenerator(out); dumpGenerator.writeStartObject(); dumpGenerator.writeFieldName("properties"); dumpGenerator.writeStartArray(); dumpGenerator.flush(); ConfigRedactor redactor = new ConfigRedactor(config); synchronized (config) { for (Map.Entry<Object,Object> item: config.getProps().entrySet()) { appendJSONProperty(dumpGenerator, config, item.getKey().toString(), redactor); } } dumpGenerator.writeEndArray(); dumpGenerator.writeEndObject(); dumpGenerator.flush(); }
Writes out all properties and their attributes (final and resource) to the given {@link Writer}, the format of the output would be, <pre> { "properties" : [ { key : "key1", value : "value1", isFinal : "key1.isFinal", resource : "key1.resource" }, { key : "key2", value : "value2", isFinal : "ke2.isFinal", resource : "key2.resource" } ] } </pre> It does not output the properties of the configuration object which is loaded from an input stream. <p> @param config the configuration @param out the Writer to write to @throws IOException
private static void appendJSONProperty(JsonGenerator jsonGen, Configuration config, String name, ConfigRedactor redactor) throws IOException { // skip writing if given property name is empty or null if(!Strings.isNullOrEmpty(name) && jsonGen != null) { jsonGen.writeStartObject(); jsonGen.writeStringField("key", name); jsonGen.writeStringField("value", redactor.redact(name, config.get(name))); jsonGen.writeBooleanField("isFinal", config.finalParameters.contains(name)); String[] resources = config.updatingResource != null ? config.updatingResource.get(name) : null; String resource = UNKNOWN_RESOURCE; if (resources != null && resources.length > 0) { resource = resources[0]; } jsonGen.writeStringField("resource", resource); jsonGen.writeEndObject(); } }
Write property and its attributes as json format to given {@link JsonGenerator}. @param jsonGen json writer @param config configuration @param name property name @throws IOException
public static boolean hasWarnedDeprecation(String name) { DeprecationContext deprecations = deprecationContext.get(); if(deprecations.getDeprecatedKeyMap().containsKey(name)) { if(deprecations.getDeprecatedKeyMap().get(name).accessed.get()) { return true; } } return false; }
Returns whether or not a deprecated name has been warned. If the name is not deprecated then always return false
public void putBuildRow(BaseRow row) throws IOException { final int hashCode = hash(this.buildSideProjection.apply(row).hashCode(), 0); // TODO: combine key projection and build side conversion to code gen. insertIntoTable(originBuildSideSerializer.baseRowToBinary(row), hashCode); }
Put a build side row to hash table.
public void endBuild() throws IOException { // finalize the partitions int buildWriteBuffers = 0; for (BinaryHashPartition p : this.partitionsBeingBuilt) { buildWriteBuffers += p.finalizeBuildPhase(this.ioManager, this.currentEnumerator); } buildSpillRetBufferNumbers += buildWriteBuffers; // the first prober is the probe-side input, but the input is null at beginning this.probeIterator = new ProbeIterator(this.binaryProbeSideSerializer.createInstance()); // the bucket iterator can remain constant over the time this.bucketIterator = new LookupBucketIterator(this); }
End build phase.
public boolean tryProbe(BaseRow record) throws IOException { if (!this.probeIterator.hasSource()) { // set the current probe value when probeIterator is null at the begging. this.probeIterator.setInstance(record); } // calculate the hash BinaryRow probeKey = probeSideProjection.apply(record); final int hash = hash(probeKey.hashCode(), this.currentRecursionDepth); BinaryHashPartition p = this.partitionsBeingBuilt.get(hash % partitionsBeingBuilt.size()); // for an in-memory partition, process set the return iterators, else spill the probe records if (p.isInMemory()) { this.probeKey = probeKey; this.probeRow = record; p.bucketArea.startLookup(hash); return true; } else { if (p.testHashBloomFilter(hash)) { BinaryRow row = originProbeSideSerializer.baseRowToBinary(record); p.insertIntoProbeBuffer(row); } return false; } }
Find matched build side rows for a probe row. @return return false if the target partition has spilled, we will spill this probe row too. The row will be re-match in rebuild phase.
@Override public void clearPartitions() { // clear the iterators, so the next call to next() will notice this.bucketIterator = null; this.probeIterator = null; for (int i = this.partitionsBeingBuilt.size() - 1; i >= 0; --i) { final BinaryHashPartition p = this.partitionsBeingBuilt.get(i); try { p.clearAllMemory(this.availableMemory); } catch (Exception e) { LOG.error("Error during partition cleanup.", e); } } this.partitionsBeingBuilt.clear(); // clear the partitions that are still to be done (that have files on disk) for (final BinaryHashPartition p : this.partitionsPending) { p.clearAllMemory(this.availableMemory); } }
This method clears all partitions currently residing (partially) in memory. It releases all memory and deletes all spilled partitions. <p>This method is intended for a hard cleanup in the case that the join is aborted.
@Override protected int spillPartition() throws IOException { // find the largest partition int largestNumBlocks = 0; int largestPartNum = -1; for (int i = 0; i < partitionsBeingBuilt.size(); i++) { BinaryHashPartition p = partitionsBeingBuilt.get(i); if (p.isInMemory() && p.getNumOccupiedMemorySegments() > largestNumBlocks) { largestNumBlocks = p.getNumOccupiedMemorySegments(); largestPartNum = i; } } final BinaryHashPartition p = partitionsBeingBuilt.get(largestPartNum); // spill the partition int numBuffersFreed = p.spillPartition(this.ioManager, this.currentEnumerator.next(), this.buildSpillReturnBuffers); this.buildSpillRetBufferNumbers += numBuffersFreed; LOG.info(String.format("Grace hash join: Ran out memory, choosing partition " + "[%d] to spill, %d memory segments being freed", largestPartNum, numBuffersFreed)); // grab as many buffers as are available directly MemorySegment currBuff; while (this.buildSpillRetBufferNumbers > 0 && (currBuff = this.buildSpillReturnBuffers.poll()) != null) { this.availableMemory.add(currBuff); this.buildSpillRetBufferNumbers--; } numSpillFiles++; spillInBytes += numBuffersFreed * segmentSize; // The bloomFilter is built after the data is spilled, so that we can use enough memory. p.buildBloomFilterAndFreeBucket(); return largestPartNum; }
Selects a partition and spills it. The number of the spilled partition is returned. @return The number of the spilled partition.
public void shutdown() { // remove all of our temp directories for (File path : paths) { try { if (path != null) { if (path.exists()) { FileUtils.deleteDirectory(path); LOG.info("I/O manager removed spill file directory {}", path.getAbsolutePath()); } } } catch (Throwable t) { LOG.error("IOManager failed to properly clean up temp file directory: " + path, t); } } }
Close method, marks the I/O manager as closed and removed all temporary files.
public boolean isProperlyShutDown() { for (File path : paths) { if (path != null && path.exists()) { return false; } } return true; }
Utility method to check whether the IO manager has been properly shut down. For this base implementation, this means that all files have been removed. @return True, if the IO manager has properly shut down, false otherwise.
public FileIOChannel.ID createChannel() { final int num = getNextPathNum(); return new FileIOChannel.ID(this.paths[num], num, this.random); }
Creates a new {@link FileIOChannel.ID} in one of the temp directories. Multiple invocations of this method spread the channels evenly across the different directories. @return A channel to a temporary directory.
public void deleteChannel(FileIOChannel.ID channel) throws IOException { if (channel != null) { if (channel.getPathFile().exists() && !channel.getPathFile().delete()) { LOG.warn("IOManager failed to delete temporary file {}", channel.getPath()); } } }
Deletes the file underlying the given channel. If the channel is still open, this call may fail. @param channel The channel to be deleted. @throws IOException Thrown if the deletion fails.
public BlockChannelWriter<MemorySegment> createBlockChannelWriter(FileIOChannel.ID channelID) throws IOException { return createBlockChannelWriter(channelID, new LinkedBlockingQueue<MemorySegment>()); }
Creates a block channel writer that writes to the given channel. The writer adds the written segment to its return-queue afterwards (to allow for asynchronous implementations). @param channelID The descriptor for the channel to write to. @return A block channel writer that writes to the given channel. @throws IOException Thrown, if the channel for the writer could not be opened.
public BlockChannelReader<MemorySegment> createBlockChannelReader(FileIOChannel.ID channelID) throws IOException { return createBlockChannelReader(channelID, new LinkedBlockingQueue<MemorySegment>()); }
Creates a block channel reader that reads blocks from the given channel. The reader pushed full memory segments (with the read data) to its "return queue", to allow for asynchronous read implementations. @param channelID The descriptor for the channel to write to. @return A block channel reader that reads from the given channel. @throws IOException Thrown, if the channel for the reader could not be opened.
public String[] getSpillingDirectoriesPaths() { String[] strings = new String[this.paths.length]; for (int i = 0; i < strings.length; i++) { strings[i] = paths[i].getAbsolutePath(); } return strings; }
Gets the directories that the I/O manager spills to, as path strings. @return The directories that the I/O manager spills to, as path strings.
public static String byteToHexString(final byte[] bytes, final int start, final int end) { if (bytes == null) { throw new IllegalArgumentException("bytes == null"); } int length = end - start; char[] out = new char[length * 2]; for (int i = start, j = 0; i < end; i++) { out[j++] = HEX_CHARS[(0xF0 & bytes[i]) >>> 4]; out[j++] = HEX_CHARS[0x0F & bytes[i]]; } return new String(out); }
Given an array of bytes it will convert the bytes to a hex string representation of the bytes. @param bytes the bytes to convert in a hex string @param start start index, inclusively @param end end index, exclusively @return hex string representation of the byte array
public static String arrayAwareToString(Object o) { if (o == null) { return "null"; } if (o.getClass().isArray()) { return arrayToString(o); } return o.toString(); }
This method calls {@link Object#toString()} on the given object, unless the object is an array. In that case, it will use the {@link #arrayToString(Object)} method to create a string representation of the array that includes all contained elements. @param o The object for which to create the string representation. @return The string representation of the object.
public static String getRandomString(Random rnd, int minLength, int maxLength) { int len = rnd.nextInt(maxLength - minLength + 1) + minLength; char[] data = new char[len]; for (int i = 0; i < data.length; i++) { data[i] = (char) (rnd.nextInt(0x7fff) + 1); } return new String(data); }
Creates a random string with a length within the given interval. The string contains only characters that can be represented as a single code point. @param rnd The random used to create the strings. @param minLength The minimum string length. @param maxLength The maximum string length (inclusive). @return A random String.
public static String getRandomString(Random rnd, int minLength, int maxLength, char minValue, char maxValue) { int len = rnd.nextInt(maxLength - minLength + 1) + minLength; char[] data = new char[len]; int diff = maxValue - minValue + 1; for (int i = 0; i < data.length; i++) { data[i] = (char) (rnd.nextInt(diff) + minValue); } return new String(data); }
Creates a random string with a length within the given interval. The string contains only characters that can be represented as a single code point. @param rnd The random used to create the strings. @param minLength The minimum string length. @param maxLength The maximum string length (inclusive). @param minValue The minimum character value to occur. @param maxValue The maximum character value to occur. @return A random String.
public static String generateRandomAlphanumericString(Random rnd, int length) { checkNotNull(rnd); checkArgument(length >= 0); StringBuilder buffer = new StringBuilder(length); for (int i = 0; i < length; i++) { buffer.append(nextAlphanumericChar(rnd)); } return buffer.toString(); }
Creates a random alphanumeric string of given length. @param rnd The random number generator to use. @param length The number of alphanumeric characters to append.
public static void writeString(@Nonnull String str, DataOutputView out) throws IOException { checkNotNull(str); StringValue.writeString(str, out); }
Writes a String to the given output. The written string can be read with {@link #readString(DataInputView)}. @param str The string to write @param out The output to write to @throws IOException Thrown, if the writing or the serialization fails.
public static void writeNullableString(@Nullable String str, DataOutputView out) throws IOException { if (str != null) { out.writeBoolean(true); writeString(str, out); } else { out.writeBoolean(false); } }
Writes a String to the given output. The string may be null. The written string can be read with {@link #readNullableString(DataInputView)}- @param str The string to write, or null. @param out The output to write to. @throws IOException Thrown, if the writing or the serialization fails.
public static @Nullable String readNullableString(DataInputView in) throws IOException { if (in.readBoolean()) { return readString(in); } else { return null; } }
Reads a String from the given input. The string may be null and must have been written with {@link #writeNullableString(String, DataOutputView)}. @param in The input to read from. @return The deserialized string, or null. @throws IOException Thrown, if the reading or the deserialization fails.