Преглед изворни кода

HDFS-12205. Ozone: List Key on an empty ozone bucket fails with command failed error. Contributed by Lokesh Jain.

Yiqun Lin пре 7 година
родитељ
комит
f6d656642c

+ 5 - 6
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/localstorage/OzoneMetadataManager.java

@@ -634,6 +634,10 @@ public final class OzoneMetadataManager {
       userDB.put(args.getParentName().getBytes(encoding),
           bucketList.toDBString().getBytes(encoding));
 
+      // Update userDB with volume/bucket -> empty key list
+      userDB.put(args.getResourceName().getBytes(encoding),
+          new ListKeys().toDBString().getBytes(encoding));
+
       // and update the metadataDB with volume/bucket->BucketInfo
       metadataDB.put(args.getResourceName().getBytes(encoding),
           bucketInfo.toDBString().getBytes(encoding));
@@ -916,12 +920,7 @@ public final class OzoneMetadataManager {
       ListKeys keyList;
       byte[] bucketListBytes = userDB.get(args.getParentName()
           .getBytes(encoding));
-      if (bucketListBytes == null) {
-        keyList = new ListKeys();
-      } else {
-        keyList = ListKeys.parse(new String(bucketListBytes, encoding));
-      }
-
+      keyList = ListKeys.parse(new String(bucketListBytes, encoding));
       KeyInfo keyInfo;
 
       byte[] objectBytes = metadataDB.get(args.getResourceName()

+ 4 - 0
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/ozone/web/TestLocalOzoneVolumes.java

@@ -178,4 +178,8 @@ public class TestLocalOzoneVolumes extends TestOzoneHelper {
     super.testGetVolumesOfAnotherUserShouldFail(port);
   }
 
+  @Test
+  public void testListKeyOnEmptyBucket() throws IOException {
+    super.testListKeyOnEmptyBucket(port);
+  }
 }

+ 60 - 0
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/ozone/web/TestOzoneHelper.java

@@ -351,4 +351,64 @@ public class TestOzoneHelper {
     }
   }
 
+  public void testListKeyOnEmptyBucket(int port) throws IOException {
+    SimpleDateFormat format =
+        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZ", Locale.US);
+    CloseableHttpClient client = createHttpClient();
+    String volumeName = OzoneUtils.getRequestID().toLowerCase();
+    String bucketName = OzoneUtils.getRequestID().toLowerCase() + "bucket";
+    try {
+
+      HttpPost httppost = new HttpPost(
+          String.format("http://localhost:%d/%s", port, volumeName));
+      httppost.addHeader(Header.OZONE_VERSION_HEADER,
+          Header.OZONE_V1_VERSION_HEADER);
+      httppost.addHeader(HttpHeaders.DATE,
+          format.format(new Date(Time.monotonicNow())));
+      httppost.addHeader(HttpHeaders.AUTHORIZATION,
+          Header.OZONE_SIMPLE_AUTHENTICATION_SCHEME + " "
+              + OzoneConsts.OZONE_SIMPLE_HDFS_USER);
+      httppost.addHeader(Header.OZONE_USER, OzoneConsts.OZONE_SIMPLE_HDFS_USER);
+      HttpResponse response = client.execute(httppost);
+      assertEquals(response.toString(), HTTP_CREATED,
+          response.getStatusLine().getStatusCode());
+      client.close();
+
+      client = createHttpClient();
+      httppost = new HttpPost(String
+          .format("http://localhost:%d/%s/%s", port, volumeName, bucketName));
+      httppost.addHeader(Header.OZONE_VERSION_HEADER,
+          Header.OZONE_V1_VERSION_HEADER);
+      httppost.addHeader(HttpHeaders.DATE,
+          format.format(new Date(Time.monotonicNow())));
+      httppost.addHeader(HttpHeaders.AUTHORIZATION,
+          Header.OZONE_SIMPLE_AUTHENTICATION_SCHEME + " "
+              + OzoneConsts.OZONE_SIMPLE_HDFS_USER);
+      httppost.addHeader(Header.OZONE_USER, OzoneConsts.OZONE_SIMPLE_HDFS_USER);
+      response = client.execute(httppost);
+      assertEquals(response.toString(), HTTP_CREATED,
+          response.getStatusLine().getStatusCode());
+      client.close();
+
+      client = createHttpClient();
+      HttpGet httpget = new HttpGet(String
+          .format("http://localhost:%d/%s/%s", port, volumeName, bucketName));
+      httpget.addHeader(Header.OZONE_VERSION_HEADER,
+          Header.OZONE_V1_VERSION_HEADER);
+      httpget.addHeader(HttpHeaders.DATE,
+          format.format(new Date(Time.monotonicNow())));
+      httpget.addHeader(HttpHeaders.AUTHORIZATION,
+          Header.OZONE_SIMPLE_AUTHENTICATION_SCHEME + " "
+              + OzoneConsts.OZONE_SIMPLE_HDFS_USER);
+      httpget.addHeader(Header.OZONE_USER, OzoneConsts.OZONE_SIMPLE_HDFS_USER);
+      response = client.execute(httpget);
+      assertEquals(response.toString() + " " + response.getStatusLine()
+              .getReasonPhrase(), HTTP_OK,
+          response.getStatusLine().getStatusCode());
+
+    } finally {
+      client.close();
+    }
+  }
+
 }