Search in sources :

Example 1 with RPCException

use of org.exoplatform.services.rpc.RPCException in project kernel by exoplatform.

the class AbstractRPCService method executeCommandOnCoordinatorMain.

/**
 * Executes a command on the coordinator only. This method is equivalent to the other method of the
 * same type but with the default timeout. The command must be registered first otherwise an
 * {@link RPCException} will be thrown.
 *
 * @param command The command to execute on the coordinator node
 * @param synchronous if true, sets group request mode to org.jgroups.blocks.GroupRequest#GET_ALL,
 * and if false sets it to org.jgroups.blocks.GroupRequest#GET_NONE.
 * @param timeout a timeout after which to throw a replication exception.
 * @param args an array of {@link Serializable} objects corresponding to parameters of the command
 * to execute remotely
 * @return the response of the coordinator.
 * @throws RPCException in the event of problems.
 */
protected Object executeCommandOnCoordinatorMain(RemoteCommand command, boolean synchronous, long timeout, Serializable... args) throws RPCException {
    Address coordinator = this.coordinator;
    Vector<Address> v = new Vector<Address>(1);
    v.add(coordinator);
    List<Object> lResults = excecuteCommand(v, command, synchronous, timeout, args);
    Object result = lResults == null || lResults.size() == 0 ? null : lResults.get(0);
    if (allowFailover && result instanceof MemberHasLeftException) {
        // The failover capabilities have been enabled and the coordinator seems to have left
        if (coordinator.equals(this.coordinator)) {
            synchronized (topologyChangeLock) {
                if (coordinator.equals(this.coordinator)) {
                    if (LOG.isTraceEnabled())
                        LOG.trace("The coordinator did not change yet, we will relaunch the command after " + retryTimeout + " ms or once a topology change has been detected");
                    try {
                        topologyChangeLock.wait(retryTimeout);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        }
        if (LOG.isTraceEnabled())
            LOG.trace("The coordinator has changed, we will automatically retry with the new coordinator");
        return executeCommandOnCoordinator(command, synchronous, timeout, args);
    } else if (result instanceof RPCException) {
        throw (RPCException) result;
    }
    return result;
}
Also used : RPCException(org.exoplatform.services.rpc.RPCException) Address(org.jgroups.Address) Vector(java.util.Vector)

Example 2 with RPCException

use of org.exoplatform.services.rpc.RPCException in project kernel by exoplatform.

the class AbstractRPCService method handle.

/**
 * {@inheritDoc}
 */
public Object handle(Message msg) {
    String commandId = null;
    try {
        // Ensure that the service is fully started before trying to execute any command
        startSignal.await();
        MessageBody body = (MessageBody) msg.getObject();
        commandId = body.getCommandId();
        if (!body.accept(getLocalAddress())) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Command : " + commandId + " needs to be executed on the coordinator " + "only and the local node is not the coordinator, the command will be ignored");
            }
            return null;
        }
        RemoteCommand command = getCommand(commandId);
        if (command == null) {
            return new RPCException("Command " + commandId + " unkown, please register your command first");
        }
        Object execResult = command.execute(body.getArgs());
        if (LOG.isTraceEnabled()) {
            LOG.trace("Command : " + commandId + " executed, result is: " + execResult);
        }
        return execResult;
    } catch (// NOSONAR
    Throwable x) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Problems invoking command.", x);
        }
        return new RPCException("Cannot execute the command " + (commandId == null ? "" : commandId), x);
    }
}
Also used : RPCException(org.exoplatform.services.rpc.RPCException) RemoteCommand(org.exoplatform.services.rpc.RemoteCommand)

Example 3 with RPCException

use of org.exoplatform.services.rpc.RPCException in project kernel by exoplatform.

the class AbstractRPCService method excecuteCommand.

/**
 * Execute the command on all the nodes corresponding to the list of destinations.
 * @param dests the list of members on which the command needs to be executed
 * @param command the command to execute
 * @param synchronous if true, sets group request mode to org.jgroups.blocks.GroupRequest#GET_ALL, and if false sets
 * it to org.jgroups.blocks.GroupRequest#GET_NONE.
 * @param timeout a timeout after which to throw a replication exception.
 * @param args the list of parameters
 * @return a list of responses from all the targeted members of the cluster.
 * @throws RPCException in the event of problems.
 */
