Search in sources :

Example 1 with MockWorkerService

use of com.baidu.hugegraph.computer.core.worker.MockWorkerService in project hugegraph-computer by hugegraph.

the class AlgorithmTestBase method runAlgorithm.

public static void runAlgorithm(String algorithmParams, String... options) throws InterruptedException {
    final Logger log = Log.logger(AlgorithmTestBase.class);
    ExecutorService pool = Executors.newFixedThreadPool(2);
    CountDownLatch countDownLatch = new CountDownLatch(2);
    Throwable[] exceptions = new Throwable[2];
    pool.submit(() -> {
        WorkerService workerService = null;
        try {
            Map<String, String> params = new HashMap<>();
            params.put(RpcOptions.RPC_REMOTE_URL.name(), "127.0.0.1:8090");
            params.put(ComputerOptions.JOB_ID.name(), "algo_test_job1");
            params.put(ComputerOptions.JOB_WORKERS_COUNT.name(), "1");
            params.put(ComputerOptions.TRANSPORT_SERVER_PORT.name(), "8086");
            params.put(ComputerOptions.BSP_REGISTER_TIMEOUT.name(), "100000");
            params.put(ComputerOptions.BSP_LOG_INTERVAL.name(), "30000");
            params.put(ComputerOptions.BSP_MAX_SUPER_STEP.name(), "10");
            params.put(ComputerOptions.ALGORITHM_PARAMS_CLASS.name(), algorithmParams);
            if (options != null) {
                for (int i = 0; i < options.length; i += 2) {
                    params.put(options[i], options[i + 1]);
                }
            }
            Config config = ComputerContextUtil.initContext(params);
            workerService = new MockWorkerService();
            workerService.init(config);
            workerService.execute();
        } catch (Throwable e) {
            LOG.error("Failed to start worker", e);
            exceptions[0] = e;
            // If worker failed, the master also should quit
            while (countDownLatch.getCount() > 0) {
                countDownLatch.countDown();
            }
        } finally {
            if (workerService != null) {
                workerService.close();
            }
            countDownLatch.countDown();
        }
    });
    pool.submit(() -> {
        MasterService masterService = null;
        try {
            Map<String, String> params = new HashMap<>();
            params.put(RpcOptions.RPC_SERVER_HOST.name(), "localhost");
            params.put(RpcOptions.RPC_SERVER_PORT.name(), "8090");
            params.put(ComputerOptions.JOB_ID.name(), "algo_test_job1");
            params.put(ComputerOptions.JOB_WORKERS_COUNT.name(), "1");
            params.put(ComputerOptions.BSP_REGISTER_TIMEOUT.name(), "100000");
            params.put(ComputerOptions.BSP_LOG_INTERVAL.name(), "30000");
            params.put(ComputerOptions.BSP_MAX_SUPER_STEP.name(), "10");
            params.put(ComputerOptions.ALGORITHM_PARAMS_CLASS.name(), algorithmParams);
            if (options != null) {
                for (int i = 0; i < options.length; i += 2) {
                    params.put(options[i], options[i + 1]);
                }
            }
            Config config = ComputerContextUtil.initContext(params);
            masterService = new MasterService();
            masterService.init(config);
            masterService.execute();
        } catch (Throwable e) {
            LOG.error("Failed to start master", e);
            exceptions[1] = e;
            // If master failed, the worker also should quit
            while (countDownLatch.getCount() > 0) {
                countDownLatch.countDown();
            }
        } finally {
            /*
                 * It must close the service first. The pool will be shutdown
                 * if count down is executed first, and the server thread in
                 * master service will not be closed.
                 */
            if (masterService != null) {
                masterService.close();
            }
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
    pool.shutdownNow();
    Assert.assertFalse(Arrays.asList(exceptions).toString(), existError(exceptions));
}
Also used : HashMap(java.util.HashMap) Config(com.baidu.hugegraph.computer.core.config.Config) Logger(org.slf4j.Logger) CountDownLatch(java.util.concurrent.CountDownLatch) MasterService(com.baidu.hugegraph.computer.core.master.MasterService) MockWorkerService(com.baidu.hugegraph.computer.core.worker.MockWorkerService) WorkerService(com.baidu.hugegraph.computer.core.worker.WorkerService) MockWorkerService(com.baidu.hugegraph.computer.core.worker.MockWorkerService) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

Config (com.baidu.hugegraph.computer.core.config.Config)1 MasterService (com.baidu.hugegraph.computer.core.master.MasterService)1 MockWorkerService (com.baidu.hugegraph.computer.core.worker.MockWorkerService)1 WorkerService (com.baidu.hugegraph.computer.core.worker.WorkerService)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Logger (org.slf4j.Logger)1