1 /* 2 * Copyright 2017 Google LLC 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google LLC nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package com.google.api.gax.retrying; 31 32 import com.google.api.core.ApiFuture; 33 import java.util.concurrent.Callable; 34 35 /** 36 * Represents a retrying future. This is a facade hiding all the complications of an 37 * asynchronous/synchronous execution of a retriable task. 38 * 39 * <p>This interface is for advanced/internal use only. 40 * 41 * @param <ResponseT> response type 42 */ 43 public interface RetryingFuture<ResponseT> extends ApiFuture<ResponseT> { 44 /** 45 * Sets the attempt in a form of a future. This future represents a concrete retry attempt, 46 * potentially scheduled for execution in a some form of {@link 47 * java.util.concurrent.ScheduledExecutorService}, or an already completed future if the attempts 48 * are executed synchronously. 49 * 50 * @param attemptFuture the attempt future 51 */ setAttemptFuture(ApiFuture<ResponseT> attemptFuture)52 void setAttemptFuture(ApiFuture<ResponseT> attemptFuture); 53 54 /** Returns callable tracked by this future. */ getCallable()55 Callable<ResponseT> getCallable(); 56 57 /** Returns current (active) attempt settings. */ getAttemptSettings()58 TimedAttemptSettings getAttemptSettings(); 59 60 /** 61 * Returns latest completed attempt result or {@code null} if the first attempt hasn't completed 62 * yet. 63 * 64 * <p>This method is for internal/advanced use only. 65 * 66 * <p>If not null, the returned value is guaranteed to be an already completed future, so {@link 67 * ApiFuture#isDone()} will always be {@code true} and {@link ApiFuture#get()} will always be 68 * non-blocking. 69 * 70 * <p>In case if the whole retrying future is completed, this method returns the same result as 71 * the retrying future itself. 72 * 73 * <p>The number of attempt results may be (and usually is) lower than the number of actual 74 * attempts, since only a completed attempt has a result and not all attempts complete (some of 75 * the service attempts, needed for proper execution of the actual attempts). 76 * 77 * <p>For each execution the following invariants hold: 78 * 79 * <ul> 80 * <li>If the first attempt hasn't completed yet, this method returns {@code null}. 81 * <li>Right after completion of each attempt this method starts returning a new already 82 * completed future, which represents the result of the latest completed attempt. 83 * <li>If it was the last attempt, the events happen in the following order: 1) the attempt 84 * future completes; 2) the whole retrying future completes; 3) this method starts returning 85 * a new already completed future, which represents the result of the last completed 86 * attempt. 87 * <li>After completion of the whole retrying future this method always returns exactly same 88 * future object. 89 * </ul> 90 */ peekAttemptResult()91 ApiFuture<ResponseT> peekAttemptResult(); 92 93 /** 94 * Returns the current (active on the moment of the execution of this method) attempt result 95 * future, allowing to track progress of the retrying future execution. 96 * 97 * <p>Adding direct executor (same thread) callbacks to the future returned by this method is 98 * strongly not recommended, since the future is resolved under retrying future's internal lock 99 * and may affect the whole retrying process. Adding separate thread callbacks is ok. 100 * 101 * <p>This method is for internal/advanced use only. 102 * 103 * <p>The returned future completes right after the corresponding attempt which it tracks, so 104 * calling {@link ApiFuture#get()} is potentially a blocking operation. This method returns 105 * exactly same future object until it completes (meaning that the corresponding attempt has 106 * completed). If there is another attempt made after completion of the current attempt, the 107 * subsequent call to this method will return a new future which will track the new attempt. 108 * 109 * <p>In case if the whole retrying future is completed, this method returns the same result as 110 * the retrying future itself. 111 * 112 * <p>The returned future is non-cancellable, so calling {@link ApiFuture#cancel(boolean)} will 113 * have no effect and will always return {@code false}. 114 * 115 * <p>The number of attempt results may be (and usually is) lower than the number of actual 116 * attempts, since only a completed attempt has a result and not all attempts complete (some of 117 * the service attempts, needed for proper execution of the actual attempts). 118 * 119 * <p>For each execution the following invariants hold: 120 * 121 * <ul> 122 * <li>The future returned by this method completes soon after the attempt it tracks. 123 * <li>If it was the last attempt, the futures complete in the following order: 1) the attempt 124 * future; 2) the whole retrying future; 3) the attempt result future returned by this 125 * method. 126 * <li>After completion of the whole retrying future this method always returns exactly same 127 * future object. 128 * </ul> 129 */ getAttemptResult()130 ApiFuture<ResponseT> getAttemptResult(); 131 } 132