/** Method waits until all initialized {@link SelectorHandler}s will not get stopped */
 protected void waitUntilSeletorHandlersStop() {
   synchronized (stoppedSelectorHandlerCounter) {
     while (stoppedSelectorHandlerCounter.get() > 0
         || !State.STOPPED.equals(stateHolder.getState())) {
       try {
         stoppedSelectorHandlerCounter.wait(1000);
       } catch (InterruptedException ex) {
       }
     }
   }
 }
Example #2
0
 private void rescan() throws IOException {
   try {
     synchronized (processing) {
       while (processing.get() > 0) {
         processing.wait();
       }
     }
     for (WatchKey key : keys.keySet()) {
       key.cancel();
     }
     keys.clear();
     Files.walkFileTree(root, new FilteringFileVisitor());
     synchronized (processing) {
       while (processing.get() > 0) {
         processing.wait();
       }
     }
   } catch (InterruptedException e) {
     throw (IOException) new InterruptedIOException().initCause(e);
   }
 }
Example #3
0
    public void assertDone(int msgExpected) throws Throwable {
      if (_e != null) throw _e;

      long end = System.currentTimeMillis() + 5000;

      synchronized (_msgReceived) {
        while (end > System.currentTimeMillis() && _msgReceived.get() < msgExpected) {
          try {
            _msgReceived.wait(end - System.currentTimeMillis());
          } catch (InterruptedException e) {
          }
        }
      }
      if (_e != null) throw _e;
      if (_msgReceived.get() != msgExpected)
        Assert.fail("Received " + _msgReceived + " messages when expected " + msgExpected);
    }
