Search in sources :

Example 1 with RequestID

use of edu.iu.dsc.tws.api.net.request.RequestID in project twister2 by DSC-SPIDAL.

the class RRClient method sendRequestWaitResponse.

/**
 * return requestID and response message
 * throw an exception with the failure reason
 * @param message message
 * @param waitLimit waitlimit
 * @return request id
 */
public Tuple<RequestID, Message> sendRequestWaitResponse(Message message, long waitLimit) throws BlockingSendException {
    // if this method is already called and waiting for a response
    if (requestIdOfWaitedResponse != null) {
        throw new BlockingSendException(BlockingSendFailureReason.ALREADY_SENDING_ANOTHER_MESSAGE, "Already sending another message.", null);
    }
    synchronized (responseWaitObject) {
        responseReceived = false;
        errorWhenSending = false;
        RequestID requestID = sendRequest(message);
        requestIdOfWaitedResponse = requestID;
        if (requestIdOfWaitedResponse == null) {
            throw new BlockingSendException(BlockingSendFailureReason.ERROR_WHEN_TRYING_TO_SEND, "Problem when trying to send the message.", null);
        }
        try {
            responseWaitObject.wait(waitLimit);
            if (errorWhenSending) {
                throw new BlockingSendException(BlockingSendFailureReason.CONNECTION_LOST_WHEN_SENDING, "Connection lost when sending the message.", null);
            }
            if (!responseReceived) {
                throw new BlockingSendException(BlockingSendFailureReason.TIME_LIMIT_REACHED, "Response not received for Wait limit[" + waitLimit + "ms].", null);
            }
        } catch (InterruptedException e) {
            throw new BlockingSendException(BlockingSendFailureReason.EXCEPTION_WHEN_WAITING, "Exception when waiting the response.", e);
        }
        return new Tuple(requestID, waitedResponseMessage);
    }
}
Also used : BlockingSendException(edu.iu.dsc.tws.api.exceptions.net.BlockingSendException) RequestID(edu.iu.dsc.tws.api.net.request.RequestID) Tuple(edu.iu.dsc.tws.api.comms.structs.Tuple)

Example 2 with RequestID

use of edu.iu.dsc.tws.api.net.request.RequestID in project twister2 by DSC-SPIDAL.

the class RRClient method sendRequest.

public RequestID sendRequest(Message message) {
    if (!client.isConnected()) {
        return null;
    }
    String messageType = message.getDescriptorForType().getFullName();
    if (!messageBuilders.containsKey(messageType)) {
        throw new RuntimeException("Message without a message builder");
    }
    RequestID id = RequestID.generate();
    byte[] data = message.toByteArray();
    // lets serialize the message
    int capacity = id.getId().length + data.length + 4 + messageType.getBytes().length + 4;
    ByteBuffer buffer = ByteBuffer.allocate(capacity);
    // we send message id, worker id and data
    buffer.put(id.getId());
    // pack the name of the message
    ByteUtils.packString(messageType, buffer);
    // pack the worker id
    buffer.putInt(workerID);
    // pack data
    buffer.put(data);
    TCPMessage request = client.send(channel, buffer, capacity, 0);
    if (request != null) {
        loop.wakeup();
        return id;
    } else {
        return null;
    }
}
Also used : RequestID(edu.iu.dsc.tws.api.net.request.RequestID) TCPMessage(edu.iu.dsc.tws.common.net.tcp.TCPMessage) ByteBuffer(java.nio.ByteBuffer)

Example 3 with RequestID

use of edu.iu.dsc.tws.api.net.request.RequestID in project twister2 by DSC-SPIDAL.

the class RRServer method sendMessage.

/**
 * Send a non-response message to a worker or to the client
 * @param message message
 * @return true if response was accepted
 */
public boolean sendMessage(Message message, int targetID) {
    SocketChannel channel;
    if (targetID == CLIENT_ID) {
        if (clientChannel == null) {
            LOG.severe("Trying to send a message to the client, but it has not connected yet.");
            return false;
        }
        channel = clientChannel;
    } else if (workerChannels.containsValue(targetID)) {
        channel = workerChannels.inverse().get(targetID);
    } else {
        LOG.severe("Trying to send a message to a worker that has not connected yet. workerID: " + targetID);
        return false;
    }
    // this is most likely not needed, but just to make sure
    if (channel == null) {
        LOG.log(Level.SEVERE, "Channel is NULL for response");
        return false;
    }
    // since this is not a request/response message, we put the dummy request id
    RequestID dummyRequestID = RequestID.DUMMY_REQUEST_ID;
    TCPMessage tcpMessage = sendMessage(message, dummyRequestID, channel);
    return tcpMessage != null;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) RequestID(edu.iu.dsc.tws.api.net.request.RequestID) TCPMessage(edu.iu.dsc.tws.common.net.tcp.TCPMessage)

