Search in sources :

Example 26 with TimeoutException

use of java.util.concurrent.TimeoutException in project hadoop by apache.

the class TestFsVolumeList method testGetNextVolumeWithClosedVolume.

@Test(timeout = 30000)
public void testGetNextVolumeWithClosedVolume() throws IOException {
    FsVolumeList volumeList = new FsVolumeList(Collections.<VolumeFailureInfo>emptyList(), blockScanner, blockChooser);
    final List<FsVolumeImpl> volumes = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        File curDir = new File(baseDir, "nextvolume-" + i);
        curDir.mkdirs();
        FsVolumeImpl volume = new FsVolumeImplBuilder().setConf(conf).setDataset(dataset).setStorageID("storage-id").setStorageDirectory(new StorageDirectory(StorageLocation.parse(curDir.getPath()))).build();
        volume.setCapacityForTesting(1024 * 1024 * 1024);
        volumes.add(volume);
        volumeList.addVolume(volume.obtainReference());
    }
    // Close the second volume.
    volumes.get(1).setClosed();
    try {
        GenericTestUtils.waitFor(new Supplier<Boolean>() {

            @Override
            public Boolean get() {
                return volumes.get(1).checkClosed();
            }
        }, 100, 3000);
    } catch (TimeoutException e) {
        fail("timed out while waiting for volume to be removed.");
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
    }
    for (int i = 0; i < 10; i++) {
        try (FsVolumeReference ref = volumeList.getNextVolume(StorageType.DEFAULT, 128)) {
            // volume No.2 will not be chosen.
            assertNotEquals(ref.getVolume(), volumes.get(1));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) StorageDirectory(org.apache.hadoop.hdfs.server.common.Storage.StorageDirectory) FsVolumeReference(org.apache.hadoop.hdfs.server.datanode.fsdataset.FsVolumeReference) File(java.io.File) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 27 with TimeoutException

use of java.util.concurrent.TimeoutException in project hadoop by apache.

the class TestEditLogTailer method testTriggersLogRollsForAllStandbyNN.

/*
    1. when all NN become standby nn, standby NN execute to roll log,
    it will be failed.
    2. when one NN become active, standby NN roll log success.
   */
@Test
public void testTriggersLogRollsForAllStandbyNN() throws Exception {
    Configuration conf = getConf();
    // Roll every 1s
    conf.setInt(DFSConfigKeys.DFS_HA_LOGROLL_PERIOD_KEY, 1);
    conf.setInt(DFSConfigKeys.DFS_HA_TAILEDITS_PERIOD_KEY, 1);
    conf.setInt(DFSConfigKeys.DFS_HA_TAILEDITS_ALL_NAMESNODES_RETRY_KEY, 100);
    // Have to specify IPC ports so the NNs can talk to each other.
    MiniDFSNNTopology topology = new MiniDFSNNTopology().addNameservice(new MiniDFSNNTopology.NSConf("ns1").addNN(new MiniDFSNNTopology.NNConf("nn1").setIpcPort(ServerSocketUtil.getPort(0, 100))).addNN(new MiniDFSNNTopology.NNConf("nn2").setIpcPort(ServerSocketUtil.getPort(0, 100))).addNN(new MiniDFSNNTopology.NNConf("nn3").setIpcPort(ServerSocketUtil.getPort(0, 100))));
    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).nnTopology(topology).numDataNodes(0).build();
    try {
        cluster.transitionToStandby(0);
        cluster.transitionToStandby(1);
        cluster.transitionToStandby(2);
        try {
            waitForLogRollInSharedDir(cluster, 3);
            fail("After all NN become Standby state, Standby NN should roll log, " + "but it will be failed");
        } catch (TimeoutException ignore) {
        }
        cluster.transitionToActive(0);
        waitForLogRollInSharedDir(cluster, 3);
    } finally {
        cluster.shutdown();
    }
}
Also used : MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) Configuration(org.apache.hadoop.conf.Configuration) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) MiniDFSNNTopology(org.apache.hadoop.hdfs.MiniDFSNNTopology) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 28 with TimeoutException

