Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CDAP-21096] Write appfabric server port to tmp file for k8s readiness probe #15811

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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 @@ -53,8 +53,12 @@
import io.cdap.http.AbstractHandlerHook;
import io.cdap.http.HttpHandler;
import io.cdap.http.NettyHttpService;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -242,6 +246,9 @@ private Cancellable startHttpService(NettyHttpService httpService) throws Except
ResolvingDiscoverable.of(uriScheme.createDiscoverable(serviceName, socketAddress))));
}

writeServerPortToFile(Constants.Service.APP_FABRIC_HTTP, announcePort);
LOG.debug("AppFabric HTTP server started.");

return () -> {
LOG.debug("Stopping AppFabric HTTP service.");
for (Cancellable cancellable : cancellables) {
Expand All @@ -259,4 +266,12 @@ private Cancellable startHttpService(NettyHttpService httpService) throws Except
LOG.info("AppFabric HTTP service stopped.");
};
}

private void writeServerPortToFile(String service, int port) throws IOException {
String dir = cConf.get(Constants.Service.SERVER_PORT_DIR);
String filename = service + ".port";
File file = new File(dir, filename);
LOG.debug("Writing server port {} to file: {}", port, file.getAbsolutePath());
Files.write(file.toPath(), String.valueOf(port).getBytes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
import io.cdap.cdap.common.conf.CConfiguration;
import io.cdap.cdap.common.conf.Constants;
import io.cdap.cdap.common.conf.SConfiguration;
import io.cdap.cdap.common.conf.StringUtils;
import io.cdap.cdap.common.discovery.EndpointStrategy;
import io.cdap.cdap.common.discovery.RandomEndpointStrategy;
import io.cdap.cdap.common.discovery.URIScheme;
import io.cdap.cdap.common.utils.Tasks;
import io.cdap.cdap.internal.AppFabricTestHelper;
import io.cdap.cdap.security.server.LdapLoginModule;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import javax.net.ssl.SSLSocket;
Expand Down Expand Up @@ -96,4 +99,35 @@ public void testSsl() throws IOException {
AppFabricTestHelper.shutdown();
}
}

@Test
public void testServerPortFile() throws Exception {
Injector injector = AppFabricTestHelper.getInjector();
try {
AppFabricServer server = injector.getInstance(AppFabricServer.class);
DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
Service.State state = server.startAndWait();
Assert.assertSame(state, Service.State.RUNNING);

final EndpointStrategy endpointStrategy = new RandomEndpointStrategy(
() -> discoveryServiceClient.discover(Constants.Service.APP_FABRIC_HTTP));
Assert.assertNotNull(endpointStrategy.pick(5, TimeUnit.SECONDS));

// Check file exists and port number is in range of 0 to 65535.
File file = new File("/tmp/appfabric.port");
Assert.assertTrue("server port file doesn't exist",
file.exists() && !file.isDirectory());
byte[] bytes = Files.readAllBytes(file.toPath());
String content = new String(bytes);
Assert.assertTrue(Integer.valueOf(content) > 0);
Assert.assertTrue(Integer.valueOf(content) < 65535);

state = server.stopAndWait();
Assert.assertSame(state, Service.State.TERMINATED);

Tasks.waitFor(true, () -> endpointStrategy.pick() == null, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
} finally {
AppFabricTestHelper.shutdown();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ public static final class Service {
public static final String REMOTE_AGENT_SERVICE = "remote.agent.service";
public static final String ARTIFACT_CACHE_SERVICE = "artifact.cache.service";
public static final String RUNTIME_MONITOR_RETRY_PREFIX = "system.runtime.monitor.";

public static final String SERVER_PORT_DIR = "system.services.server.dir";
}

/**
Expand Down
8 changes: 8 additions & 0 deletions cdap-common/src/main/resources/cdap-default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6563,4 +6563,12 @@
</description>
</property>

<property>
<name>system.services.server.dir</name>
<value>/tmp</value>
<description>
Directory for storing files related to server for system services.
</description>
</property>

</configuration>
Loading