|
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 | + private static final long ON_DEMAND_FETCH_BACKOFF_BASE_MS = 10000; //backoff base starting at 10 seconds (!) |
| 25 | + private static final int ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES = 10; |
| 26 | + |
24 | 27 | private static final Logger _log = LoggerFactory.getLogger(Synchronizer.class);
|
25 | 28 | private final SplitSynchronizationTask _splitSynchronizationTask;
|
26 | 29 | private final SplitFetcher _splitFetcher;
|
@@ -83,42 +86,82 @@ public void stopPeriodicFetching() {
|
83 | 86 | _segmentSynchronizationTaskImp.stop();
|
84 | 87 | }
|
85 | 88 |
|
86 |
| - @Override |
87 |
| - public void refreshSplits(long targetChangeNumber) { |
| 89 | + private static class SyncResult { |
88 | 90 |
|
89 |
| - if (targetChangeNumber <= _splitCache.getChangeNumber()) { |
90 |
| - return; |
| 91 | + /* package private */ SyncResult(boolean success, int remainingAttempts) { |
| 92 | + _success = success; |
| 93 | + _remainingAttempts = remainingAttempts; |
91 | 94 | }
|
92 | 95 |
|
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(); |
| 96 | + public boolean success() { return _success; } |
| 97 | + public int remainingAttempts() { return _remainingAttempts; } |
99 | 98 |
|
100 |
| - int remainingAttempts = _onDemandFetchMaxRetries; |
| 99 | + private final boolean _success; |
| 100 | + private final int _remainingAttempts; |
| 101 | + } |
| 102 | + |
| 103 | + private SyncResult attemptSync(long targetChangeNumber, |
| 104 | + FetchOptions opts, |
| 105 | + Function<Void, Long> nextWaitMs, |
| 106 | + int maxRetries) { |
| 107 | + int remainingAttempts = maxRetries; |
101 | 108 | while(true) {
|
102 | 109 | remainingAttempts--;
|
103 | 110 | _splitFetcher.forceRefresh(opts);
|
104 | 111 | if (targetChangeNumber <= _splitCache.getChangeNumber()) {
|
105 |
| - _log.debug(String.format("Refresh completed in %s attempts.", _onDemandFetchMaxRetries - remainingAttempts)); |
106 |
| - break; |
| 112 | + return new SyncResult(true, remainingAttempts); |
107 | 113 | } else if (remainingAttempts <= 0) {
|
108 |
| - _log.info(String.format("No changes fetched after %s attempts.", _onDemandFetchMaxRetries)); |
109 |
| - break; |
| 114 | + _log.info(String.format("No changes fetched after %s attempts.", maxRetries)); |
| 115 | + return new SyncResult(false, remainingAttempts); |
110 | 116 | }
|
111 | 117 | try {
|
112 |
| - Thread.sleep(_onDemandFetchRetryDelayMs); |
| 118 | + Thread.sleep(nextWaitMs.apply(null)); |
113 | 119 | } catch (InterruptedException e) {
|
114 | 120 | Thread.currentThread().interrupt();
|
115 | 121 | _log.debug("Error trying to sleep current Thread.");
|
116 | 122 | }
|
117 | 123 | }
|
| 124 | + } |
| 125 | + |
| 126 | + private void logCdnHeaders(int maxRetries, int remainingAttempts, List<Map<String, String>> headers) { |
| 127 | + if (maxRetries - remainingAttempts > _failedAttemptsBeforeLogging) { |
| 128 | + _log.info(String.format("CDN Debug headers: %s", gson.toJson(headers))); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void refreshSplits(long targetChangeNumber) { |
| 134 | + |
| 135 | + if (targetChangeNumber <= _splitCache.getChangeNumber()) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + FastlyHeadersCaptor captor = new FastlyHeadersCaptor(); |
| 140 | + FetchOptions opts = new FetchOptions.Builder() |
| 141 | + .cacheControlHeaders(true) |
| 142 | + .fastlyDebugHeader(_cdnResponseHeadersLogging) |
| 143 | + .responseHeadersCallback(_cdnResponseHeadersLogging ? captor::handle : null) |
| 144 | + .build(); |
| 145 | + |
| 146 | + SyncResult regularResult = attemptSync(targetChangeNumber, opts, |
| 147 | + (discard) -> (long) _onDemandFetchRetryDelayMs, _onDemandFetchMaxRetries); |
| 148 | + |
| 149 | + if (regularResult.success()) { |
| 150 | + _log.debug(String.format("Refresh completed in %s attempts.", _onDemandFetchMaxRetries - regularResult.remainingAttempts())); |
| 151 | + if (_cdnResponseHeadersLogging) { |
| 152 | + logCdnHeaders(_onDemandFetchMaxRetries , regularResult.remainingAttempts(), captor.get()); |
| 153 | + } |
| 154 | + return; |
| 155 | + } |
118 | 156 |
|
119 |
| - if (_cdnResponseHeadersLogging && |
120 |
| - (_onDemandFetchMaxRetries - remainingAttempts) > _failedAttemptsBeforeLogging) { |
121 |
| - _log.info(String.format("CDN Debug headers: %s", gson.toJson(captor.get()))); |
| 157 | + FetchOptions withCdnBypass = new FetchOptions.Builder(opts).cdnBypass(true).build(); |
| 158 | + Backoff backoff = new Backoff(ON_DEMAND_FETCH_BACKOFF_BASE_MS); |
| 159 | + SyncResult withCDNBypassed = attemptSync(targetChangeNumber, withCdnBypass, |
| 160 | + (discard) -> backoff.interval(), ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES); |
| 161 | + |
| 162 | + if (_cdnResponseHeadersLogging) { |
| 163 | + logCdnHeaders(_onDemandFetchMaxRetries + ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES, |
| 164 | + withCDNBypassed.remainingAttempts(), captor.get()); |
122 | 165 | }
|
123 | 166 | }
|
124 | 167 |
|
|
0 commit comments