Skip to content

Commit e7b49d8

Browse files
committed
Add more dynamic settings validation
1 parent d385e1b commit e7b49d8

File tree

4 files changed

+290
-70
lines changed

4 files changed

+290
-70
lines changed

src/main/java/org/elasticsearch/cluster/settings/ClusterDynamicSettingsModule.java

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,35 @@ public class ClusterDynamicSettingsModule extends AbstractModule {
3838

3939
public ClusterDynamicSettingsModule() {
4040
clusterDynamicSettings = new DynamicSettings();
41-
clusterDynamicSettings.addDynamicSettings(
42-
AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTES,
43-
AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_FORCE_GROUP + "*",
44-
BalancedShardsAllocator.SETTING_INDEX_BALANCE_FACTOR,
45-
BalancedShardsAllocator.SETTING_PRIMARY_BALANCE_FACTOR,
46-
BalancedShardsAllocator.SETTING_SHARD_BALANCE_FACTOR,
47-
BalancedShardsAllocator.SETTING_THRESHOLD,
48-
ConcurrentRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_CLUSTER_CONCURRENT_REBALANCE,
49-
DisableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_DISABLE_NEW_ALLOCATION,
50-
DisableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_DISABLE_ALLOCATION,
51-
DisableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_DISABLE_REPLICA_ALLOCATION,
52-
ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES,
53-
FilterAllocationDecider.CLUSTER_ROUTING_INCLUDE_GROUP + "*",
54-
FilterAllocationDecider.CLUSTER_ROUTING_EXCLUDE_GROUP + "*",
55-
FilterAllocationDecider.CLUSTER_ROUTING_REQUIRE_GROUP + "*",
56-
IndicesFilterCache.INDICES_CACHE_FILTER_SIZE,
57-
IndicesFilterCache.INDICES_CACHE_FILTER_EXPIRE,
58-
IndicesStore.INDICES_STORE_THROTTLE_TYPE,
59-
IndicesStore.INDICES_STORE_THROTTLE_MAX_BYTES_PER_SEC,
60-
IndicesTTLService.INDICES_TTL_INTERVAL,
61-
MetaData.SETTING_READ_ONLY,
62-
RecoverySettings.INDICES_RECOVERY_FILE_CHUNK_SIZE,
63-
RecoverySettings.INDICES_RECOVERY_TRANSLOG_OPS,
64-
RecoverySettings.INDICES_RECOVERY_TRANSLOG_SIZE,
65-
RecoverySettings.INDICES_RECOVERY_COMPRESS,
66-
RecoverySettings.INDICES_RECOVERY_CONCURRENT_STREAMS,
67-
RecoverySettings.INDICES_RECOVERY_MAX_SIZE_PER_SEC,
68-
ThreadPool.THREADPOOL_GROUP + "*",
69-
ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_INITIAL_PRIMARIES_RECOVERIES,
70-
ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_RECOVERIES
71-
);
41+
clusterDynamicSettings.addDynamicSetting(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTES);
42+
clusterDynamicSettings.addDynamicSetting(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_FORCE_GROUP + "*");
43+
clusterDynamicSettings.addDynamicSetting(BalancedShardsAllocator.SETTING_INDEX_BALANCE_FACTOR, Validator.FLOAT);
44+
clusterDynamicSettings.addDynamicSetting(BalancedShardsAllocator.SETTING_PRIMARY_BALANCE_FACTOR, Validator.FLOAT);
45+
clusterDynamicSettings.addDynamicSetting(BalancedShardsAllocator.SETTING_SHARD_BALANCE_FACTOR, Validator.FLOAT);
46+
clusterDynamicSettings.addDynamicSetting(BalancedShardsAllocator.SETTING_THRESHOLD, Validator.NON_NEGATIVE_FLOAT);
47+
clusterDynamicSettings.addDynamicSetting(ConcurrentRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_CLUSTER_CONCURRENT_REBALANCE, Validator.INTEGER);
48+
clusterDynamicSettings.addDynamicSetting(DisableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_DISABLE_NEW_ALLOCATION);
49+
clusterDynamicSettings.addDynamicSetting(DisableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_DISABLE_ALLOCATION);
50+
clusterDynamicSettings.addDynamicSetting(DisableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_DISABLE_REPLICA_ALLOCATION);
51+
clusterDynamicSettings.addDynamicSetting(ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES, Validator.INTEGER);
52+
clusterDynamicSettings.addDynamicSetting(FilterAllocationDecider.CLUSTER_ROUTING_INCLUDE_GROUP + "*");
53+
clusterDynamicSettings.addDynamicSetting(FilterAllocationDecider.CLUSTER_ROUTING_EXCLUDE_GROUP + "*");
54+
clusterDynamicSettings.addDynamicSetting(FilterAllocationDecider.CLUSTER_ROUTING_REQUIRE_GROUP + "*");
55+
clusterDynamicSettings.addDynamicSetting(IndicesFilterCache.INDICES_CACHE_FILTER_SIZE);
56+
clusterDynamicSettings.addDynamicSetting(IndicesFilterCache.INDICES_CACHE_FILTER_EXPIRE, Validator.TIME);
57+
clusterDynamicSettings.addDynamicSetting(IndicesStore.INDICES_STORE_THROTTLE_TYPE);
58+
clusterDynamicSettings.addDynamicSetting(IndicesStore.INDICES_STORE_THROTTLE_MAX_BYTES_PER_SEC, Validator.BYTES_SIZE);
59+
clusterDynamicSettings.addDynamicSetting(IndicesTTLService.INDICES_TTL_INTERVAL, Validator.TIME);
60+
clusterDynamicSettings.addDynamicSetting(MetaData.SETTING_READ_ONLY);
61+
clusterDynamicSettings.addDynamicSetting(RecoverySettings.INDICES_RECOVERY_FILE_CHUNK_SIZE, Validator.BYTES_SIZE);
62+
clusterDynamicSettings.addDynamicSetting(RecoverySettings.INDICES_RECOVERY_TRANSLOG_OPS, Validator.INTEGER);
63+
clusterDynamicSettings.addDynamicSetting(RecoverySettings.INDICES_RECOVERY_TRANSLOG_SIZE, Validator.BYTES_SIZE);
64+
clusterDynamicSettings.addDynamicSetting(RecoverySettings.INDICES_RECOVERY_COMPRESS);
65+
clusterDynamicSettings.addDynamicSetting(RecoverySettings.INDICES_RECOVERY_CONCURRENT_STREAMS, Validator.POSITIVE_INTEGER);
66+
clusterDynamicSettings.addDynamicSetting(RecoverySettings.INDICES_RECOVERY_MAX_SIZE_PER_SEC, Validator.BYTES_SIZE);
67+
clusterDynamicSettings.addDynamicSetting(ThreadPool.THREADPOOL_GROUP + "*");
68+
clusterDynamicSettings.addDynamicSetting(ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_INITIAL_PRIMARIES_RECOVERIES, Validator.INTEGER);
69+
clusterDynamicSettings.addDynamicSetting(ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_RECOVERIES, Validator.INTEGER);
7270
}
7371

7472
public void addDynamicSettings(String... settings) {

src/main/java/org/elasticsearch/cluster/settings/Validator.java

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.elasticsearch.ElasticSearchParseException;
2323
import org.elasticsearch.common.unit.TimeValue;
2424

25+
import static org.elasticsearch.common.unit.ByteSizeValue.parseBytesSizeValue;
26+
2527
/**
2628
* Validates a setting, returning a failure message if applicable.
2729
*/
@@ -49,4 +51,137 @@ public String validate(String setting, String value) {
4951
return null;
5052
}
5153
};
54+
55+
public static final Validator FLOAT = new Validator() {
56+
@Override
57+
public String validate(String setting, String value) {
58+
try {
59+
Float.parseFloat(value);
60+
} catch (NumberFormatException ex) {
61+
return "cannot parse value [" + value + "] as a float";
62+
}
63+
return null;
64+
}
65+
};
66+
67+
public static final Validator NON_NEGATIVE_FLOAT = new Validator() {
68+
@Override
69+
public String validate(String setting, String value) {
70+
try {
71+
if (Float.parseFloat(value) < 0.0) {
72+
return "the value of the setting " + setting + " must be a non negative float";
73+
}
74+
} catch (NumberFormatException ex) {
75+
return "cannot parse value [" + value + "] as a double";
76+
}
77+
return null;
78+
}
79+
};
80+
81+
public static final Validator DOUBLE = new Validator() {
82+
@Override
83+
public String validate(String setting, String value) {
84+
try {
85+
Double.parseDouble(value);
86+
} catch (NumberFormatException ex) {
87+
return "cannot parse value [" + value + "] as a double";
88+
}
89+
return null;
90+
}
91+
};
92+
93+
public static final Validator NON_NEGATIVE_DOUBLE = new Validator() {
94+
@Override
95+
public String validate(String setting, String value) {
96+
try {
97+
if (Double.parseDouble(value) < 0.0) {
98+
return "the value of the setting " + setting + " must be a non negative double";
99+
}
100+
} catch (NumberFormatException ex) {
101+
return "cannot parse value [" + value + "] as a double";
102+
}
103+
return null;
104+
}
105+
};
106+
107+
public static final Validator DOUBLE_GTE_2 = new Validator() {
108+
@Override
109+
public String validate(String setting, String value) {
110+
try {
111+
if (Double.parseDouble(value) < 2.0) {
112+
return "the value of the setting " + setting + " must be >= 2.0";
113+
}
114+
} catch (NumberFormatException ex) {
115+
return "cannot parse value [" + value + "] as a double";
116+
}
117+
return null;
118+
}
119+
};
120+
121+
public static final Validator INTEGER = new Validator() {
122+
@Override
123+
public String validate(String setting, String value) {
124+
try {
125+
Integer.parseInt(value);
126+
} catch (NumberFormatException ex) {
127+
return "cannot parse value [" + value + "] as an integer";
128+
}
129+
return null;
130+
}
131+
};
132+
133+
public static final Validator POSITIVE_INTEGER = new Validator() {
134+
@Override
135+
public String validate(String setting, String value) {
136+
try {
137+
if (Integer.parseInt(value) <= 0) {
138+
return "the value of the setting " + setting + " must be a positive integer";
139+
}
140+
} catch (NumberFormatException ex) {
141+
return "cannot parse value [" + value + "] as an integer";
142+
}
143+
return null;
144+
}
145+
};
146+
147+
public static final Validator NON_NEGATIVE_INTEGER = new Validator() {
148+
@Override
149+
public String validate(String setting, String value) {
150+
try {
151+
if (Integer.parseInt(value) < 0) {
152+
return "the value of the setting " + setting + " must be a non negative integer";
153+
}
154+
} catch (NumberFormatException ex) {
155+
return "cannot parse value [" + value + "] as an integer";
156+
}
157+
return null;
158+
}
159+
};
160+
161+
public static final Validator INTEGER_GTE_2 = new Validator() {
162+
@Override
163+
public String validate(String setting, String value) {
164+
try {
165+
if (Integer.parseInt(value) < 2) {
166+
return "the value of the setting " + setting + " must be >= 2";
167+
}
168+
} catch (NumberFormatException ex) {
169+
return "cannot parse value [" + value + "] as an integer";
170+
}
171+
return null;
172+
}
173+
};
174+
175+
public static final Validator BYTES_SIZE = new Validator() {
176+
@Override
177+
public String validate(String setting, String value) {
178+
try {
179+
parseBytesSizeValue(value);
180+
} catch (ElasticSearchParseException ex) {
181+
return ex.getMessage();
182+
}
183+
return null;
184+
}
185+
};
186+
52187
}

0 commit comments

Comments
 (0)