2222import com .google .cloud .hadoop .util .AccessTokenProvider ;
2323import com .google .cloud .hadoop .util .CredentialFactory ;
2424import dev .failsafe .Failsafe ;
25+ import dev .failsafe .FailsafeException ;
2526import dev .failsafe .RetryPolicy ;
2627import io .cdap .cdap .api .exception .ErrorCategory ;
2728import io .cdap .cdap .api .exception .ErrorCategory .ErrorCategoryEnum ;
2829import io .cdap .cdap .api .exception .ErrorType ;
2930import io .cdap .cdap .api .exception .ErrorUtils ;
30- import io .cdap .plugin .gcp .bigquery .source .BigQuerySourceConfig ;
31- import io .cdap .plugin .gcp .bigquery .util .BigQueryConstants ;
31+ import io .cdap .plugin .gcp .common .GCPErrorDetailsProviderUtil ;
3232import io .cdap .plugin .gcp .common .GCPUtils ;
3333import io .cdap .plugin .gcp .common .ServerErrorException ;
3434import org .apache .hadoop .conf .Configuration ;
@@ -52,53 +52,47 @@ public class ServiceAccountAccessTokenProvider implements AccessTokenProvider {
5252 private Configuration conf ;
5353 private GoogleCredentials credentials ;
5454 private static final Gson GSON = new Gson ();
55- private static final Logger logger = LoggerFactory .getLogger (ServiceAccountAccessTokenProvider .class );
55+ private static final Logger LOG = LoggerFactory .getLogger (ServiceAccountAccessTokenProvider .class );
5656 public static final int DEFAULT_INITIAL_RETRY_DURATION_SECONDS = 5 ;
5757 public static final int DEFAULT_MAX_RETRY_COUNT = 5 ;
5858 public static final int DEFAULT_MAX_RETRY_DURATION_SECONDS = 80 ;
59-
59+ private static final RetryPolicy < Object > RETRY_POLICY = createRetryPolicy ();
6060
6161 @ Override
6262 public AccessToken getAccessToken () {
63- int initialRetryDuration = DEFAULT_INITIAL_RETRY_DURATION_SECONDS ;
64- int maxRetryCount = DEFAULT_MAX_RETRY_COUNT ;
65- int maxRetryDuration = DEFAULT_MAX_RETRY_DURATION_SECONDS ;
66- logger .debug (
63+ LOG .debug (
6764 "Initializing RetryPolicy with the following configuration: MaxRetryCount: {}, InitialRetryDuration: {}s, " +
68- "MaxRetryDuration: {}s" , maxRetryCount , initialRetryDuration , maxRetryDuration );
65+ "MaxRetryDuration: {}s" , DEFAULT_MAX_RETRY_COUNT , DEFAULT_INITIAL_RETRY_DURATION_SECONDS ,
66+ DEFAULT_MAX_RETRY_DURATION_SECONDS );
6967 try {
70- return Failsafe .with (getRetryPolicy (initialRetryDuration , maxRetryDuration , maxRetryCount ))
71- .get (() -> {
68+ return Failsafe .with (RETRY_POLICY ).get (() -> {
7269 com .google .auth .oauth2 .AccessToken token = safeGetAccessToken ();
7370 if (token == null || token .getExpirationTime ().before (Date .from (Instant .now ()))) {
7471 refresh ();
7572 token = safeGetAccessToken ();
7673 }
7774 return new AccessToken (token .getTokenValue (), token .getExpirationTime ().getTime ());
7875 });
79- } catch (Exception e ) {
80- throw ErrorUtils .getProgramFailureException (
81- new ErrorCategory (ErrorCategoryEnum .PLUGIN ),
82- "Unable to get service account access token after retries." ,
83- e .getMessage (),
84- ErrorType .UNKNOWN ,
85- true ,
86- e
76+ } catch (FailsafeException e ) {
77+ Throwable t = e .getCause () != null ? e .getCause () : e ;
78+ ErrorType errorType = (t instanceof ServerErrorException ) ? ErrorType .SYSTEM : ErrorType .UNKNOWN ;
79+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (
80+ e , "Unable to get service account access token after retries." , errorType , true ,
81+ GCPUtils .SERVER_ERROR_SUPPORTED_DOC_URL
8782 );
8883 }
8984 }
9085
91-
92- private RetryPolicy <Object > getRetryPolicy (int initialRetryDuration , int maxRetryDuration ,
93- int maxRetryCount ) {
86+ private static RetryPolicy <Object > createRetryPolicy () {
9487 return RetryPolicy .builder ()
9588 .handle (ServerErrorException .class )
96- .withBackoff (Duration .ofSeconds (initialRetryDuration ), Duration .ofSeconds (maxRetryDuration ))
97- .withMaxRetries (maxRetryCount )
98- .onRetry (event -> logger .debug ("Retry attempt {} due to {}" , event .getAttemptCount (), event .getLastException ().
89+ .withBackoff (Duration .ofSeconds (DEFAULT_INITIAL_RETRY_DURATION_SECONDS ),
90+ Duration .ofSeconds (DEFAULT_MAX_RETRY_DURATION_SECONDS ))
91+ .withMaxRetries (DEFAULT_MAX_RETRY_COUNT )
92+ .onRetry (event -> LOG .debug ("Retry attempt {} due to {}" , event .getAttemptCount (), event .getLastException ().
9993 getMessage ()))
100- .onSuccess (event -> logger .debug ("Access Token Fetched Successfully." ))
101- .onRetriesExceeded (event -> logger .error ("Retry limit reached for Service account." ))
94+ .onSuccess (event -> LOG .debug ("Access Token Fetched Successfully." ))
95+ .onRetriesExceeded (event -> LOG .error ("Retry limit reached for Service account." ))
10296 .build ();
10397 }
10498
@@ -113,7 +107,7 @@ private com.google.auth.oauth2.AccessToken safeGetAccessToken() throws IOExcepti
113107 } catch (IOException e ) {
114108 if (isServerError (e )) {
115109 throw new ServerErrorException (HttpStatus .SC_SERVICE_UNAVAILABLE , "Server error while fetching access token: "
116- + e .getMessage ());
110+ + e .getMessage (), e );
117111 }
118112 throw e ;
119113 }
@@ -126,11 +120,11 @@ public void refresh() throws IOException {
126120 } catch (IOException e ) {
127121 if (isServerError (e )) {
128122 throw new ServerErrorException (HttpStatus .SC_SERVICE_UNAVAILABLE , "Server error during refresh: " +
129- e .getMessage ());
123+ e .getMessage (), e );
130124 }
131- throw ErrorUtils . getProgramFailureException ( new ErrorCategory ( ErrorCategoryEnum . PLUGIN ),
132- "Unable to refresh service account access token." , e . getMessage () ,
133- ErrorType . UNKNOWN , true , e );
125+ throw GCPErrorDetailsProviderUtil . getHttpResponseExceptionDetailsFromChain (
126+ e , "Unable to refresh service account access token." , ErrorType . UNKNOWN , true ,
127+ GCPUtils . SERVER_ERROR_SUPPORTED_DOC_URL );
134128 }
135129 }
136130
0 commit comments