use of java.util.concurrent.TimeoutException in project hadoop by apache.

the class TestIPC method checkBlocking.

// goal is to jam a handler with a connection, fill the callq with
// connections, in turn jamming the readers - then flood the server and
// ensure that the listener blocks when the reader connection queues fill
@SuppressWarnings("unchecked")
private void checkBlocking(int readers, int readerQ, int callQ) throws Exception {
    // makes it easier
    int handlers = 1;
    final Configuration conf = new Configuration();
    conf.setInt(CommonConfigurationKeys.IPC_SERVER_RPC_READ_CONNECTION_QUEUE_SIZE_KEY, readerQ);
    // send in enough clients to block up the handlers, callq, and readers
    final int initialClients = readers + callQ + handlers;
    // max connections we should ever end up accepting at once
    // 1 = listener
    final int maxAccept = initialClients + readers * readerQ + 1;
    // stress it with 2X the max
    int clients = maxAccept * 2;
    final AtomicInteger failures = new AtomicInteger(0);
    final CountDownLatch callFinishedLatch = new CountDownLatch(clients);
    // start server
    final TestServerQueue server = new TestServerQueue(clients, readers, callQ, handlers, conf);
    CallQueueManager<Call> spy = spy((CallQueueManager<Call>) Whitebox.getInternalState(server, "callQueue"));
    Whitebox.setInternalState(server, "callQueue", spy);
    final InetSocketAddress addr = NetUtils.getConnectAddress(server);
    server.start();
    Client.setConnectTimeout(conf, 10000);
    // instantiate the threads, will start in batches
    Thread[] threads = new Thread[clients];
    for (int i = 0; i < clients; i++) {
        threads[i] = new Thread(new Runnable() {

            @Override
            public void run() {
                Client client = new Client(LongWritable.class, conf);
                try {
                    call(client, new LongWritable(Thread.currentThread().getId()), addr, 60000, conf);
                } catch (Throwable e) {
                    LOG.error(e);
                    failures.incrementAndGet();
                    return;
                } finally {
                    callFinishedLatch.countDown();
                    client.stop();
                }
            }
        });
    }
    // and others not blocking in the race to fill the callq
    for (int i = 0; i < initialClients; i++) {
        threads[i].start();
        if (i == 0) {
            // let first reader block in a call
            server.firstCallLatch.await();
        }
        // wait until reader put a call to callQueue, to make sure all readers
        // are blocking on the queue after initialClients threads are started.
        verify(spy, timeout(100).times(i + 1)).put(Mockito.<Call>anyObject());
    }
    try {
        // wait till everything is slotted, should happen immediately
        GenericTestUtils.waitFor(new Supplier<Boolean>() {

            @Override
            public Boolean get() {
                return server.getNumOpenConnections() >= initialClients;
            }
        }, 100, 3000);
    } catch (TimeoutException e) {
        fail("timed out while waiting for connections to open.");
    }
    LOG.info("(initial clients) need:" + initialClients + " connections have:" + server.getNumOpenConnections());
    LOG.info("ipc layer should be blocked");
    assertEquals(callQ, server.getCallQueueLen());
    assertEquals(initialClients, server.getNumOpenConnections());
    // connection queues should fill and then the listener should block
    for (int i = initialClients; i < clients; i++) {
        threads[i].start();
    }
    Thread.sleep(10);
    try {
        GenericTestUtils.waitFor(new Supplier<Boolean>() {

            @Override
            public Boolean get() {
                return server.getNumOpenConnections() >= maxAccept;
            }
        }, 100, 3000);
    } catch (TimeoutException e) {
        fail("timed out while waiting for connections to open until maxAccept.");
    }
    LOG.info("(max clients) need:" + maxAccept + " connections have:" + server.getNumOpenConnections());
    // check a few times to make sure we didn't go over
    for (int i = 0; i < 4; i++) {
        assertEquals(maxAccept, server.getNumOpenConnections());
        Thread.sleep(100);
    }
    // sanity check that no calls have finished
    assertEquals(clients, callFinishedLatch.getCount());
    LOG.info("releasing the calls");
    server.callBlockLatch.countDown();
    callFinishedLatch.await();
    for (Thread t : threads) {
        t.join();
    }
    assertEquals(0, failures.get());
    server.stop();
}
Also used : Call(org.apache.hadoop.ipc.Server.Call) Configuration(org.apache.hadoop.conf.Configuration) InetSocketAddress(java.net.InetSocketAddress) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongWritable(org.apache.hadoop.io.LongWritable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TimeoutException(java.util.concurrent.TimeoutException) ConnectTimeoutException(org.apache.hadoop.net.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 29 with TimeoutException

use of java.util.concurrent.TimeoutException in project hadoop by apache.

the class TestSaslRPC method testSaslResponseOrdering.

// ensure that for all qop settings, client can handle postponed rpc
// responses.  basically ensures that the rpc server isn't encrypting
// and queueing the responses out of order.
@Test(timeout = 10000)
public void testSaslResponseOrdering() throws Exception {
    SecurityUtil.setAuthenticationMethod(AuthenticationMethod.TOKEN, conf);
    UserGroupInformation.setConfiguration(conf);
    TestTokenSecretManager sm = new TestTokenSecretManager();
    Server server = setupTestServer(conf, 1, sm);
    try {
        final InetSocketAddress addr = NetUtils.getConnectAddress(server);
        final UserGroupInformation clientUgi = UserGroupInformation.createRemoteUser("client");
        clientUgi.setAuthenticationMethod(AuthenticationMethod.TOKEN);
        TestTokenIdentifier tokenId = new TestTokenIdentifier(new Text(clientUgi.getUserName()));
        Token<?> token = new Token<>(tokenId, sm);
        SecurityUtil.setTokenService(token, addr);
        clientUgi.addToken(token);
        clientUgi.doAs(new PrivilegedExceptionAction<Void>() {

            @Override
            public Void run() throws Exception {
                final TestRpcService proxy = getClient(addr, conf);
                final ExecutorService executor = Executors.newCachedThreadPool();
                final AtomicInteger count = new AtomicInteger();
                try {
                    // queue up a bunch of futures for postponed calls serviced
                    // in a random order.
                    Future<?>[] futures = new Future<?>[10];
                    for (int i = 0; i < futures.length; i++) {
                        futures[i] = executor.submit(new Callable<Void>() {

                            @Override
                            public Void call() throws Exception {
                                String expect = "future" + count.getAndIncrement();
                                String answer = convert(proxy.echoPostponed(null, newEchoRequest(expect)));
                                assertEquals(expect, answer);
                                return null;
                            }
                        });
                        try {
                            // ensures the call is initiated and the response is blocked.
                            futures[i].get(100, TimeUnit.MILLISECONDS);
                        } catch (TimeoutException te) {
                            // expected.
                            continue;
                        }
                        Assert.fail("future" + i + " did not block");
                    }
                    // triggers responses to be unblocked in a random order.  having
                    // only 1 handler ensures that the prior calls are already
                    // postponed.  1 handler also ensures that this call will
                    // timeout if the postponing doesn't work (ie. free up handler)
                    proxy.sendPostponed(null, newEmptyRequest());
                    for (int i = 0; i < futures.length; i++) {
                        LOG.info("waiting for future" + i);
                        futures[i].get();
                    }
                } finally {
                    RPC.stopProxy(proxy);
                    executor.shutdownNow();
                }
                return null;
            }
        });
    } finally {
        server.stop();
    }
}
Also used : SaslServer(javax.security.sasl.SaslServer) InetSocketAddress(java.net.InetSocketAddress) Text(org.apache.hadoop.io.Text) InvalidToken(org.apache.hadoop.security.token.SecretManager.InvalidToken) ServiceException(com.google.protobuf.ServiceException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) TimeoutException(java.util.concurrent.TimeoutException) SaslException(javax.security.sasl.SaslException) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 30 with TimeoutException

use of java.util.concurrent.TimeoutException in project hadoop by apache.

the class LambdaTestUtils method await.

/**
   * Wait for a condition to be met, with a retry policy returning the
   * sleep time before the next attempt is made. If, at the end
   * of the timeout period, the condition is still false (or failing with
   * an exception), the timeout handler is invoked, passing in the timeout
   * and any exception raised in the last invocation. The exception returned
   * by this timeout handler is then rethrown.
   * <p>
   * Example: Wait 30s for a condition to be met, with a sleep of 30s
   * between each probe.
   * If the operation is failing, then, after 30s, the timeout handler
   * is called. This returns the exception passed in (if any),
   * or generates a new one.
   * <pre>
   * await(
   *   30 * 1000,
   *   () -> { return 0 == filesystem.listFiles(new Path("/")).length); },
   *   () -> 500),
   *   (timeout, ex) -> ex != null ? ex : new TimeoutException("timeout"));
   * </pre>
   *
   * @param timeoutMillis timeout in milliseconds.
   * Can be zero, in which case only one attempt is made.
   * @param check predicate to evaluate
   * @param retry retry escalation logic
   * @param timeoutHandler handler invoked on timeout;
   * the returned exception will be thrown
   * @return the number of iterations before the condition was satisfied
   * @throws Exception the exception returned by {@code timeoutHandler} on
   * timeout
   * @throws FailFastException immediately if the evaluated operation raises it
   * @throws InterruptedException if interrupted.
   */
public static int await(int timeoutMillis, Callable<Boolean> check, Callable<Integer> retry, TimeoutHandler timeoutHandler) throws Exception {
    Preconditions.checkArgument(timeoutMillis >= 0, "timeoutMillis must be >= 0");
    Preconditions.checkNotNull(timeoutHandler);
    long endTime = Time.now() + timeoutMillis;
    Exception ex = null;
    boolean running = true;
    int iterations = 0;
    while (running) {
        iterations++;
        try {
            if (check.call()) {
                return iterations;
            }
            // the probe failed but did not raise an exception. Reset any
            // exception raised by a previous probe failure.
            ex = null;
        } catch (InterruptedException | FailFastException e) {
            throw e;
        } catch (Exception e) {
            LOG.debug("eventually() iteration {}", iterations, e);
            ex = e;
        }
        running = Time.now() < endTime;
        if (running) {
            int sleeptime = retry.call();
            if (sleeptime >= 0) {
                Thread.sleep(sleeptime);
            } else {
                running = false;
            }
        }
    }
    // timeout
    Exception evaluate = timeoutHandler.evaluate(timeoutMillis, ex);
    if (evaluate == null) {
        // bad timeout handler logic; fall back to GenerateTimeout so the
        // underlying problem isn't lost.
        LOG.error("timeout handler {} did not throw an exception ", timeoutHandler);
        evaluate = new GenerateTimeout().evaluate(timeoutMillis, ex);
    }
    throw evaluate;
}
Also used : TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

TimeoutException (java.util.concurrent.TimeoutException)717 ExecutionException (java.util.concurrent.ExecutionException)229 IOException (java.io.IOException)167 Test (org.junit.Test)131 CountDownLatch (java.util.concurrent.CountDownLatch)73 ArrayList (java.util.ArrayList)67 ExecutorService (java.util.concurrent.ExecutorService)62 Future (java.util.concurrent.Future)45 CancellationException (java.util.concurrent.CancellationException)44 Test (org.testng.annotations.Test)44 File (java.io.File)34 List (java.util.List)34 Map (java.util.Map)32 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)32 HashMap (java.util.HashMap)26 TimeUnit (java.util.concurrent.TimeUnit)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 URI (java.net.URI)20