Search in sources :

Example 61 with Bytes

use of org.apache.kafka.common.utils.Bytes in project apache-kafka-on-k8s by banzaicloud.

the class NamedCacheTest method shouldNotThrowIllegalArgumentAfterEvictingDirtyRecordAndThenPuttingNewRecordWithSameKey.

@Test
public void shouldNotThrowIllegalArgumentAfterEvictingDirtyRecordAndThenPuttingNewRecordWithSameKey() {
    final LRUCacheEntry dirty = new LRUCacheEntry(new byte[] { 3 }, true, 0, 0, 0, "");
    final LRUCacheEntry clean = new LRUCacheEntry(new byte[] { 3 });
    final Bytes key = Bytes.wrap(new byte[] { 3 });
    cache.setListener(new ThreadCache.DirtyEntryFlushListener() {

        @Override
        public void apply(final List<ThreadCache.DirtyEntry> dirty) {
            cache.put(key, clean);
        }
    });
    cache.put(key, dirty);
    cache.evict();
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) Test(org.junit.Test)

Example 62 with Bytes

use of org.apache.kafka.common.utils.Bytes in project apache-kafka-on-k8s by banzaicloud.

the class RocksDBStoreTest method shouldThrowProcessorStateExceptionOnPutDeletedDir.

@Test(expected = ProcessorStateException.class)
public void shouldThrowProcessorStateExceptionOnPutDeletedDir() throws IOException {
    rocksDBStore.init(context, rocksDBStore);
    Utils.delete(dir);
    rocksDBStore.put(new Bytes(stringSerializer.serialize(null, "anyKey")), stringSerializer.serialize(null, "anyValue"));
    rocksDBStore.flush();
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) Test(org.junit.Test)

Example 63 with Bytes

use of org.apache.kafka.common.utils.Bytes in project apache-kafka-on-k8s by banzaicloud.

the class SegmentedCacheFunctionTest method cacheKey.

