You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ratis.apache.org by GitBox <gi...@apache.org> on 2019/06/17 18:54:43 UTC

[GitHub] [incubator-ratis] joshelser commented on a change in pull request #25: RATIS-588 LogStream StateMachine export

joshelser commented on a change in pull request #25: RATIS-588 LogStream StateMachine export
URL: https://github.com/apache/incubator-ratis/pull/25#discussion_r294368193
 
 

 ##########
 File path: ratis-logservice/src/main/java/org/apache/ratis/logservice/impl/ArchiveHdfsLogWriter.java
 ##########
 @@ -0,0 +1,91 @@
+/**
+ * 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.ratis.logservice.impl;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FSDataOutputStream;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.ratis.logservice.api.ArchiveLogWriter;
+import org.apache.ratis.logservice.api.LogName;
+import org.apache.ratis.logservice.util.LogServiceUtils;
+
+public class ArchiveHdfsLogWriter implements ArchiveLogWriter {
+  private FileSystem hdfs;
+  private FSDataOutputStream os;
+  private Path currentPath;
+  private long currentRecordId;
+  private long lastRollRecordId;
+
+  @Override
+  public void init(String location, LogName logName) throws IOException {
+    Configuration configuration = new Configuration();
+    hdfs = FileSystem.get(configuration);
+    currentPath = new Path(location + "/" + logName.getName());
+    os = hdfs.create(currentPath,true);
+  }
+
+  @Override public long write(ByteBuffer data) throws IOException {
+    os.writeInt(data.array().length);
+    os.write(data.array());
+    currentRecordId++;
+    return currentRecordId;
+  }
+
+  @Override public List<Long> write(List<ByteBuffer> records) throws IOException {
+    List<Long> list = new ArrayList<Long>();
+    for (ByteBuffer record : records) {
+      list.add(write(record));
+    }
+    return list;
+  }
+
+  @Override public long sync() throws IOException {
+    return 0;
+  }
+
+  @Override public void close() throws IOException {
+    os.close();
+    renameIfNewData();
+    hdfs.close();
+  }
+
+  @Override public void rollWriter() throws IOException {
+    //close old file
+    os.close();
+    renameIfNewData();
+    lastRollRecordId = currentRecordId;
+    //create new file
+    os = hdfs.create(currentPath, true);
+  }
+
+  @Override public long getLastWrittenRecordId() {
+    return currentRecordId;
 
 Review comment:
   Just a thought -- until we call `close()` on the outputstream, this won't be accurate for multi-threaded invocations. Same sort of problem we have in WALs in HBase.
   
   As long as we aren't trying to read from a Log from HDFS while it's in the ARCHIVING state, I don't think this is an issue. If we wanted to do that, I think we'd have to call `hflush` to make this "honest".

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services