Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.zeppelin.interpreter.remote.RemoteInterpreterManagedProcess;
import org.apache.zeppelin.interpreter.remote.RemoteInterpreterServer;
import org.apache.zeppelin.interpreter.remote.RemoteInterpreterUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -46,6 +48,7 @@
import io.fabric8.kubernetes.api.model.PodStatus;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.LocalPortForward;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.dsl.ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;

public class K8sRemoteInterpreterProcess extends RemoteInterpreterManagedProcess {
Expand Down Expand Up @@ -143,16 +146,16 @@ public void start(String userName) throws IOException {

// special handling if we doesn't want timeout the process during lifecycle phase pending
if (!timeoutDuringPending) {
while (!StringUtils.equalsAnyIgnoreCase(getPodPhase(), "Succeeded", "Failed", "Running")
&& !Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LOGGER.error("Interrupt received during pending phase. Try to stop the interpreter and interrupt the current thread.", e);
processStopped("Start process was interrupted during the pending phase");
stop();
Thread.currentThread().interrupt();
}
// WATCH
PodPhaseWatcher podWatcher = new PodPhaseWatcher(
phase -> StringUtils.equalsAnyIgnoreCase(phase, "Succeeded", "Failed", "Running"));
try (Watch watch = client.pods().inNamespace(namespace).withName(podName).watch(podWatcher)) {
podWatcher.getCountDownLatch().await();
} catch (InterruptedException e) {
LOGGER.error("Interrupt received during waiting for Running phase. Try to stop the interpreter and interrupt the current thread.", e);
processStopped("Start process was interrupted during waiting for Running phase");
stop();
Thread.currentThread().interrupt();
}
}

Expand Down Expand Up @@ -183,6 +186,18 @@ public void start(String userName) throws IOException {
@Override
public void stop() {
super.stop();
// WATCH for soft shutdown
PodPhaseWatcher podWatcher = new PodPhaseWatcher(phase -> StringUtils.equalsAny(phase, "Succeeded", "Failed"));
try (Watch watch = client.pods().inNamespace(namespace).withName(podName).watch(podWatcher)) {
if (!podWatcher.getCountDownLatch().await(RemoteInterpreterServer.DEFAULT_SHUTDOWN_TIMEOUT + 500,
TimeUnit.MILLISECONDS)) {
LOGGER.warn("Pod {} doesn't terminate in time", podName);
}
} catch (InterruptedException e) {
LOGGER.error("Interruption received while waiting for stop.", e);
processStopped("Stop process was interrupted during termination");
Thread.currentThread().interrupt();
}
Properties templateProperties = getTemplateBindings(null);
// delete pod
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.zeppelin.interpreter.launcher;

import java.util.concurrent.CountDownLatch;
import java.util.function.Predicate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodStatus;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watcher;

public class PodPhaseWatcher implements Watcher<Pod> {
private static final Logger LOGGER = LoggerFactory.getLogger(PodPhaseWatcher.class);
private final CountDownLatch countDownLatch;
private final Predicate<String> predicate;

public PodPhaseWatcher(Predicate<String> predicate) {
this.countDownLatch = new CountDownLatch(1);
this.predicate = predicate;
}

@Override
public void eventReceived(Action action, Pod pod) {
PodStatus status = pod.getStatus();
if (status != null && predicate.test(status.getPhase())) {
LOGGER.info("Pod {} meets phase {}", pod.getMetadata().getName(), status.getPhase());
countDownLatch.countDown();
}
}

@Override
public void onClose(KubernetesClientException cause) {
if (cause != null) {
LOGGER.error("PodWatcher exits abnormally", cause);
}
// always count down, so threads that are waiting will continue
countDownLatch.countDown();
}

public CountDownLatch getCountDownLatch() {
return countDownLatch;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.zeppelin.interpreter.launcher;

import static org.junit.Assert.*;

import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.junit.Rule;
import org.junit.Test;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.PodStatus;
import io.fabric8.kubernetes.api.model.PodStatusBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;

public class PodPhaseWatcherTest {

@Rule
public KubernetesServer server = new KubernetesServer(true, true);

@Test
public void testPhase() throws InterruptedException {
KubernetesClient client = server.getClient();
// CREATE
client.pods().inNamespace("ns1")
.create(new PodBuilder().withNewMetadata().withName("pod1").endMetadata().build());
// READ
PodList podList = client.pods().inNamespace("ns1").list();
assertNotNull(podList);
assertEquals(1, podList.getItems().size());
Pod pod = podList.getItems().get(0);
// WATCH
PodPhaseWatcher podWatcher = new PodPhaseWatcher(
phase -> StringUtils.equalsAnyIgnoreCase(phase, "Succeeded", "Failed", "Running"));
Watch watch = client.pods().inNamespace("ns1").withName("pod1").watch(podWatcher);

// Update Pod to "pending" phase
pod.setStatus(new PodStatus(null, null, null, null, null, null, null, "Pending", null, null,
null, null, null));
client.pods().inNamespace("ns1").updateStatus(pod);

// Update Pod to "Running" phase
pod.setStatus(new PodStatusBuilder(new PodStatus(null, null, null, null, null, null, null,
"Running", null, null, null, null, null)).build());
client.pods().inNamespace("ns1").updateStatus(pod);

assertTrue(podWatcher.getCountDownLatch().await(1, TimeUnit.SECONDS));
watch.close();
}

@Test
public void testPhaseWithError() throws InterruptedException {
KubernetesClient client = server.getClient();
// CREATE
client.pods().inNamespace("ns1")
.create(new PodBuilder().withNewMetadata().withName("pod1").endMetadata().build());
// READ
PodList podList = client.pods().inNamespace("ns1").list();
assertNotNull(podList);
assertEquals(1, podList.getItems().size());
// WATCH
PodPhaseWatcher podWatcher = new PodPhaseWatcher(
phase -> StringUtils.equalsAnyIgnoreCase(phase, "Succeeded", "Failed", "Running"));
Watch watch = client.pods().inNamespace("ns1").withName("pod1").watch(podWatcher);

// In the case of close, we do not block thread execution
watch.close();
assertTrue(podWatcher.getCountDownLatch().await(1, TimeUnit.SECONDS));
}
}