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