瀏覽代碼

HDFS-17772. Fix JournaledEditsCache int overflow while the maximum capacity to be Integer MAX_VALUE. (#7617). Contributed by Guo Wei.

Signed-off-by: He Xiaoqiao <hexiaoqiao@apache.org>
YongZhuang Guo 1 周之前
父節點
當前提交
4235dc6268

+ 5 - 5
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/qjournal/server/JournaledEditsCache.java

@@ -79,7 +79,7 @@ class JournaledEditsCache {
   private static final long INVALID_TXN_ID = -1;
 
   /** The capacity, in bytes, of this cache. */
-  private final int capacity;
+  private final long capacity;
 
   /**
    * Read/write lock pair wrapped in AutoCloseable; these refer to the same
@@ -117,7 +117,7 @@ class JournaledEditsCache {
    */
   private long initialTxnId;
   /** The current total size of all buffers in this cache. */
-  private int totalSize;
+  private long totalSize;
 
   // ** End lock-protected fields **
 
@@ -128,8 +128,8 @@ class JournaledEditsCache {
         String.format("Cache config %s is set at %f, it should be a positive float value, " +
             "less than 1.0. The recommended value is less than 0.9.",
             DFSConfigKeys.DFS_JOURNALNODE_EDIT_CACHE_SIZE_FRACTION_KEY, fraction));
-    capacity = conf.getInt(DFSConfigKeys.DFS_JOURNALNODE_EDIT_CACHE_SIZE_KEY,
-        (int) (Runtime.getRuntime().maxMemory() * fraction));
+    capacity = conf.getLong(DFSConfigKeys.DFS_JOURNALNODE_EDIT_CACHE_SIZE_KEY,
+        (long) (Runtime.getRuntime().maxMemory() * fraction));
     if (capacity > 0.9 * Runtime.getRuntime().maxMemory()) {
       Journal.LOG.warn(String.format("Cache capacity is set at %d bytes but " +
           "maximum JVM memory is only %d bytes. It is recommended that you " +
@@ -424,7 +424,7 @@ class JournaledEditsCache {
   }
 
   @VisibleForTesting
-  int getCapacity() {
+  long getCapacity() {
     return capacity;
   }
 

+ 2 - 2
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/qjournal/server/TestJournaledEditsCache.java

@@ -226,7 +226,7 @@ public class TestJournaledEditsCache {
     // Assert the default configs.
     Configuration config = new Configuration();
     cache = new JournaledEditsCache(config);
-    assertEquals((int) (Runtime.getRuntime().maxMemory() * 0.5f), cache.getCapacity());
+    assertEquals((long) (Runtime.getRuntime().maxMemory() * 0.5f), cache.getCapacity());
 
     // Set dfs.journalnode.edit-cache-size.bytes.
     Configuration config1 = new Configuration();
@@ -239,7 +239,7 @@ public class TestJournaledEditsCache {
     Configuration config2 = new Configuration();
     config2.setFloat(DFSConfigKeys.DFS_JOURNALNODE_EDIT_CACHE_SIZE_FRACTION_KEY, 0.1f);
     cache = new JournaledEditsCache(config2);
-    assertEquals((int) (Runtime.getRuntime().maxMemory() * 0.1f), cache.getCapacity());
+    assertEquals((long) (Runtime.getRuntime().maxMemory() * 0.1f), cache.getCapacity());
   }
 
   private void storeEdits(int startTxn, int endTxn) throws Exception {