Search in sources :

Example 6 with MasterService

use of com.baidu.hugegraph.computer.core.master.MasterService in project hugegraph-computer by hugegraph.

the class HugeGraphComputer method executeMasterService.

private static void executeMasterService(ComputerContext context) {
    try (MasterService masterService = new MasterService()) {
        masterService.init(context.config());
        masterService.execute();
    }
}
Also used : MasterService(com.baidu.hugegraph.computer.core.master.MasterService)

Example 7 with MasterService

use of com.baidu.hugegraph.computer.core.master.MasterService 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)

Example 8 with MasterService

use of com.baidu.hugegraph.computer.core.master.MasterService in project hugegraph-computer by hugegraph.

the class SenderIntegrateTest method testMultiWorkers.

@Test
public void testMultiWorkers() throws InterruptedException {
    int workerCount = 3;
    int partitionCount = 3;
    Thread masterThread = new Thread(() -> {
        String[] args = OptionsBuilder.newInstance().withJobId("local_003").withAlgorithm(PageRankParams.class).withResultName("rank").withResultClass(DoubleValue.class).withMessageClass(DoubleValue.class).withMaxSuperStep(3).withComputationClass(COMPUTATION).withWorkerCount(workerCount).withPartitionCount(partitionCount).withRpcServerHost("127.0.0.1").withRpcServerPort(8090).build();
        try {
            MasterService service = initMaster(args);
            service.execute();
            service.close();
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    });
    List<Thread> workers = new ArrayList<>(workerCount);
    for (int i = 1; i <= workerCount; i++) {
        int port = 8090 + i;
        String dir = "[jobs-" + i + "]";
        workers.add(new Thread(() -> {
            String[] args;
            args = OptionsBuilder.newInstance().withJobId("local_003").withAlgorithm(PageRankParams.class).withResultName("rank").withResultClass(DoubleValue.class).withMessageClass(DoubleValue.class).withMaxSuperStep(3).withComputationClass(COMPUTATION).withWorkerCount(workerCount).withPartitionCount(partitionCount).withTransoprtServerPort(port).withRpcServerRemote("127.0.0.1:8090").withDataDirs(dir).build();
            try {
                WorkerService service = initWorker(args);
                service.execute();
                service.close();
            } catch (Exception e) {
                Assert.fail(e.getMessage());
            }
        }));
    }
    masterThread.start();
    for (Thread worker : workers) {
        worker.start();
    }
    for (Thread worker : workers) {
        worker.join();
    }
    masterThread.join();
}
Also used : DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) ArrayList(java.util.ArrayList) PageRankParams(com.baidu.hugegraph.computer.algorithm.centrality.pagerank.PageRankParams) MasterService(com.baidu.hugegraph.computer.core.master.MasterService) TransportException(com.baidu.hugegraph.computer.core.common.exception.TransportException) WorkerService(com.baidu.hugegraph.computer.core.worker.WorkerService) Test(org.junit.Test)

Aggregations

MasterService (com.baidu.hugegraph.computer.core.master.MasterService)8 DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)5 Test (org.junit.Test)5 Config (com.baidu.hugegraph.computer.core.config.Config)4 WorkerService (com.baidu.hugegraph.computer.core.worker.WorkerService)4 PageRankParams (com.baidu.hugegraph.computer.algorithm.centrality.pagerank.PageRankParams)3 TransportException (com.baidu.hugegraph.computer.core.common.exception.TransportException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutorService (java.util.concurrent.ExecutorService)3 LimitedLogOutput (com.baidu.hugegraph.computer.core.output.LimitedLogOutput)1 MockWorkerService (com.baidu.hugegraph.computer.core.worker.MockWorkerService)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Logger (org.slf4j.Logger)1