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

 ##########
 File path: 
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieStatus.java
 ##########
 @@ -0,0 +1,246 @@
+/**
+ * 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 static com.google.common.base.Charsets.UTF_8;
+import static 
org.apache.bookkeeper.util.BookKeeperConstants.BOOKIE_STATUS_FILENAME;
+
+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;
+
+/**
+ * The status object represents the current status of a bookie instance.
+ */
+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;
+    }
+
+    private synchronized BookieMode getBookieMode() {
+        return bookieMode;
+    }
+
+    public synchronized 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;
+    }
+
+    synchronized 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());
+                success = true;
+            } catch (IOException e) {
+                LOG.warn("IOException while trying to write bookie status to 
directory {}." +
+                    " This is fine if not all directories are failed.", dir);
+            }
+        }
+        if(success){
+            LOG.info("Successfully persist bookie status {}", this.bookieMode);
+        } else {
+            LOG.warn("Failed to persist bookie status {}", this.bookieMode);
+        }
+        return success;
+    }
+
+    /**
+     * Write content to the file. If file does not exist, it will create one.
+     *
+     * @param file file that you want to write to
+     * @param body content to write
+     * @throws IOException
+     */
+    private void writeToFile(File file, String body) throws IOException {
+        FileOutputStream fos = new FileOutputStream(file);
+        BufferedWriter bw = null;
+        try {
+            bw = new BufferedWriter(new OutputStreamWriter(fos, UTF_8));
+            bw.write(body);
+        } finally {
+            if (bw != null) {
+                bw.close();
+            }
+            fos.close();
+        }
+    }
+
+    /**
+     * Read bookie status from the status files, and update the bookie status 
if read succeed.
+     * If a status file is not readable or not found, it will skip and try to 
read from the next file.
+     *
+     * @param directories list of directories that store the status file
+     * @return true if successfully read from file, otherwise false.
+     */
+    boolean readFromDirectories(List<File> directories) {
+        boolean success = false;
+        for (File dir : directories) {
+            File statusFile = new File(dir, BOOKIE_STATUS_FILENAME);
+            try {
+                BookieStatus status = readFromFile(statusFile);
+                if (null != status) {
+                    synchronized (status) {
+                        if (status.lastUpdateTime > this.lastUpdateTime) {
+                            this.lastUpdateTime = status.lastUpdateTime;
+                            this.layoutVersion = status.layoutVersion;
+                            this.bookieMode = status.bookieMode;
+                            success = true;
+                        }
+                    }
+                }
+            } catch (IOException e) {
+                LOG.warn("IOException while trying to read bookie status from 
directory {}." +
+                    " This is fine if not all directories failed.", dir);
+            } catch (IllegalArgumentException e ){
+                LOG.warn("IllegalArgumentException while trying to read bookie 
status from directory {}." +
+                    " This is fine if not all directories failed.", dir);
+            }
+        }
+        if (success) {
 
 Review comment:
   the status with latest 'lastUpdateTime' wins.
 
----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to