Search in sources :

Example 86 with Bytes

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

the class InMemoryTimeOrderedKeyValueBuffer method put.

@Override
public void put(final long time, final Record<K, Change<V>> record, final ProcessorRecordContext recordContext) {
    requireNonNull(record.value(), "value cannot be null");
    requireNonNull(recordContext, "recordContext cannot be null");
    final Bytes serializedKey = Bytes.wrap(keySerde.serializer().serialize(changelogTopic, record.key()));
    final Change<byte[]> serialChange = valueSerde.serializeParts(changelogTopic, record.value());
    final BufferValue buffered = getBuffered(serializedKey);
    final byte[] serializedPriorValue;
    if (buffered == null) {
        serializedPriorValue = serialChange.oldValue;
    } else {
        serializedPriorValue = buffered.priorValue();
    }
    cleanPut(time, serializedKey, new BufferValue(serializedPriorValue, serialChange.oldValue, serialChange.newValue, recordContext));
    dirtyKeys.add(serializedKey);
    updateBufferMetrics();
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes)

Example 87 with Bytes

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

the class InMemoryWindowStore method registerNewWindowedKeyValueIterator.

private WrappedWindowedKeyValueIterator registerNewWindowedKeyValueIterator(final Bytes keyFrom, final Bytes keyTo, final Iterator<Map.Entry<Long, ConcurrentNavigableMap<Bytes, byte[]>>> segmentIterator, final boolean forward) {
    final Bytes from = (retainDuplicates && keyFrom != null) ? wrapForDups(keyFrom, 0) : keyFrom;
    final Bytes to = (retainDuplicates && keyTo != null) ? wrapForDups(keyTo, Integer.MAX_VALUE) : keyTo;
    final WrappedWindowedKeyValueIterator iterator = new WrappedWindowedKeyValueIterator(from, to, segmentIterator, openIterators::remove, retainDuplicates, windowSize, forward);
    openIterators.add(iterator);
    return iterator;
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) WindowKeySchema.extractStoreKeyBytes(org.apache.kafka.streams.state.internals.WindowKeySchema.extractStoreKeyBytes)

Example 88 with Bytes

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

the class CachingSessionStore method findSessions.

@Override
public KeyValueIterator<Windowed<Bytes>, byte[]> findSessions(final Bytes keyFrom, final Bytes keyTo, final long earliestSessionEndTime, final long latestSessionStartTime) {
    if (keyFrom != null && keyTo != null && keyFrom.compareTo(keyTo) > 0) {
        LOG.warn(INVALID_RANGE_WARN_MSG);
        return KeyValueIterators.emptyIterator();
    }
    validateStoreOpen();
    final Bytes cacheKeyFrom = keyFrom == null ? null : cacheFunction.cacheKey(keySchema.lowerRange(keyFrom, earliestSessionEndTime));
    final Bytes cacheKeyTo = keyTo == null ? null : cacheFunction.cacheKey(keySchema.upperRange(keyTo, latestSessionStartTime));
    final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = context.cache().range(cacheName, cacheKeyFrom, cacheKeyTo);
    final KeyValueIterator<Windowed<Bytes>, byte[]> storeIterator = wrapped().findSessions(keyFrom, keyTo, earliestSessionEndTime, latestSessionStartTime);
    final HasNextCondition hasNextCondition = keySchema.hasNextCondition(keyFrom, keyTo, earliestSessionEndTime, latestSessionStartTime);
    final PeekingKeyValueIterator<Bytes, LRUCacheEntry> filteredCacheIterator = new FilteredCacheIterator(cacheIterator, hasNextCondition, cacheFunction);
    return new MergedSortedCacheSessionStoreIterator(filteredCacheIterator, storeIterator, cacheFunction, true);
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes)

Example 89 with Bytes

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

the class CachingSessionStore method putAndMaybeForward.

private void putAndMaybeForward(final ThreadCache.DirtyEntry entry, final InternalProcessorContext<?, ?> context) {
    final Bytes binaryKey = cacheFunction.key(entry.key());
    final Windowed<Bytes> bytesKey = SessionKeySchema.from(binaryKey);
    if (flushListener != null) {
        final byte[] newValueBytes = entry.newValue();
        final byte[] oldValueBytes = newValueBytes == null || sendOldValues ? wrapped().fetchSession(bytesKey.key(), bytesKey.window().start(), bytesKey.window().end()) : null;
        // we can skip flushing to downstream as well as writing to underlying store
        if (newValueBytes != null || oldValueBytes != null) {
            // we need to get the old values if needed, and then put to store, and then flush
            wrapped().put(bytesKey, entry.newValue());
            final ProcessorRecordContext current = context.recordContext();
            context.setRecordContext(entry.entry().context());
            try {
                flushListener.apply(new Record<>(binaryKey.get(), new Change<>(newValueBytes, sendOldValues ? oldValueBytes : null), entry.entry().context().timestamp(), entry.entry().context().headers()));
            } finally {
                context.setRecordContext(current);
            }
        }
    } else {
        wrapped().put(bytesKey, entry.newValue());
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) ProcessorRecordContext(org.apache.kafka.streams.processor.internals.ProcessorRecordContext) Change(org.apache.kafka.streams.kstream.internals.Change)

Example 90 with Bytes

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

the class CachingKeyValueStore method runKeyQuery.

@SuppressWarnings("unchecked")
private <R> QueryResult<R> runKeyQuery(final Query<R> query, final Position mergedPosition, final PositionBound positionBound, final QueryConfig config) {
    QueryResult<R> result = null;
    final KeyQuery<Bytes, byte[]> keyQuery = (KeyQuery<Bytes, byte[]>) query;
    if (keyQuery.isSkipCache()) {
        return wrapped().query(query, positionBound, config);
    }
    final Bytes key = keyQuery.getKey();
    if (context.cache() != null) {
        final LRUCacheEntry lruCacheEntry = context.cache().get(cacheName, key);
        if (lruCacheEntry != null) {
            final byte[] rawValue;
            if (timestampedSchema && !WrappedStateStore.isTimestamped(wrapped())) {
                rawValue = ValueAndTimestampDeserializer.rawValue(lruCacheEntry.value());
            } else {
                rawValue = lruCacheEntry.value();
            }
            result = (QueryResult<R>) QueryResult.forResult(rawValue);
        }
    }
    // the merged position above
    if (result == null) {
        result = wrapped().query(query, PositionBound.unbounded(), config);
    }
    result.setPosition(mergedPosition);
    return result;
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) KeyQuery(org.apache.kafka.streams.query.KeyQuery)

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