Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The original implementation for VoldemortDatastoreProvider.setNextValue() totally blocks concurrency because of the exclusive lock. The second version allows concurrency but I'm not sure if it's the right way to calculate next values.
public void setNextValue(RowKey key, IntegralDataTypeHolder value, int increment, int initialValue) {
this.lock.lock();
try {
Versioned v = this.getValue(this.getVoldemortSequecenStoreName(),
this.toJSON(key.getRowKeyAsMap()), true);
if (v == null) {
Map<String, Integer> nextSequence = new HashMap<String, Integer>();
nextSequence.put("nextSequence", initialValue);
this.putValue(this.getVoldemortSequecenStoreName(),
this.toJSON(key.getRowKeyAsMap()), nextSequence, true,
false);
value.initialize(initialValue);
return;
}
int nextValue = (Integer) ((Map<String, Integer>) v.getValue())
.get("nextSequence") + increment;
value.initialize(nextValue);
Map<String, Integer> nextSequence = new HashMap<String, Integer>();
nextSequence.put("nextSequence",
((Map<String, Integer>) v.getValue()).get("nextSequence")
+ increment);
this.putValue(this.getVoldemortSequecenStoreName(),
this.toJSON(key.getRowKeyAsMap()), nextSequence, true,
false);
} finally {
this.lock.unlock();
}
}
public void setNextValue(RowKey key, IntegralDataTypeHolder value,
int increment, int initialValue) {
List<Integer> l = new LinkedList<Integer>();
l.add(initialValue);
if (this.nextValues.putIfAbsent(key, l) == null) {
Map<String, Integer> nextSequence = new HashMap<String, Integer>();
nextSequence.put(VoldemortDatastoreProvider.SEQUENCE_LABEL,
initialValue);
value.initialize(initialValue);
if (this.flushToDb) {
// this.taskQueue.offer(new PutSequenceRunnable(this, this
// .getVoldemortSequenceStoreName(), this.toJSON(key
// .getRowKeyAsMap()), nextSequence, true, false));
this.putValue(this.getVoldemortSequenceStoreName(),
this.toJSON(key.getRowKeyAsMap()), nextSequence, true,
false);
}
} else {
List<Integer> list = this.nextValues.get(key);
synchronized (list) {
boolean notContained = false;
int candidate = list.get(list.size() - 1) + increment;
if (!list.contains(candidate)) {
this.nextValues.get(key).add(candidate);
notContained = true;
}
value.initialize(candidate);
Map<String, Integer> nextSequence = new HashMap<String, Integer>();
nextSequence.put(VoldemortDatastoreProvider.SEQUENCE_LABEL,
candidate);
if (this.flushToDb) {
if (notContained) {
this.putValue(this.getVoldemortSequenceStoreName(),
this.toJSON(key.getRowKeyAsMap()),
nextSequence, true, false);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.