markrmiller commented on code in PR #3341: URL: https://github.com/apache/solr/pull/3341#discussion_r2070444044
########## solr/solrj/src/java/org/apache/solr/common/util/ResumableInputStream.java: ########## @@ -0,0 +1,119 @@ +/* + * 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.solr.common.util; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.invoke.MethodHandles; +import java.util.function.BiFunction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * An {@link InputStream} that can be resumed when the connection that is driving the input is + * interrupted. + */ +public class ResumableInputStream extends InputStream { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private final long contentLength; + private long bytesRead; + private long markedBytesRead; + private final BiFunction<Long, Long, InputStream> nextInputStreamSupplier; + private InputStream delegate; + + /** + * Create a new ResumableInputStream + * + * @param delegate The original {@link InputStream} that will be used as a delegate + * @param contentLength The full length of the content being read. + * @param nextInputStreamSupplier A function to create the next InputStream given the number of + * bytes already read and the total content length of the input. These inputs can, for + * example, be used to populate the <a + * href="https://www.rfc-editor.org/rfc/rfc9110.html#name-range">HTTP Range header</a>. + */ + public ResumableInputStream( + InputStream delegate, + long contentLength, + BiFunction<Long, Long, InputStream> nextInputStreamSupplier) { + this.delegate = delegate; + this.nextInputStreamSupplier = nextInputStreamSupplier; + this.contentLength = contentLength; + bytesRead = 0; + markedBytesRead = 0; + } + + @Override + public int read() throws IOException { + return read(false); + } + + public int read(boolean isRetry) throws IOException { + if (delegate == null) { + delegate = nextInputStreamSupplier.apply(bytesRead, contentLength); + } + int val; + try { + val = delegate.read(); + bytesRead += 1; + } catch (IOException e) { + // Only retry once on a single read + if (isRetry) { + throw e; + } + log.warn( + "Exception thrown while consuming InputStream, retrying from byte: {}", bytesRead, e); + resetInputStream(); + val = read(true); + } + return val; + } + + @Override + public boolean markSupported() { + return true; + } + + @Override + public void mark(int readlimit) { + markedBytesRead = bytesRead; + } + + @Override + public int available() { Review Comment: Prevent overflow when over 2G: ` @Override public int available() { long remaining = (contentLength < 0) ? 0 : contentLength - bytesRead; return (int) Math.min(Integer.MAX_VALUE, Math.max(0, remaining)); } ` ########## solr/solrj/src/java/org/apache/solr/common/util/ResumableInputStream.java: ########## @@ -0,0 +1,119 @@ +/* + * 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.solr.common.util; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.invoke.MethodHandles; +import java.util.function.BiFunction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * An {@link InputStream} that can be resumed when the connection that is driving the input is + * interrupted. + */ +public class ResumableInputStream extends InputStream { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private final long contentLength; + private long bytesRead; + private long markedBytesRead; + private final BiFunction<Long, Long, InputStream> nextInputStreamSupplier; + private InputStream delegate; + + /** + * Create a new ResumableInputStream + * + * @param delegate The original {@link InputStream} that will be used as a delegate + * @param contentLength The full length of the content being read. + * @param nextInputStreamSupplier A function to create the next InputStream given the number of + * bytes already read and the total content length of the input. These inputs can, for + * example, be used to populate the <a + * href="https://www.rfc-editor.org/rfc/rfc9110.html#name-range">HTTP Range header</a>. + */ + public ResumableInputStream( + InputStream delegate, + long contentLength, + BiFunction<Long, Long, InputStream> nextInputStreamSupplier) { + this.delegate = delegate; + this.nextInputStreamSupplier = nextInputStreamSupplier; + this.contentLength = contentLength; + bytesRead = 0; + markedBytesRead = 0; + } + + @Override + public int read() throws IOException { + return read(false); + } + + public int read(boolean isRetry) throws IOException { + if (delegate == null) { + delegate = nextInputStreamSupplier.apply(bytesRead, contentLength); + } + int val; + try { + val = delegate.read(); + bytesRead += 1; Review Comment: Don't increment when delegate returns -1. ########## solr/solrj/src/java/org/apache/solr/common/util/ResumableInputStream.java: ########## @@ -0,0 +1,119 @@ +/* + * 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.solr.common.util; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.invoke.MethodHandles; +import java.util.function.BiFunction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * An {@link InputStream} that can be resumed when the connection that is driving the input is + * interrupted. + */ +public class ResumableInputStream extends InputStream { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private final long contentLength; + private long bytesRead; + private long markedBytesRead; + private final BiFunction<Long, Long, InputStream> nextInputStreamSupplier; + private InputStream delegate; + + /** + * Create a new ResumableInputStream + * + * @param delegate The original {@link InputStream} that will be used as a delegate + * @param contentLength The full length of the content being read. + * @param nextInputStreamSupplier A function to create the next InputStream given the number of + * bytes already read and the total content length of the input. These inputs can, for + * example, be used to populate the <a + * href="https://www.rfc-editor.org/rfc/rfc9110.html#name-range">HTTP Range header</a>. + */ + public ResumableInputStream( + InputStream delegate, + long contentLength, + BiFunction<Long, Long, InputStream> nextInputStreamSupplier) { + this.delegate = delegate; + this.nextInputStreamSupplier = nextInputStreamSupplier; + this.contentLength = contentLength; + bytesRead = 0; + markedBytesRead = 0; + } + + @Override + public int read() throws IOException { + return read(false); + } + + public int read(boolean isRetry) throws IOException { Review Comment: If you don't override bulk read, you get slow byte-by-byte reading for any code using buffered reads, and the retry logic will apply per byte instead of per buffer attempt. ########## solr/modules/s3-repository/src/java/org/apache/solr/s3/S3StorageClient.java: ########## @@ -374,8 +377,23 @@ InputStream pullStream(String path) throws S3Exception { final String s3Path = sanitizedFilePath(path); try { + GetObjectRequest.Builder getBuilder = + GetObjectRequest.builder().bucket(bucketName).key(s3Path); // This InputStream instance needs to be closed by the caller - return s3Client.getObject(b -> b.bucket(bucketName).key(s3Path)); + return s3Client.getObject( + getBuilder.build(), + ResponseTransformer.unmanaged( + (response, inputStream) -> + new ResumableInputStream( + inputStream, + response.contentLength(), + (bytesRead, contentLength) -> { + if (bytesRead > 0) { + getBuilder.range( + String.format(Locale.ROOT, "bytes=%d-%d", bytesRead, contentLength)); Review Comment: Don't you need contentLength - 1 since it's 0 based? Wouldn't `range(String.format("bytes=%d-", bytesRead));` be better here? -- 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: issues-unsubscr...@solr.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org