sashapolo commented on code in PR #6577:
URL: https://github.com/apache/ignite-3/pull/6577#discussion_r2341341768


##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/storage/segstore/SegmentFileManager.java:
##########
@@ -0,0 +1,224 @@
+/*
+ * 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.ignite.internal.raft.storage.segstore;
+
+import static org.apache.ignite.lang.ErrorGroups.Common.NODE_STOPPING_ERR;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.file.Path;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.lang.IgniteInternalException;
+import 
org.apache.ignite.internal.raft.storage.segstore.SegmentFile.WriteBuffer;
+
+/**
+ * File manager responsible for allocating and maintaining a pointer to the 
current segment file.
+ *
+ * <p>When the current segment file becomes full, that is, it does not contain 
enough bytes left to satisfy a request by one of the writer
+ * threads, then a new segment file is allocated and is atomically switched to 
be the current one. This operation is called rollover.
+ *
+ * <p>Every segment file has the following structure:
+ * <pre>
+ * +------------------+---------+-----+---------+
+ * | Header (8 bytes) | Payload | ... | Payload |
+ * +------------------+---------+-----+---------+
+ * </pre>
+ *
+ * <p>Header structure is the following:
+ * <pre>
+ * +------------------------+-------------------+
+ * | Magic number (4 bytes) | Version (4 bytes) |
+ * +------------------------+-------------------+
+ * </pre>
+ *
+ * <p>Payload structure is defined by the outer callers.
+ *
+ * <p>When a rollover happens and the segment file being replaced has at least 
8 bytes left, a special {@link #SWITCH_SEGMENT_RECORD} is
+ * written at the end of the file. If there are less than 8 bytes left, no 
switch records are written.
+ */
+class SegmentFileManager implements ManuallyCloseable {
+    private static final int WAIT_TIMEOUT_MS = 30_000;
+
+    private static final int MAGIC_NUMBER = 0xFEEDFACE;
+
+    private static final int FORMAT_VERSION = 1;
+
+    static final byte[] HEADER_RECORD = ByteBuffer.allocate(Integer.BYTES + 
Integer.BYTES)
+            .order(SegmentFile.BYTE_ORDER)
+            .putInt(MAGIC_NUMBER)
+            .putInt(FORMAT_VERSION)
+            .array();
+
+    static final byte[] SWITCH_SEGMENT_RECORD = new byte[8];
+
+    private final Path baseDir;
+
+    /** Configured size of a segment file. */
+    private final long fileSize;
+
+    /**
+     * Current segment file. Can store {@code null} while a rollover is in 
progress.
+     */
+    private final AtomicReference<SegmentFile> currentSegmentFile = new 
AtomicReference<>();
+
+    /** Lock used to block threads while a rollover is in progress. */
+    private final Object rolloverLock = new Object();
+
+    /**
+     * Current segment file index (used to generate segment file names).
+     *
+     * <p>Must always be accessed under the {@link #rolloverLock}.
+     */
+    private int curFileIndex;
+
+    /**
+     * Flag indicating whether the file manager has been stopped.
+     *
+     * <p>Must always be accessed under the {@link #rolloverLock}.
+     */
+    private boolean isStopped;
+
+    SegmentFileManager(Path baseDir, long fileSize) {
+        this.baseDir = baseDir;
+        this.fileSize = fileSize;
+    }
+
+    void start() throws IOException {
+        // TODO: implement recovery, see 
https://issues.apache.org/jira/browse/IGNITE-26283.
+        currentSegmentFile.set(allocateNewSegmentFile(0));
+    }
+
+    private SegmentFile allocateNewSegmentFile(int fileIndex) throws 
IOException {
+        Path path = baseDir.resolve(segmentFileName(fileIndex, 0));
+
+        var segmentFile = new SegmentFile(path, fileSize, 0);
+
+        writeHeader(segmentFile);
+
+        return segmentFile;
+    }
+
+    private static String segmentFileName(int fileIndex, int generation) {
+        return String.format("segment-%010d-%010d.bin", fileIndex, generation);

Review Comment:
   yes, but integers require up to 10 positions, I don't know why it is 8 in 
the ticket...



-- 
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]

Reply via email to