Search in sources :

Example 6 with RPCServiceImpl

use of org.exoplatform.services.rpc.jgv3.RPCServiceImpl in project kernel by exoplatform.

the class TestRPCServiceImpl method testSeveralNodes.

public void testSeveralNodes() throws Exception {
    InitParams params = new InitParams();
    ValueParam paramConf = new ValueParam();
    paramConf.setName(RPCServiceImpl.PARAM_JGROUPS_CONFIG);
    paramConf.setValue("jar:/conf/portal/udp.xml");
    params.addParameter(paramConf);
    RPCServiceImpl service1 = null, service2 = null;
    try {
        service1 = new RPCServiceImpl(container.getContext(), params, configManager);
        service2 = new RPCServiceImpl(container.getContext(), params, configManager);
        RemoteCommand CmdUnknownOnNode2 = new RemoteCommand() {

            public String getId() {
                return "CmdUnknownOnNode2";
            }

            public String execute(Serializable[] args) throws Throwable {
                return "OK";
            }
        };
        service1.registerCommand(CmdUnknownOnNode2);
        RemoteCommand ExceptionOnNode2 = new RemoteCommand() {

            public String getId() {
                return "ExceptionOnNode2";
            }

            public String execute(Serializable[] args) throws Throwable {
                return "OK";
            }
        };
        service1.registerCommand(ExceptionOnNode2);
        RemoteCommand ErrorOnNode2 = new RemoteCommand() {

            public String getId() {
                return "ErrorOnNode2";
            }

            public String execute(Serializable[] args) throws Throwable {
                return "OK";
            }
        };
        service1.registerCommand(ErrorOnNode2);
        RemoteCommand LongTaskOnNode2 = new RemoteCommand() {

            public String getId() {
                return "LongTaskOnNode2";
            }

            public String execute(Serializable[] args) throws Throwable {
                return "OK";
            }
        };
        service1.registerCommand(LongTaskOnNode2);
        service1.registerCommand(new RemoteCommand() {

            public String getId() {
                return "LongTask";
            }

            public String execute(Serializable[] args) throws Throwable {
                Thread.sleep(3000);
                return "OldCoordinator";
            }
        });
        service1.registerCommand(new RemoteCommand() {

            public String getId() {
                return "OK";
            }

            public String execute(Serializable[] args) throws Throwable {
                return "OK";
            }
        });
        service2.registerCommand(new RemoteCommand() {

            public String getId() {
                return "ExceptionOnNode2";
            }

            public String execute(Serializable[] args) throws Throwable {
                throw new Exception("MyException");
            }
        });
        service2.registerCommand(new RemoteCommand() {

            public String getId() {
                return "ErrorOnNode2";
            }

            public String execute(Serializable[] args) throws Throwable {
                throw new Error("MyError");
            }
        });
        service2.registerCommand(new RemoteCommand() {

            public String getId() {
                return "LongTaskOnNode2";
            }

            public String execute(Serializable[] args) throws Throwable {
                Thread.sleep(2000);
                return null;
            }
        });
        RemoteCommand OK = new RemoteCommand() {

            public String getId() {
                return "OK";
            }

            public String execute(Serializable[] args) throws Throwable {
                return "OK";
            }
        };
        service2.registerCommand(OK);
        final RemoteCommand LongTask = new RemoteCommand() {

            public String getId() {
                return "LongTask";
            }

            public String execute(Serializable[] args) throws Throwable {
                return "NewCoordinator";
            }
        };
        service2.registerCommand(LongTask);
        MyListener listener1 = new MyListener();
        service1.registerTopologyChangeListener(listener1);
        MyListener listener2 = new MyListener();
        service2.registerTopologyChangeListener(listener2);
        assertFalse(listener1.coordinatorHasChanged);
        assertFalse(listener1.isCoordinator);
        assertEquals(0, listener1.count);
        assertFalse(listener2.coordinatorHasChanged);
        assertFalse(listener2.isCoordinator);
        assertEquals(0, listener2.count);
        service1.start();
        assertFalse(listener1.coordinatorHasChanged);
        assertTrue(listener1.isCoordinator);
        assertEquals(1, listener1.count);
        assertFalse(listener2.coordinatorHasChanged);
        assertFalse(listener2.isCoordinator);
        assertEquals(0, listener2.count);
        service2.start();
        assertFalse(listener1.coordinatorHasChanged);
        assertTrue(listener1.isCoordinator);
        assertEquals(2, listener1.count);
        assertFalse(listener2.coordinatorHasChanged);
        assertFalse(listener2.isCoordinator);
        assertEquals(1, listener2.count);
        assertEquals(true, service1.isCoordinator());
        assertEquals(false, service2.isCoordinator());
        List<Object> result;
        Object o;
        result = service1.executeCommandOnAllNodes(CmdUnknownOnNode2, true);
        assertTrue(result != null && result.size() == 2);
        assertEquals("OK", result.get(0));
        assertTrue("We expect a RPCException since the command is unknown on node 2", result.get(1) instanceof RPCException);
        o = service1.executeCommandOnCoordinator(CmdUnknownOnNode2, true);
        assertEquals("OK", o);
        result = service1.executeCommandOnAllNodes(ExceptionOnNode2, true);
        assertTrue(result != null && result.size() == 2);
        assertEquals("OK", result.get(0));
        assertTrue("We expect a RPCException since the command fails on node 2", result.get(1) instanceof RPCException);
        o = service1.executeCommandOnCoordinator(ExceptionOnNode2, true);
        assertEquals("OK", o);
        result = service1.executeCommandOnAllNodes(ErrorOnNode2, true);
        assertTrue(result != null && result.size() == 2);
        assertEquals("OK", result.get(0));
        assertTrue("We expect a RPCException since the command fails on node 2", result.get(1) instanceof RPCException);
        o = service1.executeCommandOnCoordinator(ErrorOnNode2, true);
        assertEquals("OK", o);
        result = service1.executeCommandOnAllNodes(LongTaskOnNode2, 1000);
        assertNotNull(result);
        assertTrue(result.size() == 2);
        assertEquals("OK", result.get(0));
        assertTrue("We expect an RPCException due to a Replication Timeout", result.get(1) instanceof RPCException);
        o = service1.executeCommandOnCoordinator(LongTaskOnNode2, 1000);
        assertEquals("OK", o);
        List<Address> allMembers = service1.members;
        List<Address> coordinatorOnly = new ArrayList<Address>(1);
        coordinatorOnly.add(service1.coordinator);
        final RPCServiceImpl service = service2;
        final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
        final CountDownLatch doneSignal = new CountDownLatch(1);
        Thread t = new Thread() {

            @Override
            public void run() {
                try {
                    Object o = service.executeCommandOnCoordinator(LongTask, true);
                    assertEquals("NewCoordinator", o);
                } catch (Throwable e) {
                    error.set(e);
                    e.printStackTrace();
                } finally {
                    doneSignal.countDown();
                }
            }
        };
        t.start();
        service1.stop();
        listener2.waitTopologyChange();
        assertFalse(listener1.coordinatorHasChanged);
        assertTrue(listener1.isCoordinator);
        assertEquals(2, listener1.count);
        assertTrue(listener2.coordinatorHasChanged);
        assertTrue(listener2.isCoordinator);
        assertEquals(2, listener2.count);
        doneSignal.await();
        assertNull(error.get() != null ? error.get().getMessage() : "", error.get());
        result = service2.excecuteCommand(allMembers, OK, true, 0);
        assertNotNull(result);
        assertTrue(result.size() == 2);
        assertTrue("We expect an RPCException due to a member that has left", result.get(0) instanceof MemberHasLeftException);
        assertEquals("OK", result.get(1));
        result = service2.excecuteCommand(coordinatorOnly, OK, true, 0);
        assertNotNull(result);
        assertTrue(result.size() == 1);
        assertTrue("We expect an RPCException due to a member that has left", result.get(0) instanceof MemberHasLeftException);
        try {
            service1.isCoordinator();
            fail("We expect a RPCException since the current state is not the expected one");
        } catch (RPCException e) {
        // OK
        }
        assertEquals(true, service2.isCoordinator());
    } finally {
        if (service1 != null) {
            service1.stop();
        }
        if (service2 != null) {
            service2.stop();
        }
    }
}
Also used : InitParams(org.exoplatform.container.xml.InitParams) Serializable(java.io.Serializable) Address(org.jgroups.Address) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) RPCException(org.exoplatform.services.rpc.RPCException) MemberHasLeftException(org.exoplatform.services.rpc.impl.AbstractRPCService.MemberHasLeftException) RPCException(org.exoplatform.services.rpc.RPCException) RemoteCommand(org.exoplatform.services.rpc.RemoteCommand) ValueParam(org.exoplatform.container.xml.ValueParam) RPCServiceImpl(org.exoplatform.services.rpc.jgv3.RPCServiceImpl) MemberHasLeftException(org.exoplatform.services.rpc.impl.AbstractRPCService.MemberHasLeftException)

