[ https://issues.apache.org/jira/browse/HIVE-23897?focusedWorklogId=464426&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-464426 ]
ASF GitHub Bot logged work on HIVE-23897: ----------------------------------------- Author: ASF GitHub Bot Created on: 30/Jul/20 08:29 Start Date: 30/Jul/20 08:29 Worklog Time Spent: 10m Work Description: aasha commented on a change in pull request #1329: URL: https://github.com/apache/hive/pull/1329#discussion_r462835685 ########## File path: ql/src/java/org/apache/hadoop/hive/ql/exec/util/Retryable.java ########## @@ -0,0 +1,212 @@ +/* + * 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.hadoop.hive.ql.exec.util; + +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.metastore.utils.SecurityUtils; +import org.apache.hadoop.security.UserGroupInformation; + +import java.security.PrivilegedExceptionAction; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; + +/** + * Class to implement any retry logic in case of exceptions. + */ +public class Retryable { + private static long MINIMUM_DELAY_IN_SEC = 1; + + private long totalDurationInSeconds; + private List<Class<? extends Exception>> retryOn; + private List<Class<? extends Exception>> failOn; + private long initialDelayInSeconds; + private long maxRetryDelayInSeconds; + private double backOff; + private int maxJitterInSeconds; + + private Retryable() { + this.retryOn = new ArrayList<>(); + this.failOn = new ArrayList<>(); + this.initialDelayInSeconds = HiveConf.toTime(HiveConf.ConfVars.REPL_RETRY_INTIAL_DELAY.defaultStrVal, + HiveConf.getDefaultTimeUnit(HiveConf.ConfVars.REPL_RETRY_INTIAL_DELAY), TimeUnit.SECONDS); + this.maxRetryDelayInSeconds = HiveConf.toTime(HiveConf.ConfVars.REPL_RETRY_MAX_DELAY_BETWEEN_RETRIES.defaultStrVal, + HiveConf.getDefaultTimeUnit(HiveConf.ConfVars.REPL_RETRY_MAX_DELAY_BETWEEN_RETRIES), TimeUnit.SECONDS); + this.backOff = HiveConf.ConfVars.REPL_RETRY_BACKOFF_COEFFICIENT.defaultFloatVal; + this.maxJitterInSeconds = (int) HiveConf.toTime(HiveConf.ConfVars.REPL_RETRY_JITTER.defaultStrVal, + HiveConf.getDefaultTimeUnit(HiveConf.ConfVars.REPL_RETRY_JITTER), TimeUnit.SECONDS); + this.totalDurationInSeconds = HiveConf.toTime(HiveConf.ConfVars.REPL_RETRY_TOTAL_DURATION.defaultStrVal, + HiveConf.getDefaultTimeUnit(HiveConf.ConfVars.REPL_RETRY_TOTAL_DURATION), TimeUnit.SECONDS);; + } + + public static Builder builder() { + return new Builder(); + } + + public <T> T executeCallable(Callable<T> callable) throws Exception { + long startTime = System.currentTimeMillis(); + long delay = this.initialDelayInSeconds; + Exception currentCapturedException = null; + while(true) { + try { + if (UserGroupInformation.isSecurityEnabled()) { + SecurityUtils.reloginExpiringKeytabUser(); + return UserGroupInformation.getLoginUser().doAs((PrivilegedExceptionAction<T>) () -> callable.call()); + } else { + return callable.call(); + } + } catch (Exception e) { + if (this.failOn.stream().noneMatch(k -> e.getClass().equals(k)) + && this.retryOn.stream().anyMatch(k -> e.getClass().isAssignableFrom(k))) { + if (elapsedTimeInSeconds(startTime) + delay > this.totalDurationInSeconds) { + // case where waiting would go beyond max duration. So throw exception and return + throw e; + } + sleep(delay); + //retry case. compute next sleep time + delay = getNextDelay(delay, currentCapturedException, e); + // reset current captured exception. + currentCapturedException = e; + } else { + // Exception cannot be retried on. Throw exception and return + throw e; + } + } + } + } + + private void sleep(long seconds) { + try { + Thread.sleep(seconds * 1000); + } catch (InterruptedException e) { + // no-op.. just proceed + } + } + + private long getNextDelay(long currentDelay, final Exception previousException, final Exception currentException) { + if (previousException != null && !previousException.getClass().equals(currentException.getClass())) { + // New exception encountered. Returning initial delay for next retry. + return this.initialDelayInSeconds; + } + + if (currentDelay <= 0) { // in case initial delay was set to 0. + currentDelay = MINIMUM_DELAY_IN_SEC; + } + + currentDelay *= this.backOff; + if (this.maxJitterInSeconds > 0) { + currentDelay += new Random().nextInt(this.maxJitterInSeconds); + } + + if (currentDelay > this.maxRetryDelayInSeconds) { + currentDelay = this.maxRetryDelayInSeconds; + } + + return currentDelay; + } + + private long elapsedTimeInSeconds(long fromTimeMillis) { + return (System.currentTimeMillis() - fromTimeMillis)/ 1000; + } + + public static class Builder { + private final Retryable runnable = new Retryable(); + public Builder() { + } + + public Builder withHiveConf(HiveConf conf) { + runnable.totalDurationInSeconds = conf.getTimeVar(HiveConf.ConfVars.REPL_RETRY_TOTAL_DURATION, TimeUnit.SECONDS); + runnable.initialDelayInSeconds = conf.getTimeVar(HiveConf.ConfVars.REPL_RETRY_INTIAL_DELAY, TimeUnit.SECONDS); + runnable.maxRetryDelayInSeconds = conf.getTimeVar(HiveConf.ConfVars + .REPL_RETRY_MAX_DELAY_BETWEEN_RETRIES, TimeUnit.SECONDS); + runnable.backOff = conf.getFloatVar(HiveConf.ConfVars.REPL_RETRY_BACKOFF_COEFFICIENT); + runnable.maxJitterInSeconds = (int) conf.getTimeVar(HiveConf.ConfVars.REPL_RETRY_JITTER, TimeUnit.SECONDS); + return this; + } + + public Retryable build() { + return runnable; + } + + public Builder withTotalDuration(long maxDuration) { + runnable.totalDurationInSeconds = maxDuration; + return this; + } + + // making this thread safe as it appends to list Review comment: If we use a common retryable it can be used in a multi threaded way as well. Right now we are not using it but keeping it synchronized will keep it thread safe and allow case where you create retryable once and pass the common retry params and then call excute from different places. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 464426) Time Spent: 50m (was: 40m) > Create a common Retry Interface for replication > ----------------------------------------------- > > Key: HIVE-23897 > URL: https://issues.apache.org/jira/browse/HIVE-23897 > Project: Hive > Issue Type: Task > Reporter: Aasha Medhi > Assignee: Aasha Medhi > Priority: Major > Labels: pull-request-available > Attachments: HIVE-23897.01.patch, HIVE-23897.02.patch, > HIVE-23897.03.patch, Retry Logic for Replication.pdf > > Time Spent: 50m > Remaining Estimate: 0h > -- This message was sent by Atlassian Jira (v8.3.4#803005)