Created
February 24, 2012 21:16
-
-
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.
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
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(); | |
} | |
} |
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
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