public static void main(String[] args) { Cook cook1 = new Cook("Amigo"); cook1.setQueue(QUEUE); Cook cook2 = new Cook("Bamigo"); cook2.setQueue(QUEUE); Waitor waitor = new Waitor(); cook1.addObserver(waitor); cook2.addObserver(waitor); List<Tablet> tabletList = new ArrayList<>(); for (int i = 0; i < 5; i++) { Tablet tablet = new Tablet(i + 1); tablet.setQueue(QUEUE); tabletList.add(tablet); } Thread cook1Task = new Thread(cook1); Thread cook2Task = new Thread(cook2); cook1Task.start(); cook2Task.start(); Thread task = new Thread(new RandomOrderGeneratorTask(tabletList, ORDER_CREATING_INTERVAL)); task.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { } task.interrupt(); while (!QUEUE.isEmpty()) {} while ((cook1.isBusy()) || (cook2.isBusy())) {} cook1Task.interrupt(); cook2Task.interrupt(); DirectorTablet directorTablet = new DirectorTablet(); directorTablet.printAdvertisementProfit(); directorTablet.printCookWorkloading(); directorTablet.printActiveVideoSet(); directorTablet.printArchivedVideoSet(); }
public ScanBatch read() throws IOException, TabletClosedException { ScanDataSource dataSource = null; Batch results = null; try { try { scannerSemaphore.acquire(); } catch (InterruptedException e) { sawException = true; } // sawException may have occurred within close, so we cannot assume that an interrupted // exception was its cause if (sawException) throw new IllegalStateException("Tried to use scanner after exception occurred."); if (scanClosed) throw new IllegalStateException("Tried to use scanner after it was closed."); if (options.isIsolated()) { if (isolatedDataSource == null) isolatedDataSource = new ScanDataSource(tablet, options); dataSource = isolatedDataSource; } else { dataSource = new ScanDataSource(tablet, options); } SortedKeyValueIterator<Key, Value> iter; if (options.isIsolated()) { if (isolatedIter == null) isolatedIter = new SourceSwitchingIterator(dataSource, true); else isolatedDataSource.reattachFileManager(); iter = isolatedIter; } else { iter = new SourceSwitchingIterator(dataSource, false); } results = tablet.nextBatch( iter, range, options.getNum(), options.getColumnSet(), options.getBatchTimeOut()); if (results.getResults() == null) { range = null; return new ScanBatch(new ArrayList<KVEntry>(), false); } else if (results.getContinueKey() == null) { return new ScanBatch(results.getResults(), false); } else { range = new Range( results.getContinueKey(), !results.isSkipContinueKey(), range.getEndKey(), range.isEndKeyInclusive()); return new ScanBatch(results.getResults(), true); } } catch (IterationInterruptedException iie) { sawException = true; if (tablet.isClosed()) throw new TabletClosedException(iie); else throw iie; } catch (IOException ioe) { if (tablet.shutdownInProgress()) { log.debug("IOException while shutdown in progress ", ioe); throw new TabletClosedException( ioe); // assume IOException was caused by execution of HDFS shutdown hook } sawException = true; dataSource.close(true); throw ioe; } catch (RuntimeException re) { sawException = true; throw re; } finally { // code in finally block because always want // to return mapfiles, even when exception is thrown if (null != dataSource && !options.isIsolated()) { dataSource.close(false); } else if (null != dataSource) { dataSource.detachFileManager(); } if (results != null && results.getResults() != null) tablet.updateQueryStats(results.getResults().size(), results.getNumBytes()); scannerSemaphore.release(); } }