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_r350013195
 
 

 ##########
 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();
+        }
+      } catch (Throwable e) {
+        LOG.warn("Failed to read active index from cache file "
+            + cacheActiveFile + ", will begin from index 0.", e);
 
 Review comment:
   OK, thanks, I have added it like this:
   
   2019-11-25 14:52:11,678 WARN ha.AbstractNNFailoverProxyProvider: Failed to 
read active index from cache file /tmp/ns1, It may be due to permission issues, 
the dfs.client.failover.cache-active.dir must be properly configured! Now we 
will begin from index 0.
   java.io.FileNotFoundException: /tmp/ns1 (Permission denied)
        at java.io.RandomAccessFile.open0(Native Method)
        at java.io.RandomAccessFile.open(RandomAccessFile.java:316)
        at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
        ...

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