Search in sources :

Example 11 with RpcServer

use of com.baidu.hugegraph.rpc.RpcServer in project hugegraph-common by hugegraph.

the class ServerClientTest method testLoadBalancer.

@Test
public void testLoadBalancer() {
    // Init 3 servers
    HugeConfig server3 = config("server3");
    RpcServer rpcServer3 = new RpcServer(server3);
    HugeConfig server4 = config("server4");
    RpcServer rpcServer4 = new RpcServer(server4);
    HugeConfig server5 = config("server5");
    RpcServer rpcServer5 = new RpcServer(server5);
    GraphHelloServiceImpl s3g1 = new GraphHelloServiceImpl("g1");
    GraphHelloServiceImpl s4g1 = new GraphHelloServiceImpl("g1");
    GraphHelloServiceImpl s5g1 = new GraphHelloServiceImpl("g1");
    rpcServer3.config().addService(HelloService.class, s3g1);
    rpcServer4.config().addService(HelloService.class, s4g1);
    rpcServer5.config().addService(HelloService.class, s5g1);
    startServer(rpcServer3);
    startServer(rpcServer4);
    startServer(rpcServer5);
    // Test LB "consistentHash"
    HugeConfig clientLB = config("client-lb");
    RpcClientProvider rpcClientCHash = new RpcClientProvider(clientLB);
    HelloService cHash = rpcClientCHash.config().serviceProxy(HelloService.class);
    Assert.assertEquals("g1: load", cHash.echo("load"));
    Assert.assertEquals(16.8, cHash.sum(10, 6.8), 0.00000001d);
    Assert.assertEquals(16.8, s3g1.result() + s4g1.result() + s5g1.result(), 0.00000001d);
    Assert.assertEquals("g1: load", cHash.echo("load"));
    Assert.assertEquals(16.8, cHash.sum(10, 6.8), 0.00000001d);
    Assert.assertEquals(16.8, s3g1.result() + s4g1.result() + s5g1.result(), 0.00000001d);
    Assert.assertEquals("g1: load", cHash.echo("load"));
    Assert.assertEquals(16.8, cHash.sum(10, 6.8), 0.00000001d);
    Assert.assertEquals(16.8, s3g1.result() + s4g1.result() + s5g1.result(), 0.00000001d);
    s3g1.resetResult();
    s4g1.resetResult();
    s5g1.resetResult();
    // Test LB "roundRobin"
    String lbKey = com.baidu.hugegraph.config.RpcOptions.RPC_CLIENT_LOAD_BALANCER.name();
    clientLB.setProperty(lbKey, "roundRobin");
    RpcClientProvider rpcClientRound = new RpcClientProvider(clientLB);
    HelloService round = rpcClientRound.config().serviceProxy(HelloService.class);
    Assert.assertEquals("g1: load", round.echo("load"));
    Assert.assertEquals(1.1, round.sum(1, 0.1), 0.00000001d);
    Assert.assertEquals(1.1, s3g1.result() + s4g1.result() + s5g1.result(), 0.00000001d);
    Assert.assertEquals("g1: load", round.echo("load"));
    Assert.assertEquals(1.1, round.sum(1, 0.1), 0.00000001d);
    Assert.assertEquals(2.2, s3g1.result() + s4g1.result() + s5g1.result(), 0.00000001d);
    Assert.assertEquals("g1: load", round.echo("load"));
    Assert.assertEquals(1.1, round.sum(1, 0.1), 0.00000001d);
    Assert.assertEquals(3.3, s3g1.result() + s4g1.result() + s5g1.result(), 0.00000001d);
    s3g1.resetResult();
    s4g1.resetResult();
    s5g1.resetResult();
    // Test LB "random"
    clientLB.setProperty(lbKey, "random");
    RpcClientProvider rpcClientRandom = new RpcClientProvider(clientLB);
    HelloService random = rpcClientRandom.config().serviceProxy(HelloService.class);
    Assert.assertEquals("g1: load", random.echo("load"));
    Assert.assertEquals(1.1, random.sum(1, 0.1), 0.00000001d);
    Assert.assertEquals(1.1, s3g1.result() + s4g1.result() + s5g1.result(), 0.00000001d);
    Assert.assertEquals("g1: load", random.echo("load"));
    Assert.assertEquals(1.1, random.sum(1, 0.1), 0.00000001d);
    double sum = s3g1.result() + s4g1.result() + s5g1.result();
    Assert.assertTrue(2.2 == sum || 1.1 == sum);
    Assert.assertEquals("g1: load", random.echo("load"));
    Assert.assertEquals(1.1, random.sum(1, 0.1), 0.00000001d);
    double sum2 = s3g1.result() + s4g1.result() + s5g1.result();
    Assert.assertTrue(sum == sum2 || sum + 1.1 == sum2);
    for (int i = 0; i < 9; i++) {
        Assert.assertEquals(1.1, random.sum(1, 0.1), 0.00000001d);
    }
    Assert.assertEquals(3.3, s3g1.result() + s4g1.result() + s5g1.result(), 0.00000001d);
    s3g1.resetResult();
    s4g1.resetResult();
    s5g1.resetResult();
    // Destroy all
    rpcClientCHash.destroy();
    rpcClientRound.destroy();
    rpcClientRandom.destroy();
    stopServer(rpcServer3);
    stopServer(rpcServer4);
    stopServer(rpcServer5);
}
Also used : RpcServer(com.baidu.hugegraph.rpc.RpcServer) HugeConfig(com.baidu.hugegraph.config.HugeConfig) RpcClientProvider(com.baidu.hugegraph.rpc.RpcClientProvider) Test(org.junit.Test)

