Spring Retry

概要

Spring实现了一套重试机制,功能简单实用。Spring Retry是从Spring Batch独立出来的一个功能,已经广泛应用于Spring Batch,Spring Integration,Spring for Apache Hadoop等Spring项目。这里讲述如何使用Spring Retry及其实现原理。

背景

重试其实其实很多时候都需要的,为了保证容错性,可用性,一致性等。一般用来应对外部系统的一些不可预料的返回、异常等,特别是网络延迟,中断等情况。还有在现在流行的微服务治理框架中,通常都有自己的重试与超时配置,比如dubbo可以设置retries=1,timeout=500调用失败只重试1次,超过500ms调用仍未返回则调用失败。如果要做重试,要为特定的某个操作做重试功能,则要硬编码,大概逻辑基本都是写个循环,根据返回或异常,计数失败次数,然后设定退出条件。这样做,且不说每个操作都要写这种类似的代码,而且重试逻辑和业务逻辑混在一起,给维护和扩展带来了麻烦。从面向对象的角度来看,应该把重试的代码独立出来。

使用介绍

基本使用

先举个例子:

  1. @Configuration
  2. @EnableRetry
  3. public class Application {
  4. @Bean
  5. public RetryService retryService(){
  6. return new RetryService();
  7. }
  8. public static void main(String[] args) throws Exception{
  9. ApplicationContext applicationContext = new AnnotationConfigApplicationContext("springretry");
  10. RetryService service1 = applicationContext.getBean("service", RetryService.class);
  11. service1.service();
  12. }
  13. }
  14. @Service("service")
  15. public class RetryService {
  16. @Retryable(value = IllegalAccessException.class, maxAttempts = 5,
  17. backoff= @Backoff(value = 1500, maxDelay = 100000, multiplier = 1.2))
  18. public void service() throws IllegalAccessException {
  19. System.out.println("service method...");
  20. throw new IllegalAccessException("manual exception");
  21. }
  22. @Recover
  23. public void recover(IllegalAccessException e){
  24. System.out.println("service retry after Recover => " + e.getMessage());
  25. }
  26. }

@EnableRetry - 表示开启重试机制 @Retryable - 表示这个方法需要重试,它有很丰富的参数,可以满足对重试的需求 @Backoff - 表示重试中的退避策略 @Recover - 兜底方法,即多次重试后还是失败就会执行这个方法Spring-Retry 的功能丰富在于其重试策略和退避策略,还有兜底,监听器等操作。

重试策略

看一下Spring Retry自带的一些重试策略,主要是用来判断当方法调用异常时是否需要重试。

  • SimpleRetryPolicy 默认最多重试3次
  • TimeoutRetryPolicy 默认在1秒内失败都会重试
  • ExpressionRetryPolicy 符合表达式就会重试
  • CircuitBreakerRetryPolicy 增加了熔断的机制,如果不在熔断状态,则允许重试
  • CompositeRetryPolicy 可以组合多个重试策略
  • NeverRetryPolicy 从不重试(也是一种重试策略)
  • AlwaysRetryPolicy 总是重试

    退避策略

    看一下退避策略,退避是指怎么去做下一次的重试,在这里其实就是等待多长时间。Spring-Retry重试机制实现原理 - 图1

  • FixedBackOffPolicy 默认固定延迟1秒后执行下一次重试

  • ExponentialBackOffPolicy 指数递增延迟执行重试,默认初始0.1秒,系数是2,那么下次延迟0.2秒,再下次就是延迟0.4秒,如此类推,最大30秒。
  • ExponentialRandomBackOffPolicy 在上面那个策略上增加随机性
  • UniformRandomBackOffPolicy 这个跟上面的区别就是,上面的延迟会不停递增,这个只会在固定的区间随机
  • StatelessBackOffPolicy 这个说明是无状态的,所谓无状态就是对上次的退避无感知,从它下面的子类也能看出来

    原理

    原理部分,一是重试机制的切入点,即它是如何使得代码实现重试功能的;二是重试机制的详细,包括重试的逻辑以及重试策略和退避策略的实现。

    切入点

    @EnableRetry

    ```java @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @EnableAspectJAutoProxy(proxyTargetClass = false)
    @Import(RetryConfiguration.class)
    @Documented
    public @interface EnableRetry {

    /**

    • Indicate whether subclass-based (CGLIB) proxies are to be created as opposed
    • to standard Java interface-based proxies. The default is {@code false}.
    • @return whether to proxy or not to proxy the class
      */
      boolean proxyTargetClass() default false;

}

  1. 可以看到`@EnableAspectJAutoProxy(proxyTargetClass = false)`这个并不陌生,就是打开Spring AOP功能。重点看看`@Import(RetryConfiguration.class)``@Import`相当于注册这个Bean,看看这个`RetryConfiguration`![](https://cdn.nlark.com/yuque/0/2020/webp/396745/1608081416536-a0a57d0d-bdfa-4379-800e-885b3595577d.webp#crop=0&crop=0&crop=1&crop=1&height=328&id=yoIU3&originHeight=328&originWidth=998&originalType=binary&ratio=1&rotation=0&showTitle=false&size=0&status=done&style=shadow&title=&width=998)它是一个`AbstractPointcutAdvisor`,它有一个pointcut和一个advice。在IOC过程中会根据`PointcutAdvisor`类来对Bean进行`Pointcut`的过滤,然后生成对应的AOP代理类,用advice来加强处理。看看`RetryConfiguration`的初始化:
  2. ```java
  3. @PostConstruct
  4. public void init() {
  5. Set<Class<? extends Annotation>> retryableAnnotationTypes = new LinkedHashSet<Class<? extends Annotation>>(1);
  6. retryableAnnotationTypes.add(Retryable.class);
  7. //创建pointcut
  8. this.pointcut = buildPointcut(retryableAnnotationTypes);
  9. //创建advice
  10. this.advice = buildAdvice();
  11. if (this.advice instanceof BeanFactoryAware) {
  12. ((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);
  13. }
  14. }
  15. protected Pointcut buildPointcut(Set<Class<? extends Annotation>> retryAnnotationTypes) {
  16. ComposablePointcut result = null;
  17. for (Class<? extends Annotation> retryAnnotationType : retryAnnotationTypes) {
  18. Pointcut filter = new AnnotationClassOrMethodPointcut(retryAnnotationType);
  19. if (result == null) {
  20. result = new ComposablePointcut(filter);
  21. }
  22. else {
  23. result.union(filter);
  24. }
  25. }
  26. return result;
  27. }

上面代码用到了AnnotationClassOrMethodPointcut,其实它最终还是用到了AnnotationMethodMatcher来根据注解进行切入点的过滤。这里就是@Retryable注解了。

  1. //创建advice对象,即拦截器
  2. protected Advice buildAdvice() {
  3. //下面关注这个对象
  4. AnnotationAwareRetryOperationsInterceptor interceptor = new AnnotationAwareRetryOperationsInterceptor();
  5. if (retryContextCache != null) {
  6. interceptor.setRetryContextCache(retryContextCache);
  7. }
  8. if (retryListeners != null) {
  9. interceptor.setListeners(retryListeners);
  10. }
  11. if (methodArgumentsKeyGenerator != null) {
  12. interceptor.setKeyGenerator(methodArgumentsKeyGenerator);
  13. }
  14. if (newMethodArgumentsIdentifier != null) {
  15. interceptor.setNewItemIdentifier(newMethodArgumentsIdentifier);
  16. }
  17. if (sleeper != null) {
  18. interceptor.setSleeper(sleeper);
  19. }
  20. return interceptor;
  21. }

AnnotationAwareRetryOperationsInterceptor

Spring-Retry重试机制实现原理 - 图2可以看出AnnotationAwareRetryOperationsInterceptor是一个MethodInterceptor,在创建AOP代理过程中如果目标方法符合pointcut的规则,它就会加到interceptor列表中,然后做增强,看看invoke方法做了什么增强。

  1. @Override
  2. public Object invoke(MethodInvocation invocation) throws Throwable {
  3. MethodInterceptor delegate = getDelegate(invocation.getThis(), invocation.getMethod());
  4. if (delegate != null) {
  5. return delegate.invoke(invocation);
  6. }
  7. else {
  8. return invocation.proceed();
  9. }
  10. }

这里用到了委托,主要是需要根据配置委托给具体“有状态”的interceptor还是“无状态”的interceptor

  1. private MethodInterceptor getDelegate(Object target, Method method) {
  2. if (!this.delegates.containsKey(target) || !this.delegates.get(target).containsKey(method)) {
  3. synchronized (this.delegates) {
  4. if (!this.delegates.containsKey(target)) {
  5. this.delegates.put(target, new HashMap<Method, MethodInterceptor>());
  6. }
  7. Map<Method, MethodInterceptor> delegatesForTarget = this.delegates.get(target);
  8. if (!delegatesForTarget.containsKey(method)) {
  9. Retryable retryable = AnnotationUtils.findAnnotation(method, Retryable.class);
  10. if (retryable == null) {
  11. retryable = AnnotationUtils.findAnnotation(method.getDeclaringClass(), Retryable.class);
  12. }
  13. if (retryable == null) {
  14. retryable = findAnnotationOnTarget(target, method);
  15. }
  16. if (retryable == null) {
  17. return delegatesForTarget.put(method, null);
  18. }
  19. MethodInterceptor delegate;
  20. //支持自定义MethodInterceptor,而且优先级最高
  21. if (StringUtils.hasText(retryable.interceptor())) {
  22. delegate = this.beanFactory.getBean(retryable.interceptor(), MethodInterceptor.class);
  23. }
  24. else if (retryable.stateful()) {
  25. //得到“有状态”的interceptor
  26. delegate = getStatefulInterceptor(target, method, retryable);
  27. }
  28. else {
  29. //得到“无状态”的interceptor
  30. delegate = getStatelessInterceptor(target, method, retryable);
  31. }
  32. delegatesForTarget.put(method, delegate);
  33. }
  34. }
  35. }
  36. return this.delegates.get(target).get(method);
  37. }

getStatefulInterceptorgetStatelessInterceptor都是差不多,先看看比较简单的getStatelessInterceptor

  1. private MethodInterceptor getStatelessInterceptor(Object target, Method method, Retryable retryable) {
  2. //生成一个RetryTemplate
  3. RetryTemplate template = createTemplate(retryable.listeners());
  4. //生成retryPolicy
  5. template.setRetryPolicy(getRetryPolicy(retryable));
  6. //生成backoffPolicy
  7. template.setBackOffPolicy(getBackoffPolicy(retryable.backoff()));
  8. return RetryInterceptorBuilder.stateless()
  9. .retryOperations(template)
  10. .label(retryable.label())
  11. .recoverer(getRecoverer(target, method))
  12. .build();
  13. }

具体生成retryPolicybackoffPolicy的规则,等下再回头来看。RetryInterceptorBuilder其实就是为了生成RetryOperationsInterceptorRetryOperationsInterceptor也是一个MethodInterceptor,来看看它的invoke方法。

  1. public Object invoke(final MethodInvocation invocation) throws Throwable {
  2. String name;
  3. if (StringUtils.hasText(label)) {
  4. name = label;
  5. } else {
  6. name = invocation.getMethod().toGenericString();
  7. }
  8. final String label = name;
  9. //定义了一个RetryCallback,其实看它的doWithRetry方法,调用了invocation的proceed()方法,是不是有点眼熟,这就是AOP的拦截链调用,如果没有拦截链,那就是对原来方法的调用。
  10. RetryCallback<Object, Throwable> retryCallback = new RetryCallback<Object, Throwable>() {
  11. public Object doWithRetry(RetryContext context) throws Exception {
  12. context.setAttribute(RetryContext.NAME, label);
  13. /*
  14. * If we don't copy the invocation carefully it won't keep a reference to
  15. * the other interceptors in the chain. We don't have a choice here but to
  16. * specialise to ReflectiveMethodInvocation (but how often would another
  17. * implementation come along?).
  18. */
  19. if (invocation instanceof ProxyMethodInvocation) {
  20. try {
  21. return ((ProxyMethodInvocation) invocation).invocableClone().proceed();
  22. }
  23. catch (Exception e) {
  24. throw e;
  25. }
  26. catch (Error e) {
  27. throw e;
  28. }
  29. catch (Throwable e) {
  30. throw new IllegalStateException(e);
  31. }
  32. }
  33. else {
  34. throw new IllegalStateException(
  35. "MethodInvocation of the wrong type detected - this should not happen with Spring AOP, " +
  36. "so please raise an issue if you see this exception");
  37. }
  38. }
  39. };
  40. if (recoverer != null) {
  41. ItemRecovererCallback recoveryCallback = new ItemRecovererCallback(
  42. invocation.getArguments(), recoverer);
  43. return this.retryOperations.execute(retryCallback, recoveryCallback);
  44. }
  45. //最终还是进入到retryOperations的execute方法,这个retryOperations就是在之前的builder set进来的RetryTemplate。
  46. return this.retryOperations.execute(retryCallback);
  47. }

无论是RetryOperationsInterceptor还是StatefulRetryOperationsInterceptor,最终的拦截处理逻辑还是调用到RetryTemplateexecute方法,从名字也看出来,RetryTemplate作为一个模板类,里面包含了重试统一逻辑。

重试逻辑及策略实现

上面介绍了Spring Retry利用了AOP代理使重试机制对业务代码进行“入侵”。下面继续看看重试的逻辑做了什么。RetryTemplatedoExecute方法。

  1. protected <T, E extends Throwable> T doExecute(RetryCallback<T, E> retryCallback,
  2. RecoveryCallback<T> recoveryCallback, RetryState state)
  3. throws E, ExhaustedRetryException {
  4. RetryPolicy retryPolicy = this.retryPolicy;
  5. BackOffPolicy backOffPolicy = this.backOffPolicy;
  6. //新建一个RetryContext来保存本轮重试的上下文
  7. RetryContext context = open(retryPolicy, state);
  8. if (this.logger.isTraceEnabled()) {
  9. this.logger.trace("RetryContext retrieved: " + context);
  10. }
  11. // Make sure the context is available globally for clients who need
  12. // it...
  13. RetrySynchronizationManager.register(context);
  14. Throwable lastException = null;
  15. boolean exhausted = false;
  16. try {
  17. //如果有注册RetryListener,则会调用它的open方法,给调用者一个通知。
  18. boolean running = doOpenInterceptors(retryCallback, context);
  19. if (!running) {
  20. throw new TerminatedRetryException(
  21. "Retry terminated abnormally by interceptor before first attempt");
  22. }
  23. // Get or Start the backoff context...
  24. BackOffContext backOffContext = null;
  25. Object resource = context.getAttribute("backOffContext");
  26. if (resource instanceof BackOffContext) {
  27. backOffContext = (BackOffContext) resource;
  28. }
  29. if (backOffContext == null) {
  30. backOffContext = backOffPolicy.start(context);
  31. if (backOffContext != null) {
  32. context.setAttribute("backOffContext", backOffContext);
  33. }
  34. }
  35. //判断能否重试,就是调用RetryPolicy的canRetry方法来判断。
  36. //这个循环会直到原方法不抛出异常,或不需要再重试
  37. while (canRetry(retryPolicy, context) && !context.isExhaustedOnly()) {
  38. try {
  39. if (this.logger.isDebugEnabled()) {
  40. this.logger.debug("Retry: count=" + context.getRetryCount());
  41. }
  42. //清除上次记录的异常
  43. lastException = null;
  44. //doWithRetry方法,一般来说就是原方法
  45. return retryCallback.doWithRetry(context);
  46. }
  47. catch (Throwable e) {
  48. //原方法抛出了异常
  49. lastException = e;
  50. try {
  51. //记录异常信息
  52. registerThrowable(retryPolicy, state, context, e);
  53. }
  54. catch (Exception ex) {
  55. throw new TerminatedRetryException("Could not register throwable",
  56. ex);
  57. }
  58. finally {
  59. //调用RetryListener的onError方法
  60. doOnErrorInterceptors(retryCallback, context, e);
  61. }
  62. //再次判断能否重试
  63. if (canRetry(retryPolicy, context) && !context.isExhaustedOnly()) {
  64. try {
  65. //如果可以重试则走退避策略
  66. backOffPolicy.backOff(backOffContext);
  67. }
  68. catch (BackOffInterruptedException ex) {
  69. lastException = e;
  70. // back off was prevented by another thread - fail the retry
  71. if (this.logger.isDebugEnabled()) {
  72. this.logger
  73. .debug("Abort retry because interrupted: count="
  74. + context.getRetryCount());
  75. }
  76. throw ex;
  77. }
  78. }
  79. if (this.logger.isDebugEnabled()) {
  80. this.logger.debug(
  81. "Checking for rethrow: count=" + context.getRetryCount());
  82. }
  83. if (shouldRethrow(retryPolicy, context, state)) {
  84. if (this.logger.isDebugEnabled()) {
  85. this.logger.debug("Rethrow in retry for policy: count="
  86. + context.getRetryCount());
  87. }
  88. throw RetryTemplate.<E>wrapIfNecessary(e);
  89. }
  90. }
  91. /*
  92. * A stateful attempt that can retry may rethrow the exception before now,
  93. * but if we get this far in a stateful retry there's a reason for it,
  94. * like a circuit breaker or a rollback classifier.
  95. */
  96. if (state != null && context.hasAttribute(GLOBAL_STATE)) {
  97. break;
  98. }
  99. }
  100. if (state == null && this.logger.isDebugEnabled()) {
  101. this.logger.debug(
  102. "Retry failed last attempt: count=" + context.getRetryCount());
  103. }
  104. exhausted = true;
  105. //重试结束后如果有兜底Recovery方法则执行,否则抛异常
  106. return handleRetryExhausted(recoveryCallback, context, state);
  107. }
  108. catch (Throwable e) {
  109. throw RetryTemplate.<E>wrapIfNecessary(e);
  110. }
  111. finally {
  112. //处理一些关闭逻辑
  113. close(retryPolicy, context, state, lastException == null || exhausted);
  114. //调用RetryListener的close方法
  115. doCloseInterceptors(retryCallback, context, lastException);
  116. RetrySynchronizationManager.clear();
  117. }
  118. }

主要核心重试逻辑就是上面的代码了,看上去还是挺简单的。在上面,漏掉了RetryPolicycanRetry方法和BackOffPolicy的backOff方法,以及这两个Policy是怎么来的。回头看看getStatelessInterceptor方法中的getRetryPolicygetRetryPolicy方法。

  1. private RetryPolicy getRetryPolicy(Annotation retryable) {
  2. Map<String, Object> attrs = AnnotationUtils.getAnnotationAttributes(retryable);
  3. @SuppressWarnings("unchecked")
  4. Class<? extends Throwable>[] includes = (Class<? extends Throwable>[]) attrs.get("value");
  5. String exceptionExpression = (String) attrs.get("exceptionExpression");
  6. boolean hasExpression = StringUtils.hasText(exceptionExpression);
  7. if (includes.length == 0) {
  8. @SuppressWarnings("unchecked")
  9. Class<? extends Throwable>[] value = (Class<? extends Throwable>[]) attrs.get("include");
  10. includes = value;
  11. }
  12. @SuppressWarnings("unchecked")
  13. Class<? extends Throwable>[] excludes = (Class<? extends Throwable>[]) attrs.get("exclude");
  14. Integer maxAttempts = (Integer) attrs.get("maxAttempts");
  15. String maxAttemptsExpression = (String) attrs.get("maxAttemptsExpression");
  16. if (StringUtils.hasText(maxAttemptsExpression)) {
  17. maxAttempts = PARSER.parseExpression(resolve(maxAttemptsExpression), PARSER_CONTEXT)
  18. .getValue(this.evaluationContext, Integer.class);
  19. }
  20. if (includes.length == 0 && excludes.length == 0) {
  21. SimpleRetryPolicy simple = hasExpression ? new ExpressionRetryPolicy(resolve(exceptionExpression))
  22. .withBeanFactory(this.beanFactory)
  23. : new SimpleRetryPolicy();
  24. simple.setMaxAttempts(maxAttempts);
  25. return simple;
  26. }
  27. Map<Class<? extends Throwable>, Boolean> policyMap = new HashMap<Class<? extends Throwable>, Boolean>();
  28. for (Class<? extends Throwable> type : includes) {
  29. policyMap.put(type, true);
  30. }
  31. for (Class<? extends Throwable> type : excludes) {
  32. policyMap.put(type, false);
  33. }
  34. boolean retryNotExcluded = includes.length == 0;
  35. if (hasExpression) {
  36. return new ExpressionRetryPolicy(maxAttempts, policyMap, true, exceptionExpression, retryNotExcluded)
  37. .withBeanFactory(this.beanFactory);
  38. }
  39. else {
  40. return new SimpleRetryPolicy(maxAttempts, policyMap, true, retryNotExcluded);
  41. }
  42. }

这里简单做一下总结。就是通过@Retryable注解中的参数,来判断具体使用哪个重试策略,是SimpleRetryPolicy还是ExpressionRetryPolicy等。

  1. private BackOffPolicy getBackoffPolicy(Backoff backoff) {
  2. long min = backoff.delay() == 0 ? backoff.value() : backoff.delay();
  3. if (StringUtils.hasText(backoff.delayExpression())) {
  4. min = PARSER.parseExpression(resolve(backoff.delayExpression()), PARSER_CONTEXT)
  5. .getValue(this.evaluationContext, Long.class);
  6. }
  7. long max = backoff.maxDelay();
  8. if (StringUtils.hasText(backoff.maxDelayExpression())) {
  9. max = PARSER.parseExpression(resolve(backoff.maxDelayExpression()), PARSER_CONTEXT)
  10. .getValue(this.evaluationContext, Long.class);
  11. }
  12. double multiplier = backoff.multiplier();
  13. if (StringUtils.hasText(backoff.multiplierExpression())) {
  14. multiplier = PARSER.parseExpression(resolve(backoff.multiplierExpression()), PARSER_CONTEXT)
  15. .getValue(this.evaluationContext, Double.class);
  16. }
  17. if (multiplier > 0) {
  18. ExponentialBackOffPolicy policy = new ExponentialBackOffPolicy();
  19. if (backoff.random()) {
  20. policy = new ExponentialRandomBackOffPolicy();
  21. }
  22. policy.setInitialInterval(min);
  23. policy.setMultiplier(multiplier);
  24. policy.setMaxInterval(max > min ? max : ExponentialBackOffPolicy.DEFAULT_MAX_INTERVAL);
  25. if (this.sleeper != null) {
  26. policy.setSleeper(this.sleeper);
  27. }
  28. return policy;
  29. }
  30. if (max > min) {
  31. UniformRandomBackOffPolicy policy = new UniformRandomBackOffPolicy();
  32. policy.setMinBackOffPeriod(min);
  33. policy.setMaxBackOffPeriod(max);
  34. if (this.sleeper != null) {
  35. policy.setSleeper(this.sleeper);
  36. }
  37. return policy;
  38. }
  39. FixedBackOffPolicy policy = new FixedBackOffPolicy();
  40. policy.setBackOffPeriod(min);
  41. if (this.sleeper != null) {
  42. policy.setSleeper(this.sleeper);
  43. }
  44. return policy;
  45. }

通过@Backoff注解中的参数,来判断具体使用哪个退避策略,是FixedBackOffPolicy还是UniformRandomBackOffPolicy等。那么每个RetryPolicy都会重写canRetry方法,然后在RetryTemplate判断是否需要重试。看看SimpleRetryPolicy

  1. @Override
  2. public boolean canRetry(RetryContext context) {
  3. Throwable t = context.getLastThrowable();
  4. //判断抛出的异常是否符合重试的异常
  5. //还有,是否超过了重试的次数
  6. return (t == null || retryForException(t)) && context.getRetryCount() < maxAttempts;
  7. }

同样,看看FixedBackOffPolicy的退避方法。

  1. protected void doBackOff() throws BackOffInterruptedException {
  2. try {
  3. //就是sleep固定的时间
  4. sleeper.sleep(backOffPeriod);
  5. }
  6. catch (InterruptedException e) {
  7. throw new BackOffInterruptedException("Thread interrupted while sleeping", e);
  8. }
  9. }

至此,重试的主要原理以及逻辑大概就是这样了。

RetryContext

先看看RetryContext的继承关系。Spring-Retry重试机制实现原理 - 图3可以看出对每一个策略都有对应的Context。在Spring Retry里,其实每一个策略都是单例来的。Spring Retry采用了一个更加轻量级的做法,就是针对每一个需要重试的方法只new一个上下文Context对象,然后在重试时,把这个Context传到策略里,策略再根据这个Context做重试,而且Spring Retry还对这个Context做了cache。这样就相当于对重试的上下文做了优化。

总结

Spring Retry通过AOP机制来实现对业务代码的重试”入侵“,RetryTemplate中包含了核心的重试逻辑,还提供了丰富的重试策略和退避策略。