Search in sources :

Example 21 with Callable

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

the class TestDomainSocket method testSocketReadEof.

/**
   * Test that we get a read result of -1 on EOF.
   *
   * @throws IOException
   */
@Test(timeout = 180000)
public void testSocketReadEof() throws Exception {
    final String TEST_PATH = new File(sockDir.getDir(), "testSocketReadEof").getAbsolutePath();
    final DomainSocket serv = DomainSocket.bindAndListen(TEST_PATH);
    ExecutorService exeServ = Executors.newSingleThreadExecutor();
    Callable<Void> callable = new Callable<Void>() {

        public Void call() {
            DomainSocket conn;
            try {
                conn = serv.accept();
            } catch (IOException e) {
                throw new RuntimeException("unexpected IOException", e);
            }
            byte[] buf = new byte[100];
            for (int i = 0; i < buf.length; i++) {
                buf[i] = 0;
            }
            try {
                Assert.assertEquals(-1, conn.getInputStream().read());
            } catch (IOException e) {
                throw new RuntimeException("unexpected IOException", e);
            }
            return null;
        }
    };
    Future<Void> future = exeServ.submit(callable);
    DomainSocket conn = DomainSocket.connect(serv.getPath());
    Thread.sleep(50);
    conn.close();
    serv.close();
    future.get(2, TimeUnit.MINUTES);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) File(java.io.File) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 22 with Callable

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

the class TestInMemorySCMStore method testAddResourceRefAddResourceConcurrency.