Example 12 with RpcServer

use of com.baidu.hugegraph.rpc.RpcServer in project hugegraph-common by hugegraph.

the class ServerClientTest method testExportNoneService.

@Test
public void testExportNoneService() {
    RpcServer rpcServerNoneService = new RpcServer(config(true));
    // Will be ignored if none service added
    rpcServerNoneService.exportAll();
    stopServer(rpcServerNoneService);
}
Also used : RpcServer(com.baidu.hugegraph.rpc.RpcServer) Test(org.junit.Test)

Example 13 with RpcServer

use of com.baidu.hugegraph.rpc.RpcServer in project hugegraph-common by hugegraph.

the class ServerClientTest method testStartServerWithAdaptivePort.

@Test
public void testStartServerWithAdaptivePort() throws IOException {
    // Init server
    RpcServer rpcServerAdaptive = new RpcServer(config("server-adaptive"));
    RpcProviderConfig serverConfig = rpcServerAdaptive.config();
    serverConfig.addService(HelloService.class, new HelloServiceImpl());
    // Start other server bound the port
    int usedPort = rpcServerAdaptive.port();
    InetAddress ip = InetAddress.getByName(rpcServerAdaptive.host());
    ServerSocket inUse = new ServerSocket(usedPort, 50, ip);
    // Start server after the port in use
    startServer(rpcServerAdaptive);
    Assert.assertNotEquals(0, rpcServerAdaptive.port());
    Assert.assertNotEquals(usedPort, rpcServerAdaptive.port());
    // Init client
    HugeConfig config = config(false);
    String url = rpcServerAdaptive.host() + ":" + rpcServerAdaptive.port();
    String remoteUrlKey = com.baidu.hugegraph.config.RpcOptions.RPC_REMOTE_URL.name();
    config.setProperty(remoteUrlKey, url);
    RpcClientProvider rpcClientAdaptive = new RpcClientProvider(config);
    RpcConsumerConfig clientConfig = rpcClientAdaptive.config();
    HelloService client = clientConfig.serviceProxy(HelloService.class);
    // Test call
    Assert.assertEquals("hello tom!", client.hello("tom"));
    Assert.assertEquals("tom", client.echo("tom"));
    Assert.assertEquals(5.14, client.sum(2, 3.14), 0.00000001d);
    // Destroy all
    rpcClientAdaptive.destroy();
    stopServer(rpcServerAdaptive);
    inUse.close();
}
Also used : RpcProviderConfig(com.baidu.hugegraph.rpc.RpcProviderConfig) RpcServer(com.baidu.hugegraph.rpc.RpcServer) ServerSocket(java.net.ServerSocket) RpcConsumerConfig(com.baidu.hugegraph.rpc.RpcConsumerConfig) HugeConfig(com.baidu.hugegraph.config.HugeConfig) RpcClientProvider(com.baidu.hugegraph.rpc.RpcClientProvider) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 14 with RpcServer

