mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 10:25:15 +00:00
Simplify random name index and move method to its only user
This commit is contained in:
parent
f3d63095db
commit
867f056cf6
@ -1,59 +0,0 @@
|
|||||||
/*
|
|
||||||
* Licensed to Elasticsearch under one or more contributor
|
|
||||||
* license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright
|
|
||||||
* ownership. Elasticsearch 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://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.elasticsearch.common;
|
|
||||||
|
|
||||||
import com.google.common.base.Charsets;
|
|
||||||
import org.elasticsearch.common.io.FileSystemUtils;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public abstract class Names {
|
|
||||||
|
|
||||||
public static String randomNodeName(InputStream namesFile) {
|
|
||||||
try {
|
|
||||||
List<String> names = new ArrayList<>();
|
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(namesFile, Charsets.UTF_8))) {
|
|
||||||
String name = reader.readLine();
|
|
||||||
while (name != null) {
|
|
||||||
names.add(name);
|
|
||||||
name = reader.readLine();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
int index = ((ThreadLocalRandom.current().nextInt(names.size())) % names.size());
|
|
||||||
return names.get(index);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("Could not read node names list", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Names() {}
|
|
||||||
}
|
|
@ -19,11 +19,11 @@
|
|||||||
|
|
||||||
package org.elasticsearch.node.internal;
|
package org.elasticsearch.node.internal;
|
||||||
|
|
||||||
|
import com.google.common.base.Charsets;
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.common.collect.UnmodifiableIterator;
|
import com.google.common.collect.UnmodifiableIterator;
|
||||||
import org.elasticsearch.cluster.ClusterName;
|
import org.elasticsearch.cluster.ClusterName;
|
||||||
import org.elasticsearch.common.Booleans;
|
import org.elasticsearch.common.Booleans;
|
||||||
import org.elasticsearch.common.Names;
|
|
||||||
import org.elasticsearch.common.Strings;
|
import org.elasticsearch.common.Strings;
|
||||||
import org.elasticsearch.common.cli.Terminal;
|
import org.elasticsearch.common.cli.Terminal;
|
||||||
import org.elasticsearch.common.collect.Tuple;
|
import org.elasticsearch.common.collect.Tuple;
|
||||||
@ -31,12 +31,16 @@ import org.elasticsearch.common.settings.Settings;
|
|||||||
import org.elasticsearch.common.settings.SettingsException;
|
import org.elasticsearch.common.settings.SettingsException;
|
||||||
import org.elasticsearch.env.Environment;
|
import org.elasticsearch.env.Environment;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
import static org.elasticsearch.common.Strings.cleanPath;
|
import static org.elasticsearch.common.Strings.cleanPath;
|
||||||
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
|
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
|
||||||
@ -160,18 +164,7 @@ public class InternalSettingsPreparer {
|
|||||||
if (settings.get("name") == null) {
|
if (settings.get("name") == null) {
|
||||||
String name = settings.get("node.name");
|
String name = settings.get("node.name");
|
||||||
if (name == null || name.isEmpty()) {
|
if (name == null || name.isEmpty()) {
|
||||||
InputStream input;
|
name = randomNodeName(environment);
|
||||||
Path namesPath = environment.configFile().resolve("names.txt");
|
|
||||||
if (Files.exists(namesPath)) {
|
|
||||||
try {
|
|
||||||
input = Files.newInputStream(namesPath);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("Failed to load custom names.txt from " + namesPath, e);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
input = InternalSettingsPreparer.class.getResourceAsStream("/config/names.txt");
|
|
||||||
}
|
|
||||||
name = Names.randomNodeName(input);
|
|
||||||
}
|
}
|
||||||
settings = settingsBuilder().put(settings).put("name", name).build();
|
settings = settingsBuilder().put(settings).put("name", name).build();
|
||||||
}
|
}
|
||||||
@ -188,6 +181,35 @@ public class InternalSettingsPreparer {
|
|||||||
return new Tuple<>(settings, environment);
|
return new Tuple<>(settings, environment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String randomNodeName(Environment environment) {
|
||||||
|
InputStream input;
|
||||||
|
Path namesPath = environment.configFile().resolve("names.txt");
|
||||||
|
if (Files.exists(namesPath)) {
|
||||||
|
try {
|
||||||
|
input = Files.newInputStream(namesPath);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("Failed to load custom names.txt from " + namesPath, e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
input = InternalSettingsPreparer.class.getResourceAsStream("/config/names.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
List<String> names = new ArrayList<>();
|
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, Charsets.UTF_8))) {
|
||||||
|
String name = reader.readLine();
|
||||||
|
while (name != null) {
|
||||||
|
names.add(name);
|
||||||
|
name = reader.readLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int index = ThreadLocalRandom.current().nextInt(names.size());
|
||||||
|
return names.get(index);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("Could not read node names list", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static Settings replacePromptPlaceholders(Settings settings, Terminal terminal) {
|
static Settings replacePromptPlaceholders(Settings settings, Terminal terminal) {
|
||||||
UnmodifiableIterator<Map.Entry<String, String>> iter = settings.getAsMap().entrySet().iterator();
|
UnmodifiableIterator<Map.Entry<String, String>> iter = settings.getAsMap().entrySet().iterator();
|
||||||
Settings.Builder builder = Settings.builder();
|
Settings.Builder builder = Settings.builder();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user