Search in sources :

Example 1 with FutureMessage

use of org.janusgraph.diskstorage.log.util.FutureMessage in project janusgraph by JanusGraph.

the class KCVSLog method add.

/**
 * Adds the given message (content) to the timeslice for the partition identified by the provided partitionId.
 * If a persistor is specified, this persistor is used to add the message otherwise the internal delivery systems are used.
 *
 * @param content
 * @param partitionId
 * @param persistor
 * @return
 */
private Future<Message> add(StaticBuffer content, int partitionId, ExternalPersistor persistor) {
    ResourceUnavailableException.verifyOpen(isOpen, "Log", name);
    Preconditions.checkArgument(content != null && content.length() > 0, "Content is empty");
    Preconditions.checkArgument(partitionId >= 0 && partitionId < (1 << manager.partitionBitWidth), "Invalid partition id: %s", partitionId);
    final Instant timestamp = times.getTime();
    KCVSMessage msg = new KCVSMessage(content, timestamp, manager.senderId);
    FutureMessage futureMessage = new FutureMessage(msg);
    StaticBuffer key = getLogKey(partitionId, (int) (numBucketCounter.incrementAndGet() % numBuckets), getTimeSlice(timestamp));
    MessageEnvelope envelope = new MessageEnvelope(futureMessage, key, writeMessage(msg));
    if (persistor != null) {
        try {
            persistor.add(envelope.key, envelope.entry);
            envelope.message.delivered();
        } catch (JanusGraphException e) {
            envelope.message.failed(e);
            throw e;
        }
    } else if (outgoingMsg == null) {
        sendMessages(Collections.singletonList(envelope));
    } else {
        try {
            // Produces back pressure when full
            outgoingMsg.put(envelope);
            log.debug("Enqueued {} for partition {}", envelope, partitionId);
        } catch (InterruptedException e) {
            throw new JanusGraphException("Got interrupted waiting to send message", e);
        }
    }
    return futureMessage;
}
Also used : Instant(java.time.Instant) JanusGraphException(org.janusgraph.core.JanusGraphException) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) FutureMessage(org.janusgraph.diskstorage.log.util.FutureMessage)

Aggregations

Instant (java.time.Instant)1 JanusGraphException (org.janusgraph.core.JanusGraphException)1 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)1 FutureMessage (org.janusgraph.diskstorage.log.util.FutureMessage)1