Example #4
0
  @Override
  public Void call() throws Exception {
    try {
      jobStarted();
      driver.jc.setMonitorCb(
          new MonitorCallback() {
            @Override
            public void call(JavaFutureAction<?> future) {
              sparkJobs.add(future);
              jobSubmitted(future);
            }
          });

      T result = job.call(driver.jc);
      synchronized (completed) {
        while (completed.get() < sparkJobs.size()) {
          LOG.debug(
              "Client job {} finished, {} of {} Spark jobs finished.",
              jobId,
              completed.get(),
              sparkJobs.size());
          completed.wait();
        }
      }

      // make sure job has really succeeded
      // at this point, future.get shall not block us
      for (JavaFutureAction<?> future : sparkJobs) {
        future.get();
      }
      finished(result, null);
    } catch (Throwable t) {
      // Catch throwables in a best-effort to report job status back to the client. It's
      // re-thrown so that the executor can destroy the affected thread (or the JVM can
      // die or whatever would happen if the throwable bubbled up).
      LOG.info("Failed to run job " + jobId, t);
      finished(null, t);
      throw new ExecutionException(t);
    } finally {
      driver.jc.setMonitorCb(null);
      driver.activeJobs.remove(jobId);
    }
    return null;
  }
 /**
  * Waits until a notify
  *
  * @return true if interrupted during the wait
  */
 private final boolean atomicWait(int index) {
   AtomicInteger i = auxStore.getWaiting(index);
   i.incrementAndGet();
   try {
     short blockId = blockIds.get(index);
     boolean reserved = auxStore.isReserved(blockId);
     synchronized (i) {
       if (!reserved || !auxStore.testUnstable(blockId)) {
         return false;
       }
       try {
         i.wait();
       } catch (InterruptedException e) {
         return true;
       }
     }
   } finally {
     i.decrementAndGet();
   }
   return false;
 }
 private void waitForCounterToGetZero(
     String counterName,
     AtomicInteger counter,
     int numberOfExpectedEvents,
     long timeoutInMilliseconds)
     throws TimeoutException, InterruptedException {
   synchronized (counter) {
     counter.set(counter.get() - numberOfExpectedEvents);
     if (counter.get() != 0) {
       counter.wait(timeoutInMilliseconds);
       if (counter.get() != 0) {
         throw new TimeoutException(
             "Received "
                 + (numberOfExpectedEvents + counter.get())
                 + " "
                 + counterName
                 + " event(s) instead of expected "
                 + numberOfExpectedEvents);
       }
     }
   }
 }
  /**
   * Determines the last entry written to a ledger not closed properly due to a client crash
   *
   * @param passwd
   */
  boolean recover(byte[] passwd)
      throws IOException, InterruptedException, BKException, KeeperException {
    /*
     * Create BookieClient objects and send a request to each one.
     */

    for (InetSocketAddress s : bookies) {
      LOG.info(s);
      BookieClient client = new BookieClient(s, 3000);
      clients.add(client);
      client.readEntry(lId, -1, this, null);
    }

    /*
     * Wait until I have received enough responses
     */
    synchronized (counter) {
      LOG.info("Counter: " + counter.get() + ", " + minimum + ", " + qMode);
      if (counter.get() < minimum) {
        LOG.info("Waiting...");
        counter.wait(5000);
      }
    }

    /*
     * Obtain largest hint
     */
    LedgerHandle lh = new LedgerHandle(self, lId, 0, qSize, qMode, passwd);
    for (InetSocketAddress addr : bookies) {
      lh.addBookieForReading(addr);
    }

    boolean notLegitimate = true;
    long readCounter = 0;
    while (notLegitimate) {
      readCounter = getNextHint();
      if (readCounter > -1) {
        lh.setLast(readCounter);
        boolean hasMore = true;
        while (hasMore) {
          hasMore = false;
          LOG.debug("Recovering: " + lh.getLast());
          LedgerSequence ls = lh.readEntries(lh.getLast(), lh.getLast());
          LOG.debug("Received entry for: " + lh.getLast());

          byte[] le = ls.nextElement().getEntry();
          if (le != null) {
            if (notLegitimate) notLegitimate = false;
            lh.addEntry(le);
            hasMore = true;
          }
        }
      } else break;
    }

    /*
     * Write counter as the last entry of ledger
     */
    if (!notLegitimate) {
      lh.setAddConfirmed(readCounter);
      lh.close();

      return true;
    } else {
      lh.setLast(0);
      lh.close();

      return false;
    }
  }
  /**
   * Returns a link for the remote address if cached; otherwise opens, caches and returns. When it
   * opens a link for the remote address, only one attempt for the address is made at a given time
   *
   * @param remoteAddr the remote socket address
   * @param encoder the encoder
   * @param listener the link listener
   * @return a link associated with the address
   */
  @Override
  public <T> Link<T> open(
      final SocketAddress remoteAddr,
      final Encoder<? super T> encoder,
      final LinkListener<? super T> listener)
      throws IOException {

    Link<T> link = null;

    for (int i = 0; i <= this.numberOfTries; ++i) {
      LinkReference linkRef = this.addrToLinkRefMap.get(remoteAddr);

      if (linkRef != null) {
        link = (Link<T>) linkRef.getLink();
        if (LOG.isLoggable(Level.FINE)) {
          LOG.log(Level.FINE, "Link {0} for {1} found", new Object[] {link, remoteAddr});
        }
        if (link != null) {
          return link;
        }
      }

      if (i == this.numberOfTries) {
        // Connection failure
        throw new ConnectException("Connection to " + remoteAddr + " refused");
      }

      LOG.log(
          Level.FINE,
          "No cached link for {0} thread {1}",
          new Object[] {remoteAddr, Thread.currentThread()});

      // no linkRef
      final LinkReference newLinkRef = new LinkReference();
      final LinkReference prior = this.addrToLinkRefMap.putIfAbsent(remoteAddr, newLinkRef);
      final AtomicInteger flag =
          prior != null ? prior.getConnectInProgress() : newLinkRef.getConnectInProgress();

      synchronized (flag) {
        if (!flag.compareAndSet(0, 1)) {
          while (flag.get() == 1) {
            try {
              flag.wait();
            } catch (final InterruptedException ex) {
              LOG.log(Level.WARNING, "Wait interrupted", ex);
            }
          }
        }
      }

      linkRef = this.addrToLinkRefMap.get(remoteAddr);
      link = (Link<T>) linkRef.getLink();

      if (link != null) {
        return link;
      }

      ChannelFuture connectFuture = null;
      try {
        connectFuture = this.clientBootstrap.connect(remoteAddr);
        connectFuture.syncUninterruptibly();

        link = new NettyLink<>(connectFuture.channel(), encoder, listener);
        linkRef.setLink(link);

        synchronized (flag) {
          flag.compareAndSet(1, 2);
          flag.notifyAll();
        }
        break;
      } catch (final Exception e) {
        if (e.getClass().getSimpleName().compareTo("ConnectException") == 0) {
          LOG.log(
              Level.WARNING,
              "Connection refused. Retry {0} of {1}",
              new Object[] {i + 1, this.numberOfTries});
          synchronized (flag) {
            flag.compareAndSet(1, 0);
            flag.notifyAll();
          }

          if (i < this.numberOfTries) {
            try {
              Thread.sleep(retryTimeout);
            } catch (final InterruptedException interrupt) {
              LOG.log(
                  Level.WARNING, "Thread {0} interrupted while sleeping", Thread.currentThread());
            }
          }
        } else {
          throw e;
        }
      }
    }

    return link;
  }
  @Test
  public void testFeedServer() throws Exception {
    final InetSocketAddress serverAddress = startThreadedServer();
    synchronized (m_dataServerStarted) {
      if (!m_dataServerStarted.get()) {
        m_dataServerStarted.wait(3000);
      }
    }
    final ServerDataFeeder feeder = new ServerDataFeeder(serverAddress);

    final Map<InetSocketAddress, Collection<ByteBuffer>> addressesToMessages =
        new HashMap<InetSocketAddress, Collection<ByteBuffer>>();
    final Collection<SendIndication> messages = new LinkedList<SendIndication>();

    final AtomicInteger totalDataSent = new AtomicInteger(0);
    final AtomicInteger totalDataReceived = new AtomicInteger(0);
    final IoSession session =
        new IoSessionStub() {
          @Override
          public WriteFuture write(final Object message) {
            final SendIndication di = (SendIndication) message;
            final InetSocketAddress address = di.getRemoteAddress();
            if (addressesToMessages.containsKey(address)) {
              final Collection<ByteBuffer> bufs = addressesToMessages.get(address);
              addRaw(bufs, di);
            } else {
              final Collection<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
              addressesToMessages.put(address, bufs);
              addRaw(bufs, di);
            }
            messages.add(di);
            if (messages.size() == NUM_REMOTE_HOSTS) {
              synchronized (messages) {
                messages.notify();
              }
            }
            return null;
          }

          // This method is required to extract the data from the TCP frames
          // to verify it matches the original data sent (since the server
          // just echoes everything it sees).
          private void addRaw(final Collection<ByteBuffer> bufs, final SendIndication si) {
            final byte[] rawData = si.getData();
            final byte[] noFrame = ArrayUtils.subarray(rawData, 2, rawData.length);

            totalDataReceived.addAndGet(noFrame.length);
            bufs.add(ByteBuffer.wrap(noFrame));

            synchronized (totalDataReceived) {
              if (totalDataReceived.get() == totalDataSent.get()) {
                totalDataReceived.notify();
              }
            }
          }
        };

    for (int j = 0; j < NUM_MESSAGES_PER_HOST; j++) {
      for (int i = 0; i < NUM_REMOTE_HOSTS; i++) {
        final InetSocketAddress remoteAddress =
            new InetSocketAddress("44.52.67." + (1 + i), 4728 + i);
        feeder.onRemoteAddressOpened(remoteAddress, session);
        feeder.onData(remoteAddress, session, DATA);
        totalDataSent.addAndGet(DATA.length);
      }
    }

    synchronized (m_dataServerSockets) {
      if (m_dataServerSockets.get() < NUM_REMOTE_HOSTS) {
        m_dataServerSockets.wait(6000);
      }
    }

    assertEquals(NUM_REMOTE_HOSTS, m_dataServerSockets.get());

    synchronized (totalDataReceived) {
      if (totalDataReceived.get() < totalDataSent.get()) {
        totalDataReceived.wait(6000);
      }
    }

    assertEquals(totalDataSent.get(), totalDataReceived.get());

    // The messages received should be TURN Send Indications that wrap
    // TCP Frames.  They're already placed in the appropriate buckets for
    // each remote host.  Now we just need to verify the data.

    for (final Map.Entry<InetSocketAddress, Collection<ByteBuffer>> entry :
        addressesToMessages.entrySet()) {
      verifyData(entry.getValue());
    }
  }