use of com.baidu.hugegraph.rpc.RpcServer in project hugegraph-common by hugegraph.

the class ServerClientTest method testStartBothServerAndClientThroughSameConfig.

@Test
public void testStartBothServerAndClientThroughSameConfig() {
    // Init server1
    HugeConfig server1 = config("server1-client");
    RpcServer rpcServer1 = new RpcServer(server1);
    RpcClientProvider rpcClient1 = new RpcClientProvider(server1);
    GraphHelloServiceImpl s1g1 = new GraphHelloServiceImpl("g1");
    GraphHelloServiceImpl s1g2 = new GraphHelloServiceImpl("g2");
    rpcServer1.config().addService(s1g1.graph(), HelloService.class, s1g1);
    rpcServer1.config().addService(s1g2.graph(), HelloService.class, s1g2);
    startServer(rpcServer1);
    // Init server2
    HugeConfig server2 = config("server2-client");
    RpcServer rpcServer2 = new RpcServer(server2);
    RpcClientProvider rpcClient2 = new RpcClientProvider(server2);
    GraphHelloServiceImpl s2g1 = new GraphHelloServiceImpl("g1");
    GraphHelloServiceImpl s2g2 = new GraphHelloServiceImpl("g2");
    rpcServer2.config().addService(s2g1.graph(), HelloService.class, s2g1);
    rpcServer2.config().addService(s2g2.graph(), HelloService.class, s2g2);
    startServer(rpcServer2);
    // Init client1
    HelloService s2g1Client = rpcClient1.config().serviceProxy("g1", HelloService.class);
    HelloService s2g2Client = rpcClient1.config().serviceProxy("g2", HelloService.class);
    // Init client2
    HelloService s1g1Client = rpcClient2.config().serviceProxy("g1", HelloService.class);
    HelloService s1g2Client = rpcClient2.config().serviceProxy("g2", HelloService.class);
    // Test call
    Assert.assertEquals(2.1, s2g1Client.sum(1, 1.1), 0.00000001d);
    Assert.assertEquals(2.2, s2g2Client.sum(1, 1.2), 0.00000001d);
    Assert.assertEquals(1.1, s1g1Client.sum(1, 0.1), 0.00000001d);
    Assert.assertEquals(1.2, s1g2Client.sum(0, 1.2), 0.00000001d);
    Assert.assertEquals(1.1, s1g1.result(), 0.00000001d);
    Assert.assertEquals(1.2, s1g2.result(), 0.00000001d);
    Assert.assertEquals(2.1, s2g1.result(), 0.00000001d);
    Assert.assertEquals(2.2, s2g2.result(), 0.00000001d);
    // Destroy all
    rpcClient1.destroy();
    rpcClient2.destroy();
    stopServer(rpcServer1);
    stopServer(rpcServer2);
}
Also used : RpcServer(com.baidu.hugegraph.rpc.RpcServer) HugeConfig(com.baidu.hugegraph.config.HugeConfig) RpcClientProvider(com.baidu.hugegraph.rpc.RpcClientProvider) Test(org.junit.Test)

Aggregations

RpcServer (com.baidu.hugegraph.rpc.RpcServer)14 Test (org.junit.Test)13 HugeConfig (com.baidu.hugegraph.config.HugeConfig)7 RpcClientProvider (com.baidu.hugegraph.rpc.RpcClientProvider)7 RpcConsumerConfig (com.baidu.hugegraph.rpc.RpcConsumerConfig)4 RpcProviderConfig (com.baidu.hugegraph.rpc.RpcProviderConfig)4 InetAddress (java.net.InetAddress)1 ServerSocket (java.net.ServerSocket)1 BeforeClass (org.junit.BeforeClass)1