From e8674e2a60aed3b223809bbcb124d132634c19f8 Mon Sep 17 00:00:00 2001 From: Parag Jain Date: Wed, 19 Apr 2023 11:26:18 +0530 Subject: [PATCH] fix npe with gs uri having underscores (#14107) * fix npe with gs uri having underscores * compile fix --- .../google/GoogleDataSegmentPuller.java | 4 +- .../google/GoogleDataSegmentPullerTest.java | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleDataSegmentPuller.java b/extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleDataSegmentPuller.java index 40bd926b4bb..fc3f7d371f4 100644 --- a/extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleDataSegmentPuller.java +++ b/extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleDataSegmentPuller.java @@ -83,14 +83,14 @@ public class GoogleDataSegmentPuller implements URIDataPuller public InputStream getInputStream(URI uri) throws IOException { String path = StringUtils.maybeRemoveLeadingSlash(uri.getPath()); - return storage.get(uri.getHost(), path); + return storage.get(uri.getHost() != null ? uri.getHost() : uri.getAuthority(), path); } @Override public String getVersion(URI uri) throws IOException { String path = StringUtils.maybeRemoveLeadingSlash(uri.getPath()); - return storage.version(uri.getHost(), path); + return storage.version(uri.getHost() != null ? uri.getHost() : uri.getAuthority(), path); } @Override diff --git a/extensions-core/google-extensions/src/test/java/org/apache/druid/storage/google/GoogleDataSegmentPullerTest.java b/extensions-core/google-extensions/src/test/java/org/apache/druid/storage/google/GoogleDataSegmentPullerTest.java index 1c39be2a811..deb2383fd6c 100644 --- a/extensions-core/google-extensions/src/test/java/org/apache/druid/storage/google/GoogleDataSegmentPullerTest.java +++ b/extensions-core/google-extensions/src/test/java/org/apache/druid/storage/google/GoogleDataSegmentPullerTest.java @@ -23,6 +23,7 @@ import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.googleapis.testing.json.GoogleJsonResponseExceptionFactoryTesting; import com.google.api.client.json.jackson2.JacksonFactory; import org.apache.druid.java.util.common.FileUtils; +import org.apache.druid.java.util.common.StringUtils; import org.apache.druid.segment.loading.SegmentLoadingException; import org.easymock.EasyMock; import org.easymock.EasyMockSupport; @@ -31,6 +32,8 @@ import org.junit.Test; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.net.URI; public class GoogleDataSegmentPullerTest extends EasyMockSupport { @@ -64,4 +67,38 @@ public class GoogleDataSegmentPullerTest extends EasyMockSupport FileUtils.deleteDirectory(outDir); } } + + @Test + public void testGetVersionBucketNameWithUnderscores() throws IOException + { + String bucket = "bucket_test"; + String prefix = "prefix/"; + String version = "0"; + + GoogleStorage storage = createMock(GoogleStorage.class); + EasyMock.expect(storage.version(EasyMock.eq(bucket), EasyMock.eq(prefix))).andReturn("0"); + EasyMock.replay(storage); + + GoogleDataSegmentPuller puller = new GoogleDataSegmentPuller(storage); + + String actual = puller.getVersion(URI.create(StringUtils.format("gs://%s/%s", bucket, prefix))); + Assert.assertEquals(version, actual); + EasyMock.verify(storage); + } + + @Test + public void testGetInputStreamBucketNameWithUnderscores() throws IOException + { + String bucket = "bucket_test"; + String prefix = "prefix/"; + + GoogleStorage storage = createMock(GoogleStorage.class); + EasyMock.expect(storage.get(EasyMock.eq(bucket), EasyMock.eq(prefix))).andReturn(EasyMock.createMock(InputStream.class)); + EasyMock.replay(storage); + + GoogleDataSegmentPuller puller = new GoogleDataSegmentPuller(storage); + + puller.getInputStream(URI.create(StringUtils.format("gs://%s/%s", bucket, prefix))); + EasyMock.verify(storage); + } }