Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seiyak/1903794 to your computer and use it in GitHub Desktop.
Save seiyak/1903794 to your computer and use it in GitHub Desktop.
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