-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from tlopatic/insert-fix
Proposed fix for #327.
- Loading branch information
Showing
2 changed files
with
82 additions
and
5 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
core/src/main/java/com/yahoo/ycsb/generator/AcknowledgedCounterGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.yahoo.ycsb.generator; | ||
|
||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* A CounterGenerator that reports generated integers via lastInt() | ||
* only after they have been acknowledged. | ||
*/ | ||
public class AcknowledgedCounterGenerator extends CounterGenerator | ||
{ | ||
private static final int WINDOW_SIZE = 1000000; | ||
|
||
private final ReentrantLock lock; | ||
private final boolean[] window; | ||
private volatile int limit; | ||
|
||
/** | ||
* Create a counter that starts at countstart. | ||
*/ | ||
public AcknowledgedCounterGenerator(int countstart) | ||
{ | ||
super(countstart); | ||
lock = new ReentrantLock(); | ||
window = new boolean[WINDOW_SIZE]; | ||
limit = countstart - 1; | ||
} | ||
|
||
/** | ||
* In this generator, the highest acknowledged counter value | ||
* (as opposed to the highest generated counter value). | ||
*/ | ||
@Override | ||
public int lastInt() | ||
{ | ||
return limit; | ||
} | ||
|
||
/** | ||
* Make a generated counter value available via lastInt(). | ||
*/ | ||
public void acknowledge(int value) | ||
{ | ||
if (value > limit + WINDOW_SIZE) { | ||
throw new RuntimeException("Too many unacknowledged insertion keys."); | ||
} | ||
|
||
window[value % WINDOW_SIZE] = true; | ||
|
||
if (lock.tryLock()) { | ||
// move a contiguous sequence from the window | ||
// over to the "limit" variable | ||
|
||
try { | ||
int index; | ||
|
||
for (index = limit + 1; index <= value; ++index) { | ||
int slot = index % WINDOW_SIZE; | ||
|
||
if (!window[slot]) { | ||
break; | ||
} | ||
|
||
window[slot] = false; | ||
} | ||
|
||
limit = index - 1; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters