xloya commented on code in PR #4281:
URL: https://github.com/apache/gravitino/pull/4281#discussion_r1711223730


##########
common/src/main/java/org/apache/gravitino/context/CallerContext.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.gravitino.context;
+
+import com.google.common.base.Objects;
+import com.google.common.base.Preconditions;
+import java.util.Map;
+
+/**
+ * A class defining the caller context for auditing coarse granularity 
operations.
+ *
+ * <p>Reference:
+ *
+ * 
<p>hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/CallerContext.java
+ */
+public class CallerContext {
+  private Map<String, String> context;
+
+  private CallerContext() {}
+
+  public Map<String, String> context() {
+    return context;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof CallerContext)) return false;
+    CallerContext context1 = (CallerContext) o;
+    return Objects.equal(context, context1.context);
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hashCode(context);
+  }
+
+  public static class Builder {
+    private final CallerContext callerContext;
+
+    private Builder() {
+      callerContext = new CallerContext();
+    }
+
+    /**
+     * Sets the context for CallerContext
+     *
+     * @param context The context to set.
+     * @return This Builder instance for method chaining.
+     */
+    public CallerContext.Builder withContext(Map<String, String> context) {
+      callerContext.context = context;
+      return this;
+    }
+
+    private void validate() {
+      Preconditions.checkArgument(callerContext.context != null, "context 
cannot be null");
+    }
+
+    public CallerContext build() {
+      validate();
+      return callerContext;
+    }
+  }
+
+  /**
+   * Create a new builder for the CallerContext.
+   *
+   * @return A new builder for the CallerContext.
+   */
+  public static CallerContext.Builder builder() {
+    return new CallerContext.Builder();
+  }
+
+  public static class CallerContextHolder {
+
+    private static final ThreadLocal<CallerContext> CALLER_CONTEXT = new 
ThreadLocal<>();

Review Comment:
   Here I use the thread local context to pass the audit info to the server. 
The idea comes from the process of interaction between HDFS Client and NN.



##########
server/src/main/java/org/apache/gravitino/server/web/rest/FilesetOperations.java:
##########
@@ -245,4 +250,44 @@ public Response dropFileset(
       return ExceptionHandlers.handleFilesetException(OperationType.DROP, 
fileset, schema, e);
     }
   }
+
+  @GET
+  @Path("{fileset}/fileLocation")
+  @Produces("application/vnd.gravitino.v1+json")
+  @Timed(name = "get-file-location." + MetricNames.HTTP_PROCESS_DURATION, 
absolute = true)
+  @ResponseMetered(name = "get-file-location", absolute = true)
+  public Response getFileLocation(
+      @PathParam("metalake") String metalake,
+      @PathParam("catalog") String catalog,
+      @PathParam("schema") String schema,
+      @PathParam("fileset") String fileset,
+      @QueryParam("subPath") @NotNull String subPath) {
+    LOG.info(
+        "Received get file location request: {}.{}.{}.{}, sub path:{}",
+        metalake,
+        catalog,
+        schema,
+        fileset,
+        subPath);
+    try {
+      return Utils.doAs(
+          httpRequest,
+          () -> {
+            NameIdentifier ident = NameIdentifierUtil.ofFileset(metalake, 
catalog, schema, fileset);
+            Map<String, String> filteredAuditHeaders = 
Utils.filterFilesetAuditHeaders(httpRequest);
+            // set the audit info into the thread local context
+            if (!filteredAuditHeaders.isEmpty()) {

Review Comment:
   I will set the audit context to the server's thread local audit context here.



##########
core/src/main/java/org/apache/gravitino/listener/FilesetEventDispatcher.java:
##########
@@ -138,4 +141,22 @@ public boolean dropFileset(NameIdentifier ident) {
       throw e;
     }
   }
+
+  @Override
+  public String getFileLocation(NameIdentifier ident, String subPath)
+      throws NoSuchFilesetException {
+    try {
+      String actualFileLocation = dispatcher.getFileLocation(ident, subPath);
+      // get the audit info from the thread local context
+      CallerContext context = CallerContext.CallerContextHolder.get();

Review Comment:
   Here we can get the thread local audit context, and put it into the event 
bus to audit.



##########
clients/client-java/src/main/java/org/apache/gravitino/client/FilesetCatalog.java:
##########
@@ -226,6 +229,36 @@ public boolean dropFileset(NameIdentifier ident) {
     return resp.dropped();
   }
 
+  /**
+   * Get the actual path of a file or directory based on the storage location 
of Fileset and the sub
+   * path.
+   *
+   * @param ident A fileset identifier.
+   * @param subPath The sub path to the file or directory.
+   * @return The actual location of the file or directory.
+   * @throws NoSuchFilesetException If the fileset does not exist.
+   */
+  @Override
+  public String getFileLocation(NameIdentifier ident, String subPath)
+      throws NoSuchFilesetException {
+    checkFilesetNameIdentifier(ident);
+    Namespace fullNamespace = getFilesetFullNamespace(ident.namespace());
+
+    CallerContext callerContext = CallerContext.CallerContextHolder.get();

Review Comment:
   I get the thread local audit context here, add put it into the request 
header.



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

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to