public void sendrecv(Request request, Response response, long timeout) throws IOException { synchronized (response_map) { makeKey(request); response.isReceived = false; try { response_map.put(request, response); doSend(request); response.expiration = System.currentTimeMillis() + timeout; while (!response.isReceived) { response_map.wait(timeout); timeout = response.expiration - System.currentTimeMillis(); if (timeout <= 0) { throw new TransportException(name + " timedout waiting for response to " + request); } } } catch (IOException ioe) { if (log.level > 2) ioe.printStackTrace(log); try { disconnect(true); } catch (IOException ioe2) { ioe2.printStackTrace(log); } throw ioe; } catch (InterruptedException ie) { throw new TransportException(ie); } finally { response_map.remove(request); } } }
@Override public RenderTask takeTask() { try { synchronized (taskResultListeners) { while (taskResultListeners.size() == 0) taskResultListeners.wait(); return getRandom(taskResultListeners.values()).task; } } catch (InterruptedException e) { Thread.currentThread().interrupt(); return null; } }
@Override public void run() { try { ArrayList<TCPConnection> timeout_list = new ArrayList<>(); while (true) { timeout_list.clear(); synchronized (connection_pool) { // We check the connections every 2 minutes, connection_pool.wait(2 * 60 * 1000); long time_now = System.currentTimeMillis(); Set<ServiceAddress> s = connection_pool.keySet(); // For each key entry, Iterator<ServiceAddress> i = s.iterator(); while (i.hasNext()) { ServiceAddress addr = i.next(); TCPConnection c = connection_pool.get(addr); // System.out.println("CHECK: " + (c.last_lock_timestamp+(10*1000)) + " - // " + time_now); // System.out.println("c.lock_count = " + c.lock_count); // If lock is 0, and past timeout, we can safely remove it. // The timeout on a connection is 5 minutes plus the poll artifact if (c.lock_count == 0 && c.last_lock_timestamp + (5 * 60 * 1000) < time_now) { i.remove(); timeout_list.add(c); } } // If the thread was stopped, we finish the run method which stops // the thread. if (stopped) { return; } } // synchronized (connection_pool) // For each connection that timed out, for (TCPConnection c : timeout_list) { // System.out.println("KILLING: " + c.s); DataOutputStream dout = new DataOutputStream(c.out); // Write the stream close message, and flush, try { dout.writeChar('e'); dout.flush(); c.s.close(); } catch (IOException e) { log.log(Level.SEVERE, "Failed to dispose timed out connection", e); } } } } catch (InterruptedException e) { // Thread was killed, } }
public void runBenchmark() { /* Read requests are done by zookeeper extremely * quickly compared with write requests. If the time * interval and threshold are not chosen appropriately, * it could happen that when the timer awakes, all requests * have already been finished. In this case, the output * of read test doesn't reflect the actual rate of * read requests. */ doTest(TestType.READ, "warm-up"); doTest(TestType.READ, "znode read"); // Do twice to allow for warm-up doTest(TestType.SETSINGLE, "repeated single-znode write"); doTest(TestType.CREATE, "znode create"); doTest(TestType.SETMULTI, "different znode write"); /* In the test, node creation and deletion tests are * done by creating a lot of nodes at first and then * deleting them. Since both of these two tests run * for a certain time, there is no guarantee that which * requests is more than the other. If there are more * delete requests than create requests, the extra delete * requests would end up not actually deleting anything. * Though these requests are sent and processed by * zookeeper server anyway, this could still be an issue.*/ doTest(TestType.DELETE, "znode delete"); LOG.info("Tests completed, now cleaning-up"); for (int i = 0; i < _clients.length; i++) { _clients[i].setTest(TestType.CLEANING); Thread tmp = new Thread(_clients[i]); _running.put(new Integer(i), tmp); tmp.start(); } while (!_finished) { synchronized (_running) { try { _running.wait(); } catch (InterruptedException e) { LOG.warn("Benchmark main thread was interrupted while waiting", e); } } } LOG.info("All tests are complete"); }
public void resolve(IbisIdentifier toResolve, String ibisName) throws IOException { if (logger.isDebugEnabled()) { logger.debug("Making Resolve Request for: " + ibisName); } Integer id = new Integer(toResolve.hashCode()); synchronized (resolveQueue) { // Make sure we don't collide while (resolveQueue.get(id) != null) { try { resolveQueue.wait(); } catch (InterruptedException e) { // Ignored } } resolveQueue.put(id, toResolve); } synchronized (toResolve) { this.requestPort.connect(toResolve, resolvePortName); if (logger.isDebugEnabled()) { logger.debug("Sending Request for: " + ibisName); } WriteMessage writeMessage = this.requestPort.newMessage(); writeMessage.writeByte(OPP_REQUEST); writeMessage.writeInt(toResolve.hashCode()); if (logger.isDebugEnabled()) { logger.debug("Finishing Request for: " + ibisName); } writeMessage.finish(); if (logger.isDebugEnabled()) { logger.debug("Disconnecting Request for: " + ibisName); } this.requestPort.disconnect(toResolve, resolvePortName); while (!ibis.isResolved(toResolve)) { try { // Wait for the resolution to finish. if (logger.isDebugEnabled()) { logger.debug("Waiting For Resolution For: " + ibisName + " on: " + this); } toResolve.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (logger.isDebugEnabled()) { logger.debug("Resolution Complete for: " + ibisName); } } }
private static Object checkLookupTable(String host) { // make sure obj is null. Object obj = null; synchronized (lookupTable) { // If the host isn't in the lookupTable, add it in the // lookuptable and return null. The caller should do // the lookup. if (lookupTable.containsKey(host) == false) { lookupTable.put(host, null); return obj; } // If the host is in the lookupTable, it means that another // thread is trying to look up the address of this host. // This thread should wait. while (lookupTable.containsKey(host)) { try { lookupTable.wait(); } catch (InterruptedException e) { } } } // The other thread has finished looking up the address of // the host. This thread should retry to get the address // from the addressCache. If it doesn't get the address from // the cache, it will try to look up the address itself. obj = getCachedAddress(host); if (obj == null) { synchronized (lookupTable) { lookupTable.put(host, null); } } return obj; }
public void doTest(TestType test, String description) { _currentTest = test; _finishedTotal = new AtomicInteger(0); _lastfinished = 0; _currentTotalOps = new AtomicInteger(_totalOps); _finished = false; System.out.print( "Running " + description + " benchmark for " + _totalTimeSeconds + " seconds... "); try { _rateFile = new BufferedWriter(new FileWriter(new File(test + ".dat"))); } catch (IOException e) { LOG.error("Unable to create output file", e); } _startCpuTime = System.nanoTime(); _lastCpuTime = _startCpuTime; // Start the testing clients! for (int i = 0; i < _clients.length; i++) { _clients[i].setTest(test); Thread tmp = new Thread(_clients[i]); _running.put(new Integer(i), tmp); tmp.start(); } // Wait for clients to connect to their assigned server, and // start timer which ensures we have outstanding requests. try { _barrier.await(); } catch (BrokenBarrierException e) { LOG.warn("Some other client was interrupted; Benchmark main thread is out of sync", e); } catch (InterruptedException e) { LOG.warn("Benchmark main thread was interrupted while waiting on barrier", e); } Timer timer = new Timer(); timer.scheduleAtFixedRate(new ResubmitTimer(), _interval, _interval); // Wait for the test to finish while (!_finished) { synchronized (_running) { try { _running.wait(); } catch (InterruptedException e) { LOG.warn("Benchmark main thread was interrupted while waiting", e); } } } // Test is finished _currentTest = TestType.UNDEFINED; timer.cancel(); try { if (_rateFile != null) { _rateFile.close(); } } catch (IOException e) { LOG.warn("Error while closing output file", e); } double time = getTime(); LOG.info( test + " finished, time elapsed (sec): " + time + " operations: " + _finishedTotal.get() + " avg rate: " + _finishedTotal.get() / time); System.out.println("done"); }