-
Notifications
You must be signed in to change notification settings - Fork 9
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
Implement sidecar streaming read to accommodate the tracking report digest computing #64
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,52 +16,135 @@ | |
package org.commonjava.util.sidecar.util; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import org.apache.commons.io.IOUtils; | ||
import okhttp3.ResponseBody; | ||
import okio.BufferedSource; | ||
import org.apache.commons.io.output.CountingOutputStream; | ||
import org.commonjava.util.sidecar.model.TrackedContentEntry; | ||
import org.commonjava.util.sidecar.services.ReportService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.ws.rs.core.StreamingOutput; | ||
import javax.xml.bind.DatatypeConverter; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ProxyStreamingOutput | ||
implements StreamingOutput | ||
{ | ||
private final Logger logger = LoggerFactory.getLogger( getClass() ); | ||
|
||
private final InputStream bodyStream; | ||
private static final String MD5 = "MD5"; | ||
|
||
private static final String SHA1 = "SHA-1"; | ||
|
||
private static final String SHA256 = "SHA-256"; | ||
|
||
private static final String[] DIGESTS = { MD5, SHA1, SHA256 }; | ||
|
||
private static final long bufSize = 10 * 1024 * 1024; | ||
|
||
private final ResponseBody responseBody; | ||
|
||
private final TrackedContentEntry entry; | ||
|
||
private final String serviceOrigin; | ||
|
||
private final String indyOrigin; | ||
|
||
private final ReportService reportService; | ||
|
||
private final OtelAdapter otel; | ||
|
||
public ProxyStreamingOutput( InputStream bodyStream, OtelAdapter otel ) | ||
private final Map<String, MessageDigest> digests = new HashMap<>(); | ||
|
||
public ProxyStreamingOutput( ResponseBody responseBody, TrackedContentEntry entry, String serviceOrigin, | ||
String indyOrigin, ReportService reportService, OtelAdapter otel ) | ||
{ | ||
this.bodyStream = bodyStream; | ||
this.responseBody = responseBody; | ||
this.entry = entry; | ||
this.serviceOrigin = serviceOrigin; | ||
this.indyOrigin = indyOrigin; | ||
this.reportService = reportService; | ||
this.otel = otel; | ||
|
||
for ( String key : DIGESTS ) | ||
{ | ||
try | ||
{ | ||
digests.put( key, MessageDigest.getInstance( key ) ); | ||
} | ||
catch ( NoSuchAlgorithmException e ) | ||
{ | ||
logger.warn( "Bytes hash calculation failed for request. Cannot get digest of type: {}", key ); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void write( OutputStream output ) throws IOException | ||
{ | ||
if ( bodyStream != null ) | ||
if ( responseBody != null ) | ||
{ | ||
try | ||
try (CountingOutputStream cout = new CountingOutputStream( output )) | ||
{ | ||
OutputStream out = output; | ||
CountingOutputStream cout = new CountingOutputStream( out ); | ||
out = cout; | ||
logger.trace( "Copying from: {} to: {}", bodyStream, out ); | ||
IOUtils.copy( bodyStream, out ); | ||
|
||
OutputStream out = cout; | ||
BufferedSource peek = responseBody.source().peek(); | ||
while ( !peek.exhausted() ) | ||
{ | ||
byte[] bytes; | ||
if ( peek.request( bufSize ) ) | ||
{ | ||
bytes = peek.readByteArray( | ||
bufSize ); // byteCount bytes will be removed from current buffer after read | ||
} | ||
else | ||
{ | ||
bytes = peek.readByteArray(); | ||
} | ||
out.write( bytes ); | ||
if ( entry != null ) | ||
{ | ||
digests.values().forEach( d -> d.update( bytes ) ); | ||
} | ||
} | ||
out.flush(); | ||
peek.close(); | ||
if ( otel.enabled() ) | ||
{ | ||
Span.current().setAttribute( "response.content_length", cout.getByteCount() ); | ||
} | ||
if ( entry != null ) | ||
{ | ||
entry.setSize( cout.getByteCount() ); | ||
String[] headers = indyOrigin.split( ":" ); | ||
entry.setOriginUrl( | ||
serviceOrigin + "/api/content/" + headers[0] + "/" + headers[1] + "/" + headers[2] | ||
+ entry.getPath() ); | ||
if ( digests.containsKey( MD5 ) ) | ||
entry.setMd5( DatatypeConverter.printHexBinary( digests.get( MD5 ).digest() ).toLowerCase() ); | ||
|
||
if ( digests.containsKey( SHA1 ) ) | ||
entry.setSha1( DatatypeConverter.printHexBinary( digests.get( SHA1 ).digest() ).toLowerCase() ); | ||
|
||
if ( digests.containsKey( SHA256 ) ) | ||
entry.setSha256( DatatypeConverter.printHexBinary( digests.get( SHA256 ).digest() ) | ||
.toLowerCase() ); | ||
|
||
reportService.appendDownload( entry ); | ||
} | ||
} | ||
finally | ||
{ | ||
closeBodyStream( bodyStream ); | ||
if ( responseBody == null ) | ||
{ | ||
return; | ||
} | ||
responseBody.close(); | ||
} | ||
} | ||
else | ||
|
@@ -72,26 +155,4 @@ public void write( OutputStream output ) throws IOException | |
} | ||
} | ||
} | ||
|
||
private void closeBodyStream( InputStream is ) | ||
{ | ||
if ( is == null ) | ||
{ | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
is.close(); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
if ( otel.enabled() ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this possible to happen in the new okhttp3 way? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ligangty it's verified, IOException is never thrown in responsebody.close(). |
||
{ | ||
Span.current().setAttribute( "body.ignored_error_class", e.getClass().getSimpleName() ); | ||
Span.current().setAttribute( "body.ignored_error_class", e.getMessage() ); | ||
} | ||
logger.trace( "Failed to close body stream in proxy response.", e ); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this out needed? Why not use cout directly here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ligangty This is the same with the previously removed code, I think what John considered here might be out is used for writing, cout is used for bytes count.