Example 4 with RequestID

use of edu.iu.dsc.tws.api.net.request.RequestID in project twister2 by DSC-SPIDAL.

the class CheckpointingClientImpl method sendVersionUpdate.

@Override
public void sendVersionUpdate(String family, int index, long version, MessageHandler messageHandler) {
    RequestID requestID = this.rrClient.sendRequest(Checkpoint.VersionUpdateRequest.newBuilder().setFamily(family).setIndex(index).setVersion(version).build());
    this.asyncHandlers.put(requestID, messageHandler);
}
Also used : RequestID(edu.iu.dsc.tws.api.net.request.RequestID)

Example 5 with RequestID

use of edu.iu.dsc.tws.api.net.request.RequestID in project twister2 by DSC-SPIDAL.

the class JMWorkerController method sendBarrierRequest.

private void sendBarrierRequest(JobMasterAPI.BarrierType barrierType, long timeLimit) throws TimeoutException {
    JobMasterAPI.BarrierRequest barrierRequest = JobMasterAPI.BarrierRequest.newBuilder().setWorkerID(workerInfo.getWorkerID()).setBarrierType(barrierType).setTimeout(timeLimit).build();
    LOG.fine("Sending BarrierRequest message: \n" + barrierRequest.toString());
    try {
        // set the local wait time for the barrier response to (2 * timeLimit)
        // if the requested time limit is more than half of the long max value,
        // set it to the long max value
        long tl = timeLimit > Long.MAX_VALUE / 2 ? Long.MAX_VALUE : timeLimit * 2;
        Tuple<RequestID, Message> response = rrClient.sendRequestWaitResponse(barrierRequest, tl);
        JobMasterAPI.BarrierResponse barrierResponse = (JobMasterAPI.BarrierResponse) response.getValue();
        if (barrierResponse.getResult() == JobMasterAPI.BarrierResult.SUCCESS) {
            return;
        } else if (barrierResponse.getResult() == JobMasterAPI.BarrierResult.JOB_FAULTY) {
            throw new JobFaultyException("Job became faulty and Default Barrier failed.");
        } else if (barrierResponse.getResult() == JobMasterAPI.BarrierResult.TIMED_OUT) {
            throw new TimeoutException("Barrier timed out. Not all workers arrived at the barrier " + "on the time limit: " + timeLimit + "ms");
        }
    } catch (BlockingSendException e) {
        throw new TimeoutException("Not all workers arrived at the barrier on the time limit: " + timeLimit + "ms.", e);
    }
}
Also used : JobMasterAPI(edu.iu.dsc.tws.proto.jobmaster.JobMasterAPI) BlockingSendException(edu.iu.dsc.tws.api.exceptions.net.BlockingSendException) RequestID(edu.iu.dsc.tws.api.net.request.RequestID) Message(com.google.protobuf.Message) JobFaultyException(edu.iu.dsc.tws.api.exceptions.JobFaultyException) TimeoutException(edu.iu.dsc.tws.api.exceptions.TimeoutException)

Aggregations

RequestID (edu.iu.dsc.tws.api.net.request.RequestID)8 JobMasterAPI (edu.iu.dsc.tws.proto.jobmaster.JobMasterAPI)3 BlockingSendException (edu.iu.dsc.tws.api.exceptions.net.BlockingSendException)2 TCPMessage (edu.iu.dsc.tws.common.net.tcp.TCPMessage)2 Message (com.google.protobuf.Message)1 Tuple (edu.iu.dsc.tws.api.comms.structs.Tuple)1 JobFaultyException (edu.iu.dsc.tws.api.exceptions.JobFaultyException)1 TimeoutException (edu.iu.dsc.tws.api.exceptions.TimeoutException)1 ByteBuffer (java.nio.ByteBuffer)1 SocketChannel (java.nio.channels.SocketChannel)1