-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
DefaultSharedIndexInformer.java
303 lines (257 loc) · 9.69 KB
/
DefaultSharedIndexInformer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed 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 io.fabric8.kubernetes.client.informers.impl;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.informers.InformerExceptionHandler;
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
import io.fabric8.kubernetes.client.informers.cache.Indexer;
import io.fabric8.kubernetes.client.informers.cache.ItemStore;
import io.fabric8.kubernetes.client.informers.cache.Store;
import io.fabric8.kubernetes.client.informers.impl.cache.CacheImpl;
import io.fabric8.kubernetes.client.informers.impl.cache.ProcessorStore;
import io.fabric8.kubernetes.client.informers.impl.cache.Reflector;
import io.fabric8.kubernetes.client.informers.impl.cache.SharedProcessor;
import io.fabric8.kubernetes.client.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.stream.Stream;
public class DefaultSharedIndexInformer<T extends HasMetadata, L extends KubernetesResourceList<T>>
implements SharedIndexInformer<T> {
private static final Logger log = LoggerFactory.getLogger(DefaultSharedIndexInformer.class);
private static final long MINIMUM_RESYNC_PERIOD_MILLIS = 1000L;
// resyncCheckPeriod is how often we want the reflector's resync timer to fire so it can
// call shouldResync to check if any of our listeners need a resync.
private long resyncCheckPeriodMillis;
// defaultEventHandlerResyncPeriod is the default resync period for any handlers added via
// AddEventHandler(i.e they don't specify one and just want to use the shared informer's default
// value).
private final long defaultEventHandlerResyncPeriod;
private final Reflector<T, L> reflector;
private final Class<T> apiTypeClass;
private final ProcessorStore<T> processorStore;
private final CacheImpl<T> indexer = new CacheImpl<>();
private final SharedProcessor<T> processor;
private final Executor informerExecutor;
private final String description;
private final AtomicBoolean started = new AtomicBoolean();
private volatile boolean stopped = false;
private Future<?> resyncFuture;
private Stream<T> initialState;
private InformerExceptionHandler exceptionHandler;
public DefaultSharedIndexInformer(Class<T> apiTypeClass, ListerWatcher<T, L> listerWatcher, long resyncPeriod,
Executor informerExecutor) {
if (resyncPeriod < 0) {
throw new IllegalArgumentException("Invalid resync period provided, It should be a non-negative value");
}
this.resyncCheckPeriodMillis = resyncPeriod;
this.defaultEventHandlerResyncPeriod = resyncPeriod;
this.apiTypeClass = apiTypeClass;
this.description = listerWatcher.getApiEndpointPath();
this.informerExecutor = informerExecutor;
// reuse the informer executor, but ensure serial processing
this.processor = new SharedProcessor<>(informerExecutor, description);
processorStore = new ProcessorStore<>(this.indexer, this.processor);
this.reflector = new Reflector<>(listerWatcher, processorStore, this::getExceptionHandler);
}
public InformerExceptionHandler getExceptionHandler() {
return exceptionHandler;
}
/**
* add event callback
*
* @param handler event handler
*/
@Override
public SharedIndexInformer<T> addEventHandler(ResourceEventHandler<? super T> handler) {
addEventHandlerWithResyncPeriod(handler, defaultEventHandlerResyncPeriod);
return this;
}
@Override
public SharedIndexInformer<T> addEventHandlerWithResyncPeriod(ResourceEventHandler<? super T> handler,
long resyncPeriodMillis) {
if (stopped) {
log.info("DefaultSharedIndexInformer#Handler was not added to {} because it has stopped already", this);
return this;
}
if (resyncPeriodMillis > 0) {
if (resyncPeriodMillis < MINIMUM_RESYNC_PERIOD_MILLIS) {
log.warn("DefaultSharedIndexInformer#resyncPeriod {} is too small for {}. Changing it to minimal allowed value of {}",
resyncPeriodMillis, this, MINIMUM_RESYNC_PERIOD_MILLIS);
resyncPeriodMillis = MINIMUM_RESYNC_PERIOD_MILLIS;
}
if (resyncPeriodMillis < this.resyncCheckPeriodMillis) {
if (started.get()) {
log.warn(
"DefaultSharedIndexInformer#resyncPeriod {} is smaller than resyncCheckPeriod {} and the {} informer has already started. Changing it to {}",
resyncPeriodMillis, resyncCheckPeriodMillis, this, resyncCheckPeriodMillis);
resyncPeriodMillis = resyncCheckPeriodMillis;
} else {
// if the event handler's resyncPeriod is smaller than the current resyncCheckPeriod
// update resyncCheckPeriod to match resyncPeriod and adjust the resync periods of all
// the listeners accordingly.
this.resyncCheckPeriodMillis = resyncPeriodMillis;
}
}
}
this.processor.addProcessorListener(handler,
determineResyncPeriod(resyncPeriodMillis, this.resyncCheckPeriodMillis), this.indexer::list);
return this;
}
@Override
public String lastSyncResourceVersion() {
return this.reflector.getLastSyncResourceVersion();
}
@Override
public CompletableFuture<Void> start() {
if (stopped) {
throw new IllegalStateException("Cannot restart a stopped informer");
}
synchronized (this) {
if (!started.compareAndSet(false, true)) {
return CompletableFuture.completedFuture(null);
}
if (initialState != null) {
initialState.forEach(indexer::put);
}
}
log.debug("Ready to run resync and reflector for {} with resync {}", this, resyncCheckPeriodMillis);
scheduleResync(processor::shouldResync);
return reflector.start().whenComplete((v, t) -> {
// stop called while run is called could be ineffective, check for it afterwards
synchronized (this) {
if (stopped) {
stop();
}
}
});
}
@Override
public SharedIndexInformer<T> run() {
Utils.waitUntilReadyOrFail(start(), -1, TimeUnit.MILLISECONDS);
return this;
}
@Override
public synchronized void stop() {
stopped = true;
reflector.stop();
stopResync();
processor.stop();
}
private synchronized void stopResync() {
if (resyncFuture != null) {
resyncFuture.cancel(true);
resyncFuture = null;
}
}
@Override
public SharedIndexInformer<T> addIndexers(Map<String, Function<T, List<String>>> indexers) {
indexer.addIndexers(indexers);
return this;
}
@Override
public Indexer<T> getIndexer() {
return this.indexer;
}
@Override
public Store<T> getStore() {
return this.indexer;
}
private long determineResyncPeriod(long desired, long check) {
if (desired == 0) {
return desired;
}
if (check == 0) {
return 0;
}
return Math.max(desired, check);
}
@Override
public boolean isRunning() {
return !stopped && started.get() && reflector.isRunning();
}
@Override
public boolean isWatching() {
return reflector.isWatching();
}
synchronized void scheduleResync(BooleanSupplier resyncFunc) {
// schedule the resync runnable
if (resyncCheckPeriodMillis > 0) {
resyncFuture = Utils.scheduleAtFixedRate(informerExecutor, () -> {
if (log.isDebugEnabled()) {
log.debug("Checking for resync at interval for {}", this);
}
if (resyncFunc.getAsBoolean()) {
log.debug("Resync running for {}", this);
processorStore.resync();
}
}, resyncCheckPeriodMillis,
resyncCheckPeriodMillis, TimeUnit.MILLISECONDS);
} else {
log.debug("Resync skipped due to 0 full resync period for {}", this);
}
}
public long getFullResyncPeriod() {
return resyncCheckPeriodMillis;
}
Future<?> getResyncFuture() {
return resyncFuture;
}
@Override
public Class<T> getApiTypeClass() {
return apiTypeClass;
}
@Override
public SharedIndexInformer<T> removeIndexer(String name) {
this.indexer.removeIndexer(name);
return this;
}
@Override
public synchronized SharedIndexInformer<T> initialState(Stream<T> items) {
if (started.get()) {
throw new KubernetesClientException("Informer cannot be running when initial state is added");
}
this.initialState = items;
return this;
}
@Override
public synchronized SharedIndexInformer<T> itemStore(ItemStore<T> itemStore) {
if (started.get()) {
throw new KubernetesClientException("Informer cannot be running when setting item store");
}
this.indexer.setItemStore(itemStore);
return this;
}
@Override
public String toString() {
return this.description;
}
@Override
public void setExceptionHandler(InformerExceptionHandler handler) {
this.exceptionHandler = handler;
}
}