gargvishesh commented on code in PR #15398: URL: https://github.com/apache/druid/pull/15398#discussion_r1424845218
########## extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/output/GoogleStorageConnector.java: ########## @@ -0,0 +1,200 @@ +/* + * 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.druid.storage.google.output; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import com.google.common.collect.Iterators; +import org.apache.druid.data.input.impl.CloudObjectLocation; +import org.apache.druid.data.input.impl.prefetch.ObjectOpenFunction; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.storage.google.GoogleInputDataConfig; +import org.apache.druid.storage.google.GoogleStorage; +import org.apache.druid.storage.google.GoogleStorageDruidModule; +import org.apache.druid.storage.google.GoogleStorageObjectMetadata; +import org.apache.druid.storage.google.GoogleUtils; +import org.apache.druid.storage.remote.ChunkingStorageConnector; +import org.apache.druid.storage.remote.ChunkingStorageConnectorParameters; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Iterator; + +public class GoogleStorageConnector extends ChunkingStorageConnector<GoogleInputRange> Review Comment: Added for this and some other classes as well. ########## extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/ObjectStorageIterator.java: ########## @@ -91,35 +75,35 @@ public boolean hasNext() } @Override - public StorageObject next() + public GoogleStorageObjectMetadata next() { if (!hasNext()) { throw new NoSuchElementException(); } - final StorageObject retVal = currentObject; + final GoogleStorageObjectMetadata retVal = currentObject; advanceStorageObject(); return retVal; } private void advanceStorageObject() { - while (storageObjectsIterator.hasNext() || nextPageToken != null || uris.hasNext()) { - while (storageObjectsIterator.hasNext()) { - final StorageObject next = storageObjectsIterator.next(); + while (blobIterator.hasNext() || nextPageToken != null || uris.hasNext()) { + while (blobIterator.hasNext()) { + final GoogleStorageObjectMetadata next = blobIterator.next(); // list with prefix can return directories, but they should always end with `/`, ignore them. // also skips empty objects. - if (!next.getName().endsWith("/") && next.getSize().signum() > 0) { + if (!next.getName().endsWith("/") && Long.signum(next.getSize()) > 0) { Review Comment: This is more a check meant for != 0 ########## extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/output/GoogleInputRange.java: ########## @@ -0,0 +1,80 @@ +/* + * 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.druid.storage.google.output; + +import java.util.Objects; + +public class GoogleInputRange +{ + private final long start; + private final long size; + private final String bucket; + private final String path; + + public GoogleInputRange(long start, long size, String bucket, String path) + { + this.start = start; + this.size = size; + this.bucket = bucket; + this.path = path; + } + + public long getStart() + { + return start; + } + + public long getSize() + { + return size; + } + + public String getBucket() + { + return bucket; + } + + public String getPath() + { + return path; + } + + @Override + public boolean equals(Object o) + { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + GoogleInputRange that = (GoogleInputRange) o; + return start == that.start + && size == that.size + && Objects.equals(bucket, that.bucket) + && Objects.equals(path, that.path); + } + Review Comment: Done ########## extensions-core/google-extensions/pom.xml: ########## @@ -48,16 +48,11 @@ </dependency> <dependency> - <groupId>com.google.apis</groupId> Review Comment: Removed now. ########## extensions-core/google-extensions/src/main/java/org/apache/druid/storage/google/GoogleStorage.java: ########## @@ -36,69 +47,137 @@ public class GoogleStorage * if we have a Storage instead of a supplier of it, it can cause unnecessary config validation * against Google storage even when it's not used at all. To perform the config validation * only when it is actually used, we use a supplier. - * + * <p> * See OmniDataSegmentKiller for how DataSegmentKillers are initialized. */ private final Supplier<Storage> storage; - public GoogleStorage(Supplier<Storage> storage) + public GoogleStorage(final Supplier<Storage> storage) { this.storage = storage; } public void insert(final String bucket, final String path, AbstractInputStreamContent mediaContent) throws IOException { - Storage.Objects.Insert insertObject = storage.get().objects().insert(bucket, null, mediaContent); - insertObject.setName(path); - insertObject.getMediaHttpUploader().setDirectUploadEnabled(false); - insertObject.execute(); + storage.get().createFrom(getBlobInfo(bucket, path), mediaContent.getInputStream()); } - public InputStream get(final String bucket, final String path) throws IOException + public InputStream getInputStream(final String bucket, final String path) throws IOException { - return get(bucket, path, 0); + return getInputStream(bucket, path, 0, null); } - public InputStream get(final String bucket, final String path, long start) throws IOException + public InputStream getInputStream(final String bucket, final String path, long start) throws IOException { - final Get get = storage.get().objects().get(bucket, path); - InputStream inputStream = get.executeMediaAsInputStream(); - inputStream.skip(start); - return inputStream; + return getInputStream(bucket, path, start, null); } - public StorageObject getMetadata(final String bucket, final String path) throws IOException + public InputStream getInputStream(final String bucket, final String path, long start, @Nullable Long length) + throws IOException { - return storage.get().objects().get(bucket, path).execute(); + ReadChannel reader = storage.get().reader(bucket, path); + reader.seek(start); + if (length != null) { + reader.limit(start + length); + } + return Channels.newInputStream(reader); + } + + public OutputStream getObjectOutputStream( + final String bucket, + final String path + ) + { + WriteChannel writer = storage.get().writer(getBlobInfo(bucket, path)); + return Channels.newOutputStream(writer); + } + + public GoogleStorageObjectMetadata getMetadata( + final String bucket, + final String path + ) + { + Blob blob = storage.get().get(bucket, path, Storage.BlobGetOption.fields(Storage.BlobField.values())); + return new GoogleStorageObjectMetadata( + blob.getBucket(), + blob.getName(), + blob.getSize(), + blob.getUpdateTimeOffsetDateTime() + .toEpochSecond() + ); } public void delete(final String bucket, final String path) throws IOException { - storage.get().objects().delete(bucket, path).execute(); + storage.get().delete(bucket, path); } public boolean exists(final String bucket, final String path) { - try { - return storage.get().objects().get(bucket, path).executeUsingHead().isSuccessStatusCode(); - } - catch (Exception e) { - return false; - } + + Blob blob = storage.get().get(bucket, path); + return blob != null; } - + public long size(final String bucket, final String path) throws IOException { - return storage.get().objects().get(bucket, path).execute().getSize().longValue(); + Blob blob = storage.get().get(bucket, path, Storage.BlobGetOption.fields(Storage.BlobField.SIZE)); + return blob.getSize(); } public String version(final String bucket, final String path) throws IOException { - return storage.get().objects().get(bucket, path).execute().getEtag(); + Blob blob = storage.get().get(bucket, path, Storage.BlobGetOption.fields(Storage.BlobField.GENERATION)); + return blob.getGeneratedId(); + } + + public GoogleStorageObjectPage list( Review Comment: Done. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
