2 * Copyright 2017-2022 Open Networking Foundation and others. All rights reserved.
3 * Copyright (c) 2024 PANTHEON.tech, s.r.o.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package io.atomix.storage.journal;
19 import com.google.common.base.MoreObjects;
20 import io.atomix.storage.journal.index.JournalIndex;
21 import io.atomix.storage.journal.index.SparseJournalIndex;
22 import java.io.IOException;
23 import java.nio.channels.FileChannel;
24 import java.nio.file.Files;
25 import java.nio.file.StandardOpenOption;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.atomic.AtomicInteger;
33 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
35 final class JournalSegment<E> implements AutoCloseable {
36 private final JournalSegmentFile file;
37 private final JournalSegmentDescriptor descriptor;
38 private final StorageLevel storageLevel;
39 private final int maxEntrySize;
40 private final JournalIndex index;
41 private final JournalSerdes namespace;
42 private final Set<JournalSegmentReader<E>> readers = ConcurrentHashMap.newKeySet();
43 private final AtomicInteger references = new AtomicInteger();
44 private final FileChannel channel;
46 private JournalSegmentWriter<E> writer;
47 private boolean open = true;
50 JournalSegmentFile file,
51 JournalSegmentDescriptor descriptor,
52 StorageLevel storageLevel,
55 JournalSerdes namespace) {
57 this.descriptor = descriptor;
58 this.storageLevel = storageLevel;
59 this.maxEntrySize = maxEntrySize;
60 this.namespace = namespace;
61 index = new SparseJournalIndex(indexDensity);
63 channel = FileChannel.open(file.file().toPath(),
64 StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
65 } catch (IOException e) {
66 throw new StorageException(e);
68 writer = switch (storageLevel) {
69 case DISK -> new DiskJournalSegmentWriter<>(channel, this, maxEntrySize, index, namespace);
70 case MAPPED -> new MappedJournalSegmentWriter<>(channel, this, maxEntrySize, index, namespace).toFileChannel();
75 * Returns the segment's starting index.
77 * @return The segment's starting index.
80 return descriptor.index();
84 * Returns the last index in the segment.
86 * @return The last index in the segment.
89 return writer.getLastIndex();
93 * Returns the size of the segment.
95 * @return the size of the segment
99 return (int) channel.size();
100 } catch (IOException e) {
101 throw new StorageException(e);
106 * Returns the segment file.
108 * @return The segment file.
110 JournalSegmentFile file() {
115 * Returns the segment descriptor.
117 * @return The segment descriptor.
119 JournalSegmentDescriptor descriptor() {
124 * Acquires a reference to the log segment.
126 private void acquire() {
127 if (references.getAndIncrement() == 0 && storageLevel == StorageLevel.MAPPED) {
128 writer = writer.toMapped();
133 * Releases a reference to the log segment.
135 private void release() {
136 if (references.decrementAndGet() == 0) {
137 if (storageLevel == StorageLevel.MAPPED) {
138 writer = writer.toFileChannel();
147 * Acquires a reference to the segment writer.
149 * @return The segment writer.
151 JournalSegmentWriter<E> acquireWriter() {
159 * Releases the reference to the segment writer.
161 void releaseWriter() {
166 * Creates a new segment reader.
168 * @return A new segment reader.
170 JournalSegmentReader<E> createReader() {
174 final var buffer = writer.buffer();
175 final var reader = buffer == null
176 ? new DiskJournalSegmentReader<>(channel, this, maxEntrySize, index, namespace)
177 : new MappedJournalSegmentReader<>(buffer, this, maxEntrySize, index, namespace);
183 * Closes a segment reader.
185 * @param reader the closed segment reader
187 void closeReader(JournalSegmentReader<E> reader) {
188 if (readers.remove(reader)) {
194 * Checks whether the segment is open.
196 private void checkOpen() {
198 throw new IllegalStateException("Segment not open");
203 * Returns a boolean indicating whether the segment is open.
205 * @return indicates whether the segment is open
207 public boolean isOpen() {
212 * Closes the segment.
215 public void close() {
221 readers.forEach(JournalSegmentReader::close);
222 if (references.get() == 0) {
227 private void finishClose() {
231 } catch (IOException e) {
232 throw new StorageException(e);
237 * Deletes the segment.
241 Files.deleteIfExists(file.file().toPath());
242 } catch (IOException e) {
243 throw new StorageException(e);
248 public String toString() {
249 return MoreObjects.toStringHelper(this)
250 .add("id", descriptor.id())
251 .add("version", descriptor.version())
252 .add("index", index())