|
12 | 12 | import org.slf4j.Logger;
|
13 | 13 | import org.slf4j.LoggerFactory;
|
14 | 14 |
|
15 |
| -import java.util.concurrent.Executors; |
16 |
| -import java.util.concurrent.ScheduledExecutorService; |
17 |
| -import java.util.concurrent.ThreadFactory; |
18 |
| -import java.util.concurrent.TimeUnit; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.concurrent.*; |
| 18 | +import java.util.function.Function; |
19 | 19 |
|
20 | 20 | import static com.google.common.base.Preconditions.checkNotNull;
|
21 | 21 |
|
22 | 22 | public class SynchronizerImp implements Synchronizer {
|
23 | 23 |
|
| 24 | + // The boxing here IS necessary, so that the constants are not inlined by the compiler |
| 25 | + // and can be modified for the test (we don't want to wait that much in an UT) |
| 26 | + private static final long ON_DEMAND_FETCH_BACKOFF_BASE_MS = new Long(10000); //backoff base starting at 10 seconds (!) |
| 27 | + private static final long ON_DEMAND_FETCH_BACKOFF_MAX_WAIT_MS = new Long(60000); // don't sleep for more than 1 second |
| 28 | + private static final int ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES = 10; |
| 29 | + |
24 | 30 | private static final Logger _log = LoggerFactory.getLogger(Synchronizer.class);
|
25 | 31 | private final SplitSynchronizationTask _splitSynchronizationTask;
|
26 | 32 | private final SplitFetcher _splitFetcher;
|
@@ -49,7 +55,7 @@ public SynchronizerImp(SplitSynchronizationTask splitSynchronizationTask,
|
49 | 55 | _segmentSynchronizationTaskImp = checkNotNull(segmentSynchronizationTaskImp);
|
50 | 56 | _splitCache = checkNotNull(splitCache);
|
51 | 57 | _segmentCache = checkNotNull(segmentCache);
|
52 |
| - _onDemandFetchRetryDelayMs = checkNotNull(onDemandFetchRetryDelayMs); |
| 58 | + _onDemandFetchRetryDelayMs = onDemandFetchRetryDelayMs; |
53 | 59 | _cdnResponseHeadersLogging = cdnResponseHeadersLogging;
|
54 | 60 | _onDemandFetchMaxRetries = onDemandFetchMaxRetries;
|
55 | 61 | _failedAttemptsBeforeLogging = failedAttemptsBeforeLogging;
|
@@ -83,42 +89,83 @@ public void stopPeriodicFetching() {
|
83 | 89 | _segmentSynchronizationTaskImp.stop();
|
84 | 90 | }
|
85 | 91 |
|
86 |
| - @Override |
87 |
| - public void refreshSplits(long targetChangeNumber) { |
| 92 | + private static class SyncResult { |
88 | 93 |
|
89 |
| - if (targetChangeNumber <= _splitCache.getChangeNumber()) { |
90 |
| - return; |
| 94 | + /* package private */ SyncResult(boolean success, int remainingAttempts) { |
| 95 | + _success = success; |
| 96 | + _remainingAttempts = remainingAttempts; |
91 | 97 | }
|
92 | 98 |
|
93 |
| - FastlyHeadersCaptor captor = new FastlyHeadersCaptor(); |
94 |
| - FetchOptions opts = new FetchOptions.Builder() |
95 |
| - .cacheControlHeaders(true) |
96 |
| - .fastlyDebugHeader(_cdnResponseHeadersLogging) |
97 |
| - .responseHeadersCallback(_cdnResponseHeadersLogging ? captor::handle : null) |
98 |
| - .build(); |
| 99 | + public boolean success() { return _success; } |
| 100 | + public int remainingAttempts() { return _remainingAttempts; } |
| 101 | + |
| 102 | + private final boolean _success; |
| 103 | + private final int _remainingAttempts; |
| 104 | + } |
99 | 105 |
|
100 |
| - int remainingAttempts = _onDemandFetchMaxRetries; |
| 106 | + private SyncResult attemptSync(long targetChangeNumber, |
| 107 | + FetchOptions opts, |
| 108 | + Function<Void, Long> nextWaitMs, |
| 109 | + int maxRetries) { |
| 110 | + int remainingAttempts = maxRetries; |
101 | 111 | while(true) {
|
102 | 112 | remainingAttempts--;
|
103 | 113 | _splitFetcher.forceRefresh(opts);
|
104 | 114 | if (targetChangeNumber <= _splitCache.getChangeNumber()) {
|
105 |
| - _log.debug(String.format("Refresh completed in %s attempts.", _onDemandFetchMaxRetries - remainingAttempts)); |
106 |
| - break; |
| 115 | + return new SyncResult(true, remainingAttempts); |
107 | 116 | } else if (remainingAttempts <= 0) {
|
108 |
| - _log.info(String.format("No changes fetched after %s attempts.", _onDemandFetchMaxRetries)); |
109 |
| - break; |
| 117 | + _log.info(String.format("No changes fetched after %s attempts.", maxRetries)); |
| 118 | + return new SyncResult(false, remainingAttempts); |
110 | 119 | }
|
111 | 120 | try {
|
112 |
| - Thread.sleep(_onDemandFetchRetryDelayMs); |
| 121 | + long howLong = nextWaitMs.apply(null); |
| 122 | + Thread.sleep(howLong); |
113 | 123 | } catch (InterruptedException e) {
|
114 | 124 | Thread.currentThread().interrupt();
|
115 | 125 | _log.debug("Error trying to sleep current Thread.");
|
116 | 126 | }
|
117 | 127 | }
|
| 128 | + } |
| 129 | + |
| 130 | + private void logCdnHeaders(int maxRetries, int remainingAttempts, List<Map<String, String>> headers) { |
| 131 | + if (maxRetries - remainingAttempts > _failedAttemptsBeforeLogging) { |
| 132 | + _log.info(String.format("CDN Debug headers: %s", gson.toJson(headers))); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void refreshSplits(long targetChangeNumber) { |
| 138 | + |
| 139 | + if (targetChangeNumber <= _splitCache.getChangeNumber()) { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + FastlyHeadersCaptor captor = new FastlyHeadersCaptor(); |
| 144 | + FetchOptions opts = new FetchOptions.Builder() |
| 145 | + .cacheControlHeaders(true) |
| 146 | + .fastlyDebugHeader(_cdnResponseHeadersLogging) |
| 147 | + .responseHeadersCallback(_cdnResponseHeadersLogging ? captor::handle : null) |
| 148 | + .build(); |
| 149 | + |
| 150 | + SyncResult regularResult = attemptSync(targetChangeNumber, opts, |
| 151 | + (discard) -> (long) _onDemandFetchRetryDelayMs, _onDemandFetchMaxRetries); |
| 152 | + |
| 153 | + if (regularResult.success()) { |
| 154 | + _log.debug(String.format("Refresh completed in %s attempts.", _onDemandFetchMaxRetries - regularResult.remainingAttempts())); |
| 155 | + if (_cdnResponseHeadersLogging) { |
| 156 | + logCdnHeaders(_onDemandFetchMaxRetries , regularResult.remainingAttempts(), captor.get()); |
| 157 | + } |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + FetchOptions withCdnBypass = new FetchOptions.Builder(opts).cdnBypass(true).build(); |
| 162 | + Backoff backoff = new Backoff(ON_DEMAND_FETCH_BACKOFF_BASE_MS, ON_DEMAND_FETCH_BACKOFF_MAX_WAIT_MS); |
| 163 | + SyncResult withCDNBypassed = attemptSync(targetChangeNumber, withCdnBypass, |
| 164 | + (discard) -> backoff.interval(), ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES); |
118 | 165 |
|
119 |
| - if (_cdnResponseHeadersLogging && |
120 |
| - (_onDemandFetchMaxRetries - remainingAttempts) > _failedAttemptsBeforeLogging) { |
121 |
| - _log.info(String.format("CDN Debug headers: %s", gson.toJson(captor.get()))); |
| 166 | + if (_cdnResponseHeadersLogging) { |
| 167 | + logCdnHeaders(_onDemandFetchMaxRetries + ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES, |
| 168 | + withCDNBypassed.remainingAttempts(), captor.get()); |
122 | 169 | }
|
123 | 170 | }
|
124 | 171 |
|
|
0 commit comments