Search in sources :

Example 6 with BroadcastKey

use of com.hazelcast.jet.core.BroadcastKey in project hazelcast by hazelcast.

the class StreamKafkaP method restoreFromSnapshot.

@Override
public void restoreFromSnapshot(@Nonnull Object key0, @Nonnull Object value) {
    @SuppressWarnings("unchecked") Object key = ((BroadcastKey<Object>) key0).key();
    if (PARTITION_COUNTS_SNAPSHOT_KEY.equals(key)) {
        @SuppressWarnings("unchecked") Map<String, Integer> partitionCounts = (Map<String, Integer>) value;
        for (Entry<String, Integer> partitionCountEntry : partitionCounts.entrySet()) {
            String topicName = partitionCountEntry.getKey();
            int partitionCount = partitionCountEntry.getValue();
            int topicIndex = topics.indexOf(topicName);
            assert topicIndex >= 0;
            handleNewPartitions(topicIndex, partitionCount, true);
        }
    } else {
        TopicPartition topicPartition = (TopicPartition) key;
        long[] value1 = (long[]) value;
        long offset = value1[0];
        long watermark = value1[1];
        if (!offsets.containsKey(topicPartition.topic())) {
            getLogger().warning("Offset for topic '" + topicPartition.topic() + "' is restored from the snapshot, but the topic is not supposed to be read, ignoring");
            return;
        }
        int topicIndex = topics.indexOf(topicPartition.topic());
        assert topicIndex >= 0;
        handleNewPartitions(topicIndex, topicPartition.partition() + 1, true);
        if (!handledByThisProcessor(topicIndex, topicPartition.partition())) {
            return;
        }
        long[] topicOffsets = offsets.get(topicPartition.topic());
        assert topicOffsets[topicPartition.partition()] < 0 : "duplicate offset for topicPartition '" + topicPartition + "' restored, offset1=" + topicOffsets[topicPartition.partition()] + ", offset2=" + offset;
        topicOffsets[topicPartition.partition()] = offset;
        consumer.seek(topicPartition, offset + 1);
        Integer partitionIndex = currentAssignment.get(topicPartition);
        assert partitionIndex != null;
        eventTimeMapper.restoreWatermark(partitionIndex, watermark);
    }
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) BroadcastKey(com.hazelcast.jet.core.BroadcastKey) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with BroadcastKey

use of com.hazelcast.jet.core.BroadcastKey in project hazelcast by hazelcast.

the class StreamKafkaP method saveToSnapshot.

@Override
public boolean saveToSnapshot() {
    if (!emitFromTraverser(traverser)) {
        return false;
    }
    if (snapshotTraverser == null) {
        Stream<Entry<BroadcastKey<?>, ?>> snapshotStream = offsets.entrySet().stream().flatMap(entry -> IntStream.range(0, entry.getValue().length).filter(partition -> entry.getValue()[partition] >= 0).mapToObj(partition -> {
            TopicPartition key = new TopicPartition(entry.getKey(), partition);
            long offset = entry.getValue()[partition];
            long watermark = eventTimeMapper.getWatermark(currentAssignment.get(key));
            return entry(broadcastKey(key), new long[] { offset, watermark });
        }));
        snapshotTraverser = traverseStream(snapshotStream).onFirstNull(() -> {
            snapshotTraverser = null;
            if (getLogger().isFinestEnabled()) {
                getLogger().finest("Finished saving snapshot." + " Saved offsets: " + offsets() + ", Saved watermarks: " + watermarks());
            }
        });
        if (processorIndex == 0) {
            Entry<BroadcastKey<?>, ?> partitionCountsItem = entry(broadcastKey(PARTITION_COUNTS_SNAPSHOT_KEY), topics.stream().collect(Collectors.toMap(topic -> topic, topic -> offsets.get(topic).length)));
            snapshotTraverser = snapshotTraverser.append(partitionCountsItem);
        }
    }
    return emitFromTraverserToSnapshot(snapshotTraverser);
}
Also used : IntStream(java.util.stream.IntStream) AbstractProcessor(com.hazelcast.jet.core.AbstractProcessor) Traverser(com.hazelcast.jet.Traverser) Arrays(java.util.Arrays) Processor(com.hazelcast.jet.core.Processor) Traversers.traverseStream(com.hazelcast.jet.Traversers.traverseStream) LoggingUtil.logFinest(com.hazelcast.jet.impl.util.LoggingUtil.logFinest) HashMap(java.util.HashMap) EventTimePolicy(com.hazelcast.jet.core.EventTimePolicy) KafkaProcessors(com.hazelcast.jet.kafka.KafkaProcessors) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ArrayList(java.util.ArrayList) Traversers.traverseIterable(com.hazelcast.jet.Traversers.traverseIterable) Util.entry(com.hazelcast.jet.Util.entry) Duration(java.time.Duration) Map(java.util.Map) Nonnull(javax.annotation.Nonnull) EventTimeMapper(com.hazelcast.jet.core.EventTimeMapper) FunctionEx(com.hazelcast.function.FunctionEx) TopicPartition(org.apache.kafka.common.TopicPartition) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Properties(java.util.Properties) Collection(java.util.Collection) Traversers(com.hazelcast.jet.Traversers) Collectors(java.util.stream.Collectors) SupplierEx(com.hazelcast.function.SupplierEx) BroadcastKey(com.hazelcast.jet.core.BroadcastKey) List(java.util.List) BroadcastKey.broadcastKey(com.hazelcast.jet.core.BroadcastKey.broadcastKey) Stream(java.util.stream.Stream) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Entry(java.util.Map.Entry) SECONDS(java.util.concurrent.TimeUnit.SECONDS) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) Entry(java.util.Map.Entry) TopicPartition(org.apache.kafka.common.TopicPartition) BroadcastKey(com.hazelcast.jet.core.BroadcastKey)

