2020import static feast .serving .store .bigquery .QueryTemplater .generateFullTableName ;
2121import static feast .serving .util .Metrics .requestLatency ;
2222
23+ import com .google .cloud .RetryOption ;
2324import com .google .cloud .bigquery .BigQuery ;
2425import com .google .cloud .bigquery .BigQueryException ;
2526import com .google .cloud .bigquery .Field ;
5758import java .util .Optional ;
5859import java .util .UUID ;
5960import java .util .stream .Collectors ;
60- import org .joda .time .Duration ;
6161import org .slf4j .Logger ;
62+ import org .threeten .bp .Duration ;
6263
6364public class BigQueryServingService implements ServingService {
6465
65- public static final long TEMP_TABLE_EXPIRY_DURATION_MS = Duration .standardDays (1 ).getMillis ();
66+ public static final long TEMP_TABLE_EXPIRY_DURATION_MS = Duration .ofDays (1 ).toMillis ();
6667 private static final Logger log = org .slf4j .LoggerFactory .getLogger (BigQueryServingService .class );
6768
6869 private final BigQuery bigquery ;
@@ -71,6 +72,8 @@ public class BigQueryServingService implements ServingService {
7172 private final CachedSpecService specService ;
7273 private final JobService jobService ;
7374 private final String jobStagingLocation ;
75+ private final int initialRetryDelaySecs ;
76+ private final int totalTimeoutSecs ;
7477 private final Storage storage ;
7578
7679 public BigQueryServingService (
@@ -80,13 +83,17 @@ public BigQueryServingService(
8083 CachedSpecService specService ,
8184 JobService jobService ,
8285 String jobStagingLocation ,
86+ int initialRetryDelaySecs ,
87+ int totalTimeoutSecs ,
8388 Storage storage ) {
8489 this .bigquery = bigquery ;
8590 this .projectId = projectId ;
8691 this .datasetId = datasetId ;
8792 this .specService = specService ;
8893 this .jobService = jobService ;
8994 this .jobStagingLocation = jobStagingLocation ;
95+ this .initialRetryDelaySecs = initialRetryDelaySecs ;
96+ this .totalTimeoutSecs = totalTimeoutSecs ;
9097 this .storage = storage ;
9198 }
9299
@@ -156,6 +163,8 @@ public GetBatchFeaturesResponse getBatchFeatures(GetBatchFeaturesRequest getFeat
156163 .setEntityTableColumnNames (entityNames )
157164 .setFeatureSetInfos (featureSetInfos )
158165 .setJobStagingLocation (jobStagingLocation )
166+ .setInitialRetryDelaySecs (initialRetryDelaySecs )
167+ .setTotalTimeoutSecs (totalTimeoutSecs )
159168 .build ())
160169 .start ();
161170
@@ -199,7 +208,7 @@ private Table loadEntities(DatasetSource datasetSource) {
199208 loadJobConfiguration =
200209 loadJobConfiguration .toBuilder ().setUseAvroLogicalTypes (true ).build ();
201210 Job job = bigquery .create (JobInfo .of (loadJobConfiguration ));
202- job . waitFor ( );
211+ waitForJob ( job );
203212
204213 TableInfo expiry =
205214 bigquery
@@ -239,15 +248,15 @@ private TableId generateUUIDs(Table loadedEntityTable) {
239248 .setDestinationTable (TableId .of (projectId , datasetId , createTempTableName ()))
240249 .build ();
241250 Job queryJob = bigquery .create (JobInfo .of (queryJobConfig ));
242- queryJob . waitFor ( );
251+ Job completedJob = waitForJob ( queryJob );
243252 TableInfo expiry =
244253 bigquery
245254 .getTable (queryJobConfig .getDestinationTable ())
246255 .toBuilder ()
247256 .setExpirationTime (System .currentTimeMillis () + TEMP_TABLE_EXPIRY_DURATION_MS )
248257 .build ();
249258 bigquery .update (expiry );
250- queryJobConfig = queryJob .getConfiguration ();
259+ queryJobConfig = completedJob .getConfiguration ();
251260 return queryJobConfig .getDestinationTable ();
252261 } catch (InterruptedException | BigQueryException e ) {
253262 throw Status .INTERNAL
@@ -257,6 +266,22 @@ private TableId generateUUIDs(Table loadedEntityTable) {
257266 }
258267 }
259268
269+ private Job waitForJob (Job queryJob ) throws InterruptedException {
270+ Job completedJob = queryJob .waitFor (
271+ RetryOption .initialRetryDelay (Duration .ofSeconds (initialRetryDelaySecs )),
272+ RetryOption .totalTimeout (Duration .ofSeconds (totalTimeoutSecs )));
273+ if (completedJob == null ) {
274+ throw Status .INTERNAL
275+ .withDescription ("Job no longer exists" )
276+ .asRuntimeException ();
277+ } else if (completedJob .getStatus ().getError () != null ) {
278+ throw Status .INTERNAL
279+ .withDescription ("Job failed: " + completedJob .getStatus ().getError ())
280+ .asRuntimeException ();
281+ }
282+ return completedJob ;
283+ }
284+
260285 public static String createTempTableName () {
261286 return "_" + UUID .randomUUID ().toString ().replace ("-" , "" );
262287 }
0 commit comments