Search in sources :

Example 1 with Bytez

use of org.nustaq.offheap.bytez.Bytez in project fast-cast by RuedigerMoeller.

the class FCPing method runPingClientSync.

// CO !
public void runPingClientSync() throws InterruptedException {
    final FastCast fc = initFC("pclie", "pingponglat.kson");
    final FCPublisher pingserver = fc.onTransport("ping").publish(fc.getPublisherConf("pingtopic"));
    final Executor ex = Executors.newSingleThreadExecutor();
    final AtomicInteger await = new AtomicInteger(0);
    fc.onTransport(PING_BACK_ON_SAME_TOPIC ? "ping" : "pong").subscribe(fc.getSubscriberConf("pongtopic"), new FCSubscriber() {

        String pongName;

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
            await.decrementAndGet();
        }

        @Override
        public boolean dropped() {
            // reset
            await.set(0);
            System.out.println("Drop and Reset counter !");
            return true;
        }

        @Override
        public void senderTerminated(String senderNodeId) {
            System.out.println(senderNodeId + " terminated");
        }

        @Override
        public void senderBootstrapped(String receivesFrom, long seqNo) {
            System.out.println("bootstrap " + receivesFrom);
            pongName = receivesFrom;
        }
    });
    // just create a byte[] for each struct (*)
    FSTStructAllocator alloc = new FSTStructAllocator(0);
    PingRequest pr = alloc.newStruct(new PingRequest());
    Histogram histo = new Histogram(TimeUnit.SECONDS.toNanos(10), 3);
    // wait for at least one heartbeat
    Thread.sleep(1000);
    System.out.println("starting ping pong " + randomId);
    System.gc();
    pr.setNanoSendTime(randomId);
    Sleeper sl = new Sleeper();
    while (true) {
        await.set(0);
        for (int i = 0; i < NUM_MSG; i++) {
            //                sl.sleepMicros(100);
            pingAndAwaitPong(pingserver, await, pr, histo, i);
        }
        histo.outputPercentileDistribution(System.out, 1000.0);
        histo.reset();
        System.gc();
    }
}
Also used : Histogram(org.HdrHistogram.Histogram) FCPublisher(org.nustaq.fastcast.api.FCPublisher) Bytez(org.nustaq.offheap.bytez.Bytez) Sleeper(org.nustaq.fastcast.util.Sleeper) FSTStructAllocator(org.nustaq.offheap.structs.FSTStructAllocator) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber) Executor(java.util.concurrent.Executor) FastCast(org.nustaq.fastcast.api.FastCast) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 2 with Bytez

use of org.nustaq.offheap.bytez.Bytez in project fast-cast by RuedigerMoeller.

the class FCPing method runPingClientASync.

// no coordinated ommission, async
public void runPingClientASync() throws InterruptedException {
    final FastCast fc = initFC("pclie", "pingponglat.kson");
    final FCPublisher pingserver = fc.onTransport("ping").publish(fc.getPublisherConf("pingtopic"));
    final Executor ex = Executors.newSingleThreadExecutor();
    final Histogram histo = new Histogram(TimeUnit.SECONDS.toNanos(10), 3);
    fc.onTransport(PING_BACK_ON_SAME_TOPIC ? "ping" : "pong").subscribe(fc.getSubscriberConf("pongtopic"), new FCSubscriber() {

        int msgCount;

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
            // decode bounced back ping request
            PingRequest received = FSTStructFactory.getInstance().getStructPointer(b, off).cast();
            if (// filter out pressure of printing histogram
            msgCount > 10_000)
                histo.recordValue(System.nanoTime() - received.getNanoSendTime());
            msgCount++;
            if (msgCount > NUM_MSG + 10_000) {
                histo.outputPercentileDistribution(System.out, 1000.0);
                histo.reset();
                msgCount = 0;
            }
        }

        @Override
        public boolean dropped() {
            return true;
        }

        @Override
        public void senderTerminated(String senderNodeId) {
            System.out.println(senderNodeId + " terminated");
        }

        @Override
        public void senderBootstrapped(String receivesFrom, long seqNo) {
            System.out.println("bootstrap " + receivesFrom);
        }
    });
    // just create a byte[] for each struct (*)
    FSTStructAllocator alloc = new FSTStructAllocator(0);
    PingRequest pr = alloc.newStruct(new PingRequest());
    Sleeper sl = new Sleeper();
    while (true) {
        // need rate limiting cause of async
        sl.spinMicros(200);
        pr.setNanoSendTime(System.nanoTime());
        // can be sure off is 0, see (*)
        pingserver.offer(null, pr.getBase(), true);
    }
}
Also used : Histogram(org.HdrHistogram.Histogram) FCPublisher(org.nustaq.fastcast.api.FCPublisher) Bytez(org.nustaq.offheap.bytez.Bytez) Sleeper(org.nustaq.fastcast.util.Sleeper) FSTStructAllocator(org.nustaq.offheap.structs.FSTStructAllocator) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber) Executor(java.util.concurrent.Executor) FastCast(org.nustaq.fastcast.api.FastCast)

Example 3 with Bytez

use of org.nustaq.offheap.bytez.Bytez in project fast-cast by RuedigerMoeller.

the class FCPing method runPongServer.