@Test
public void testAddResourceRefAddResourceConcurrency() throws Exception {
    startEmptyStore();
    final String key = "key1";
    final String fileName = "foo.jar";
    final String user = "user";
    final ApplicationId id = createAppId(1, 1L);
    // add the resource and add the resource ref at the same time
    ExecutorService exec = HadoopExecutors.newFixedThreadPool(2);
    final CountDownLatch start = new CountDownLatch(1);
    Callable<String> addKeyTask = new Callable<String>() {

        public String call() throws Exception {
            start.await();
            return store.addResource(key, fileName);
        }
    };
    Callable<String> addAppIdTask = new Callable<String>() {

        public String call() throws Exception {
            start.await();
            return store.addResourceReference(key, new SharedCacheResourceReference(id, user));
        }
    };
    Future<String> addAppIdFuture = exec.submit(addAppIdTask);
    Future<String> addKeyFuture = exec.submit(addKeyTask);
    // start them at the same time
    start.countDown();
    // get the results
    String addKeyResult = addKeyFuture.get();
    String addAppIdResult = addAppIdFuture.get();
    assertEquals(fileName, addKeyResult);
    System.out.println("addAppId() result: " + addAppIdResult);
    // it may be null or the fileName depending on the timing
    assertTrue(addAppIdResult == null || addAppIdResult.equals(fileName));
    exec.shutdown();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 23 with Callable

use of java.util.concurrent.Callable in project hbase by apache.

the class AssignmentManager method onRegionSplit.

private String onRegionSplit(final RegionState current, final HRegionInfo hri, final ServerName serverName, final RegionStateTransition transition) {
    // it could be a reportRegionTransition RPC retry.
    if (current == null || !current.isSplittingOrSplitOnServer(serverName)) {
        return hri.getShortNameToLog() + " is not splitting on " + serverName;
    }
    // Just return in case of retrying
    if (current.isSplit()) {
        return null;
    }
    final HRegionInfo a = HRegionInfo.convert(transition.getRegionInfo(1));
    final HRegionInfo b = HRegionInfo.convert(transition.getRegionInfo(2));
    RegionState rs_a = regionStates.getRegionState(a);
    RegionState rs_b = regionStates.getRegionState(b);
    if (rs_a == null || !rs_a.isSplittingNewOnServer(serverName) || rs_b == null || !rs_b.isSplittingNewOnServer(serverName)) {
        return "Some daughter is not known to be splitting on " + serverName + ", a=" + rs_a + ", b=" + rs_b;
    }
    if (TEST_SKIP_SPLIT_HANDLING) {
        return "Skipping split message, TEST_SKIP_SPLIT_HANDLING is set";
    }
    regionOffline(hri, State.SPLIT);
    regionOnline(a, serverName, 1);
    regionOnline(b, serverName, 1);
    // User could disable the table before master knows the new region.
    if (getTableStateManager().isTableState(hri.getTable(), TableState.State.DISABLED, TableState.State.DISABLING)) {
        invokeUnAssign(a);
        invokeUnAssign(b);
    } else {
        Callable<Object> splitReplicasCallable = new Callable<Object>() {

            @Override
            public Object call() {
                doSplittingOfReplicas(hri, a, b);
                return null;
            }
        };
        threadPoolExecutorService.submit(splitReplicasCallable);
    }
    return null;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) Callable(java.util.concurrent.Callable)

Example 24 with Callable

use of java.util.concurrent.Callable in project hbase by apache.

the class AssignmentManager method onRegionMerged.

private String onRegionMerged(final RegionState current, final HRegionInfo hri, final ServerName serverName, final RegionStateTransition transition) {
    // it could be a reportRegionTransition RPC retry.
    if (current == null || !current.isMergingNewOrOpenedOnServer(serverName)) {
        return hri.getShortNameToLog() + " is not merging on " + serverName;
    }
    // Just return in case of retrying
    if (current.isOpened()) {
        return null;
    }
    final HRegionInfo a = HRegionInfo.convert(transition.getRegionInfo(1));
    final HRegionInfo b = HRegionInfo.convert(transition.getRegionInfo(2));
    RegionState rs_a = regionStates.getRegionState(a);
    RegionState rs_b = regionStates.getRegionState(b);
    if (rs_a == null || !rs_a.isMergingOnServer(serverName) || rs_b == null || !rs_b.isMergingOnServer(serverName)) {
        return "Some daughter is not known to be merging on " + serverName + ", a=" + rs_a + ", b=" + rs_b;
    }
    regionOffline(a, State.MERGED);
    regionOffline(b, State.MERGED);
    regionOnline(hri, serverName, 1);
    try {
        processFavoredNodesForMerge(hri, a, b);
    } catch (IOException e) {
        LOG.error("Error while processing favored nodes after merge.", e);
        return StringUtils.stringifyException(e);
    }
    // User could disable the table before master knows the new region.
    if (getTableStateManager().isTableState(hri.getTable(), TableState.State.DISABLED, TableState.State.DISABLING)) {
        invokeUnAssign(hri);
    } else {
        Callable<Object> mergeReplicasCallable = new Callable<Object>() {

            @Override
            public Object call() {
                doMergingOfReplicas(hri, a, b);
                return null;
            }
        };
        threadPoolExecutorService.submit(mergeReplicasCallable);
    }
    return null;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) IOException(java.io.IOException) Callable(java.util.concurrent.Callable)

Example 25 with Callable

use of java.util.concurrent.Callable in project hbase by apache.

the class PerformanceEvaluation method doLocalClients.

/*
   * Run all clients in this vm each to its own thread.
   */
static RunResult[] doLocalClients(final TestOptions opts, final Configuration conf) throws IOException, InterruptedException {
    final Class<? extends Test> cmd = determineCommandClass(opts.cmdName);
    assert cmd != null;
    @SuppressWarnings("unchecked") Future<RunResult>[] threads = new Future[opts.numClientThreads];
    RunResult[] results = new RunResult[opts.numClientThreads];
    ExecutorService pool = Executors.newFixedThreadPool(opts.numClientThreads, new ThreadFactoryBuilder().setNameFormat("TestClient-%s").build());
    final Connection con = ConnectionFactory.createConnection(conf);
    for (int i = 0; i < threads.length; i++) {
        final int index = i;
        threads[i] = pool.submit(new Callable<RunResult>() {

            @Override
            public RunResult call() throws Exception {
                TestOptions threadOpts = new TestOptions(opts);
                if (threadOpts.startRow == 0)
                    threadOpts.startRow = index * threadOpts.perClientRunRows;
                RunResult run = runOneClient(cmd, conf, con, threadOpts, new Status() {

                    @Override
                    public void setStatus(final String msg) throws IOException {
                        LOG.info(msg);
                    }
                });
                LOG.info("Finished " + Thread.currentThread().getName() + " in " + run.duration + "ms over " + threadOpts.perClientRunRows + " rows");
                return run;
            }
        });
    }
    pool.shutdown();
    for (int i = 0; i < threads.length; i++) {
        try {
            results[i] = threads[i].get();
        } catch (ExecutionException e) {
            throw new IOException(e.getCause());
        }
    }
    final String test = cmd.getSimpleName();
    LOG.info("[" + test + "] Summary of timings (ms): " + Arrays.toString(results));
    Arrays.sort(results);
    long total = 0;
    for (RunResult result : results) {
        total += result.duration;
    }
    LOG.info("[" + test + "]" + "\tMin: " + results[0] + "ms" + "\tMax: " + results[results.length - 1] + "ms" + "\tAvg: " + (total / results.length) + "ms");
    con.close();
    return results;
}
Also used : Connection(org.apache.hadoop.hbase.client.Connection) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Callable (java.util.concurrent.Callable)850 Test (org.junit.Test)254 ArrayList (java.util.ArrayList)245 ExecutorService (java.util.concurrent.ExecutorService)243 Future (java.util.concurrent.Future)203 IOException (java.io.IOException)118 ExecutionException (java.util.concurrent.ExecutionException)103 CountDownLatch (java.util.concurrent.CountDownLatch)77 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)77 Ignite (org.apache.ignite.Ignite)60 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)58 File (java.io.File)52 HashMap (java.util.HashMap)44 List (java.util.List)44 Map (java.util.Map)33 LinkedList (java.util.LinkedList)32 HashSet (java.util.HashSet)29 IgniteException (org.apache.ignite.IgniteException)27 Message (org.graylog2.plugin.Message)26 Result (org.graylog2.plugin.inputs.Extractor.Result)26