iamcaoxudong commented on a change in pull request #1700: HDFS-14963. Add HDFS 
Client machine caching active namenode index mechanism.
URL: https://github.com/apache/hadoop/pull/1700#discussion_r350032506
 
 

 ##########
 File path: 
hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/server/namenode/ha/ConfiguredFailoverProxyProvider.java
 ##########
 @@ -93,4 +109,65 @@ public synchronized void close() throws IOException {
   public boolean useLogicalURI() {
     return true;
   }
+
+  /**
+   * Write active NameNode index to cache file.
+   */
+  public void writeActiveCache(int index) {
+    if (!cacheActiveEnabled) {
+      return;
+    }
+
+    synchronized (cacheActiveFile) {
+      boolean exist = cacheActiveFile.exists();
+      try (RandomAccessFile raf = new RandomAccessFile(cacheActiveFile, "rw");
+          FileChannel fc = raf.getChannel();
+          FileLock lock = fc.tryLock(0, Long.MAX_VALUE, false)) {
+        if (lock != null) {
+          raf.setLength(0);
+          raf.writeBytes(String.valueOf(index));
+          if (!exist) {
+            boolean ret = cacheActiveFile.setWritable(true, false)
+                && cacheActiveFile.setReadable(true, false);
+            if (!ret) {
+              throw new IOException("Cannot set file rw mode.");
+            }
+          }
+          LOG.debug("Succeed in writing active index " + index
+              + " to cache file " + cacheActiveFile);
+        }
+      } catch (Throwable e) {
+        LOG.warn("Filed to write active index " + index + " to cache file "
+            + cacheActiveFile, e);
+      }
+    }
+  }
+
+  /**
+   * Read active NameNode index from cache file.
+   */
+  public int readActiveCache() {
+    if (!cacheActiveEnabled) {
+      return 0;
+    }
+
+    int index = 0;
+    synchronized (cacheActiveFile) {
+      if (!cacheActiveFile.exists()) {
+        return 0;
+      }
+
+      try (RandomAccessFile raf = new RandomAccessFile(cacheActiveFile, "rw");
+          FileChannel fc = raf.getChannel();
+          FileLock lock = fc.tryLock(0, Long.MAX_VALUE, true)) {
+        if (lock != null) {
+          index = Integer.parseInt(raf.readLine()) % proxies.size();
 
 Review comment:
   Sorry, I have missed this comment before,  Done now.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to