Search in sources :

Example 26 with Future

use of java.util.concurrent.Future 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)

Example 27 with Future

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

the class TwoConcurrentActionPolicy method runOneIteration.

@Override
protected void runOneIteration() {
    Action actionOne = PolicyBasedChaosMonkey.selectRandomItem(actionsOne);
    Action actionTwo = PolicyBasedChaosMonkey.selectRandomItem(actionsTwo);
    Future fOne = executor.submit(new ActionRunner(actionOne));
    Future fTwo = executor.submit(new ActionRunner(actionTwo));
    try {
        fOne.get();
        fTwo.get();
    } catch (InterruptedException e) {
        LOG.warn("Exception occurred during performing action: " + StringUtils.stringifyException(e));
    } catch (ExecutionException ex) {
        LOG.warn("Exception occurred during performing action: " + StringUtils.stringifyException(ex));
    }
}
Also used : Action(org.apache.hadoop.hbase.chaos.actions.Action) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 28 with Future

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

the class PartitionedMobCompactor method compactMobFiles.

/**
   * Compacts the selected small mob files and all the del files.
   * @param request The compaction request.
   * @return The paths of new mob files after compactions.
   * @throws IOException if IO failure is encountered
   */
protected List<Path> compactMobFiles(final PartitionedMobCompactionRequest request) throws IOException {
    Collection<CompactionPartition> partitions = request.compactionPartitions;
    if (partitions == null || partitions.isEmpty()) {
        LOG.info("No partitions of mob files");
        return Collections.emptyList();
    }
    List<Path> paths = new ArrayList<>();
    final Connection c = ConnectionFactory.createConnection(conf);
    final Table table = c.getTable(tableName);
    try {
        Map<CompactionPartitionId, Future<List<Path>>> results = new HashMap<>();
        // compact the mob files by partitions in parallel.
        for (final CompactionPartition partition : partitions) {
            // How to efficiently come up a list of delFiles for one partition?
            // Search the delPartitions and collect all the delFiles for the partition
            // One optimization can do is that if there is no del file, we do not need to
            // come up with startKey/endKey.
            List<StoreFile> delFiles = getListOfDelFilesForPartition(partition, request.getDelPartitions());
            results.put(partition.getPartitionId(), pool.submit(new Callable<List<Path>>() {

                @Override
                public List<Path> call() throws Exception {
                    LOG.info("Compacting mob files for partition " + partition.getPartitionId());
                    return compactMobFilePartition(request, partition, delFiles, c, table);
                }
            }));
        }
        // compact the partitions in parallel.
        List<CompactionPartitionId> failedPartitions = new ArrayList<>();
        for (Entry<CompactionPartitionId, Future<List<Path>>> result : results.entrySet()) {
            try {
                paths.addAll(result.getValue().get());
            } catch (Exception e) {
                // just log the error
                LOG.error("Failed to compact the partition " + result.getKey(), e);
                failedPartitions.add(result.getKey());
            }
        }
        if (!failedPartitions.isEmpty()) {
            // if any partition fails in the compaction, directly throw an exception.
            throw new IOException("Failed to compact the partitions " + failedPartitions);
        }
    } finally {
        try {
            table.close();
        } catch (IOException e) {
            LOG.error("Failed to close the Table", e);
        }
    }
    return paths;
}
Also used : Path(org.apache.hadoop.fs.Path) CompactionPartition(org.apache.hadoop.hbase.mob.compactions.PartitionedMobCompactionRequest.CompactionPartition) Table(org.apache.hadoop.hbase.client.Table) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Connection(org.apache.hadoop.hbase.client.Connection) CompactionPartitionId(org.apache.hadoop.hbase.mob.compactions.PartitionedMobCompactionRequest.CompactionPartitionId) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) StoreFile(org.apache.hadoop.hbase.regionserver.StoreFile) Future(java.util.concurrent.Future)

Example 29 with Future

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

the class TestNMTimelineCollectorManager method testMultithreadedAddAndRemove.

