Search in sources :

Example 11 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class SqaleRepoBaseTest method extKey.

@NotNull
private String extKey(Containerable extContainer, String itemName, MExtItemHolderType holder) {
    PrismContainerValue<?> pcv = extContainer.asPrismContainerValue();
    ItemDefinition<?> def = pcv.getDefinition().findItemDefinition(new ItemName(itemName));
    MExtItem.Key key = MExtItem.keyFrom(def, holder);
    try (JdbcSession jdbcSession = startReadOnlyTransaction()) {
        QExtItem ei = QExtItem.DEFAULT;
        return jdbcSession.newQuery().from(ei).where(ei.itemName.eq(key.itemName).and(ei.valueType.eq(key.valueType)).and(ei.holderType.eq(key.holderType)).and(ei.cardinality.eq(key.cardinality))).select(ei.id).fetchFirst().toString();
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) MExtItem(com.evolveum.midpoint.repo.sqale.qmodel.ext.MExtItem) ItemName(com.evolveum.midpoint.prism.path.ItemName) QExtItem(com.evolveum.midpoint.repo.sqale.qmodel.ext.QExtItem) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class SqaleRepoBaseTest method refreshOrgClosureForce.

protected void refreshOrgClosureForce() {
    try (JdbcSession jdbcSession = startTransaction()) {
        jdbcSession.executeStatement("CALL m_refresh_org_closure(true)");
        jdbcSession.commit();
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession)

Example 13 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class SqaleRepoSearchIterativeTest method test125SearchIterativeWithCustomOrdering.

@Test
public void test125SearchIterativeWithCustomOrdering() throws Exception {
    OperationResult operationResult = createOperationResult();
    SqlPerformanceMonitorImpl pm = getPerformanceMonitor();
    pm.clearGlobalPerformanceInformation();
    given("query with custom ordering");
    ObjectQuery query = prismContext.queryFor(UserType.class).asc(UserType.F_COST_CENTER).maxSize(// see the limit below
    47).build();
    when("calling search iterative");
    SearchResultMetadata metadata = searchObjectsIterative(query, operationResult);
    then("result metadata is not null and reports partial result (because of the break)");
    assertThatOperationResult(operationResult).isSuccess();
    assertThat(metadata).isNotNull();
    assertThat(metadata.getApproxNumberOfAllResults()).isEqualTo(testHandler.getCounter());
    // everything was processed
    assertThat(metadata.isPartialResults()).isFalse();
    and("search operations were called");
    assertOperationRecordedCount(REPO_OP_PREFIX + RepositoryService.OP_SEARCH_OBJECTS_ITERATIVE, 1);
    assertTypicalPageOperationCount(metadata);
    and("all objects were processed in proper order");
    QUser u = aliasFor(QUser.class);
    try (JdbcSession jdbcSession = startReadOnlyTransaction()) {
        List<String> result = jdbcSession.newQuery().from(u).orderBy(u.costCenter.asc(), u.oid.asc()).select(u.employeeNumber).limit(// must match the maxSize above
        47).fetch();
        for (int i = 1; i < result.size(); i++) {
            // order matches
            assertThat(result.get(i)).isEqualTo(getTestNumber() + "-" + i);
        }
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) QUser(com.evolveum.midpoint.repo.sqale.qmodel.focus.QUser) SqlPerformanceMonitorImpl(com.evolveum.midpoint.repo.sqlbase.perfmon.SqlPerformanceMonitorImpl) SearchResultMetadata(com.evolveum.midpoint.schema.SearchResultMetadata) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) Test(org.testng.annotations.Test) SqaleRepoBaseTest(com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest)

Example 14 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class SqaleRepoModifyObjectTest method test952ReindexFixingColumnsOutOfSync.

@Test
public void test952ReindexFixingColumnsOutOfSync() throws SchemaException, ObjectNotFoundException, ObjectAlreadyExistsException {
    OperationResult result = createOperationResult();
    given("user existing in the repository");
    String name = "user" + getTestNumber();
    UserType user = new UserType(prismContext).name(name).emailAddress(name + "@goodmail.com").subtype("subtype-1").subtype("subtype-2").assignment(new AssignmentType(prismContext).order(1));
    String oid = repositoryService.addObject(user.asPrismObject(), null, result);
    and("object aggregate is modified in the DB (simulating out-of-sync data)");
    QAssignment<MObject> a = QAssignmentMapping.getAssignmentMapping().defaultAlias();
    UUID oidUuid = UUID.fromString(oid);
    try (JdbcSession jdbcSession = startTransaction()) {
        jdbcSession.newInsert(a).set(a.ownerOid, oidUuid).set(a.cid, -1L).set(a.containerType, MContainerType.ASSIGNMENT).set(a.ownerType, MObjectType.USER).set(a.orderValue, // test number, hopefully unique
        952).execute();
        QUser u = QUserMapping.getUserMapping().defaultAlias();
        jdbcSession.newUpdate(u).set(u.emailAddress, "bad@badmail.com").set(u.subtypes, new String[] { "subtype-952" }).set(u.costCenter, "invasive value").where(u.oid.eq(oidUuid)).execute();
        jdbcSession.commit();
    }
    and("search provides obviously bad results");
    assertThat(repositorySearchObjects(UserType.class, prismContext.queryFor(UserType.class).item(UserType.F_EMAIL_ADDRESS).eq(name + "@goodmail.com").build(), result)).isEmpty();
    assertThat(repositorySearchObjects(UserType.class, prismContext.queryFor(UserType.class).item(UserType.F_COST_CENTER).eq("invasive value").build(), result)).hasSize(// can find by bad value, that's wrong
    1);
    assertThat(repositorySearchObjects(UserType.class, prismContext.queryFor(UserType.class).item(UserType.F_ASSIGNMENT, AssignmentType.F_ORDER).eq(952).build(), result)).hasSize(// can find by wrong assignment
    1);
    assertThat(repositorySearchObjects(UserType.class, prismContext.queryFor(UserType.class).item(UserType.F_SUBTYPE).eq("subtype-952").build(), result)).hasSize(// can find by wrong subtype
    1);
    when("reindex is called to fix it");
    repositoryService.modifyObject(UserType.class, oid, Collections.emptyList(), RepoModifyOptions.createForceReindex(), result);
    then("the searches work as expected");
    assertThat(repositorySearchObjects(UserType.class, prismContext.queryFor(UserType.class).item(UserType.F_EMAIL_ADDRESS).eq(name + "@goodmail.com").build(), result)).hasSize(1).extracting(o -> o.getOid()).containsExactlyInAnyOrder(// can find by the original mail, that's good!
    oid);
    assertThat(repositorySearchObjects(UserType.class, prismContext.queryFor(UserType.class).item(UserType.F_COST_CENTER).eq("invasive value").build(), result)).isEmpty();
    assertThat(repositorySearchObjects(UserType.class, prismContext.queryFor(UserType.class).item(UserType.F_ASSIGNMENT, AssignmentType.F_ORDER).eq(952).build(), result)).isEmpty();
    assertThat(repositorySearchObjects(UserType.class, prismContext.queryFor(UserType.class).item(UserType.F_ASSIGNMENT, AssignmentType.F_ORDER).eq(1).build(), result)).extracting(o -> o.getOid()).contains(// good, can have more results, but our user is there too
    oid);
    and("database values are as good as new");
    MUser userRow = selectObjectByOid(QUser.class, oid);
    // tested by search above as well
    assertThat(userRow.emailAddress).isEqualTo(name + "@goodmail.com");
    assertThat(userRow.costCenter).isNull();
    assertThat(userRow.subtypes).containsExactlyInAnyOrder("subtype-1", "subtype-2");
    List<MAssignment> assRows = select(a, a.ownerOid.eq(oidUuid));
    // single row with proper order is returned, wrong row is gone
    assertThat(assRows).hasSize(1);
    assertThat(assRows.get(0).orderValue).isEqualTo(1);
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) QService(com.evolveum.midpoint.repo.sqale.qmodel.role.QService) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SqaleUtils(com.evolveum.midpoint.repo.sqale.SqaleUtils) MService(com.evolveum.midpoint.repo.sqale.qmodel.role.MService) JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Test(org.testng.annotations.Test) BigDecimal(java.math.BigDecimal) Assertions(org.assertj.core.api.Assertions) MReference(com.evolveum.midpoint.repo.sqale.qmodel.ref.MReference) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) QUser(com.evolveum.midpoint.repo.sqale.qmodel.focus.QUser) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) BeforeClass(org.testng.annotations.BeforeClass) MContainerType(com.evolveum.midpoint.repo.sqale.qmodel.common.MContainerType) MiscUtil(com.evolveum.midpoint.util.MiscUtil) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) MTask(com.evolveum.midpoint.repo.sqale.qmodel.task.MTask) QConnector(com.evolveum.midpoint.repo.sqale.qmodel.connector.QConnector) SystemException(com.evolveum.midpoint.util.exception.SystemException) PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) MObject(com.evolveum.midpoint.repo.sqale.qmodel.object.MObject) QUserMapping(com.evolveum.midpoint.repo.sqale.qmodel.focus.QUserMapping) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) java.util(java.util) com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) Item(com.evolveum.midpoint.prism.Item) Jsonb(com.evolveum.midpoint.repo.sqale.jsonb.Jsonb) SchemaConstants(com.evolveum.midpoint.schema.constants.SchemaConstants) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Assert.assertEquals(org.testng.Assert.assertEquals) SqaleRepoBaseTest(com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) QObjectReferenceMapping(com.evolveum.midpoint.repo.sqale.qmodel.ref.QObjectReferenceMapping) MShadow(com.evolveum.midpoint.repo.sqale.qmodel.shadow.MShadow) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ObjectTypeUtil(com.evolveum.midpoint.schema.util.ObjectTypeUtil) TestUtil(com.evolveum.midpoint.test.util.TestUtil) Comparator.comparing(java.util.Comparator.comparing) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) MObjectType(com.evolveum.midpoint.repo.sqale.qmodel.object.MObjectType) QObjectReference(com.evolveum.midpoint.repo.sqale.qmodel.ref.QObjectReference) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) com.evolveum.midpoint.repo.sqale.qmodel.assignment(com.evolveum.midpoint.repo.sqale.qmodel.assignment) PrismObject(com.evolveum.midpoint.prism.PrismObject) QShadow(com.evolveum.midpoint.repo.sqale.qmodel.shadow.QShadow) MUser(com.evolveum.midpoint.repo.sqale.qmodel.focus.MUser) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) ItemName(com.evolveum.midpoint.prism.path.ItemName) RepoModifyOptions(com.evolveum.midpoint.repo.api.RepoModifyOptions) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) SearchResultList(com.evolveum.midpoint.schema.SearchResultList) GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) Assert.assertTrue(org.testng.Assert.assertTrue) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) QTask(com.evolveum.midpoint.repo.sqale.qmodel.task.QTask) com.evolveum.midpoint.repo.sqale.qmodel.accesscert(com.evolveum.midpoint.repo.sqale.qmodel.accesscert) QUser(com.evolveum.midpoint.repo.sqale.qmodel.focus.QUser) MObject(com.evolveum.midpoint.repo.sqale.qmodel.object.MObject) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) MUser(com.evolveum.midpoint.repo.sqale.qmodel.focus.MUser) Test(org.testng.annotations.Test) SqaleRepoBaseTest(com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest)

Example 15 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class SequenceConcurrencyTest method concurrencyUniversal.

private void concurrencyUniversal(long duration, WorkerThread[] workerThreads, boolean alwaysOrder) throws Exception {
    try (JdbcSession jdbcSession = startTransaction()) {
        System.out.println(">>>>" + jdbcSession.connection().getTransactionIsolation());
    }
    OperationResult result = createOperationResult();
    String oid = repositoryService.addObject(new SequenceType(prismContext).name(getTestNameShort()).counter(0L).maxUnusedValues(10).asPrismObject(), null, result);
    display("*** Object added: " + oid + " ***");
    display("*** Starting modifier threads ***");
    for (WorkerThread t : workerThreads) {
        t.setOid(oid);
        t.start();
    }
    display("*** Waiting " + duration + " ms ***");
    Thread.sleep(duration);
    for (WorkerThread t : workerThreads) {
        t.stop = true;
    }
    long endTime = System.currentTimeMillis() + STOP_TIMEOUT;
    for (; ; ) {
        long remaining = endTime - System.currentTimeMillis();
        if (remaining <= 0) {
            break;
        }
        for (WorkerThread t : workerThreads) {
            t.join(remaining);
            remaining = endTime - System.currentTimeMillis();
            if (remaining <= 0) {
                break;
            }
        }
    }
    for (WorkerThread t : workerThreads) {
        display("Worker thread " + t.id + " finished after " + t.counter + " iterations with result: " + (t.threadResult != null ? t.threadResult : "OK"));
    }
    for (WorkerThread t : workerThreads) {
        if (t.threadResult != null) {
            throw new AssertionError("Worker thread " + t.id + " finished with an exception: " + t.threadResult, t.threadResult);
        }
    }
    List<Long> allValues = new ArrayList<>();
    for (WorkerThread t : workerThreads) {
        allValues.addAll(t.values);
    }
    if (alwaysOrder || workerThreads.length > 1) {
        Collections.sort(allValues);
    }
    logger.trace("Checking a list of {} values", allValues.size());
    for (int i = 0; i < allValues.size(); i++) {
        if (allValues.get(i) != i) {
            logger.error("Incorrect value at position {}: {}", i, allValues.get(i));
            for (WorkerThread t : workerThreads) {
                display("Thread " + t.id + ": " + t.values);
            }
            fail("Incorrect value at position " + i + ": " + allValues.get(i));
        }
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) SequenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.SequenceType)

