public Object runTaskNow(Task task) { boolean pauseThread; synchronized (m_lock) { pauseThread = m_state != STATE_STOPPED && m_state != STATE_PAUSED; // Wait for the worker thread to first terminate its job if (pauseThread) { // System.out.println("MainThread: Pausing WorkerThread"); m_state = STATE_PAUSE; // Wait for worker thread to notify it is paused try { m_lock.wait(); } catch (InterruptedException e) { } // if (m_state != STATE_PAUSED) { // System.err.println("ERROR: WorkerThread notified but not paused !!"); // } // System.out.println("MainThread: WorkerThread paused"); } } // Run the given task boolean retry; do { task.setState(Task.STATE_RUNNING); retry = false; try { task.doTheJob(); task.setState(Task.STATE_FINISHED); task.onSuccess(); } catch (Exception e) { task.setState(Task.STATE_ERROR); retry = task.onError(e); } } while (retry); // if we paused the worker thread, restart it if (pauseThread) { // System.out.println("MainThread: restart WorkerThread "); synchronized (m_lock) { m_state = STATE_WORK; m_lock.notify(); } } return task.getResult(); }
public void run() { // System.out.println("WorkerThread: Starting."); while (m_state != STATE_QUIT) { Task task = null; synchronized (m_lock) { while (m_state != STATE_QUIT && task == null) { // System.out.println("WorkerThread: Checking for new task."); task = (Task) Link.removeFromHead(m_tail); if (task == null || m_state == STATE_PAUSE) { // System.out.println("WorkerThread: Sleeping..."); m_state = STATE_PAUSED; m_lock.notify(); try { m_lock.wait(); } catch (InterruptedException e) { } // System.out.println("WorkerThread: Wakeup !"); } } } if (task != null) { // System.out.println("WorkerThread: Working on task: "+task.getId()); task.setState(Task.STATE_RUNNING); try { task.doTheJob(); task.setState(Task.STATE_FINISHED); task.onSuccess(); } catch (Exception e) { task.setState(Task.STATE_ERROR); if (task.onError(e)) continue; // Task asked for a new attempt } // System.out.println("WorkerThread: Finished."); Thread.yield(); } } // System.out.println("WorkerThread: Exiting."); m_state = STATE_STOPPED; }
public int addTask(Task task) { synchronized (m_lock) { m_tail = Link.addToTail(task, m_tail); m_nbTasks++; task.setId(m_countId++); task.setState(Task.STATE_READY); // Start or wake up worker thread m_state = STATE_WORK; if (m_thread == null) { m_thread = new Thread(this); m_thread.setPriority(Thread.MIN_PRIORITY); m_thread.start(); } else { m_lock.notify(); } return m_countId; } }
/** Adds a previously running task to this tip. This is used in case of jobtracker restarts. */ public Task addRunningTask(TaskAttemptID taskid, String taskTracker, boolean taskCleanup) { // 1 slot is enough for taskCleanup task int numSlotsNeeded = taskCleanup ? 1 : numSlotsRequired; // create the task Task t = null; if (isMapTask()) { LOG.debug( "attempt " + numTaskFailures + " sending skippedRecords " + failedRanges.getIndicesCount()); String splitClass = null; BytesWritable split; if (!jobSetup && !jobCleanup) { splitClass = rawSplit.getClassName(); split = rawSplit.getBytes(); } else { split = new BytesWritable(); } t = new MapTask(jobFile, taskid, partition, splitClass, split, numSlotsNeeded, job.getUser()); } else { t = new ReduceTask(jobFile, taskid, partition, numMaps, numSlotsNeeded, job.getUser()); } if (jobCleanup) { t.setJobCleanupTask(); } if (jobSetup) { t.setJobSetupTask(); } if (taskCleanup) { t.setTaskCleanupTask(); t.setState(taskStatuses.get(taskid).getRunState()); cleanupTasks.put(taskid, taskTracker); } t.setConf(conf); LOG.debug("Launching task with skipRanges:" + failedRanges.getSkipRanges()); t.setSkipRanges(failedRanges.getSkipRanges()); t.setSkipping(skipping); if (failedRanges.isTestAttempt()) { t.setWriteSkipRecs(false); } if (activeTasks.size() >= 1) { speculativeTaskId = taskid; } else { speculativeTaskId = null; } activeTasks.put(taskid, taskTracker); tasks.add(taskid); // Ask JobTracker to note that the task exists // jobtracker.createTaskEntry(taskid, taskTracker, this); /* // code to find call paths to createTaskEntry StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); boolean found = false; for (StackTraceElement s: stackTraceElements) { if (s.getMethodName().indexOf("heartbeat") != -1 || s.getMethodName().indexOf("findTask") != -1 || s.getMethodName().indexOf("createAndAddAttempt") != -1 || s.getMethodName().indexOf("processTaskAttempt") != -1) { found = true; break; } } if (!found) { RuntimeException e = new RuntimeException ("calling addRunningTask from outside heartbeat"); LOG.info(StringUtils.stringifyException(e)); throw (e); } */ // check and set the first attempt if (firstTaskId == null) { firstTaskId = taskid; } return t; }
@Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("addTask")) { taskList.add(new Task("new Task")); /// @todo A new created task should be editable right away. table.editCellAt(taskList.getRowCount() - 1, 0); } if (e.getActionCommand().equals("deleteTask") && (table.getSelectedRow() != -1)) { taskList.remove(table.getSelectedRow()); } if (e.getActionCommand().equals("cross") && (table.getSelectedRow() != -1)) { taskList.get(table.getSelectedRow()).setState(State.crossed); } if (e.getActionCommand().equals("dismiss") && (table.getSelectedRow() != -1)) { taskList.get(table.getSelectedRow()).setState(State.dismissed); } if (e.getActionCommand().equals("workedOn") && (table.getSelectedRow() != -1)) { Task task = taskList.get(table.getSelectedRow()); task.setState(State.crossed); taskList.add(new Task(task.getName())); } // File handling File file = null; Writer writer = null; JAXBContext context; if (e.getActionCommand().equals("save")) { Marshaller m = null; int returnVal = fileChooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); if (!file.getName().endsWith(".xml")) { /// @TODO Append automaticly .xml } try { context = JAXBContext.newInstance(TaskList.class); m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); writer = new FileWriter(file); m.marshal(taskList, writer); } catch (JAXBException exeption) { exeption.printStackTrace(); } catch (IOException exeption) { exeption.printStackTrace(); } finally { try { writer.close(); } catch (Exception exeption) { } } } } if (e.getActionCommand().equals("open")) { Unmarshaller um = null; int returnVal = fileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); // if(!file.getName().endsWith(".xml")){ /// @TODO Append automaticly .xml // } try { context = JAXBContext.newInstance(TaskList.class); um = context.createUnmarshaller(); taskList = (TaskList) um.unmarshal(file); table.setModel(taskList); table.getColumnModel().getColumn(0).setCellRenderer(new TaskRenderer()); } catch (JAXBException exeption) { exeption.printStackTrace(); } finally { try { writer.close(); } catch (Exception exeption) { } } } } taskList.fireTableDataChanged(); }