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();
}
}
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));
}
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();
}
Aggregations