|
28 | 28 | import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem;
|
29 | 29 | import co.elastic.clients.elasticsearch.core.bulk.OperationType;
|
30 | 30 | import co.elastic.clients.elasticsearch.end_to_end.RequestTest;
|
| 31 | +import co.elastic.clients.elasticsearch.indices.IndicesStatsResponse; |
31 | 32 | import co.elastic.clients.json.JsonpMapper;
|
32 | 33 | import co.elastic.clients.json.JsonpUtils;
|
33 | 34 | import co.elastic.clients.json.SimpleJsonpMapper;
|
@@ -156,6 +157,50 @@ private void multiThreadTest(int maxOperations, int maxRequests, int numThreads,
|
156 | 157 | assertEquals(expectedRequests, transport.requestsStarted.get());
|
157 | 158 | }
|
158 | 159 |
|
| 160 | + @Test |
| 161 | + public void multiThreadStressTest() throws InterruptedException, IOException { |
| 162 | + |
| 163 | + String index = "bulk-ingester-stress-test"; |
| 164 | + ElasticsearchClient client = ElasticsearchTestServer.global().client(); |
| 165 | + |
| 166 | + // DISCLAIMER: this configuration is highly inefficient and only used here to showcase an extreme |
| 167 | + // situation where the number of adding threads greatly exceeds the number of concurrent requests |
| 168 | + // handled by the ingester. It's strongly recommended to always tweak maxConcurrentRequests accordingly. |
| 169 | + BulkIngester<?> ingester = BulkIngester.of(b -> b |
| 170 | + .client(client) |
| 171 | + .globalSettings(s -> s.index(index)) |
| 172 | + .flushInterval(5, TimeUnit.SECONDS) |
| 173 | + ); |
| 174 | + |
| 175 | + RequestTest.AppData appData = new RequestTest.AppData(); |
| 176 | + appData.setIntValue(42); |
| 177 | + appData.setMsg("Some message"); |
| 178 | + |
| 179 | + ExecutorService executor = Executors.newFixedThreadPool(50); |
| 180 | + |
| 181 | + for (int i = 0; i < 100000; i++) { |
| 182 | + int ii = i; |
| 183 | + Runnable thread = () -> { |
| 184 | + int finalI = ii; |
| 185 | + ingester.add(_1 -> _1 |
| 186 | + .create(_2 -> _2 |
| 187 | + .id(String.valueOf(finalI)) |
| 188 | + .document(appData) |
| 189 | + )); |
| 190 | + }; |
| 191 | + executor.submit(thread); |
| 192 | + } |
| 193 | + |
| 194 | + executor.awaitTermination(10,TimeUnit.SECONDS); |
| 195 | + ingester.close(); |
| 196 | + |
| 197 | + client.indices().refresh(); |
| 198 | + |
| 199 | + IndicesStatsResponse indexStats = client.indices().stats(g -> g.index(index)); |
| 200 | + |
| 201 | + assertTrue(indexStats.indices().get(index).primaries().docs().count()==100000); |
| 202 | + } |
| 203 | + |
159 | 204 | @Test
|
160 | 205 | public void sizeLimitTest() throws Exception {
|
161 | 206 | TestTransport transport = new TestTransport();
|
|
0 commit comments