Example 8 with BroadcastKey

use of com.hazelcast.jet.core.BroadcastKey in project hazelcast-jet by hazelcast.

the class SessionWindowP method restoreFromSnapshot.

@Override
@SuppressWarnings("unchecked")
protected void restoreFromSnapshot(@Nonnull Object key, @Nonnull Object value) {
    if (key instanceof BroadcastKey) {
        BroadcastKey bcastKey = (BroadcastKey) key;
        if (!Keys.CURRENT_WATERMARK.equals(bcastKey.key())) {
            throw new JetException("Unexpected broadcast key: " + bcastKey.key());
        }
        long newCurrentWatermark = (long) value;
        assert processingGuarantee != EXACTLY_ONCE || minRestoredCurrentWatermark == Long.MAX_VALUE || minRestoredCurrentWatermark == newCurrentWatermark : "different values for currentWatermark restored, before=" + minRestoredCurrentWatermark + ", new=" + newCurrentWatermark;
        minRestoredCurrentWatermark = Math.min(newCurrentWatermark, minRestoredCurrentWatermark);
        return;
    }
    keyToWindows.put((K) key, (Windows) value);
}
Also used : BroadcastKey(com.hazelcast.jet.core.BroadcastKey) JetException(com.hazelcast.jet.JetException)

Example 9 with BroadcastKey

use of com.hazelcast.jet.core.BroadcastKey in project hazelcast-jet by hazelcast.

the class StreamKafkaP method restoreFromSnapshot.

@Override
public void restoreFromSnapshot(@Nonnull Object key, @Nonnull Object value) {
    TopicPartition topicPartition = ((BroadcastKey<TopicPartition>) key).key();
    long[] value1 = (long[]) value;
    long offset = value1[0];
    long watermark = value1[1];
    long[] topicOffsets = offsets.get(topicPartition.topic());
    if (topicOffsets == null) {
        getLogger().severe("Offset for topic '" + topicPartition.topic() + "' is present in snapshot, but the topic is not supposed to be read");
        return;
    }
    if (topicPartition.partition() >= topicOffsets.length) {
        getLogger().severe("Offset for partition '" + topicPartition + "' is present in snapshot," + " but that topic currently has only " + topicOffsets.length + " partitions");
    }
    Integer partitionIndex = currentAssignment.get(topicPartition);
    if (partitionIndex != null) {
        assert topicOffsets[topicPartition.partition()] < 0 : "duplicate offset for topicPartition '" + topicPartition + "' restored, offset1=" + topicOffsets[topicPartition.partition()] + ", offset2=" + offset;
        topicOffsets[topicPartition.partition()] = offset;
        consumer.seek(topicPartition, offset + 1);
        watermarkSourceUtil.restoreWatermark(partitionIndex, watermark);
    }
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) BroadcastKey(com.hazelcast.jet.core.BroadcastKey)

