Skip to content

Commit 9288786

Browse files
committed
Log updating tunables
1 parent 1ec2dc6 commit 9288786

File tree

6 files changed

+99
-39
lines changed

6 files changed

+99
-39
lines changed

src/main/java/oracle/kubernetes/operator/Main.java

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package oracle.kubernetes.operator;
55

6+
import java.io.IOException;
67
import java.util.ArrayList;
78
import java.util.Collection;
89
import java.util.Collections;
@@ -91,19 +92,34 @@ public class Main {
9192
private static final FiberGate domainUpdaters = new FiberGate(engine);
9293
private static final ConcurrentMap<String, DomainPresenceInfo> domains = new ConcurrentHashMap<String, DomainPresenceInfo>();
9394

94-
private static final ConfigMapConsumer config = new ConfigMapConsumer("/operator/config");
95+
private static final ConfigMapConsumer config;
96+
static {
97+
try {
98+
config = new ConfigMapConsumer(engine.getExecutor(), "/operator/config", () -> {
99+
LOGGER.info(MessageKeys.TUNING_PARAMETERS);
100+
setTuningParameters();
101+
});
102+
} catch (IOException e) {
103+
LOGGER.warning(MessageKeys.EXCEPTION, e);
104+
throw new RuntimeException(e);
105+
}
106+
}
95107

96108
// tuning parameters
97-
private static final int statusUpdateTimeoutSeconds = (int) readTuningParameter("statusUpdateTimeoutSeconds", 10);
98-
private static final int unchangedCountToDelayStatusRecheck = (int) readTuningParameter("statueUpdateUnchangedCountToDelayStatusRecheck", 10);
99-
private static final long initialShortDelay = readTuningParameter("statusUpdateInitialShortDelay", 3);
100-
private static final long eventualLongDelay = readTuningParameter("statusUpdateEventualLongDelay", 30);
101-
static {
102-
int callRequestLimit = (int) readTuningParameter("callRequestLimit", 500);
103-
int callMaxRetryCount = (int) readTuningParameter("callMaxRetryCount", 5);
104-
int callTimeoutSeconds = (int) readTuningParameter("callTimeoutSeconds", 10);
109+
private static int statusUpdateTimeoutSeconds;
110+
private static int unchangedCountToDelayStatusRecheck;
111+
private static long initialShortDelay;
112+
private static long eventualLongDelay;
113+
private static void setTuningParameters() {
114+
statusUpdateTimeoutSeconds = (int) config.readTuningParameter("statusUpdateTimeoutSeconds", 10);
115+
unchangedCountToDelayStatusRecheck = (int) config.readTuningParameter("statueUpdateUnchangedCountToDelayStatusRecheck", 10);
116+
initialShortDelay = config.readTuningParameter("statusUpdateInitialShortDelay", 3);
117+
eventualLongDelay = config.readTuningParameter("statusUpdateEventualLongDelay", 30);
118+
int callRequestLimit = (int) config.readTuningParameter("callRequestLimit", 500);
119+
int callMaxRetryCount = (int) config.readTuningParameter("callMaxRetryCount", 5);
120+
int callTimeoutSeconds = (int) config.readTuningParameter("callTimeoutSeconds", 10);
105121
CallBuilder.setTuningParameters(callRequestLimit, callMaxRetryCount, callTimeoutSeconds);
106-
int watchLifetime = (int) readTuningParameter("watchLifetime", 45);
122+
int watchLifetime = (int) config.readTuningParameter("watchLifetime", 45);
107123
WatchBuilder.setTuningParameters(watchLifetime);
108124
}
109125

@@ -426,19 +442,6 @@ private static void normalizeDomainSpec(DomainSpec spec) {
426442
}
427443
}
428444

429-
private static long readTuningParameter(String parameter, long defaultValue) {
430-
String val = config.get(parameter);
431-
if (val != null) {
432-
try {
433-
return Long.parseLong(val);
434-
} catch (NumberFormatException nfe) {
435-
LOGGER.warning(MessageKeys.EXCEPTION, nfe);
436-
}
437-
}
438-
439-
return defaultValue;
440-
}
441-
442445
/**
443446
* Restarts the admin server, if already running
444447
* @param principal Service principal
@@ -499,7 +502,7 @@ private static void scheduleDomainStatusUpdating(DomainPresenceInfo info) {
499502
public void run() {
500503
Runnable r = this; // resolve visibility
501504
Packet packet = new Packet();
502-
packet.getComponents().put(ProcessingConstants.DOMAIN_COMPONENT_NAME, Component.createFor(info, version));
505+
packet.getComponents().put(ProcessingConstants.DOMAIN_COMPONENT_NAME, Component.createFor(info, version, config));
503506
Step strategy = DomainStatusUpdater.createStatusStep(statusUpdateTimeoutSeconds, null);
504507
domainUpdaters.startFiberIfNoCurrentFiber(domainUID, strategy, packet, new CompletionCallback() {
505508
@Override
@@ -601,7 +604,7 @@ private static void doCheckAndCreateDomainPresence(
601604
String ns = dom.getMetadata().getNamespace();
602605
if (initialized.getOrDefault(ns, Boolean.FALSE)) {
603606
PodWatcher pw = podWatchers.get(ns);
604-
p.getComponents().put(ProcessingConstants.DOMAIN_COMPONENT_NAME, Component.createFor(info, version, pw));
607+
p.getComponents().put(ProcessingConstants.DOMAIN_COMPONENT_NAME, Component.createFor(info, version, pw, config));
605608
p.put(ProcessingConstants.PRINCIPAL, principal);
606609

607610
if (explicitRestartAdmin) {
@@ -1514,8 +1517,7 @@ public NextAction onSuccess(Packet packet, V1Status result, int statusCode, Map<
15141517
private static Collection<String> getTargetNamespaces(String namespace) {
15151518
Collection<String> targetNamespaces = new ArrayList<String>();
15161519

1517-
ConfigMapConsumer cmc = new ConfigMapConsumer("/operator/config");
1518-
String tnValue = cmc.get("targetNamespaces");
1520+
String tnValue = config.get("targetNamespaces");
15191521
if (tnValue != null) {
15201522
StringTokenizer st = new StringTokenizer(tnValue, ",");
15211523
while (st.hasMoreTokens()) {

src/main/java/oracle/kubernetes/operator/helpers/ConfigMapConsumer.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@
99

1010
import java.io.File;
1111
import java.io.IOException;
12+
import java.nio.file.FileSystems;
1213
import java.nio.file.Files;
14+
import java.nio.file.WatchEvent;
15+
16+
import static java.nio.file.StandardWatchEventKinds.*;
17+
import java.nio.file.WatchService;
18+
import java.nio.file.WatchKey;
1319
import java.util.Collection;
1420
import java.util.HashSet;
21+
import java.util.List;
1522
import java.util.Map;
1623
import java.util.Set;
24+
import java.util.concurrent.ScheduledExecutorService;
25+
import java.util.concurrent.ScheduledFuture;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.atomic.AtomicReference;
1728

1829
/**
1930
* Kubernetes mounts ConfigMaps in the Pod's file-system as directories where the contained
@@ -24,11 +35,59 @@ public class ConfigMapConsumer implements Map<String, String> {
2435
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
2536

2637
private final File mountPointDir;
38+
private final WatchService watcher;
39+
private final ScheduledExecutorService threadPool;
40+
private final AtomicReference<ScheduledFuture<?>> future = new AtomicReference<>(null);
41+
private final Runnable onUpdate;
2742

28-
public ConfigMapConsumer(String mountPoint) {
43+
public ConfigMapConsumer(ScheduledExecutorService threadPool, String mountPoint, Runnable onUpdate) throws IOException {
44+
this.threadPool = threadPool;
2945
this.mountPointDir = new File(mountPoint);
46+
this.watcher = FileSystems.getDefault().newWatchService();
47+
this.onUpdate = onUpdate;
48+
if (mountPointDir.exists()) {
49+
mountPointDir.toPath().register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
50+
schedule();
51+
}
52+
}
53+
54+
private void schedule() {
55+
long initialDelay = readTuningParameter("configMapUpdateInitialDelay", 3);
56+
long delay = readTuningParameter("configMapUpdateDelay", 30);
57+
ScheduledFuture<?> old = future.getAndSet(threadPool.scheduleWithFixedDelay(() -> {
58+
// wait for key to be signaled
59+
WatchKey key;
60+
try {
61+
key = watcher.take();
62+
} catch (InterruptedException x) {
63+
return;
64+
}
65+
List<WatchEvent<?>> events = key.pollEvents();
66+
if (events != null && !events.isEmpty()) {
67+
onUpdate.run();
68+
schedule();
69+
return;
70+
}
71+
key.reset();
72+
}, initialDelay, delay, TimeUnit.SECONDS));
73+
if (old != null) {
74+
old.cancel(true);
75+
}
3076
}
3177

78+
public long readTuningParameter(String parameter, long defaultValue) {
79+
String val = get(parameter);
80+
if (val != null) {
81+
try {
82+
return Long.parseLong(val);
83+
} catch (NumberFormatException nfe) {
84+
LOGGER.warning(MessageKeys.EXCEPTION, nfe);
85+
}
86+
}
87+
88+
return defaultValue;
89+
}
90+
3291
@Override
3392
public int size() {
3493
String[] list = mountPointDir.list();

src/main/java/oracle/kubernetes/operator/helpers/PodHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public NextAction apply(Packet packet) {
195195
}
196196

197197
// Add internal-weblogic-operator-service certificate to Admin Server pod
198-
ConfigMapConsumer configMapHelper = new ConfigMapConsumer("/operator/config");
198+
ConfigMapConsumer configMapHelper = packet.getSPI(ConfigMapConsumer.class);
199199
String internalOperatorCert = configMapHelper.get(INTERNAL_OPERATOR_CERT_FILE);
200200
addEnvVar(container, INTERNAL_OPERATOR_CERT_ENV, internalOperatorCert);
201201

src/main/java/oracle/kubernetes/operator/logging/MessageKeys.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
* message keys makes the code more readable.
99
*/
1010
public class MessageKeys {
11-
12-
private MessageKeys() {
13-
// hide implicit public constructor
14-
}
11+
private MessageKeys() {}
1512

1613
public static final String OPERATOR_STARTED = "WLSKO-0000";
1714
public static final String CREATING_API_CLIENT = "WLSKO-0001";
@@ -139,5 +136,6 @@ private MessageKeys() {
139136
public static final String SERVER_SERVICE_DELETED = "WLSKO-0123";
140137
public static final String CLUSTER_SERVICE_DELETED = "WLSKO-0124";
141138
public static final String INGRESS_DELETED = "WLSKO-0125";
139+
public static final String TUNING_PARAMETERS = "WLSKO-0126";
142140

143141
}

src/main/java/oracle/kubernetes/operator/work/Engine.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ Container getContainer() {
4040
* @return executor
4141
*/
4242
public ScheduledExecutorService getExecutor() {
43+
if (threadPool == null) {
44+
synchronized (this) {
45+
threadPool = wrap(Executors.newScheduledThreadPool(DEFAULT_THREAD_COUNT, new DaemonThreadFactory()));
46+
}
47+
}
4348
return threadPool;
4449
}
4550

@@ -90,12 +95,7 @@ public void setExecutor(ScheduledExecutorService threadPool) {
9095
}
9196

9297
void addRunnable(Fiber fiber) {
93-
if (threadPool == null) {
94-
synchronized (this) {
95-
threadPool = wrap(Executors.newScheduledThreadPool(DEFAULT_THREAD_COUNT, new DaemonThreadFactory()));
96-
}
97-
}
98-
threadPool.execute(fiber);
98+
getExecutor().execute(fiber);
9999
}
100100

101101
private ScheduledExecutorService wrap(ScheduledExecutorService ex) {

src/main/resources/Operator.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,6 @@ WLSKO-0122=Pod for domain with domainUID {0} in namespace {1} and with server na
124124
WLSKO-0123=Service for domain with domainUID {0} in namespace {1} and with server name {2} deleted; validating domain
125125
WLSKO-0124=Service for domain with domainUID {0} in namespace {1} and with cluster name {2} deleted; validating domain
126126
WLSKO-0125=Ingress for domain with domainUID {0} in namespace {1} and with cluster name {2} deleted; validating domain
127+
WLSKO-0126=Reloading tuning parameters from Operator's config map
127128
128129

0 commit comments

Comments
 (0)