Search in sources :

Example 26 with TaskMessage

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

the class NettyUnitTest method test_server_delay.

@Test
public void test_server_delay() {
    System.out.println("!!!!!!!!!!Start delay message test!!!!!!!!");
    String req_msg = setupLargMsg();
    IConnection server = null;
    IConnection client = null;
    server = initNettyServer();
    client = context.connect(null, "localhost", port);
    List<TaskMessage> list = new ArrayList<TaskMessage>();
    TaskMessage message = new TaskMessage(task, req_msg.getBytes());
    list.add(message);
    LOG.info("Client send data");
    client.send(message);
    JStormUtils.sleepMs(1000);
    byte[] recv = (byte[]) server.recv(task, 0);
    Assert.assertEquals(req_msg, new String(recv));
    server.close();
    client.close();
    System.out.println("!!!!!!!!!!End delay message test!!!!!!!!");
}
Also used : IConnection(backtype.storm.messaging.IConnection) TaskMessage(backtype.storm.messaging.TaskMessage) Test(org.junit.Test)

Example 27 with TaskMessage

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

the class NettyUnitTest method test_batch.

@Test
public void test_batch() {
    System.out.println("!!!!!!!!!!Start batch message test!!!!!!!!");
    final int base = 100000;
    final IContext context = TransportFactory.makeContext(storm_conf);
    final IConnection server = initNettyServer();
    new Thread(new Runnable() {

        public void send() {
            final IConnection client = context.connect(null, "localhost", port);
            List<TaskMessage> list = new ArrayList<TaskMessage>();
            for (int i = 1; i < Short.MAX_VALUE; i++) {
                String req_msg = String.valueOf(i + base);
                TaskMessage message = new TaskMessage(task, req_msg.getBytes());
                list.add(message);
            }
            client.send(list);
            System.out.println("Finish Send ");
            JStormUtils.sleepMs(1000);
            try {
                clientClose.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            client.close();
            contextClose.signal();
        }

        @Override
        public void run() {
            lock.lock();
            try {
                send();
            } finally {
                lock.unlock();
            }
        }
    }).start();
    for (int i = 1; i < Short.MAX_VALUE; i++) {
        byte[] message = (byte[]) server.recv(task, 0);
        Assert.assertEquals(String.valueOf(i + base), new String(message));
        if (i % 1000 == 0) {
        // System.out.println("Receive " + new String(message));
        }
    }
    System.out.println("Finish Receive ");
    lock.lock();
    try {
        clientClose.signal();
        server.close();
        try {
            contextClose.await();
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        }
        context.term();
    } finally {
        lock.unlock();
    }
    System.out.println("!!!!!!!!!!End batch message test!!!!!!!!");
}
Also used : IContext(backtype.storm.messaging.IContext) IConnection(backtype.storm.messaging.IConnection) TaskMessage(backtype.storm.messaging.TaskMessage) Test(org.junit.Test)

Example 28 with TaskMessage

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

the class MessageBatch method add.

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

Example 29 with TaskMessage

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

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 
    if (buf.readableBytes() < 2) {
        //need more data
        return null;
    }
    // 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();
    //case 1: Control message
    ControlMessage ctrl_msg = ControlMessage.mkMessage(code);
    if (ctrl_msg != null)
        return ctrl_msg;
    //case 2: task Message
    short task = code;
    // Make sure that we have received at least an integer (length) 
    if (buf.readableBytes() < 4) {
        //need more data
        buf.resetReaderIndex();
        return null;
    }
    // Read the length field.
    int length = buf.readInt();
    if (length <= 0) {
        return new TaskMessage(task, null);
    }
    // Make sure if there's enough bytes in the buffer.
    if (buf.readableBytes() < 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);
    // Return a TaskMessage object
    return new TaskMessage(task, payload.array());
}
Also used : TaskMessage(backtype.storm.messaging.TaskMessage) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 30 with TaskMessage

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

the class Server method recv.

/**
     * fetch a message from message queue synchronously (flags != 1) or asynchronously (flags==1)
     */
public TaskMessage recv(int flags) {
    if ((flags & 0x01) == 0x01) {
        //non-blocking
        return message_queue.poll();
    } else {
        try {
            TaskMessage request = message_queue.take();
            LOG.debug("request to be processed: {}", request);
            return request;
        } catch (InterruptedException e) {
            LOG.info("exception within msg receiving", e);
            return null;
        }
    }
}
Also used : TaskMessage(backtype.storm.messaging.TaskMessage)

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