forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-12468][yarn] Derive HistoryServer's URL from HistoryServerOptions
This commit derives the HistoryServer's URL from the availabe HistoryServerOptions. This closes apache#8396.
- Loading branch information
1 parent
b3f102d
commit 792f2a2
Showing
6 changed files
with
205 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...runtime/src/main/java/org/apache/flink/runtime/webmonitor/history/HistoryServerUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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:https://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.flink.runtime.webmonitor.history; | ||
|
||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.configuration.HistoryServerOptions; | ||
import org.apache.flink.runtime.net.SSLUtils; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Utility class for the HistoryServer. | ||
*/ | ||
public enum HistoryServerUtils { | ||
; | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(HistoryServerUtils.class); | ||
|
||
public static boolean isSSLEnabled(Configuration config) { | ||
return config.getBoolean(HistoryServerOptions.HISTORY_SERVER_WEB_SSL_ENABLED) && SSLUtils.isRestSSLEnabled(config); | ||
} | ||
|
||
public static Optional<URL> getHistoryServerURL(Configuration configuration) { | ||
final String hostname = getHostname(configuration); | ||
|
||
if (hostname != null) { | ||
final String protocol = getProtocol(configuration); | ||
final int port = getPort(configuration); | ||
|
||
try { | ||
return Optional.of(new URL(protocol, hostname, port, "")); | ||
} catch (MalformedURLException e) { | ||
LOG.debug("Could not create the HistoryServer's URL from protocol: {}, hostname: {} and port: {}.", protocol, hostname, port, e); | ||
return Optional.empty(); | ||
} | ||
} else { | ||
LOG.debug("Not hostname has been specified for the HistoryServer. This indicates that it has not been started."); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private static int getPort(Configuration configuration) { | ||
return configuration.getInteger(HistoryServerOptions.HISTORY_SERVER_WEB_PORT); | ||
} | ||
|
||
@Nullable | ||
private static String getHostname(Configuration configuration) { | ||
return configuration.getString(HistoryServerOptions.HISTORY_SERVER_WEB_ADDRESS); | ||
} | ||
|
||
private static String getProtocol(Configuration configuration) { | ||
if (isSSLEnabled(configuration)) { | ||
return "https"; | ||
} else { | ||
return "http"; | ||
} | ||
} | ||
|
||
} |
114 changes: 114 additions & 0 deletions
114
...ime/src/test/java/org/apache/flink/runtime/webmonitor/history/HistoryServerUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* 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:https://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.flink.runtime.webmonitor.history; | ||
|
||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.configuration.HistoryServerOptions; | ||
import org.apache.flink.configuration.SecurityOptions; | ||
import org.apache.flink.util.TestLogger; | ||
|
||
import org.junit.Test; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Optional; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* Tests for the {@link HistoryServerUtils}. | ||
*/ | ||
public class HistoryServerUtilsTest extends TestLogger { | ||
|
||
private static final String HOSTNAME = "foobar"; | ||
private static final int PORT = 1234; | ||
|
||
@Test | ||
public void testIsSSLEnabledDefault() { | ||
final Configuration configuration = new Configuration(); | ||
|
||
assertThat(HistoryServerUtils.isSSLEnabled(configuration), is(false)); | ||
} | ||
|
||
@Test | ||
public void testIsSSLEnabledWithoutRestSSL() { | ||
final Configuration configuration = new Configuration(); | ||
configuration.setBoolean(HistoryServerOptions.HISTORY_SERVER_WEB_SSL_ENABLED, true); | ||
|
||
assertThat(HistoryServerUtils.isSSLEnabled(configuration), is(false)); | ||
} | ||
|
||
@Test | ||
public void testIsSSLEnabledOnlyRestSSL() { | ||
final Configuration configuration = new Configuration(); | ||
configuration.setBoolean(SecurityOptions.SSL_REST_ENABLED, true); | ||
|
||
assertThat(HistoryServerUtils.isSSLEnabled(configuration), is(false)); | ||
} | ||
|
||
@Test | ||
public void testIsSSLEnabled() { | ||
final Configuration configuration = new Configuration(); | ||
enableSSL(configuration); | ||
|
||
assertThat(HistoryServerUtils.isSSLEnabled(configuration), is(true)); | ||
} | ||
|
||
private void enableSSL(Configuration configuration) { | ||
configuration.setBoolean(HistoryServerOptions.HISTORY_SERVER_WEB_SSL_ENABLED, true); | ||
configuration.setBoolean(SecurityOptions.SSL_REST_ENABLED, true); | ||
} | ||
|
||
@Test | ||
public void testGetHistoryServerURL() throws MalformedURLException { | ||
final Configuration configuration = createDefaultConfiguration(); | ||
|
||
final Optional<URL> historyServerURL = HistoryServerUtils.getHistoryServerURL(configuration); | ||
|
||
assertThat(historyServerURL.get(), is(new URL("http", HOSTNAME, PORT, ""))); | ||
} | ||
|
||
@Test | ||
public void testGetHistoryServerURLWithSSL() throws MalformedURLException { | ||
final Configuration configuration = createDefaultConfiguration(); | ||
enableSSL(configuration); | ||
|
||
final Optional<URL> historyServerURL = HistoryServerUtils.getHistoryServerURL(configuration); | ||
|
||
assertThat(historyServerURL.get(), is(new URL("https", HOSTNAME, PORT, ""))); | ||
} | ||
|
||
@Test | ||
public void testGetHistoryServerURLWithoutHS() { | ||
final Configuration configuration = new Configuration(); | ||
|
||
assertThat(HistoryServerUtils.getHistoryServerURL(configuration).isPresent(), is(false)); | ||
} | ||
|
||
@Nonnull | ||
private Configuration createDefaultConfiguration() { | ||
final Configuration configuration = new Configuration(); | ||
configuration.setString(HistoryServerOptions.HISTORY_SERVER_WEB_ADDRESS, HOSTNAME); | ||
configuration.setInteger(HistoryServerOptions.HISTORY_SERVER_WEB_PORT, PORT); | ||
return configuration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters