Maxwell-Guo commented on code in PR #2777:
URL: https://github.com/apache/cassandra/pull/2777#discussion_r1349438815


##########
src/java/org/apache/cassandra/config/DatabaseDescriptor.java:
##########
@@ -2649,6 +2649,11 @@ public static void setCommitLogSegmentSize(int 
sizeMebibytes)
         conf.commitlog_segment_size = new 
DataStorageSpec.IntMebibytesBound(sizeMebibytes);
     }
 
+    public static boolean getDirectIOStatus()

Review Comment:
   I think we can rename the function name to enableDirectIOForCommitlog 



##########
src/java/org/apache/cassandra/db/commitlog/CommitLogSegment.java:
##########
@@ -175,7 +177,10 @@ static long getNextId()
 
         try
         {
-            channel = FileChannel.open(logFile.toPath(), 
StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);
+            if(commitLog.configuration.isDirectIOEnabled())
+                channel = FileChannel.open(logFile.toPath(), 
StandardOpenOption.WRITE, StandardOpenOption.READ, 
StandardOpenOption.CREATE,ExtendedOpenOption.DIRECT);

Review Comment:
   `channel = FileChannel.open(logFile.toPath(), StandardOpenOption.WRITE, 
StandardOpenOption.READ, StandardOpenOption.CREATE, ExtendedOpenOption.DIRECT);`



##########
src/java/org/apache/cassandra/db/commitlog/DirectIOSegment.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.db.commitlog;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.file.Files;
+
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.io.FSWriteError;
+import org.apache.cassandra.io.util.FileUtils;
+
+/*
+ * Direct-IO segment. Allocates ByteBuffer using ByteBuffer.allocateDirect and 
align
+ * ByteBuffer.position, ByteBuffer.limit and FileChannel.position to page size 
(4K).
+ * Java-11 forces minimum page size to be written to disk with Direct-IO.
+ */
+public class DirectIOSegment extends CommitLogSegment
+{
+    ByteBuffer original;
+    static int minimumAllowedAlign;
+
+    /**
+     * Constructs a new segment file.
+     *
+     * @param commitLog the commit log it will be used with.
+     */
+    DirectIOSegment(CommitLog commitLog, AbstractCommitLogSegmentManager 
manager)
+    {
+        super(commitLog, manager);
+
+        // mark the initial sync marker as uninitialised
+        int firstSync = buffer.position();
+        buffer.putInt(firstSync + 0, 0);
+        buffer.putInt(firstSync + 4, 0);
+
+        // Testing shows writing initial bytes takes some time. During peak 
load, it helps
+        // lot and Syncer thread can actually do flush activity. Making this 
initial
+        // slow operation to be executed by Allocator thread here.
+        int oldLastSyncedOffset=lastSyncedOffset;
+        // 8 bytes are written above.
+        lastSyncedOffset=8;

Review Comment:
    lastSyncedOffset = 8;



##########
src/java/org/apache/cassandra/db/commitlog/CommitLogSegment.java:
##########
@@ -175,7 +177,10 @@ static long getNextId()
 
         try
         {
-            channel = FileChannel.open(logFile.toPath(), 
StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);
+            if(commitLog.configuration.isDirectIOEnabled())
+                channel = FileChannel.open(logFile.toPath(), 
StandardOpenOption.WRITE, StandardOpenOption.READ, 
StandardOpenOption.CREATE,ExtendedOpenOption.DIRECT);

Review Comment:
   miss a space before ExtendedOpenOption.DIRECT



##########
src/java/org/apache/cassandra/db/commitlog/CommitLog.java:
##########
@@ -129,6 +129,10 @@ private static CommitLog construct()
 
         // register metrics
         metrics.attach(executor, segmentManager);
+
+       if (configuration.isDirectIOEnabled()) {

Review Comment:
   I think we may do not need this logging ~~~ or just  logger.info("Use 
direcrtor io for commit log " +  configuration.isDirectIOEnabled())



##########
src/java/org/apache/cassandra/db/commitlog/CommitLogSegment.java:
##########
@@ -138,7 +139,8 @@ static CommitLogSegment createSegment(CommitLog commitLog, 
AbstractCommitLogSegm
         Configuration config = commitLog.configuration;
         CommitLogSegment segment = config.useEncryption() ? new 
EncryptedSegment(commitLog, manager)
                                                           : 
config.useCompression() ? new CompressedSegment(commitLog, manager)
-                                                                               
     : new MemoryMappedSegment(commitLog, manager);
+                                                                               
     : ! config.isDirectIOEnabled() ? new MemoryMappedSegment(commitLog, 
manager)

Review Comment:
   what about : config.isDirectIOEnabled() ? new DirectIOSegment(commitLog, 
manager) : new MemoryMappedSegment(commitLog, manager);
                                                                                
                                      



##########
src/java/org/apache/cassandra/db/commitlog/DirectIOSegment.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.db.commitlog;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.file.Files;
+
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.io.FSWriteError;
+import org.apache.cassandra.io.util.FileUtils;
+
+/*
+ * Direct-IO segment. Allocates ByteBuffer using ByteBuffer.allocateDirect and 
align
+ * ByteBuffer.position, ByteBuffer.limit and FileChannel.position to page size 
(4K).
+ * Java-11 forces minimum page size to be written to disk with Direct-IO.
+ */
+public class DirectIOSegment extends CommitLogSegment
+{
+    ByteBuffer original;
+    static int minimumAllowedAlign;
+
+    /**
+     * Constructs a new segment file.
+     *
+     * @param commitLog the commit log it will be used with.
+     */
+    DirectIOSegment(CommitLog commitLog, AbstractCommitLogSegmentManager 
manager)
+    {
+        super(commitLog, manager);
+
+        // mark the initial sync marker as uninitialised
+        int firstSync = buffer.position();
+        buffer.putInt(firstSync + 0, 0);
+        buffer.putInt(firstSync + 4, 0);
+
+        // Testing shows writing initial bytes takes some time. During peak 
load, it helps
+        // lot and Syncer thread can actually do flush activity. Making this 
initial
+        // slow operation to be executed by Allocator thread here.
+        int oldLastSyncedOffset=lastSyncedOffset;
+        // 8 bytes are written above.
+        lastSyncedOffset=8;
+        flush(0,lastSyncedOffset);

Review Comment:
      flush(0, lastSyncedOffset);



##########
src/java/org/apache/cassandra/db/commitlog/DirectIOSegment.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.db.commitlog;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.file.Files;
+
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.io.FSWriteError;
+import org.apache.cassandra.io.util.FileUtils;
+
+/*
+ * Direct-IO segment. Allocates ByteBuffer using ByteBuffer.allocateDirect and 
align
+ * ByteBuffer.position, ByteBuffer.limit and FileChannel.position to page size 
(4K).
+ * Java-11 forces minimum page size to be written to disk with Direct-IO.
+ */
+public class DirectIOSegment extends CommitLogSegment
+{
+    ByteBuffer original;
+    static int minimumAllowedAlign;
+
+    /**
+     * Constructs a new segment file.
+     *
+     * @param commitLog the commit log it will be used with.
+     */
+    DirectIOSegment(CommitLog commitLog, AbstractCommitLogSegmentManager 
manager)
+    {
+        super(commitLog, manager);
+
+        // mark the initial sync marker as uninitialised
+        int firstSync = buffer.position();
+        buffer.putInt(firstSync + 0, 0);
+        buffer.putInt(firstSync + 4, 0);
+
+        // Testing shows writing initial bytes takes some time. During peak 
load, it helps
+        // lot and Syncer thread can actually do flush activity. Making this 
initial
+        // slow operation to be executed by Allocator thread here.
+        int oldLastSyncedOffset=lastSyncedOffset;
+        // 8 bytes are written above.
+        lastSyncedOffset=8;
+        flush(0,lastSyncedOffset);
+        lastSyncedOffset = oldLastSyncedOffset;
+    }
+
+    ByteBuffer createBuffer(CommitLog commitLog)
+    {
+        if (minimumAllowedAlign ==0)
+        {
+            try
+            {
+                minimumAllowedAlign = 
(int)Files.getFileStore(logFile.toPath()).getBlockSize();
+            }
+            catch (IOException e)
+            {
+                throw new FSWriteError(e, logFile);
+            }
+        }
+
+        int cl_size = DatabaseDescriptor.getCommitLogSegmentSize();
+        original = ByteBuffer.allocateDirect(cl_size + minimumAllowedAlign);
+
+        ByteBuffer alignedBuffer = original.alignedSlice(minimumAllowedAlign);
+        assert alignedBuffer.limit() >= cl_size : String.format("Bytebuffer 
slicing failed to get required buffer size (required=%d,current size=%d", 
cl_size, alignedBuffer.limit());
+
+        assert alignedBuffer.alignmentOffset(0, minimumAllowedAlign) ==0 : 
"Index 0 should be aligned to 4K page size";

Review Comment:
   minimumAllowedAlign) == 0 :



##########
src/java/org/apache/cassandra/db/commitlog/CommitLog.java:
##########
@@ -609,6 +613,7 @@ private static String 
addAdditionalInformationIfPossible(String msg)
 
     public static final class Configuration
     {
+        private final boolean useDirectIO;

Review Comment:
   Add comment like : the flag to show wether use dir io for commitlog 



##########
src/java/org/apache/cassandra/db/commitlog/DirectIOSegment.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.db.commitlog;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.file.Files;
+
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.io.FSWriteError;
+import org.apache.cassandra.io.util.FileUtils;
+
+/*
+ * Direct-IO segment. Allocates ByteBuffer using ByteBuffer.allocateDirect and 
align
+ * ByteBuffer.position, ByteBuffer.limit and FileChannel.position to page size 
(4K).
+ * Java-11 forces minimum page size to be written to disk with Direct-IO.
+ */
+public class DirectIOSegment extends CommitLogSegment
+{
+    ByteBuffer original;
+    static int minimumAllowedAlign;
+
+    /**
+     * Constructs a new segment file.
+     *
+     * @param commitLog the commit log it will be used with.
+     */
+    DirectIOSegment(CommitLog commitLog, AbstractCommitLogSegmentManager 
manager)
+    {
+        super(commitLog, manager);
+
+        // mark the initial sync marker as uninitialised
+        int firstSync = buffer.position();
+        buffer.putInt(firstSync + 0, 0);
+        buffer.putInt(firstSync + 4, 0);
+
+        // Testing shows writing initial bytes takes some time. During peak 
load, it helps
+        // lot and Syncer thread can actually do flush activity. Making this 
initial
+        // slow operation to be executed by Allocator thread here.
+        int oldLastSyncedOffset=lastSyncedOffset;
+        // 8 bytes are written above.
+        lastSyncedOffset=8;
+        flush(0,lastSyncedOffset);
+        lastSyncedOffset = oldLastSyncedOffset;
+    }
+
+    ByteBuffer createBuffer(CommitLog commitLog)
+    {
+        if (minimumAllowedAlign ==0)

Review Comment:
   minimumAllowedAlign == 0



##########
src/java/org/apache/cassandra/db/commitlog/DirectIOSegment.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.db.commitlog;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.file.Files;
+
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.io.FSWriteError;
+import org.apache.cassandra.io.util.FileUtils;
+
+/*
+ * Direct-IO segment. Allocates ByteBuffer using ByteBuffer.allocateDirect and 
align
+ * ByteBuffer.position, ByteBuffer.limit and FileChannel.position to page size 
(4K).
+ * Java-11 forces minimum page size to be written to disk with Direct-IO.
+ */
+public class DirectIOSegment extends CommitLogSegment
+{
+    ByteBuffer original;
+    static int minimumAllowedAlign;
+
+    /**
+     * Constructs a new segment file.
+     *
+     * @param commitLog the commit log it will be used with.
+     */
+    DirectIOSegment(CommitLog commitLog, AbstractCommitLogSegmentManager 
manager)
+    {
+        super(commitLog, manager);
+
+        // mark the initial sync marker as uninitialised
+        int firstSync = buffer.position();
+        buffer.putInt(firstSync + 0, 0);
+        buffer.putInt(firstSync + 4, 0);
+
+        // Testing shows writing initial bytes takes some time. During peak 
load, it helps
+        // lot and Syncer thread can actually do flush activity. Making this 
initial
+        // slow operation to be executed by Allocator thread here.
+        int oldLastSyncedOffset=lastSyncedOffset;

Review Comment:
   int oldLastSyncedOffset = lastSyncedOffset;



##########
src/java/org/apache/cassandra/db/commitlog/DirectIOSegment.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.db.commitlog;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.file.Files;
+
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.io.FSWriteError;
+import org.apache.cassandra.io.util.FileUtils;
+
+/*
+ * Direct-IO segment. Allocates ByteBuffer using ByteBuffer.allocateDirect and 
align
+ * ByteBuffer.position, ByteBuffer.limit and FileChannel.position to page size 
(4K).
+ * Java-11 forces minimum page size to be written to disk with Direct-IO.
+ */
+public class DirectIOSegment extends CommitLogSegment
+{
+    ByteBuffer original;
+    static int minimumAllowedAlign;
+
+    /**
+     * Constructs a new segment file.
+     *
+     * @param commitLog the commit log it will be used with.
+     */
+    DirectIOSegment(CommitLog commitLog, AbstractCommitLogSegmentManager 
manager)
+    {
+        super(commitLog, manager);
+
+        // mark the initial sync marker as uninitialised
+        int firstSync = buffer.position();
+        buffer.putInt(firstSync + 0, 0);
+        buffer.putInt(firstSync + 4, 0);
+
+        // Testing shows writing initial bytes takes some time. During peak 
load, it helps
+        // lot and Syncer thread can actually do flush activity. Making this 
initial
+        // slow operation to be executed by Allocator thread here.
+        int oldLastSyncedOffset=lastSyncedOffset;
+        // 8 bytes are written above.
+        lastSyncedOffset=8;
+        flush(0,lastSyncedOffset);
+        lastSyncedOffset = oldLastSyncedOffset;
+    }
+
+    ByteBuffer createBuffer(CommitLog commitLog)
+    {
+        if (minimumAllowedAlign ==0)
+        {
+            try
+            {
+                minimumAllowedAlign = 
(int)Files.getFileStore(logFile.toPath()).getBlockSize();
+            }
+            catch (IOException e)
+            {
+                throw new FSWriteError(e, logFile);
+            }
+        }
+
+        int cl_size = DatabaseDescriptor.getCommitLogSegmentSize();
+        original = ByteBuffer.allocateDirect(cl_size + minimumAllowedAlign);
+
+        ByteBuffer alignedBuffer = original.alignedSlice(minimumAllowedAlign);
+        assert alignedBuffer.limit() >= cl_size : String.format("Bytebuffer 
slicing failed to get required buffer size (required=%d,current size=%d", 
cl_size, alignedBuffer.limit());
+
+        assert alignedBuffer.alignmentOffset(0, minimumAllowedAlign) ==0 : 
"Index 0 should be aligned to 4K page size";
+        assert alignedBuffer.alignmentOffset(alignedBuffer.limit(), 
minimumAllowedAlign) ==0 : "Limit should be aligned to 4K page size" ;

Review Comment:
   minimumAllowedAlign) == 0 :



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