Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

This page is meant as a template for writing a KIP. To create a KIP choose Tools->Copy on this page and modify with your content and replace the heading with the next KIP number and a description of your issue. Replace anything in italics with your own description.

Status

Current state"Under Discussion"

...

Motivation

This is for adding mocks for state stores mock testing support for StateStore, StoreBuilder, StoreSupplier and other store related components which are used in Streams unit testing.

We'd like to use mocks for different types of state stores: KV, window, session - that can be used to record the number of expected put / get calls used in the DSL operator unit testing. These will provide conveniency provide convenience for developers when they are writing unit test for kafka Kafka stream and other related modules.

Public Interfaces

These will be internal classes, so no public API/interface.

Proposed Changes

Describe the new thing you want to do in appropriate detail. This may be fairly extensive and have large subsections of its own. Or it may be a few sentences. Use judgement based on the scope of the change.

I will use the KeyValueStore as an example. Window and Session will have a similar structure.

We will provide a MockStoreFactory to generate Mock store builders:

For example, in the current streams/TopologyTest.java:

Code Block
languagejava
public class TopologyTest {

    private final StoreBuilder storeBuilder = EasyMock.createNiceMock(StoreBuilder.class);
    private final KeyValueStoreBuilder globalStoreBuilder = EasyMock.createNiceMock(KeyValueStoreBuilder.class);
    private final Topology topology = new Topology();    
...

	@Test
    public void shouldFailIfSinkIsParent() {
        topology.addSource("source", "topic-1");
        topology.addSink("sink-1", "topic-2", "source");
        try {
            topology.addSink("sink-2", "topic-3", "sink-1");
            fail("Should throw TopologyException for using sink as parent");
        } catch (final TopologyException expected) { }
    }

    @Test(expected = TopologyException.class)
    public void shouldNotAllowToAddStateStoreToNonExistingProcessor() {
        mockStoreBuilder();
        EasyMock.replay(storeBuilder);
        topology.addStateStore(storeBuilder, "no-such-processor");
    }

    @Test
    public void shouldNotAllowToAddStateStoreToSource() {
        mockStoreBuilder();
        EasyMock.replay(storeBuilder);
        topology.addSource("source-1", "topic-1");
        try {
            topology.addStateStore(storeBuilder, "source-1");
            fail("Should have thrown TopologyException for adding store to source node");
        } catch (final TopologyException expected) { }
    }

	private void mockStoreBuilder() {
        EasyMock.expect(storeBuilder.name()).andReturn("store").anyTimes();
        EasyMock.expect(storeBuilder.logConfig()).andReturn(Collections.emptyMap());
        EasyMock.expect(storeBuilder.loggingEnabled()).andReturn(false);
    }
}

One of the goal is to replace the in-test vanilla mockStoreBuilder with a more general purpose mockStoreBuilder class, which simplify writing unit test and can be reuse later.

After the improvements, we will replace the easyMock StoreBuilder with our mockStoreBuilder.

Code Block
public class TopologyTest {

    private final StoreBuilder storeBuilder = EasyMock.createNiceMock(StoreBuilder.class);
    private final Topology topology = new Topology();
    private final MockStoreFactory mockStoreFactory = new MockStoreFactory<>();
    private final KeyValueStoreBuilder keyValueStoreBuilder = mockStoreFactory.createKeyValueStoreBuilder(
            Stores.inMemoryKeyValueStore("store"),
            Serdes.Bytes(),
            Serdes.Bytes(),
            false,
			Time.System);

...
    @Test(expected = TopologyException.class)
    public void shouldNotAllowToAddStateStoreToNonExistingProcessor() {
        topology.addStateStore(keyValueStoreBuilder, "no-such-processor");
    }
}


Public Interfaces

