Skip to content

Commit

Permalink
[Rename] Refactor ES and Elasticsearch classes in server module boots…
Browse files Browse the repository at this point in the history
…trap package (#197)

This commit refactors the heavily used ESPolicy, Elasticsearch (main class), and Elasticsearch 
prefixed test classes used in the bootstrap package under the server module. Refactoring the 
namespace will come in a separate commit.

Signed-off-by: Nicholas Knize <nknize@amazon.com>
  • Loading branch information
nknize authored Mar 12, 2021
1 parent 51069e4 commit 4fd14d0
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.io.IOException;
import java.util.Collections;

public class ElasticsearchExceptionTests extends AbstractResponseTestCase<OpenSearchException,
public class OpenSearchExceptionTests extends AbstractResponseTestCase<OpenSearchException,
org.elasticsearch.client.tasks.ElasticsearchException> {

@Override
Expand Down
2 changes: 1 addition & 1 deletion distribution/src/bin/elasticsearch-service.bat
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ if not "%SERVICE_USERNAME%" == "" (
set SERVICE_PARAMS=%SERVICE_PARAMS% --ServiceUser "%SERVICE_USERNAME%" --ServicePassword "%SERVICE_PASSWORD%"
)
)
"%EXECUTABLE%" //IS//%SERVICE_ID% --Startup %ES_START_TYPE% --StopTimeout %ES_STOP_TIMEOUT% --StartClass org.elasticsearch.bootstrap.Elasticsearch --StartMethod main ++StartParams --quiet --StopClass org.elasticsearch.bootstrap.Elasticsearch --StopMethod close --Classpath "%ES_CLASSPATH%" --JvmMs %JVM_MS% --JvmMx %JVM_MX% --JvmSs %JVM_SS% --JvmOptions %OTHER_JAVA_OPTS% ++JvmOptions %ES_PARAMS% %LOG_OPTS% --PidFile "%SERVICE_ID%.pid" --DisplayName "%SERVICE_DISPLAY_NAME%" --Description "%SERVICE_DESCRIPTION%" --Jvm "%JAVA_HOME%%JVM_DLL%" --StartMode jvm --StopMode jvm --StartPath "%ES_HOME%" %SERVICE_PARAMS% ++Environment HOSTNAME="%%COMPUTERNAME%%"
"%EXECUTABLE%" //IS//%SERVICE_ID% --Startup %ES_START_TYPE% --StopTimeout %ES_STOP_TIMEOUT% --StartClass org.elasticsearch.bootstrap.OpenSearch --StartMethod main ++StartParams --quiet --StopClass org.elasticsearch.bootstrap.OpenSearch --StopMethod close --Classpath "%ES_CLASSPATH%" --JvmMs %JVM_MS% --JvmMx %JVM_MX% --JvmSs %JVM_SS% --JvmOptions %OTHER_JAVA_OPTS% ++JvmOptions %ES_PARAMS% %LOG_OPTS% --PidFile "%SERVICE_ID%.pid" --DisplayName "%SERVICE_DISPLAY_NAME%" --Description "%SERVICE_DESCRIPTION%" --Jvm "%JAVA_HOME%%JVM_DLL%" --StartMode jvm --StopMode jvm --StartPath "%ES_HOME%" %SERVICE_PARAMS% ++Environment HOSTNAME="%%COMPUTERNAME%%"

if not errorlevel 1 goto installed
echo Failed installing '%SERVICE_ID%' service
Expand Down
2 changes: 1 addition & 1 deletion distribution/src/bin/elasticsearch.bat
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ ECHO.!KEYSTORE_PASSWORD!| %JAVA% %ES_JAVA_OPTS% -Delasticsearch ^
-Des.path.home="%ES_HOME%" -Des.path.conf="%ES_PATH_CONF%" ^
-Des.distribution.type="%ES_DISTRIBUTION_TYPE%" ^
-Des.bundled_jdk="%ES_BUNDLED_JDK%" ^
-cp "%ES_CLASSPATH%" "org.elasticsearch.bootstrap.Elasticsearch" !newparams!
-cp "%ES_CLASSPATH%" "org.elasticsearch.bootstrap.OpenSearch" !newparams!

endlocal
endlocal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* Unit tests for ESPolicy: these cannot run with security manager,
* we don't allow messing with the policy
*/
public class ESPolicyUnitTests extends OpenSearchTestCase {
public class OpenSearchPolicyUnitTests extends OpenSearchTestCase {
/**
* Test policy with null codesource.
* <p>
Expand Down Expand Up @@ -78,7 +78,7 @@ public void testListen() {
final ESPolicy policy = new ESPolicy(Collections.emptyMap(), noPermissions, Collections.emptyMap(), true, new Permissions());
assertFalse(
policy.implies(
new ProtectionDomain(ESPolicyUnitTests.class.getProtectionDomain().getCodeSource(), noPermissions),
new ProtectionDomain(OpenSearchPolicyUnitTests.class.getProtectionDomain().getCodeSource(), noPermissions),
new SocketPermission("localhost:" + randomFrom(0, randomIntBetween(49152, 65535)), "listen")));
}

Expand Down
14 changes: 7 additions & 7 deletions server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void run() {
// bail out
}
}
}, "elasticsearch[keepAlive/" + Version.CURRENT + "]");
}, "opensearch[keepAlive/" + Version.CURRENT + "]");
keepAliveThread.setDaemon(false);
// keep this thread alive (non daemon thread) until we shutdown
Runtime.getRuntime().addShutdownHook(new Thread() {
Expand All @@ -108,7 +108,7 @@ public static void initializeNatives(Path tmpFile, boolean mlockAll, boolean sys

// check if the user is running as root, and bail
if (Natives.definitelyRunningAsRoot()) {
throw new RuntimeException("can not run elasticsearch as root");
throw new RuntimeException("can not run opensearch as root");
}

// enable system call filter
Expand Down Expand Up @@ -333,7 +333,7 @@ static void stop() throws IOException {
}

/**
* This method is invoked by {@link Elasticsearch#main(String[])} to startup elasticsearch.
* This method is invoked by {@link OpenSearch#main(String[])} to startup opensearch.
*/
static void init(
final boolean foreground,
Expand All @@ -358,7 +358,7 @@ static void init(
if (JavaVersion.current().compareTo(JavaVersion.parse("11")) < 0) {
final String message = String.format(
Locale.ROOT,
"future versions of Elasticsearch will require Java 11; " +
"future versions of OpenSearch will require Java 11; " +
"your Java version from [%s] does not meet this requirement",
System.getProperty("java.home"));
DeprecationLogger.getLogger(Bootstrap.class).deprecate("java_version_11_required", message);
Expand Down Expand Up @@ -388,7 +388,7 @@ static void init(
// install the default uncaught exception handler; must be done before security is
// initialized as we do not want to grant the runtime permission
// setDefaultUncaughtExceptionHandler
Thread.setDefaultUncaughtExceptionHandler(new ElasticsearchUncaughtExceptionHandler());
Thread.setDefaultUncaughtExceptionHandler(new OpenSearchUncaughtExceptionHandler());

INSTANCE.setup(true, environment);

Expand All @@ -402,7 +402,7 @@ static void init(
INSTANCE.start();

// We don't close stderr if `--quiet` is passed, because that
// hides fatal startup errors. For example, if Elasticsearch is
// hides fatal startup errors. For example, if OpenSearch is
// running via systemd, the init script only specifies
// `--quiet`, not `-d`, so we want users to be able to see
// startup errors via journalctl.
Expand Down Expand Up @@ -463,7 +463,7 @@ private static void closeSysError() {

private static void checkLucene() {
if (Version.CURRENT.luceneVersion.equals(org.apache.lucene.util.Version.LATEST) == false) {
throw new AssertionError("Lucene version mismatch this version of Elasticsearch requires lucene version ["
throw new AssertionError("Lucene version mismatch this version of OpenSearch requires lucene version ["
+ Version.CURRENT.luceneVersion + "] but the current lucene version is [" + org.apache.lucene.util.Version.LATEST + "]");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ long getMaxSizeVirtualMemory() {
}

/**
* Bootstrap check that the maximum file size is unlimited (otherwise Elasticsearch could run in to an I/O exception writing files).
* Bootstrap check that the maximum file size is unlimited (otherwise OpenSearch could run in to an I/O exception writing files).
*/
static class MaxFileSizeCheck implements BootstrapCheck {

Expand Down Expand Up @@ -507,7 +507,7 @@ String getVmName() {

/**
* Checks if the serial collector is in use. This collector is single-threaded and devastating
* for performance and should not be used for a server application like Elasticsearch.
* for performance and should not be used for a server application like OpenSearch.
*/
static class UseSerialGCCheck implements BootstrapCheck {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private JNANatives() {}
// otherwise they are only inherited for new threads (ES app threads)
static boolean LOCAL_SYSTEM_CALL_FILTER_ALL = false;
// set to the maximum number of threads that can be created for
// the user ID that owns the running Elasticsearch process
// the user ID that owns the running OpenSearch process
static long MAX_NUMBER_OF_THREADS = -1;

static long MAX_SIZE_VIRTUAL_MEMORY = Long.MIN_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import org.opensearch.cli.UserException;
import org.elasticsearch.common.logging.LogConfigurator;
import org.elasticsearch.env.Environment;
import org.elasticsearch.monitor.jvm.JvmInfo;
import org.elasticsearch.node.NodeValidationException;
import org.opensearch.monitor.jvm.JvmInfo;
import org.opensearch.node.NodeValidationException;

import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -41,22 +41,22 @@
import java.util.Locale;

/**
* This class starts elasticsearch.
* This class starts opensearch.
*/
class Elasticsearch extends EnvironmentAwareCommand {
class OpenSearch extends EnvironmentAwareCommand {

private final OptionSpecBuilder versionOption;
private final OptionSpecBuilder daemonizeOption;
private final OptionSpec<Path> pidfileOption;
private final OptionSpecBuilder quietOption;

// visible for testing
Elasticsearch() {
super("Starts Elasticsearch", () -> {}); // we configure logging later so we override the base class from configuring logging
OpenSearch() {
super("Starts OpenSearch", () -> {}); // we configure logging later so we override the base class from configuring logging
versionOption = parser.acceptsAll(Arrays.asList("V", "version"),
"Prints Elasticsearch version information and exits");
"Prints OpenSearch version information and exits");
daemonizeOption = parser.acceptsAll(Arrays.asList("d", "daemonize"),
"Starts Elasticsearch in the background")
"Starts OpenSearch in the background")
.availableUnless(versionOption);
pidfileOption = parser.acceptsAll(Arrays.asList("p", "pidfile"),
"Creates a pid file in the specified path on start")
Expand All @@ -70,7 +70,7 @@ class Elasticsearch extends EnvironmentAwareCommand {
}

/**
* Main entry point for starting elasticsearch
* Main entry point for starting opensearch
*/
public static void main(final String[] args) throws Exception {
overrideDnsCachePolicyProperties();
Expand All @@ -88,18 +88,18 @@ public void checkPermission(Permission perm) {

});
LogConfigurator.registerErrorListener();
final Elasticsearch elasticsearch = new Elasticsearch();
int status = main(args, elasticsearch, Terminal.DEFAULT);
final OpenSearch opensearch = new OpenSearch();
int status = main(args, opensearch, Terminal.DEFAULT);
if (status != ExitCodes.OK) {
final String basePath = System.getProperty("es.logs.base_path");
final String basePath = System.getProperty("opensearch.logs.base_path");
// It's possible to fail before logging has been configured, in which case there's no point
// suggesting that the user look in the log file.
if (basePath != null) {
Terminal.DEFAULT.errorPrintln(
"ERROR: Elasticsearch did not exit normally - check the logs at "
"ERROR: OpenSearch did not exit normally - check the logs at "
+ basePath
+ System.getProperty("file.separator")
+ System.getProperty("es.logs.cluster_name") + ".log"
+ System.getProperty("opensearch.logs.cluster_name") + ".log"
);
}
exit(status);
Expand All @@ -108,7 +108,7 @@ public void checkPermission(Permission perm) {

private static void overrideDnsCachePolicyProperties() {
for (final String property : new String[] {"networkaddress.cache.ttl", "networkaddress.cache.negative.ttl" }) {
final String overrideProperty = "es." + property;
final String overrideProperty = "opensearch." + property;
final String overrideValue = System.getProperty(overrideProperty);
if (overrideValue != null) {
try {
Expand All @@ -122,8 +122,8 @@ private static void overrideDnsCachePolicyProperties() {
}
}

static int main(final String[] args, final Elasticsearch elasticsearch, final Terminal terminal) throws Exception {
return elasticsearch.main(args, terminal);
static int main(final String[] args, final OpenSearch opensearch, final Terminal terminal) throws Exception {
return opensearch.main(args, terminal);
}

@Override
Expand Down Expand Up @@ -181,7 +181,7 @@ void init(final boolean daemonize, final Path pidFile, final boolean quiet, Envi
* http://commons.apache.org/proper/commons-daemon/procrun.html
*
* NOTE: If this method is renamed and/or moved, make sure to
* update elasticsearch-service.bat!
* update opensearch-service.bat!
*/
static void close(String[] args) throws IOException {
Bootstrap.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.util.function.Predicate;

/** custom policy for union of static and dynamic permissions */
final class ESPolicy extends Policy {
final class OpenSearchPolicy extends Policy {

/** template policy file, the one used in tests */
static final String POLICY_RESOURCE = "security.policy";
Expand All @@ -50,8 +50,8 @@ final class ESPolicy extends Policy {
final PermissionCollection dataPathPermission;
final Map<String,Policy> plugins;

ESPolicy(Map<String, URL> codebases, PermissionCollection dynamic, Map<String,Policy> plugins, boolean filterBadDefaults,
PermissionCollection dataPathPermission) {
OpenSearchPolicy(Map<String, URL> codebases, PermissionCollection dynamic, Map<String,Policy> plugins, boolean filterBadDefaults,
PermissionCollection dataPathPermission) {
this.template = Security.readPolicy(getClass().getResource(POLICY_RESOURCE), codebases);
this.dataPathPermission = dataPathPermission;
this.untrusted = Security.readPolicy(getClass().getResource(UNTRUSTED_RESOURCE), Collections.emptyMap());
Expand Down Expand Up @@ -102,7 +102,7 @@ public boolean implies(ProtectionDomain domain, Permission permission) {
}

// The FilePermission to check access to the path.data is the hottest permission check in
// Elasticsearch, so we check it first.
// OpenSearch, so we check it first.
if (permission instanceof FilePermission && dataPathPermission.implies(permission)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import java.security.AccessController;
import java.security.PrivilegedAction;

class ElasticsearchUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private static final Logger logger = LogManager.getLogger(ElasticsearchUncaughtExceptionHandler.class);
class OpenSearchUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private static final Logger logger = LogManager.getLogger(OpenSearchUncaughtExceptionHandler.class);

@Override
public void uncaughtException(Thread thread, Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ static void configure(Environment environment, boolean filterBadDefaults) throws

// enable security policy: union of template and environment-based paths, and possibly plugin permissions
Map<String, URL> codebases = getCodebaseJarMap(JarHell.parseClassPath());
Policy.setPolicy(new ESPolicy(codebases, createPermissions(environment), getPluginPermissions(environment), filterBadDefaults,
Policy.setPolicy(new OpenSearchPolicy(codebases, createPermissions(environment), getPluginPermissions(environment), filterBadDefaults,
createRecursiveDataPathPermission(environment)));

// enable security manager
final String[] classesThatCanExit =
new String[]{
// SecureSM matches class names as regular expressions so we escape the $ that arises from the nested class name
ElasticsearchUncaughtExceptionHandler.PrivilegedHaltAction.class.getName().replace("$", "\\$"),
OpenSearchUncaughtExceptionHandler.PrivilegedHaltAction.class.getName().replace("$", "\\$"),
Command.class.getName()};
System.setSecurityManager(new SecureSM(classesThatCanExit));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;

public class ElasticsearchNodeCommandTests extends ESTestCase {
public class OpenSearchNodeCommandTests extends ESTestCase {

public void testLoadStateWithoutMissingCustoms() throws IOException {
runLoadStateTest(false, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import java.util.Collections;
import java.util.Map;

import static org.elasticsearch.ElasticsearchExceptionTests.assertDeepEquals;
import static org.opensearch.OpenSearchExceptionTests.assertDeepEquals;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,22 +139,22 @@ public class BootstrapForTesting {

// read test-framework permissions
Map<String, URL> codebases = Security.getCodebaseJarMap(JarHell.parseClassPath());
// when testing server, the main elasticsearch code is not yet in a jar, so we need to manually add it
addClassCodebase(codebases,"elasticsearch", "org.elasticsearch.plugins.PluginsService");
// when testing server, the main opensearch code is not yet in a jar, so we need to manually add it
addClassCodebase(codebases,"opensearch", "org.elasticsearch.plugins.PluginsService");
if (System.getProperty("tests.gradle") == null) {
// intellij and eclipse don't package our internal libs, so we need to set the codebases for them manually
addClassCodebase(codebases,"plugin-classloader", "org.elasticsearch.plugins.ExtendedPluginsClassLoader");
addClassCodebase(codebases,"elasticsearch-nio", "org.elasticsearch.nio.ChannelFactory");
addClassCodebase(codebases, "elasticsearch-secure-sm", "org.elasticsearch.secure_sm.SecureSM");
addClassCodebase(codebases, "elasticsearch-rest-client", "org.elasticsearch.client.RestClient");
addClassCodebase(codebases,"opensearch-nio", "org.elasticsearch.nio.ChannelFactory");
addClassCodebase(codebases, "opensearch-secure-sm", "org.elasticsearch.secure_sm.SecureSM");
addClassCodebase(codebases, "opensearch-rest-client", "org.elasticsearch.client.RestClient");
}
final Policy testFramework = Security.readPolicy(Bootstrap.class.getResource("test-framework.policy"), codebases);
final Policy esPolicy = new ESPolicy(codebases, perms, getPluginPermissions(), true, new Permissions());
final Policy opensearchPolicy = new OpenSearchPolicy(codebases, perms, getPluginPermissions(), true, new Permissions());
Policy.setPolicy(new Policy() {
@Override
public boolean implies(ProtectionDomain domain, Permission permission) {
// implements union
return esPolicy.implies(domain, permission) || testFramework.implies(domain, permission);
return opensearchPolicy.implies(domain, permission) || testFramework.implies(domain, permission);
}
});
System.setSecurityManager(SecureSM.createTestSecureSM());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
import static org.hamcrest.Matchers.containsString;

public class ElasticsearchAssertionsTests extends ESTestCase {
public class OpenSearchAssertionsTests extends ESTestCase {

public void testAssertXContentEquivalent() throws IOException {
try (XContentBuilder original = JsonXContent.contentBuilder()) {
Expand Down

0 comments on commit 4fd14d0

Please sign in to comment.