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

IGNITE-20512 Remove port range from HTTP server #2673

Merged
merged 16 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ javax = "1.3.2"
jetbrainsAnnotations = "20.1.0"
jline = "3.21.0"
jmh = "1.35"
junit5 = "5.9.1"
junit5 = "5.9.3"
junitPioneer = "2.0.1"
jsr305 = "3.0.2"
okhttp = "4.9.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public class NodeConfig {
+ " },\n"
+ " },\n"
+ " clientConnector.port: {} ,\n"
+ " rest: {"
+ " rest: {\n"
+ " port: {}\n"
+ " ssl: {\n"
+ " port: {},\n"
+ " enabled: true,\n"
+ " keyStore: {\n"
+ " path: \"" + escapeWindowsPath(resolvedKeystorePath) + "\",\n"
Expand Down Expand Up @@ -77,6 +79,10 @@ public class NodeConfig {
+ " password: " + trustStorePassword + "\n"
+ " }\n"
+ " }\n"
+ " },\n"
+ " rest: {\n"
+ " port: {},\n"
+ " ssl.port: {}\n"
+ " }\n"
+ "}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public abstract class CliIntegrationTestBase extends IntegrationTestBase {
+ " }\n"
+ " },\n"
+ " clientConnector: { port:{} }\n"
+ " rest: {\n"
+ " port: {}\n"
+ " ssl.port: {}\n"
+ " }\n"
+ "}";


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ private static String buildConfig(int nodeIdx) {
+ " netClusterNodes: [ \"localhost:3344\", \"localhost:3345\", \"localhost:3346\" ] \n"
+ " }\n"
+ " },\n"
+ " clientConnector.port: " + (BASE_CLIENT_PORT + nodeIdx) + "\n"
+ " clientConnector.port: " + (BASE_CLIENT_PORT + nodeIdx) + ",\n"
+ " rest.port: " + (BASE_REST_PORT + nodeIdx) + "\n"
+ "}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
}
},
"rest": {
"port": <REST_PORT>,
"portRange": 0
"port": <REST_PORT>
},
"clientConnector": {
"port": <CLIENT_PORT>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,20 @@
@MicronautTest(rebuildContext = true)
@ExtendWith(WorkDirectoryExtension.class)
public class IntegrationTestBase extends BaseIgniteAbstractTest {
/** Base port network number. */
private static final int BASE_PORT = 3344;

/** Base client port. */
private static final int BASE_CLIENT_PORT = 10800;

/** Base HTTP port. */
private static final int BASE_HTTP_PORT = 10300;

/** Base HTTPS port. */
private static final int BASE_HTTPS_PORT = 10400;

/** Correct ignite cluster url. */
protected static final String NODE_URL = "http://localhost:10300";
protected static final String NODE_URL = "http://localhost:" + BASE_HTTP_PORT;

/** Cluster nodes. */
protected static final List<Ignite> CLUSTER_NODES = new ArrayList<>();
Expand All @@ -65,12 +77,6 @@ public class IntegrationTestBase extends BaseIgniteAbstractTest {

private static final IgniteLogger LOG = Loggers.forClass(IntegrationTestBase.class);

/** Base port number. */

private static final int BASE_PORT = 3344;

private static final int BASE_CLIENT_PORT = 10800;

/** Nodes bootstrap configuration pattern. */
private static final String NODE_BOOTSTRAP_CFG = "{\n"
+ " network: {\n"
Expand All @@ -80,7 +86,11 @@ public class IntegrationTestBase extends BaseIgniteAbstractTest {
+ " netClusterNodes: [ {} ]\n"
+ " }\n"
+ " },\n"
+ " clientConnector: { port:{} }\n"
+ " clientConnector: { port: {} }\n"
+ " rest: {\n"
+ " port:{},\n"
+ " ssl: { port: {} }\n"
+ " }\n"
+ "}";

/** Futures that are going to be completed when all nodes are started and the cluster is initialized. */
Expand All @@ -107,7 +117,9 @@ protected void startNodes(TestInfo testInfo) {
nodeBootstrapConfigTemplate(),
BASE_PORT + i,
connectNodeAddr,
BASE_CLIENT_PORT + i);
BASE_CLIENT_PORT + i,
BASE_HTTP_PORT + i,
BASE_HTTPS_PORT + i);

NODE_CONFIGS.put(nodeName, config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,44 +91,17 @@ public void start() {

boolean sslEnabled = sslConfigurationView.enabled();
boolean dualProtocol = restConfiguration.dualProtocol().value();
int desiredHttpPort = restConfigurationView.port();
int portRange = restConfigurationView.portRange();
int desiredHttpsPort = sslConfigurationView.port();
int httpsPortRange = sslConfigurationView.portRange();
int httpPortCandidate = desiredHttpPort;
int httpsPortCandidate = desiredHttpsPort;

while (httpPortCandidate <= desiredHttpPort + portRange
&& httpsPortCandidate <= desiredHttpsPort + httpsPortRange) {
if (startServer(httpPortCandidate, httpsPortCandidate)) {
return;
}

LOG.debug("Got exception during node start, going to try again [httpPort={}, httpsPort={}]",
httpPortCandidate,
httpsPortCandidate);

if (sslEnabled && dualProtocol) {
httpPortCandidate++;
httpsPortCandidate++;
} else if (sslEnabled) {
httpsPortCandidate++;
} else {
httpPortCandidate++;
}
if (startServer(restConfigurationView.port(), sslConfigurationView.port(), sslEnabled, dualProtocol)) {
return;
}

LOG.debug("Unable to start REST endpoint."
+ " Couldn't find available port for HTTP or HTTPS"
+ " [HTTP ports=[{}, {}]],"
+ " [HTTPS ports=[{}, {}]]",
desiredHttpPort, (desiredHttpPort + portRange),
desiredHttpsPort, (desiredHttpsPort + httpsPortRange));

String msg = "Cannot start REST endpoint."
+ " Couldn't find available port for HTTP or HTTPS"
+ " [HTTP ports=[" + desiredHttpPort + ", " + desiredHttpPort + portRange + "]],"
+ " [HTTPS ports=[" + desiredHttpsPort + ", " + desiredHttpsPort + httpsPortRange + "]]";
+ " [HTTP port=" + httpPort + "],"
+ " [HTTPS port=" + httpsPort + "]";

LOG.error(msg);

throw new IgniteException(Common.COMPONENT_NOT_STARTED_ERR, msg);
}
Expand All @@ -137,17 +110,20 @@ public void start() {
*
* @param httpPortCandidate HTTP port candidate.
* @param httpsPortCandidate HTTPS port candidate.
* @param sslEnabled SSL enabled flag.
* @param dualProtocol Dual protocol flag.
* @return {@code True} if server was started successfully, {@code False} if couldn't bind one of the ports.
*/
private boolean startServer(int httpPortCandidate, int httpsPortCandidate) {
private synchronized boolean startServer(int httpPortCandidate, int httpsPortCandidate, boolean sslEnabled, boolean dualProtocol) {
try {
httpPort = httpPortCandidate;
httpsPort = httpsPortCandidate;
context = buildMicronautContext(httpPortCandidate, httpsPortCandidate)
.deduceEnvironment(false)
.environments(BARE_METAL)
.start();
LOG.info("REST protocol started successfully");

logSuccessRestStart(sslEnabled, dualProtocol);
return true;
} catch (ApplicationStartupException e) {
BindException bindException = findBindException(e);
Expand All @@ -158,6 +134,20 @@ private boolean startServer(int httpPortCandidate, int httpsPortCandidate) {
}
}

private void logSuccessRestStart(boolean sslEnabled, boolean dualProtocol) {
String successReportMsg = null;

if (sslEnabled && dualProtocol) {
successReportMsg = "[httpPort=" + httpPort + "], [httpsPort=" + httpsPort + "]";
} else if (sslEnabled) {
successReportMsg = "[httpsPort=" + httpsPort + "]";
} else {
successReportMsg = "[httpPort=" + httpPort + "]";
}

LOG.info("REST server started successfully: " + successReportMsg);
}

@Nullable
private BindException findBindException(ApplicationStartupException e) {
var throwable = e.getCause();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ public class RestConfigurationSchema {
@Value(hasDefault = true)
public final int port = 10300;

/** TCP port range. */
@Range(min = 0)
@Value(hasDefault = true)
public final int portRange = 100;

/** The dual protocol (http/https) configuration. */
@Value(hasDefault = true)
public final boolean dualProtocol = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,4 @@ public class RestSslConfigurationSchema extends AbstractSslConfigurationSchema {
@Range(min = 1024, max = 0xFFFF)
@Value(hasDefault = true)
public final int port = 10400;

/** SSL port range. */
@Range(min = 0)
@Value(hasDefault = true)
public final int portRange = 100;
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public abstract class BaseIgniteRestartTest extends IgniteAbstractTest {

protected static final int DEFAULT_CLIENT_PORT = 10800;

protected static final int DEFAULT_HTTP_PORT = 10300;

protected static final int DEFAULT_HTTPS_PORT = 10400;

@Language("HOCON")
protected static final String RAFT_CFG = "{\n"
+ " fsync: false,\n"
Expand All @@ -87,7 +91,11 @@ public abstract class BaseIgniteRestartTest extends IgniteAbstractTest {
+ " },\n"
+ " },\n"
+ " raft: " + RAFT_CFG + ",\n"
+ " clientConnector.port: {}\n"
+ " clientConnector.port: {},\n"
+ " rest: {\n"
+ " port: {}, \n"
+ " ssl.port: {} \n"
+ " }\n"
+ "}";

public TestInfo testInfo;
Expand Down Expand Up @@ -203,11 +211,13 @@ public static <T extends IgniteComponent> T findComponent(List<IgniteComponent>
protected static String configurationString(int idx) {
int port = DEFAULT_NODE_PORT + idx;
int clientPort = DEFAULT_CLIENT_PORT + idx;
int httpPort = DEFAULT_HTTP_PORT + idx;
int httpsPort = DEFAULT_HTTPS_PORT + idx;

// The address of the first node.
@Language("HOCON") String connectAddr = "[localhost\":\"" + DEFAULT_NODE_PORT + "]";

return IgniteStringFormatter.format(NODE_BOOTSTRAP_CFG, port, connectAddr, clientPort);
return IgniteStringFormatter.format(NODE_BOOTSTRAP_CFG, port, connectAddr, clientPort, httpPort, httpsPort);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
public abstract class ClusterPerTestIntegrationTest extends IgniteIntegrationTest {
private static final IgniteLogger LOG = Loggers.forClass(ClusterPerTestIntegrationTest.class);

/** Base port number. */
private static final int BASE_PORT = 3344;

/** Nodes bootstrap configuration pattern. */
private static final String NODE_BOOTSTRAP_CFG_TEMPLATE = "{\n"
+ " network: {\n"
Expand All @@ -48,7 +45,8 @@ public abstract class ClusterPerTestIntegrationTest extends IgniteIntegrationTes
+ " netClusterNodes: [ {} ]\n"
+ " }\n"
+ " },\n"
+ " clientConnector: { port:{} }\n"
+ " clientConnector: { port:{} },\n"
+ " rest.port: {}\n"
+ "}";

/** Template for node bootstrap config with Scalecube settings for fast failure detection. */
Expand All @@ -68,7 +66,8 @@ public abstract class ClusterPerTestIntegrationTest extends IgniteIntegrationTes
+ " },\n"
+ " }\n"
+ " },\n"
+ " clientConnector: { port:{} }\n"
+ " clientConnector: { port:{} }, \n"
+ " rest.port: {}\n"
+ "}";

/** Template for node bootstrap config with Scalecube settings for a disabled failure detection. */
Expand All @@ -82,7 +81,8 @@ public abstract class ClusterPerTestIntegrationTest extends IgniteIntegrationTes
+ " failurePingInterval: 1000000000\n"
+ " }\n"
+ " },\n"
+ " clientConnector: { port:{} }\n"
+ " clientConnector: { port:{} },\n"
+ " rest.port: {}\n"
+ "}";

protected Cluster cluster;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ private void createCluster(TestInfo testInfo, int numNodes) {
String config = "{"
+ " network.port: " + port + ","
+ " clientConnector.port: " + (port + 8000) + ","
+ " network.nodeFinder.netClusterNodes: " + nodeFinderConfig
+ " network.nodeFinder.netClusterNodes: " + nodeFinderConfig + ","
+ " rest.port: " + (port + 10000)
+ "}";

String nodeName = testNodeName(testInfo, port);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class ItLogicalTopologyTest extends ClusterPerTestIntegrationTest {
+ " nodeAttributes: {\n"
+ " nodeAttributes: " + NODE_ATTRIBUTES
+ " },\n"
+ " clientConnector: { port:{} }\n"
+ " clientConnector: { port:{} },\n"
+ " rest.port: {}\n"
+ "}";

private final LogicalTopologyEventListener listener = new LogicalTopologyEventListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ private static String createStartConfig(@Language("JSON") String nodeAttributes)
+ " nodeAttributes: {\n"
+ " nodeAttributes: " + nodeAttributes
+ " },\n"
+ " clientConnector: { port:{} }\n"
+ " clientConnector: { port:{} },\n"
+ " rest.port: {}\n"
+ "}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ class ItTableRaftSnapshotsTest extends IgniteIntegrationTest {
+ " }\n"
+ " },\n"
+ " raft.rpcInstallSnapshotTimeout: 10000,\n"
+ " clientConnector.port: {}\n"
+ " clientConnector.port: {},\n"
+ " rest.port: {}\n"
+ "}";

/**
Expand Down