Skip to content

Commit 92ebb3a

Browse files
committed
Update pauser to handle exception and run unpause as long as possible
1 parent 9b87019 commit 92ebb3a

File tree

1 file changed

+38
-12
lines changed
  • lib/src/main/java/com/scalar/admin/kubernetes

1 file changed

+38
-12
lines changed

lib/src/main/java/com/scalar/admin/kubernetes/Pauser.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.scalar.admin.kubernetes;
22

3+
import com.google.common.annotations.VisibleForTesting;
34
import com.google.common.util.concurrent.Uninterruptibles;
45
import com.scalar.admin.RequestCoordinator;
56
import io.kubernetes.client.openapi.Configuration;
@@ -34,7 +35,7 @@
3435
@NotThreadSafe
3536
public class Pauser {
3637

37-
private static final int MAX_UNPAUSE_RETRY_COUNT = 3;
38+
@VisibleForTesting static final int MAX_UNPAUSE_RETRY_COUNT = 3;
3839

3940
private final Logger logger = LoggerFactory.getLogger(Pauser.class);
4041
private final TargetSelector targetSelector;
@@ -84,17 +85,30 @@ public PausedDuration pause(int pauseDuration, @Nullable Long maxPauseWaitTime)
8485
throw new PauserException("Failed to find the target pods to pause.", e);
8586
}
8687

87-
RequestCoordinator coordinator = getRequestCoordinator(target);
88+
RequestCoordinator coordinator;
89+
try {
90+
coordinator = getRequestCoordinator(target);
91+
} catch (Exception e) {
92+
throw new PauserException("Failed to initialize the coordinator.", e);
93+
}
94+
95+
Instant startTime;
96+
Instant endTime;
97+
try {
98+
coordinator.pause(true, maxPauseWaitTime);
8899

89-
coordinator.pause(true, maxPauseWaitTime);
100+
startTime = Instant.now();
90101

91-
Instant startTime = Instant.now();
102+
Uninterruptibles.sleepUninterruptibly(pauseDuration, TimeUnit.MILLISECONDS);
92103

93-
Uninterruptibles.sleepUninterruptibly(pauseDuration, TimeUnit.MILLISECONDS);
104+
endTime = Instant.now();
94105

95-
Instant endTime = Instant.now();
106+
unpauseWithRetry(coordinator, MAX_UNPAUSE_RETRY_COUNT, target);
96107

97-
unpauseWithRetry(coordinator, MAX_UNPAUSE_RETRY_COUNT, target);
108+
} catch (Exception e) {
109+
unpauseWithRetry(coordinator, MAX_UNPAUSE_RETRY_COUNT, target);
110+
throw e;
111+
}
98112

99113
TargetSnapshot targetAfterPause;
100114
try {
@@ -113,8 +127,9 @@ public PausedDuration pause(int pauseDuration, @Nullable Long maxPauseWaitTime)
113127
return new PausedDuration(startTime, endTime);
114128
}
115129

116-
private void unpauseWithRetry(
117-
RequestCoordinator coordinator, int maxRetryCount, TargetSnapshot target) {
130+
@VisibleForTesting
131+
void unpauseWithRetry(RequestCoordinator coordinator, int maxRetryCount, TargetSnapshot target)
132+
throws PauserException {
118133
int retryCounter = 0;
119134

120135
while (true) {
@@ -123,12 +138,23 @@ private void unpauseWithRetry(
123138
return;
124139
} catch (Exception e) {
125140
if (++retryCounter >= maxRetryCount) {
126-
logger.warn(
141+
// If someone uses this library directly instead of using our CLI, users should handle the
142+
// exception properly. However, this case is a very critical issue. Therefore, we output
143+
// the error message here despite whether the exception is handled or not on the caller
144+
// side.
145+
logger.error(
127146
"Failed to unpause Scalar product. They are still in paused. You must restart related"
128147
+ " pods by using the `kubectl rollout restart deployment {}`"
129-
+ " command to unpase all pods.",
148+
+ " command to unpause all pods.",
130149
target.getDeployment().getMetadata().getName());
131-
return;
150+
// In our CLI, we catch this exception and output the message as an error on the CLI side.
151+
throw new PauserException(
152+
String.format(
153+
"Failed to unpause Scalar product. They are still in paused. You must restart"
154+
+ " related pods by using the `kubectl rollout restart deployment %s` command"
155+
+ " to unpause all pods.",
156+
target.getDeployment().getMetadata().getName()),
157+
e);
132158
}
133159
}
134160
}

0 commit comments

Comments
 (0)