Skip to content

Commit

Permalink
Implemented MID-3072 "reindex task".
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Jun 28, 2016
1 parent 5ef866c commit a87be8e
Show file tree
Hide file tree
Showing 16 changed files with 318 additions and 114 deletions.
Expand Up @@ -142,6 +142,7 @@ <h3 class="box-title"><wicket:message key="PageAbout.title.jvmProperties"/></h3>
<div class="main-button-bar">
<a class="btn btn-default" wicket:id="testRepository"/>
<a class="btn btn-default" wicket:id="testRepositoryCheckOrgClosure"/>
<a class="btn btn-default" wicket:id="reindexRepositoryObjects"/>
<a class="btn btn-default" wicket:id="testProvisioning"/>
</div>

Expand Down
Expand Up @@ -18,12 +18,16 @@

import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.model.api.ModelPublicConstants;
import com.evolveum.midpoint.schema.LabeledString;
import com.evolveum.midpoint.schema.ProvisioningDiag;
import com.evolveum.midpoint.schema.RepositoryDiag;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskManager;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.util.logging.LoggingUtils;
Expand All @@ -32,9 +36,10 @@
import com.evolveum.midpoint.web.application.AuthorizationAction;
import com.evolveum.midpoint.web.application.PageDescriptor;
import com.evolveum.midpoint.web.component.AjaxButton;
import com.evolveum.midpoint.web.page.admin.home.PageAdminHome;