@Test
public void testMultithreadedAddAndRemove() throws Exception {
    final int numApps = 5;
    List<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>();
    for (int i = 0; i < numApps; i++) {
        final ApplicationId appId = ApplicationId.newInstance(0L, i);
        Callable<Boolean> task = new Callable<Boolean>() {

            public Boolean call() {
                AppLevelTimelineCollector collector = new AppLevelTimelineCollector(appId);
                boolean successPut = (collectorManager.putIfAbsent(appId, collector) == collector);
                return successPut && collectorManager.remove(appId);
            }
        };
        tasks.add(task);
    }
    ExecutorService executor = Executors.newFixedThreadPool(numApps);
    try {
        List<Future<Boolean>> futures = executor.invokeAll(tasks);
        for (Future<Boolean> future : futures) {
            assertTrue(future.get());
        }
    } finally {
        executor.shutdownNow();
    }
    // check the keys
    for (int i = 0; i < numApps; i++) {
        final ApplicationId appId = ApplicationId.newInstance(0L, i);
        assertFalse(collectorManager.containsTimelineCollector(appId));
    }
}
Also used : ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) Test(org.junit.Test)

Example 30 with Future

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

the class ProcedureWALPerformanceEvaluation method doWork.

@Override
public int doWork() {
    try {
        setupProcedureStore();
        ExecutorService executor = Executors.newFixedThreadPool(numThreads);
        Future<?>[] futures = new Future<?>[numThreads];
        // Start worker threads.
        long start = System.currentTimeMillis();
        for (int i = 0; i < numThreads; i++) {
            futures[i] = executor.submit(new Worker(start));
        }
        boolean failure = false;
        try {
            for (Future<?> future : futures) {
                long timeout = start + WORKER_THREADS_TIMEOUT_SEC * 1000 - System.currentTimeMillis();
                failure |= (future.get(timeout, TimeUnit.MILLISECONDS).equals(EXIT_FAILURE));
            }
        } catch (Exception e) {
            System.err.println("Exception in worker thread.");
            e.printStackTrace();
            return EXIT_FAILURE;
        }
        executor.shutdown();
        if (failure) {
            return EXIT_FAILURE;
        }
        long timeTaken = System.currentTimeMillis() - start;
        System.out.println("******************************************");
        System.out.println("Num threads    : " + numThreads);
        System.out.println("Num procedures : " + numProcs);
        System.out.println("Sync type      : " + syncType);
        System.out.println("Time taken     : " + (timeTaken / 1000.0f) + "sec");
        System.out.println("******************************************");
        System.out.println("Raw format for scripts");
        System.out.println(String.format("RESULT [%s=%s, %s=%s, %s=%s, %s=%s, %s=%s, " + "total_time_ms=%s]", NUM_PROCS_OPTION.getOpt(), numProcs, STATE_SIZE_OPTION.getOpt(), stateSize, SYNC_OPTION.getOpt(), syncType, NUM_THREADS_OPTION.getOpt(), numThreads, NUM_WALS_OPTION.getOpt(), numWals, timeTaken));
        return EXIT_SUCCESS;
    } catch (IOException e) {
        e.printStackTrace();
        return EXIT_FAILURE;
    } finally {
        tearDownProcedureStore();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

Future (java.util.concurrent.Future)1138 ArrayList (java.util.ArrayList)479 ExecutorService (java.util.concurrent.ExecutorService)445 Test (org.junit.Test)413 ExecutionException (java.util.concurrent.ExecutionException)264 Callable (java.util.concurrent.Callable)206 IOException (java.io.IOException)177 ParallelTest (com.hazelcast.test.annotation.ParallelTest)148 QuickTest (com.hazelcast.test.annotation.QuickTest)148 HashMap (java.util.HashMap)92 List (java.util.List)84 CountDownLatch (java.util.concurrent.CountDownLatch)71 LinkedList (java.util.LinkedList)67 TimeoutException (java.util.concurrent.TimeoutException)63 HashSet (java.util.HashSet)62 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)59 Map (java.util.Map)58 ICompletableFuture (com.hazelcast.core.ICompletableFuture)57 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)53 File (java.io.File)46