Skip to content

Commit

Permalink
Merge pull request #435 from yma96/master
Browse files Browse the repository at this point in the history
Add proxy as alternative way to do transfer http jobs
  • Loading branch information
yma96 authored Aug 21, 2024
2 parents c8a969a + 96d539f commit b4faac4
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public interface Http
extends Closeable
{

CloseableHttpClient createClient( HttpLocation location, boolean isProxy )
throws GalleyException;

CloseableHttpClient createClient( HttpLocation location )
throws GalleyException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,39 +87,50 @@ public CloseableHttpClient createClient()
@Override
public CloseableHttpClient createClient( final HttpLocation location )
throws GalleyException
{
// Default to not use proxy
return createClient( location, false );
}

@Override
public CloseableHttpClient createClient( final HttpLocation location, final boolean isProxy )
throws GalleyException
{
try
{
if ( location != null )
{
logger.debug( "The location class: {}", location.getClass().getSimpleName() );
if ( location instanceof WrapperHttpLocation )
{
WrapperHttpLocation wrapper = (WrapperHttpLocation) location;
logger.debug( "WrapperHttpLocation:{}, isProxyAllowHttpJobType:{}", wrapper,
wrapper.isProxyAllowHttpJobType() );
}
locationLookup.register( location );

int maxConnections = LocationUtils.getMaxConnections( location );
SiteConfigBuilder configBuilder = new SiteConfigBuilder( location.getName(), location.getUri() );
configBuilder.withAttributes( location.getAttributes() )
.withKeyCertPem( location.getKeyCertPem() )
.withServerCertPem( location.getServerCertPem() )
.withProxyHost( location.getProxyHost() )
.withProxyPort( location.getProxyPort() )
.withProxyUser( location.getProxyUser() )
.withRequestTimeoutSeconds( LocationUtils.getTimeoutSeconds( location ) )
.withUser( location.getUser() )
.withIgnoreHostnameVerification( location.isIgnoreHostnameVerification() )
.withMaxConnections( maxConnections );
if ( isProxy )
{
logger.trace( "The location class: {}", location.getClass().getSimpleName() );
configBuilder.withProxyHost( location.getProxyHost() )
.withProxyPort( location.getProxyPort() )
.withProxyUser( location.getProxyUser() );
if ( location instanceof WrapperHttpLocation )
{
WrapperHttpLocation wrapper = (WrapperHttpLocation) location;
logger.debug(
"Proxy with the WrapperHttpLocation config: {}, isGlobalProxyAllowHttpJobType: {}",
wrapper, wrapper.isGlobalProxyAllowHttpJobType() );
}
}

if ( location.getTrustType() != null )
{
configBuilder.withTrustType( SiteTrustType.getType( location.getTrustType().name() ) );
}


SiteConfig config = configBuilder.build();

return httpFactory.createClient( config );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,36 +101,15 @@ public long getTransferSize()
protected boolean executeHttp()
throws TransferException
{
boolean result = true;
try
{
client = http.createClient( location );
response = client.execute( request, http.createContext( location ) );

final StatusLine line = response.getStatusLine();
final int sc = line.getStatusCode();

logger.trace( "{} {} : {}", request.getMethod(), line, url );

if ( sc > 399 && sc != 404 && sc != 408 && sc != 502 && sc != 503 && sc != 504 )
{
throw new TransferLocationException( location,
"Server misconfigured or not responding normally for url %s: '%s'",
url, line );
}
else if ( !successStatuses.contains( sc ) )
{
logger.trace( "Detected failure respon se: " + sc );
success = TransferResponseUtils.handleUnsuccessfulResponse( request, response, location, url );
logger.trace( "Returning non-error failure response for code: " + sc );
return false;
}
result = doExecuteHttp();
}
catch ( final NoHttpResponseException | ConnectTimeoutException | SocketTimeoutException e )
{
addFieldToActiveSpan( "target-error-reason", "timeout" );
addFieldToActiveSpan( "target-error", e.getClass().getSimpleName() );
throw new TransferTimeoutException( location, url, "Repository remote request failed for: {}. Reason: {}",
e, url, e.getMessage() );
// For those are not in the iad2 tenant egress rules, will throw timeout so use the configured proxy to retry
result = executeProxyHttp();
}
catch ( final IOException e )
{
Expand Down Expand Up @@ -171,6 +150,75 @@ else if ( !successStatuses.contains( sc ) )
}
}

return result;
}

protected boolean executeProxyHttp()
throws TransferException
{
try
{
return doExecuteHttp( true );
}
catch ( final NoHttpResponseException | ConnectTimeoutException | SocketTimeoutException e )
{
addFieldToActiveSpan( "target-error-reason", "timeout" );
addFieldToActiveSpan( "target-error", e.getClass().getSimpleName() );
throw new TransferTimeoutException( location, url, "Repository remote request failed for: {}. Reason: {}",
e, url, e.getMessage() );
}
catch ( final IOException e )
{
addFieldToActiveSpan( "target-error-reason", "I/O" );
addFieldToActiveSpan( "target-error", e.getClass().getSimpleName() );
throw new TransferLocationException( location, "Repository remote request failed for: {}. Reason: {}", e,
url, e.getMessage() );
}
catch ( TransferLocationException e )
{
addFieldToActiveSpan( "target-error-reason", "no transport" );
addFieldToActiveSpan( "target-error", e.getClass().getSimpleName() );
throw e;
}
catch ( final GalleyException e )
{
addFieldToActiveSpan( "target-error-reason", "unknown" );
addFieldToActiveSpan( "target-error", e.getClass().getSimpleName() );
throw new TransferException( "Repository remote request failed for: {}. Reason: {}", e, url,
e.getMessage() );
}
}

private boolean doExecuteHttp()
throws GalleyException, IOException
{
return doExecuteHttp( false );
}

private boolean doExecuteHttp( boolean isProxy )
throws GalleyException, IOException
{
client = http.createClient( location, isProxy );
response = client.execute( request, http.createContext( location ) );

final StatusLine line = response.getStatusLine();
final int sc = line.getStatusCode();

logger.trace( "{} {} : {}", request.getMethod(), line, url );

if ( sc > 399 && sc != 404 && sc != 408 && sc != 502 && sc != 503 && sc != 504 )
{
throw new TransferLocationException( location,
"Server misconfigured or not responding normally for url %s: '%s'",
url, line );
}
else if ( !successStatuses.contains( sc ) )
{
logger.trace( "Detected failure respon se: " + sc );
success = TransferResponseUtils.handleUnsuccessfulResponse( request, response, location, url );
logger.trace( "Returning non-error failure response for code: " + sc );
return false;
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,24 @@ public String getUser()
public String getProxyHost()
{
GlobalProxyConfig proxy = getGlobalProxyConfig();
return isProxyAllowHttpJobType() ? proxy.getHost() : null;
String proxyHost = delegate instanceof HttpLocation ? ( (HttpLocation) delegate ).getProxyHost() : null;
return isGlobalProxyAllowHttpJobType() ? proxy.getHost() : proxyHost;
}

@Override
public String getProxyUser()
{
GlobalProxyConfig proxy = getGlobalProxyConfig();
return isProxyAllowHttpJobType() ? proxy.getUser() : null;
String proxyUser = delegate instanceof HttpLocation ? ( (HttpLocation) delegate ).getProxyUser() : null;
return isGlobalProxyAllowHttpJobType() ? proxy.getUser() : proxyUser;
}

@Override
public int getProxyPort()
{
GlobalProxyConfig proxy = getGlobalProxyConfig();
return isProxyAllowHttpJobType() ? proxy.getPort() : 8080;
int proxyPort = delegate instanceof HttpLocation ? ( (HttpLocation) delegate ).getProxyPort() : 8080;
return isGlobalProxyAllowHttpJobType() ? proxy.getPort() : proxyPort;
}

@Override
Expand Down Expand Up @@ -221,7 +224,7 @@ public GlobalProxyConfig getGlobalProxyConfig()
return globalProxyConfig;
}

public boolean isProxyAllowHttpJobType()
public boolean isGlobalProxyAllowHttpJobType()
{
if ( globalProxyConfig == null )
{
Expand Down

0 comments on commit b4faac4

Please sign in to comment.