Reformat for checkstyle compliance and modernize with JUnit5, etc.
Change-Id: I6fab572f429578e6be924f6d8bcffcc6d083d333
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
long nextIndex();
/**
- * Try to move to the next binary data block
+ * Try to move to the next binary data block.
*
* @param entryMapper callback to be invoked on binary data
* @return processed binary data, or {@code null}
/**
* Stores information about a {@link JournalSegment} of the log.
+ *
* <p>
* The segment descriptor manages metadata related to a single segment of the log. Descriptors are stored within the
* first {@code 64} bytes of each segment in the following order:
/**
* Returns the segment version.
+ *
* <p>
* Versions are monotonically increasing starting at {@code 1}.
*
/**
* Returns the segment identifier.
+ *
* <p>
* The segment ID is a monotonically increasing number within each log. Segments with in-sequence identifiers should
* contain in-sequence indexes.
/**
* Returns the segment index.
+ *
* <p>
- * The index indicates the index at which the first entry should be written to the segment. Indexes are monotonically
- * increasing thereafter.
+ * The index indicates the index at which the first entry should be written to the segment. Indexes are
+ * monotonically increasing thereafter.
*
* @return The segment index.
*/
/**
* Returns last time the segment was updated.
+ *
* <p>
- * When the segment is first constructed, the {@code updated} time is {@code 0}. Once all entries in the segment have
- * been committed, the {@code updated} time should be set to the current time. Log compaction should not result in a
- * change to {@code updated}.
+ * When the segment is first constructed, the {@code updated} time is {@code 0}. Once all entries in the segment
+ * have been committed, the {@code updated} time should be set to the current time. Log compaction should not result
+ * in a change to {@code updated}.
*
* @return The last time the segment was updated in terms of milliseconds since the epoch.
*/
}
/**
- * Returns this segment as an array of bytes
+ * Returns this segment as an array of bytes.
*
* @return bytes
*/
/**
* Returns a descriptor builder.
+ *
* <p>
* The descriptor builder will write segment metadata to a {@code 48} byte in-memory buffer.
*
}
/**
- * Sets updated timestamp;
+ * Sets updated timestamp.
*
* @param updated Epoch milliseconds
* @return The segment descriptor builder.
}
/**
- * Reads the next binary data block
+ * Reads the next binary data block.
*
* @return The binary data, or {@code null}
*/
*
* @deprecated due to dependency on outdated Kryo library, {@link ByteBufMapper} to be used instead.
*/
-@Deprecated(forRemoval = true, since="9.0.3")
+@Deprecated(forRemoval = true, since = "9.0.3")
public interface JournalSerdes {
/**
* Serializes given object to byte array.
* @param <T> deserialized Object type
* @return deserialized Object
*/
- <T> T deserialize(final ByteBuffer buffer);
+ <T> T deserialize(ByteBuffer buffer);
/**
* Deserializes given InputStream to an Object.
* @param bufferSize size of the buffer in front of the stream
* @return deserialized Object
*/
- <T> T deserialize(final InputStream stream, final int bufferSize);
+ <T> T deserialize(InputStream stream, int bufferSize);
/**
* Returns a {@link ByteBufMapper} backed by this object.
/**
* Registers serializer for the given set of classes.
+ *
* <p>
* When multiple classes are registered with an explicitly provided serializer, the namespace guarantees
* all instances will be serialized with the same type ID.
*/
record SegmentEntry(int checksum, @NonNull ByteBuffer bytes) {
/**
- * The size of the header, comprising of:
+ * The size of the header. It is comprised of
* <ul>
* <li>32-bit signed entry length</li>
* <li>32-bit unsigned CRC32 checksum</li>
- * </li>
+ * </ul>
*/
static final int HEADER_BYTES = Integer.BYTES + Integer.BYTES;
/**
* Compacts the journal up to the given index.
+ *
* <p>
* The semantics of compaction are not specified by this interface.
*
}
/**
- * Sets the journal directory
+ * Sets the journal directory.
*
* @param directory The log directory.
* @return The builder instance
/**
* Sets the journal index density.
+ *
* <p>
* The index density is the frequency at which the position of entries written to the journal will be
* recorded in an in-memory index for faster seeking.
/**
* Enables flushing buffers to disk when entries are committed to a segment.
+ *
* <p>
* When flush-on-commit is enabled, log entry buffers will be automatically flushed to disk each time
* an entry is committed in a given segment.
/**
* Sets whether to flush buffers to disk when entries are committed to a segment.
+ *
* <p>
* When flush-on-commit is enabled, log entry buffers will be automatically flushed to disk each time
* an entry is committed in a given segment.
/**
* Compacts the journal up to the given index.
+ *
* <p>
* The semantics of compaction are not specified by this interface.
*
/**
* Sets the journal storage level.
+ *
* <p>
* The storage level indicates how individual entries will be persisted in the journal.
*
/**
* Sets the journal storage directory.
+ *
* <p>
* The journal will write segment files into the provided directory.
*
/**
* Sets the journal storage directory.
+ *
* <p>
* The journal will write segment files into the provided directory.
*
* @throws NullPointerException If the {@code directory} is {@code null}
*/
public Builder<E> withDirectory(final File directory) {
- byteJournalBuilder.withDirectory(directory);
+ byteJournalBuilder.withDirectory(directory);
return this;
}
* @return The journal builder.
* @deprecated due to serialization refactoring, use {@link Builder#withMapper(ByteBufMapper)} instead
*/
- @Deprecated(forRemoval = true, since="9.0.3")
+ @Deprecated(forRemoval = true, since = "9.0.3")
public Builder<E> withNamespace(final JournalSerdes namespace) {
return withMapper(requireNonNull(namespace, "namespace cannot be null").toMapper());
}
/**
* Sets the maximum segment size in bytes.
+ *
* <p>
* The maximum segment size dictates when journal should roll over to new segments. As entries are written
* to a journal segment, once the size of the segment surpasses the configured maximum segment size, the
* journal will create a new segment and append new entries to that segment.
+ *
* <p>
* By default, the maximum segment size is 32M.
*
/**
* Sets the journal index density.
+ *
* <p>
* The index density is the frequency at which the position of entries written to the journal will be recorded
* in an in-memory index for faster seeking.
/**
* Enables flushing buffers to disk when entries are committed to a segment.
+ *
* <p>
* When flush-on-commit is enabled, log entry buffers will be automatically flushed to disk each time an
* entry is committed in a given segment.
/**
* Enables flushing buffers to disk when entries are committed to a segment.
+ *
* <p>
* When flush-on-commit is enabled, log entry buffers will be automatically flushed to disk each time an
* entry is committed in a given segment.
import io.atomix.storage.journal.JournalSerdes.EntrySerdes;
import java.io.IOException;
-@Deprecated(forRemoval = true, since="9.0.3")
+@Deprecated(forRemoval = true, since = "9.0.3")
final class EntrySerializer<T> extends Serializer<T> {
// Note: uses identity to create things in Kryo, hence we want an instance for every serdes we wrap
private final JavaSerializer javaSerializer = new JavaSerializer();
import io.atomix.storage.journal.JournalSerdes.EntryInput;
import java.io.IOException;
-@Deprecated(forRemoval = true, since="9.0.3")
+@Deprecated(forRemoval = true, since = "9.0.3")
final class KryoEntryInput implements EntryInput {
private final Kryo kryo;
private final Input input;
import io.atomix.storage.journal.JournalSerdes.EntryOutput;
import java.io.IOException;
-@Deprecated(forRemoval = true, since="9.0.3")
+@Deprecated(forRemoval = true, since = "9.0.3")
final class KryoEntryOutput implements EntryOutput {
private final Kryo kryo;
private final Output output;
/**
* Pool of Kryo instances, with classes pre-registered.
*/
-@Deprecated(forRemoval = true, since="9.0.3")
+@Deprecated(forRemoval = true, since = "9.0.3")
final class KryoJournalSerdes implements JournalSerdes, KryoFactory, KryoPool {
/**
* Default buffer size used for serialization.
}
for (Class<?> type : types) {
- Registration r = null;
+ Registration reg = null;
if (serializer == null) {
- r = kryo.register(type, id);
+ reg = kryo.register(type, id);
} else if (type.isInterface()) {
kryo.addDefaultSerializer(type, serializer);
} else {
- r = kryo.register(type, serializer, id);
+ reg = kryo.register(type, serializer, id);
}
- if (r != null) {
- if (r.getId() != id) {
+ if (reg != null) {
+ if (reg.getId() != id) {
LOGGER.debug("{}: {} already registered as {}. Skipping {}.",
- friendlyName, r.getType(), r.getId(), id);
+ friendlyName, reg.getType(), reg.getId(), id);
}
- LOGGER.trace("{} registered as {}", r.getType(), r.getId());
+ LOGGER.trace("{} registered as {}", reg.getType(), reg.getId());
}
}
}
import java.util.ArrayList;
import java.util.List;
-@Deprecated(forRemoval = true, since="9.0.3")
+@Deprecated(forRemoval = true, since = "9.0.3")
public final class KryoJournalSerdesBuilder implements Builder {
private final List<RegisteredType> types = new ArrayList<>();
private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
checkState(!types.isEmpty(), "No serializers registered");
return new KryoJournalSerdes(types, classLoader, friendlyName);
}
-}
\ No newline at end of file
+}
import static java.util.Objects.requireNonNull;
-@Deprecated(forRemoval = true, since="9.0.3")
+@Deprecated(forRemoval = true, since = "9.0.3")
record RegisteredType(EntrySerializer<?> serializer, Class<?>[] types) {
RegisteredType {
requireNonNull(serializer);
*/
@RunWith(Parameterized.class)
public abstract class AbstractJournalTest {
- @Deprecated(forRemoval = true, since="9.0.3")
+ @Deprecated(forRemoval = true, since = "9.0.3")
private static final JournalSerdes NAMESPACE = JournalSerdes.builder()
.register(new TestEntrySerdes(), TestEntry.class)
.register(new ByteArraySerdes(), byte[].class)
@Test
public void testWriteRead() throws Exception {
- try (Journal<TestEntry> journal = createJournal()) {
- JournalWriter<TestEntry> writer = journal.writer();
- JournalReader<TestEntry> reader = journal.openReader(1);
+ try (var journal = createJournal()) {
+ var writer = journal.writer();
+ var reader = journal.openReader(1);
// Append a couple entries.
assertEquals(1, writer.getNextIndex());
@Test
public void testTruncateRead() throws Exception {
- int i = 10;
+ final int cnt = 10;
try (Journal<TestEntry> journal = createJournal()) {
JournalWriter<TestEntry> writer = journal.writer();
JournalReader<TestEntry> reader = journal.openReader(1);
- for (int j = 1; j <= i; j++) {
- assertEquals(j, writer.append(new TestEntry(32)).index());
+ for (int i = 1; i <= cnt; i++) {
+ assertEquals(i, writer.append(new TestEntry(32)).index());
}
- for (int j = 1; j <= i - 2; j++) {
- assertEquals(j, assertNext(reader).index());
+ for (int i = 1; i <= cnt - 2; i++) {
+ assertEquals(i, assertNext(reader).index());
}
- writer.reset(i - 1);
+ writer.reset(cnt - 1);
assertNoNext(reader);
- assertEquals(i - 1, writer.append(new TestEntry(32)).index());
- assertEquals(i, writer.append(new TestEntry(32)).index());
+ assertEquals(cnt - 1, writer.append(new TestEntry(32)).index());
+ assertEquals(cnt, writer.append(new TestEntry(32)).index());
var entry = assertNext(reader);
- assertEquals(i - 1, entry.index());
+ assertEquals(cnt - 1, entry.index());
entry = assertNext(reader);
assertNotNull(entry);
- assertEquals(i, entry.index());
+ assertEquals(cnt, entry.index());
}
}
import io.atomix.storage.journal.JournalSerdes.EntrySerdes;
import java.io.IOException;
-@Deprecated(forRemoval = true, since="9.0.3")
+@Deprecated(forRemoval = true, since = "9.0.3")
final class ByteArraySerdes implements EntrySerdes<byte[]> {
@Override
public byte[] read(final EntryInput input) throws IOException {
*/
package io.atomix.storage.journal;
-import java.io.File;
-
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import java.io.File;
+import org.junit.jupiter.api.Test;
/**
* Journal segment file test.
*/
-public class JournalSegmentFileTest {
-
- @Test
- public void testIsSegmentFile() throws Exception {
- assertTrue(JournalSegmentFile.isSegmentFile("foo", "foo-1.log"));
- assertFalse(JournalSegmentFile.isSegmentFile("foo", "bar-1.log"));
- assertTrue(JournalSegmentFile.isSegmentFile("foo", "foo-1-1.log"));
- }
-
- @Test
- public void testCreateSegmentFile() throws Exception {
- File file = JournalSegmentFile.createSegmentFile("foo", new File(System.getProperty("user.dir")), 1);
- assertTrue(JournalSegmentFile.isSegmentFile("foo", file));
- }
+class JournalSegmentFileTest {
+ @Test
+ void testIsSegmentFile() {
+ assertTrue(JournalSegmentFile.isSegmentFile("foo", "foo-1.log"));
+ assertFalse(JournalSegmentFile.isSegmentFile("foo", "bar-1.log"));
+ assertTrue(JournalSegmentFile.isSegmentFile("foo", "foo-1-1.log"));
+ }
+ @Test
+ void testCreateSegmentFile() {
+ final var file = JournalSegmentFile.createSegmentFile("foo", new File(System.getProperty("user.dir")), 1);
+ assertTrue(JournalSegmentFile.isSegmentFile("foo", file));
+ }
}
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
public class TestEntry {
- private final byte[] bytes;
-
- public TestEntry(int size) {
- this(new byte[size]);
- }
-
- public TestEntry(byte[] bytes) {
- this.bytes = bytes;
- }
-
- public byte[] bytes() {
- return bytes;
- }
-
- @Override
- public boolean equals(Object obj) {
- return this == obj || obj instanceof TestEntry testEntry && Arrays.equals(bytes, testEntry.bytes);
- }
-
- @Override
- public int hashCode() {
- return Arrays.hashCode(bytes);
- }
-
- @Override
- public String toString() {
- return toStringHelper(this)
- .add("length", bytes.length)
- .add("hash", Arrays.hashCode(bytes))
- .toString();
- }
+ private final byte[] bytes;
+
+ public TestEntry(final int size) {
+ this(new byte[size]);
+ }
+
+ public TestEntry(final byte[] bytes) {
+ this.bytes = bytes;
+ }
+
+ public byte[] bytes() {
+ return bytes;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ return this == obj || obj instanceof TestEntry other && Arrays.equals(bytes, other.bytes);
+ }
+
+ @Override
+ public int hashCode() {
+ return Arrays.hashCode(bytes);
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this).add("length", bytes.length).add("hash", Arrays.hashCode(bytes)).toString();
+ }
}
import io.atomix.storage.journal.JournalSerdes.EntrySerdes;
import java.io.IOException;
-@Deprecated(forRemoval = true, since="9.0.3")
+@Deprecated(forRemoval = true, since = "9.0.3")
final class TestEntrySerdes implements EntrySerdes<TestEntry> {
private static final ByteArraySerdes BA_SERIALIZER = new ByteArraySerdes();
assertNull(sparseIndex.lookup(8));
}
- @Test
- void secondTest() {
+ @Test
+ void secondTest() {
assertNull(sparseIndex.lookup(100));
assertIndex(101, 2);
assertNull(sparseIndex.lookup(1));
*/
package io.atomix.utils.serializer;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.Assert.assertEquals;
+import org.junit.jupiter.api.Test;
-public class BufferAwareByteArrayOutputStreamTest {
-
- @Test
- public void testBufferSize() throws Exception {
- BufferAwareByteArrayOutputStream outputStream = new BufferAwareByteArrayOutputStream(8);
- assertEquals(8, outputStream.getBufferSize());
- outputStream.write(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
- assertEquals(8, outputStream.getBufferSize());
- outputStream.write(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
- assertEquals(16, outputStream.getBufferSize());
- outputStream.reset();
- assertEquals(16, outputStream.getBufferSize());
- }
+class BufferAwareByteArrayOutputStreamTest {
+ @Test
+ void testBufferSize() throws Exception {
+ final var outputStream = new BufferAwareByteArrayOutputStream(8);
+ assertEquals(8, outputStream.getBufferSize());
+ outputStream.write(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
+ assertEquals(8, outputStream.getBufferSize());
+ outputStream.write(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
+ assertEquals(16, outputStream.getBufferSize());
+ outputStream.reset();
+ assertEquals(16, outputStream.getBufferSize());
+ }
}
*/
package io.atomix.utils.serializer;
-import com.esotericsoftware.kryo.io.Input;
-import org.junit.Before;
-import org.junit.Test;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
-public class KryoInputPoolTest {
-
- private KryoInputPool kryoInputPool;
+import com.esotericsoftware.kryo.io.Input;
+import org.junit.jupiter.api.Test;
- @Before
- public void setUp() throws Exception {
- kryoInputPool = new KryoInputPool();
- }
+class KryoInputPoolTest {
+ private final KryoInputPool kryoInputPool = new KryoInputPool();
- @Test
- public void discardOutput() {
- final Input[] result = new Input[2];
- kryoInputPool.run(input -> {
- result[0] = input;
- return null;
- }, KryoInputPool.MAX_POOLED_BUFFER_SIZE + 1);
- kryoInputPool.run(input -> {
- result[1] = input;
- return null;
- }, 0);
- assertTrue(result[0] != result[1]);
- }
+ @Test
+ void discardOutput() {
+ final var result = new Input[2];
+ kryoInputPool.run(input -> {
+ result[0] = input;
+ return null;
+ }, KryoInputPool.MAX_POOLED_BUFFER_SIZE + 1);
+ kryoInputPool.run(input -> {
+ result[1] = input;
+ return null;
+ }, 0);
+ assertNotSame(result[0], result[1]);
+ }
- @Test
- public void recycleOutput() {
- final Input[] result = new Input[2];
- kryoInputPool.run(input -> {
- assertEquals(0, input.position());
- byte[] payload = new byte[]{1, 2, 3, 4};
- input.setBuffer(payload);
- assertArrayEquals(payload, input.readBytes(4));
- result[0] = input;
- return null;
- }, 0);
- assertNull(result[0].getInputStream());
- assertEquals(0, result[0].position());
- kryoInputPool.run(input -> {
- result[1] = input;
- return null;
- }, 0);
- assertTrue(result[0] == result[1]);
- }
+ @Test
+ void recycleOutput() {
+ final var result = new Input[2];
+ kryoInputPool.run(input -> {
+ assertEquals(0, input.position());
+ byte[] payload = new byte[]{1, 2, 3, 4};
+ input.setBuffer(payload);
+ assertArrayEquals(payload, input.readBytes(4));
+ result[0] = input;
+ return null;
+ }, 0);
+ assertNull(result[0].getInputStream());
+ assertEquals(0, result[0].position());
+ kryoInputPool.run(input -> {
+ result[1] = input;
+ return null;
+ }, 0);
+ assertSame(result[0], result[1]);
+ }
}
*/
package io.atomix.utils.serializer;
-import com.esotericsoftware.kryo.io.Output;
-import org.junit.Before;
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
-public class KryoOutputPoolTest {
-
- private KryoOutputPool kryoOutputPool;
+import com.esotericsoftware.kryo.io.Output;
+import org.junit.jupiter.api.Test;
- @Before
- public void setUp() throws Exception {
- kryoOutputPool = new KryoOutputPool();
- }
+class KryoOutputPoolTest {
+ private final KryoOutputPool kryoOutputPool = new KryoOutputPool();
- @Test
- public void discardOutput() {
- final Output[] result = new Output[2];
- kryoOutputPool.run(output -> {
- result[0] = output;
- return null;
- }, KryoOutputPool.MAX_POOLED_BUFFER_SIZE + 1);
- kryoOutputPool.run(output -> {
- result[1] = output;
- return null;
- }, 0);
- assertTrue(result[0] != result[1]);
- }
+ @Test
+ void discardOutput() {
+ final var result = new Output[2];
+ kryoOutputPool.run(output -> {
+ result[0] = output;
+ return null;
+ }, KryoOutputPool.MAX_POOLED_BUFFER_SIZE + 1);
+ kryoOutputPool.run(output -> {
+ result[1] = output;
+ return null;
+ }, 0);
+ assertNotSame(result[0], result[1]);
+ }
- @Test
- public void recycleOutput() {
- final ByteArrayOutput[] result = new ByteArrayOutput[2];
- kryoOutputPool.run(output -> {
- output.writeInt(1);
- assertEquals(Integer.BYTES, output.position());
- result[0] = output;
- return null;
- }, 0);
- assertEquals(0, result[0].position());
- assertEquals(0, result[0].getByteArrayOutputStream().size());
- kryoOutputPool.run(output -> {
- assertEquals(0, output.position());
- result[1] = output;
- return null;
- }, 0);
- assertTrue(result[0] == result[1]);
- }
+ @Test
+ void recycleOutput() {
+ final var result = new ByteArrayOutput[2];
+ kryoOutputPool.run(output -> {
+ output.writeInt(1);
+ assertEquals(Integer.BYTES, output.position());
+ result[0] = output;
+ return null;
+ }, 0);
+ assertEquals(0, result[0].position());
+ assertEquals(0, result[0].getByteArrayOutputStream().size());
+ kryoOutputPool.run(output -> {
+ assertEquals(0, output.position());
+ result[1] = output;
+ return null;
+ }, 0);
+ assertSame(result[0], result[1]);
+ }
}