Search in sources :

Example 16 with TaskMessage

use of backtype.storm.messaging.TaskMessage in project storm by nathanmarz.

the class MessageBatch method buffer.

/**
     * create a buffer containing the encoding of this batch
     */
ChannelBuffer buffer() throws Exception {
    ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(encoded_length));
    for (Object msg : msgs) if (msg instanceof TaskMessage)
        writeTaskMessage(bout, (TaskMessage) msg);
    else
        ((ControlMessage) msg).write(bout);
    //add a END_OF_BATCH indicator
    ControlMessage.EOB_MESSAGE.write(bout);
    bout.close();
    return bout.buffer();
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) TaskMessage(backtype.storm.messaging.TaskMessage)

Example 17 with TaskMessage

use of backtype.storm.messaging.TaskMessage in project jstorm by alibaba.

the class BackpressureCallback method execute.

public <T> Object execute(T... args) {
    if (monitorQueue.pctFull() < lowMark && monitorQueue.cacheSize() == 0) {
        HashSet<String> remoteAddrs = remoteClientsUnderFlowCtrl.get(taskId);
        synchronized (remoteAddrs) {
            for (String remoteAddr : remoteAddrs) {
                Channel channel = stormChannelGroup.getChannel(remoteAddr);
                if (channel == null) {
                    continue;
                }
                // send back backpressure flow control request to source client
                ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE + 1);
                // 1-> start flow control; 0-> stop flow control
                buffer.put((byte) 0);
                buffer.putInt(taskId);
                TaskMessage flowCtrlMsg = new TaskMessage(TaskMessage.BACK_PRESSURE_REQUEST, 0, buffer.array());
                channel.write(flowCtrlMsg);
            }
            remoteAddrs.clear();
        }
        return true;
    } else {
        return false;
    }
}
Also used : Channel(org.jboss.netty.channel.Channel) ByteBuffer(java.nio.ByteBuffer) TaskMessage(backtype.storm.messaging.TaskMessage)

Example 18 with TaskMessage

use of backtype.storm.messaging.TaskMessage in project jstorm by alibaba.

the class MessageBatch method add.

void add(List objs) {
    if (objs == null || objs.size() == 0)
        throw new RuntimeException("null object forbidded in message batch");
    if (objs.get(0) instanceof TaskMessage) {
        for (Object obj : objs) {
            TaskMessage msg = (TaskMessage) obj;
            msgs.add(msg);
            encodedLength += msgEncodeLength(msg);
        }
        return;
    }
    if (objs.get(0) instanceof ControlMessage) {
        for (Object obj : objs) {
            ControlMessage msg = (ControlMessage) obj;
            msgs.add(msg);
            encodedLength += msg.getEncodedLength();
        }
        return;
    }
    throw new RuntimeException("Unsuppoted object type " + objs.get(0).getClass().getName());
}
Also used : ControlMessage(backtype.storm.messaging.ControlMessage) TaskMessage(backtype.storm.messaging.TaskMessage)

Example 19 with TaskMessage

use of backtype.storm.messaging.TaskMessage in project jstorm by alibaba.

the class MessageBatch method add.

void add(NettyMessage obj) {
    if (obj == null)
        throw new RuntimeException("null object forbidded in message batch");
    if (obj instanceof TaskMessage) {
        TaskMessage msg = (TaskMessage) obj;
        msgs.add(msg);
        encodedLength += msgEncodeLength(msg);
        return;
    }
    if (obj instanceof MessageBatch) {
        MessageBatch batch = (MessageBatch) obj;
        add(batch);
        return;
    }
    if (obj instanceof ControlMessage) {
        ControlMessage msg = (ControlMessage) obj;
        msgs.add(msg);
        encodedLength += msg.getEncodedLength();
        return;
    }
    throw new RuntimeException("Unsuppoted object type " + obj.getClass().getName());
}
Also used : ControlMessage(backtype.storm.messaging.ControlMessage) TaskMessage(backtype.storm.messaging.TaskMessage)

Example 20 with TaskMessage

use of backtype.storm.messaging.TaskMessage in project jstorm by alibaba.

the class MessageDecoder method decode.