Aggregations

JdbcSession (com.evolveum.midpoint.repo.sqlbase.JdbcSession)27 SqaleRepoBaseTest (com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest)4 MExtItem (com.evolveum.midpoint.repo.sqale.qmodel.ext.MExtItem)4 QExtItem (com.evolveum.midpoint.repo.sqale.qmodel.ext.QExtItem)4 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)4 NotNull (org.jetbrains.annotations.NotNull)4 MUri (com.evolveum.midpoint.repo.sqale.qmodel.common.MUri)3 QUser (com.evolveum.midpoint.repo.sqale.qmodel.focus.QUser)3 Test (org.testng.annotations.Test)3 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)2 ItemName (com.evolveum.midpoint.prism.path.ItemName)2 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)2 RepoModifyOptions (com.evolveum.midpoint.repo.api.RepoModifyOptions)2 Jsonb (com.evolveum.midpoint.repo.sqale.jsonb.Jsonb)2 QUri (com.evolveum.midpoint.repo.sqale.qmodel.common.QUri)2 MUser (com.evolveum.midpoint.repo.sqale.qmodel.focus.MUser)2 QUserMapping (com.evolveum.midpoint.repo.sqale.qmodel.focus.QUserMapping)2 Instant (java.time.Instant)2 QName (javax.xml.namespace.QName)2