-
Notifications
You must be signed in to change notification settings - Fork 2k
separate initial load to avoid retaining list items in memory #4107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,18 @@ | |
*/ | ||
package io.kubernetes.client.informer.cache; | ||
|
||
import java.io.IOException; | ||
import java.net.ConnectException; | ||
import java.net.HttpURLConnection; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.BiConsumer; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.kubernetes.client.common.KubernetesListObject; | ||
import io.kubernetes.client.common.KubernetesObject; | ||
import io.kubernetes.client.informer.EventType; | ||
|
@@ -23,16 +35,6 @@ | |
import io.kubernetes.client.util.CallGeneratorParams; | ||
import io.kubernetes.client.util.Strings; | ||
import io.kubernetes.client.util.Watchable; | ||
import java.io.IOException; | ||
import java.net.ConnectException; | ||
import java.net.HttpURLConnection; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.BiConsumer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ReflectorRunnable< | ||
ApiType extends KubernetesObject, ApiListType extends KubernetesListObject> | ||
|
@@ -48,8 +50,6 @@ public class ReflectorRunnable< | |
|
||
private boolean isLastSyncResourceVersionUnavailable; | ||
|
||
private Watchable<ApiType> watch; | ||
|
||
private ListerWatcher<ApiType, ApiListType> listerWatcher; | ||
|
||
private DeltaFIFO store; | ||
|
@@ -87,30 +87,13 @@ public void run() { | |
log.info("{}#Start listing and watching...", apiTypeClass); | ||
|
||
try { | ||
ApiListType list = | ||
listerWatcher.list( | ||
new CallGeneratorParams(Boolean.FALSE, getRelistResourceVersion(), null)); | ||
|
||
V1ListMeta listMeta = list.getMetadata(); | ||
String resourceVersion = listMeta.getResourceVersion(); | ||
List<? extends KubernetesObject> items = list.getItems(); | ||
|
||
if (log.isDebugEnabled()) { | ||
log.debug("{}#Extract resourceVersion {} list meta", apiTypeClass, resourceVersion); | ||
} | ||
this.syncWith(items, resourceVersion); | ||
this.lastSyncResourceVersion = resourceVersion; | ||
this.lastSyncResourceVersion = initialLoad(); | ||
this.isLastSyncResourceVersionUnavailable = false; | ||
|
||
Watchable<ApiType> watch = null; | ||
if (log.isDebugEnabled()) { | ||
log.debug("{}#Start watching with {}...", apiTypeClass, lastSyncResourceVersion); | ||
} | ||
while (true) { | ||
if (!isActive.get()) { | ||
closeWatch(); | ||
return; | ||
} | ||
|
||
while (isActive.get()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not think that this logic is quite the same as the |
||
try { | ||
if (log.isDebugEnabled()) { | ||
log.debug( | ||
|
@@ -120,21 +103,14 @@ public void run() { | |
long jitteredWatchTimeoutSeconds = | ||
Double.valueOf(REFLECTOR_WATCH_CLIENTSIDE_TIMEOUT.getSeconds() * (1 + Math.random())) | ||
.longValue(); | ||
Watchable<ApiType> newWatch = | ||
watch = | ||
listerWatcher.watch( | ||
new CallGeneratorParams( | ||
Boolean.TRUE, | ||
lastSyncResourceVersion, | ||
Long.valueOf(jitteredWatchTimeoutSeconds).intValue())); | ||
|
||
synchronized (this) { | ||
if (!isActive.get()) { | ||
newWatch.close(); | ||
continue; | ||
} | ||
watch = newWatch; | ||
} | ||
watchHandler(newWatch); | ||
watchHandler(watch); | ||
} catch (WatchExpiredException e) { | ||
// Watch calls were failed due to expired resource-version. Returning | ||
// to unwind the list-watch loops so that we can respawn a new round | ||
|
@@ -165,7 +141,13 @@ public void run() { | |
this.exceptionHandler.accept(apiTypeClass, t); | ||
return; | ||
} finally { | ||
closeWatch(); | ||
if (watch != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the value of this refactor, can you keep the original |
||
try { | ||
watch.close(); | ||
} catch (IOException e) { | ||
log.warn("{}#Error while closing watcher", this.apiTypeClass, e); | ||
} | ||
} | ||
} | ||
} | ||
} catch (ApiException e) { | ||
|
@@ -191,11 +173,20 @@ public void stop() { | |
} | ||
} | ||
|
||
private synchronized void closeWatch() throws IOException { | ||
if (watch != null) { | ||
watch.close(); | ||
watch = null; | ||
} | ||
private String initialLoad() throws ApiException { | ||
ApiListType list = | ||
listerWatcher.list( | ||
new CallGeneratorParams(Boolean.FALSE, getRelistResourceVersion(), null)); | ||
|
||
V1ListMeta listMeta = list.getMetadata(); | ||
String resourceVersion = listMeta.getResourceVersion(); | ||
List<? extends KubernetesObject> items = list.getItems(); | ||
|
||
if (log.isDebugEnabled()) { | ||
log.debug("{}#Extract resourceVersion {} list meta", apiTypeClass, resourceVersion); | ||
} | ||
this.syncWith(items, resourceVersion); | ||
return resourceVersion; | ||
} | ||
|
||
private void syncWith(List<? extends KubernetesObject> items, String resourceVersion) { | ||
|
@@ -278,6 +269,11 @@ private void watchHandler(Watchable<ApiType> watch) { | |
log.debug("{}#Receiving resourceVersion {}", apiTypeClass, lastSyncResourceVersion); | ||
} | ||
} | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is adding this code needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the changes were aimed in making it easier for the |
||
watch.close(); | ||
} catch (IOException e) { | ||
log.warn("{}#Error while closing watcher", this.apiTypeClass, e); | ||
} | ||
} | ||
|
||
static <ApiType extends KubernetesObject> void defaultWatchErrorHandler( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this change and maintain the orginal order of imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something made VSCode change imports upon save. I ended up fixing them using
vi
. :(