Search in sources :

Example 96 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class MeteredTimestampedKeyValueStoreTest method shouldDelegateInit.

@Test
public void shouldDelegateInit() {
    final KeyValueStore<Bytes, byte[]> inner = mock(InMemoryKeyValueStore.class);
    final MeteredTimestampedKeyValueStore<String, String> outer = new MeteredTimestampedKeyValueStore<>(inner, STORE_TYPE, new MockTime(), Serdes.String(), new ValueAndTimestampSerde<>(Serdes.String()));
    expect(inner.name()).andStubReturn("store");
    inner.init((StateStoreContext) context, outer);
    expectLastCall();
    replay(inner, context);
    outer.init((StateStoreContext) context, outer);
    verify(inner);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 97 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class MeteredTimestampedKeyValueStoreTest method shouldDelegateDeprecatedInit.

@SuppressWarnings("deprecation")
@Test
public void shouldDelegateDeprecatedInit() {
    final KeyValueStore<Bytes, byte[]> inner = mock(InMemoryKeyValueStore.class);
    final MeteredTimestampedKeyValueStore<String, String> outer = new MeteredTimestampedKeyValueStore<>(inner, STORE_TYPE, new MockTime(), Serdes.String(), new ValueAndTimestampSerde<>(Serdes.String()));
    expect(inner.name()).andStubReturn("store");
    inner.init((ProcessorContext) context, outer);
    expectLastCall();
    replay(inner, context);
    outer.init((ProcessorContext) context, outer);
    verify(inner);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 98 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class MeteredWindowStoreTest method shouldDelegateDeprecatedInit.

@SuppressWarnings("deprecation")
@Test
public void shouldDelegateDeprecatedInit() {
    final WindowStore<Bytes, byte[]> inner = mock(WindowStore.class);
    final MeteredWindowStore<String, String> outer = new MeteredWindowStore<>(inner, // any size
    WINDOW_SIZE_MS, STORE_TYPE, new MockTime(), Serdes.String(), new SerdeThatDoesntHandleNull());
    expect(inner.name()).andStubReturn("store");
    inner.init((ProcessorContext) context, outer);
    expectLastCall();
    replay(inner);
    outer.init((ProcessorContext) context, outer);
    verify(inner);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 99 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class RocksDBTimestampedStoreTest method shouldMigrateDataFromDefaultToTimestampColumnFamily.

@Test
public void shouldMigrateDataFromDefaultToTimestampColumnFamily() throws Exception {
    prepareOldStore();
    try (final LogCaptureAppender appender = LogCaptureAppender.createAndRegister(RocksDBTimestampedStore.class)) {
        rocksDBStore.init((StateStoreContext) context, rocksDBStore);
        assertThat(appender.getMessages(), hasItem("Opening store " + DB_NAME + " in upgrade mode"));
    }
    // approx: 7 entries on old CF, 0 in new CF
    assertThat(rocksDBStore.approximateNumEntries(), is(7L));
    // get()
    // should be no-op on both CF
    assertThat(rocksDBStore.get(new Bytes("unknown".getBytes())), new IsNull<>());
    // approx: 7 entries on old CF, 0 in new CF
    assertThat(rocksDBStore.approximateNumEntries(), is(7L));
    // should migrate key1 from old to new CF
    // must return timestamp plus value, ie, it's not 1 byte but 9 bytes
    assertThat(rocksDBStore.get(new Bytes("key1".getBytes())).length, is(8 + 1));
    // one delete on old CF, one put on new CF
    // approx: 6 entries on old CF, 1 in new CF
    assertThat(rocksDBStore.approximateNumEntries(), is(7L));
    // put()
    // should migrate key2 from old to new CF with new value
    rocksDBStore.put(new Bytes("key2".getBytes()), "timestamp+22".getBytes());
    // one delete on old CF, one put on new CF
    // approx: 5 entries on old CF, 2 in new CF
    assertThat(rocksDBStore.approximateNumEntries(), is(7L));
    // should delete key3 from old and new CF
    rocksDBStore.put(new Bytes("key3".getBytes()), null);
    // count is off by one, due to two delete operations (even if one does not delete anything)
    // approx: 4 entries on old CF, 1 in new CF
    assertThat(rocksDBStore.approximateNumEntries(), is(5L));
    // should add new key8 to new CF
    rocksDBStore.put(new Bytes("key8".getBytes()), "timestamp+88888888".getBytes());
    // one delete on old CF, one put on new CF
    // approx: 3 entries on old CF, 2 in new CF
    assertThat(rocksDBStore.approximateNumEntries(), is(5L));
    // putIfAbsent()
    // should migrate key4 from old to new CF with old value
    assertThat(rocksDBStore.putIfAbsent(new Bytes("key4".getBytes()), "timestamp+4444".getBytes()).length, is(8 + 4));
    // one delete on old CF, one put on new CF
    // approx: 2 entries on old CF, 3 in new CF
    assertThat(rocksDBStore.approximateNumEntries(), is(5L));
    // should add new key11 to new CF
    assertThat(rocksDBStore.putIfAbsent(new Bytes("key11".getBytes()), "timestamp+11111111111".getBytes()), new IsNull<>());
    // one delete on old CF, one put on new CF
    // approx: 1 entries on old CF, 4 in new CF
    assertThat(rocksDBStore.approximateNumEntries(), is(5L));
    // should not delete key5 but migrate to new CF
    assertThat(rocksDBStore.putIfAbsent(new Bytes("key5".getBytes()), null).length, is(8 + 5));
    // one delete on old CF, one put on new CF
    // approx: 0 entries on old CF, 5 in new CF
    assertThat(rocksDBStore.approximateNumEntries(), is(5L));
    // should be no-op on both CF
    assertThat(rocksDBStore.putIfAbsent(new Bytes("key12".getBytes()), null), new IsNull<>());
    // two delete operation, however, only one is counted because old CF count was zero before already
    // approx: 0 entries on old CF, 4 in new CF
    assertThat(rocksDBStore.approximateNumEntries(), is(4L));
    // delete()
    // should delete key6 from old and new CF
    assertThat(rocksDBStore.delete(new Bytes("key6".getBytes())).length, is(8 + 6));
    // two delete operation, however, only one is counted because old CF count was zero before already
    // approx: 0 entries on old CF, 3 in new CF
    assertThat(rocksDBStore.approximateNumEntries(), is(3L));
    iteratorsShouldNotMigrateData();
    assertThat(rocksDBStore.approximateNumEntries(), is(3L));
    rocksDBStore.close();
    verifyOldAndNewColumnFamily();
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) LogCaptureAppender(org.apache.kafka.streams.processor.internals.testutil.LogCaptureAppender) Test(org.junit.Test)

Example 100 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class RocksDBTimestampedStoreTest method shouldOpenNewStoreInRegularMode.

@Test
public void shouldOpenNewStoreInRegularMode() {
    try (final LogCaptureAppender appender = LogCaptureAppender.createAndRegister(RocksDBTimestampedStore.class)) {
        rocksDBStore.init((StateStoreContext) context, rocksDBStore);
        assertThat(appender.getMessages(), hasItem("Opening store " + DB_NAME + " in regular mode"));
    }
    try (final KeyValueIterator<Bytes, byte[]> iterator = rocksDBStore.all()) {
        assertThat(iterator.hasNext(), is(false));
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) LogCaptureAppender(org.apache.kafka.streams.processor.internals.testutil.LogCaptureAppender) Test(org.junit.Test)

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