Skip to content

Commit

Permalink
Caching constraint violation checking results (focus and shadow). Mov…
Browse files Browse the repository at this point in the history
…ed shadow constraint checking to provisioning.
  • Loading branch information
mederly committed Dec 4, 2014
1 parent 6c0af44 commit 3a00eea
Show file tree
Hide file tree
Showing 20 changed files with 817 additions and 141 deletions.
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2010-2014 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.evolveum.midpoint.util.caching;

import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

/**
* @author mederly
*/
public class AbstractCache {

private int entryCount = 0;

public static <T extends AbstractCache> void enter(ThreadLocal<T> cacheThreadLocal, Class<T> cacheClass, Trace logger) {
T inst = cacheThreadLocal.get();
logger.trace("Cache: ENTER for thread {}, {}", Thread.currentThread().getName(), inst);
if (inst == null) {
logger.info("Cache: creating for thread {}",Thread.currentThread().getName());
try {
inst = cacheClass.newInstance();
} catch (InstantiationException|IllegalAccessException e) {
throw new SystemException("Couldn't instantiate cache: " + e.getMessage(), e);
}
cacheThreadLocal.set(inst);
}
inst.incrementEntryCount();
}

public static <T extends AbstractCache> void exit(ThreadLocal<T> cacheThreadLocal, Trace logger) {
T inst = cacheThreadLocal.get();
logger.trace("Cache: EXIT for thread {}, {}", Thread.currentThread().getName(), inst);
if (inst == null || inst.getEntryCount() == 0) {
logger.error("Cache: Attempt to exit cache that does not exist or has entry count 0: {}", inst);
cacheThreadLocal.set(null);
} else {
inst.decrementEntryCount();
if (inst.getEntryCount() <= 0) {
logger.info("Cache: DESTROY for thread {}", Thread.currentThread().getName());
cacheThreadLocal.set(null);
}
}
}

public void incrementEntryCount() {
entryCount++;
}

public void decrementEntryCount() {
entryCount--;
}

public int getEntryCount() {
return entryCount;
}

}
Expand Up @@ -30,6 +30,7 @@
import com.evolveum.midpoint.model.common.expression.ExpressionEvaluationContext;
import com.evolveum.midpoint.model.common.expression.ExpressionFactory;
import com.evolveum.midpoint.model.common.expression.ExpressionVariables;
import com.evolveum.midpoint.model.impl.lens.projector.FocusConstraintsChecker;
import com.evolveum.midpoint.model.impl.util.Utils;
import com.evolveum.midpoint.prism.ConsistencyCheckScope;
import com.evolveum.midpoint.prism.PrismContext;
Expand Down Expand Up @@ -928,6 +929,8 @@ private <T extends ObjectType, F extends ObjectType> void executeAddition(Object
}
result.addReturn("createdAccountOid", oid);
} else {
FocusConstraintsChecker.clearCacheFor(objectToAdd.asObjectable().getName());

RepoAddOptions addOpt = new RepoAddOptions();
if (ModelExecuteOptions.isOverwrite(options)){
addOpt.setOverwrite(true);
Expand Down Expand Up @@ -1016,6 +1019,7 @@ private <T extends ObjectType, F extends ObjectType> void executeModification(Ob
change.setOid(oid);
}
} else {
FocusConstraintsChecker.clearCacheForDelta(change.getModifications());
cacheRepositoryService.modifyObject(objectTypeClass, change.getOid(), change.getModifications(), result);
}
}
Expand Down
Expand Up @@ -32,6 +32,9 @@
import com.evolveum.midpoint.model.api.hooks.ChangeHook;
import com.evolveum.midpoint.model.api.hooks.HookOperationMode;
import com.evolveum.midpoint.model.api.hooks.HookRegistry;
import com.evolveum.midpoint.model.impl.lens.projector.FocusConstraintsChecker;
import com.evolveum.midpoint.model.impl.lens.projector.ShadowConstraintsChecker;
import com.evolveum.midpoint.provisioning.api.ProvisioningService;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.constants.SchemaConstants;