protected List<Object> excecuteCommand(final List<Address> dests, RemoteCommand command, final boolean synchronous, final long timeout, Serializable... args) throws RPCException {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkPermission(RPCService.ACCESS_RPC_SERVICE_PERMISSION);
    }
    if (state != State.STARTED) {
        throw new RPCException("Cannot execute any commands if the service is not started, the current state of the service is " + state);
    }
    final String commandId = command.getId();
    if (commands.get(commandId) != command) {
        throw new RPCException("Command " + commandId + " unknown, please register your command first");
    }
    final Message msg = new Message();
    // NOSONAR
    setObject(msg, new MessageBody(dests.size() == 1 && dests != members ? dests.get(0) : null, commandId, args));
    RspList rsps = SecurityHelper.doPrivilegedAction(new PrivilegedAction<RspList>() {

        public RspList run() {
            try {
                return castMessage(dests, msg, synchronous, timeout);
            } catch (Exception e) {
                LOG.error("Could not cast the message corresponding to the command " + commandId + ".", e);
            }
            return null;
        }
    });
    if (LOG.isTraceEnabled())
        LOG.trace("responses: " + rsps);
    if (rsps == null)
        throw new RPCException("Could not get the responses for command " + commandId + ".");
    if (!synchronous)
        // async case
        return Collections.emptyList();
    if (LOG.isTraceEnabled()) {
        LOG.trace("(" + getLocalAddress() + "): responses for command " + commandId + ":\n" + rsps);
    }
    List<Object> retval = new ArrayList<Object>(rsps.size());
    for (Address dest : dests) {
        Rsp rsp = rsps.get(dest);
        if (rsp == null || (rsp.wasSuspected() && !rsp.wasReceived())) {
            // The corresponding member has left
            retval.add(new MemberHasLeftException("No response for the member " + dest + ", this member has probably left the cluster."));
        } else if (!rsp.wasReceived()) {
            retval.add(new RPCException("Replication timeout for " + rsp.getSender() + ", rsp=" + rsp));
        } else {
            Object value = rsp.getValue();
            if (value instanceof RPCException) {
                // if we have any application-level exceptions make sure we throw them!!
                if (LOG.isTraceEnabled())
                    LOG.trace("Recieved exception'" + value + "' from " + rsp.getSender(), (RPCException) value);
            }
            retval.add(value);
        }
    }
    return retval;
}
Also used : Message(org.jgroups.Message) Address(org.jgroups.Address) RspList(org.jgroups.util.RspList) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RPCException(org.exoplatform.services.rpc.RPCException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) Rsp(org.jgroups.util.Rsp) RPCException(org.exoplatform.services.rpc.RPCException)

Example 4 with RPCException

use of org.exoplatform.services.rpc.RPCException 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)

Example 5 with RPCException

use of org.exoplatform.services.rpc.RPCException in project kernel by exoplatform.

the class TestRPCServiceImpl method testStates.

public void testStates() 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;
    RemoteCommand foo = new RemoteCommand() {

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

        public String execute(Serializable[] args) throws Throwable {
            return null;
        }
    };
    try {
        service = new RPCServiceImpl(container.getContext(), params, configManager);
        service.registerCommand(foo);
        try {
            service.executeCommandOnAllNodes(foo, true);
            fail("We expect a RPCException since the current state is not the expected one");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.executeCommandOnAllNodes(foo, 10);
            fail("We expect a RPCException since the current state is not the expected one");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.executeCommandOnCoordinator(foo, true);
            fail("We expect a RPCException since the current state is not the expected one");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.executeCommandOnCoordinator(foo, 10);
            fail("We expect a RPCException since the current state is not the expected one");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.isCoordinator();
            fail("We expect a RPCException since the current state is not the expected one");
        } catch (RPCException e) {
        // OK
        }
        service.start();
        assertEquals(true, service.isCoordinator());
        service.executeCommandOnAllNodes(foo, true);
        service.executeCommandOnAllNodes(foo, 10);
        service.executeCommandOnCoordinator(foo, true);
        service.executeCommandOnCoordinator(foo, 10);
    } finally {
        if (service != null) {
            service.stop();
        }
    }
    try {
        service.executeCommandOnAllNodes(foo, true);
        fail("We expect a RPCException since the current state is not the expected one");
    } catch (RPCException e) {
    // OK
    }
    try {
        service.executeCommandOnAllNodes(foo, 10);
        fail("We expect a RPCException since the current state is not the expected one");
    } catch (RPCException e) {
    // OK
    }
    try {
        service.executeCommandOnCoordinator(foo, true);
        fail("We expect a RPCException since the current state is not the expected one");
    } catch (RPCException e) {
    // OK
    }
    try {
        service.executeCommandOnCoordinator(foo, 10);
        fail("We expect a RPCException since the current state is not the expected one");
    } catch (RPCException e) {
    // OK
    }
}
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)

Aggregations

RPCException (org.exoplatform.services.rpc.RPCException)7 RemoteCommand (org.exoplatform.services.rpc.RemoteCommand)5 InitParams (org.exoplatform.container.xml.InitParams)4 ValueParam (org.exoplatform.container.xml.ValueParam)4 RPCServiceImpl (org.exoplatform.services.rpc.jgv3.RPCServiceImpl)4 ArrayList (java.util.ArrayList)3 Address (org.jgroups.Address)3 Serializable (java.io.Serializable)2 MemberHasLeftException (org.exoplatform.services.rpc.impl.AbstractRPCService.MemberHasLeftException)2 IOException (java.io.IOException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 List (java.util.List)1 Vector (java.util.Vector)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SingleMethodCallCommand (org.exoplatform.services.rpc.SingleMethodCallCommand)1 Message (org.jgroups.Message)1 Rsp (org.jgroups.util.Rsp)1 RspList (org.jgroups.util.RspList)1