use of org.apache.commons.lang.mutable.MutableBoolean in project hadoop by apache.
the class TestEnhancedByteBufferAccess method waitForReplicaAnchorStatus.
private void waitForReplicaAnchorStatus(final ShortCircuitCache cache, final ExtendedBlock block, final boolean expectedIsAnchorable, final boolean expectedIsAnchored, final int expectedOutstandingMmaps) throws Exception {
GenericTestUtils.waitFor(new Supplier<Boolean>() {
@Override
public Boolean get() {
final MutableBoolean result = new MutableBoolean(false);
cache.accept(new CacheVisitor() {
@Override
public void visit(int numOutstandingMmaps, Map<ExtendedBlockId, ShortCircuitReplica> replicas, Map<ExtendedBlockId, InvalidToken> failedLoads, LinkedMap evictable, LinkedMap evictableMmapped) {
Assert.assertEquals(expectedOutstandingMmaps, numOutstandingMmaps);
ShortCircuitReplica replica = replicas.get(ExtendedBlockId.fromExtendedBlock(block));
Assert.assertNotNull(replica);
Slot slot = replica.getSlot();
if ((expectedIsAnchorable != slot.isAnchorable()) || (expectedIsAnchored != slot.isAnchored())) {
LOG.info("replica " + replica + " has isAnchorable = " + slot.isAnchorable() + ", isAnchored = " + slot.isAnchored() + ". Waiting for isAnchorable = " + expectedIsAnchorable + ", isAnchored = " + expectedIsAnchored);
return;
}
result.setValue(true);
}
});
return result.toBoolean();
}
}, 10, 60000);
}
use of org.apache.commons.lang.mutable.MutableBoolean in project hadoop by apache.
the class TestShortCircuitCache method testExpiry.
@Test(timeout = 100000)
public void testExpiry() throws Exception {
final ShortCircuitCache cache = new ShortCircuitCache(2, 1, 1, 10000000, 1, 10000000, 0);
final TestFileDescriptorPair pair = new TestFileDescriptorPair();
ShortCircuitReplicaInfo replicaInfo1 = cache.fetchOrCreate(new ExtendedBlockId(123, "test_bp1"), new SimpleReplicaCreator(123, cache, pair));
Preconditions.checkNotNull(replicaInfo1.getReplica());
Preconditions.checkState(replicaInfo1.getInvalidTokenException() == null);
pair.compareWith(replicaInfo1.getReplica().getDataStream(), replicaInfo1.getReplica().getMetaStream());
replicaInfo1.getReplica().unref();
final MutableBoolean triedToCreate = new MutableBoolean(false);
do {
Thread.sleep(10);
ShortCircuitReplicaInfo replicaInfo2 = cache.fetchOrCreate(new ExtendedBlockId(123, "test_bp1"), new ShortCircuitReplicaCreator() {
@Override
public ShortCircuitReplicaInfo createShortCircuitReplicaInfo() {
triedToCreate.setValue(true);
return null;
}
});
if ((replicaInfo2 != null) && (replicaInfo2.getReplica() != null)) {
replicaInfo2.getReplica().unref();
}
} while (triedToCreate.isFalse());
cache.close();
}
use of org.apache.commons.lang.mutable.MutableBoolean in project voldemort by voldemort.
the class VersionedPutPruningTest method testOnlineBehaviorWithConflicts.
@Test
public void testOnlineBehaviorWithConflicts() {
long now = System.currentTimeMillis();
// let's assume previous replicas are [4, 5, 0]. Note the intersection
// between old and new replicas
VectorClock fetchedClock1 = TestUtils.getVersionedPutClock(now, 4, 4, 5, 0);
VectorClock fetchedClock2 = TestUtils.getVersionedPutClock(now, 5, 4, 5, 0);
// to tighten the screws, assume the fetch delay was 0ms and two
// conflicting writes followed on the same ms
VectorClock onlineClock1 = TestUtils.getVersionedPutClock(now, 0, 0, 2, 1);
VectorClock onlineClock2 = TestUtils.getVersionedPutClock(now, 2, 0, 2, 1);
assertEquals("fetched versions themselves should conflict", Occurred.CONCURRENTLY, VectorClockUtils.compare(fetchedClock1, fetchedClock2));
assertEquals("online versions themselves should conflict", Occurred.CONCURRENTLY, VectorClockUtils.compare(onlineClock1, onlineClock2));
assertEquals("fetched and online versions should conflict", Occurred.CONCURRENTLY, VectorClockUtils.compare(fetchedClock1, onlineClock1));
assertEquals("fetched and online versions should conflict", Occurred.CONCURRENTLY, VectorClockUtils.compare(fetchedClock1, onlineClock1));
assertEquals("fetched and online versions should conflict", Occurred.CONCURRENTLY, VectorClockUtils.compare(fetchedClock1, onlineClock2));
assertEquals("fetched and online versions should conflict", Occurred.CONCURRENTLY, VectorClockUtils.compare(fetchedClock2, onlineClock1));
assertEquals("fetched and online versions should conflict", Occurred.CONCURRENTLY, VectorClockUtils.compare(fetchedClock2, onlineClock2));
// case where key has received writes before the prune job
List<Versioned<byte[]>> vals = new ArrayList<Versioned<byte[]>>();
vals.add(new Versioned<byte[]>(key, fetchedClock1));
vals.add(new Versioned<byte[]>(key, onlineClock1));
vals.add(new Versioned<byte[]>(key, fetchedClock2));
vals.add(new Versioned<byte[]>(key, onlineClock2));
MutableBoolean didPrune = new MutableBoolean();
vals = pruneAndResolve(vals, didPrune);
assertEquals("Must have pruned something", true, didPrune.booleanValue());
assertEquals("Must have two winning versions", 2, vals.size());
assertEquals("Must have onlineClock1", onlineClock1, vals.get(0).getVersion());
assertEquals("Must have onlineClock2", onlineClock2, vals.get(1).getVersion());
// case where key has not received any writes before the prune job
vals = new ArrayList<Versioned<byte[]>>();
vals.add(new Versioned<byte[]>(key, fetchedClock1));
vals.add(new Versioned<byte[]>(key, fetchedClock2));
didPrune = new MutableBoolean();
vals = pruneAndResolve(vals, didPrune);
assertEquals("Must have pruned something", true, didPrune.booleanValue());
// Note that since 0 is not a master in both fetched clocks, there will
// be one version. If 0 were to be a master, there will be one version,
// since master clock will trump non-master clock
assertEquals("Must have one winning version", 1, vals.size());
assertEquals("Must resolve to [0:ts] clock", TestUtils.getVersionedPutClock(now, -1, 0), vals.get(0).getVersion());
VectorClock nextOnlineClock1 = TestUtils.getVersionedPutClock(now, 0, 0, 2, 1);
VectorClock nextOnlineClock2 = TestUtils.getVersionedPutClock(now, 2, 0, 2, 1);
assertFalse("Next online write would not result in conflict", Occurred.CONCURRENTLY == VectorClockUtils.compare((VectorClock) vals.get(0).getVersion(), nextOnlineClock1));
assertFalse("Next online write would not result in conflict", Occurred.CONCURRENTLY == VectorClockUtils.compare((VectorClock) vals.get(0).getVersion(), nextOnlineClock2));
}
use of org.apache.commons.lang.mutable.MutableBoolean in project voldemort by voldemort.
the class VersionedPutPruningTest method testOutOfSyncPruning.
@Test
public void testOutOfSyncPruning() {
long now = System.currentTimeMillis();
byte[] onlineVal = "online".getBytes();
ChainedResolver<Versioned<byte[]>> resolver = new ChainedResolver<Versioned<byte[]>>(new VectorClockInconsistencyResolver<byte[]>(), new TimeBasedInconsistencyResolver<byte[]>());
// let's assume previous replicas are [4, 5, 0]
VectorClock fetchedClock = TestUtils.getVersionedPutClock(now, 4, 4, 5, 0);
VectorClock onlineClock1 = TestUtils.getVersionedPutClock(now + Time.MS_PER_SECOND, 0, 0, 2, 1);
// Both the servers have the two versions.
List<Versioned<byte[]>> vals1 = new ArrayList<Versioned<byte[]>>();
vals1.add(new Versioned<byte[]>(key, fetchedClock));
vals1.add(new Versioned<byte[]>(onlineVal, onlineClock1));
List<Versioned<byte[]>> vals2 = new ArrayList<Versioned<byte[]>>();
vals2.add(new Versioned<byte[]>(key, fetchedClock));
vals2.add(new Versioned<byte[]>(onlineVal, onlineClock1));
// job fixes server 1
vals1 = pruneAndResolve(vals1, new MutableBoolean());
assertEquals("Must have one winning version", 1, vals1.size());
assertEquals("Must resolve to onlineClock", onlineClock1, vals1.get(0).getVersion());
// reads only from pruned server
List<Versioned<byte[]>> resolvedVersions = resolver.resolveConflicts(vals1);
assertEquals("Must read out latest timestamp version", onlineClock1.getTimestamp(), ((VectorClock) resolvedVersions.get(0).getVersion()).getTimestamp());
assertEquals("Online value to be read out", new String(onlineVal), new String(resolvedVersions.get(0).getValue()));
// reads only from non pruned server
resolvedVersions = resolver.resolveConflicts(vals2);
// Note : check on timestamp is meaningless when actual merging is
// involved , since merging clocks will result in
// System.currentTimeMillis() being used for the merged vector clock
assertEquals("Online value to be read out", new String(onlineVal), new String(resolvedVersions.get(0).getValue()));
// reads combining both
List<Versioned<byte[]>> vals = new ArrayList<Versioned<byte[]>>();
vals.addAll(vals1);
vals.addAll(vals2);
resolvedVersions = resolver.resolveConflicts(vals);
assertEquals("Online value to be read out", new String(onlineVal), new String(resolvedVersions.get(0).getValue()));
}
use of org.apache.commons.lang.mutable.MutableBoolean in project midpoint by Evolveum.
the class LensUtil method consolidateTripleToDelta.
/**
* Consolidate the mappings of a single item to a delta. It takes the convenient structure of ItemValueWithOrigin triple.
* It produces the delta considering the mapping exclusion, authoritativeness and strength.
*
* filterExistingValues: if true, then values that already exist in the item are not added (and those that don't exist are not removed)
*/
@NotNull
public static <V extends PrismValue, D extends ItemDefinition, I extends ItemValueWithOrigin<V, D>> ItemDelta<V, D> consolidateTripleToDelta(ItemPath itemPath, DeltaSetTriple<I> triple, D itemDefinition, ItemDelta<V, D> aprioriItemDelta, PrismContainer<?> itemContainer, ValueMatcher<?> valueMatcher, Comparator<V> comparator, boolean addUnchangedValues, boolean filterExistingValues, boolean isExclusiveStrong, String contextDescription, boolean applyWeak) throws ExpressionEvaluationException, PolicyViolationException, SchemaException {
ItemDelta<V, D> itemDelta = itemDefinition.createEmptyDelta(itemPath);
Item<V, D> itemExisting = null;
if (itemContainer != null) {
itemExisting = itemContainer.findItem(itemPath);
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Consolidating {} triple:\n{}\nApriori Delta:\n{}\nExisting item:\n{}", itemPath, triple.debugDump(1), DebugUtil.debugDump(aprioriItemDelta, 1), DebugUtil.debugDump(itemExisting, 1));
}
Collection<V> allValues = collectAllValues(triple, valueMatcher);
final MutableBoolean itemHasStrongMutable = new MutableBoolean(false);
SimpleVisitor<I> visitor = pvwo -> {
if (pvwo.getMapping().getStrength() == MappingStrengthType.STRONG) {
itemHasStrongMutable.setValue(true);
}
};
triple.accept(visitor);
boolean ignoreNormalMappings = itemHasStrongMutable.booleanValue() && isExclusiveStrong;
// a single item (e.g. attribute). But this loop iterates over every potential value of that item.
for (V value : allValues) {
LOGGER.trace(" consolidating value: {}", value);
// Check what to do with the value using the usual "triple routine". It means that if a value is
// in zero set than we need no delta, plus set means add delta and minus set means delete delta.
// The first set that the value is present determines the result.
Collection<ItemValueWithOrigin<V, D>> zeroPvwos = collectPvwosFromSet(value, triple.getZeroSet(), valueMatcher);
Collection<ItemValueWithOrigin<V, D>> plusPvwos = collectPvwosFromSet(value, triple.getPlusSet(), valueMatcher);
Collection<ItemValueWithOrigin<V, D>> minusPvwos = collectPvwosFromSet(value, triple.getMinusSet(), valueMatcher);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("PVWOs for value {}:\nzero = {}\nplus = {}\nminus = {}", value, zeroPvwos, plusPvwos, minusPvwos);
}
boolean zeroHasStrong = false;
if (!zeroPvwos.isEmpty()) {
for (ItemValueWithOrigin<V, D> pvwo : zeroPvwos) {
PrismValueDeltaSetTripleProducer<V, D> mapping = pvwo.getMapping();
if (mapping.getStrength() == MappingStrengthType.STRONG) {
zeroHasStrong = true;
}
}
}
if (zeroHasStrong && aprioriItemDelta != null && aprioriItemDelta.isValueToDelete(value, true)) {
throw new PolicyViolationException("Attempt to delete value " + value + " from item " + itemPath + " but that value is mandated by a strong mapping (in " + contextDescription + ")");
}
if (!zeroPvwos.isEmpty() && !addUnchangedValues) {
// Value unchanged, nothing to do
LOGGER.trace("Value {} unchanged, doing nothing", value);
continue;
}
PrismValueDeltaSetTripleProducer<V, D> exclusiveMapping = null;
Collection<ItemValueWithOrigin<V, D>> pvwosToAdd = null;
if (addUnchangedValues) {
pvwosToAdd = MiscUtil.union(zeroPvwos, plusPvwos);
} else {
pvwosToAdd = plusPvwos;
}
if (!pvwosToAdd.isEmpty()) {
boolean weakOnly = true;
boolean hasStrong = false;
// exclusions and strength
for (ItemValueWithOrigin<V, D> pvwoToAdd : pvwosToAdd) {
PrismValueDeltaSetTripleProducer<V, D> mapping = pvwoToAdd.getMapping();
if (mapping.getStrength() != MappingStrengthType.WEAK) {
weakOnly = false;
}
if (mapping.getStrength() == MappingStrengthType.STRONG) {
hasStrong = true;
}
if (mapping.isExclusive()) {
if (exclusiveMapping == null) {
exclusiveMapping = mapping;
} else {
String message = "Exclusion conflict in " + contextDescription + ", item " + itemPath + ", conflicting constructions: " + exclusiveMapping + " and " + mapping;
LOGGER.error(message);
throw new ExpressionEvaluationException(message);
}
}
}
if (weakOnly) {
// Postpone processing of weak values until we process all other values
LOGGER.trace("Value {} mapping is weak in item {}, postponing processing in {}", value, itemPath, contextDescription);
continue;
}
if (!hasStrong && ignoreNormalMappings) {
LOGGER.trace("Value {} mapping is normal in item {} and we have exclusiveStrong, skipping processing in {}", value, itemPath, contextDescription);
continue;
}
if (hasStrong && aprioriItemDelta != null && aprioriItemDelta.isValueToDelete(value, true)) {
throw new PolicyViolationException("Attempt to delete value " + value + " from item " + itemPath + " but that value is mandated by a strong mapping (in " + contextDescription + ")");
}
if (!hasStrong && (aprioriItemDelta != null && !aprioriItemDelta.isEmpty())) {
// There is already a delta, skip this
LOGGER.trace("Value {} mapping is not strong and the item {} already has a delta that is more concrete, " + "skipping adding in {}", value, itemPath, contextDescription);
continue;
}
if (filterExistingValues && hasValue(itemExisting, value, valueMatcher, comparator)) {
LOGGER.trace("Value {} NOT added to delta for item {} because the item already has that value in {}", value, itemPath, contextDescription);
continue;
}
LOGGER.trace("Value {} added to delta as ADD for item {} in {}", value, itemPath, contextDescription);
itemDelta.addValueToAdd((V) value.clone());
continue;
}
// So check for that special case here to avoid removing them.
if (!minusPvwos.isEmpty() && plusPvwos.isEmpty()) {
boolean weakOnly = true;
boolean hasStrong = false;
boolean hasAuthoritative = false;
// exclusions and strength
for (ItemValueWithOrigin<V, D> pvwo : minusPvwos) {
PrismValueDeltaSetTripleProducer<V, D> mapping = pvwo.getMapping();
if (mapping.getStrength() != MappingStrengthType.WEAK) {
weakOnly = false;
}
if (mapping.getStrength() == MappingStrengthType.STRONG) {
hasStrong = true;
}
if (mapping.isAuthoritative()) {
hasAuthoritative = true;
}
}
if (!hasAuthoritative) {
LOGGER.trace("Value {} has no authoritative mapping for item {}, skipping deletion in {}", value, itemPath, contextDescription);
continue;
}
if (!hasStrong && (aprioriItemDelta != null && !aprioriItemDelta.isEmpty())) {
// There is already a delta, skip this
LOGGER.trace("Value {} mapping is not strong and the item {} already has a delta that is more concrete, skipping deletion in {}", value, itemPath, contextDescription);
continue;
}
if (weakOnly && (itemExisting != null && !itemExisting.isEmpty())) {
// There is already a value, skip this
LOGGER.trace("Value {} mapping is weak and the item {} already has a value, skipping deletion in {}", value, itemPath, contextDescription);
continue;
}
if (weakOnly && !applyWeak && (itemExisting == null || itemExisting.isEmpty())) {
// There is a weak mapping on a property, but we do not have full account available, so skipping deletion of the value is better way
LOGGER.trace("Value {} mapping is weak and the full account could not be fetched, skipping deletion in {}", value, itemPath, contextDescription);
continue;
}
if (filterExistingValues && !hasValue(itemExisting, value, valueMatcher, comparator)) {
LOGGER.trace("Value {} NOT add to delta as DELETE because item {} the item does not have that value in {} (matcher: {})", value, itemPath, contextDescription, valueMatcher);
continue;
}
LOGGER.trace("Value {} added to delta as DELETE for item {} in {}", value, itemPath, contextDescription);
itemDelta.addValueToDelete((V) value.clone());
}
if (!zeroPvwos.isEmpty()) {
boolean weakOnly = true;
boolean hasStrong = false;
boolean hasAuthoritative = false;
// exclusions and strength
for (ItemValueWithOrigin<V, D> pvwo : zeroPvwos) {
PrismValueDeltaSetTripleProducer<V, D> mapping = pvwo.getMapping();
if (mapping.getStrength() != MappingStrengthType.WEAK) {
weakOnly = false;
}
if (mapping.getStrength() == MappingStrengthType.STRONG) {
hasStrong = true;
}
if (mapping.isAuthoritative()) {
hasAuthoritative = true;
}
}
if (aprioriItemDelta != null && aprioriItemDelta.isReplace()) {
// Any strong mappings in the zero set needs to be re-applied as otherwise the replace will destroy it
if (hasStrong) {
LOGGER.trace("Value {} added to delta for item {} in {} because there is strong mapping in the zero set", value, itemPath, contextDescription);
itemDelta.addValueToAdd((V) value.clone());
continue;
}
}
}
}
Item<V, D> itemNew = null;
if (itemContainer != null) {
itemNew = itemContainer.findItem(itemPath);
}
if (!hasValue(itemNew, itemDelta)) {
// The application of computed delta results in no value, apply weak mappings
Collection<? extends ItemValueWithOrigin<V, D>> nonNegativePvwos = triple.getNonNegativeValues();
Collection<V> valuesToAdd = addWeakValues(nonNegativePvwos, OriginType.ASSIGNMENTS, applyWeak);
if (valuesToAdd.isEmpty()) {
valuesToAdd = addWeakValues(nonNegativePvwos, OriginType.OUTBOUND, applyWeak);
}
if (valuesToAdd.isEmpty()) {
valuesToAdd = addWeakValues(nonNegativePvwos, null, applyWeak);
}
LOGGER.trace("No value for item {} in {}, weak mapping processing yielded values: {}", itemPath, contextDescription, valuesToAdd);
itemDelta.addValuesToAdd(valuesToAdd);
} else {
LOGGER.trace("Existing values for item {} in {}, weak mapping processing skipped", new Object[] { itemPath, contextDescription });
}
if (itemExisting != null) {
List<V> existingValues = itemExisting.getValues();
if (existingValues != null) {
itemDelta.setEstimatedOldValues(PrismValue.cloneCollection(existingValues));
}
}
return itemDelta;
}
Aggregations