Expand Down Expand Up @@ -132,6 +135,9 @@ public class Clockwork {
@Autowired(required = true)
@Qualifier("cacheRepositoryService")
private transient RepositoryService repositoryService;

@Autowired
private transient ProvisioningService provisioningService;

@Autowired(required = true)
private ScriptExpressionFactory scriptExpressionFactory;
Expand All @@ -157,26 +163,33 @@ public <F extends ObjectType> HookOperationMode run(LensContext<F> context, Task
}

int clicked = 0;

while (context.getState() != ModelState.FINAL) {

// TODO implement in model context (as transient or even non-transient attribute) to allow for checking in more complex scenarios
if (clicked >= MAX_CLICKS) {
throw new IllegalStateException("Model operation took too many clicks (limit is " + MAX_CLICKS + "). Is there a cycle?");
}
clicked++;
try {
FocusConstraintsChecker.enterCache();
provisioningService.enterConstraintsCheckerCache();
while (context.getState() != ModelState.FINAL) {

HookOperationMode mode = click(context, task, result);
// TODO implement in model context (as transient or even non-transient attribute) to allow for checking in more complex scenarios
if (clicked >= MAX_CLICKS) {
throw new IllegalStateException("Model operation took too many clicks (limit is " + MAX_CLICKS + "). Is there a cycle?");
}
clicked++;

if (mode == HookOperationMode.BACKGROUND) {
result.recordInProgress();
return mode;
} else if (mode == HookOperationMode.ERROR) {
return mode;
}
HookOperationMode mode = click(context, task, result);

if (mode == HookOperationMode.BACKGROUND) {
result.recordInProgress();
return mode;
} else if (mode == HookOperationMode.ERROR) {
return mode;
}
}
// One last click in FINAL state
return click(context, task, result);
} finally {
FocusConstraintsChecker.exitCache();
provisioningService.exitConstraintsCheckerCache();
}
// One last click in FINAL state
return click(context, task, result);
}

public <F extends ObjectType> HookOperationMode click(LensContext<F> context, Task task, OperationResult result) throws SchemaException, PolicyViolationException, ExpressionEvaluationException, ObjectNotFoundException, ObjectAlreadyExistsException, CommunicationException, ConfigurationException, SecurityViolationException {
Expand Down
Expand Up @@ -15,28 +15,38 @@
*/
package com.evolveum.midpoint.model.impl.lens.projector;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.evolveum.midpoint.model.impl.lens.LensContext;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.query.EqualFilter;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.caching.AbstractCache;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;

/**
* @author semancik
*
*/
public class FocusConstraintsChecker<F extends FocusType> {

private static ThreadLocal<Cache> cacheThreadLocal = new ThreadLocal<>();

private static final Trace LOGGER = TraceManager.getTrace(FocusConstraintsChecker.class);

Expand Down Expand Up @@ -94,8 +104,16 @@ public void check(PrismObject<F> objectNew, OperationResult result) throws Schem
return;
}

// Hardcode to name ... for now
satisfiesConstraints = checkPropertyUniqueness(objectNew, new ItemPath(ObjectType.F_NAME), context, result);
// Hardcode to name ... for now
PolyStringType name = objectNew.asObjectable().getName();
if (Cache.isOk(name)) {
satisfiesConstraints = true;
} else {
satisfiesConstraints = checkPropertyUniqueness(objectNew, new ItemPath(ObjectType.F_NAME), context, result);
if (satisfiesConstraints) {
Cache.setOk(name);
}
}
}

private <T> boolean checkPropertyUniqueness(PrismObject<F> objectNew, ItemPath propPath, LensContext<F> context, OperationResult result) throws SchemaException {
Expand Down Expand Up @@ -146,4 +164,86 @@ private void message(String message) {
messageBuilder.append(message);
}

public static void enterCache() {
Cache.enter(cacheThreadLocal, Cache.class, LOGGER);
}

public static void exitCache() {
Cache.exit(cacheThreadLocal, LOGGER);
}

public static <T extends ObjectType> void clearCacheFor(PolyStringType name) {
Cache.remove(name);
}

public static void clearCacheForValues(Collection<? extends PrismValue> values) {
if (values == null) {
return;
}
for (PrismValue value : values) {
if (value instanceof PrismPropertyValue) {
Object real = ((PrismPropertyValue) value).getValue();
if (real instanceof PolyStringType) {
clearCacheFor((PolyStringType) real);
}
}
}
}

public static void clearCacheForDelta(Collection<? extends ItemDelta> modifications) {
if (modifications == null) {
return;
}
for (ItemDelta itemDelta : modifications) {
if (new ItemPath(ObjectType.F_NAME).equivalent(itemDelta.getPath())) {
clearCacheForValues(itemDelta.getValuesToAdd()); // these may present a conflict
clearCacheForValues(itemDelta.getValuesToReplace()); // so do these
}
}
}

public static class Cache extends AbstractCache {

private Set<String> conflictFreeNames = new HashSet<>();

public static boolean isOk(PolyStringType name) {
if (name == null) {
LOGGER.info("Null name");
return false; // strange case
}

Cache cache = getCache();
if (cache == null) {
LOGGER.info("Cache NULL for {}", name);
return false;
}

if (cache.conflictFreeNames.contains(name.getOrig())) {
LOGGER.info("Cache HIT for {}", name);
return true;
} else {
LOGGER.info("Cache MISS for {}", name);
return false;
}
}

public static void setOk(PolyStringType name) {
Cache cache = getCache();
if (name != null && cache != null) {
cache.conflictFreeNames.add(name.getOrig());
}
}

private static Cache getCache() {
return cacheThreadLocal.get();
}

public static void remove(PolyStringType name) {
Cache cache = getCache();
if (name != null && cache != null) {
LOGGER.info("Cache REMOVE for {}", name);
cache.conflictFreeNames.remove(name.getOrig());
}
}
}
}

0 comments on commit 3a00eea

Please sign in to comment.