use of org.opensearch.common.settings.Setting in project OpenSearch by opensearch-project.
the class ProxyConnectionStrategyTests method testProxyStrategyWillNeedToBeRebuiltIfNumOfSocketsOrAddressesOrServerNameChange.
public void testProxyStrategyWillNeedToBeRebuiltIfNumOfSocketsOrAddressesOrServerNameChange() {
try (MockTransportService remoteTransport = startTransport("node1", Version.CURRENT)) {
TransportAddress remoteAddress = remoteTransport.boundAddress().publishAddress();
try (MockTransportService localService = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool)) {
localService.start();
localService.acceptIncomingRequests();
ClusterConnectionManager connectionManager = new ClusterConnectionManager(profile, localService.transport);
int numOfConnections = randomIntBetween(4, 8);
try (RemoteConnectionManager remoteConnectionManager = new RemoteConnectionManager(clusterAlias, connectionManager);
ProxyConnectionStrategy strategy = new ProxyConnectionStrategy(clusterAlias, localService, remoteConnectionManager, Settings.EMPTY, numOfConnections, remoteAddress.toString(), "server-name")) {
PlainActionFuture<Void> connectFuture = PlainActionFuture.newFuture();
strategy.connect(connectFuture);
connectFuture.actionGet();
assertTrue(connectionManager.getAllConnectedNodes().stream().anyMatch(n -> n.getAddress().equals(remoteAddress)));
assertEquals(numOfConnections, connectionManager.size());
assertTrue(strategy.assertNoRunningConnections());
Setting<?> modeSetting = RemoteConnectionStrategy.REMOTE_CONNECTION_MODE.getConcreteSettingForNamespace("cluster-alias");
Setting<?> addressesSetting = ProxyConnectionStrategy.PROXY_ADDRESS.getConcreteSettingForNamespace("cluster-alias");
Setting<?> socketConnections = ProxyConnectionStrategy.REMOTE_SOCKET_CONNECTIONS.getConcreteSettingForNamespace("cluster-alias");
Setting<?> serverName = ProxyConnectionStrategy.SERVER_NAME.getConcreteSettingForNamespace("cluster-alias");
Settings noChange = Settings.builder().put(modeSetting.getKey(), "proxy").put(addressesSetting.getKey(), remoteAddress.toString()).put(socketConnections.getKey(), numOfConnections).put(serverName.getKey(), "server-name").build();
assertFalse(strategy.shouldRebuildConnection(noChange));
Settings addressesChanged = Settings.builder().put(modeSetting.getKey(), "proxy").put(addressesSetting.getKey(), remoteAddress.toString()).build();
assertTrue(strategy.shouldRebuildConnection(addressesChanged));
Settings socketsChanged = Settings.builder().put(modeSetting.getKey(), "proxy").put(addressesSetting.getKey(), remoteAddress.toString()).put(socketConnections.getKey(), numOfConnections + 1).build();
assertTrue(strategy.shouldRebuildConnection(socketsChanged));
Settings serverNameChange = Settings.builder().put(modeSetting.getKey(), "proxy").put(addressesSetting.getKey(), remoteAddress.toString()).put(socketConnections.getKey(), numOfConnections).put(serverName.getKey(), "server-name2").build();
assertTrue(strategy.shouldRebuildConnection(serverNameChange));
}
}
}
}
use of org.opensearch.common.settings.Setting in project OpenSearch by opensearch-project.
the class IndexSettingsTests method testMergedSettingsArePassed.
public void testMergedSettingsArePassed() {
Version version = VersionUtils.getPreviousVersion();
Settings theSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).put(IndexMetadata.SETTING_INDEX_UUID, "0xdeadbeef").build();
final AtomicInteger integer = new AtomicInteger(0);
final StringBuilder builder = new StringBuilder();
Setting<Integer> integerSetting = Setting.intSetting("index.test.setting.int", -1, Property.Dynamic, Property.IndexScope);
Setting<String> notUpdated = new Setting<>("index.not.updated", "", Function.identity(), Property.Dynamic, Property.IndexScope);
IndexSettings settings = newIndexSettings(newIndexMeta("index", theSettings), Settings.EMPTY, integerSetting, notUpdated);
settings.getScopedSettings().addSettingsUpdateConsumer(integerSetting, integer::set);
settings.getScopedSettings().addSettingsUpdateConsumer(notUpdated, builder::append);
assertEquals(0, integer.get());
assertEquals("", builder.toString());
IndexMetadata newMetadata = newIndexMeta("index", Settings.builder().put(settings.getIndexMetadata().getSettings()).put("index.test.setting.int", 42).build());
assertTrue(settings.updateIndexMetadata(newMetadata));
assertSame(settings.getIndexMetadata(), newMetadata);
assertEquals(42, integer.get());
assertEquals("", builder.toString());
integer.set(0);
assertTrue(settings.updateIndexMetadata(newIndexMeta("index", Settings.builder().put(settings.getIndexMetadata().getSettings()).put("index.not.updated", "boom").build())));
assertEquals("boom", builder.toString());
assertEquals("not updated - we preserve the old settings", 0, integer.get());
}
use of org.opensearch.common.settings.Setting in project OpenSearch by opensearch-project.
the class MetadataUpdateSettingsService method updateSettings.
public void updateSettings(final UpdateSettingsClusterStateUpdateRequest request, final ActionListener<ClusterStateUpdateResponse> listener) {
final Settings normalizedSettings = Settings.builder().put(request.settings()).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX).build();
Settings.Builder settingsForClosedIndices = Settings.builder();
Settings.Builder settingsForOpenIndices = Settings.builder();
final Set<String> skippedSettings = new HashSet<>();
indexScopedSettings.validate(// don't validate wildcards
normalizedSettings.filter(s -> Regex.isSimpleMatchPattern(s) == false), // don't validate dependencies here we check it below never allow to change the number of shards
false, true);
// validate internal or private index settings
for (String key : normalizedSettings.keySet()) {
Setting setting = indexScopedSettings.get(key);
boolean isWildcard = setting == null && Regex.isSimpleMatchPattern(key);
assert // we already validated the normalized settings
setting != null || (isWildcard && normalizedSettings.hasValue(key) == false) : "unknown setting: " + key + " isWildcard: " + isWildcard + " hasValue: " + normalizedSettings.hasValue(key);
settingsForClosedIndices.copy(key, normalizedSettings);
if (isWildcard || setting.isDynamic()) {
settingsForOpenIndices.copy(key, normalizedSettings);
} else {
skippedSettings.add(key);
}
}
final Settings closedSettings = settingsForClosedIndices.build();
final Settings openSettings = settingsForOpenIndices.build();
final boolean preserveExisting = request.isPreserveExisting();
clusterService.submitStateUpdateTask("update-settings " + Arrays.toString(request.indices()), new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(Priority.URGENT, request, wrapPreservingContext(listener, threadPool.getThreadContext())) {
@Override
protected ClusterStateUpdateResponse newResponse(boolean acknowledged) {
return new ClusterStateUpdateResponse(acknowledged);
}
@Override
public ClusterState execute(ClusterState currentState) {
RoutingTable.Builder routingTableBuilder = RoutingTable.builder(currentState.routingTable());
Metadata.Builder metadataBuilder = Metadata.builder(currentState.metadata());
// allow to change any settings to a close index, and only allow dynamic settings to be changed
// on an open index
Set<Index> openIndices = new HashSet<>();
Set<Index> closeIndices = new HashSet<>();
final String[] actualIndices = new String[request.indices().length];
for (int i = 0; i < request.indices().length; i++) {
Index index = request.indices()[i];
actualIndices[i] = index.getName();
final IndexMetadata metadata = currentState.metadata().getIndexSafe(index);
if (metadata.getState() == IndexMetadata.State.OPEN) {
openIndices.add(index);
} else {
closeIndices.add(index);
}
}
if (!skippedSettings.isEmpty() && !openIndices.isEmpty()) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "Can't update non dynamic settings [%s] for open indices %s", skippedSettings, openIndices));
}
if (IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.exists(openSettings)) {
final int updatedNumberOfReplicas = IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(openSettings);
if (preserveExisting == false) {
// Verify that this won't take us over the cluster shard limit.
int totalNewShards = Arrays.stream(request.indices()).mapToInt(i -> getTotalNewShards(i, currentState, updatedNumberOfReplicas)).sum();
Optional<String> error = shardLimitValidator.checkShardLimit(totalNewShards, currentState);
if (error.isPresent()) {
ValidationException ex = new ValidationException();
ex.addValidationError(error.get());
throw ex;
}
/*
* We do not update the in-sync allocation IDs as they will be removed upon the first index operation which makes
* these copies stale.
*
* TODO: should we update the in-sync allocation IDs once the data is deleted by the node?
*/
routingTableBuilder.updateNumberOfReplicas(updatedNumberOfReplicas, actualIndices);
metadataBuilder.updateNumberOfReplicas(updatedNumberOfReplicas, actualIndices);
logger.info("updating number_of_replicas to [{}] for indices {}", updatedNumberOfReplicas, actualIndices);
}
}
if (!openIndices.isEmpty()) {
for (Index index : openIndices) {
IndexMetadata indexMetadata = metadataBuilder.getSafe(index);
Settings.Builder updates = Settings.builder();
Settings.Builder indexSettings = Settings.builder().put(indexMetadata.getSettings());
if (indexScopedSettings.updateDynamicSettings(openSettings, indexSettings, updates, index.getName())) {
if (preserveExisting) {
indexSettings.put(indexMetadata.getSettings());
}
/*
* The setting index.number_of_replicas is special; we require that this setting has a value in the index. When
* creating the index, we ensure this by explicitly providing a value for the setting to the default (one) if
* there is a not value provided on the source of the index creation. A user can update this setting though,
* including updating it to null, indicating that they want to use the default value. In this case, we again
* have to provide an explicit value for the setting to the default (one).
*/
if (IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.exists(indexSettings) == false) {
indexSettings.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(Settings.EMPTY));
}
Settings finalSettings = indexSettings.build();
indexScopedSettings.validate(finalSettings.filter(k -> indexScopedSettings.isPrivateSetting(k) == false), true);
metadataBuilder.put(IndexMetadata.builder(indexMetadata).settings(finalSettings));
}
}
}
if (!closeIndices.isEmpty()) {
for (Index index : closeIndices) {
IndexMetadata indexMetadata = metadataBuilder.getSafe(index);
Settings.Builder updates = Settings.builder();
Settings.Builder indexSettings = Settings.builder().put(indexMetadata.getSettings());
if (indexScopedSettings.updateSettings(closedSettings, indexSettings, updates, index.getName())) {
if (preserveExisting) {
indexSettings.put(indexMetadata.getSettings());
}
/*
* The setting index.number_of_replicas is special; we require that this setting has a value in the index. When
* creating the index, we ensure this by explicitly providing a value for the setting to the default (one) if
* there is a not value provided on the source of the index creation. A user can update this setting though,
* including updating it to null, indicating that they want to use the default value. In this case, we again
* have to provide an explicit value for the setting to the default (one).
*/
if (IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.exists(indexSettings) == false) {
indexSettings.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(Settings.EMPTY));
}
Settings finalSettings = indexSettings.build();
indexScopedSettings.validate(finalSettings.filter(k -> indexScopedSettings.isPrivateSetting(k) == false), true);
metadataBuilder.put(IndexMetadata.builder(indexMetadata).settings(finalSettings));
}
}
}
if (IndexSettings.INDEX_TRANSLOG_RETENTION_AGE_SETTING.exists(normalizedSettings) || IndexSettings.INDEX_TRANSLOG_RETENTION_SIZE_SETTING.exists(normalizedSettings)) {
Settings indexSettings;
for (String index : actualIndices) {
indexSettings = metadataBuilder.get(index).getSettings();
MetadataCreateIndexService.validateTranslogRetentionSettings(indexSettings);
// validate storeType for deprecating index stores
MetadataCreateIndexService.validateStoreTypeSettings(indexSettings);
}
}
boolean changed = false;
// increment settings versions
for (final String index : actualIndices) {
if (same(currentState.metadata().index(index).getSettings(), metadataBuilder.get(index).getSettings()) == false) {
changed = true;
final IndexMetadata.Builder builder = IndexMetadata.builder(metadataBuilder.get(index));
builder.settingsVersion(1 + builder.settingsVersion());
metadataBuilder.put(builder);
}
}
final ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks());
for (IndexMetadata.APIBlock block : IndexMetadata.APIBlock.values()) {
changed |= maybeUpdateClusterBlock(actualIndices, blocks, block.block, block.setting, openSettings);
}
if (changed == false) {
return currentState;
}
ClusterState updatedState = ClusterState.builder(currentState).metadata(metadataBuilder).routingTable(routingTableBuilder.build()).blocks(blocks).build();
// now, reroute in case things change that require it (like number of replicas)
updatedState = allocationService.reroute(updatedState, "settings update");
try {
for (Index index : openIndices) {
final IndexMetadata currentMetadata = currentState.getMetadata().getIndexSafe(index);
final IndexMetadata updatedMetadata = updatedState.metadata().getIndexSafe(index);
indicesService.verifyIndexMetadata(currentMetadata, updatedMetadata);
}
for (Index index : closeIndices) {
final IndexMetadata currentMetadata = currentState.getMetadata().getIndexSafe(index);
final IndexMetadata updatedMetadata = updatedState.metadata().getIndexSafe(index);
// Verifies that the current index settings can be updated with the updated dynamic settings.
indicesService.verifyIndexMetadata(currentMetadata, updatedMetadata);
// Now check that we can create the index with the updated settings (dynamic and non-dynamic).
// This step is mandatory since we allow to update non-dynamic settings on closed indices.
indicesService.verifyIndexMetadata(updatedMetadata, updatedMetadata);
}
} catch (IOException ex) {
throw ExceptionsHelper.convertToOpenSearchException(ex);
}
return updatedState;
}
});
}
use of org.opensearch.common.settings.Setting in project OpenSearch by opensearch-project.
the class ProxyConnectionStrategyTests method testModeSettingsCannotBeUsedWhenInDifferentMode.
public void testModeSettingsCannotBeUsedWhenInDifferentMode() {
List<Tuple<Setting.AffixSetting<?>, String>> restrictedSettings = Arrays.asList(new Tuple<>(ProxyConnectionStrategy.PROXY_ADDRESS, "192.168.0.1:8080"), new Tuple<>(ProxyConnectionStrategy.REMOTE_SOCKET_CONNECTIONS, "3"));
RemoteConnectionStrategy.ConnectionStrategy sniff = RemoteConnectionStrategy.ConnectionStrategy.SNIFF;
String clusterName = "cluster_name";
Settings settings = Settings.builder().put(RemoteConnectionStrategy.REMOTE_CONNECTION_MODE.getConcreteSettingForNamespace(clusterName).getKey(), sniff.name()).build();
Set<Setting<?>> clusterSettings = new HashSet<>();
clusterSettings.add(RemoteConnectionStrategy.REMOTE_CONNECTION_MODE);
clusterSettings.addAll(restrictedSettings.stream().map(Tuple::v1).collect(Collectors.toList()));
AbstractScopedSettings service = new ClusterSettings(Settings.EMPTY, clusterSettings);
// Should validate successfully
service.validate(settings, true);
for (Tuple<Setting.AffixSetting<?>, String> restrictedSetting : restrictedSettings) {
Setting<?> concreteSetting = restrictedSetting.v1().getConcreteSettingForNamespace(clusterName);
Settings invalid = Settings.builder().put(settings).put(concreteSetting.getKey(), restrictedSetting.v2()).build();
IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> service.validate(invalid, true));
String expected = "Setting \"" + concreteSetting.getKey() + "\" cannot be used with the configured " + "\"cluster.remote.cluster_name.mode\" [required=PROXY, configured=SNIFF]";
assertEquals(expected, iae.getMessage());
}
}
use of org.opensearch.common.settings.Setting in project OpenSearch by opensearch-project.
the class SniffConnectionStrategyTests method testModeSettingsCannotBeUsedWhenInDifferentMode.
public void testModeSettingsCannotBeUsedWhenInDifferentMode() {
List<Tuple<Setting.AffixSetting<?>, String>> restrictedSettings = Arrays.asList(new Tuple<>(SniffConnectionStrategy.REMOTE_CLUSTER_SEEDS, "192.168.0.1:8080"), new Tuple<>(SniffConnectionStrategy.REMOTE_NODE_CONNECTIONS, "2"));
RemoteConnectionStrategy.ConnectionStrategy proxy = RemoteConnectionStrategy.ConnectionStrategy.PROXY;
String clusterName = "cluster_name";
Settings settings = Settings.builder().put(RemoteConnectionStrategy.REMOTE_CONNECTION_MODE.getConcreteSettingForNamespace(clusterName).getKey(), proxy.name()).build();
Set<Setting<?>> clusterSettings = new HashSet<>();
clusterSettings.add(RemoteConnectionStrategy.REMOTE_CONNECTION_MODE);
clusterSettings.addAll(restrictedSettings.stream().map(Tuple::v1).collect(Collectors.toList()));
AbstractScopedSettings service = new ClusterSettings(Settings.EMPTY, clusterSettings);
// Should validate successfully
service.validate(settings, true);
for (Tuple<Setting.AffixSetting<?>, String> restrictedSetting : restrictedSettings) {
Setting<?> concreteSetting = restrictedSetting.v1().getConcreteSettingForNamespace(clusterName);
Settings invalid = Settings.builder().put(settings).put(concreteSetting.getKey(), restrictedSetting.v2()).build();
IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> service.validate(invalid, true));
String expected = "Setting \"" + concreteSetting.getKey() + "\" cannot be used with the configured " + "\"cluster.remote.cluster_name.mode\" [required=SNIFF, configured=PROXY]";
assertEquals(expected, iae.getMessage());
}
}
Aggregations