You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by GitBox <gi...@apache.org> on 2021/07/16 18:47:33 UTC

[GitHub] [ozone] smengcl commented on a change in pull request #2417: HDDS-5378 Add APIs to retrieve Namespace Summary from Recon

smengcl commented on a change in pull request #2417:
URL: https://github.com/apache/ozone/pull/2417#discussion_r671460328



##########
File path: hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/api/NSSummaryEndpoint.java
##########
@@ -0,0 +1,561 @@
+/*
+ * 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.hadoop.ozone.recon.api;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.OmVolumeArgs;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.apache.hadoop.ozone.recon.ReconConstants;
+import org.apache.hadoop.ozone.recon.api.types.BasicResponse;
+import org.apache.hadoop.ozone.recon.api.types.DUResponse;
+import org.apache.hadoop.ozone.recon.api.types.EntityType;
+import org.apache.hadoop.ozone.recon.api.types.FileSizeDistributionResponse;
+import org.apache.hadoop.ozone.recon.api.types.NSSummary;
+import org.apache.hadoop.ozone.recon.api.types.QuotaUsageResponse;
+import org.apache.hadoop.ozone.recon.recovery.ReconOMMetadataManager;
+import org.apache.hadoop.ozone.recon.spi.ReconNamespaceSummaryManager;
+
+import javax.inject.Inject;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX;
+import static org.apache.hadoop.ozone.om.helpers.OzoneFSUtils.removeTrailingSlashIfNeeded;
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * REST APIs for namespace metadata summary.
+ */
+@Path("/nssummary")
+@Produces(MediaType.APPLICATION_JSON)
+public class NSSummaryEndpoint {
+  @Inject
+  private ReconNamespaceSummaryManager reconNamespaceSummaryManager;
+
+  @Inject
+  private ReconOMMetadataManager omMetadataManager;
+
+  /**
+   * This endpoint will return the entity type and aggregate count of objects.
+   * @param path the request path.
+   * @return HTTP response with basic info: entity type, num of objects
+   * @throws IOException IOE
+   */
+  @GET
+  @Path("/basic")
+  public Response getBasicInfo(
+          @QueryParam("path") String path) throws IOException {
+
+    String[] names = parseRequestPath(path);
+    EntityType type = getEntityType(names);
+
+    BasicResponse basicResponse = null;
+    switch (type) {
+    case VOLUME:
+      basicResponse = new BasicResponse(EntityType.VOLUME);
+      List<OmBucketInfo> buckets = omMetadataManager.listBuckets(names[0],
+              null, null, Integer.MAX_VALUE);
+      basicResponse.setTotalBucket(buckets.size());
+      int totalDir = 0;
+      int totalKey = 0;
+
+      // iterate all buckets to collect the total object count.
+      for (OmBucketInfo bucket : buckets) {
+        long bucketObjectId = bucket.getObjectID();
+        totalDir += getTotalDirCount(bucketObjectId);
+        totalKey += getTotalKeyCount(bucketObjectId);
+      }
+      basicResponse.setTotalDir(totalDir);
+      basicResponse.setTotalKey(totalKey);
+      break;
+    case BUCKET:
+      basicResponse = new BasicResponse(EntityType.BUCKET);
+      assert (names.length == 2);
+      long bucketObjectId = getBucketObjectId(names);
+      basicResponse.setTotalDir(getTotalDirCount(bucketObjectId));
+      basicResponse.setTotalKey(getTotalKeyCount(bucketObjectId));
+      break;
+    case DIRECTORY:
+      // path should exist so we don't need any extra verification/null check
+      long dirObjectId = getDirObjectId(names);
+      basicResponse = new BasicResponse(EntityType.DIRECTORY);
+      basicResponse.setTotalDir(getTotalDirCount(dirObjectId));
+      basicResponse.setTotalKey(getTotalKeyCount(dirObjectId));
+      break;
+    case KEY:
+      basicResponse = new BasicResponse(EntityType.KEY);
+      break;
+    case INVALID:
+      basicResponse = new BasicResponse(EntityType.INVALID);
+      basicResponse.setPathNotFound(true);
+      break;
+    default:
+      break;
+    }
+    return Response.ok(basicResponse).build();
+  }
+
+  /**
+   * DU endpoint to return datasize for subdirectory (bucket for volume).
+   * @param path request path
+   * @return DU response
+   * @throws IOException
+   */
+  @GET
+  @Path("/du")
+  public Response getDiskUsage(@QueryParam("path") String path)
+          throws IOException {
+    String[] names = parseRequestPath(path);
+    EntityType type = getEntityType(names);
+    DUResponse duResponse = new DUResponse();
+    switch (type) {
+    case VOLUME:
+      String volName = names[0];
+      List<OmBucketInfo> buckets = omMetadataManager.listBuckets(volName,
+              null, null, Integer.MAX_VALUE);
+      duResponse.setCount(buckets.size());
+
+      // List of DiskUsage data for all buckets
+      List<DUResponse.DiskUsage> bucketDuData = new ArrayList<>();
+      for (OmBucketInfo bucket : buckets) {
+        String bucketName = bucket.getBucketName();
+        long bucketObjectID = bucket.getObjectID();
+        String subpath = omMetadataManager.getBucketKey(volName, bucketName);
+        DUResponse.DiskUsage diskUsage = new DUResponse.DiskUsage();
+        diskUsage.setSubpath(subpath);
+        long dataSize = getTotalSize(bucketObjectID);
+        diskUsage.setSize(dataSize);
+        bucketDuData.add(diskUsage);
+      }
+      duResponse.setDuData(bucketDuData);
+      break;
+    case BUCKET:
+      long bucketObjectId = getBucketObjectId(names);
+      NSSummary bucketNSSummary =
+              reconNamespaceSummaryManager.getNSSummary(bucketObjectId);
+
+      // get object IDs for all its subdirectories
+      Set<Long> bucketSubdirs = bucketNSSummary.getChildDir();
+      duResponse.setCount(bucketSubdirs.size());
+      List<DUResponse.DiskUsage> dirDUData = new ArrayList<>();
+      for (long subdirObjectId: bucketSubdirs) {
+        NSSummary subdirNSSummary = reconNamespaceSummaryManager
+                .getNSSummary(subdirObjectId);
+
+        // get directory's name and generate the next-level subpath.
+        String dirName = subdirNSSummary.getDirName();
+        String subpath = path + OM_KEY_PREFIX + dirName;
+        // we need to reformat the subpath in the response in a
+        // format with leading slash and without trailing slash
+        DUResponse.DiskUsage diskUsage = new DUResponse.DiskUsage();
+        diskUsage.setSubpath(reformatString(subpath));
+        long dataSize = getTotalSize(subdirObjectId);
+        diskUsage.setSize(dataSize);
+        dirDUData.add(diskUsage);
+      }
+      duResponse.setDuData(dirDUData);
+      break;
+    case DIRECTORY:
+      long dirObjectId = getDirObjectId(names);
+      NSSummary dirNSSummary =
+              reconNamespaceSummaryManager.getNSSummary(dirObjectId);
+      Set<Long> subdirs = dirNSSummary.getChildDir();
+
+      duResponse = new DUResponse();
+      duResponse.setCount(subdirs.size());
+      List<DUResponse.DiskUsage> subdirDUData = new ArrayList<>();
+      // iterate all subdirectories to get disk usage data
+      for (long subdirObjectId: subdirs) {
+        NSSummary subdirNSSummary =
+                reconNamespaceSummaryManager.getNSSummary(subdirObjectId);
+        String subdirName = subdirNSSummary.getDirName();
+        // build the path for subdirectory
+        String subpath = path + OM_KEY_PREFIX + subdirName;
+        DUResponse.DiskUsage diskUsage = new DUResponse.DiskUsage();
+        // reformat the response
+        diskUsage.setSubpath(reformatString(subpath));
+        long dataSize = getTotalSize(subdirObjectId);
+        diskUsage.setSize(dataSize);
+        subdirDUData.add(diskUsage);
+      }
+      duResponse.setDuData(subdirDUData);
+      break;
+    case KEY:
+      // DU for key is the data size
+      duResponse.setCount(1);
+      DUResponse.DiskUsage keyDU = new DUResponse.DiskUsage();
+      // The object ID for the directory that the key is directly in
+      long parentObjectId = getDirObjectId(names, names.length - 1);
+      String fileName = names[names.length - 1];
+      String ozoneKey =
+              omMetadataManager.getOzonePathKey(parentObjectId, fileName);
+      OmKeyInfo keyInfo = omMetadataManager.getKeyTable().get(ozoneKey);
+      keyDU.setSubpath(reformatString(path));
+      keyDU.setSize(keyInfo.getDataSize());
+      duResponse.setDuData(Collections.singletonList(keyDU));
+      break;
+    case INVALID:
+      duResponse.setPathNotFound(true);
+      break;
+    default:
+      break;
+    }
+    return Response.ok(duResponse).build();
+  }
+
+  /**
+   * Quora usage endpoint that summarize the quota allowed and quota used in

Review comment:
       ```suggestion
      * Quota usage endpoint that summarize the quota allowed and quota used in
   ```
   typo?




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

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org