/*
     * Each ControlMessage is encoded as: code (<0) ... short(2)
     * Each TaskMessage is encoded as: task (>=0) ... short(2) len ... int(4) payload ... byte[] *
     */
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
    // Make sure that we have received at least a short
    long available = buf.readableBytes();
    // Minimum length of a task message is 6(short taskId, int length).
    if (available < 6) {
        // need more data
        return null;
    }
    long startTime = 0L;
    if (isServer && enableNettyMetrics) {
        startTime = msgDecodeTimer.getTime();
    }
    try {
        // Mark the current buffer position before reading task/len field
        // because the whole frame might not be in the buffer yet.
        // We will reset the buffer position to the marked position if
        // there's not enough bytes in the buffer.
        buf.markReaderIndex();
        // read the short field
        short code = buf.readShort();
        available -= 2;
        // case 1: Control message
        ControlMessage ctrl_msg = ControlMessage.mkMessage(code);
        if (ctrl_msg != null) {
            if (available < 12) {
                // The time stamp bytes were not received yet - return null.
                buf.resetReaderIndex();
                return null;
            }
            long timeStamp = buf.readLong();
            int clientPort = buf.readInt();
            available -= 12;
            if (ctrl_msg == ControlMessage.EOB_MESSAGE) {
                long interval = System.currentTimeMillis() - timeStamp;
                if (interval < 0)
                    interval = 0;
                if (enableNettyMetrics) {
                    AsmHistogram netTransTime = getTransmitHistogram(channel, clientPort);
                    if (netTransTime != null) {
                        netTransTime.update(interval * TimeUtils.NS_PER_US);
                    }
                }
                if (MetricUtils.metricAccurateCal)
                    NettyMetricInstance.networkWorkerTransmitTime.update(interval * TimeUtils.NS_PER_US);
            }
            NettyMetricInstance.nettyServerRecvSpeed.update(ctrl_msg.getEncodedLength());
            return ctrl_msg;
        }
        // case 2: task Message
        short type = code;
        short task = buf.readShort();
        available -= 2;
        // Make sure that we have received at least an integer (length)
        if (available < 4) {
            // need more data
            buf.resetReaderIndex();
            return null;
        }
        // Read the length field.
        int length = buf.readInt();
        if (length <= 0) {
            LOG.info("Receive one message whose TaskMessage's message length is {}", length);
            return new TaskMessage(task, null);
        }
        // Make sure if there's enough bytes in the buffer.
        available -= 4;
        if (available < length) {
            // The whole bytes were not received yet - return null.
            buf.resetReaderIndex();
            return null;
        }
        // There's enough bytes in the buffer. Read it.
        ChannelBuffer payload = buf.readBytes(length);
        // Successfully decoded a frame.
        // Return a TaskMessage object
        byte[] rawBytes = payload.array();
        // @@@ TESTING CODE
        // LOG.info("Receive task:{}, length: {}, data:{}",
        // task, length, JStormUtils.toPrintableString(rawBytes));
        TaskMessage ret = new TaskMessage(type, task, rawBytes);
        NettyMetricInstance.nettyServerRecvSpeed.update(rawBytes.length + 8);
        return ret;
    } finally {
        if (isServer && enableNettyMetrics && msgDecodeTimer != null) {
            msgDecodeTimer.updateTime(startTime);
        }
    }
}
Also used : AsmHistogram(com.alibaba.jstorm.common.metric.AsmHistogram) ControlMessage(backtype.storm.messaging.ControlMessage) TaskMessage(backtype.storm.messaging.TaskMessage) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

TaskMessage (backtype.storm.messaging.TaskMessage)30 IConnection (backtype.storm.messaging.IConnection)15 Test (org.junit.Test)12 IContext (backtype.storm.messaging.IContext)7 ControlMessage (backtype.storm.messaging.ControlMessage)4 ByteBuffer (java.nio.ByteBuffer)3 ITupleExt (backtype.storm.tuple.ITupleExt)2 KryoException (com.esotericsoftware.kryo.KryoException)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 ChannelBufferOutputStream (org.jboss.netty.buffer.ChannelBufferOutputStream)2 DisruptorQueue (backtype.storm.utils.DisruptorQueue)1 AsmHistogram (com.alibaba.jstorm.common.metric.AsmHistogram)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Condition (java.util.concurrent.locks.Condition)1 Channel (org.jboss.netty.channel.Channel)1