public void runPongServer() throws InterruptedException {
    FastCast fc = initFC("pserv", "pingponglat.kson");
    final FCPublisher echoresp = fc.onTransport(PING_BACK_ON_SAME_TOPIC ? "ping" : "pong").publish(fc.getPublisherConf("pongtopic"));
    fc.onTransport("ping").subscribe(fc.getSubscriberConf("pingtopic"), new FCSubscriber() {

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
            //                System.out.println("snd:"+sender);
            while (!echoresp.offer(sender, b, off, len, true)) {
                // ensure retrans processing etc.
                echoresp.flush();
            }
        }

        @Override
        public boolean dropped() {
            return true;
        }

        @Override
        public void senderTerminated(String senderNodeId) {
            System.out.println(senderNodeId + " terminated");
        }

        @Override
        public void senderBootstrapped(String receivesFrom, long seqNo) {
            System.out.println("bootstrap " + receivesFrom);
        }
    });
    while (true) {
        Thread.sleep(100000);
    }
}
Also used : FastCast(org.nustaq.fastcast.api.FastCast) FCPublisher(org.nustaq.fastcast.api.FCPublisher) Bytez(org.nustaq.offheap.bytez.Bytez) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber)

Example 4 with Bytez

use of org.nustaq.offheap.bytez.Bytez in project fast-cast by RuedigerMoeller.

the class SendReceive method send.

//    @Test
public void send() throws InterruptedException {
    initFC();
    TestMsg template = new TestMsg();
    FSTStructAllocator allocator = new FSTStructAllocator(0);
    TestMsg toSend = allocator.newStruct(template);
    FCPublisher sender = fc.onTransport("default").publish(fc.getPublisherConf("test"));
    toSend.getString().setString("H");
    RateMeasure measure = new RateMeasure("msg send " + toSend.getByteSize());
    final Bytez base = toSend.getBase();
    final int byteSize = toSend.getByteSize();
    final long offset = toSend.getOffset();
    long tim = System.currentTimeMillis();
    while (true) {
        Sleeper.spinMicros(50);
        toSend.setTimeNanos(System.nanoTime());
        while (!sender.offer(null, base, offset, byteSize, true)) {
        //                System.out.println("offer rejected !");
        }
        measure.count();
        //            System.out.println("sent msg");
        if (System.currentTimeMillis() - tim > 5000) {
            break;
        }
    }
}
Also used : Bytez(org.nustaq.offheap.bytez.Bytez) RateMeasure(org.nustaq.fastcast.util.RateMeasure) FSTStructAllocator(org.nustaq.offheap.structs.FSTStructAllocator)

Example 5 with Bytez

use of org.nustaq.offheap.bytez.Bytez in project fast-cast by RuedigerMoeller.

the class AsyncPubStruct method initFastCast.

public void initFastCast() throws Exception {
    fastCast = FastCast.getFastCast();
    fastCast.setNodeId("PUB");
    fastCast.loadConfig(CFG_FILE_PATH);
    pub = fastCast.onTransport("default").publish("stream");
    // pointer to message
    final FSTStruct msg = FSTStructFactory.getInstance().createEmptyStructPointer(FSTStruct.class);
    fastCast.onTransport("back").subscribe("back", new FCSubscriber() {

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
            // as structs decode literally in zero time, we can decode inside receiver thread
            msg.baseOn(b, (int) off);
            Class type = msg.getPointedClass();
            if (type == StructString.class) {
                // sequence end, print histogram
                final Histogram oldHi = hi;
                hi = new Histogram(TimeUnit.SECONDS.toNanos(2), 3);
                // no lambdas to stay 1.7 compatible
                // move printing out of the receiving thread
                dumper.execute(new Runnable() {

                    @Override
                    public void run() {
                        oldHi.outputPercentileDistribution(System.out, 1000.0);
                    }
                });
            } else {
                // a regular message, record latency
                AsyncLatMessageStruct mdata = msg.cast();
                long value = System.nanoTime() - mdata.getSendTimeStampNanos();
                if (value < 1_000_000_000) {
                    // avoid AIOB during init + JITTING
                    hi.recordValue(value);
                }
            // a real app would need to copy the message (recycle byte arrays/objects !) and
            // run msg processing in an executor to get out of the receiving thread.
            // mdata gets invalid on finish of this method
            }
        }

        @Override
        public boolean dropped() {
            System.out.println("DROPPED");
            System.exit(-1);
            return false;
        }

        @Override
        public void senderTerminated(String senderNodeId) {
        }

        @Override
        public void senderBootstrapped(String receivesFrom, long seqNo) {
        }
    });
}
Also used : StructString(org.nustaq.offheap.structs.structtypes.StructString) FSTStruct(org.nustaq.offheap.structs.FSTStruct) Histogram(org.HdrHistogram.Histogram) HeapBytez(org.nustaq.offheap.bytez.onheap.HeapBytez) Bytez(org.nustaq.offheap.bytez.Bytez) StructString(org.nustaq.offheap.structs.structtypes.StructString) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber)

Aggregations

Bytez (org.nustaq.offheap.bytez.Bytez)9 FCSubscriber (org.nustaq.fastcast.api.FCSubscriber)7 RateMeasure (org.nustaq.fastcast.util.RateMeasure)4 Histogram (org.HdrHistogram.Histogram)3 FCPublisher (org.nustaq.fastcast.api.FCPublisher)3 FastCast (org.nustaq.fastcast.api.FastCast)3 FSTStruct (org.nustaq.offheap.structs.FSTStruct)3 FSTStructAllocator (org.nustaq.offheap.structs.FSTStructAllocator)3 Executor (java.util.concurrent.Executor)2 SubscriberConf (org.nustaq.fastcast.config.SubscriberConf)2 Sleeper (org.nustaq.fastcast.util.Sleeper)2 StructString (org.nustaq.offheap.structs.structtypes.StructString)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 HeapBytez (org.nustaq.offheap.bytez.onheap.HeapBytez)1