File tree 6 files changed +91
-0
lines changed
main/java/org/elasticsearch/bootstrap
test/java/org/elasticsearch/bootstrap
qa/evil-tests/src/test/java/org/elasticsearch/bootstrap 6 files changed +91
-0
lines changed Original file line number Diff line number Diff line change @@ -135,6 +135,8 @@ public boolean handle(int code) {
135
135
136
136
JNANatives .trySetMaxNumberOfThreads ();
137
137
138
+ JNANatives .trySetMaxSizeVirtualMemory ();
139
+
138
140
// init lucene random seed. it will use /dev/urandom where available:
139
141
StringHelper .randomId ();
140
142
}
Original file line number Diff line number Diff line change @@ -123,6 +123,7 @@ private static List<Check> checks(final Settings settings) {
123
123
if (Constants .LINUX ) {
124
124
checks .add (new MaxNumberOfThreadsCheck ());
125
125
}
126
+ checks .add (new MaxSizeVirtualMemoryCheck ());
126
127
return Collections .unmodifiableList (checks );
127
128
}
128
129
@@ -249,4 +250,27 @@ long getMaxNumberOfThreads() {
249
250
250
251
}
251
252
253
+ static class MaxSizeVirtualMemoryCheck implements Check {
254
+
255
+ @ Override
256
+ public boolean check () {
257
+ return getMaxSizeVirtualMemory () != Long .MIN_VALUE && getMaxSizeVirtualMemory () != JNACLibrary .RLIM_INFINITY ;
258
+ }
259
+
260
+ @ Override
261
+ public String errorMessage () {
262
+ return String .format (
263
+ Locale .ROOT ,
264
+ "max size virtual memory [%d] for user [%s] likely too low, increase to [unlimited]" ,
265
+ getMaxSizeVirtualMemory (),
266
+ BootstrapInfo .getSystemProperties ().get ("user.name" ));
267
+ }
268
+
269
+ // visible for testing
270
+ long getMaxSizeVirtualMemory () {
271
+ return JNANatives .MAX_SIZE_VIRTUAL_MEMORY ;
272
+ }
273
+
274
+ }
275
+
252
276
}
Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ final class JNACLibrary {
39
39
public static final int MCL_CURRENT = 1 ;
40
40
public static final int ENOMEM = 12 ;
41
41
public static final int RLIMIT_MEMLOCK = Constants .MAC_OS_X ? 6 : 8 ;
42
+ public static final int RLIMIT_AS = Constants .MAC_OS_X ? 5 : 9 ;
42
43
public static final long RLIM_INFINITY = Constants .MAC_OS_X ? 9223372036854775807L : -1L ;
43
44
44
45
static {
Original file line number Diff line number Diff line change @@ -52,6 +52,8 @@ private JNANatives() {}
52
52
// the user ID that owns the running Elasticsearch process
53
53
static long MAX_NUMBER_OF_THREADS = -1 ;
54
54
55
+ static long MAX_SIZE_VIRTUAL_MEMORY = Long .MIN_VALUE ;
56
+
55
57
static void tryMlockall () {
56
58
int errno = Integer .MIN_VALUE ;
57
59
String errMsg = null ;
@@ -124,6 +126,17 @@ static void trySetMaxNumberOfThreads() {
124
126
}
125
127
}
126
128
129
+ static void trySetMaxSizeVirtualMemory () {
130
+ if (Constants .LINUX || Constants .MAC_OS_X ) {
131
+ final JNACLibrary .Rlimit rlimit = new JNACLibrary .Rlimit ();
132
+ if (JNACLibrary .getrlimit (JNACLibrary .RLIMIT_AS , rlimit ) == 0 ) {
133
+ MAX_SIZE_VIRTUAL_MEMORY = rlimit .rlim_cur .longValue ();
134
+ } else {
135
+ logger .warn ("unable to retrieve max size virtual memory [" + JNACLibrary .strerror (Native .getLastError ()) + "]" );
136
+ }
137
+ }
138
+ }
139
+
127
140
static String rlimitToString (long value ) {
128
141
assert Constants .LINUX || Constants .MAC_OS_X ;
129
142
if (value == JNACLibrary .RLIM_INFINITY ) {
Original file line number Diff line number Diff line change @@ -157,6 +157,33 @@ long getMaxNumberOfThreads() {
157
157
BootstrapCheck .check (true , Collections .singletonList (check ));
158
158
}
159
159
160
+ public void testMaxSizeVirtualMemory () {
161
+ final long limit = JNACLibrary .RLIM_INFINITY ;
162
+ final AtomicLong maxSizeVirtualMemory = new AtomicLong (randomInt ());
163
+ final BootstrapCheck .MaxSizeVirtualMemoryCheck check = new BootstrapCheck .MaxSizeVirtualMemoryCheck () {
164
+ @ Override
165
+ long getMaxSizeVirtualMemory () {
166
+ return maxSizeVirtualMemory .get ();
167
+ }
168
+ };
169
+
170
+ try {
171
+ BootstrapCheck .check (true , Collections .singletonList (check ));
172
+ fail ("should have failed due to max size virtual memory too low" );
173
+ } catch (final RuntimeException e ) {
174
+ assertThat (e .getMessage (), containsString ("max size virtual memory" ));
175
+ }
176
+
177
+ maxSizeVirtualMemory .set (limit );
178
+
179
+ BootstrapCheck .check (true , Collections .singletonList (check ));
180
+
181
+ // nothing should happen if max size virtual memory is not
182
+ // available
183
+ maxSizeVirtualMemory .set (Long .MIN_VALUE );
184
+ BootstrapCheck .check (true , Collections .singletonList (check ));
185
+ }
186
+
160
187
public void testEnforceLimits () {
161
188
final Set <Setting > enforceSettings = BootstrapCheck .enforceSettings ();
162
189
final Setting setting = randomFrom (Arrays .asList (enforceSettings .toArray (new Setting [enforceSettings .size ()])));
Original file line number Diff line number Diff line change 27
27
import java .nio .file .Files ;
28
28
import java .util .List ;
29
29
30
+ import static org .hamcrest .Matchers .anyOf ;
30
31
import static org .hamcrest .Matchers .equalTo ;
32
+ import static org .hamcrest .Matchers .greaterThanOrEqualTo ;
31
33
32
34
public class EvilJNANativesTests extends ESTestCase {
33
35
@@ -49,4 +51,26 @@ public void testSetMaximumNumberOfThreads() throws IOException {
49
51
assertThat (JNANatives .MAX_NUMBER_OF_THREADS , equalTo (-1L ));
50
52
}
51
53
}
54
+
55
+ public void testSetMaxSizeVirtualMemory () throws IOException {
56
+ if (Constants .LINUX ) {
57
+ final List <String > lines = Files .readAllLines (PathUtils .get ("/proc/self/limits" ));
58
+ if (!lines .isEmpty ()) {
59
+ for (String line : lines ) {
60
+ if (line != null && line .startsWith ("Max address space" )) {
61
+ final String [] fields = line .split ("\\ s+" );
62
+ final String limit = fields [3 ];
63
+ assertEquals (JNANatives .rlimitToString (JNANatives .MAX_SIZE_VIRTUAL_MEMORY ), limit );
64
+ return ;
65
+ }
66
+ }
67
+ }
68
+ fail ("should have read max size virtual memory from /proc/self/limits" );
69
+ } else if (Constants .MAC_OS_X ) {
70
+ assertThat (JNANatives .MAX_SIZE_VIRTUAL_MEMORY , anyOf (equalTo (Long .MIN_VALUE ), greaterThanOrEqualTo (0L )));
71
+ } else {
72
+ assertThat (JNANatives .MAX_SIZE_VIRTUAL_MEMORY , equalTo (Long .MIN_VALUE ));
73
+ }
74
+ }
75
+
52
76
}
You can’t perform that action at this time.
0 commit comments