@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 void |
closeChannel(io.netty.channel.Channel channel)
Closes the netty channel from outside the netty I/O thread.
|
static void |
closeChannelSync(io.netty.channel.Channel channel)
Closes the netty channel synchronously.
|
static String |
convertMsToClockTime(long millis)
Converts milliseconds to clock time.
|
static String |
convertMsToDate(long millis)
Converts a millisecond number to a formatted date String.
|
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 long |
getCurrentMs() |
static List<String> |
getGroups(String userName)
Using
CachedGroupMapping to get the group list of a user. |
static String |
getPrimaryGroupName(String userName)
Gets the primary group name of a user.
|
static Throwable |
getRootCause(Throwable e)
Gets the root cause of an exception.
|
static String |
getTmpDir() |
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 <T> void |
invokeAll(List<Callable<T>> callables,
long timeout,
TimeUnit unit)
Executes the given callables, waiting for them to complete (or time out).
|
static boolean |
isLocalHost(WorkerNetAddress address) |
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 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 <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 boolean |
waitFor(String description,
com.google.common.base.Function<Void,Boolean> condition)
Waits for a condition to be satisfied.
|
static boolean |
waitFor(String description,
com.google.common.base.Function<Void,Boolean> condition,
WaitForOptions options)
Waits for a condition to be satisfied.
|
static boolean |
waitFor(String description,
java.util.function.Supplier<Boolean> condition)
Waits for a condition to be satisfied.
|
static <T> T |
waitForResult(String description,
com.google.common.base.Function<Void,T> operation,
WaitForOptions options)
Waits for an operation to return a non-null value with a specified timeout.
|
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 long getCurrentMs()
public static String getTmpDir()
public 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 boolean waitFor(String description, java.util.function.Supplier<Boolean> condition)
description
- a description of what causes condition to evaluate to truecondition
- the condition to wait onpublic static boolean waitFor(String description, com.google.common.base.Function<Void,Boolean> condition)
description
- a description of what causes condition to evaluate to truecondition
- the condition to wait onpublic static boolean waitFor(String description, com.google.common.base.Function<Void,Boolean> condition, WaitForOptions options)
description
- a description of what causes condition to evaluate to truecondition
- the condition to wait onoptions
- the options to usepublic static <T> T waitForResult(String description, com.google.common.base.Function<Void,T> operation, WaitForOptions options)
T
- the type of the return valuedescription
- the description of this operationoperation
- the operationoptions
- the options to usepublic static String getPrimaryGroupName(String userName) throws IOException
userName
- Alluxio user nameIOException
public static List<String> getGroups(String userName) throws IOException
CachedGroupMapping
to get the group list of a user.userName
- Alluxio user nameIOException
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 strippublic 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 timeout, TimeUnit unit) throws TimeoutException, Exception
T
- the return type of the callablescallables
- the callables to executetimeout
- the maximum time to waitunit
- the time unit of the timeout argumentException
- if any of the callables throws an exceptionTimeoutException
public 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 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)
address
- the Alluxio worker network addresspublic static void closeChannel(io.netty.channel.Channel channel)
channel
- the netty channelpublic static void closeChannelSync(io.netty.channel.Channel channel)
channel
- the netty channelpublic static String convertMsToDate(long millis)
millis
- a long millisecond numberpublic static String convertMsToClockTime(long millis)
millis
- millisecondspublic static <T> java.util.function.Supplier<T> memoize(java.util.function.Supplier<T> original)
T
- the object typeoriginal
- the original supplierCopyright © 2023. All Rights Reserved.