Search in sources :

Example 1 with StructString

use of org.nustaq.offheap.structs.structtypes.StructString in project fast-cast by RuedigerMoeller.

the class AsyncPubStruct method run.

public void run(int pauseNanos, int numEvents) throws Throwable {
    RateMeasure report = new RateMeasure("send rate");
    // alloc + init a single instance which is reused then. if no allocator is used, default is OnHeap Alloc (struct is byte[] backed)
    AsyncLatMessageStruct event = (AsyncLatMessageStruct) new AsyncLatMessageStruct(System.nanoTime(), 0, 0 + 1, 10, 110).toOffHeap();
    byte[] underlying = event.getBase().asByteArray();
    byte[] end = new StructString("END").toOffHeap().getBase().asByteArray();
    for (int i = 0; i < numEvents; i++) {
        double bidPrc = Math.random() * 10;
        event.setBidPrc(bidPrc);
        event.setAskPrc(bidPrc + 1);
        event.setSendTimeStampNanos(System.nanoTime());
        while (!pub.offer(null, underlying, 0, underlying.length, true)) {
        // spin
        }
        report.count();
        long time = System.nanoTime();
        while (System.nanoTime() - time < pauseNanos) {
        // spin
        }
    }
    while (!pub.offer(null, end, 0, end.length, true)) {
    // spin
    }
}
Also used : StructString(org.nustaq.offheap.structs.structtypes.StructString) RateMeasure(org.nustaq.fastcast.util.RateMeasure)

Example 2 with StructString

use of org.nustaq.offheap.structs.structtypes.StructString in project fast-cast by RuedigerMoeller.

the class ReceiveBufferDispatcher method cleanupTopic.

public void cleanupTopic() {
    ArrayList<StructString> keys = new ArrayList(bufferMap.keySet());
    for (int i = 0; i < keys.size(); i++) {
        StructString o = keys.get(i);
        cleanup(o.toString());
    }
}
Also used : StructString(org.nustaq.offheap.structs.structtypes.StructString) ArrayList(java.util.ArrayList)

Example 3 with StructString

use of org.nustaq.offheap.structs.structtypes.StructString in project fast-cast by RuedigerMoeller.

the class ReceiveBufferDispatcher method cleanup.

/**
     * if a sender stops sending, remove from map to free memory
     * @param senderName
     */
public void cleanup(String senderName) {
    StructString struct = new StructString(senderName);
    PacketReceiveBuffer packetReceiveBuffer = bufferMap.get(struct);
    bufferMap.remove(struct);
    if (packetReceiveBuffer != null) {
        FCSubscriber subscriber = packetReceiveBuffer.getTopicEntry().getSubscriber();
        if (subscriber != null) {
            subscriber.senderTerminated(senderName);
        } else {
            FCLog.get().severe("no subscriber for stopped sender", null);
        }
        packetReceiveBuffer.terminate();
    } else {
        FCLog.get().warn("cannot find packetReceiver to terminate");
    }
}
Also used : StructString(org.nustaq.offheap.structs.structtypes.StructString) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber)

Example 4 with StructString

use of org.nustaq.offheap.structs.structtypes.StructString in project fast-cast by RuedigerMoeller.

the class TransportDriver method receiveDatagram.

private boolean receiveDatagram(ByteBuffer p, byte[] wrappedArr) throws IOException {
    if (trans.receive(p)) {
        boolean selfSent = receivedPacket.getSender().equals(nodeId);
        if (!selfSent) {
            // debug
            if (RETRANS_DEBUG) {
                Class debugtype = receivedPacket.getPointedClass();
                if (debugtype == RetransPacket.class) {
                    RetransPacket retransPacket = receivedPacket.cast().detach();
                    System.out.println("retrans received " + retransPacket);
                }
            }
            //
            int topic = receivedPacket.getTopic();
            if (topic > MAX_NUM_TOPICS || topic < 0) {
                FCLog.get().warn("foreign traffic");
                return true;
            }
            if (receiver[topic] == null && sender[topic] == null) {
                return true;
            }
            Class type = receivedPacket.getPointedClass();
            StructString receivedPacketReceiver = receivedPacket.getReceiver();
            if (type == DataPacket.class) {
                if (receiver[topic] == null)
                    return true;
                dispatchDataPacket(receivedPacket, topic);
            } else if (type == RetransPacket.class) {
                if (sender[topic] == null)
                    return true;
                if (receivedPacketReceiver == null || !receivedPacketReceiver.equals(nodeId)) {
                    return true;
                }
                if (RETRANS_DEBUG)
                    System.out.println("retrans dispatched ");
                dispatchRetransmissionRequest(receivedPacket, topic);
            } else if (type == ControlPacket.class) {
                if (isForeignReceiver(receivedPacketReceiver)) {
                    return true;
                }
                ControlPacket control = receivedPacket.cast();
                if (control.getType() == ControlPacket.DROPPED) {
                    ReceiveBufferDispatcher receiveBufferDispatcher = receiver[topic];
                    if (receiveBufferDispatcher != null) {
                        FCLog.get().warn(nodeId + " has been dropped by " + receivedPacket.getSender() + " on service " + receiveBufferDispatcher.getTopicEntry().getTopicId());
                        FCSubscriber service = receiveBufferDispatcher.getTopicEntry().getSubscriber();
                        if (service != null) {
                            if (service.dropped()) {
                                // retry if returns true
                                FCLog.get().warn("..resyncing..");
                                PacketReceiveBuffer buffer = receiveBufferDispatcher.getBuffer(receivedPacket.getSender());
                                if (buffer != null) {
                                    buffer.resync();
                                } else {
                                    FCLog.get().warn("unexpected null buffer");
                                }
                            } else {
                                // topic is lost forever now ..
                                receiver[topic] = null;
                                receiveBufferDispatcher.cleanupTopic();
                            }
                        }
                    }
                }
            // heartbeats are sent as regular data packets in order to have initialSync to happen correctly
            //                    else if ( control.getType() == ControlPacket.HEARTBEAT ) {
            //                        ReceiveBufferDispatcher receiveBufferDispatcher = receiver[topic];
            //                        if ( receiveBufferDispatcher != null ) {
            //                            PacketReceiveBuffer buffer = receiveBufferDispatcher.getBuffer(control.getSender());
            //                            if ( buffer != null ) {
            //                                buffer.updateHeartBeat(control.getSeqNo(),System.currentTimeMillis());
            //                            }
            //                        }
            //                    }
            }
            return true;
        } else {
            return false;
        }
    }
    return false;
}
Also used : StructString(org.nustaq.offheap.structs.structtypes.StructString) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber)

Aggregations

StructString (org.nustaq.offheap.structs.structtypes.StructString)4 FCSubscriber (org.nustaq.fastcast.api.FCSubscriber)2 ArrayList (java.util.ArrayList)1 RateMeasure (org.nustaq.fastcast.util.RateMeasure)1