Skip to content

Commit 5d808ef

Browse files
committed
Add validation to BulkIngester settings (#602)
1 parent e74f5c3 commit 5d808ef

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
@@ -410,6 +410,32 @@ public void endToEndTest() throws Exception {
410410

411411
}
412412

413+
@Test
414+
public void testConfigValidation() {
415+
416+
BulkIngester.Builder<Void> b = new BulkIngester.Builder<>();
417+
418+
assertThrows(IllegalArgumentException.class, () -> {
419+
b.flushInterval(-1, TimeUnit.MILLISECONDS);
420+
});
421+
422+
assertThrows(IllegalArgumentException.class, () -> {
423+
b.maxConcurrentRequests(0);
424+
});
425+
426+
assertThrows(IllegalArgumentException.class, () -> {
427+
b.maxSize(-2);
428+
});
429+
430+
assertThrows(IllegalArgumentException.class, () -> {
431+
b.maxOperations(-2);
432+
});
433+
434+
assertThrows(IllegalStateException.class, () -> {
435+
b.maxSize(-1).maxOperations(-1).build();
436+
});
437+
}
438+
413439
//-----------------------------------------------------------------------------------------------------------------
414440

415441
private static class CountingListener implements BulkListener<Void> {

0 commit comments

Comments
 (0)