Skip to content

Commit 87aa1a9

Browse files
authored
Merge pull request #169 from oracle/executor
Share executor service
2 parents 4b1b54b + c566b34 commit 87aa1a9

File tree

5 files changed

+31
-74
lines changed

5 files changed

+31
-74
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.concurrent.ConcurrentHashMap;
1616
import java.util.concurrent.ConcurrentMap;
1717
import java.util.concurrent.Executors;
18+
import java.util.concurrent.ScheduledExecutorService;
1819
import java.util.concurrent.ScheduledFuture;
1920
import java.util.concurrent.ThreadFactory;
2021
import java.util.concurrent.TimeUnit;
@@ -70,7 +71,6 @@
7071
import oracle.kubernetes.operator.wlsconfig.WlsServerConfig;
7172
import oracle.kubernetes.operator.work.Component;
7273
import oracle.kubernetes.operator.work.Container;
73-
import oracle.kubernetes.operator.work.ContainerResolver;
7474
import oracle.kubernetes.operator.work.Engine;
7575
import oracle.kubernetes.operator.work.Fiber;
7676
import oracle.kubernetes.operator.work.Fiber.CompletionCallback;
@@ -111,13 +111,17 @@ public class Main {
111111
private static final CallBuilderFactory callBuilderFactory = new CallBuilderFactory(tuningAndConfig);
112112

113113
private static final Container container = new Container();
114+
private static final ScheduledExecutorService wrappedExecutorService =
115+
Engine.wrappedExecutorService("operator", container);
116+
114117
static {
115118
container.getComponents().put(
116119
ProcessingConstants.MAIN_COMPONENT_NAME,
117-
Component.createFor(TuningParameters.class, tuningAndConfig, callBuilderFactory));
120+
Component.createFor(ScheduledExecutorService.class, wrappedExecutorService,
121+
TuningParameters.class, tuningAndConfig, callBuilderFactory));
118122
}
119123

120-
private static final Engine engine = new Engine("operator", container);
124+
private static final Engine engine = new Engine(wrappedExecutorService);
121125
private static final FiberGate domainUpdaters = new FiberGate(engine);
122126

123127
private static final ConcurrentMap<String, Boolean> initialized = new ConcurrentHashMap<>();

operator/src/main/java/oracle/kubernetes/operator/http/HttpClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import io.kubernetes.client.ApiException;
77
import io.kubernetes.client.models.V1Service;
88
import io.kubernetes.client.models.V1ServiceSpec;
9-
import oracle.kubernetes.operator.helpers.CallBuilder;
109
import oracle.kubernetes.operator.helpers.CallBuilderFactory;
1110
import oracle.kubernetes.operator.helpers.SecretHelper;
1211
import oracle.kubernetes.operator.logging.LoggingFacade;

operator/src/main/java/oracle/kubernetes/operator/wlsconfig/WlsRetriever.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import oracle.kubernetes.operator.logging.LoggingFacade;
1515
import oracle.kubernetes.operator.logging.LoggingFactory;
1616
import oracle.kubernetes.operator.logging.MessageKeys;
17+
import oracle.kubernetes.operator.work.ContainerResolver;
1718
import oracle.kubernetes.operator.work.NextAction;
1819
import oracle.kubernetes.operator.work.Packet;
1920
import oracle.kubernetes.operator.work.Step;
@@ -24,9 +25,8 @@
2425
import java.util.Random;
2526
import java.util.concurrent.ConcurrentMap;
2627
import java.util.concurrent.ExecutionException;
27-
import java.util.concurrent.ExecutorService;
28-
import java.util.concurrent.Executors;
2928
import java.util.concurrent.Future;
29+
import java.util.concurrent.ScheduledExecutorService;
3030
import java.util.concurrent.TimeUnit;
3131
import java.util.concurrent.TimeoutException;
3232

