jvrao commented on a change in pull request #266: Issue 265: Add persistable 
bookie status
URL: https://github.com/apache/bookkeeper/pull/266#discussion_r140661558
 
 

 ##########
 File path: 
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieStatus.java
 ##########
 @@ -0,0 +1,234 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.bookkeeper.bookie;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static com.google.common.base.Charsets.UTF_8;
+
+import static 
org.apache.bookkeeper.util.BookKeeperConstants.BOOKIE_STATUS_FILENAME;
+
+public class BookieStatus {
+    static Logger LOG = LoggerFactory.getLogger(BookieStatus.class);
+    static final int CURRENT_STATUS_LAYOUT_VERSION = 1;
+
+    enum BookieMode {
+        READ_ONLY,
+        READ_WRITE;
+    }
+
+    private final static long INVALID_UPDATE_TIME = -1;
+
+    private int layoutVersion;
+    private long lastUpdateTime;
+    private BookieMode bookieMode;
+
+
+    BookieStatus() {
+        this.bookieMode = BookieMode.READ_WRITE;
+        this.layoutVersion = CURRENT_STATUS_LAYOUT_VERSION;
+        this.lastUpdateTime = INVALID_UPDATE_TIME;
+    }
+
+    public boolean isInWritable() {
+        return bookieMode.equals(BookieMode.READ_WRITE);
+    }
+
+    synchronized boolean setToWritableMode() {
+        if (!bookieMode.equals(BookieMode.READ_WRITE)) {
+            bookieMode = BookieMode.READ_WRITE;
+            this.lastUpdateTime = System.currentTimeMillis();
+            return true;
+        }
+        return false;
+    }
+
+    boolean isInReadOnlyMode() {
+        return bookieMode.equals(BookieMode.READ_ONLY);
+    }
+
+    synchronized boolean setToReadOnlyMode() {
+        if (!bookieMode.equals(BookieMode.READ_ONLY)) {
+            bookieMode = BookieMode.READ_ONLY;
+            this.lastUpdateTime = System.currentTimeMillis();
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * Write bookie status to multiple directories in best effort
+     *
+     * @param directories list of directories to write to
+     * @return true if any write succeed, false if all failed
+     *
+     */
+    synchronized boolean writeToDirectories(List<File> directories) {
+        boolean success = false;
+        for (File dir : directories) {
+            try {
+                File statusFile = new File(dir, BOOKIE_STATUS_FILENAME);
+                writeToFile(statusFile, toString());
 
 Review comment:
   Why setting the mode and writing to disk is not an atomic operation? What 
happens if bookie crash before writing to file, or after writing to a couple of 
dirs?
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to