Skip to content

Commit 54b2c8a

Browse files
authored
Add validation to BulkIngester settings (#602)
1 parent bf459ff commit 54b2c8a

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

java-client/src/main/java/co/elastic/clients/elasticsearch/_helpers/bulk/BulkIngester.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,36 +426,56 @@ public Builder<Context> client(ElasticsearchClient client) {
426426
/**
427427
* Sets when to flush a new bulk request based on the number of operations currently added. Defaults to
428428
* {@code 1000}. Can be set to {@code -1} to disable it.
429+
*
430+
* @throws IllegalArgumentException if less than -1.
429431
*/
430432
public Builder<Context> maxOperations(int count) {
433+
if (count < -1) {
434+
throw new IllegalArgumentException("Max operations should be at least -1");
435+
}
431436
this.bulkOperations = count;
432437
return this;
433438
}
434439

435440
/**
436441
* Sets when to flush a new bulk request based on the size in bytes of actions currently added. A request is sent
437442
* once that size has been exceeded. Defaults to 5 megabytes. Can be set to {@code -1} to disable it.
443+
*
444+
* @throws IllegalArgumentException if less than -1.
438445
*/
439446
public Builder<Context> maxSize(long bytes) {
447+
if (bytes < -1) {
448+
throw new IllegalArgumentException("Max size should be at least -1");
449+
}
440450
this.bulkSize = bytes;
441451
return this;
442452
}
443453

444454
/**
445-
* Sets the number of concurrent requests allowed to be executed. A value of 1 means 1 concurrent request is allowed to be executed
455+
* Sets the number of concurrent requests allowed to be executed. A value of 1 means 1 request is allowed to be executed
446456
* while accumulating new bulk requests. Defaults to {@code 1}.
457+
*
458+
* @throws IllegalArgumentException if less than 1.
447459
*/
448460
public Builder<Context> maxConcurrentRequests(int max) {
461+
if (max < 1) {
462+
throw new IllegalArgumentException("Max concurrent request should be at least 1");
463+
}
449464
this.maxConcurrentRequests = max;
450465
return this;
451466
}
452467

453468
/**
454469
* Sets an interval flushing any bulk actions pending if the interval passes. Defaults to not set.
455470
* <p>
456-
* Flushing is still subject to the maximum number of requests set with {@link #maxConcurrentRequests}.
471+
* Flushing is still subject to the maximum number of requests set with {@link #maxConcurrentRequests}.
472+
*
473+
* @throws IllegalArgumentException if not a positive duration.
457474
*/
458475
public Builder<Context> flushInterval(long value, TimeUnit unit) {
476+
if (value < 0) {
477+
throw new IllegalArgumentException("Duration should be positive");
478+
}
459479
this.flushIntervalMillis = unit.toMillis(value);
460480
return this;
461481
}
@@ -497,6 +517,13 @@ public Builder<Context> globalSettings(Function<BulkRequest.Builder, BulkRequest
497517

498518
@Override
499519
public BulkIngester<Context> build() {
520+
// Ensure some chunking criteria are defined
521+
boolean hasCriteria = this.bulkOperations >= 0 || this.bulkSize >= 0 || this.flushIntervalMillis != null;
522+
523+
if (!hasCriteria) {
524+
throw new IllegalStateException("No bulk operation chunking criteria have been set.");
525+
}
526+
500527
return new BulkIngester<>(this);
501528
}
502529
}

java-client/src/test/java/co/elastic/clients/elasticsearch/_helpers/bulk/BulkIngesterTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,32 @@ public void endToEndTest() throws Exception {
433433

434434
}
435435

436+
@Test
437+
public void testConfigValidation() {
438+
439+
BulkIngester.Builder<Void> b = new BulkIngester.Builder<>();
440+
441+
assertThrows(IllegalArgumentException.class, () -> {
442+
b.flushInterval(-1, TimeUnit.MILLISECONDS);
443+
});
444+
445+
assertThrows(IllegalArgumentException.class, () -> {
446+
b.maxConcurrentRequests(0);
447+
});
448+
449+
assertThrows(IllegalArgumentException.class, () -> {
450+
b.maxSize(-2);
451+
});
452+
453+
assertThrows(IllegalArgumentException.class, () -> {
454+
b.maxOperations(-2);
455+
});
456+
457+
assertThrows(IllegalStateException.class, () -> {
458+
b.maxSize(-1).maxOperations(-1).build();
459+
});
460+
}
461+
436462
//-----------------------------------------------------------------------------------------------------------------
437463

438464
private static class CountingListener implements BulkListener<Void> {

0 commit comments

Comments
 (0)