Skip to content

Commit f104abb

Browse files
Fix race condition in nextInsertBatch() causing off-by-1 doc count
has_next_create() and next() were separate non-atomic operations, allowing two threads to both pass the boundary check at create_e and produce one extra document beyond the intended range. Fix: atomically claim the index with getAndIncrement() first, then break if the claimed index is out of bounds — eliminating the check-then-act gap. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 94ef7e5 commit f104abb

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/main/java/utils/docgen/DocumentGenerator.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,22 @@ public Tuple2<String, Object> nextExpiry() {
464464
public List<Tuple2<String, Object>> nextInsertBatch() {
465465
List<Tuple2<String, Object>> docs = new ArrayList<Tuple2<String,Object>>();
466466
int count = 0;
467-
while (this.has_next_create() && count<ws.batchSize*ws.creates/100) {
468-
docs.add(this.next());
467+
while (count < ws.batchSize * ws.creates / 100) {
468+
long idx = this.ws.dr.createItr.getAndIncrement();
469+
if (idx >= this.ws.dr.create_e) break;
470+
String k = null;
471+
Object v = null;
472+
try {
473+
if (this.target_vbuckets != null && this.target_vbuckets.length > 0) {
474+
k = this.generated_create_keys.get(idx);
475+
} else {
476+
k = (String) this.keyMethod.invoke(this.keys, idx);
477+
}
478+
v = (Object) this.valMethod.invoke(this.vals, k);
479+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
480+
e1.printStackTrace();
481+
}
482+
docs.add(Tuples.of(k, v));
469483
count += 1;
470484
}
471485
return docs;

0 commit comments

Comments
 (0)