Search in sources :

Example 1 with OctetsFW

use of io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW in project zilla by aklivity.

the class KafkaCachePartition method writeEntryFinish.

public void writeEntryFinish(ArrayFW<KafkaHeaderFW> headers, KafkaDeltaType deltaType) {
    final Node head = sentinel.previous;
    assert head != sentinel;
    final KafkaCacheSegment headSegment = head.segment;
    assert headSegment != null;
    final KafkaCacheFile logFile = headSegment.logFile();
    final KafkaCacheFile deltaFile = headSegment.deltaFile();
    final KafkaCacheFile hashFile = headSegment.hashFile();
    final KafkaCacheFile indexFile = headSegment.indexFile();
    final int logAvailable = logFile.available();
    final int logRequired = headers.sizeof();
    assert logAvailable >= logRequired : String.format("%s %d >= %d", headSegment, logAvailable, logRequired);
    logFile.appendBytes(headers);
    final long offsetDelta = (int) (progress - headSegment.baseOffset());
    final long indexEntry = (offsetDelta << 32) | logFile.markValue();
    if (!headers.isEmpty()) {
        final DirectBuffer buffer = headers.buffer();
        final ByteBuffer byteBuffer = buffer.byteBuffer();
        assert byteBuffer != null;
        byteBuffer.clear();
        headers.forEach(h -> {
            final long hash = computeHash(h);
            final long hashEntry = (hash << 32) | logFile.markValue();
            hashFile.appendLong(hashEntry);
        });
    }
    assert indexFile.available() >= Long.BYTES;
    indexFile.appendLong(indexEntry);
    final KafkaCacheEntryFW headEntry = logFile.readBytes(logFile.markValue(), headEntryRO::wrap);
    if (deltaType == JSON_PATCH && ancestorEntry != null && ancestorEntry.valueLen() != -1 && headEntry.valueLen() != -1) {
        final OctetsFW ancestorValue = ancestorEntry.value();
        final OctetsFW headValue = headEntry.value();
        assert headEntry.offset$() == progress;
        final JsonProvider json = JsonProvider.provider();
        ancestorIn.wrap(ancestorValue.buffer(), ancestorValue.offset(), ancestorValue.sizeof());
        final JsonReader ancestorReader = json.createReader(ancestorIn);
        final JsonStructure ancestorJson = ancestorReader.read();
        ancestorReader.close();
        headIn.wrap(headValue.buffer(), headValue.offset(), headValue.sizeof());
        final JsonReader headReader = json.createReader(headIn);
        final JsonStructure headJson = headReader.read();
        headReader.close();
        final JsonPatch diff = json.createDiff(ancestorJson, headJson);
        final JsonArray diffJson = diff.toJsonArray();
        diffOut.wrap(diffBuffer, Integer.BYTES);
        final JsonWriter writer = json.createWriter(diffOut);
        writer.write(diffJson);
        writer.close();
        // TODO: signal delta.sizeof > head.sizeof via null delta, otherwise delta file can exceed log file
        final int deltaLength = diffOut.position();
        diffBuffer.putInt(0, deltaLength);
        deltaFile.appendBytes(diffBuffer, 0, Integer.BYTES + deltaLength);
    }
    headSegment.lastOffset(progress);
}
Also used : OctetsFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW) ByteBuffer(java.nio.ByteBuffer) JsonProvider(jakarta.json.spi.JsonProvider) JsonPatch(jakarta.json.JsonPatch) JsonWriter(jakarta.json.JsonWriter) MutableDirectBuffer(org.agrona.MutableDirectBuffer) DirectBuffer(org.agrona.DirectBuffer) JsonArray(jakarta.json.JsonArray) JsonReader(jakarta.json.JsonReader) KafkaCacheEntryFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.cache.KafkaCacheEntryFW) JsonStructure(jakarta.json.JsonStructure)

Example 2 with OctetsFW

use of io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW in project zilla by aklivity.

the class KafkaCacheClientFactory method newStream.

@Override
public MessageConsumer newStream(int msgTypeId, DirectBuffer buffer, int index, int length, MessageConsumer sender) {
    final BeginFW begin = beginRO.wrap(buffer, index, index + length);
    final OctetsFW extension = begin.extension();
    final ExtensionFW beginEx = extensionRO.tryWrap(extension.buffer(), extension.offset(), extension.limit());
    final KafkaBeginExFW kafkaBeginEx = beginEx != null && beginEx.typeId() == kafkaTypeId ? kafkaBeginExRO.tryWrap(extension.buffer(), extension.offset(), extension.limit()) : null;
    MessageConsumer newStream = null;
    if (kafkaBeginEx != null) {
        final BindingHandler factory = factories.get(kafkaBeginEx.kind());
        if (factory != null) {
            newStream = factory.newStream(begin.typeId(), begin.buffer(), begin.offset(), begin.sizeof(), sender);
        }
    }
    return newStream;
}
Also used : ExtensionFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.ExtensionFW) MessageConsumer(io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer) OctetsFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW) BeginFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.BeginFW) KafkaBeginExFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.KafkaBeginExFW) BindingHandler(io.aklivity.zilla.runtime.engine.binding.BindingHandler)

Example 3 with OctetsFW

use of io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW in project zilla by aklivity.

the class KafkaCacheClientFetchFactory method newStream.

@Override
public MessageConsumer newStream(int msgTypeId, DirectBuffer buffer, int index, int length, MessageConsumer sender) {
    final BeginFW begin = beginRO.wrap(buffer, index, index + length);
    final long routeId = begin.routeId();
    final long initialId = begin.streamId();
    final long affinity = begin.affinity();
    final long authorization = begin.authorization();
    assert (initialId & 0x0000_0000_0000_0001L) != 0L;
    final OctetsFW extension = begin.extension();
    final ExtensionFW beginEx = extension.get(extensionRO::tryWrap);
    assert beginEx != null && beginEx.typeId() == kafkaTypeId;
    final KafkaBeginExFW kafkaBeginEx = extension.get(kafkaBeginExRO::wrap);
    assert kafkaBeginEx.kind() == KafkaBeginExFW.KIND_FETCH;
    final KafkaFetchBeginExFW kafkaFetchBeginEx = kafkaBeginEx.fetch();
    final String16FW beginTopic = kafkaFetchBeginEx.topic();
    final KafkaOffsetFW progress = kafkaFetchBeginEx.partition();
    final ArrayFW<KafkaFilterFW> filters = kafkaFetchBeginEx.filters();
    final KafkaDeltaType deltaType = kafkaFetchBeginEx.deltaType().get();
    final String topicName = beginTopic.asString();
    MessageConsumer newStream = null;
    final KafkaBindingConfig binding = supplyBinding.apply(routeId);
    final KafkaRouteConfig resolved = binding != null ? binding.resolve(authorization, topicName) : null;
    if (resolved != null) {
        final long resolvedId = resolved.id;
        final int partitionId = progress.partitionId();
        final long partitionOffset = progress.partitionOffset();
        final KafkaCacheRoute cacheRoute = supplyCacheRoute.apply(resolvedId);
        final long partitionKey = cacheRoute.topicPartitionKey(topicName, partitionId);
        KafkaCacheClientFetchFanout fanout = cacheRoute.clientFetchFanoutsByTopicPartition.get(partitionKey);
        if (fanout == null) {
            final String cacheName = String.format("%s.%s", supplyNamespace.apply(resolvedId), supplyLocalName.apply(resolvedId));
            final KafkaCache cache = supplyCache.apply(cacheName);
            final KafkaCacheTopic topic = cache.supplyTopic(topicName);
            final KafkaCachePartition partition = topic.supplyFetchPartition(partitionId);
            final long defaultOffset = resolved.with != null ? resolved.with.defaultOffset.value() : KafkaOffsetType.HISTORICAL.value();
            final KafkaCacheClientFetchFanout newFanout = new KafkaCacheClientFetchFanout(resolvedId, authorization, affinity, partition, defaultOffset);
            cacheRoute.clientFetchFanoutsByTopicPartition.put(partitionKey, newFanout);
            fanout = newFanout;
        }
        final KafkaFilterCondition condition = cursorFactory.asCondition(filters);
        final long latestOffset = kafkaFetchBeginEx.partition().latestOffset();
        final KafkaOffsetType maximumOffset = KafkaOffsetType.valueOf((byte) latestOffset);
        final int leaderId = cacheRoute.leadersByPartitionId.get(partitionId);
        newStream = new KafkaCacheClientFetchStream(fanout, sender, routeId, initialId, leaderId, authorization, partitionOffset, condition, maximumOffset, deltaType)::onClientMessage;
    }
    return newStream;
}
Also used : KafkaCachePartition(io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCachePartition) KafkaDeltaType(io.aklivity.zilla.runtime.binding.kafka.internal.types.KafkaDeltaType) OctetsFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW) KafkaFetchBeginExFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.KafkaFetchBeginExFW) BeginFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.BeginFW) KafkaCache(io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCache) String16FW(io.aklivity.zilla.runtime.binding.kafka.internal.types.String16FW) ExtensionFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.ExtensionFW) MessageConsumer(io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer) KafkaBindingConfig(io.aklivity.zilla.runtime.binding.kafka.internal.config.KafkaBindingConfig) KafkaBeginExFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.KafkaBeginExFW) KafkaCacheTopic(io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCacheTopic) KafkaRouteConfig(io.aklivity.zilla.runtime.binding.kafka.internal.config.KafkaRouteConfig) KafkaOffsetFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.KafkaOffsetFW) KafkaOffsetType(io.aklivity.zilla.runtime.binding.kafka.internal.types.KafkaOffsetType) KafkaFilterCondition(io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCacheCursorFactory.KafkaFilterCondition) KafkaFilterFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.KafkaFilterFW)

Example 4 with OctetsFW

use of io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW in project zilla by aklivity.

the class KafkaCacheServerProduceFactory method newStream.

@Override
public MessageConsumer newStream(int msgTypeId, DirectBuffer buffer, int index, int length, MessageConsumer sender) {
    final BeginFW begin = beginRO.wrap(buffer, index, index + length);
    final long routeId = begin.routeId();
    final long initialId = begin.streamId();
    final long affinity = begin.affinity();
    final long authorization = begin.authorization();
    assert (initialId & 0x0000_0000_0000_0001L) != 0L;
    final OctetsFW extension = begin.extension();
    final ExtensionFW beginEx = extension.get(extensionRO::wrap);
    assert beginEx != null && beginEx.typeId() == kafkaTypeId;
    final KafkaBeginExFW kafkaBeginEx = extension.get(kafkaBeginExRO::wrap);
    final KafkaProduceBeginExFW kafkaProduceBeginEx = kafkaBeginEx.produce();
    final String16FW beginTopic = kafkaProduceBeginEx.topic();
    final int partitionId = kafkaProduceBeginEx.partition().partitionId();
    final int remoteIndex = supplyRemoteIndex.applyAsInt(initialId);
    final String topicName = beginTopic.asString();
    MessageConsumer newStream = null;
    final KafkaBindingConfig binding = supplyBinding.apply(routeId);
    final KafkaRouteConfig resolved = binding != null ? binding.resolve(authorization, topicName) : null;
    if (resolved != null) {
        final long resolvedId = resolved.id;
        final KafkaCacheRoute cacheRoute = supplyCacheRoute.apply(resolvedId);
        final long partitionKey = cacheRoute.topicPartitionKey(topicName, partitionId);
        KafkaCacheServerProduceFan fan = cacheRoute.serverProduceFansByTopicPartition.get(partitionKey);
        if (fan == null) {
            final KafkaCacheServerProduceFan newFan = new KafkaCacheServerProduceFan(resolvedId, authorization, affinity, partitionId, topicName);
            cacheRoute.serverProduceFansByTopicPartition.put(partitionKey, newFan);
            fan = newFan;
        }
        final int leaderId = cacheRoute.leadersByPartitionId.get(partitionId);
        final String cacheName = String.format("%s.%s", supplyNamespace.apply(routeId), supplyLocalName.apply(routeId));
        final KafkaCache cache = supplyCache.apply(cacheName);
        final KafkaCacheTopic topic = cache.supplyTopic(topicName);
        final KafkaCachePartition partition = topic.supplyProducePartition(partitionId, remoteIndex);
        newStream = new KafkaCacheServerProduceStream(fan, sender, routeId, initialId, leaderId, authorization, partition)::onServerMessage;
    }
    return newStream;
}
Also used : ExtensionFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.ExtensionFW) KafkaProduceBeginExFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.KafkaProduceBeginExFW) MessageConsumer(io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer) KafkaCachePartition(io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCachePartition) OctetsFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW) BeginFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.BeginFW) KafkaBindingConfig(io.aklivity.zilla.runtime.binding.kafka.internal.config.KafkaBindingConfig) KafkaBeginExFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.KafkaBeginExFW) KafkaCacheTopic(io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCacheTopic) KafkaCache(io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCache) KafkaRouteConfig(io.aklivity.zilla.runtime.binding.kafka.internal.config.KafkaRouteConfig) String16FW(io.aklivity.zilla.runtime.binding.kafka.internal.types.String16FW)

Example 5 with OctetsFW

use of io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW in project zilla by aklivity.

the class KafkaCacheServerFactory method newStream.

@Override
public MessageConsumer newStream(int msgTypeId, DirectBuffer buffer, int index, int length, MessageConsumer sender) {
    final BeginFW begin = beginRO.wrap(buffer, index, index + length);
    final OctetsFW extension = begin.extension();
    final ExtensionFW beginEx = extension.get(extensionRO::tryWrap);
    assert beginEx != null;
    final int typeId = beginEx.typeId();
    assert beginEx != null && typeId == kafkaTypeId;
    MessageConsumer newStream = null;
    final KafkaBeginExFW kafkaBeginEx = extension.get(kafkaBeginExRO::tryWrap);
    if (kafkaBeginEx != null) {
        final BindingHandler streamFactory = factories.get(kafkaBeginEx.kind());
        if (streamFactory != null) {
            newStream = streamFactory.newStream(begin.typeId(), begin.buffer(), begin.offset(), begin.sizeof(), sender);
        }
    }
    return newStream;
}
Also used : ExtensionFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.ExtensionFW) MessageConsumer(io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer) OctetsFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW) BeginFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.BeginFW) KafkaBeginExFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.KafkaBeginExFW) BindingHandler(io.aklivity.zilla.runtime.engine.binding.BindingHandler)

Aggregations

OctetsFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW)21 BeginFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.BeginFW)16 ExtensionFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.ExtensionFW)16 KafkaBeginExFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.KafkaBeginExFW)16 MessageConsumer (io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer)16 KafkaBindingConfig (io.aklivity.zilla.runtime.binding.kafka.internal.config.KafkaBindingConfig)13 String16FW (io.aklivity.zilla.runtime.binding.kafka.internal.types.String16FW)13 KafkaRouteConfig (io.aklivity.zilla.runtime.binding.kafka.internal.config.KafkaRouteConfig)11 KafkaCache (io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCache)6 KafkaCacheTopic (io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCacheTopic)6 DirectBuffer (org.agrona.DirectBuffer)5 MutableDirectBuffer (org.agrona.MutableDirectBuffer)5 KafkaCachePartition (io.aklivity.zilla.runtime.binding.kafka.internal.cache.KafkaCachePartition)4 KafkaOffsetFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.KafkaOffsetFW)4 KafkaDeltaType (io.aklivity.zilla.runtime.binding.kafka.internal.types.KafkaDeltaType)3 KafkaDescribeBeginExFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.KafkaDescribeBeginExFW)3 KafkaFetchBeginExFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.KafkaFetchBeginExFW)3 KafkaProduceBeginExFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.stream.KafkaProduceBeginExFW)3 BindingHandler (io.aklivity.zilla.runtime.engine.binding.BindingHandler)3 ArrayList (java.util.ArrayList)3