Example 7 with RPCServiceImpl

use of org.exoplatform.services.rpc.jgv3.RPCServiceImpl in project kernel by exoplatform.

the class TestRPCServiceImpl method testCommands.

public void testCommands() throws Exception {
    InitParams params = new InitParams();
    ValueParam paramConf = new ValueParam();
    paramConf.setName(RPCServiceImpl.PARAM_JGROUPS_CONFIG);
    paramConf.setValue("jar:/conf/portal/udp.xml");
    params.addParameter(paramConf);
    RPCServiceImpl service = null;
    try {
        service = new RPCServiceImpl(container.getContext(), params, configManager);
        RemoteCommand fake = new RemoteCommand() {

            public String getId() {
                return "fake";
            }

            public String execute(Serializable[] args) throws Throwable {
                return null;
            }
        };
        RemoteCommand fake2 = new RemoteCommand() {

            public String getId() {
                return "fake2";
            }

            public String execute(Serializable[] args) throws Throwable {
                return null;
            }
        };
        RemoteCommand fake2_Unregistered = new RemoteCommand() {

            public String getId() {
                return "fake2";
            }

            public String execute(Serializable[] args) throws Throwable {
                return null;
            }
        };
        service.registerCommand(fake2);
        RemoteCommand Exception = new RemoteCommand() {

            public String getId() {
                return "Exception";
            }

            public String execute(Serializable[] args) throws Throwable {
                throw new Exception("MyException");
            }
        };
        service.registerCommand(Exception);
        RemoteCommand Error = new RemoteCommand() {

            public String getId() {
                return "Error";
            }

            public String execute(Serializable[] args) throws Throwable {
                throw new Error("MyError");
            }
        };
        service.registerCommand(Error);
        RemoteCommand StringValue = new RemoteCommand() {

            public String getId() {
                return "StringValue";
            }

            public String execute(Serializable[] args) throws Throwable {
                return "OK";
            }
        };
        service.registerCommand(StringValue);
        RemoteCommand NullValue = new RemoteCommand() {

            public String getId() {
                return "NullValue";
            }

            public String execute(Serializable[] args) throws Throwable {
                return null;
            }
        };
        service.registerCommand(NullValue);
        RemoteCommand LongTask = new RemoteCommand() {

            public String getId() {
                return "LongTask";
            }

            public String execute(Serializable[] args) throws Throwable {
                Thread.sleep(2000);
                return null;
            }
        };
        service.registerCommand(LongTask);
        service.start();
        try {
            service.executeCommandOnAllNodes(fake, true);
            fail("We expect a RPCException since the command is unknown");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.executeCommandOnCoordinator(fake, true);
            fail("We expect a RPCException since the command is unknown");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.executeCommandOnAllNodes(fake2_Unregistered, true);
            fail("We expect a RPCException since the command is unknown");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.executeCommandOnCoordinator(fake2_Unregistered, true);
            fail("We expect a RPCException since the command is unknown");
        } catch (RPCException e) {
        // OK
        }
        List<Object> result;
        result = service.executeCommandOnAllNodes(Exception, true);
        assertTrue(result != null && result.size() == 1);
        assertTrue("We expect a RPCException since one node could not execute the command", result.get(0) instanceof RPCException);
        try {
            service.executeCommandOnCoordinator(Exception, true);
            fail("We expect a RPCException since one node could not execute the command");
        } catch (RPCException e) {
        // OK
        }
        result = service.executeCommandOnAllNodes(Error, true);
        assertTrue(result != null && result.size() == 1);
        assertTrue("We expect a RPCException since one node could not execute the command", result.get(0) instanceof RPCException);
        try {
            service.executeCommandOnCoordinator(Error, true);
            fail("We expect a RPCException since one node could not execute the command");
        } catch (RPCException e) {
        // OK
        }
        result = service.executeCommandOnAllNodes(LongTask, true);
        assertNotNull(result);
        assertTrue(result.size() == 1);
        assertNull(result.get(0));
        Object o = service.executeCommandOnCoordinator(LongTask, true);
        assertNull(o);
        result = service.executeCommandOnAllNodes(LongTask, 1000);
        assertNotNull(result);
        assertTrue(result.size() == 1);
        assertTrue("We expect an RPCException due to a Replication Timeout", result.get(0) instanceof RPCException);
        try {
            service.executeCommandOnCoordinator(LongTask, 1000);
            fail("We expect an RPCException due to a Replication Timeout");
        } catch (RPCException e) {
        // OK
        }
        result = service.executeCommandOnAllNodes(LongTask, false);
        assertNotNull(result);
        assertTrue(result.isEmpty());
        assertNull(service.executeCommandOnCoordinator(LongTask, false));
        result = service.executeCommandOnAllNodes(StringValue, true);
        assertNotNull(result);
        assertTrue(result.size() == 1);
        assertEquals("OK", result.get(0));
        o = service.executeCommandOnCoordinator(StringValue, true);
        assertEquals("OK", o);
        result = service.executeCommandOnAllNodes(NullValue, true);
        assertNotNull(result);
        assertTrue(result.size() == 1);
        assertNull(result.get(0));
        o = service.executeCommandOnCoordinator(NullValue, true);
        assertNull(o);
    } finally {
        if (service != null) {
            service.stop();
        }
    }
}
Also used : InitParams(org.exoplatform.container.xml.InitParams) RPCException(org.exoplatform.services.rpc.RPCException) RemoteCommand(org.exoplatform.services.rpc.RemoteCommand) ValueParam(org.exoplatform.container.xml.ValueParam) RPCServiceImpl(org.exoplatform.services.rpc.jgv3.RPCServiceImpl) RPCException(org.exoplatform.services.rpc.RPCException) MemberHasLeftException(org.exoplatform.services.rpc.impl.AbstractRPCService.MemberHasLeftException)

Aggregations

InitParams (org.exoplatform.container.xml.InitParams)7 ValueParam (org.exoplatform.container.xml.ValueParam)7 RPCServiceImpl (org.exoplatform.services.rpc.jgv3.RPCServiceImpl)7 RemoteCommand (org.exoplatform.services.rpc.RemoteCommand)5 RPCException (org.exoplatform.services.rpc.RPCException)4 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 MemberHasLeftException (org.exoplatform.services.rpc.impl.AbstractRPCService.MemberHasLeftException)2 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ConfigurationManager (org.exoplatform.container.configuration.ConfigurationManager)1 SingleMethodCallCommand (org.exoplatform.services.rpc.SingleMethodCallCommand)1 Address (org.jgroups.Address)1 Before (org.junit.Before)1