Skip to content

Commit 7d0d748

Browse files
committed
Clarify stopping versus starting
1 parent 9358043 commit 7d0d748

File tree

4 files changed

+63
-24
lines changed

4 files changed

+63
-24
lines changed

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,6 @@ static void makeRightDomainPresence(
612612
boolean explicitRecheck,
613613
boolean isDeleting,
614614
boolean isWillInterrupt) {
615-
LOGGER.entering();
616-
617615
DomainSpec spec = null;
618616
String ns;
619617
if (dom != null) {
@@ -624,25 +622,27 @@ static void makeRightDomainPresence(
624622
ns = existing.getNamespace();
625623
}
626624

627-
if (existing != null) {
628-
Domain current = existing.getDomain();
629-
if (current != null) {
630-
// Is this an outdated watch event?
631-
if (isOutdated(current.getMetadata(), dom.getMetadata())) {
632-
LOGGER.fine(MessageKeys.NOT_STARTING_DOMAINUID_THREAD, domainUID);
633-
return;
634-
}
635-
// Has the spec actually changed? We will get watch events for status updates
636-
if (!explicitRecheck && spec != null && spec.equals(current.getSpec())) {
637-
// nothing in the spec has changed, but status likely did; update current
638-
existing.setDomain(dom);
639-
LOGGER.fine(MessageKeys.NOT_STARTING_DOMAINUID_THREAD, domainUID);
640-
return;
625+
if (!Main.isNamespaceStopping(ns).get()) {
626+
if (existing != null) {
627+
Domain current = existing.getDomain();
628+
if (current != null) {
629+
// Is this an outdated watch event?
630+
if (isOutdated(current.getMetadata(), dom.getMetadata())) {
631+
LOGGER.fine(MessageKeys.NOT_STARTING_DOMAINUID_THREAD, domainUID);
632+
return;
633+
}
634+
// Has the spec actually changed? We will get watch events for status updates
635+
if (!explicitRecheck && spec != null && spec.equals(current.getSpec())) {
636+
// nothing in the spec has changed, but status likely did; update current
637+
existing.setDomain(dom);
638+
LOGGER.fine(MessageKeys.NOT_STARTING_DOMAINUID_THREAD, domainUID);
639+
return;
640+
}
641641
}
642642
}
643-
}
644643

645-
internalMakeRightDomainPresence(existing, dom, domainUID, ns, isDeleting, isWillInterrupt);
644+
internalMakeRightDomainPresence(existing, dom, domainUID, ns, isDeleting, isWillInterrupt);
645+
}
646646
}
647647

648648
private static void internalMakeRightDomainPresence(

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ private static ThreadFactory getThreadFactory() {
6767

6868
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
6969

70+
private static final String NS_STARTING_NOW = "NS_STARTING_NOW";
71+
7072
static final TuningParameters tuningAndConfig;
7173

7274
static {
@@ -102,6 +104,7 @@ private static ThreadFactory getThreadFactory() {
102104

103105
static final Engine engine = new Engine(wrappedExecutorService);
104106

107+
static final ConcurrentMap<String, AtomicBoolean> isNamespaceStarted = new ConcurrentHashMap<>();
105108
static final ConcurrentMap<String, AtomicBoolean> isNamespaceStopping = new ConcurrentHashMap<>();
106109

107110
private static final Map<String, ConfigMapWatcher> configMapWatchers = new ConcurrentHashMap<>();
@@ -262,7 +265,11 @@ private static class StartNamespaceBeforeStep extends Step {
262265

263266
@Override
264267
public NextAction apply(Packet packet) {
265-
if (isNamespaceStopping(ns).get()) {
268+
AtomicBoolean a = isNamespaceStarted.computeIfAbsent(ns, (key) -> new AtomicBoolean(false));
269+
boolean startingNow = !a.getAndSet(true);
270+
packet.put(NS_STARTING_NOW, (Boolean) startingNow);
271+
272+
if (startingNow) {
266273
try {
267274
HealthCheckHelper.performSecurityChecks(version, operatorNamespace, ns);
268275
} catch (Throwable e) {
@@ -281,11 +288,12 @@ private static void stopNamespaces(Collection<String> namespacesToStop) {
281288
if (stopping != null) {
282289
stopping.set(true);
283290
}
291+
isNamespaceStarted.remove(ns);
284292
}
285293
}
286294

287295
static AtomicBoolean isNamespaceStopping(String ns) {
288-
return isNamespaceStopping.computeIfAbsent(ns, (key) -> new AtomicBoolean(true));
296+
return isNamespaceStopping.computeIfAbsent(ns, (key) -> new AtomicBoolean(false));
289297
}
290298

291299
static void runSteps(Step firstStep) {
@@ -526,15 +534,18 @@ public NextAction onFailure(Packet packet, CallResponse<DomainList> callResponse
526534

527535
@Override
528536
public NextAction onSuccess(Packet packet, CallResponse<DomainList> callResponse) {
529-
boolean starting = isNamespaceStopping.putIfAbsent(ns, new AtomicBoolean(false)) == null;
537+
Boolean startingNow = (Boolean) packet.get(NS_STARTING_NOW);
538+
if (startingNow == null) {
539+
startingNow = Boolean.TRUE;
540+
}
530541

531542
Set<String> domainUIDs = new HashSet<>();
532543
if (callResponse.getResult() != null) {
533544
for (Domain dom : callResponse.getResult().getItems()) {
534545
String domainUID = dom.getSpec().getDomainUID();
535546
domainUIDs.add(domainUID);
536547
DomainPresenceInfo info = DomainPresenceInfoManager.getOrCreate(dom);
537-
if (starting) {
548+
if (startingNow) {
538549
// Update domain here if namespace is not yet running
539550
info.setDomain(dom);
540551
}

operator/src/test/java/oracle/kubernetes/operator/DomainPresenceTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public void setUp() throws Exception {
9696
mementos.add(
9797
installStub(DomainProcessor.class, "FIBER_GATE", testSupport.createFiberGateStub()));
9898

99+
getStartedVariable();
99100
isNamespaceStopping = getStoppingVariable();
101+
isNamespaceStopping.computeIfAbsent(NS, k -> new AtomicBoolean(true)).set(true);
100102
}
101103

102104
private Map getDomainPresenceInfoMap() throws NoSuchFieldException {
@@ -109,14 +111,19 @@ private static Memento installStub(Class<?> containingClass, String fieldName, O
109111
return StaticStubSupport.install(containingClass, fieldName, newValue);
110112
}
111113

114+
private Map<String, AtomicBoolean> getStartedVariable() throws NoSuchFieldException {
115+
Memento startedMemento = StaticStubSupport.preserve(Main.class, "isNamespaceStarted");
116+
return startedMemento.getOriginalValue();
117+
}
118+
112119
private Map<String, AtomicBoolean> getStoppingVariable() throws NoSuchFieldException {
113120
Memento stoppingMemento = StaticStubSupport.preserve(Main.class, "isNamespaceStopping");
114121
return stoppingMemento.getOriginalValue();
115122
}
116123

117124
@After
118125
public void tearDown() throws Exception {
119-
isNamespaceStopping.forEach((key, value) -> value.set(true));
126+
isNamespaceStopping.computeIfAbsent(NS, k -> new AtomicBoolean(true)).set(true);
120127
shutDownThreads();
121128

122129
for (Memento memento : mementos) memento.revert();
@@ -133,7 +140,7 @@ public void whenNoPreexistingDomains_createEmptyDomainPresenceInfoMap() {
133140

134141
private void readExistingResources() {
135142
createCannedListDomainResponses();
136-
testSupport.runSteps(Main.readExistingResources("operator", NS));
143+
testSupport.runStepsToCompletion(Main.readExistingResources("operator", NS));
137144
}
138145

139146
@Test
@@ -477,6 +484,8 @@ public void whenStrandedResourcesExist_removeThem() {
477484
.ignoringBody()
478485
.returning(new V1Status());
479486

487+
isNamespaceStopping.get(NS).set(false);
488+
480489
readExistingResources();
481490

482491
testSupport.verifyAllDefinedResponsesInvoked();

operator/src/test/java/oracle/kubernetes/operator/work/FiberTestSupport.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Queue;
1212
import java.util.SortedSet;
1313
import java.util.TreeSet;
14+
import java.util.concurrent.ExecutionException;
1415
import java.util.concurrent.ScheduledExecutorService;
1516
import java.util.concurrent.ScheduledFuture;
1617
import java.util.concurrent.TimeUnit;
@@ -119,6 +120,24 @@ public FiberTestSupport addVersion(KubernetesVersion kubernetesVersion) {
119120
*/
120121
public Packet runSteps(Step step) {
121122
fiber.start(step, packet, completionCallback);
123+
124+
return packet;
125+
}
126+
127+
/**
128+
* Starts a unit-test fiber with the specified step and runs until the fiber is done
129+
*
130+
* @param step the first step to run
131+
*/
132+
public Packet runStepsToCompletion(Step step) {
133+
fiber.start(step, packet, completionCallback);
134+
135+
// Wait for fiber to finish
136+
try {
137+
fiber.get();
138+
} catch (InterruptedException | ExecutionException e) {
139+
throw new RuntimeException(e);
140+
}
122141
return packet;
123142
}
124143

0 commit comments

Comments
 (0)