Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#4890] improvement(IT): Add the Integration Tests for getFileLocation interface #4920

Merged
merged 6 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.CatalogChange;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.Namespace;
import org.apache.gravitino.Schema;
import org.apache.gravitino.audit.CallerContext;
import org.apache.gravitino.audit.FilesetAuditConstants;
import org.apache.gravitino.audit.FilesetDataOperation;
import org.apache.gravitino.audit.InternalClientType;
import org.apache.gravitino.client.GravitinoMetalake;
import org.apache.gravitino.exceptions.FilesetAlreadyExistsException;
import org.apache.gravitino.exceptions.IllegalNameIdentifierException;
Expand Down Expand Up @@ -71,7 +76,6 @@ public class HadoopCatalogIT extends AbstractIT {
@BeforeAll
public static void setup() throws IOException {
containerSuite.startHiveContainer();

Configuration conf = new Configuration();
conf.set("fs.defaultFS", defaultBaseLocation());
hdfs = FileSystem.get(conf);
Expand Down Expand Up @@ -609,6 +613,82 @@ public void testDropCatalogWithEmptySchema() {
Assertions.assertFalse(metalake.catalogExists(catalogName), "catalog should not be exists");
}

@Test
public void testGetFileLocation() {
String filesetName = GravitinoITUtils.genRandomName("fileset");
NameIdentifier filesetIdent = NameIdentifier.of(schemaName, filesetName);
Assertions.assertFalse(catalog.asFilesetCatalog().filesetExists(filesetIdent));
Fileset expectedFileset =
catalog
.asFilesetCatalog()
.createFileset(
filesetIdent,
"fileset comment",
Fileset.Type.MANAGED,
generateLocation(filesetName),
Maps.newHashMap());
Assertions.assertTrue(catalog.asFilesetCatalog().filesetExists(filesetIdent));
// test without caller context
try {
String actualFileLocation =
catalog.asFilesetCatalog().getFileLocation(filesetIdent, "/test1.par");

Assertions.assertEquals(expectedFileset.storageLocation() + "/test1.par", actualFileLocation);
} finally {
CallerContext.CallerContextHolder.remove();
}

// test with caller context
try {
Map<String, String> context = new HashMap<>();
context.put(
FilesetAuditConstants.HTTP_HEADER_INTERNAL_CLIENT_TYPE,
InternalClientType.HADOOP_GVFS.name());
context.put(
FilesetAuditConstants.HTTP_HEADER_FILESET_DATA_OPERATION,
FilesetDataOperation.CREATE.name());
CallerContext callerContext = CallerContext.builder().withContext(context).build();
CallerContext.CallerContextHolder.set(callerContext);

String actualFileLocation =
catalog.asFilesetCatalog().getFileLocation(filesetIdent, "/test2.par");

Assertions.assertEquals(expectedFileset.storageLocation() + "/test2.par", actualFileLocation);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see you verify the CallerContext in the server side, can you please add some tests about this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test case in the server-side unit test.

} finally {
CallerContext.CallerContextHolder.remove();
}
}

@Test
public void testGetFileLocationWithInvalidAuditHeaders() {
try {
String filesetName = GravitinoITUtils.genRandomName("fileset");
NameIdentifier filesetIdent = NameIdentifier.of(schemaName, filesetName);

Map<String, String> context = new HashMap<>();
// this is an invalid internal client type.
context.put(FilesetAuditConstants.HTTP_HEADER_INTERNAL_CLIENT_TYPE, "test");
CallerContext callerContext = CallerContext.builder().withContext(context).build();
CallerContext.CallerContextHolder.set(callerContext);

Assertions.assertThrows(
IllegalArgumentException.class,
() -> catalog.asFilesetCatalog().getFileLocation(filesetIdent, "/test.par"));
} finally {
CallerContext.CallerContextHolder.remove();
}
}

private static String generateLocation(String filesetName) {
return String.format(
"hdfs://%s:%d/user/hadoop/%s/%s/%s",
containerSuite.getHiveContainer().getContainerIpAddress(),
HiveContainer.HDFS_DEFAULTFS_PORT,
catalogName,
schemaName,
filesetName);
}

private Fileset createFileset(
String filesetName,
String comment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ public String getFileLocation(NameIdentifier ident, String subPath)
try {
String actualFileLocation = dispatcher.getFileLocation(ident, subPath);
// get the audit info from the thread local context
CallerContext callerContext = CallerContext.CallerContextHolder.get();
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
builder.putAll(callerContext.context());
CallerContext callerContext = CallerContext.CallerContextHolder.get();
if (callerContext != null && callerContext.context() != null) {
builder.putAll(callerContext.context());
}
eventBus.dispatchEvent(
new GetFileLocationEvent(
PrincipalUtils.getCurrentUserName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.apache.gravitino.lock.LockType;
import org.apache.gravitino.lock.TreeLockUtils;
import org.apache.gravitino.metrics.MetricNames;
import org.apache.gravitino.rest.RESTUtils;
import org.apache.gravitino.server.web.Utils;
import org.apache.gravitino.utils.NameIdentifierUtil;
import org.apache.gravitino.utils.NamespaceUtil;
Expand Down Expand Up @@ -268,7 +269,7 @@ public Response getFileLocation(
catalog,
schema,
fileset,
subPath);
RESTUtils.decodeString(subPath));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add the test to cover the scenario that path should be encoded and decoded?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the server-side encode/decode unit test more complete. In addition, the current integration test also covers the normal situation of encode/decode, otherwise the actualFileLocation returned by the server cannot match the Location expected by the client.

try {
return Utils.doAs(
httpRequest,
Expand All @@ -283,7 +284,9 @@ public Response getFileLocation(
}
String actualFileLocation =
TreeLockUtils.doWithTreeLock(
ident, LockType.READ, () -> dispatcher.getFileLocation(ident, subPath));
ident,
LockType.READ,
() -> dispatcher.getFileLocation(ident, RESTUtils.decodeString(subPath)));
return Utils.ok(new FileLocationResponse(actualFileLocation));
});
} catch (Exception e) {
Expand Down
Loading