@Test
public void cacheKey() {
    final long segmentId = TIMESTAMP / SEGMENT_INTERVAL;
    final Bytes actualCacheKey = cacheFunction.cacheKey(THE_KEY);
    final ByteBuffer buffer = ByteBuffer.wrap(actualCacheKey.get());
    assertThat(buffer.getLong(), equalTo(segmentId));
    byte[] actualKey = new byte[buffer.remaining()];
    buffer.get(actualKey);
    assertThat(Bytes.wrap(actualKey), equalTo(THE_KEY));
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 64 with Bytes

use of org.apache.kafka.common.utils.Bytes in project apache-kafka-on-k8s by banzaicloud.

the class StreamThreadTest method shouldReturnStandbyTaskMetadataWhileRunningState.

@Test
public void shouldReturnStandbyTaskMetadataWhileRunningState() {
    internalStreamsBuilder.stream(Collections.singleton(topic1), consumed).groupByKey().count(Materialized.<Object, Long, KeyValueStore<Bytes, byte[]>>as("count-one"));
    final StreamThread thread = createStreamThread(clientId, config, false);
    final MockConsumer<byte[], byte[]> restoreConsumer = clientSupplier.restoreConsumer;
    restoreConsumer.updatePartitions("stream-thread-test-count-one-changelog", Collections.singletonList(new PartitionInfo("stream-thread-test-count-one-changelog", 0, null, new Node[0], new Node[0])));
    final HashMap<TopicPartition, Long> offsets = new HashMap<>();
    offsets.put(new TopicPartition("stream-thread-test-count-one-changelog", 1), 0L);
    restoreConsumer.updateEndOffsets(offsets);
    restoreConsumer.updateBeginningOffsets(offsets);
    thread.setState(StreamThread.State.RUNNING);
    thread.rebalanceListener.onPartitionsRevoked(null);
    final Map<TaskId, Set<TopicPartition>> standbyTasks = new HashMap<>();
    // assign single partition
    standbyTasks.put(task1, Collections.singleton(t1p1));
    thread.taskManager().setAssignmentMetadata(Collections.<TaskId, Set<TopicPartition>>emptyMap(), standbyTasks);
    thread.rebalanceListener.onPartitionsAssigned(Collections.<TopicPartition>emptyList());
    thread.runOnce(-1);
    ThreadMetadata threadMetadata = thread.threadMetadata();
    assertEquals(StreamThread.State.RUNNING.name(), threadMetadata.threadState());
    assertTrue(threadMetadata.standbyTasks().contains(new TaskMetadata(task1.toString(), Utils.mkSet(t1p1))));
    assertTrue(threadMetadata.activeTasks().isEmpty());
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) TaskMetadata(org.apache.kafka.streams.processor.TaskMetadata) Bytes(org.apache.kafka.common.utils.Bytes) TopicPartition(org.apache.kafka.common.TopicPartition) ThreadMetadata(org.apache.kafka.streams.processor.ThreadMetadata) PartitionInfo(org.apache.kafka.common.PartitionInfo) InternalStreamsBuilderTest(org.apache.kafka.streams.kstream.internals.InternalStreamsBuilderTest) Test(org.junit.Test)

Example 65 with Bytes

use of org.apache.kafka.common.utils.Bytes in project apache-kafka-on-k8s by banzaicloud.

the class StreamsMetadataStateTest method before.

@Before
public void before() throws Exception {
    builder = new StreamsBuilder();
    final KStream<Object, Object> one = builder.stream("topic-one");
    one.groupByKey().count("table-one");
    final KStream<Object, Object> two = builder.stream("topic-two");
    two.groupByKey().count("table-two");
    builder.stream("topic-three").groupByKey().count("table-three");
    one.merge(two).groupByKey().count("merged-table");
    builder.stream("topic-four").mapValues(new ValueMapper<Object, Object>() {

        @Override
        public Object apply(final Object value) {
            return value;
        }
    });
    builder.globalTable("global-topic", Consumed.with(null, null), Materialized.<Object, Object, KeyValueStore<Bytes, byte[]>>as(globalTable));
    StreamsBuilderTest.internalTopologyBuilder(builder).setApplicationId("appId");
    topic1P0 = new TopicPartition("topic-one", 0);
    topic1P1 = new TopicPartition("topic-one", 1);
    topic2P0 = new TopicPartition("topic-two", 0);
    topic2P1 = new TopicPartition("topic-two", 1);
    topic3P0 = new TopicPartition("topic-three", 0);
    topic4P0 = new TopicPartition("topic-four", 0);
    hostOne = new HostInfo("host-one", 8080);
    hostTwo = new HostInfo("host-two", 9090);
    hostThree = new HostInfo("host-three", 7070);
    hostToPartitions = new HashMap<>();
    hostToPartitions.put(hostOne, Utils.mkSet(topic1P0, topic2P1, topic4P0));
    hostToPartitions.put(hostTwo, Utils.mkSet(topic2P0, topic1P1));
    hostToPartitions.put(hostThree, Collections.singleton(topic3P0));
    partitionInfos = Arrays.asList(new PartitionInfo("topic-one", 0, null, null, null), new PartitionInfo("topic-one", 1, null, null, null), new PartitionInfo("topic-two", 0, null, null, null), new PartitionInfo("topic-two", 1, null, null, null), new PartitionInfo("topic-three", 0, null, null, null), new PartitionInfo("topic-four", 0, null, null, null));
    cluster = new Cluster(null, Collections.<Node>emptyList(), partitionInfos, Collections.<String>emptySet(), Collections.<String>emptySet());
    discovery = new StreamsMetadataState(StreamsBuilderTest.internalTopologyBuilder(builder), hostOne);
    discovery.onChange(hostToPartitions, cluster);
    partitioner = new StreamPartitioner<String, Object>() {

        @Override
        public Integer partition(final String key, final Object value, final int numPartitions) {
            return 1;
        }
    };
}
Also used : Node(org.apache.kafka.common.Node) Cluster(org.apache.kafka.common.Cluster) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Bytes(org.apache.kafka.common.utils.Bytes) TopicPartition(org.apache.kafka.common.TopicPartition) PartitionInfo(org.apache.kafka.common.PartitionInfo) HostInfo(org.apache.kafka.streams.state.HostInfo) Before(org.junit.Before)

Aggregations

Bytes (org.apache.kafka.common.utils.Bytes)398 Test (org.junit.Test)309 Windowed (org.apache.kafka.streams.kstream.Windowed)84 KeyValue (org.apache.kafka.streams.KeyValue)68 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)53 SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)50 Properties (java.util.Properties)49 ArrayList (java.util.ArrayList)42 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)38 StreamsConfig (org.apache.kafka.streams.StreamsConfig)35 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)35 Materialized (org.apache.kafka.streams.kstream.Materialized)33 Serdes (org.apache.kafka.common.serialization.Serdes)32 Metrics (org.apache.kafka.common.metrics.Metrics)29 KafkaStreams (org.apache.kafka.streams.KafkaStreams)28 MockStreamsMetrics (org.apache.kafka.streams.processor.internals.MockStreamsMetrics)27 Consumed (org.apache.kafka.streams.kstream.Consumed)25 KTable (org.apache.kafka.streams.kstream.KTable)23 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)22 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)21