diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/RetryTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/RetryTests.java index 36fe08afc1ef5..a3d61fcaf1ac0 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/RetryTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/RetryTests.java @@ -176,15 +176,16 @@ private void testCase( // Not all test cases use the dest index but those that do require that it be on the node will small thread pools indicesAdmin().prepareCreate("dest").setSettings(indexSettings).get(); // Build the test data. Don't use indexRandom because that won't work consistently with such small thread pools. - BulkRequestBuilder bulk = client().prepareBulk(); - for (int i = 0; i < DOC_COUNT; i++) { - bulk.add(prepareIndex("source").setSource("foo", "bar " + i)); - } + try (BulkRequestBuilder bulk = client().prepareBulk()) { + for (int i = 0; i < DOC_COUNT; i++) { + bulk.add(prepareIndex("source").setSource("foo", "bar " + i)); + } - Retry retry = new Retry(BackoffPolicy.exponentialBackoff(), client().threadPool()); - BulkResponse initialBulkResponse = retry.withBackoff(client()::bulk, bulk.request()).actionGet(); - assertFalse(initialBulkResponse.buildFailureMessage(), initialBulkResponse.hasFailures()); - indicesAdmin().prepareRefresh("source").get(); + Retry retry = new Retry(BackoffPolicy.exponentialBackoff(), client().threadPool()); + BulkResponse initialBulkResponse = retry.withBackoff(client()::bulk, bulk.request()).actionGet(); + assertFalse(initialBulkResponse.buildFailureMessage(), initialBulkResponse.hasFailures()); + indicesAdmin().prepareRefresh("source").get(); + } AbstractBulkByScrollRequestBuilder builder = request.apply(internalCluster().masterClient()); // Make sure we use more than one batch so we have to scroll diff --git a/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/analytics/ingest/BulkProcessorFactoryTests.java b/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/analytics/ingest/BulkProcessorFactoryTests.java index aac7a4212fb7d..825fc236376ef 100644 --- a/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/analytics/ingest/BulkProcessorFactoryTests.java +++ b/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/analytics/ingest/BulkProcessorFactoryTests.java @@ -41,7 +41,9 @@ public static void afterClass() { public void testDefaultConstructor() throws Exception { BulkProcessorFactory factory = new BulkProcessorFactory(mock(Client.class), mock(AnalyticsEventIngestConfig.class)); - assertThat(factory.create(), instanceOf(BulkProcessor2.class)); + try (BulkProcessor2 bulkProcessor = factory.create()) { + assertThat(bulkProcessor, instanceOf(BulkProcessor2.class)); + } } public void testConfigValueAreUsed() throws Exception { diff --git a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionIT.java b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionIT.java index 63fbb7176558c..95a0469ba8972 100644 --- a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionIT.java +++ b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionIT.java @@ -215,11 +215,12 @@ public void testFromStatsGroupingByDate() { public void testFromGroupingByNumericFieldWithNulls() { for (int i = 0; i < 5; i++) { - client().prepareBulk() - .add(new IndexRequest("test").id("no_count_old_" + i).source("data", between(1, 2), "data_d", 1d)) - .add(new IndexRequest("test").id("no_count_new_" + i).source("data", 99, "data_d", 1d)) - .add(new IndexRequest("test").id("no_data_" + i).source("count", 12, "count_d", 12d)) - .get(); + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) { + bulkRequestBuilder.add(new IndexRequest("test").id("no_count_old_" + i).source("data", between(1, 2), "data_d", 1d)) + .add(new IndexRequest("test").id("no_count_new_" + i).source("data", 99, "data_d", 1d)) + .add(new IndexRequest("test").id("no_data_" + i).source("count", 12, "count_d", 12d)) + .get(); + } if (randomBoolean()) { client().admin().indices().prepareRefresh("test").get(); } @@ -266,11 +267,14 @@ record Group(String color, double avg) { public void testFromStatsGroupingByKeywordWithNulls() { for (int i = 0; i < 5; i++) { - client().prepareBulk() - .add(new IndexRequest("test").id("no_color_" + i).source("data", 12, "count", 120, "data_d", 2d, "count_d", 120d)) - .add(new IndexRequest("test").id("no_count_red_" + i).source("data", 2, "data_d", 2d, "color", "red")) - .add(new IndexRequest("test").id("no_count_yellow_" + i).source("data", 2, "data_d", 2d, "color", "yellow")) - .get(); + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) { + bulkRequestBuilder.add( + new IndexRequest("test").id("no_color_" + i).source("data", 12, "count", 120, "data_d", 2d, "count_d", 120d) + ) + .add(new IndexRequest("test").id("no_count_red_" + i).source("data", 2, "data_d", 2d, "color", "red")) + .add(new IndexRequest("test").id("no_count_yellow_" + i).source("data", 2, "data_d", 2d, "color", "yellow")) + .get(); + } if (randomBoolean()) { client().admin().indices().prepareRefresh("test").get(); } @@ -584,7 +588,9 @@ public void testStringLength() { public void testFilterWithNullAndEvalFromIndex() { // append entry, with an absent count, to the index - client().prepareBulk().add(new IndexRequest("test").id("no_count").source("data", 12, "data_d", 2d, "color", "red")).get(); + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) { + bulkRequestBuilder.add(new IndexRequest("test").id("no_count").source("data", 12, "data_d", 2d, "color", "red")).get(); + } client().admin().indices().prepareRefresh("test").get(); // sanity @@ -731,7 +737,9 @@ public void testRefreshSearchIdleShards() throws Exception { long sum = 0; for (int i = 0; i < numDocs; i++) { long value = randomLongBetween(1, 1000); - client().prepareBulk().add(new IndexRequest(indexName).id("doc-" + i).source("data", 1, "value", value)).get(); + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) { + bulkRequestBuilder.add(new IndexRequest(indexName).id("doc-" + i).source("data", 1, "value", value)).get(); + } sum += value; } totalValues.set(sum); @@ -898,14 +906,15 @@ public void testIndexPatterns() throws Exception { .setMapping("data", "type=long", "count", "type=long") ); ensureYellow(indexName); - client().prepareBulk() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .add(new IndexRequest(indexName).id("1").source("data", ++i, "count", i * 1000)) - .add(new IndexRequest(indexName).id("2").source("data", ++i, "count", i * 1000)) - .add(new IndexRequest(indexName).id("3").source("data", ++i, "count", i * 1000)) - .add(new IndexRequest(indexName).id("4").source("data", ++i, "count", i * 1000)) - .add(new IndexRequest(indexName).id("5").source("data", ++i, "count", i * 1000)) - .get(); + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) { + bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .add(new IndexRequest(indexName).id("1").source("data", ++i, "count", i * 1000)) + .add(new IndexRequest(indexName).id("2").source("data", ++i, "count", i * 1000)) + .add(new IndexRequest(indexName).id("3").source("data", ++i, "count", i * 1000)) + .add(new IndexRequest(indexName).id("4").source("data", ++i, "count", i * 1000)) + .add(new IndexRequest(indexName).id("5").source("data", ++i, "count", i * 1000)) + .get(); + } } try (var results = run("from test_index_patterns* | stats count(data), sum(count)")) { @@ -958,24 +967,25 @@ public void testOverlappingIndexPatterns() throws Exception { .setMapping("field", "type=long") ); ensureYellow("test_overlapping_index_patterns_1"); - client().prepareBulk() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .add(new IndexRequest("test_overlapping_index_patterns_1").id("1").source("field", 10)) - .get(); + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) { + bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .add(new IndexRequest("test_overlapping_index_patterns_1").id("1").source("field", 10)) + .get(); - assertAcked( - client().admin() - .indices() - .prepareCreate("test_overlapping_index_patterns_2") - .setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5))) - .setMapping("field", "type=keyword") - ); + assertAcked( + client().admin() + .indices() + .prepareCreate("test_overlapping_index_patterns_2") + .setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5))) + .setMapping("field", "type=keyword") + ); + } ensureYellow("test_overlapping_index_patterns_2"); - client().prepareBulk() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .add(new IndexRequest("test_overlapping_index_patterns_2").id("1").source("field", "foo")) - .get(); - + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) { + bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .add(new IndexRequest("test_overlapping_index_patterns_2").id("1").source("field", "foo")) + .get(); + } expectThrows(VerificationException.class, () -> run("from test_overlapping_index_patterns_* | sort field")); } @@ -1035,11 +1045,12 @@ public void testTopNPushedToLucene() { var yellowNullCountDocId = "yellow_null_count_" + i; var yellowNullDataDocId = "yellow_null_data_" + i; - client().prepareBulk() - .add(new IndexRequest("test").id(yellowDocId).source("data", i, "count", i * 10, "color", "yellow")) - .add(new IndexRequest("test").id(yellowNullCountDocId).source("data", i, "color", "yellow")) - .add(new IndexRequest("test").id(yellowNullDataDocId).source("count", i * 10, "color", "yellow")) - .get(); + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) { + bulkRequestBuilder.add(new IndexRequest("test").id(yellowDocId).source("data", i, "count", i * 10, "color", "yellow")) + .add(new IndexRequest("test").id(yellowNullCountDocId).source("data", i, "color", "yellow")) + .add(new IndexRequest("test").id(yellowNullDataDocId).source("count", i * 10, "color", "yellow")) + .get(); + } if (randomBoolean()) { client().admin().indices().prepareRefresh("test").get(); } @@ -1380,27 +1391,28 @@ private void createNestedMappingIndex(String indexName) throws IOException { private int indexDocsIntoNestedMappingIndex(String indexName, int docsCount) throws IOException { int countValuesGreaterThanFifty = 0; - BulkRequestBuilder bulkBuilder = client().prepareBulk(); - for (int j = 0; j < docsCount; j++) { - XContentBuilder builder = JsonXContent.contentBuilder(); - int randomValue = randomIntBetween(0, 100); - countValuesGreaterThanFifty += randomValue >= 50 ? 1 : 0; - builder.startObject(); - { - builder.field("data", randomValue); - builder.startArray("nested"); + try (BulkRequestBuilder bulkBuilder = client().prepareBulk()) { + for (int j = 0; j < docsCount; j++) { + XContentBuilder builder = JsonXContent.contentBuilder(); + int randomValue = randomIntBetween(0, 100); + countValuesGreaterThanFifty += randomValue >= 50 ? 1 : 0; + builder.startObject(); { - for (int k = 0, max = randomIntBetween(1, 5); k < max; k++) { - // nested values are all greater than any non-nested values found in the "data" long field - builder.startObject().field("foo", randomIntBetween(1000, 10000)).endObject(); + builder.field("data", randomValue); + builder.startArray("nested"); + { + for (int k = 0, max = randomIntBetween(1, 5); k < max; k++) { + // nested values are all greater than any non-nested values found in the "data" long field + builder.startObject().field("foo", randomIntBetween(1000, 10000)).endObject(); + } } + builder.endArray(); } - builder.endArray(); + builder.endObject(); + bulkBuilder.add(new IndexRequest(indexName).id(Integer.toString(j)).source(builder)); } - builder.endObject(); - bulkBuilder.add(new IndexRequest(indexName).id(Integer.toString(j)).source(builder)); + bulkBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); } - bulkBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); ensureYellow(indexName); return countValuesGreaterThanFifty; @@ -1454,25 +1466,26 @@ private void createAndPopulateIndex(String indexName, Settings additionalSetting ); long timestamp = epoch; for (int i = 0; i < 10; i++) { - client().prepareBulk() - .add( + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) { + bulkRequestBuilder.add( new IndexRequest(indexName).id("1" + i) .source("data", 1, "count", 40, "data_d", 1d, "count_d", 40d, "time", timestamp++, "color", "red") ) - .add( - new IndexRequest(indexName).id("2" + i) - .source("data", 2, "count", 42, "data_d", 2d, "count_d", 42d, "time", timestamp++, "color", "blue") - ) - .add( - new IndexRequest(indexName).id("3" + i) - .source("data", 1, "count", 44, "data_d", 1d, "count_d", 44d, "time", timestamp++, "color", "green") - ) - .add( - new IndexRequest(indexName).id("4" + i) - .source("data", 2, "count", 46, "data_d", 2d, "count_d", 46d, "time", timestamp++, "color", "red") - ) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + .add( + new IndexRequest(indexName).id("2" + i) + .source("data", 2, "count", 42, "data_d", 2d, "count_d", 42d, "time", timestamp++, "color", "blue") + ) + .add( + new IndexRequest(indexName).id("3" + i) + .source("data", 1, "count", 44, "data_d", 1d, "count_d", 44d, "time", timestamp++, "color", "green") + ) + .add( + new IndexRequest(indexName).id("4" + i) + .source("data", 2, "count", 46, "data_d", 2d, "count_d", 46d, "time", timestamp++, "color", "red") + ) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); + } } ensureYellow(indexName); } diff --git a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionRuntimeFieldIT.java b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionRuntimeFieldIT.java index a362609876ea0..a12835b6582e2 100644 --- a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionRuntimeFieldIT.java +++ b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionRuntimeFieldIT.java @@ -111,11 +111,12 @@ private void createIndexWithConstRuntimeField(String type) throws InterruptedExc mapping.endObject(); client().admin().indices().prepareCreate("test").setMapping(mapping.endObject()).get(); - BulkRequestBuilder bulk = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - for (int i = 0; i < SIZE; i++) { - bulk.add(prepareIndex("test").setId(Integer.toString(i)).setSource("foo", i)); + try (BulkRequestBuilder bulk = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)) { + for (int i = 0; i < SIZE; i++) { + bulk.add(prepareIndex("test").setId(Integer.toString(i)).setSource("foo", i)); + } + bulk.get(); } - bulk.get(); } public static class TestRuntimeFieldPlugin extends Plugin implements ScriptPlugin { diff --git a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionTaskIT.java b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionTaskIT.java index 2d1d01e42b509..d8b497239bc02 100644 --- a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionTaskIT.java +++ b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionTaskIT.java @@ -119,11 +119,12 @@ public void setupIndex() throws IOException { .setMapping(mapping.endObject()) .get(); - BulkRequestBuilder bulk = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - for (int i = 0; i < NUM_DOCS; i++) { - bulk.add(prepareIndex("test").setId(Integer.toString(i)).setSource("foo", i)); + try (BulkRequestBuilder bulk = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)) { + for (int i = 0; i < NUM_DOCS; i++) { + bulk.add(prepareIndex("test").setId(Integer.toString(i)).setSource("foo", i)); + } + bulk.get(); } - bulk.get(); /* * forceMerge so we can be sure that we don't bump into tiny * segments that finish super quickly and cause us to report strange diff --git a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/ManyShardsIT.java b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/ManyShardsIT.java index 7828ba97ed62b..1eec00426a2d3 100644 --- a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/ManyShardsIT.java +++ b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/ManyShardsIT.java @@ -40,14 +40,15 @@ public void testConcurrentQueries() throws Exception { ) .setMapping("user", "type=keyword", "tags", "type=keyword") .get(); - BulkRequestBuilder bulk = client().prepareBulk(index).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - int numDocs = between(5, 10); - for (int d = 0; d < numDocs; d++) { - String user = randomFrom("u1", "u2", "u3"); - String tag = randomFrom("java", "elasticsearch", "lucene"); - bulk.add(new IndexRequest().source(Map.of("user", user, "tags", tag))); + try (BulkRequestBuilder bulk = client().prepareBulk(index).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)) { + int numDocs = between(5, 10); + for (int d = 0; d < numDocs; d++) { + String user = randomFrom("u1", "u2", "u3"); + String tag = randomFrom("java", "elasticsearch", "lucene"); + bulk.add(new IndexRequest().source(Map.of("user", user, "tags", tag))); + } + bulk.get(); } - bulk.get(); } int numQueries = between(10, 20); Thread[] threads = new Thread[numQueries]; diff --git a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/CanMatchIT.java b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/CanMatchIT.java index 3969190630fd3..fd0ffef97ae78 100644 --- a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/CanMatchIT.java +++ b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/CanMatchIT.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.esql.plugin; +import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.cluster.metadata.IndexMetadata; @@ -50,22 +51,24 @@ public void testCanMatch() { .prepareCreate("events_2022") .setMapping("@timestamp", "type=date,format=yyyy-MM-dd", "uid", "type=keyword") ); - client().prepareBulk("events_2022") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .add(new IndexRequest().source("@timestamp", "2022-02-15", "uid", "u1")) - .add(new IndexRequest().source("@timestamp", "2022-05-02", "uid", "u1")) - .add(new IndexRequest().source("@timestamp", "2022-12-15", "uid", "u1")) - .get(); + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk("events_2022")) { + bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .add(new IndexRequest().source("@timestamp", "2022-02-15", "uid", "u1")) + .add(new IndexRequest().source("@timestamp", "2022-05-02", "uid", "u1")) + .add(new IndexRequest().source("@timestamp", "2022-12-15", "uid", "u1")) + .get(); + } ElasticsearchAssertions.assertAcked( client().admin().indices().prepareCreate("events_2023").setMapping("@timestamp", "type=date", "uid", "type=keyword") ); - client().prepareBulk("events_2023") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .add(new IndexRequest().source("@timestamp", "2023-01-15", "uid", "u2")) - .add(new IndexRequest().source("@timestamp", "2023-02-01", "uid", "u2")) - .add(new IndexRequest().source("@timestamp", "2023-02-11", "uid", "u1")) - .add(new IndexRequest().source("@timestamp", "2023-03-25", "uid", "u1")) - .get(); + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk("events_2023")) { + bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .add(new IndexRequest().source("@timestamp", "2023-01-15", "uid", "u2")) + .add(new IndexRequest().source("@timestamp", "2023-02-01", "uid", "u2")) + .add(new IndexRequest().source("@timestamp", "2023-02-11", "uid", "u1")) + .add(new IndexRequest().source("@timestamp", "2023-03-25", "uid", "u1")) + .get(); + } try { Set queriedIndices = ConcurrentCollections.newConcurrentSet(); for (TransportService ts : internalCluster().getInstances(TransportService.class)) { @@ -127,15 +130,16 @@ public void testAliasFilters() { .prepareCreate("employees") .setMapping("emp_no", "type=long", "dept", "type=keyword", "hired", "type=date,format=yyyy-MM-dd", "salary", "type=double") ); - client().prepareBulk("employees") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .add(new IndexRequest().source("emp_no", 101, "dept", "engineering", "hired", "2012-02-05", "salary", 20)) - .add(new IndexRequest().source("emp_no", 102, "dept", "sales", "hired", "2012-03-15", "salary", 25)) - .add(new IndexRequest().source("emp_no", 103, "dept", "engineering", "hired", "2012-03-27", "salary", 22)) - .add(new IndexRequest().source("emp_no", 104, "dept", "engineering", "hired", "2012-04-20", "salary", 39.6)) - .add(new IndexRequest().source("emp_no", 105, "dept", "engineering", "hired", "2012-06-30", "salary", 25)) - .add(new IndexRequest().source("emp_no", 106, "dept", "sales", "hired", "2012-08-09", "salary", 30.1)) - .get(); + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk("employees")) { + bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .add(new IndexRequest().source("emp_no", 101, "dept", "engineering", "hired", "2012-02-05", "salary", 20)) + .add(new IndexRequest().source("emp_no", 102, "dept", "sales", "hired", "2012-03-15", "salary", 25)) + .add(new IndexRequest().source("emp_no", 103, "dept", "engineering", "hired", "2012-03-27", "salary", 22)) + .add(new IndexRequest().source("emp_no", 104, "dept", "engineering", "hired", "2012-04-20", "salary", 39.6)) + .add(new IndexRequest().source("emp_no", 105, "dept", "engineering", "hired", "2012-06-30", "salary", 25)) + .add(new IndexRequest().source("emp_no", 106, "dept", "sales", "hired", "2012-08-09", "salary", 30.1)) + .get(); + } ElasticsearchAssertions.assertAcked( client().admin() @@ -219,12 +223,13 @@ public void testFailOnUnavailableShards() throws Exception { ) .setMapping("timestamp", "type=long", "message", "type=keyword") ); - client().prepareBulk("events") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .add(new IndexRequest().source("timestamp", 1, "message", "a")) - .add(new IndexRequest().source("timestamp", 2, "message", "b")) - .add(new IndexRequest().source("timestamp", 3, "message", "c")) - .get(); + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk("events")) { + bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .add(new IndexRequest().source("timestamp", 1, "message", "a")) + .add(new IndexRequest().source("timestamp", 2, "message", "b")) + .add(new IndexRequest().source("timestamp", 3, "message", "c")) + .get(); + } ElasticsearchAssertions.assertAcked( client().admin() .indices() @@ -236,11 +241,12 @@ public void testFailOnUnavailableShards() throws Exception { ) .setMapping("timestamp", "type=long", "message", "type=keyword") ); - client().prepareBulk("logs") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .add(new IndexRequest().source("timestamp", 10, "message", "aa")) - .add(new IndexRequest().source("timestamp", 11, "message", "bb")) - .get(); + try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk("logs")) { + bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .add(new IndexRequest().source("timestamp", 10, "message", "aa")) + .add(new IndexRequest().source("timestamp", 11, "message", "bb")) + .get(); + } try (EsqlQueryResponse resp = run("from events,logs | KEEP timestamp,message")) { assertThat(getValuesList(resp), hasSize(5)); internalCluster().stopNode(logsOnlyNode); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceTests.java index fb681b8f966ae..455a10eace64c 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceTests.java @@ -152,6 +152,7 @@ public class TokenServiceTests extends ESTestCase { .build(); private MockLicenseState licenseState; private SecurityContext securityContext; + private BulkRequestBuilder bulkRequestBuilder; @Before public void setupClient() { @@ -164,7 +165,8 @@ public void setupClient() { return builder; }).when(client).prepareGet(anyString(), anyString()); when(client.prepareIndex(any(String.class))).thenReturn(new IndexRequestBuilder(client)); - when(client.prepareBulk()).thenReturn(new BulkRequestBuilder(client)); + bulkRequestBuilder = new BulkRequestBuilder(client); // closed in the test's cleanup() method + when(client.prepareBulk()).thenReturn(bulkRequestBuilder); when(client.prepareUpdate(any(String.class), any(String.class))).thenAnswer(inv -> { final String index = (String) inv.getArguments()[0]; final String id = (String) inv.getArguments()[1]; @@ -233,6 +235,13 @@ public void setupClient() { } } + @After + public void cleanup() { + if (bulkRequestBuilder != null) { + bulkRequestBuilder.close(); + } + } + private static DiscoveryNode addAnother7071DataNode(ClusterService clusterService) { Version version; TransportVersion transportVersion; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizationServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizationServiceTests.java index bf358f03e16a5..4ad38894f5921 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizationServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizationServiceTests.java @@ -99,6 +99,7 @@ import org.elasticsearch.common.util.concurrent.ThreadContext.StoredContext; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.Releasable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.IndexNotFoundException; @@ -1097,6 +1098,9 @@ public void testElasticUserAuthorizedForNonChangePasswordRequestsWhenNotInSetupM eq(request.v2()), authzInfoRoles(new String[] { ElasticUser.ROLE_NAME }) ); + if (request.v2() instanceof Releasable releasable) { + releasable.close(); + } } public void testSearchAgainstEmptyCluster() throws Exception { @@ -2329,6 +2333,9 @@ public void testCompositeActionsAreImmediatelyRejected() { authzInfoRoles(new String[] { role.getName() }) ); verifyNoMoreInteractions(auditTrail); + if (request instanceof Releasable releasable) { + releasable.close(); + } } public void testCompositeActionsIndicesAreNotChecked() { @@ -2355,10 +2362,17 @@ public void testCompositeActionsIndicesAreNotChecked() { authzInfoRoles(new String[] { role.getName() }) ); verifyNoMoreInteractions(auditTrail); + if (request instanceof Releasable releasable) { + releasable.close(); + } } public void testCompositeActionsMustImplementCompositeIndicesRequest() { - String action = randomCompositeRequest().v1(); + Tuple compositeRequest = randomCompositeRequest(); + if (compositeRequest.v2() instanceof Releasable releasable) { + releasable.close(); // we're not using it in this test + } + String action = compositeRequest.v1(); TransportRequest request = mock(TransportRequest.class); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); User user = new User("test user", "role");