Skip to content

Commit 62b25a2

Browse files
committed
Review comments and Javadoc for FiberGate
1 parent 41eb97d commit 62b25a2

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ public void onCompletion(Packet packet) {
544544
public void onThrowable(Packet packet, Throwable throwable) {
545545
LOGGER.severe(MessageKeys.EXCEPTION, throwable);
546546

547-
domainUpdaters.replaceAndStartFiber(domainUID, Fiber.getCurrentIfSet(), DomainStatusUpdater.createFailedStep(throwable, null), p, new CompletionCallback() {
547+
domainUpdaters.startFiberIfLastFiberMatches(domainUID, Fiber.getCurrentIfSet(), DomainStatusUpdater.createFailedStep(throwable, null), p, new CompletionCallback() {
548548
@Override
549549
public void onCompletion(Packet packet) {
550550
// no-op

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

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,57 @@
1111
import oracle.kubernetes.operator.work.Fiber.CompletionCallback;
1212
import oracle.kubernetes.operator.work.Fiber.ExitCallback;
1313

14+
/**
15+
* Allows at most one running Fiber per key value. However, rather than queue later arriving Fibers this class cancels
16+
* the earlier arriving Fibers. For the operator, this makes sense as domain presence Fibers that come later will always complete
17+
* or correct work that may have been in-flight.
18+
*/
1419
public class FiberGate {
1520
private final Engine engine;
1621
private final ConcurrentMap<String, Fiber> gateMap = new ConcurrentHashMap<String, Fiber>();
1722

23+
/**
24+
* Constructor taking Engine for running Fibers
25+
* @param engine Engine
26+
*/
1827
public FiberGate(Engine engine) {
1928
this.engine = engine;
2029
}
2130

31+
/**
32+
* Starts Fiber that cancels any earlier running Fibers with the same key. Fiber map is not updated if no Fiber
33+
* is started.
34+
* @param key Key
35+
* @param strategy Step for Fiber to begin with
36+
* @param packet Packet
37+
* @param callback Completion callback
38+
* @return started Fiber
39+
*/
2240
public Fiber startFiber(String key, Step strategy, Packet packet, CompletionCallback callback) {
23-
return replaceAndStartFiber(key, null, strategy, packet, callback);
41+
return startFiberIfLastFiberMatches(key, null, strategy, packet, callback);
2442
}
2543

26-
public Fiber replaceAndStartFiber(String key, Fiber old, Step strategy, Packet packet, CompletionCallback callback) {
44+
/**
45+
* Starts Fiber only if the last started Fiber matches the given old Fiber.
46+
* @param key Key
47+
* @param old Expected last Fiber
48+
* @param strategy Step for Fiber to begin with
49+
* @param packet Packet
50+
* @param callback Completion callback
51+
* @return started Fiber, or null, if no Fiber started
52+
*/
53+
public synchronized Fiber startFiberIfLastFiberMatches(String key, Fiber old, Step strategy, Packet packet, CompletionCallback callback) {
2754
Fiber f = engine.createFiber();
2855
WaitForOldFiberStep wfofs;
29-
synchronized (this) {
30-
if (old != null) {
31-
if (!gateMap.replace(key, old, f)) {
32-
return null;
33-
}
34-
} else {
35-
old = gateMap.put(key, f);
56+
if (old != null) {
57+
if (!gateMap.replace(key, old, f)) {
58+
return null;
3659
}
37-
wfofs = new WaitForOldFiberStep(old, strategy);
38-
f.getComponents().put(ProcessingConstants.FIBER_COMPONENT_NAME, Component.createFor(wfofs));
60+
} else {
61+
old = gateMap.put(key, f);
3962
}
63+
wfofs = new WaitForOldFiberStep(old, strategy);
64+
f.getComponents().put(ProcessingConstants.FIBER_COMPONENT_NAME, Component.createFor(wfofs));
4065
f.start(wfofs, packet, new CompletionCallback() {
4166
@Override
4267
public void onCompletion(Packet packet) {
@@ -87,5 +112,4 @@ public void onExit() {
87112
});
88113
}
89114
}
90-
91115
}

0 commit comments

Comments
 (0)