@@ -293,7 +293,7 @@ public WlsDomainConfig readConfig(String principal) {
293293
LOGGER.entering();
294294

295295
long timeout = READ_CONFIG_TIMEOUT_MILLIS;
296-
ExecutorService executorService = Executors.newSingleThreadExecutor();
296+
ScheduledExecutorService executorService = ContainerResolver.getInstance().getContainer().getSPI(ScheduledExecutorService.class);
297297
long startTime = System.currentTimeMillis();
298298
Future<WlsDomainConfig> future = executorService.submit(() -> getWlsDomainConfig(principal, timeout));
299299
executorService.shutdown();

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

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,93 +13,45 @@
1313
* Collection of {@link Fiber}s. Owns an {@link Executor} to run them.
1414
*/
1515
public class Engine {
16-
private final int DEFAULT_THREAD_COUNT = 10;
17-
18-
private volatile ScheduledExecutorService threadPool;
19-
public final String id;
20-
private final Container container;
21-
22-
/**
23-
* Returns identifier for this Engine
24-
* @return identifier
25-
*/
26-
String getId() {
27-
return id;
16+
private static final int DEFAULT_THREAD_COUNT = 10;
17+
18+
public static ScheduledExecutorService wrappedExecutorService(String id, Container container) {
19+
return wrap(container, Executors.newScheduledThreadPool(DEFAULT_THREAD_COUNT,
20+
new DaemonThreadFactory(id)));
2821
}
2922

30-
/**
31-
* Returns the container
32-
* @return container
33-
*/
34-
Container getContainer() {
35-
return container;
36-
}
23+
private volatile ScheduledExecutorService threadPool;
3724

3825
/**
3926
* Returns the executor
4027
* @return executor
4128
*/
4229
public ScheduledExecutorService getExecutor() {
43-
if (threadPool == null) {
44-
synchronized (this) {
45-
threadPool = wrap(Executors.newScheduledThreadPool(DEFAULT_THREAD_COUNT, new DaemonThreadFactory()));
46-
}
47-
}
4830
return threadPool;
4931
}
5032

5133
/**
52-
* Creates engine with the specified id, default container and specified executor
53-
* @param id Engine id
54-
* @param threadPool Executor
55-
*/
56-
public Engine(String id, ScheduledExecutorService threadPool) {
57-
this(id, ContainerResolver.getDefault().getContainer(), threadPool);
58-
}
59-
60-
/**
61-
* Creates engine with the specified id, container and executor
62-
* @param id Engine id
63-
* @param container Container
34+
* Creates engine with the specified executor
6435
* @param threadPool Executor
6536
*/
66-
public Engine(String id, Container container, ScheduledExecutorService threadPool) {
67-
this(id, container);
68-
this.threadPool = threadPool != null ? wrap(threadPool) : null;
37+
public Engine(ScheduledExecutorService threadPool) {
38+
this.threadPool = threadPool;
6939
}
7040

7141
/**
7242
* Creates engine with the specified id and default container and executor
7343
* @param id Engine id
7444
*/
7545
public Engine(String id) {
76-
this(id, ContainerResolver.getDefault().getContainer());
77-
}
78-
79-
/**
80-
* Creates engine with the specified id, container and default executor
81-
* @param id Engine id
82-
* @param container Container
83-
*/
84-
public Engine(String id, Container container) {
85-
this.id = id;
86-
this.container = container;
87-
}
88-
89-
/**
90-
* Sets the executor
91-
* @param threadPool Executor
92-
*/
93-
public void setExecutor(ScheduledExecutorService threadPool) {
94-
this.threadPool = threadPool != null ? wrap(threadPool) : null;
46+
this(wrappedExecutorService(id, ContainerResolver.getDefault().getContainer()));
9547
}
9648

9749
void addRunnable(Fiber fiber) {
9850
getExecutor().execute(fiber);
9951
}
10052

101-
private ScheduledExecutorService wrap(ScheduledExecutorService ex) {
102-
return ContainerResolver.getDefault().wrapExecutor(container, ex);
53+
private static ScheduledExecutorService wrap(Container container, ScheduledExecutorService ex) {
54+
return container != null ? ContainerResolver.getDefault().wrapExecutor(container, ex) : ex;
10355
}
10456

10557
/**
@@ -120,11 +72,11 @@ Fiber createChildFiber(Fiber parent) {
12072
return new Fiber(this, parent);
12173
}
12274

123-
private class DaemonThreadFactory implements ThreadFactory {
75+
private static class DaemonThreadFactory implements ThreadFactory {
12476
final AtomicInteger threadNumber = new AtomicInteger(1);
12577
final String namePrefix;
12678

127-
DaemonThreadFactory() {
79+
DaemonThreadFactory(String id) {
12880
namePrefix = "engine-" + id + "-thread-";
12981
}
13082

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,12 @@ public void run() {
479479

480480
final Fiber oldFiber = CURRENT_FIBER.get();
481481
CURRENT_FIBER.set(this);
482-
Container oldContainer = ContainerResolver.getDefault().enterContainer(owner.getContainer());
483482
try {
484483
// doRun returns true to indicate an early exit from fiber processing
485484
if (!doRun(next)) {
486485
completionCheck();
487486
}
488487
} finally {
489-
ContainerResolver.getDefault().exitContainer(oldContainer);
490488
CURRENT_FIBER.set(oldFiber);
491489
}
492490
}
@@ -660,9 +658,13 @@ private String getName() {
660658
sb.append(parent.getName());
661659
sb.append("-child-");
662660
} else {
663-
sb.append("engine-");
664-
sb.append(owner.id);
665-
sb.append("-fiber-");
661+
synchronized (this) {
662+
if (currentThread != null) {
663+
sb.append(currentThread.getName());
664+
sb.append("-");
665+
}
666+
}
667+
sb.append("fiber-");
666668
}
667669
sb.append(id);
668670
return sb.toString();

0 commit comments

Comments
 (0)