@ThreadSafe public final class CommonUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
CommonUtils.ProcessType
Alluxio process types.
|
Modifier and Type | Field and Description |
---|---|
static AtomicReference<CommonUtils.ProcessType> |
PROCESS_TYPE
Represents the type of Alluxio process running in this JVM.
|
Modifier and Type | Method and Description |
---|---|
static <T> String |
argsToString(String separator,
T... args)
Converts varargs of objects to a string.
|
static IOException |
castToIOException(Throwable e)
Casts a
Throwable to an IOException . |
static RuntimeException |
closeAndRethrow(com.google.common.io.Closer closer,
Throwable t)
Closes the Closer and re-throws the Throwable.
|
static RuntimeException |
closeAndRethrowRuntimeException(com.google.common.io.Closer closer,
Throwable t)
Similar to
closeAndRethrow(com.google.common.io.Closer, java.lang.Throwable) but always return a RuntimeException. |
static Map<String,byte[]> |
convertFromByteString(Map<String,com.google.protobuf.ByteString> input) |
static String |
convertMsToClockTime(long millis)
Converts milliseconds to clock time.
|
static String |
convertMsToDate(long millis,
String dateFormatPattern)
Converts a millisecond number to a formatted date String.
|
static Map<String,com.google.protobuf.ByteString> |
convertToByteString(Map<String,byte[]> input) |
static <T> T |
createNewClassInstance(Class<T> cls,
Class<?>[] ctorClassArgs,
Object[] ctorArgs)
Creates new instance of a class by calling a constructor that receives ctorClassArgs arguments.
|
static Thread |
createProgressThread(long intervalMs,
PrintStream stream)
Creates a thread which will write "." to the given print stream at the given interval.
|
static Thread |
createProgressThread(PrintStream stream)
Convenience method for calling
createProgressThread(long, PrintStream) with an
interval of 2 seconds. |
static long |
getCurrentMs() |
static List<String> |
getGroups(String userName,
AlluxioConfiguration conf)
Using
CachedGroupMapping to get the group list of a user. |
static int |
getJavaVersion() |
static String |
getPrimaryGroupName(String userName,
AlluxioConfiguration conf)
Gets the primary group name of a user.
|
static Throwable |
getRootCause(Throwable e)
Gets the root cause of an exception.
|
static String |
getTmpDir(List<String> tmpDirs) |
static List<String> |
getUnixGroups(String user)
Gets the current user's group list from Unix by running the command 'groups' NOTE.
|
static String |
getValueFromStaticMapping(String mapping,
String key)
Gets the value with a given key from a static key/value mapping in string format.
|
static String |
getWorkerDataDirectory(String storageDir,
AlluxioConfiguration conf) |
static <T> void |
invokeAll(ExecutorService service,
List<Callable<T>> callables,
long timeoutMs)
Executes the given callables, waiting for them to complete (or time out).
|
static <T> void |
invokeAll(List<Callable<T>> callables,
long timeoutMs)
Executes the given callables, waiting for them to complete (or time out).
|
static boolean |
isAddressReachable(String hostname,
int port,
int timeoutMs)
Validates whether a network address is reachable.
|
static boolean |
isLocalHost(WorkerNetAddress address,
AlluxioConfiguration conf) |
static <T> String |
listToString(List<T> list)
Converts a list of objects to a string.
|
static <T> java.util.function.Supplier<T> |
memoize(java.util.function.Supplier<T> original)
Memoize implementation for java.util.function.supplier.
|
static int |
parseMajorVersion(String version) |
static <T> List<List<T>> |
partition(List<T> list,
int numLists)
Partitions a list into numLists many lists each with around list.size() / numLists elements.
|
static String |
randomAlphaNumString(int length)
Generates a random alphanumeric string of the given length.
|
static byte[] |
randomBytes(int length)
Generates a random byte array of the given length.
|
static List<File> |
recursiveListLocalDir(File dir)
Recursively lists a local dir and all its subdirs and return all the files.
|
static <T> Iterator<T> |
singleElementIterator(T element)
Returns an iterator that iterates on a single element.
|
static void |
sleepMs(org.slf4j.Logger logger,
long timeMs)
Sleeps for the given number of milliseconds, reporting interruptions using the given logger.
|
static void |
sleepMs(long timeMs)
Sleeps for the given number of milliseconds.
|
static String |
stripLeadingAndTrailingQuotes(String str)
Strips the leading and trailing quotes from the given string.
|
static String |
stripPrefixIfPresent(String key,
String prefix)
Strips the prefix from the key if it is present.
|
static String |
stripSuffixIfPresent(String key,
String suffix)
Strips the suffix if it exists.
|
static String[] |
toStringArray(ArrayList<String> src)
Parses
ArrayList<String> into String[] . |
static void |
unwrapResponse(Protocol.Response response)
Unwraps a
Protocol.Response . |
static void |
unwrapResponseFrom(Protocol.Response response,
io.netty.channel.Channel channel)
Unwraps a
Protocol.Response associated with a channel. |
static void |
waitFor(String description,
java.util.function.Supplier<Boolean> condition)
Waits for a condition to be satisfied.
|
static void |
waitFor(String description,
java.util.function.Supplier<Boolean> condition,
WaitForOptions options)
Waits for a condition to be satisfied.
|
static <T> T |
waitForResult(String description,
java.util.function.Supplier<T> objectSupplier,
java.util.function.Function<T,Boolean> condition)
Waits for the object to meet a certain condition.
|
static <T> T |
waitForResult(String description,
java.util.function.Supplier<T> objectSupplier,
java.util.function.Function<T,Boolean> condition,
WaitForOptions options)
Waits for the object to meet a certain condition.
|
static void |
warmUpLoop()
Common empty loop utility that serves the purpose of warming up the JVM before performance
microbenchmarks.
|
public static final AtomicReference<CommonUtils.ProcessType> PROCESS_TYPE
public static Thread createProgressThread(PrintStream stream)
createProgressThread(long, PrintStream)
with an
interval of 2 seconds.stream
- the print stream to write topublic static Thread createProgressThread(long intervalMs, PrintStream stream)
intervalMs
- the time interval in milliseconds between writesstream
- the print stream to write topublic static long getCurrentMs()
public static int getJavaVersion()
public static String getTmpDir(List<String> tmpDirs)
tmpDirs
- the list of possible temporary directories to pick frompublic static String getWorkerDataDirectory(String storageDir, AlluxioConfiguration conf)
storageDir
- the root of a storage directory in tiered storageconf
- Alluxio's current configurationpublic static <T> String listToString(List<T> list)
T
- type of the objectslist
- list of objectspublic static <T> String argsToString(String separator, T... args)
T
- type of the objectsseparator
- separator stringargs
- variable argumentspublic static String[] toStringArray(ArrayList<String> src)
ArrayList<String>
into String[]
.src
- is the ArrayList of stringspublic static String randomAlphaNumString(int length)
length
- the lengthpublic static byte[] randomBytes(int length)
length
- the lengthpublic static void sleepMs(long timeMs)
timeMs
- sleep duration in millisecondspublic static void sleepMs(org.slf4j.Logger logger, long timeMs)
logger
- logger for reporting interruptions; no reporting is done if the logger is nulltimeMs
- sleep duration in millisecondspublic static void warmUpLoop()
public static <T> T createNewClassInstance(Class<T> cls, Class<?>[] ctorClassArgs, Object[] ctorArgs)
T
- type of the objectcls
- the class to createctorClassArgs
- parameters type list of the constructor to initiate, if null default
constructor will be calledctorArgs
- the arguments to pass the constructorRuntimeException
- if the class cannot be instantiatedpublic static List<String> getUnixGroups(String user) throws IOException
user
- user nameuser
belongs to. The primary group is returned firstIOException
public static void waitFor(String description, java.util.function.Supplier<Boolean> condition) throws InterruptedException, TimeoutException
description
- a description of what causes condition to evaluate to truecondition
- the condition to wait onTimeoutException
- if the function times out while waiting for the condition to be trueInterruptedException
public static void waitFor(String description, java.util.function.Supplier<Boolean> condition, WaitForOptions options) throws InterruptedException, TimeoutException
description
- a description of what causes condition to evaluate to truecondition
- the condition to wait onoptions
- the options to useTimeoutException
- if the function times out while waiting for the condition to be trueInterruptedException
public static <T> T waitForResult(String description, java.util.function.Supplier<T> objectSupplier, java.util.function.Function<T,Boolean> condition) throws TimeoutException, InterruptedException
T
- type of the objectdescription
- a description of what causes condition to be metobjectSupplier
- the object to check the condition forcondition
- the condition to wait onTimeoutException
- if the function times out while waiting for the condition to be trueInterruptedException
- if the thread was interruptedpublic static <T> T waitForResult(String description, java.util.function.Supplier<T> objectSupplier, java.util.function.Function<T,Boolean> condition, WaitForOptions options) throws TimeoutException, InterruptedException
T
- type of the objectdescription
- a description of what causes condition to be metobjectSupplier
- the object to check the condition forcondition
- the condition to wait onoptions
- the options to useTimeoutException
- if the function times out while waiting for the condition to be trueInterruptedException
- if the thread was interruptedpublic static String getPrimaryGroupName(String userName, AlluxioConfiguration conf) throws IOException
userName
- Alluxio user nameconf
- Alluxio configurationIOException
public static List<String> getGroups(String userName, AlluxioConfiguration conf) throws IOException
CachedGroupMapping
to get the group list of a user.userName
- Alluxio user nameconf
- Alluxio configurationIOException
public static String stripSuffixIfPresent(String key, String suffix)
key
- the key to strip the suffix fromsuffix
- suffix to removepublic static String stripPrefixIfPresent(String key, String prefix)
key
- the key to stripprefix
- prefix to removepublic static String stripLeadingAndTrailingQuotes(String str)
str
- The string to strip@Nullable public static String getValueFromStaticMapping(String mapping, String key)
mapping
- the "key=value" mapping in string format separated by ";"key
- the key to querypublic static Throwable getRootCause(Throwable e)
e
- the exceptionpublic static IOException castToIOException(Throwable e)
Throwable
to an IOException
.e
- the throwablepublic static <T> Iterator<T> singleElementIterator(T element)
T
- the type of the elementelement
- the elementpublic static <T> void invokeAll(List<Callable<T>> callables, long timeoutMs) throws TimeoutException, ExecutionException
T
- the return type of the callablescallables
- the callables to executetimeoutMs
- time to wait for the callables to complete, in millisecondsTimeoutException
- if the callables don't complete before the timeoutExecutionException
- if any of the callables throws an exceptionpublic static <T> void invokeAll(ExecutorService service, List<Callable<T>> callables, long timeoutMs) throws TimeoutException, ExecutionException
T
- the return type of the callablesservice
- the service to execute the callablescallables
- the callables to executetimeoutMs
- time to wait for the callables to complete, in millisecondsTimeoutException
- if the callables don't complete before the timeoutExecutionException
- if any of the callables throws an exceptionpublic static RuntimeException closeAndRethrow(com.google.common.io.Closer closer, Throwable t) throws IOException
Closer closer = new Closer(); try { Closeable c = closer.register(new Closeable()); } catch (Throwable t) { throw closeAndRethrow(closer, t); }
closer
- the Closer to closet
- the Throwable to re-throwIOException
public static RuntimeException closeAndRethrowRuntimeException(com.google.common.io.Closer closer, Throwable t)
closeAndRethrow(com.google.common.io.Closer, java.lang.Throwable)
but always return a RuntimeException.closer
- the Closer to closet
- the Throwable to re-throwpublic static void unwrapResponse(Protocol.Response response) throws AlluxioStatusException
Protocol.Response
.response
- the responseAlluxioStatusException
public static void unwrapResponseFrom(Protocol.Response response, io.netty.channel.Channel channel) throws AlluxioStatusException
Protocol.Response
associated with a channel.response
- the responsechannel
- the channel that receives this responseAlluxioStatusException
public static boolean isLocalHost(WorkerNetAddress address, AlluxioConfiguration conf)
address
- the Alluxio worker network addressconf
- Alluxio configurationpublic static String convertMsToDate(long millis, String dateFormatPattern)
millis
- a long millisecond numberdateFormatPattern
- the date format to follow when converting. i.e. mm-dd-yyyypublic static String convertMsToClockTime(long millis)
millis
- millisecondspublic static Map<String,com.google.protobuf.ByteString> convertToByteString(Map<String,byte[]> input)
input
- the input mapByteString
for values instead of byte[]
public static Map<String,byte[]> convertFromByteString(Map<String,com.google.protobuf.ByteString> input)
input
- the input mapbyte[]
for values instead of protobuf ByteString
public static <T> java.util.function.Supplier<T> memoize(java.util.function.Supplier<T> original)
T
- the object typeoriginal
- the original supplierpublic static <T> List<List<T>> partition(List<T> list, int numLists)
T
- the object typelist
- the list to partitionnumLists
- number of lists to returnpublic static boolean isAddressReachable(String hostname, int port, int timeoutMs)
hostname
- host name of the network addressport
- port of the network addresstimeoutMs
- duration to attempt connection before returning falsepublic static List<File> recursiveListLocalDir(File dir)
dir
- the directorypublic static int parseMajorVersion(String version)
version
- the version string of the JVMgiCopyright © 2023. All Rights Reserved.