nastra commented on code in PR #8303: URL: https://github.com/apache/iceberg/pull/8303#discussion_r1301688727
########## azure/src/test/java/org/apache/iceberg/azure/adlsv2/ADLSInputStreamTest.java: ########## @@ -0,0 +1,208 @@ +/* + * 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.iceberg.azure.adlsv2; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.azure.storage.file.datalake.DataLakeFileClient; +import java.io.IOException; +import java.util.Arrays; +import java.util.Random; +import org.apache.iceberg.azure.AzureProperties; +import org.apache.iceberg.io.IOUtil; +import org.apache.iceberg.io.RangeReadable; +import org.apache.iceberg.io.SeekableInputStream; +import org.apache.iceberg.metrics.MetricsContext; +import org.junit.jupiter.api.Test; + +public class ADLSInputStreamTest extends BaseAzuriteTest { + + private static final String FILE_PATH = "path/to/file"; Review Comment: would it make sense to use `@TempDir private Path temp;`? ########## azure/src/test/java/org/apache/iceberg/azure/adlsv2/ADLSOutputStreamTest.java: ########## @@ -0,0 +1,99 @@ +/* + * 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.iceberg.azure.adlsv2; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.azure.storage.file.datalake.DataLakeFileClient; +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; +import java.util.Random; +import java.util.UUID; +import org.apache.iceberg.azure.AzureProperties; +import org.apache.iceberg.io.IOUtil; +import org.apache.iceberg.metrics.MetricsContext; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +public class ADLSOutputStreamTest extends BaseAzuriteTest { + + private final Random random = new Random(1); + private final AzureProperties azureProperties = new AzureProperties(); + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + public void testWrite(boolean arrayWrite) { + // Test small file write + writeAndVerify(randomData(1024), arrayWrite); + + // Test large file + writeAndVerify(randomData(10 * 1024 * 1024), arrayWrite); + } + + @Test + public void testMultipleClose() throws IOException { + DataLakeFileClient fileClient = AZURITE_CONTAINER.fileClient(randomPath()); + ADLSOutputStream stream = + new ADLSOutputStream(fileClient, azureProperties, MetricsContext.nullMetrics()); + stream.close(); + stream.close(); + } + + private void writeAndVerify(byte[] data, boolean arrayWrite) { + String path = randomPath(); + DataLakeFileClient fileClient = AZURITE_CONTAINER.fileClient(path); + + try (ADLSOutputStream stream = + new ADLSOutputStream(fileClient, azureProperties, MetricsContext.nullMetrics())) { + if (arrayWrite) { + stream.write(data); + assertThat(stream.getPos()).isEqualTo(data.length); + } else { + for (int i = 0; i < data.length; i++) { + stream.write(data[i]); + assertThat(stream.getPos()).isEqualTo(i + 1); + } + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + + byte[] actual = new byte[data.length]; + + try (InputStream in = fileClient.openInputStream().getInputStream()) { + IOUtil.readFully(in, actual, 0, data.length); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + + assertThat(actual).isEqualTo(data); + } + + private String randomPath() { + return "dir/" + UUID.randomUUID(); Review Comment: would it make sense to use `@TempDir private Path temp;` rather than this method? ########## azure/src/main/java/org/apache/iceberg/azure/adlsv2/ADLSOutputStream.java: ########## @@ -0,0 +1,121 @@ +/* + * 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.iceberg.azure.adlsv2; + +import com.azure.storage.common.ParallelTransferOptions; +import com.azure.storage.file.datalake.DataLakeFileClient; +import com.azure.storage.file.datalake.options.DataLakeFileOutputStreamOptions; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Arrays; +import org.apache.iceberg.azure.AzureProperties; +import org.apache.iceberg.io.FileIOMetricsContext; +import org.apache.iceberg.io.PositionOutputStream; +import org.apache.iceberg.metrics.Counter; +import org.apache.iceberg.metrics.MetricsContext; +import org.apache.iceberg.metrics.MetricsContext.Unit; +import org.apache.iceberg.relocated.com.google.common.base.Joiner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class ADLSOutputStream extends PositionOutputStream { + private static final Logger LOG = LoggerFactory.getLogger(ADLSOutputStream.class); + + private final StackTraceElement[] createStack; + private final DataLakeFileClient fileClient; + private final AzureProperties azureProperties; + + private OutputStream stream; + + private final Counter writeBytes; + private final Counter writeOperations; + + private long pos; + private boolean closed; + + ADLSOutputStream( + DataLakeFileClient fileClient, AzureProperties azureProperties, MetricsContext metrics) + throws IOException { + this.fileClient = fileClient; + this.azureProperties = azureProperties; + + this.createStack = Thread.currentThread().getStackTrace(); + + this.writeBytes = metrics.counter(FileIOMetricsContext.WRITE_BYTES, Unit.BYTES); + this.writeOperations = metrics.counter(FileIOMetricsContext.WRITE_OPERATIONS); + + openStream(); + } + + @Override + public long getPos() { + return pos; + } + + @Override + public void flush() throws IOException { + stream.flush(); + } + + @Override + public void write(int b) throws IOException { + stream.write(b); + pos += 1; + writeBytes.increment(); + writeOperations.increment(); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + stream.write(b, off, len); + pos += len; + writeBytes.increment(len); + writeOperations.increment(); + } + + private void openStream() { + DataLakeFileOutputStreamOptions options = new DataLakeFileOutputStreamOptions(); + ParallelTransferOptions transferOptions = new ParallelTransferOptions(); + azureProperties.adlsWriteBlockSize().ifPresent(transferOptions::setBlockSizeLong); + this.stream = new BufferedOutputStream(fileClient.getOutputStream(options)); + } + + @Override + public void close() throws IOException { + if (closed) { + return; + } + + super.close(); + this.closed = true; + stream.close(); Review Comment: also do we need a null check against `stream`? ########## azure/src/main/java/org/apache/iceberg/azure/adlsv2/BaseADLSFile.java: ########## @@ -0,0 +1,66 @@ +/* + * 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.iceberg.azure.adlsv2; + +import com.azure.storage.file.datalake.DataLakeFileClient; +import org.apache.iceberg.azure.AzureProperties; +import org.apache.iceberg.metrics.MetricsContext; + +abstract class BaseADLSFile { + private final String location; + private final DataLakeFileClient fileClient; + private final AzureProperties azureProperties; + private final MetricsContext metrics; + + BaseADLSFile( + String location, + DataLakeFileClient fileClient, + AzureProperties azureProperties, + MetricsContext metrics) { + this.location = location; Review Comment: do we need to check any of these for nullability? ########## azure/src/main/java/org/apache/iceberg/azure/adlsv2/ADLSOutputStream.java: ########## @@ -0,0 +1,121 @@ +/* + * 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.iceberg.azure.adlsv2; + +import com.azure.storage.common.ParallelTransferOptions; +import com.azure.storage.file.datalake.DataLakeFileClient; +import com.azure.storage.file.datalake.options.DataLakeFileOutputStreamOptions; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Arrays; +import org.apache.iceberg.azure.AzureProperties; +import org.apache.iceberg.io.FileIOMetricsContext; +import org.apache.iceberg.io.PositionOutputStream; +import org.apache.iceberg.metrics.Counter; +import org.apache.iceberg.metrics.MetricsContext; +import org.apache.iceberg.metrics.MetricsContext.Unit; +import org.apache.iceberg.relocated.com.google.common.base.Joiner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class ADLSOutputStream extends PositionOutputStream { + private static final Logger LOG = LoggerFactory.getLogger(ADLSOutputStream.class); + + private final StackTraceElement[] createStack; + private final DataLakeFileClient fileClient; + private final AzureProperties azureProperties; + + private OutputStream stream; + + private final Counter writeBytes; + private final Counter writeOperations; + + private long pos; + private boolean closed; + + ADLSOutputStream( + DataLakeFileClient fileClient, AzureProperties azureProperties, MetricsContext metrics) + throws IOException { + this.fileClient = fileClient; + this.azureProperties = azureProperties; + + this.createStack = Thread.currentThread().getStackTrace(); + + this.writeBytes = metrics.counter(FileIOMetricsContext.WRITE_BYTES, Unit.BYTES); + this.writeOperations = metrics.counter(FileIOMetricsContext.WRITE_OPERATIONS); + + openStream(); + } + + @Override + public long getPos() { + return pos; + } + + @Override + public void flush() throws IOException { + stream.flush(); + } + + @Override + public void write(int b) throws IOException { + stream.write(b); + pos += 1; + writeBytes.increment(); + writeOperations.increment(); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + stream.write(b, off, len); + pos += len; + writeBytes.increment(len); + writeOperations.increment(); + } + + private void openStream() { + DataLakeFileOutputStreamOptions options = new DataLakeFileOutputStreamOptions(); + ParallelTransferOptions transferOptions = new ParallelTransferOptions(); + azureProperties.adlsWriteBlockSize().ifPresent(transferOptions::setBlockSizeLong); + this.stream = new BufferedOutputStream(fileClient.getOutputStream(options)); + } + + @Override + public void close() throws IOException { + if (closed) { + return; + } + + super.close(); + this.closed = true; + stream.close(); Review Comment: I wonder if this should be in a try-catch in case closing fails -- 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]
