SincereXIA commented on a change in pull request #616:
URL: https://github.com/apache/ratis/pull/616#discussion_r821450785



##########
File path: 
ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogOutputStream.java
##########
@@ -67,7 +67,9 @@ public SegmentedRaftLogOutputStream(File file, boolean 
append, long segmentMaxSi
       // write header
       preallocateIfNecessary(SegmentedRaftLogFormat.getHeaderLength());
       
SegmentedRaftLogFormat.applyHeaderTo(CheckedConsumer.asCheckedFunction(out::write));
-      out.flush();
+      synchronized (out) {
+        out.flush();

Review comment:
       We found errors like this during testing:
   
   ```
   2022-03-08 11:15:23,856 
[97cb3f28-f298-4ed3-93c7-3be44bfe8b7b@group-892C7CF01088-SegmentedRaftLogWorker]
 ERROR org.apache.ratis.server.raftlog.segmented.SegmentedRaftLogWorker: 
97cb3f28-f298-4ed3-93c7-3be44bfe8b7b@group-892C7CF01088-SegmentedRaftLogWorker 
hit exception
   java.lang.IllegalArgumentException
           at java.nio.Buffer.position(Buffer.java:244)
           at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:377)
           at 
org.apache.ratis.server.raftlog.segmented.BufferedWriteChannel.write(BufferedWriteChannel.java:61)
           at 
org.apache.ratis.server.raftlog.segmented.SegmentedRaftLogOutputStream.write(SegmentedRaftLogOutputStream.java:100)
           at 
org.apache.ratis.server.raftlog.segmented.SegmentedRaftLogWorker$WriteLog.execute(SegmentedRaftLogWorker.java:610)
           at 
org.apache.ratis.server.raftlog.segmented.SegmentedRaftLogWorker.run(SegmentedRaftLogWorker.java:404)
           at java.lang.Thread.run(Thread.java:748)
   2022-03-08 11:15:23,858 
[97cb3f28-f298-4ed3-93c7-3be44bfe8b7b@group-892C7CF01088-SegmentedRaftLogWorker]
 INFO org.apache.ratis.server.RaftServer$Division: 
97cb3f28-f298-4ed3-93c7-3be44bfe8b7b@group-892C7CF01088: shutdown
   ```
   I think the cause of this error is that class `BufferedWriteChannel` is not 
thread safe, but an asynchronous flush may operate on this object at the same 
time as the main thread.
   
   ```
   /**
    * Provides a buffering layer in front of a FileChannel for writing.
    *
    * This class is NOT threadsafe.
    */
   class BufferedWriteChannel implements Closeable {
   ````
   
   After we make it synchronized, the error no longer occurs.




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