use of java.util.concurrent.Callable in project hive by apache.
the class Phase method initalizeHosts.
protected List<RemoteCommandResult> initalizeHosts() throws Exception {
List<ListenableFuture<List<RemoteCommandResult>>> futures = Lists.newArrayList();
ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(hostExecutors.size()));
try {
for (final HostExecutor hostExecutor : hostExecutors) {
futures.add(executor.submit(new Callable<List<RemoteCommandResult>>() {
@Override
public List<RemoteCommandResult> call() throws Exception {
return initalizeHost(hostExecutor);
}
}));
}
List<RemoteCommandResult> results = Lists.newArrayList();
for (ListenableFuture<List<RemoteCommandResult>> future : futures) {
List<RemoteCommandResult> result = future.get();
if (result != null) {
results.addAll(result);
}
}
executor.shutdown();
return results;
} finally {
if (executor.isShutdown()) {
executor.shutdownNow();
}
}
}
use of java.util.concurrent.Callable in project storm by apache.
the class Localizer method updateBlobs.
/**
* This function updates blobs on the supervisor. It uses a separate thread pool and runs
* asynchronously of the download and delete.
*/
public List<LocalizedResource> updateBlobs(List<LocalResource> localResources, String user) throws AuthorizationException, KeyNotFoundException, IOException {
LocalizedResourceSet lrsrcSet = _userRsrc.get(user);
ArrayList<LocalizedResource> results = new ArrayList<>();
ArrayList<Callable<LocalizedResource>> updates = new ArrayList<>();
if (lrsrcSet == null) {
// resource set must have been removed
return results;
}
ClientBlobStore blobstore = null;
try {
blobstore = getClientBlobStore();
for (LocalResource localResource : localResources) {
String key = localResource.getBlobName();
LocalizedResource lrsrc = lrsrcSet.get(key, localResource.shouldUncompress());
if (lrsrc == null) {
LOG.warn("blob requested for update doesn't exist: {}", key);
continue;
} else {
// update it if either the version isn't the latest or if any local blob files are missing
if (!isLocalizedResourceUpToDate(lrsrc, blobstore) || !isLocalizedResourceDownloaded(lrsrc)) {
LOG.debug("updating blob: {}", key);
updates.add(new DownloadBlob(this, _conf, key, new File(lrsrc.getFilePath()), user, lrsrc.isUncompressed(), true));
}
}
}
} finally {
if (blobstore != null) {
blobstore.shutdown();
}
}
try {
List<Future<LocalizedResource>> futures = _updateExecService.invokeAll(updates);
for (Future<LocalizedResource> futureRsrc : futures) {
try {
LocalizedResource lrsrc = futureRsrc.get();
// put the resource just in case it was removed at same time by the cleaner
LocalizedResourceSet newSet = new LocalizedResourceSet(user);
LocalizedResourceSet newlrsrcSet = _userRsrc.putIfAbsent(user, newSet);
if (newlrsrcSet == null) {
newlrsrcSet = newSet;
}
newlrsrcSet.putIfAbsent(lrsrc.getKey(), lrsrc, lrsrc.isUncompressed());
results.add(lrsrc);
} catch (ExecutionException e) {
LOG.error("Error updating blob: ", e);
if (e.getCause() instanceof AuthorizationException) {
throw (AuthorizationException) e.getCause();
}
if (e.getCause() instanceof KeyNotFoundException) {
throw (KeyNotFoundException) e.getCause();
}
}
}
} catch (RejectedExecutionException re) {
LOG.error("Error updating blobs : ", re);
} catch (InterruptedException ie) {
throw new IOException("Interrupted Exception", ie);
}
return results;
}
use of java.util.concurrent.Callable in project hbase by apache.
the class TestHBaseFsckOneRS method testParallelWithRetriesHbck.
/**
* This test makes sure that with enough retries both parallel instances
* of hbck will be completed successfully.
*
* @throws Exception
*/
@Test(timeout = 180000)
public void testParallelWithRetriesHbck() throws Exception {
final ExecutorService service;
final Future<HBaseFsck> hbck1, hbck2;
// With the ExponentialBackoffPolicyWithLimit (starting with 200 milliseconds sleep time, and
// max sleep time of 5 seconds), we can retry around 15 times within 80 seconds before bail out.
//
// Note: the reason to use 80 seconds is that in HADOOP-2.6 and later, the create file would
// retry up to HdfsConstants.LEASE_SOFTLIMIT_PERIOD (60 seconds). See HBASE-13574 for more
// details.
final int timeoutInSeconds = 80;
final int sleepIntervalInMilliseconds = 200;
final int maxSleepTimeInMilliseconds = 6000;
final int maxRetryAttempts = 15;
class RunHbck implements Callable<HBaseFsck> {
@Override
public HBaseFsck call() throws Exception {
// Increase retry attempts to make sure the non-active hbck doesn't get starved
Configuration c = new Configuration(conf);
c.setInt("hbase.hbck.lockfile.maxwaittime", timeoutInSeconds);
c.setInt("hbase.hbck.lockfile.attempt.sleep.interval", sleepIntervalInMilliseconds);
c.setInt("hbase.hbck.lockfile.attempt.maxsleeptime", maxSleepTimeInMilliseconds);
c.setInt("hbase.hbck.lockfile.attempts", maxRetryAttempts);
return doFsck(c, false);
}
}
service = Executors.newFixedThreadPool(2);
hbck1 = service.submit(new RunHbck());
hbck2 = service.submit(new RunHbck());
service.shutdown();
//wait for some time, for both hbck calls finish
service.awaitTermination(timeoutInSeconds * 2, TimeUnit.SECONDS);
HBaseFsck h1 = hbck1.get();
HBaseFsck h2 = hbck2.get();
// Both should be successful
assertNotNull(h1);
assertNotNull(h2);
assert (h1.getRetCode() >= 0);
assert (h2.getRetCode() >= 0);
}
use of java.util.concurrent.Callable in project hive by apache.
the class StatsUtils method getFileSizeForPartitions.
/**
* Find the bytes on disks occupied by list of partitions
* @param conf
* - hive conf
* @param parts
* - partition list
* @return sizes of partitions
*/
public static List<Long> getFileSizeForPartitions(final HiveConf conf, List<Partition> parts) {
LOG.info("Number of partitions : " + parts.size());
ArrayList<Future<Long>> futures = new ArrayList<>();
int threads = Math.max(1, conf.getIntVar(ConfVars.METASTORE_FS_HANDLER_THREADS_COUNT));
final ExecutorService pool = Executors.newFixedThreadPool(threads, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Get-Partitions-Size-%d").build());
final ArrayList<Long> sizes = new ArrayList<>(parts.size());
for (final Partition part : parts) {
final Path path = part.getDataLocation();
futures.add(pool.submit(new Callable<Long>() {
@Override
public Long call() throws Exception {
try {
LOG.debug("Partition path : " + path);
FileSystem fs = path.getFileSystem(conf);
return fs.getContentSummary(path).getLength();
} catch (IOException e) {
return 0L;
}
}
}));
}
try {
for (int i = 0; i < futures.size(); i++) {
sizes.add(i, futures.get(i).get());
}
} catch (InterruptedException | ExecutionException e) {
LOG.warn("Exception in processing files ", e);
} finally {
pool.shutdownNow();
}
return sizes;
}
use of java.util.concurrent.Callable in project hive by apache.
the class Hive method trashFiles.
/**
* Trashes or deletes all files under a directory. Leaves the directory as is.
* @param fs FileSystem to use
* @param statuses fileStatuses of files to be deleted
* @param conf hive configuration
* @return true if deletion successful
* @throws IOException
*/
public static boolean trashFiles(final FileSystem fs, final FileStatus[] statuses, final Configuration conf) throws IOException {
boolean result = true;
if (statuses == null || statuses.length == 0) {
return false;
}
final List<Future<Boolean>> futures = new LinkedList<>();
final ExecutorService pool = conf.getInt(ConfVars.HIVE_MOVE_FILES_THREAD_COUNT.varname, 25) > 0 ? Executors.newFixedThreadPool(conf.getInt(ConfVars.HIVE_MOVE_FILES_THREAD_COUNT.varname, 25), new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Delete-Thread-%d").build()) : null;
final SessionState parentSession = SessionState.get();
for (final FileStatus status : statuses) {
if (null == pool) {
result &= FileUtils.moveToTrash(fs, status.getPath(), conf);
} else {
futures.add(pool.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
SessionState.setCurrentSessionState(parentSession);
return FileUtils.moveToTrash(fs, status.getPath(), conf);
}
}));
}
}
if (null != pool) {
pool.shutdown();
for (Future<Boolean> future : futures) {
try {
result &= future.get();
} catch (InterruptedException | ExecutionException e) {
LOG.error("Failed to delete: ", e);
pool.shutdownNow();
throw new IOException(e);
}
}
}
return result;
}
Aggregations