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

feat: impl configurable OperatorOutputStream maxBytes #5422

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -35,31 +35,38 @@ protected void disposeInternal(long handle) {
}
}

private static final int MAX_BYTES = 16384;
private static final int DEFAULT_MAX_BYTES = 16384;

private final Writer writer;
private final byte[] bytes = new byte[MAX_BYTES];
private final byte[] bytes;
private final int maxBytes;

private int offset = 0;

public OperatorOutputStream(Operator operator, String path) {
this(operator, path, DEFAULT_MAX_BYTES);
}

public OperatorOutputStream(Operator operator, String path, int maxBytes) {
final long op = operator.nativeHandle;
this.writer = new Writer(constructWriter(op, path));
this.maxBytes = maxBytes;
this.bytes = new byte[maxBytes];
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void write(int b) throws IOException {
bytes[offset++] = (byte) b;
if (offset >= MAX_BYTES) {
if (offset >= maxBytes) {
flush();
}
}

@Override
public void flush() throws IOException {
if (offset > MAX_BYTES) {
throw new IOException("INTERNAL ERROR: " + offset + " > " + MAX_BYTES);
} else if (offset < MAX_BYTES) {
if (offset > maxBytes) {
throw new IOException("INTERNAL ERROR: " + offset + " > " + maxBytes);
} else if (offset < maxBytes) {
final byte[] bytes = Arrays.copyOf(this.bytes, offset);
writeBytes(writer.nativeHandle, bytes);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
public class BehaviorExtension implements BeforeAllCallback, AfterAllCallback, TestWatcher {
private String testName;

public String scheme;
public AsyncOperator asyncOperator;
public Operator operator;

Expand Down Expand Up @@ -67,6 +68,7 @@ public void beforeAll(ExtensionContext context) {
this.asyncOperator = op.layer(RetryLayer.builder().build());
this.operator = this.asyncOperator.blocking();

this.scheme = scheme;
this.testName = String.format("%s(%s)", context.getDisplayName(), scheme);
log.info(
"\n================================================================================"
Expand Down Expand Up @@ -94,6 +96,7 @@ public void afterAll(ExtensionContext context) {
operator = null;
}

this.scheme = null;
this.testName = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ protected Operator op() {
return behaviorExtension.operator;
}

protected String scheme() {
return behaviorExtension.scheme;
}

/**
* Generates a byte array of random content.
*/
Expand All @@ -57,6 +61,16 @@ public static byte[] generateBytes() {
return content;
}

/**
* Generates a byte array of random content with a specific size.
*/
public static byte[] generateBytes(int size) {
final Random random = new Random();
final byte[] content = new byte[size];
random.nextBytes(content);
return content;
}

/**
* Calculate SHA256 digest of input bytes
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.opendal.test.behavior;

import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.util.UUID;
import org.apache.opendal.OperatorOutputStream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class RegressionTest extends BehaviorTestBase {
// @see https://github.com/apache/opendal/issues/5421
@Test
public void testAzblobLargeFile() throws Exception {
assumeTrue(scheme() != null && scheme().equalsIgnoreCase("azblob"));

final String path = UUID.randomUUID().toString();
final int size = 16384 * 10; // 10 x OperatorOutputStream.DEFAULT_MAX_BYTES (10 flushes per write)
final byte[] content = generateBytes(size);

try (OperatorOutputStream operatorOutputStream = op().createOutputStream(path)) {
for (int i = 0; i < 20000; i++) {
// More iterations in case BlockCountExceedsLimit doesn't pop up exactly after 100K blocks.
operatorOutputStream.write(content);
}
}
}
}
Loading