2121
2222import org .elasticsearch .common .settings .ClusterSettings ;
2323import org .elasticsearch .common .settings .Settings ;
24+ import org .elasticsearch .common .util .concurrent .EsExecutors ;
2425import org .elasticsearch .common .util .concurrent .EsThreadPoolExecutor ;
2526import org .elasticsearch .test .ESTestCase ;
2627import org .elasticsearch .threadpool .ThreadPool .Names ;
@@ -89,6 +90,51 @@ public void testThreadPoolCanNotOverrideThreadPoolType() throws InterruptedExcep
8990 }
9091 }
9192
93+ public void testIndexingThreadPoolsMaxSize () throws InterruptedException {
94+ String threadPoolName = randomThreadPoolName ();
95+ for (String name : new String [] {ThreadPool .Names .BULK , ThreadPool .Names .INDEX }) {
96+ ThreadPool threadPool = null ;
97+ try {
98+
99+ int maxSize = EsExecutors .boundedNumberOfProcessors (Settings .EMPTY );
100+
101+ // try to create a too-big (maxSize+1) thread pool
102+ threadPool = new ThreadPool (settingsBuilder ()
103+ .put ("name" , "testIndexingThreadPoolsMaxSize" )
104+ .put ("threadpool." + name + ".size" , maxSize +1 )
105+ .build ());
106+
107+ // confirm it clipped us at the maxSize:
108+ assertEquals (maxSize , ((ThreadPoolExecutor ) threadPool .executor (name )).getMaximumPoolSize ());
109+
110+ ClusterSettings clusterSettings = new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
111+ threadPool .setClusterSettings (clusterSettings );
112+
113+ // update it to a tiny size:
114+ clusterSettings .applySettings (
115+ settingsBuilder ()
116+ .put ("threadpool." + name + ".size" , 1 )
117+ .build ()
118+ );
119+
120+ // confirm it worked:
121+ assertEquals (1 , ((ThreadPoolExecutor ) threadPool .executor (name )).getMaximumPoolSize ());
122+
123+ // try to update to too-big size:
124+ clusterSettings .applySettings (
125+ settingsBuilder ()
126+ .put ("threadpool." + name + ".size" , maxSize +1 )
127+ .build ()
128+ );
129+
130+ // confirm it clipped us at the maxSize:
131+ assertEquals (maxSize , ((ThreadPoolExecutor ) threadPool .executor (name )).getMaximumPoolSize ());
132+ } finally {
133+ terminateThreadPoolIfNeeded (threadPool );
134+ }
135+ }
136+ }
137+
92138 public void testUpdateSettingsCanNotChangeThreadPoolType () throws InterruptedException {
93139 String threadPoolName = randomThreadPoolName ();
94140 ThreadPool .ThreadPoolType invalidThreadPoolType = randomIncorrectThreadPoolType (threadPoolName );
@@ -165,6 +211,14 @@ public void testCachedExecutorType() throws InterruptedException {
165211 }
166212 }
167213
214+ private static int getExpectedThreadPoolSize (Settings settings , String name , int size ) {
215+ if (name .equals (ThreadPool .Names .BULK ) || name .equals (ThreadPool .Names .INDEX )) {
216+ return Math .min (size , EsExecutors .boundedNumberOfProcessors (settings ));
217+ } else {
218+ return size ;
219+ }
220+ }
221+
168222 public void testFixedExecutorType () throws InterruptedException {
169223 String threadPoolName = randomThreadPool (ThreadPool .ThreadPoolType .FIXED );
170224 ThreadPool threadPool = null ;
@@ -179,12 +233,14 @@ public void testFixedExecutorType() throws InterruptedException {
179233 Settings settings = clusterSettings .applySettings (settingsBuilder ()
180234 .put ("threadpool." + threadPoolName + ".size" , "15" )
181235 .build ());
236+
237+ int expectedSize = getExpectedThreadPoolSize (nodeSettings , threadPoolName , 15 );
182238 assertEquals (info (threadPool , threadPoolName ).getThreadPoolType (), ThreadPool .ThreadPoolType .FIXED );
183239 assertThat (threadPool .executor (threadPoolName ), instanceOf (EsThreadPoolExecutor .class ));
184- assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getCorePoolSize (), equalTo (15 ));
185- assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getMaximumPoolSize (), equalTo (15 ));
186- assertThat (info (threadPool , threadPoolName ).getMin (), equalTo (15 ));
187- assertThat (info (threadPool , threadPoolName ).getMax (), equalTo (15 ));
240+ assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getCorePoolSize (), equalTo (expectedSize ));
241+ assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getMaximumPoolSize (), equalTo (expectedSize ));
242+ assertThat (info (threadPool , threadPoolName ).getMin (), equalTo (expectedSize ));
243+ assertThat (info (threadPool , threadPoolName ).getMax (), equalTo (expectedSize ));
188244 // keep alive does not apply to fixed thread pools
189245 assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getKeepAliveTime (TimeUnit .MINUTES ), equalTo (0L ));
190246
@@ -194,20 +250,23 @@ public void testFixedExecutorType() throws InterruptedException {
194250 // Make sure keep alive value is not used
195251 assertThat (info (threadPool , threadPoolName ).getKeepAlive (), nullValue ());
196252 // Make sure keep pool size value were reused
197- assertThat (info (threadPool , threadPoolName ).getMin (), equalTo (15 ));
198- assertThat (info (threadPool , threadPoolName ).getMax (), equalTo (15 ));
253+ assertThat (info (threadPool , threadPoolName ).getMin (), equalTo (expectedSize ));
254+ assertThat (info (threadPool , threadPoolName ).getMax (), equalTo (expectedSize ));
199255 assertThat (threadPool .executor (threadPoolName ), instanceOf (EsThreadPoolExecutor .class ));
200- assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getCorePoolSize (), equalTo (15 ));
201- assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getMaximumPoolSize (), equalTo (15 ));
256+ assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getCorePoolSize (), equalTo (expectedSize ));
257+ assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getMaximumPoolSize (), equalTo (expectedSize ));
202258
203259 // Change size
204260 Executor oldExecutor = threadPool .executor (threadPoolName );
205261 settings = clusterSettings .applySettings (settingsBuilder ().put (settings ).put ("threadpool." + threadPoolName + ".size" , "10" ).build ());
262+
263+ expectedSize = getExpectedThreadPoolSize (nodeSettings , threadPoolName , 10 );
264+
206265 // Make sure size values changed
207- assertThat (info (threadPool , threadPoolName ).getMax (), equalTo (10 ));
208- assertThat (info (threadPool , threadPoolName ).getMin (), equalTo (10 ));
209- assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getMaximumPoolSize (), equalTo (10 ));
210- assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getCorePoolSize (), equalTo (10 ));
266+ assertThat (info (threadPool , threadPoolName ).getMax (), equalTo (expectedSize ));
267+ assertThat (info (threadPool , threadPoolName ).getMin (), equalTo (expectedSize ));
268+ assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getMaximumPoolSize (), equalTo (expectedSize ));
269+ assertThat (((EsThreadPoolExecutor ) threadPool .executor (threadPoolName )).getCorePoolSize (), equalTo (expectedSize ));
211270 // Make sure executor didn't change
212271 assertEquals (info (threadPool , threadPoolName ).getThreadPoolType (), ThreadPool .ThreadPoolType .FIXED );
213272 assertThat (threadPool .executor (threadPoolName ), sameInstance (oldExecutor ));
0 commit comments