We add some new classes to a state package (org.apache.kafka.streams.stateunder streams/test-utils.


We will provide a MockStoreFactory to generate mock store builders, I will use the KeyValueStoreBuilder as an example. Window and Session will have a similar structure.

The developers/users can provide their own store as the backend storage, and their own Serde of choice. For example, for simple testing, they can just use an InMemoryKeyValueStore.

Code Block
package org.apache.kafka.streams.internals;

public class MockStoreFactory<K, V> {

    public final Map<String, StoreBuilder> stateStores = new LinkedHashMap<>();

    public MockStoreFactory () {
    }

    public KeyValueStoreBuilder createKeyValueStoreBuilder(KeyValueBytesStoreSupplier keyValueBytesStoreSupplier,
                                                           final Serde<K> keySerde,
                                                           final Serde<V> valueSerde,
                                                           boolean persistent){
		String storeName = keyValueBytesStoreSupplier.name();
        stateStores.put(storeName, new MockKeyValueStoreBuilder<>(keyValueBytesStoreSupplier, keySerde, valueSerde, persistent));
        return (KeyValueStoreBuilder)stateStores.get(storeName);
    }

	public WindowStoreBuilder createWindowStoreBuilder(KeyValueBytesStoreSupplier keyValueBytesStoreSupplier,
                                                           final Serde<K> keySerde,
                                                           final Serde<V> valueSerde,
                                                           final Time time){
	...
	}

	public SessionStoreBuilder createSessionStoreBuilder(KeyValueBytesStoreSupplier keyValueBytesStoreSupplier,
                                                           final Serde<K> keySerde,
                                                           final Serde<V> valueSerde,
                                                           final Time time){
	...
	}

    public StoreBuilder getStore(String storeName) {
        return stateStores.get(storeName);
    }
}


Each Store builder will have a build method:

Code Block
package org.apache.kafka.streams.state;

import org.apache.kafka.common.serialization.Serde;

import org.apache.kafka.common.utils.Time;
import org.apache.kafka.streams.state.internals.KeyValueStoreBuilder;


public class MockKeyValueStoreBuilder<K, V> extends KeyValueStoreBuilder<K, V> {

    private final boolean persistent;
    private final KeyValueBytesStoreSupplier storeSupplier;
    final Serde<K> keySerde;
    final Serde<V> valueSerde;
    final Time time;

    public MockKeyValueStoreBuilder(final KeyValueBytesStoreSupplier storeSupplier,
                                    final Serde<K> keySerde,
                                    final Serde<V> valueSerde,
                                    final boolean persistent,
                                    final Time time) {
        super(storeSupplier, keySerde, valueSerde, time);
        this.persistent = persistent;
        this.storeSupplier = storeSupplier;
        this.keySerde = keySerde;
        this.valueSerde = valueSerde;
        this.time = time;
    }

    @Override
    public KeyValueStore<K, V> build() {
        return new MockKeyValueStore<>(storeSupplier, keySerde, valueSerde, persistent, time);
    }
}


Then in the store, we will build a wrapper around the provided backend store. We will capture each get/put/delete call, the user can write tests accordingly. We will also track if the store has been flushed or closed.

Code Block
package org.apache.kafka.streams.state;

public class MockKeyValueStore<K, V>
        extends WrappedStateStore<KeyValueStore<Bytes, byte[]>, K, V>
        implements KeyValueStore<K, V>  {
    // keep a global counter of flushes and a local reference to which store had which
    // flush, so we can reason about the order in which stores get flushed.
    private static final AtomicInteger GLOBAL_FLUSH_COUNTER = new AtomicInteger(0);
    private final AtomicInteger instanceLastFlushCount = new AtomicInteger(-1);


    public boolean initialized = false;
    public boolean flushed = false;
    public boolean closed = true;

    public String name;
    public boolean persistent;

    protected final Time time;


    final Serde<K> keySerde;
    final Serde<V> valueSerde;
    StateSerdes<K, V> serdes;

    public final List<KeyValue<K, V>> capturedPutCalls = new LinkedList<>();
    public final List<KeyValue<K, V>> capturedGetCalls = new LinkedList<>();
    public final List<KeyValue<K, V>> capturedDeleteCalls = new LinkedList<>();


    public MockKeyValueStore(final KeyValueBytesStoreSupplier keyValueBytesStoreSupplier,
                             final Serde<K> keySerde,
                             final Serde<V> valueSerde,
                             final boolean persistent,
                             final Time time) {
        super(keyValueBytesStoreSupplier.get());
        this.name = keyValueBytesStoreSupplier.name();
        this.time = time != null ? time : Time.SYSTEM;
        this.persistent = persistent;
        this.keySerde = keySerde;
        this.valueSerde = valueSerde;
    }

    @SuppressWarnings("unchecked")
    void initStoreSerde(final ProcessorContext context) {
        serdes = new StateSerdes<>(
Code Block
package org.apache.kafka.streams.internals;

public class MockStoreFactory<K, V> {

    final String storeName;
    final Serde<K> keySerde;
    final Serde<V> valueSerde;
    final Time time;
    final Boolean persistent;

    public MockStoreFactory (final String storeName,
                             final Serde<K> keySerde,
                ProcessorStateManager.storeChangelogTopic(context.applicationId(), name()),
            final Serde<V> valueSerde,
  keySerde == null ? (Serde<K>) context.keySerde()     : keySerde,
                valueSerde final== Timenull time,
    ? (Serde<V>) context.valueSerde() : valueSerde);
    }

    @Override
    public String name() {
        return name;
 final Boolean persistent) {}

        this.storeName = storeName;
@Override
    public void init(final ProcessorContext  this.keySerde = keySerde;
context,
            this.valueSerde = valueSerde;
       final this.time = time;StateStore root) {
        this.persistent = persistent;
    }

context.register(root, stateRestoreCallback);
     public MockKeyValueStoreBuilder createKeyValueStoreBuilder(){
   initialized = true;
    return new MockKeyValueStoreBuilder<K,V>(storeName, keySerde, valueSerde,closed time,= persistent)false;
    }
}

Each Store builder will have a build method:

Code Block
package org.apache.kafka.streams.internals;

public class MockKeyValueStoreBuilder<K, V>  extends AbstractStoreBuilder<K, V, StateStore> {


    @Override
    public void flush() {
      final Boolean persistent;

 instanceLastFlushCount.set(GLOBAL_FLUSH_COUNTER.getAndIncrement());
     public MockKeyValueStoreBuilder(final String storeName, wrapped().flush();
        flushed      = true;
    }

    public int getLastFlushCount() {
        return instanceLastFlushCount.get();
  final Serde<K> keySerde,}

    @Override
    public void close() {
        wrapped().close();
        closed = true;
    }

    final@Override
 Serde<V> valueSerde,
  public boolean persistent() {
        return persistent;
    }

    @Override
    public boolean isOpen() {
       final Time time,return !closed;
    }

    public final StateRestoreCallback stateRestoreCallback = new StateRestoreCallback() {
        @Override
        public void restore(final   final Boolean persistent) {
byte[] key,
          super(storeName, keySerde, valueSerde, time);
        this.persistent = persistent;
    }

 final byte[] value) @Override{
     public KeyValueStore build() {}
    };

    return@Override
 new MockKeyValueStore(name, persistent);
  public  }
}

Then in the Store, we will be a wrapper around a InMemoryStore, and capture all the get/put calls (excluding Iterators)

Code Block
public class MockKeyValueStore extends InMemoryKeyValueStore {
void put(final K key, final V value) {
       private final boolean persistent;

capturedPutCalls.add(new KeyValue<>(key, value));
     public boolean initialized = false wrapped().put(keyBytes(key), serdes.rawValue(value));
    public}

 boolean flushed = false;
@Override
    public final List<KeyValue> capturedPutCalls = new LinkedList<>();
    public final List<KeyValue> capturedGetCalls = new LinkedList<>();
V putIfAbsent(final K key, final V value) {
       public final List<KeyValue>V capturedDeleteCallsoriginalValue = new LinkedList<>get(key);

      public MockKeyValueStore(final String name,
   if (originalValue == null) {
            put(key, value);
             final boolean persistent) {capturedPutCalls.add(new KeyValue<>(key, value));
        super(name);}
        this.persistent = persistentreturn originalValue;
    }

    @Override
    public voidV flushdelete(final K key) {
        flushedV value = true;
        super.flush(outerValue(wrapped().delete(keyBytes(key)));
    }

    @Override
    public boolean persistent() {capturedDeleteCalls.add(new KeyValue<>(key, value));
        return persistentvalue;
    }

    @Override
    public void putputAll(final Bytes key, final byte[] value List<KeyValue<K, V>> entries) {
        for (final KeyValue<K, V> entry : entries) {
            super.put(entry.key, entry.value);
            capturedPutCalls.add(new KeyValue(key, value));entry);
        }
    }

    @Override
    public byte[]V get(final BytesK key) {
        byte[]V value = super outerValue(wrapped().get(keyBytes(key)));
        capturedGetCalls.add(new KeyValueKeyValue<>(key, value));
        return value;
    }

    @SuppressWarnings("unchecked")
    @Override
    public KeyValueIterator<K,V> range(final K from, final K to) {
   }

    @Override
 return new MockKeyValueStore.MockKeyValueIterator(
 public byte[] delete(final Bytes key) {
        byte[] value = super.delete(key wrapped().range(Bytes.wrap(serdes.rawKey(from)), Bytes.wrap(serdes.rawKey(to))));
    }

    capturedDeleteCalls.add(new KeyValue(key, value));@SuppressWarnings("unchecked")
    @Override
    return value;
public KeyValueIterator<K,V>    }
all() {
    @Override
    publicreturn byte[] putIfAbsent(final Bytes key, final byte[] value) {new MockKeyValueStore.MockKeyValueIterator(wrapped().all());
    }

    @Override
    finalpublic byte[] originalValue = get(key);long approximateNumEntries() {
        ifreturn (originalValue == null) {wrapped().approximateNumEntries();
    }

    private V   put(key,outerValue(final byte[] value); {
        return value != null capturedPutCalls? serdes.addvalueFrom(new KeyValue(key, value));
 value) : null;
    }

    private Bytes  }keyBytes(final K key) {
        return originalValueBytes.wrap(serdes.rawKey(key));
    }

    @Override
private class MockKeyValueIterator  public void putAll(final List<KeyValue<Bytes, byte[]>> entries) {implements KeyValueIterator<K, V> {

        forprivate (final KeyValue<BytesKeyValueIterator<Bytes, byte[]> entry : entries) {
            put(entry.key, entry.value);
            capturedPutCalls.add(entry);
        }
    }
}iter;
		....
    }
}


Proposed Changes

I proposed to add:

  1. A MockStoreFactory class to produce mock state store builders.
  2. A mock StateStoreBuilder class for KV, Session and Window.
  3. A mock StateStore class for KV, Session and Window with tracking.

Compatibility, Deprecation, and Migration Plan

...

2) Examine the current tests (i.e. org.apache.kafka.streams.TopologyTest ), remove complicate and refactor the testing code logics and refactor with the new MockStateStores.

Rejected Alternatives

1) Rebuilding a MockStateStores vs extending an InMemoryStore

  • If we are rebuilding the functionality of a store from scratch in memory, it will basically act like an InMemoryStore.

2) Track all calls in a total order.

  • Now I haven't get a feature request to track calls within a total order, so now we track the order separately. If there is a need, we can definitely adjust it.

3)Using the MockStateStoreFactory as a main entry point to access stores. (Keeping the state of all StoreBuilder)

...

A discussion that has been brought up is 

Jira
serverASF JIRA
serverId5aa69414-a9e9-3523-82ec-879b028fb15b
keyKAFKA-8630
. In-memory Window/Session store doesn't work well in tests because in the init() method, it has internally cast ProcessorContext into InternalProcessorContext, that way the tester couldn't use MockProcessorContext. Either we twist the init() method in the mock store to accommodate or we can somehow use InternalMockProcessorContext instead?

Rejected Alternatives

  1. Using the current EasyMock implementation. There is no strong argument against the current EasyMock implementation, it is easy to use and lightweight. The main argument for this KIP is to write better tests with an in-house mock state store support.