Example 10 with BroadcastKey

use of com.hazelcast.jet.core.BroadcastKey in project hazelcast-jet by hazelcast.

the class StreamKafkaP method saveToSnapshot.

@Override
public boolean saveToSnapshot() {
    if (!emitFromTraverser(traverser)) {
        return false;
    }
    if (snapshotTraverser == null) {
        Stream<Entry<BroadcastKey<TopicPartition>, long[]>> snapshotStream = offsets.entrySet().stream().flatMap(entry -> IntStream.range(0, entry.getValue().length).filter(partition -> entry.getValue()[partition] >= 0).mapToObj(partition -> {
            TopicPartition key = new TopicPartition(entry.getKey(), partition);
            long offset = entry.getValue()[partition];
            long watermark = watermarkSourceUtil.getWatermark(currentAssignment.get(key));
            return entry(broadcastKey(key), new long[] { offset, watermark });
        }));
        snapshotTraverser = traverseStream(snapshotStream).onFirstNull(() -> snapshotTraverser = null);
    }
    return emitFromTraverserToSnapshot(snapshotTraverser);
}
Also used : IntStream(java.util.stream.IntStream) AbstractProcessor(com.hazelcast.jet.core.AbstractProcessor) Traverser(com.hazelcast.jet.Traverser) Arrays(java.util.Arrays) WatermarkSourceUtil(com.hazelcast.jet.core.WatermarkSourceUtil) Processor(com.hazelcast.jet.core.Processor) Traversers.traverseStream(com.hazelcast.jet.Traversers.traverseStream) LoggingUtil.logFinest(com.hazelcast.jet.impl.util.LoggingUtil.logFinest) HashMap(java.util.HashMap) KafkaProcessors(com.hazelcast.jet.kafka.KafkaProcessors) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) Traversers.traverseIterable(com.hazelcast.jet.Traversers.traverseIterable) InterruptException(org.apache.kafka.common.errors.InterruptException) Util.entry(com.hazelcast.jet.Util.entry) Map(java.util.Map) DistributedFunction(com.hazelcast.jet.function.DistributedFunction) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) DistributedSupplier(com.hazelcast.jet.function.DistributedSupplier) TopicPartition(org.apache.kafka.common.TopicPartition) Properties(java.util.Properties) Set(java.util.Set) Traversers(com.hazelcast.jet.Traversers) BroadcastKey(com.hazelcast.jet.core.BroadcastKey) List(java.util.List) BroadcastKey.broadcastKey(com.hazelcast.jet.core.BroadcastKey.broadcastKey) WatermarkGenerationParams(com.hazelcast.jet.core.WatermarkGenerationParams) Stream(java.util.stream.Stream) Preconditions(com.hazelcast.util.Preconditions) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Entry(java.util.Map.Entry) System.arraycopy(java.lang.System.arraycopy) SECONDS(java.util.concurrent.TimeUnit.SECONDS) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) Entry(java.util.Map.Entry) TopicPartition(org.apache.kafka.common.TopicPartition)

Aggregations

BroadcastKey (com.hazelcast.jet.core.BroadcastKey)10 JetException (com.hazelcast.jet.JetException)4 TopicPartition (org.apache.kafka.common.TopicPartition)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Entry (java.util.Map.Entry)3 Traverser (com.hazelcast.jet.Traverser)2 Traversers (com.hazelcast.jet.Traversers)2 Traversers.traverseIterable (com.hazelcast.jet.Traversers.traverseIterable)2 Traversers.traverseStream (com.hazelcast.jet.Traversers.traverseStream)2 Util.entry (com.hazelcast.jet.Util.entry)2 AbstractProcessor (com.hazelcast.jet.core.AbstractProcessor)2 BroadcastKey.broadcastKey (com.hazelcast.jet.core.BroadcastKey.broadcastKey)2 Processor (com.hazelcast.jet.core.Processor)2 BroadcastEntry (com.hazelcast.jet.impl.execution.BroadcastEntry)2 LoggingUtil.logFinest (com.hazelcast.jet.impl.util.LoggingUtil.logFinest)2 KafkaProcessors (com.hazelcast.jet.kafka.KafkaProcessors)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Properties (java.util.Properties)2