iamaleksey commented on code in PR #2256:
URL: https://github.com/apache/cassandra/pull/2256#discussion_r1168883309


##########
src/java/org/apache/cassandra/journal/SyncedOffsets.java:
##########
@@ -0,0 +1,261 @@
+/*
+ * 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.cassandra.journal;
+
+import java.io.IOException;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.StandardOpenOption;
+import java.util.zip.CRC32;
+
+import com.google.common.primitives.Ints;
+
+import org.apache.cassandra.io.util.File;
+import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.io.util.RandomAccessReader;
+import org.apache.cassandra.utils.Closeable;
+import org.apache.cassandra.utils.Crc;
+import org.apache.cassandra.utils.SyncUtil;
+
+import static org.apache.cassandra.utils.FBUtilities.updateChecksumInt;
+
+/**
+ * Keeps track of fsynced limits of a data file. Enables us to treat invalid
+ * records that are known to have been fsynced to disk differently from those
+ * that aren't.
+ * <p/>
+ * On disk representation is a sequence of 2-int tuples of:
+ *      (synced offset, CRC32(tuple position in synced offsets file, synced 
offset))
+ */
+interface SyncedOffsets extends Closeable
+{
+    /**
+     * @return furthest known synced offset
+     */
+    int syncedOffset();
+
+    /**
+     * Record an offset as synced to disk.
+     *
+     * @param offset the offset into datafile, up to which contents have been 
fsynced (exclusive)
+     */
+    void mark(int offset);
+
+    @Override
+    default void close()
+    {
+    }
+
+    /**
+     * @return a mutable MMAP-backed synced offset tracker for a new {@link 
ActiveSegment}
+     */
+    static Active active(Descriptor descriptor, boolean syncOnMark)
+    {
+        return new Active(descriptor, syncOnMark);
+    }
+
+    /**
+     * Load an existing log of synced offsets from disk into an immutable 
instance.
+     */
+    static Static load(Descriptor descriptor)
+    {
+        return Static.load(descriptor);
+    }
+
+    /**
+     * @return a placeholder instance in case this component is missing
+     */
+    static Absent absent()
+    {
+        return Absent.INSTANCE;
+    }
+
+    /**
+     * Single-threaded, MMAP-backed list of synced offsets.
+     */
+    final class Active implements SyncedOffsets
+    {
+        private static final int INITIAL_CAPACITY = 4 << 10;
+
+        private final Descriptor descriptor;
+        private final boolean syncOnMark;
+
+        private final FileChannel channel;
+        private MappedByteBuffer buffer;
+
+        private volatile int syncedOffset;
+
+        private Active(Descriptor descriptor, boolean syncOnMark)
+        {
+            this.descriptor = descriptor;
+            this.syncOnMark = syncOnMark;
+
+            File file = descriptor.fileFor(Component.SYNCED_OFFSETS);
+            if (file.exists())
+                throw new IllegalArgumentException("Synced offsets file " + 
file + " already exists");
+
+            try
+            {
+                channel = FileChannel.open(file.toPath(), 
StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);

Review Comment:
   Resolved now. Am aware of the two other cases.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to