use of java.util.concurrent.FutureTask in project hbase by apache.
the class HBaseFsck method checkAndMarkRunningHbck.
/**
* This method maintains a lock using a file. If the creation fails we return null
*
* @return FSDataOutputStream object corresponding to the newly opened lock file
* @throws IOException if IO failure occurs
*/
private FSDataOutputStream checkAndMarkRunningHbck() throws IOException {
RetryCounter retryCounter = lockFileRetryCounterFactory.create();
FileLockCallable callable = new FileLockCallable(retryCounter);
ExecutorService executor = Executors.newFixedThreadPool(1);
FutureTask<FSDataOutputStream> futureTask = new FutureTask<>(callable);
executor.execute(futureTask);
final int timeoutInSeconds = getConf().getInt("hbase.hbck.lockfile.maxwaittime", DEFAULT_WAIT_FOR_LOCK_TIMEOUT);
FSDataOutputStream stream = null;
try {
stream = futureTask.get(timeoutInSeconds, TimeUnit.SECONDS);
} catch (ExecutionException ee) {
LOG.warn("Encountered exception when opening lock file", ee);
} catch (InterruptedException ie) {
LOG.warn("Interrupted when opening lock file", ie);
Thread.currentThread().interrupt();
} catch (TimeoutException exception) {
// took too long to obtain lock
LOG.warn("Took more than " + timeoutInSeconds + " seconds in obtaining lock");
futureTask.cancel(true);
} finally {
executor.shutdownNow();
}
return stream;
}
use of java.util.concurrent.FutureTask in project hbase by apache.
the class FSUtils method getTableStoreFilePathMap.
/**
* Runs through the HBase rootdir/tablename and creates a reverse lookup map for
* table StoreFile names to the full Path. Note that because this method can be called
* on a 'live' HBase system that we will skip files that no longer exist by the time
* we traverse them and similarly the user of the result needs to consider that some
* entries in this map may not exist by the time this call completes.
* <br>
* Example...<br>
* Key = 3944417774205889744 <br>
* Value = hdfs://localhost:51169/user/userid/-ROOT-/70236052/info/3944417774205889744
*
* @param resultMap map to add values. If null, this method will create and populate one to return
* @param fs The file system to use.
* @param hbaseRootDir The root directory to scan.
* @param tableName name of the table to scan.
* @param sfFilter optional path filter to apply to store files
* @param executor optional executor service to parallelize this operation
* @param errors ErrorReporter instance or null
* @return Map keyed by StoreFile name with a value of the full Path.
* @throws IOException When scanning the directory fails.
* @throws InterruptedException
*/
public static Map<String, Path> getTableStoreFilePathMap(Map<String, Path> resultMap, final FileSystem fs, final Path hbaseRootDir, TableName tableName, final PathFilter sfFilter, ExecutorService executor, final ErrorReporter errors) throws IOException, InterruptedException {
final Map<String, Path> finalResultMap = resultMap == null ? new ConcurrentHashMap<>(128, 0.75f, 32) : resultMap;
// only include the directory paths to tables
Path tableDir = FSUtils.getTableDir(hbaseRootDir, tableName);
// Inside a table, there are compaction.dir directories to skip. Otherwise, all else
// should be regions.
final FamilyDirFilter familyFilter = new FamilyDirFilter(fs);
final Vector<Exception> exceptions = new Vector<>();
try {
List<FileStatus> regionDirs = FSUtils.listStatusWithStatusFilter(fs, tableDir, new RegionDirFilter(fs));
if (regionDirs == null) {
return finalResultMap;
}
final List<Future<?>> futures = new ArrayList<>(regionDirs.size());
for (FileStatus regionDir : regionDirs) {
if (null != errors) {
errors.progress();
}
final Path dd = regionDir.getPath();
if (!exceptions.isEmpty()) {
break;
}
Runnable getRegionStoreFileMapCall = new Runnable() {
@Override
public void run() {
try {
HashMap<String, Path> regionStoreFileMap = new HashMap<>();
List<FileStatus> familyDirs = FSUtils.listStatusWithStatusFilter(fs, dd, familyFilter);
if (familyDirs == null) {
if (!fs.exists(dd)) {
LOG.warn("Skipping region because it no longer exists: " + dd);
} else {
LOG.warn("Skipping region because it has no family dirs: " + dd);
}
return;
}
for (FileStatus familyDir : familyDirs) {
if (null != errors) {
errors.progress();
}
Path family = familyDir.getPath();
if (family.getName().equals(HConstants.RECOVERED_EDITS_DIR)) {
continue;
}
// now in family, iterate over the StoreFiles and
// put in map
FileStatus[] familyStatus = fs.listStatus(family);
for (FileStatus sfStatus : familyStatus) {
if (null != errors) {
errors.progress();
}
Path sf = sfStatus.getPath();
if (sfFilter == null || sfFilter.accept(sf)) {
regionStoreFileMap.put(sf.getName(), sf);
}
}
}
finalResultMap.putAll(regionStoreFileMap);
} catch (Exception e) {
LOG.error("Could not get region store file map for region: " + dd, e);
exceptions.add(e);
}
}
};
// just do serial sync execution
if (executor != null) {
Future<?> future = executor.submit(getRegionStoreFileMapCall);
futures.add(future);
} else {
FutureTask<?> future = new FutureTask<>(getRegionStoreFileMapCall, null);
future.run();
futures.add(future);
}
}
// Ensure all pending tasks are complete (or that we run into an exception)
for (Future<?> f : futures) {
if (!exceptions.isEmpty()) {
break;
}
try {
f.get();
} catch (ExecutionException e) {
LOG.error("Unexpected exec exception! Should've been caught already. (Bug?)", e);
// Shouldn't happen, we already logged/caught any exceptions in the Runnable
}
}
} catch (IOException e) {
LOG.error("Cannot execute getTableStoreFilePathMap for " + tableName, e);
exceptions.add(e);
} finally {
if (!exceptions.isEmpty()) {
// Just throw the first exception as an indication something bad happened
// Don't need to propagate all the exceptions, we already logged them all anyway
Throwables.propagateIfInstanceOf(exceptions.firstElement(), IOException.class);
throw Throwables.propagate(exceptions.firstElement());
}
}
return finalResultMap;
}
use of java.util.concurrent.FutureTask in project guice by google.
the class ScopesTest method testUnresolvableSingletonCircularDependencyErrorMessage.
/**
* Check that circular dependencies on non-interfaces are correctly resolved in multi-threaded
* case. And that an error message constructed is a good one.
*
* <p>I0 -> I1 -> I2 -> J1 and J0 -> J1 -> J2 -> K1 and K0 -> K1 -> K2, where I1, J1 and K1 are
* created in parallel.
*
* <p>Creation is synchronized by injection of {@link S}, first thread would block until second
* would be inside a singleton creation as well.
*
* <p>Verifies that provision results in an error, that spans two threads and has a dependency
* cycle.
*/
public void testUnresolvableSingletonCircularDependencyErrorMessage() throws Exception {
final Provider<S> provider = new SBarrierProvider(3);
final Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(S.class).toProvider(provider);
}
});
FutureTask<I0> firstThreadResult = new FutureTask<I0>(fetchClass(injector, I0.class));
Thread i0Thread = new Thread(firstThreadResult, "I0.class");
// we need to call toString() now, because the toString() changes after the thread exits.
String i0ThreadString = i0Thread.toString();
i0Thread.start();
FutureTask<J0> secondThreadResult = new FutureTask<J0>(fetchClass(injector, J0.class));
Thread j0Thread = new Thread(secondThreadResult, "J0.class");
String j0ThreadString = j0Thread.toString();
j0Thread.start();
FutureTask<K0> thirdThreadResult = new FutureTask<K0>(fetchClass(injector, K0.class));
Thread k0Thread = new Thread(thirdThreadResult, "K0.class");
String k0ThreadString = k0Thread.toString();
k0Thread.start();
// using separate threads to avoid potential deadlock on the main thread
// waiting twice as much to be sure that both would time out in their respective barriers
Throwable firstException = null;
Throwable secondException = null;
Throwable thirdException = null;
try {
firstThreadResult.get(DEADLOCK_TIMEOUT_SECONDS * 3, TimeUnit.SECONDS);
fail();
} catch (ExecutionException e) {
firstException = e.getCause();
}
try {
secondThreadResult.get(DEADLOCK_TIMEOUT_SECONDS * 3, TimeUnit.SECONDS);
fail();
} catch (ExecutionException e) {
secondException = e.getCause();
}
try {
thirdThreadResult.get(DEADLOCK_TIMEOUT_SECONDS * 3, TimeUnit.SECONDS);
fail();
} catch (ExecutionException e) {
thirdException = e.getCause();
}
// verification of error messages generated
List<Message> errors = new ArrayList<Message>();
errors.addAll(((ProvisionException) firstException).getErrorMessages());
errors.addAll(((ProvisionException) secondException).getErrorMessages());
errors.addAll(((ProvisionException) thirdException).getErrorMessages());
// We want to find the longest error reported for a cycle spanning multiple threads
Message spanningError = null;
for (Message error : errors) {
if (error.getMessage().contains("Encountered circular dependency spanning several threads")) {
if (spanningError == null || spanningError.getMessage().length() < error.getMessage().length()) {
spanningError = error;
}
}
}
if (spanningError == null) {
fail("Couldn't find multi thread circular dependency error: " + Joiner.on("\n\n").join(errors));
}
String errorMessage = spanningError.getMessage();
assertContains(errorMessage, "Encountered circular dependency spanning several threads. Tried proxying " + this.getClass().getName());
assertFalse("Both I0 and J0 can not be a part of a dependency cycle", errorMessage.contains(I0.class.getName()) && errorMessage.contains(J0.class.getName()));
assertFalse("Both J0 and K0 can not be a part of a dependency cycle", errorMessage.contains(J0.class.getName()) && errorMessage.contains(K0.class.getName()));
assertFalse("Both K0 and I0 can not be a part of a dependency cycle", errorMessage.contains(K0.class.getName()) && errorMessage.contains(I0.class.getName()));
ListMultimap<String, String> threadToSingletons = ArrayListMultimap.create();
boolean inSingletonsList = false;
String currentThread = null;
for (String errorLine : errorMessage.split("\\n")) {
if (errorLine.startsWith("Thread[")) {
inSingletonsList = true;
currentThread = errorLine.substring(0, errorLine.indexOf(" is holding locks the following singletons in the cycle:"));
} else if (inSingletonsList) {
if (errorLine.startsWith("\tat ")) {
inSingletonsList = false;
} else {
threadToSingletons.put(currentThread, errorLine);
}
}
}
assertEquals("All threads should be in the cycle", 3, threadToSingletons.keySet().size());
// NOTE: J0,K0,I0 are not reported because their locks are not part of the cycle.
assertEquals(threadToSingletons.get(j0ThreadString), ImmutableList.of(J1.class.getName(), J2.class.getName(), K1.class.getName()));
assertEquals(threadToSingletons.get(k0ThreadString), ImmutableList.of(K1.class.getName(), K2.class.getName(), I1.class.getName()));
assertEquals(threadToSingletons.get(i0ThreadString), ImmutableList.of(I1.class.getName(), I2.class.getName(), J1.class.getName()));
}
use of java.util.concurrent.FutureTask in project cobar by alibaba.
the class MySQLChannel method connect.
@Override
public void connect(long timeout) throws Exception {
// 网络IO参数设置
socket = new Socket();
socket.setTcpNoDelay(true);
socket.setTrafficClass(0x04 | 0x10);
socket.setPerformancePreferences(0, 2, 1);
socket.setReceiveBufferSize(RECV_BUFFER_SIZE);
socket.setSendBufferSize(SEND_BUFFER_SIZE);
socket.connect(new InetSocketAddress(dsc.getHost(), dsc.getPort()), SOCKET_CONNECT_TIMEOUT);
// 直接通过socket进行通信
in = new BufferedInputStream(socket.getInputStream(), INPUT_STREAM_BUFFER);
out = new BufferedOutputStream(socket.getOutputStream(), OUTPUT_STREAM_BUFFER);
// 完成连接和初始化
FutureTask<Channel> ft = new FutureTask<Channel>(new Callable<Channel>() {
@Override
public Channel call() throws Exception {
return handshake();
}
});
CobarServer.getInstance().getInitExecutor().execute(ft);
try {
ft.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
ft.cancel(true);
throw e;
} catch (ExecutionException e) {
ft.cancel(true);
throw e;
} catch (TimeoutException e) {
ft.cancel(true);
throw e;
}
}
use of java.util.concurrent.FutureTask in project platform_frameworks_base by android.
the class ViewDebug method callMethodOnAppropriateTheadBlocking.
private static Object callMethodOnAppropriateTheadBlocking(final Method method, final Object object) throws IllegalAccessException, InvocationTargetException, TimeoutException {
if (!(object instanceof View)) {
return method.invoke(object, (Object[]) null);
}
final View view = (View) object;
Callable<Object> callable = new Callable<Object>() {
@Override
public Object call() throws IllegalAccessException, InvocationTargetException {
return method.invoke(view, (Object[]) null);
}
};
FutureTask<Object> future = new FutureTask<Object>(callable);
// Try to use the handler provided by the view
Handler handler = view.getHandler();
// Fall back on using the main thread
if (handler == null) {
handler = new Handler(android.os.Looper.getMainLooper());
}
handler.post(future);
while (true) {
try {
return future.get(CAPTURE_TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
Throwable t = e.getCause();
if (t instanceof IllegalAccessException) {
throw (IllegalAccessException) t;
}
if (t instanceof InvocationTargetException) {
throw (InvocationTargetException) t;
}
throw new RuntimeException("Unexpected exception", t);
} catch (InterruptedException e) {
// Call get again
} catch (CancellationException e) {
throw new RuntimeException("Unexpected cancellation exception", e);
}
}
}
Aggregations