aliehsaeedii commented on code in PR #21446: URL: https://github.com/apache/kafka/pull/21446#discussion_r2798750250
########## streams/src/test/java/org/apache/kafka/streams/state/internals/RocksDBTimestampedStoreWithHeadersTest.java: ########## @@ -0,0 +1,535 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.streams.state.internals; + +import org.apache.kafka.common.serialization.Serializer; +import org.apache.kafka.common.serialization.StringSerializer; +import org.apache.kafka.common.utils.Bytes; +import org.apache.kafka.common.utils.LogCaptureAppender; +import org.apache.kafka.streams.KeyValue; +import org.apache.kafka.streams.state.KeyValueIterator; + +import org.junit.jupiter.api.Test; +import org.rocksdb.ColumnFamilyDescriptor; +import org.rocksdb.ColumnFamilyHandle; +import org.rocksdb.ColumnFamilyOptions; +import org.rocksdb.DBOptions; +import org.rocksdb.RocksDB; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +import static java.util.Arrays.asList; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class RocksDBTimestampedStoreWithHeadersTest extends RocksDBStoreTest { + + private final Serializer<String> stringSerializer = new StringSerializer(); + + RocksDBStore getRocksDBStore() { + return new RocksDBTimestampedStoreWithHeaders(DB_NAME, METRICS_SCOPE); + } + + @Test + public void shouldOpenNewStoreInRegularMode() { + try (final LogCaptureAppender appender = LogCaptureAppender.createAndRegister(RocksDBTimestampedStoreWithHeaders.class)) { + rocksDBStore.init(context, rocksDBStore); + + assertTrue(appender.getMessages().contains("Opening store " + DB_NAME + " in regular headers-aware mode")); + } + + try (final KeyValueIterator<Bytes, byte[]> iterator = rocksDBStore.all()) { + assertFalse(iterator.hasNext()); + } + } + + @Test + public void shouldOpenExistingStoreInRegularMode() throws Exception { + // prepare store + rocksDBStore.init(context, rocksDBStore); + rocksDBStore.put(new Bytes("key".getBytes()), "timestampedWithHeaders".getBytes()); + rocksDBStore.close(); + + // re-open store + try (final LogCaptureAppender appender = LogCaptureAppender.createAndRegister(RocksDBTimestampedStoreWithHeaders.class)) { + rocksDBStore.init(context, rocksDBStore); + + assertTrue(appender.getMessages().contains("Opening store " + DB_NAME + " in regular headers-aware mode")); + } finally { + rocksDBStore.close(); + } + + // verify store + final DBOptions dbOptions = new DBOptions(); + final ColumnFamilyOptions columnFamilyOptions = new ColumnFamilyOptions(); + + final List<ColumnFamilyDescriptor> columnFamilyDescriptors = asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, columnFamilyOptions), + new ColumnFamilyDescriptor("keyValueWithTimestampAndHeaders".getBytes(StandardCharsets.UTF_8), columnFamilyOptions)); + final List<ColumnFamilyHandle> columnFamilies = new ArrayList<>(columnFamilyDescriptors.size()); + + RocksDB db = null; + ColumnFamilyHandle defaultColumnFamily = null, headersColumnFamily = null; + try { + db = RocksDB.open( + dbOptions, + new File(new File(context.stateDir(), "rocksdb"), DB_NAME).getAbsolutePath(), + columnFamilyDescriptors, + columnFamilies); + + defaultColumnFamily = columnFamilies.get(0); + headersColumnFamily = columnFamilies.get(1); + + assertNull(db.get(defaultColumnFamily, "key".getBytes())); + assertEquals(0L, db.getLongProperty(defaultColumnFamily, "rocksdb.estimate-num-keys")); + assertEquals(22, db.get(headersColumnFamily, "key".getBytes()).length); Review Comment: I added the explanation, but we should know that the value is not longer than the pure value length as the put method did not add headers. Considering the purpose of the test, I think we can remove this check even. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
