2 * Copyright 2015-2022 Open Networking Foundation and others. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.atomix.storage.journal;
18 import com.google.common.annotations.VisibleForTesting;
19 import java.io.IOException;
20 import java.nio.ByteBuffer;
21 import java.nio.channels.ReadableByteChannel;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.Nullable;
26 * Stores information about a {@link JournalSegment} of the log.
28 * The segment descriptor manages metadata related to a single segment of the log. Descriptors are stored within the
29 * first {@code 64} bytes of each segment in the following order:
31 * <li>{@code id} (64-bit signed integer) - A unique segment identifier. This is a monotonically increasing number
32 * within each log. Segments with in-sequence identifiers should contain in-sequence indexes.</li>
33 * <li>{@code index} (64-bit signed integer) - The effective first index of the segment. This indicates the index at
34 * which the first entry should be written to the segment. Indexes are monotonically increasing thereafter.</li>
35 * <li>{@code version} (64-bit signed integer) - The version of the segment. Versions are monotonically increasing
36 * starting at {@code 1}. Versions will only be incremented whenever the segment is rewritten to another
37 * memory/disk space, e.g. after log compaction.</li>
38 * <li>{@code maxSegmentSize} (32-bit unsigned integer) - The maximum number of bytes allowed in the segment.</li>
39 * <li>{@code maxEntries} (32-bit signed integer) - The total number of expected entries in the segment. This is the
40 * final number of entries allowed within the segment both before and after compaction. This entry count is used
41 * to determine the count of internal indexing and deduplication facilities.</li>
42 * <li>{@code updated} (64-bit signed integer) - The last update to the segment in terms of milliseconds since the
44 * When the segment is first constructed, the {@code updated} time is {@code 0}. Once all entries in the segment
45 * have been committed, the {@code updated} time should be set to the current time. Log compaction should not
46 * result in a change to {@code updated}.</li>
47 * <li>{@code locked} (8-bit boolean) - A boolean indicating whether the segment is locked. Segments will be locked
48 * once all entries have been committed to the segment. The lock state of each segment is used to determine log
49 * compaction and recovery behavior.</li>
51 * The remainder of the 64 segment header bytes are reserved for future metadata.
53 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
55 public record JournalSegmentDescriptor(
63 public static final int BYTES = 64;
65 // Current segment version.
67 static final int VERSION = 1;
70 * Read a JournalSegmentDescriptor from a {@link ReadableByteChannel}.
72 * @param channel channel to read from
73 * @return A {@link JournalSegmentDescriptor}
74 * @throws IOException if an I/O error occurs or there is not enough data
76 public static @NonNull JournalSegmentDescriptor readFrom(final ReadableByteChannel channel) throws IOException {
77 final var buffer = ByteBuffer.allocate(BYTES);
78 final var read = channel.read(buffer);
80 throw new IOException("Need " + BYTES + " bytes, only " + read + " available");
84 return new JournalSegmentDescriptor(
95 * Returns the segment version.
97 * Versions are monotonically increasing starting at {@code 1}.
99 * @return The segment version.
101 public int version() {
106 * Returns the segment identifier.
108 * The segment ID is a monotonically increasing number within each log. Segments with in-sequence identifiers should
109 * contain in-sequence indexes.
111 * @return The segment identifier.
118 * Returns the segment index.
120 * The index indicates the index at which the first entry should be written to the segment. Indexes are monotonically
121 * increasing thereafter.
123 * @return The segment index.
125 public long index() {
130 * Returns the maximum count of the segment.
132 * @return The maximum allowed count of the segment.
134 public int maxSegmentSize() {
135 return maxSegmentSize;
139 * Returns the maximum number of entries allowed in the segment.
141 * @return The maximum number of entries allowed in the segment.
143 public int maxEntries() {
148 * Returns last time the segment was updated.
150 * When the segment is first constructed, the {@code updated} time is {@code 0}. Once all entries in the segment have
151 * been committed, the {@code updated} time should be set to the current time. Log compaction should not result in a
152 * change to {@code updated}.
154 * @return The last time the segment was updated in terms of milliseconds since the epoch.
156 public long updated() {
161 * Returns this segment as an array of bytes
165 byte @NonNull [] toArray() {
166 final var bytes = new byte[BYTES];
167 ByteBuffer.wrap(bytes)
171 .putInt(maxSegmentSize)
174 .put(locked ? (byte) 1 : (byte) 0);
179 * Returns a descriptor builder.
181 * The descriptor builder will write segment metadata to a {@code 48} byte in-memory buffer.
183 * @return The descriptor builder.
185 public static Builder builder() {
186 return builder(VERSION);
190 * Returns a descriptor builder for the given descriptor buffer.
192 * @param version version to build
193 * @return The descriptor builder.
194 * @throws NullPointerException if {@code buffer} is null
196 public static Builder builder(final int version) {
197 return new Builder(version);
201 * Segment descriptor builder.
203 public static final class Builder {
204 private final int version;
208 private Integer maxSegmentSize;
209 private Integer maxEntries;
210 private Long updated;
212 Builder(final int version) {
213 this.version = version;
217 * Sets the segment identifier.
219 * @param id The segment identifier.
220 * @return The segment descriptor builder.
222 public Builder withId(final long id) {
228 * Sets the segment index.
230 * @param index The segment starting index.
231 * @return The segment descriptor builder.
233 public Builder withIndex(final long index) {
239 * Sets maximum count of the segment.
241 * @param maxSegmentSize The maximum count of the segment.
242 * @return The segment descriptor builder.
244 public Builder withMaxSegmentSize(final int maxSegmentSize) {
245 this.maxSegmentSize = maxSegmentSize;
250 * Sets the maximum number of entries in the segment.
252 * @param maxEntries The maximum number of entries in the segment.
253 * @return The segment descriptor builder.
254 * @deprecated since 3.0.2
257 public Builder withMaxEntries(final int maxEntries) {
258 this.maxEntries = maxEntries;
263 * Sets updated timestamp;
265 * @param updated Epoch milliseconds
266 * @return The segment descriptor builder.
268 public Builder withUpdated(final long updated) {
269 this.updated = updated;
274 * Builds the segment descriptor.
276 * @return The built segment descriptor.
278 public JournalSegmentDescriptor build() {
279 return new JournalSegmentDescriptor(version,
281 checkSet(index, "index"),
282 checkSet(maxSegmentSize, "maxSegmentSize"),
283 checkSet(maxEntries, "maxEntries"),
284 checkSet(updated, "updated"),
288 private static <T> @NonNull T checkSet(final @Nullable T obj, final String name) {
292 throw new IllegalArgumentException(name + " not set");