List of usage examples for java.lang Thread join
public final synchronized void join(final long millis) throws InterruptedException
From source file:it.cnr.icar.eric.server.query.federation.FederatedQueryProcessor.java
/** * Submits an AdhocQueryRequest to all Registries thare are members of specified federation. * * @param user/*w w w. j a v a2 s.c om*/ * @param adhocQueryRequest the request sent to every registry ias a parrallel distrubuted query. */ public AdhocQueryResponse submitAdhocQuery(final ServerRequestContext context) throws RegistryException { this.adhocQueryRequest = (AdhocQueryRequest) context.getCurrentRegistryRequest(); try { //Reset federated flag before doing federated query to avoid infinite loop adhocQueryRequest.setFederated(false); //Reset federation flag before doing federated query to avoid potential //for implementations to interpret non-null values as implying federated query adhocQueryRequest.setFederation(null); // Create a barrier for all worker threads log.trace("Dispatching federated query to " + members.size() + " member registries."); CyclicBarrier barrier = new CyclicBarrier(members.size(), new Runnable() { public void run() { retrieveResults(context); } }); ThreadGroup threadGroup = new ThreadGroup("federatedQuery"); // Send a request to all destinations Iterator<RegistryType> i = members.iterator(); while (i.hasNext()) { RegistryType registry = i.next(); FederatedQueryWorker worker = new FederatedQueryWorker(barrier, registry, TIMEOUT_CONSTANT, context.getUser(), adhocQueryRequest); workers.add(worker); //Thread thread = new Thread(threadGroup, worker); Thread thread = new Thread(threadGroup, worker, "Federated_Query_" + registry.getId()); threads.add(thread); log.trace("Dispatching query to registry with id: " + registry.getId() + " name: " + bu.getInternationalStringAsString(registry.getName())); thread.start(); } Iterator<Thread> i1 = threads.iterator(); while (i1.hasNext()) { Thread thread = i1.next(); // Wait until all threads have finished. // CAVEAT: The timeouts will add up, max. waiting time is (number of threads) * (timeout) try { thread.join(TIMEOUT_CONSTANT); } catch (InterruptedException e) { //TODO: Try to kill the thread somehow } } } catch (JAXRException e) { //This exception is thrown potentially (unlikely) by bu.getInternationalStringAsString throw new RegistryException(e); } return getUnifiedResponse(); }
From source file:ml.shifu.guagua.mapreduce.GuaguaMapReduceClient.java
/** * Run all jobs added to JobControl.//w w w . ja v a 2 s .c om */ public void run() throws IOException { // Initially, all jobs are in wait state. List<ControlledJob> jobsWithoutIds = this.jc.getWaitingJobList(); int totalNeededMRJobs = jobsWithoutIds.size(); LOG.info("{} map-reduce job(s) waiting for submission.", jobsWithoutIds.size()); Thread jcThread = new Thread(this.jc, "Guagua-MapReduce-JobControl"); jcThread.start(); JobClient jobClient = new JobClient(new JobConf(new Configuration())); double lastProg = -1; Set<String> sucessfulJobs = new HashSet<String>(); while (!this.jc.allFinished()) { try { jcThread.join(1000); } catch (InterruptedException ignore) { Thread.currentThread().interrupt(); } List<ControlledJob> jobsAssignedIdInThisRun = new ArrayList<ControlledJob>(totalNeededMRJobs); for (ControlledJob job : jobsWithoutIds) { if (job.getJob().getJobID() != null) { jobsAssignedIdInThisRun.add(job); LOG.info("Job {} is started.", job.getJob().getJobID().toString()); } else { // This job is not assigned an id yet. } } jobsWithoutIds.removeAll(jobsAssignedIdInThisRun); List<ControlledJob> successfulJobs = jc.getSuccessfulJobList(); for (ControlledJob controlledJob : successfulJobs) { String jobId = controlledJob.getJob().getJobID().toString(); if (!sucessfulJobs.contains(jobId)) { LOG.info("Job {} is successful.", jobId); sucessfulJobs.add(jobId); } } List<ControlledJob> failedJobs = jc.getFailedJobList(); for (ControlledJob controlledJob : failedJobs) { String failedJobId = controlledJob.getJob().getJobID().toString(); if (!this.failedCheckingJobs.contains(failedJobId)) { this.failedCheckingJobs.add(failedJobId); String jobName = controlledJob.getJob().getJobName(); Integer jobIndex = this.jobIndexMap.get(jobName); Integer runTimes = this.jobRunningTimes.get(jobIndex); if (runTimes <= 1) { LOG.warn("Job {} is failed, will be submitted again.", jobName); Job newJob = createJob(this.jobIndexParams.get(jobIndex)); this.jc.addJob(new ControlledJob(newJob, null)); this.jobRunningTimes.put(jobIndex, runTimes + 1); this.jobIndexMap.put(newJob.getJobName(), jobIndex); jobsWithoutIds = this.jc.getWaitingJobList(); } else { LOG.warn("Job {} is failed twice, will not be submitted again.", jobName); } } } double prog = calculateProgress(jc, jobClient) / totalNeededMRJobs; notifyProgress(prog, lastProg); lastProg = prog; try { Thread.sleep(2 * 1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } List<ControlledJob> successfulJobs = jc.getSuccessfulJobList(); LOG.info("Sucessful jobs:"); for (ControlledJob controlledJob : successfulJobs) { LOG.info("Job: {} ", controlledJob); } if (totalNeededMRJobs == successfulJobs.size()) { LOG.info("Guagua jobs: 100% complete"); // add failed jobs to debug since all jobs are finished. List<ControlledJob> failedJobs = jc.getFailedJobList(); if (failedJobs != null && failedJobs.size() > 0) { LOG.debug("Failed jobs:"); for (ControlledJob controlledJob : failedJobs) { LOG.debug("Job: {} ", controlledJob); } } } else { List<ControlledJob> failedJobs = jc.getFailedJobList(); if (failedJobs != null && failedJobs.size() > 0) { LOG.info("Failed jobs:"); for (ControlledJob controlledJob : failedJobs) { LOG.warn("Job: {} ", controlledJob); } } } this.jc.stop(); }
From source file:de.huberlin.wbi.hiway.am.HiWay.java
private void finish() { writeEntryToLog(new JsonReportEntry(getRunId(), null, null, null, null, null, HiwayDBI.KEY_WF_TIME, Long.toString(System.currentTimeMillis() - amRMClient.getStartTime()))); Collection<Data> outputFiles = getOutputFiles(); if (outputFiles.size() > 0) { String outputs = getOutputFiles().toString(); writeEntryToLog(new JsonReportEntry(getRunId(), null, null, null, null, null, HiwayDBI.KEY_WF_OUTPUT, outputs.substring(1, outputs.length() - 1))); }//from www . j a va2 s .c o m // Join all launched threads needed for when we time out and we need to release containers for (Thread launchThread : launchThreads) { try { launchThread.join(10000); } catch (InterruptedException e) { System.err.println("Exception thrown in thread join: " + e.getMessage()); e.printStackTrace(); System.exit(-1); } } // When the application completes, it should stop all running containers System.out.println("Application completed. Stopping running containers"); nmClientAsync.stop(); // When the application completes, it should send a finish application signal to the RM System.out.println("Application completed. Signalling finish to RM"); FinalApplicationStatus appStatus; String appMessage = null; success = true; System.out.println("Failed Containers: " + numFailedContainers.get()); System.out.println("Completed Containers: " + numCompletedContainers.get()); int numTotalContainers = scheduler.getNumberOfTotalTasks(); System.out.println("Total Scheduled Containers: " + numTotalContainers); if (numFailedContainers.get() == 0 && numCompletedContainers.get() == numTotalContainers) { appStatus = FinalApplicationStatus.SUCCEEDED; } else { appStatus = FinalApplicationStatus.FAILED; appMessage = "Diagnostics." + ", total=" + numTotalContainers + ", completed=" + numCompletedContainers.get() + ", allocated=" + numAllocatedContainers.get() + ", failed=" + numFailedContainers.get() + ", killed=" + numKilledContainers.get(); success = false; } try { statLog.close(); federatedReport.stageOut(); if (summaryPath != null) { String stdout = hdfsApplicationDirectory + "/AppMaster.stdout"; String stderr = hdfsApplicationDirectory + "/AppMaster.stderr"; String statlog = hdfsApplicationDirectory + "/" + appId + ".log"; try (BufferedWriter writer = new BufferedWriter(new FileWriter(summaryPath.toString()))) { Collection<String> output = new ArrayList<>(); for (Data outputFile : getOutputFiles()) { output.add(outputFile.getHdfsPath().toString()); } JSONObject obj = new JSONObject(); try { obj.put("output", output); obj.put("stdout", stdout); obj.put("stderr", stderr); obj.put("statlog", statlog); } catch (JSONException e) { e.printStackTrace(); System.exit(-1); } writer.write(obj.toString()); } new Data("AppMaster.stdout").stageOut(); new Data("AppMaster.stderr").stageOut(); new Data(summaryPath).stageOut(); } } catch (IOException e) { System.err.println("Error when attempting to stage out federated output log."); e.printStackTrace(); System.exit(-1); } try { amRMClient.unregisterApplicationMaster(appStatus, appMessage, null); } catch (YarnException | IOException e) { System.err.println("Failed to unregister application"); e.printStackTrace(); System.exit(-1); } amRMClient.stop(); }
From source file:ApplicationMaster.java
@VisibleForTesting protected boolean finish() { // wait for completion. while (!done && (numCompletedContainers.get() != numTotalContainers)) { try {//from ww w.j ava2 s .c o m Thread.sleep(200); } catch (InterruptedException ex) { } } // Join all launched threads // needed for when we time out // and we need to release containers for (Thread launchThread : launchThreads) { try { launchThread.join(10000); } catch (InterruptedException e) { LOG.info("Exception thrown in thread join: " + e.getMessage()); e.printStackTrace(); } } // When the application completes, it should stop all running containers LOG.info("Application completed. Stopping running containers"); nmClientAsync.stop(); // When the application completes, it should send a finish application // signal to the RM LOG.info("Application completed. Signalling finish to RM"); FinalApplicationStatus appStatus; String appMessage = null; boolean success = true; if (numFailedContainers.get() == 0 && numCompletedContainers.get() == numTotalContainers) { appStatus = FinalApplicationStatus.SUCCEEDED; } else { appStatus = FinalApplicationStatus.FAILED; appMessage = "Diagnostics." + ", total=" + numTotalContainers + ", completed=" + numCompletedContainers.get() + ", allocated=" + numAllocatedContainers.get() + ", failed=" + numFailedContainers.get(); success = false; } try { amRMClient.unregisterApplicationMaster(appStatus, appMessage, null); } catch (YarnException ex) { LOG.error("Failed to unregister application", ex); } catch (IOException e) { LOG.error("Failed to unregister application", e); } amRMClient.stop(); return success; }
From source file:com.shopzilla.hadoop.mapreduce.MiniMRClusterContext.java
@PreDestroy public void stop() { try {/*from w ww .j a v a 2 s . c om*/ Thread shutdownThread = new Thread(new Runnable() { @Override public void run() { try { if (pigServer != null) { pigServer.shutdown(); } if (miniDFSCluster != null) { miniDFSCluster.shutdown(); miniDFSCluster = null; } if (miniMrCluster != null) { miniMrCluster.shutdown(); miniMrCluster = null; } } catch (Exception ex) { ex.printStackTrace(); } } }); shutdownThread.start(); shutdownThread.join(10000); FileUtils.deleteDirectory(logDirectory.getFile()); FileUtils.deleteDirectory(buildDirectory); FileUtils.deleteDirectory(new File(projectDirectory, "logs")); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:com.sogou.dockeronyarn.service.DockerApplicationMaster_24.java
@VisibleForTesting protected boolean finish() { // wait for completion. while (!done && (numCompletedContainers.get() != numTotalContainers)) { try {/*from w ww. j a va 2 s. com*/ Thread.sleep(200); } catch (InterruptedException ex) { } } // Join all launched threads // needed for when we time out // and we need to release containers for (Thread launchThread : launchThreads) { try { launchThread.join(10000); } catch (InterruptedException e) { LOG.info("Exception thrown in thread join: " + e.getMessage()); e.printStackTrace(); } } // When the application completes, it should stopContainer all running containers LOG.info("Application completed. Stopping running containers"); nmClientAsync.stop(); // When the application completes, it should send a shutdown application // signal to the RM LOG.info("Application completed. Signalling shutdown to RM"); FinalApplicationStatus appStatus; String appMessage = null; boolean success = true; if (numFailedContainers.get() == 0 && numCompletedContainers.get() == numTotalContainers) { appStatus = FinalApplicationStatus.SUCCEEDED; } else { appStatus = FinalApplicationStatus.FAILED; appMessage = "Diagnostics." + ", total=" + numTotalContainers + ", completed=" + numCompletedContainers.get() + ", allocated=" + numAllocatedContainers.get() + ", failed=" + numFailedContainers.get(); success = false; } try { amRMClient.unregisterApplicationMaster(appStatus, appMessage, null); } catch (YarnException ex) { LOG.error("Failed to unregister application", ex); } catch (IOException e) { LOG.error("Failed to unregister application", e); } amRMClient.stop(); return success; }
From source file:net.pms.PMS.java
/** * Executes a new Process and creates a fork that waits for its results. * TODO Extend explanation on where this is being used. * @param name Symbolic name for the process to be launched, only used in the trace log * @param error (boolean) Set to true if you want PMS to add error messages to the trace pane * @param workDir (File) optional working directory to run the process in * @param params (array of Strings) array containing the command to call and its arguments * @return Returns true if the command exited as expected * @throws Exception TODO: Check which exceptions to use *///from w w w .j ava 2 s . com private boolean checkProcessExistence(String name, boolean error, File workDir, String... params) throws Exception { logger.debug("launching: " + params[0]); try { ProcessBuilder pb = new ProcessBuilder(params); if (workDir != null) { pb.directory(workDir); } final Process process = pb.start(); OutputTextConsumer stderrConsumer = new OutputTextConsumer(process.getErrorStream(), false); stderrConsumer.start(); OutputTextConsumer outConsumer = new OutputTextConsumer(process.getInputStream(), false); outConsumer.start(); Runnable r = new Runnable() { public void run() { ProcessUtil.waitFor(process); } }; Thread checkThread = new Thread(r, "PMS Checker"); checkThread.start(); checkThread.join(60000); checkThread.interrupt(); checkThread = null; // XXX no longer used if (params[0].equals("vlc") && stderrConsumer.getResults().get(0).startsWith("VLC")) { return true; } // XXX no longer used if (params[0].equals("ffmpeg") && stderrConsumer.getResults().get(0).startsWith("FF")) { return true; } int exit = process.exitValue(); if (exit != 0) { if (error) { logger.info("[" + exit + "] Cannot launch " + name + " / Check the presence of " + params[0] + " ..."); } return false; } return true; } catch (Exception e) { if (error) { logger.error("Cannot launch " + name + " / Check the presence of " + params[0] + " ...", e); } return false; } }
From source file:com.alibaba.jstorm.yarn.appmaster.JstormMaster.java
protected boolean finish() { // wait for completion. String appPath;/*ww w . j a v a2s . c om*/ while (!jstormMasterContext.done) { try { Thread.sleep(JOYConstants.HEARTBEAT_TIME_INTERVAL); appPath = RegistryUtils.servicePath(JOYConstants.APP_TYPE, jstormMasterContext.instanceName, jstormMasterContext.appAttemptID.getApplicationId().toString()); ServiceRecord app = new ServiceRecord(); Date now = new Date(); app.set(JOYConstants.APP_HEARTBEAT_TIME, String.valueOf(now.getTime())); registryOperations.bind(appPath, app, BindFlags.OVERWRITE); } catch (Exception ex) { LOG.error(ex); } } if (timelineClient != null) { publishApplicationAttemptEvent(timelineClient, jstormMasterContext.appAttemptID.toString(), DSEvent.DS_APP_ATTEMPT_END, jstormMasterContext.domainId, appSubmitterUgi); } appPath = RegistryUtils.servicePath(JOYConstants.APP_TYPE, jstormMasterContext.instanceName, jstormMasterContext.appAttemptID.getApplicationId().toString()); try { registryOperations.delete(appPath, true); LOG.info("unRegister application' appPath:" + appPath); } catch (IOException e) { LOG.error("Failed to unRegister application's Registry", e); } // Join all launched threads for (Thread launchThread : launchThreads) { try { launchThread.join(JOYConstants.JOIN_THREAD_TIMEOUT); } catch (InterruptedException e) { LOG.info("Exception thrown in thread join: " + e.getMessage()); e.printStackTrace(); } } // When the application completes, it should stop all running containers LOG.info("Application completed. Stopping running containers"); nmClientAsync.stop(); // When the application completes, it should send a finish application // signal to the RM LOG.info("Application completed. Signalling finish to RM"); FinalApplicationStatus appStatus; String appMessage = null; boolean success = true; if (jstormMasterContext.numFailedContainers.get() == 0 && jstormMasterContext.numCompletedContainers.get() == jstormMasterContext.numTotalContainers) { appStatus = FinalApplicationStatus.SUCCEEDED; } else { appStatus = FinalApplicationStatus.FAILED; appMessage = "Diagnostics." + ", total=" + jstormMasterContext.numTotalContainers + ", completed=" + jstormMasterContext.numCompletedContainers.get() + ", allocated=" + jstormMasterContext.numAllocatedContainers.get() + ", failed=" + jstormMasterContext.numFailedContainers.get(); LOG.info(appMessage); success = false; } try { amRMClient.unregisterApplicationMaster(appStatus, appMessage, null); } catch (YarnException ex) { LOG.error("Failed to unregister application", ex); } catch (IOException e) { LOG.error("Failed to unregister application", e); } amRMClient.stop(); // Stop Timeline Client if (timelineClient != null) { timelineClient.stop(); } return success; }
From source file:edu.uci.ics.asterix.aoya.AsterixApplicationMaster.java
/** * Clean up, whether or not we were successful. *//*from w w w . j a v a 2s. c om*/ private void finish() { // Join all launched threads // needed for when we time out // and we need to release containers for (Thread launchThread : launchThreads) { try { launchThread.join(10000); } catch (InterruptedException e) { LOG.info("Exception thrown in thread join: " + e.getMessage()); //from https://stackoverflow.com/questions/4812570/how-to-store-printstacktrace-into-a-string StringWriter errors = new StringWriter(); e.printStackTrace(new PrintWriter(errors)); LOG.error(errors.toString()); } } // When the application completes, it should stop all running containers LOG.info("Application completed. Stopping running containers"); nmClientAsync.stop(); // When the application completes, it should send a finish application // signal to the RM LOG.info("Application completed. Signalling finish to RM"); FinalApplicationStatus appStatus; String appMessage = null; success = true; if (numFailedContainers.get() == 0 && numCompletedContainers.get() == numTotalContainers) { appStatus = FinalApplicationStatus.SUCCEEDED; } else { appStatus = FinalApplicationStatus.FAILED; appMessage = "Diagnostics." + ", total=" + numTotalContainers + ", completed=" + numCompletedContainers.get() + ", allocated=" + numAllocatedContainers.get() + ", failed=" + numFailedContainers.get(); success = false; } try { resourceManager.unregisterApplicationMaster(appStatus, appMessage, null); } catch (YarnException ex) { LOG.error("Failed to unregister application", ex); } catch (IOException e) { LOG.error("Failed to unregister application", e); } done = true; resourceManager.stop(); }
From source file:de.huberlin.wbi.hiway.am.WorkflowDriver.java
protected void finish() { /* log */ logger.writeEntryToLog(new JsonReportEntry(getRunId(), null, null, null, null, null, HiwayDBI.KEY_WF_TIME, Long.toString(System.currentTimeMillis() - amRMClient.getStartTime()))); // Join all launched threads needed for when we time out and we need to release containers for (Thread launchThread : launchThreads) { try {/* w w w . j av a 2s . co m*/ launchThread.join(10000); } catch (InterruptedException e) { Logger.writeToStdout("Exception thrown in thread join: " + e.getMessage()); e.printStackTrace(System.out); System.exit(-1); } } // When the application completes, it should stop all running containers Logger.writeToStdout("Application completed. Stopping running containers"); nmClientAsync.stop(); // When the application completes, it should send a finish application signal to the RM Logger.writeToStdout("Application completed. Signalling finish to RM"); FinalApplicationStatus appStatus; String appMessage = null; success = true; WorkflowDriver.Logger.writeToStdout("Failed Containers: " + logger.numFailedContainers.get()); WorkflowDriver.Logger.writeToStdout("Completed Containers: " + logger.numCompletedContainers.get()); int numTotalContainers = scheduler.getNumberOfTotalTasks(); // WorkflowDriver.writeToStdout("Total Scheduled Containers: " + numTotalContainers); if (logger.getNumFailedContainers().get() == 0 && logger.getNumCompletedContainers().get() == numTotalContainers) { appStatus = FinalApplicationStatus.SUCCEEDED; } else { appStatus = FinalApplicationStatus.FAILED; appMessage = "Diagnostics." + ", total=" + numTotalContainers + ", completed=" + logger.getNumCompletedContainers().get() + ", allocated=" + logger.getNumAllocatedContainers().get() + ", failed=" + logger.getNumFailedContainers().get() + ", killed=" + logger.getNumKilledContainers().get(); success = false; } Collection<String> output = getOutput(); Collection<Data> outputFiles = getOutputFiles(); if (outputFiles.size() > 0) { String outputs = outputFiles.toString(); logger.writeEntryToLog(new JsonReportEntry(getRunId(), null, null, null, null, null, HiwayDBI.KEY_WF_OUTPUT, outputs.substring(1, outputs.length() - 1))); } try { logger.statLog.close(); logger.federatedReport.stageOut(); if (summaryPath != null) { String stdout = hdfsApplicationDirectory + "/AppMaster.stdout"; String stderr = hdfsApplicationDirectory + "/AppMaster.stderr"; String statlog = hdfsApplicationDirectory + "/" + appId + ".log"; try (BufferedWriter writer = new BufferedWriter(new FileWriter(summaryPath.toString()))) { JSONObject obj = new JSONObject(); try { obj.put("output", output); obj.put("stdout", stdout); obj.put("stderr", stderr); obj.put("statlog", statlog); } catch (JSONException e) { e.printStackTrace(System.out); System.exit(-1); } writer.write(obj.toString()); } new Data("AppMaster.stdout").stageOut(); new Data("AppMaster.stderr").stageOut(); new Data(summaryPath).stageOut(); } } catch (IOException e) { Logger.writeToStdout("Error when attempting to stage out federated output log."); e.printStackTrace(System.out); System.exit(-1); } try { amRMClient.unregisterApplicationMaster(appStatus, appMessage, null); } catch (YarnException | IOException e) { Logger.writeToStdout("Failed to unregister application"); e.printStackTrace(System.out); System.exit(-1); } amRMClient.stop(); if (timelineClient != null) timelineClient.stop(); }