import com.evolveum.midpoint.web.page.login.PageLogin;
import com.evolveum.midpoint.web.security.SecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
Expand Down Expand Up @@ -64,6 +69,7 @@ public class PageAbout extends PageAdminConfiguration {
private static final String OPERATION_TEST_REPOSITORY = DOT_CLASS + "testRepository";
private static final String OPERATION_TEST_REPOSITORY_CHECK_ORG_CLOSURE = DOT_CLASS + "testRepositoryCheckOrgClosure";
private static final String OPERATION_GET_REPO_DIAG = DOT_CLASS + "getRepoDiag";
private static final String OPERATION_SUBMIT_REINDEX = DOT_CLASS + "submitReindex";
private static final String OPERATION_GET_PROVISIONING_DIAG = DOT_CLASS + "getProvisioningDiag";

private static final String ID_BUILD = "build";
Expand All @@ -73,6 +79,7 @@ public class PageAbout extends PageAdminConfiguration {
private static final String ID_LIST_SYSTEM_ITEMS = "listSystemItems";
private static final String ID_TEST_REPOSITORY = "testRepository";
private static final String ID_TEST_REPOSITORY_CHECK_ORG_CLOSURE = "testRepositoryCheckOrgClosure";
private static final String ID_REINDEX_REPOSITORY_OBJECTS = "reindexRepositoryObjects";
private static final String ID_TEST_PROVISIONING = "testProvisioning";
private static final String ID_IMPLEMENTATION_SHORT_NAME = "implementationShortName";
private static final String ID_IMPLEMENTATION_DESCRIPTION = "implementationDescription";
Expand Down Expand Up @@ -229,6 +236,16 @@ public void onClick(AjaxRequestTarget target) {
};
add(testRepositoryCheckOrgClosure);

AjaxButton reindexRepositoryObjects = new AjaxButton(ID_REINDEX_REPOSITORY_OBJECTS,
createStringResource("PageAbout.button.reindexRepositoryObjects")) {

@Override
public void onClick(AjaxRequestTarget target) {
reindexRepositoryObjectsPerformed(target);
}
};
add(reindexRepositoryObjects);

AjaxButton testProvisioning = new AjaxButton(ID_TEST_PROVISIONING,
createStringResource("PageAbout.button.testProvisioning")) {

Expand Down Expand Up @@ -306,7 +323,7 @@ private void testRepositoryPerformed(AjaxRequestTarget target) {
}

private void testRepositoryCheckOrgClosurePerformed(AjaxRequestTarget target) {
OperationResult result = new OperationResult(OPERATION_GET_PROVISIONING_DIAG);
OperationResult result = new OperationResult(OPERATION_TEST_REPOSITORY_CHECK_ORG_CLOSURE);
try {
Task task = createSimpleTask(OPERATION_TEST_REPOSITORY_CHECK_ORG_CLOSURE);
getModelDiagnosticService().repositoryTestOrgClosureConsistency(task, true, result);
Expand All @@ -320,6 +337,32 @@ private void testRepositoryCheckOrgClosurePerformed(AjaxRequestTarget target) {
target.add(getFeedbackPanel());
}

private void reindexRepositoryObjectsPerformed(AjaxRequestTarget target) {
OperationResult result = new OperationResult(OPERATION_SUBMIT_REINDEX);
try {
TaskManager taskManager = getTaskManager();
Task task = taskManager.createTaskInstance();
MidPointPrincipal user = SecurityUtils.getPrincipalUser();
if (user == null) {
throw new RestartResponseException(PageLogin.class);
} else {
task.setOwner(user.getUser().asPrismObject());
}
getSecurityEnforcer().authorize(AuthorizationConstants.AUTZ_ALL_URL, null, null, null, null, null, result);
task.setChannel(SchemaConstants.CHANNEL_GUI_USER_URI);
task.setHandlerUri(ModelPublicConstants.REINDEX_TASK_HANDLER_URI);
task.setName("Reindex repository objects");
taskManager.switchToBackground(task, result);
result.setBackgroundTaskOid(task.getOid());
} catch (SecurityViolationException|SchemaException|RuntimeException e) {
result.recordFatalError(e);
} finally {
result.computeStatusIfUnknown();
}
showResult(result);
target.add(getFeedbackPanel());
}

private void testProvisioningPerformed(AjaxRequestTarget target) {
Task task = createSimpleTask(OPERATION_TEST_REPOSITORY);

Expand Down
Expand Up @@ -880,6 +880,7 @@ OrgUnitBrowser.title=Choose new parent for org. unit
PageAbout.allRightsReserved=&copy;2014 Evolveum.
PageAbout.button.testProvisioning=Provisioning self test
PageAbout.button.testRepositoryCheckOrgClosure=Check and repair org closure consistency
PageAbout.button.reindexRepositoryObjects=Reindex repository objects
PageAbout.button.testRepository=Repository self test
PageAbout.message.couldntObtainJvmParams=Couldn't obtain JVM parameters from JMX.
PageAbout.midPointRevision=${git.describe}
Expand Down
Expand Up @@ -29,6 +29,7 @@
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.security.api.SecurityEnforcer;
import com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException;
import com.evolveum.prism.xml.ns._public.query_3.QueryType;

Expand Down Expand Up @@ -80,19 +81,22 @@ public abstract class AbstractSearchIterativeTaskHandler<O extends ObjectType, H
// This is not ideal, TODO: refactor
private Map<Task, H> handlers = Collections.synchronizedMap(new HashMap<Task, H>());

@Autowired(required=true)
@Autowired
protected TaskManager taskManager;

@Autowired(required=true)
@Autowired
protected ModelObjectResolver modelObjectResolver;

@Autowired
@Qualifier("cacheRepositoryService")
protected RepositoryService repositoryService;

@Autowired(required = true)
@Autowired
protected PrismContext prismContext;


@Autowired
protected SecurityEnforcer securityEnforcer;

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

protected AbstractSearchIterativeTaskHandler(String taskName, String taskOperationPrefix) {
Expand Down Expand Up @@ -164,7 +168,16 @@ public TaskRunResult runInternal(Task coordinatorTask) {
TaskRunResult runResult = new TaskRunResult();
runResult.setOperationResult(opResult);

H resultHandler = createHandler(runResult, coordinatorTask, opResult);
H resultHandler = null;
try {
resultHandler = createHandler(runResult, coordinatorTask, opResult);
} catch (SecurityViolationException|SchemaException|RuntimeException e) {
LOGGER.error("{}: Error while creating a result handler: {}", taskName, e.getMessage(), e);
opResult.recordFatalError("Error while creating a result handler: " + e.getMessage(), e);
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
runResult.setProgress(coordinatorTask.getProgress());
return runResult;
}
if (resultHandler == null) {
// the error should already be in the runResult
return runResult;
Expand Down Expand Up @@ -428,7 +441,7 @@ protected boolean useRepositoryDirectly(H resultHandler, TaskRunResult runResult
protected abstract Class<? extends ObjectType> getType(Task task);

protected abstract H createHandler(TaskRunResult runResult, Task coordinatorTask,
OperationResult opResult);
OperationResult opResult) throws SchemaException, SecurityViolationException;

/**
* Used to properly initialize the "run", which is kind of task instance. The result handler is already created at this stage.
Expand Down
Expand Up @@ -20,10 +20,12 @@
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.schema.result.OperationConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskCategory;
import com.evolveum.midpoint.task.api.TaskRunResult;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
Expand Down Expand Up @@ -64,7 +66,9 @@ private void initialize() {
}

@Override
protected ReindexResultHandler createHandler(TaskRunResult runResult, Task coordinatorTask, OperationResult opResult) {
protected ReindexResultHandler createHandler(TaskRunResult runResult, Task coordinatorTask, OperationResult opResult)
throws SchemaException, SecurityViolationException {
securityEnforcer.authorize(AuthorizationConstants.AUTZ_ALL_URL, null, null, null, null, null, opResult);
return new ReindexResultHandler(coordinatorTask, ReindexTaskHandler.class.getName(),
"reindex", "reindex", taskManager, repositoryService);
}
Expand Down
Expand Up @@ -27,6 +27,7 @@
import com.evolveum.midpoint.prism.util.CloneUtil;
import com.evolveum.midpoint.prism.util.PrismAsserts;
import com.evolveum.midpoint.repo.api.RepoAddOptions;
import com.evolveum.midpoint.repo.api.RepoModifyOptions;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
Expand Down Expand Up @@ -91,7 +92,11 @@ public class CertificationTest extends BaseSQLRepoTest {
private String campaign2Oid;
private PrismObjectDefinition<AccessCertificationCampaignType> campaignDef;

@Test
protected RepoModifyOptions getModifyOptions() {
return null;
}

@Test
public void test100AddCampaignNonOverwrite() throws Exception {
PrismObject<AccessCertificationCampaignType> campaign = prismContext.parseObject(CAMPAIGN_1_FILE);
campaignDef = campaign.getDefinition();
Expand Down Expand Up @@ -488,11 +493,16 @@ public void test900DeleteCampaign() throws Exception {
}

protected void executeAndCheckModification(List<ItemDelta<?,?>> modifications, OperationResult result, int versionDelta) throws ObjectNotFoundException, SchemaException, ObjectAlreadyExistsException, IOException {
PrismObject<AccessCertificationCampaignType> before = getFullCampaign(campaign1Oid, result);
RepoModifyOptions modifyOptions = getModifyOptions();
if (RepoModifyOptions.isExecuteIfNoChanges(modifyOptions) && versionDelta == 0) {
versionDelta = 1;
}

PrismObject<AccessCertificationCampaignType> before = getFullCampaign(campaign1Oid, result);
int expectedVersion = Integer.parseInt(before.getVersion()) + versionDelta;
List<ItemDelta> savedModifications = (List) CloneUtil.cloneCollectionMembers(modifications);

repositoryService.modifyObject(AccessCertificationCampaignType.class, campaign1Oid, modifications, result);
repositoryService.modifyObject(AccessCertificationCampaignType.class, campaign1Oid, modifications, modifyOptions, result);

checkCampaign(campaign1Oid, result, before, savedModifications, expectedVersion);
}
Expand Down
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2010-2016 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.repo.sql;

import com.evolveum.midpoint.repo.api.RepoModifyOptions;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;

/**
* The same as CertificationTest but with "executeIfNoChanges" (a.k.a. "reindex") option set.
* Certification cases have very special treatment in this respect.
*
* @author mederly
*/
@ContextConfiguration(locations = {"../../../../../ctx-test.xml"})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class CertificationTestReindex extends CertificationTest {

protected RepoModifyOptions getModifyOptions() {
return RepoModifyOptions.createExecuteIfNoChanges();
}

}
Expand Up @@ -23,6 +23,7 @@
import com.evolveum.midpoint.prism.util.PrismAsserts;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.repo.api.RepoAddOptions;
import com.evolveum.midpoint.repo.api.RepoModifyOptions;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.result.OperationResult;
Expand Down Expand Up @@ -63,7 +64,11 @@ public class LookupTableTest extends BaseSQLRepoTest {

private String tableOid;

@Test
protected RepoModifyOptions getModifyOptions() {
return null;
}

@Test
public void test100AddTableNonOverwrite() throws Exception {
PrismObject<LookupTableType> table = prismContext.parseObject(new File(TEST_DIR, "table-1.xml"));
OperationResult result = new OperationResult("test100AddTableNonOverwrite");
Expand Down Expand Up @@ -247,9 +252,13 @@ private void checkTable(String tableOid, PrismObject<LookupTableType> expectedOb
}

protected void executeAndCheckModification(List<ItemDelta<?,?>> modifications, OperationResult result, int versionDelta) throws ObjectNotFoundException, SchemaException, ObjectAlreadyExistsException, IOException {
PrismObject<LookupTableType> before = getFullTable(tableOid, result);
RepoModifyOptions modifyOptions = getModifyOptions();
if (RepoModifyOptions.isExecuteIfNoChanges(modifyOptions) && versionDelta == 0) {
versionDelta = 1;
}
PrismObject<LookupTableType> before = getFullTable(tableOid, result);

repositoryService.modifyObject(LookupTableType.class, tableOid, modifications, result);
repositoryService.modifyObject(LookupTableType.class, tableOid, modifications, modifyOptions, result);

checkTable(tableOid, result, before, modifications, Integer.parseInt(before.getVersion()) + versionDelta);
}
Expand Down
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2010-2016 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.repo.sql;

import com.evolveum.midpoint.repo.api.RepoModifyOptions;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;

/**
* The same as LookupTableTest but with "executeIfNoChanges" (a.k.a. "reindex") option set.
* Although this flag causes no special behavior for lookup table rows, it's better to check.
*
* @author mederly
*/
@ContextConfiguration(locations = {"../../../../../ctx-test.xml"})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class LookupTableTestReindex extends LookupTableTest {

protected RepoModifyOptions getModifyOptions() {
return RepoModifyOptions.createExecuteIfNoChanges();
}

}

0 comments on commit a87be8e

Please sign in to comment.