Skip to content

Commit 850f51d

Browse files
authored
Internal: Refactor SettingCommand into EnvironmentAwareCommand (#22175)
* Internal: Refactor SettingCommand into EnvironmentAwareCommand This change renames and changes the behavior of SettingCommand to have its primary method take in a fully initialized Environment for elasticsearch instead of just a map of settings. All of the subclasses of SettingCommand already did this at some point, so this just removes duplication.
1 parent e508f2e commit 850f51d

File tree

14 files changed

+93
-72
lines changed

14 files changed

+93
-72
lines changed

core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@
5858
import java.nio.file.Path;
5959
import java.security.NoSuchAlgorithmException;
6060
import java.util.List;
61-
import java.util.Locale;
62-
import java.util.Map;
63-
import java.util.Objects;
61+
import java.util.Collections;
6462
import java.util.concurrent.CountDownLatch;
6563

6664
/**
@@ -226,13 +224,14 @@ protected void validateNodeBeforeAcceptingRequests(
226224
};
227225
}
228226

229-
private static Environment initialEnvironment(boolean foreground, Path pidFile, Map<String, String> esSettings) {
227+
private static Environment initialEnvironment(boolean foreground, Path pidFile, Settings initialSettings) {
230228
Terminal terminal = foreground ? Terminal.DEFAULT : null;
231229
Settings.Builder builder = Settings.builder();
232230
if (pidFile != null) {
233231
builder.put(Environment.PIDFILE_SETTING.getKey(), pidFile);
234232
}
235-
return InternalSettingsPreparer.prepareEnvironment(builder.build(), terminal, esSettings);
233+
builder.put(initialSettings);
234+
return InternalSettingsPreparer.prepareEnvironment(builder.build(), terminal, Collections.emptyMap());
236235
}
237236

238237
private void start() throws NodeValidationException {
@@ -262,7 +261,7 @@ static void init(
262261
final boolean foreground,
263262
final Path pidFile,
264263
final boolean quiet,
265-
final Map<String, String> esSettings) throws BootstrapException, NodeValidationException, UserException {
264+
final Settings initialSettings) throws BootstrapException, NodeValidationException, UserException {
266265
// Set the system property before anything has a chance to trigger its use
267266
initLoggerPrefix();
268267

@@ -272,7 +271,7 @@ static void init(
272271

273272
INSTANCE = new Bootstrap();
274273

275-
Environment environment = initialEnvironment(foreground, pidFile, esSettings);
274+
Environment environment = initialEnvironment(foreground, pidFile, initialSettings);
276275
try {
277276
LogConfigurator.configure(environment);
278277
} catch (IOException e) {

core/src/main/java/org/elasticsearch/bootstrap/BootstrapException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
package org.elasticsearch.bootstrap;
2121

2222
import java.nio.file.Path;
23-
import java.util.Map;
2423

2524
/**
2625
* Wrapper exception for checked exceptions thrown during the bootstrap process. Methods invoked
2726
* during bootstrap should explicitly declare the checked exceptions that they can throw, rather
2827
* than declaring the top-level checked exception {@link Exception}. This exception exists to wrap
29-
* these checked exceptions so that {@link Bootstrap#init(boolean, Path, boolean, Map)} does not have to
30-
* declare all of these checked exceptions.
28+
* these checked exceptions so that
29+
* {@link Bootstrap#init(boolean, Path, boolean, org.elasticsearch.common.settings.Settings)}
30+
* does not have to declare all of these checked exceptions.
3131
*/
3232
class BootstrapException extends Exception {
3333

core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
import joptsimple.util.PathConverter;
2626
import org.elasticsearch.Build;
2727
import org.elasticsearch.cli.ExitCodes;
28-
import org.elasticsearch.cli.SettingCommand;
28+
import org.elasticsearch.cli.EnvironmentAwareCommand;
2929
import org.elasticsearch.cli.Terminal;
3030
import org.elasticsearch.cli.UserException;
31+
import org.elasticsearch.common.settings.Settings;
32+
import org.elasticsearch.env.Environment;
3133
import org.elasticsearch.monitor.jvm.JvmInfo;
3234
import org.elasticsearch.node.NodeValidationException;
3335

@@ -40,7 +42,7 @@
4042
/**
4143
* This class starts elasticsearch.
4244
*/
43-
class Elasticsearch extends SettingCommand {
45+
class Elasticsearch extends EnvironmentAwareCommand {
4446

4547
private final OptionSpecBuilder versionOption;
4648
private final OptionSpecBuilder daemonizeOption;
@@ -90,7 +92,7 @@ static int main(final String[] args, final Elasticsearch elasticsearch, final Te
9092
}
9193

9294
@Override
93-
protected void execute(Terminal terminal, OptionSet options, Map<String, String> settings) throws UserException {
95+
protected void execute(Terminal terminal, OptionSet options, Environment env) throws UserException {
9496
if (options.nonOptionArguments().isEmpty() == false) {
9597
throw new UserException(ExitCodes.USAGE, "Positional arguments not allowed, found " + options.nonOptionArguments());
9698
}
@@ -109,16 +111,16 @@ protected void execute(Terminal terminal, OptionSet options, Map<String, String>
109111
final boolean quiet = options.has(quietOption);
110112

111113
try {
112-
init(daemonize, pidFile, quiet, settings);
114+
init(daemonize, pidFile, quiet, env.settings());
113115
} catch (NodeValidationException e) {
114116
throw new UserException(ExitCodes.CONFIG, e.getMessage());
115117
}
116118
}
117119

118-
void init(final boolean daemonize, final Path pidFile, final boolean quiet, final Map<String, String> esSettings)
120+
void init(final boolean daemonize, final Path pidFile, final boolean quiet, Settings initialSettings)
119121
throws NodeValidationException, UserException {
120122
try {
121-
Bootstrap.init(!daemonize, pidFile, quiet, esSettings);
123+
Bootstrap.init(!daemonize, pidFile, quiet, initialSettings);
122124
} catch (BootstrapException | RuntimeException e) {
123125
// format exceptions to the console in a special way
124126
// to avoid 2MB stacktraces from guice, etc.

core/src/main/java/org/elasticsearch/cli/SettingCommand.java renamed to core/src/main/java/org/elasticsearch/cli/EnvironmentAwareCommand.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@
2222
import joptsimple.OptionSet;
2323
import joptsimple.OptionSpec;
2424
import joptsimple.util.KeyValuePair;
25+
import org.elasticsearch.common.settings.Settings;
26+
import org.elasticsearch.env.Environment;
27+
import org.elasticsearch.node.internal.InternalSettingsPreparer;
2528

2629
import java.util.HashMap;
2730
import java.util.Locale;
2831
import java.util.Map;
2932

30-
public abstract class SettingCommand extends Command {
33+
/** A cli command which requires an {@link org.elasticsearch.env.Environment} to use current paths and settings. */
34+
public abstract class EnvironmentAwareCommand extends Command {
3135

3236
private final OptionSpec<KeyValuePair> settingOption;
3337

34-
public SettingCommand(String description) {
38+
public EnvironmentAwareCommand(String description) {
3539
super(description);
3640
this.settingOption = parser.accepts("E", "Configure a setting").withRequiredArg().ofType(KeyValuePair.class);
3741
}
@@ -51,9 +55,15 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception {
5155
putSystemPropertyIfSettingIsMissing(settings, "path.home", "es.path.home");
5256
putSystemPropertyIfSettingIsMissing(settings, "path.logs", "es.path.logs");
5357

54-
execute(terminal, options, settings);
58+
execute(terminal, options, createEnv(terminal, settings));
5559
}
5660

61+
/** Create an {@link Environment} for the command to use. Overrideable for tests. */
62+
protected Environment createEnv(Terminal terminal, Map<String, String> settings) {
63+
return InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, settings);
64+
}
65+
66+
/** Ensure the given setting exists, reading it from system properties if not already set. */
5767
protected static void putSystemPropertyIfSettingIsMissing(final Map<String, String> settings, final String setting, final String key) {
5868
final String value = System.getProperty(key);
5969
if (value != null) {
@@ -72,6 +82,7 @@ protected static void putSystemPropertyIfSettingIsMissing(final Map<String, Stri
7282
}
7383
}
7484

75-
protected abstract void execute(Terminal terminal, OptionSet options, Map<String, String> settings) throws Exception;
85+
/** Execute the command with the initialized {@link Environment}. */
86+
protected abstract void execute(Terminal terminal, OptionSet options, Environment env) throws Exception;
7687

7788
}

core/src/main/java/org/elasticsearch/index/translog/TruncateTranslogCommand.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
import org.apache.lucene.util.BytesRef;
3535
import org.apache.lucene.util.IOUtils;
3636
import org.elasticsearch.ElasticsearchException;
37-
import org.elasticsearch.cli.SettingCommand;
37+
import org.elasticsearch.cli.EnvironmentAwareCommand;
3838
import org.elasticsearch.cli.Terminal;
3939
import org.elasticsearch.common.SuppressForbidden;
4040
import org.elasticsearch.common.io.PathUtils;
41+
import org.elasticsearch.env.Environment;
4142
import org.elasticsearch.index.IndexNotFoundException;
4243
import org.elasticsearch.index.seqno.SequenceNumbersService;
4344

@@ -55,7 +56,7 @@
5556
import java.util.Map;
5657
import java.util.Set;
5758

58-
public class TruncateTranslogCommand extends SettingCommand {
59+
public class TruncateTranslogCommand extends EnvironmentAwareCommand {
5960

6061
private final OptionSpec<String> translogFolder;
6162
private final OptionSpec<Void> batchMode;
@@ -87,7 +88,7 @@ private Path getTranslogPath(OptionSet options) {
8788
}
8889

8990
@Override
90-
protected void execute(Terminal terminal, OptionSet options, Map<String, String> settings) throws Exception {
91+
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
9192
boolean batch = options.has(batchMode);
9293

9394
Path translogPath = getTranslogPath(options);

core/src/main/java/org/elasticsearch/plugins/InstallPluginCommand.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.elasticsearch.Version;
2828
import org.elasticsearch.bootstrap.JarHell;
2929
import org.elasticsearch.cli.ExitCodes;
30-
import org.elasticsearch.cli.SettingCommand;
30+
import org.elasticsearch.cli.EnvironmentAwareCommand;
3131
import org.elasticsearch.cli.Terminal;
3232
import org.elasticsearch.cli.UserException;
3333
import org.elasticsearch.common.collect.Tuple;
@@ -103,7 +103,7 @@
103103
* elasticsearch config directory, using the name of the plugin. If any files to be installed
104104
* already exist, they will be skipped.
105105
*/
106-
class InstallPluginCommand extends SettingCommand {
106+
class InstallPluginCommand extends EnvironmentAwareCommand {
107107

108108
private static final String PROPERTY_STAGING_ID = "es.plugins.staging";
109109

@@ -189,18 +189,17 @@ protected void printAdditionalHelp(Terminal terminal) {
189189
}
190190

191191
@Override
192-
protected void execute(Terminal terminal, OptionSet options, Map<String, String> settings) throws Exception {
192+
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
193193
String pluginId = arguments.value(options);
194194
boolean isBatch = options.has(batchOption) || System.console() == null;
195-
execute(terminal, pluginId, isBatch, settings);
195+
execute(terminal, pluginId, isBatch, env);
196196
}
197197

198198
// pkg private for testing
199-
void execute(Terminal terminal, String pluginId, boolean isBatch, Map<String, String> settings) throws Exception {
199+
void execute(Terminal terminal, String pluginId, boolean isBatch, Environment env) throws Exception {
200200
if (pluginId == null) {
201201
throw new UserException(ExitCodes.USAGE, "plugin id is required");
202202
}
203-
final Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, settings);
204203
// TODO: remove this leniency!! is it needed anymore?
205204
if (Files.exists(env.pluginsFile()) == false) {
206205
terminal.println("Plugins directory [" + env.pluginsFile() + "] does not exist. Creating...");

core/src/main/java/org/elasticsearch/plugins/ListPluginsCommand.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package org.elasticsearch.plugins;
2121

2222
import joptsimple.OptionSet;
23-
import org.elasticsearch.cli.SettingCommand;
23+
import org.elasticsearch.cli.EnvironmentAwareCommand;
2424
import org.elasticsearch.cli.Terminal;
2525
import org.elasticsearch.common.settings.Settings;
2626
import org.elasticsearch.env.Environment;
@@ -38,15 +38,14 @@
3838
/**
3939
* A command for the plugin cli to list plugins installed in elasticsearch.
4040
*/
41-
class ListPluginsCommand extends SettingCommand {
41+
class ListPluginsCommand extends EnvironmentAwareCommand {
4242

4343
ListPluginsCommand() {
4444
super("Lists installed elasticsearch plugins");
4545
}
4646

4747
@Override
48-
protected void execute(Terminal terminal, OptionSet options, Map<String, String> settings) throws Exception {
49-
final Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, settings);
48+
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
5049
if (Files.exists(env.pluginsFile()) == false) {
5150
throw new IOException("Plugins directory missing: " + env.pluginsFile());
5251
}

core/src/main/java/org/elasticsearch/plugins/RemovePluginCommand.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,28 @@
1919

2020
package org.elasticsearch.plugins;
2121

22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.StandardCopyOption;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
2228
import joptsimple.OptionSet;
2329
import joptsimple.OptionSpec;
24-
2530
import org.apache.lucene.util.IOUtils;
31+
import org.elasticsearch.cli.EnvironmentAwareCommand;
2632
import org.elasticsearch.cli.ExitCodes;
27-
import org.elasticsearch.cli.SettingCommand;
2833
import org.elasticsearch.cli.Terminal;
2934
import org.elasticsearch.cli.UserException;
3035
import org.elasticsearch.common.Strings;
31-
import org.elasticsearch.common.settings.Settings;
3236
import org.elasticsearch.env.Environment;
33-
import org.elasticsearch.node.internal.InternalSettingsPreparer;
34-
35-
import java.nio.file.Files;
36-
import java.nio.file.Path;
37-
import java.nio.file.StandardCopyOption;
38-
import java.util.ArrayList;
39-
import java.util.List;
40-
import java.util.Map;
4137

4238
import static org.elasticsearch.cli.Terminal.Verbosity.VERBOSE;
4339

4440
/**
4541
* A command for the plugin cli to remove a plugin from elasticsearch.
4642
*/
47-
class RemovePluginCommand extends SettingCommand {
43+
class RemovePluginCommand extends EnvironmentAwareCommand {
4844

4945
private final OptionSpec<String> arguments;
5046

@@ -54,15 +50,13 @@ class RemovePluginCommand extends SettingCommand {
5450
}
5551

5652
@Override
57-
protected void execute(Terminal terminal, OptionSet options, Map<String, String> settings) throws Exception {
53+
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
5854
String arg = arguments.value(options);
59-
execute(terminal, arg, settings);
55+
execute(terminal, arg, env);
6056
}
6157

6258
// pkg private for testing
63-
void execute(Terminal terminal, String pluginName, Map<String, String> settings) throws Exception {
64-
final Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, settings);
65-
59+
void execute(Terminal terminal, String pluginName, Environment env) throws Exception {
6660
terminal.println("-> Removing " + Strings.coalesceToEmpty(pluginName) + "...");
6761

6862
final Path pluginDir = env.pluginsFile().resolve(pluginName);

core/src/test/java/org/elasticsearch/bootstrap/ElasticsearchCliTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.monitor.jvm.JvmInfo;
2626

2727
import java.nio.file.Path;
28+
import java.util.Map;
2829
import java.util.function.Consumer;
2930

3031
import static org.hamcrest.CoreMatchers.containsString;
@@ -152,9 +153,9 @@ public void testElasticsearchSettings() throws Exception {
152153
true,
153154
output -> {},
154155
(foreground, pidFile, quiet, esSettings) -> {
155-
assertThat(esSettings.size(), equalTo(2));
156-
assertThat(esSettings, hasEntry("foo", "bar"));
157-
assertThat(esSettings, hasEntry("baz", "qux"));
156+
Map<String, String> settings = esSettings.getAsMap();
157+
assertThat(settings, hasEntry("foo", "bar"));
158+
assertThat(settings, hasEntry("baz", "qux"));
158159
},
159160
"-Efoo=bar", "-E", "baz=qux"
160161
);

core/src/test/java/org/elasticsearch/index/translog/TruncateTranslogIT.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.elasticsearch.common.unit.ByteSizeUnit;
4444
import org.elasticsearch.common.unit.ByteSizeValue;
4545
import org.elasticsearch.common.unit.TimeValue;
46+
import org.elasticsearch.env.Environment;
4647
import org.elasticsearch.index.Index;
4748
import org.elasticsearch.index.IndexSettings;
4849
import org.elasticsearch.index.MockEngineFactoryPlugin;
@@ -112,7 +113,7 @@ public void testCorruptTranslogTruncation() throws Exception {
112113
// Try running it before the shard is closed, it should flip out because it can't acquire the lock
113114
try {
114115
logger.info("--> running truncate while index is open on [{}]", translogDir.toAbsolutePath());
115-
ttc.execute(t, options, new HashMap<String, String>());
116+
ttc.execute(t, options, null /* TODO: env should be real here, and ttc should actually use it... */);
116117
fail("expected the truncate command to fail not being able to acquire the lock");
117118
} catch (Exception e) {
118119
assertThat(e.getMessage(), containsString("Failed to lock shard's directory"));
@@ -160,7 +161,7 @@ public void testCorruptTranslogTruncation() throws Exception {
160161

161162
OptionSet options = parser.parse("-d", translogDir.toAbsolutePath().toString(), "-b");
162163
logger.info("--> running truncate translog command for [{}]", translogDir.toAbsolutePath());
163-
ttc.execute(t, options, new HashMap<String, String>());
164+
ttc.execute(t, options, null /* TODO: env should be real here, and ttc should actually use it... */);
164165
logger.info("--> output:\n{}", t.getOutput());
165166
}
166167

0 commit comments

Comments
 (0)