Skip to content

Commit

Permalink
fixing more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
masseyke committed Nov 30, 2023
1 parent d3177f7 commit 3bfe9a5
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,15 @@ public void testIndexedTemplateClient() throws Exception {
GetStoredScriptResponse getResponse = clusterAdmin().prepareGetStoredScript("testTemplate").get();
assertNotNull(getResponse.getSource());

BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
bulkRequestBuilder.add(prepareIndex("test").setId("1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("5").setSource("{\"theField\":\"bar\"}", XContentType.JSON));
bulkRequestBuilder.get();
indicesAdmin().prepareRefresh().get();
try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) {
bulkRequestBuilder.add(prepareIndex("test").setId("1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("5").setSource("{\"theField\":\"bar\"}", XContentType.JSON));
bulkRequestBuilder.get();
indicesAdmin().prepareRefresh().get();
}

Map<String, Object> templateParams = new HashMap<>();
templateParams.put("fieldParam", "foo");
Expand Down Expand Up @@ -262,14 +263,15 @@ public void testIndexedTemplate() throws Exception {
assertAcked(clusterAdmin().preparePutStoredScript().setId("2").setContent(new BytesArray(script), XContentType.JSON));
assertAcked(clusterAdmin().preparePutStoredScript().setId("3").setContent(new BytesArray(script), XContentType.JSON));

BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
bulkRequestBuilder.add(prepareIndex("test").setId("1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("5").setSource("{\"theField\":\"bar\"}", XContentType.JSON));
bulkRequestBuilder.get();
indicesAdmin().prepareRefresh().get();
try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) {
bulkRequestBuilder.add(prepareIndex("test").setId("1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("5").setSource("{\"theField\":\"bar\"}", XContentType.JSON));
bulkRequestBuilder.get();
indicesAdmin().prepareRefresh().get();
}

Map<String, Object> templateParams = new HashMap<>();
templateParams.put("fieldParam", "foo");
Expand Down Expand Up @@ -381,14 +383,15 @@ public void testIndexedTemplateWithArray() throws Exception {
}
}""";
assertAcked(clusterAdmin().preparePutStoredScript().setId("4").setContent(new BytesArray(multiQuery), XContentType.JSON));
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
bulkRequestBuilder.add(prepareIndex("test").setId("1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("5").setSource("{\"theField\":\"bar\"}", XContentType.JSON));
bulkRequestBuilder.get();
indicesAdmin().prepareRefresh().get();
try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) {
bulkRequestBuilder.add(prepareIndex("test").setId("1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
bulkRequestBuilder.add(prepareIndex("test").setId("5").setSource("{\"theField\":\"bar\"}", XContentType.JSON));
bulkRequestBuilder.get();
indicesAdmin().prepareRefresh().get();
}

Map<String, Object> arrayTemplateParams = new HashMap<>();
String[] fieldParams = { "foo", "bar" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,12 @@ public void testCcrRepositoryFetchesSnapshotShardSizeFromIndexShardStoreStats()

final int numDocs = scaledRandomIntBetween(0, 500);
if (numDocs > 0) {
final BulkRequestBuilder bulkRequest = leaderClient().prepareBulk(leaderIndex);
for (int i = 0; i < numDocs; i++) {
bulkRequest.add(new IndexRequest(leaderIndex).id(Integer.toString(i)).source("field", i));
try (BulkRequestBuilder bulkRequest = leaderClient().prepareBulk(leaderIndex)) {
for (int i = 0; i < numDocs; i++) {
bulkRequest.add(new IndexRequest(leaderIndex).id(Integer.toString(i)).source("field", i));
}
assertThat(bulkRequest.get().hasFailures(), is(false));
}
assertThat(bulkRequest.get().hasFailures(), is(false));
}

ensureLeaderGreen(leaderIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -931,21 +931,22 @@ public void testGetDataStreamResponse() throws Exception {
}

static void indexDocs(String dataStream, int numDocs) {
BulkRequest bulkRequest = new BulkRequest();
for (int i = 0; i < numDocs; i++) {
String value = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.formatMillis(System.currentTimeMillis());
bulkRequest.add(
new IndexRequest(dataStream).opType(DocWriteRequest.OpType.CREATE)
.source(String.format(Locale.ROOT, "{\"%s\":\"%s\"}", DEFAULT_TIMESTAMP_FIELD, value), XContentType.JSON)
);
}
BulkResponse bulkResponse = client().bulk(bulkRequest).actionGet();
assertThat(bulkResponse.getItems().length, equalTo(numDocs));
String backingIndexPrefix = DataStream.BACKING_INDEX_PREFIX + dataStream;
for (BulkItemResponse itemResponse : bulkResponse) {
assertThat(itemResponse.getFailureMessage(), nullValue());
assertThat(itemResponse.status(), equalTo(RestStatus.CREATED));
assertThat(itemResponse.getIndex(), startsWith(backingIndexPrefix));
try (BulkRequest bulkRequest = new BulkRequest()) {
for (int i = 0; i < numDocs; i++) {
String value = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.formatMillis(System.currentTimeMillis());
bulkRequest.add(
new IndexRequest(dataStream).opType(DocWriteRequest.OpType.CREATE)
.source(String.format(Locale.ROOT, "{\"%s\":\"%s\"}", DEFAULT_TIMESTAMP_FIELD, value), XContentType.JSON)
);
}
BulkResponse bulkResponse = client().bulk(bulkRequest).actionGet();
assertThat(bulkResponse.getItems().length, equalTo(numDocs));
String backingIndexPrefix = DataStream.BACKING_INDEX_PREFIX + dataStream;
for (BulkItemResponse itemResponse : bulkResponse) {
assertThat(itemResponse.getFailureMessage(), nullValue());
assertThat(itemResponse.status(), equalTo(RestStatus.CREATED));
assertThat(itemResponse.getIndex(), startsWith(backingIndexPrefix));
}
}
indicesAdmin().refresh(new RefreshRequest(dataStream)).actionGet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ public void setupSuiteScopeCluster() throws Exception {
indexRandom(true, builders);

prepareCreate("idx2").setMapping("ul_field", "type=long").setSettings(settings).get();
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
for (int i = 0; i < 4; i++) {
IndexRequest indexRequest = new IndexRequest("idx2").source("ul_field", values[i]);
bulkRequestBuilder.add(indexRequest);
try (BulkRequestBuilder bulkRequestBuilder = client().prepareBulk()) {
bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
for (int i = 0; i < 4; i++) {
IndexRequest indexRequest = new IndexRequest("idx2").source("ul_field", values[i]);
bulkRequestBuilder.add(indexRequest);
}
bulkRequestBuilder.get();
}
bulkRequestBuilder.get();

ensureSearchable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ void persist(String indexOrAlias, BytesReference bytes) throws IOException {
String msg = "failed indexing updated state docs";
LOGGER.error(() -> format("[%s] %s", jobId, msg), ex);
auditor.error(jobId, msg + " error: " + ex.getMessage());
} finally {
bulkRequest.close();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,6 @@ private void indexInvalidation(
logger.warn("No [{}] tokens provided for invalidation", srcPrefix);
listener.onFailure(invalidGrantException("No tokens provided for invalidation"));
} else {
if (true) System.out.println("**** client class: " + client.getClass() + ", " + client.prepareBulk());
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
for (String tokenId : tokenIds) {
UpdateRequest request = client.prepareUpdate(tokensIndexManager.aliasName(), getTokenDocumentId(tokenId))
Expand All @@ -945,8 +944,13 @@ private void indexInvalidation(
)
);
bulkRequestBuilder.setRefreshPolicy(refreshPolicy);
tokensIndexManager.prepareIndexIfNeededThenExecute(
ex -> listener.onFailure(traceLog("prepare index [" + tokensIndexManager.aliasName() + "]", ex)),
tokensIndexManager.prepareIndexIfNeededThenExecute(ex -> {
try {
listener.onFailure(traceLog("prepare index [" + tokensIndexManager.aliasName() + "]", ex));
} finally {
bulkRequestBuilder.close();
}
},
() -> executeAsyncWithOrigin(
client.threadPool().getThreadContext(),
SECURITY_ORIGIN,
Expand Down

0 comments on commit 3bfe9a5

Please sign in to comment.