Search in sources :

Example 1 with BusinessEntitySnapshot

use of org.ovirt.engine.core.common.businessentities.BusinessEntitySnapshot in project ovirt-engine by oVirt.

the class BusinessEntitySnapshotDaoImpl method getAllForCommandId.

@Override
public List<BusinessEntitySnapshot> getAllForCommandId(Guid commandID) {
    MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource().addValue("command_id", commandID);
    return getCallsHandler().executeReadList("get_entity_snapshot_by_command_id", (rs, rowNum) -> {
        BusinessEntitySnapshot result = new BusinessEntitySnapshot();
        result.setId(getGuidDefaultEmpty(rs, "id"));
        result.setCommandId(getGuidDefaultEmpty(rs, "command_id"));
        result.setCommandType(rs.getString("command_type"));
        result.setEntityId(rs.getString("entity_id"));
        result.setEntityType(rs.getString("entity_type"));
        result.setEntitySnapshot(rs.getString("entity_snapshot"));
        result.setSnapshotClass(rs.getString("snapshot_class"));
        result.setSnapshotType(SnapshotType.values()[rs.getInt("snapshot_type")]);
        result.setInsertionOrder(rs.getInt("insertion_order"));
        return result;
    }, parameterSource);
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) BusinessEntitySnapshot(org.ovirt.engine.core.common.businessentities.BusinessEntitySnapshot)

Example 2 with BusinessEntitySnapshot

use of org.ovirt.engine.core.common.businessentities.BusinessEntitySnapshot in project ovirt-engine by oVirt.

the class DefaultCompensationContext method createBusinessEntitySnapshot.

private BusinessEntitySnapshot createBusinessEntitySnapshot(BusinessEntity<?> entity, Serializable payload, SnapshotType snapshotType) {
    BusinessEntitySnapshot entitySnapshot = new BusinessEntitySnapshot();
    entitySnapshot.setCommandId(commandId);
    entitySnapshot.setCommandType(commandType);
    entitySnapshot.setEntityId(String.valueOf(entity.getId()));
    entitySnapshot.setEntityType(entity.getClass().getName());
    entitySnapshot.setEntitySnapshot((String) snapshotSerializer.serialize(payload));
    entitySnapshot.setSnapshotClass(payload.getClass().getName());
    entitySnapshot.setSnapshotType(snapshotType);
    entitySnapshot.setInsertionOrder(cachedEntities.size());
    return entitySnapshot;
}
Also used : BusinessEntitySnapshot(org.ovirt.engine.core.common.businessentities.BusinessEntitySnapshot)

Example 3 with BusinessEntitySnapshot

use of org.ovirt.engine.core.common.businessentities.BusinessEntitySnapshot in project ovirt-engine by oVirt.

the class BusinessEntitySnapshotDaoTest method testInsertSnapshot.

@Test
public void testInsertSnapshot() {
    BusinessEntitySnapshot snapshot = new BusinessEntitySnapshot();
    Guid commandId = Guid.newGuid();
    snapshot.setCommandId(commandId);
    snapshot.setCommandType("org.ovirt.engine.core.bll.UpdateVdsCommand");
    snapshot.setEntityId(Guid.newGuid().toString());
    snapshot.setEntityType("org.ovirt.engine.core.common.businessentities.VdsStatic");
    snapshot.setEntitySnapshot("something");
    snapshot.setSnapshotClass("someClass");
    dao.save(snapshot);
    List<BusinessEntitySnapshot> snapshotsFromDb = dao.getAllForCommandId(commandId);
    assertNotNull(snapshotsFromDb);
    assertEquals(1, snapshotsFromDb.size());
    assertEquals(snapshot, snapshotsFromDb.get(0));
}
Also used : Guid(org.ovirt.engine.core.compat.Guid) BusinessEntitySnapshot(org.ovirt.engine.core.common.businessentities.BusinessEntitySnapshot) Test(org.junit.Test)

Example 4 with BusinessEntitySnapshot

use of org.ovirt.engine.core.common.businessentities.BusinessEntitySnapshot in project ovirt-engine by oVirt.

the class CommandCompensator method compensate.

@SuppressWarnings({ "unchecked", "synthetic-access" })
public void compensate(Guid commandId, String commandType, CompensationContext compensationContext) {
    TransactionSupport.executeInNewTransaction(() -> {
        Deserializer deserializer = SerializationFactory.getDeserializer();
        List<BusinessEntitySnapshot> entitySnapshots = businessEntitySnapshotDao.getAllForCommandId(commandId);
        log.debug("Command [id={}]: {} compensation data.", commandId, entitySnapshots.isEmpty() ? "No" : "Going over");
        for (BusinessEntitySnapshot snapshot : entitySnapshots) {
            Class<Serializable> snapshotClass = (Class<Serializable>) ReflectionUtils.getClassFor(snapshot.getSnapshotClass());
            Serializable snapshotData = deserializer.deserialize(snapshot.getEntitySnapshot(), snapshotClass);
            log.info("Command [id={}]: Compensating {} of {}; snapshot: {}.", commandId, snapshot.getSnapshotType(), snapshot.getEntityType(), snapshot.getSnapshotType() == BusinessEntitySnapshot.SnapshotType.DELETED_OR_UPDATED_ENTITY ? "id=" + snapshot.getEntityId() : snapshotData.toString());
            Class<BusinessEntity<Serializable>> entityClass = (Class<BusinessEntity<Serializable>>) ReflectionUtils.getClassFor(snapshot.getEntityType());
            switch(snapshot.getSnapshotType()) {
                case CHANGED_STATUS_ONLY:
                    BusinessEntitySnapshot.EntityStatusSnapshot entityStatusSnapshot = (BusinessEntitySnapshot.EntityStatusSnapshot) snapshotData;
                    ((StatusAwareDao<Serializable, Enum<?>>) getDaoForEntity(entityClass)).updateStatus(entityStatusSnapshot.getId(), entityStatusSnapshot.getStatus());
                    break;
                case DELETED_OR_UPDATED_ENTITY:
                    deletedOrUpdateEntity(entityClass, (BusinessEntity<Serializable>) snapshotData);
                    break;
                case UPDATED_ONLY_ENTITY:
                    getDaoForEntity(entityClass).update((BusinessEntity<Serializable>) snapshotData);
                    break;
                case NEW_ENTITY_ID:
                    getDaoForEntity(entityClass).remove(snapshotData);
                    break;
                case TRANSIENT_ENTITY:
                    objectCompensation.compensate(commandType, (TransientCompensationBusinessEntity) snapshotData);
                    break;
                default:
                    throw new IllegalArgumentException(String.format("Unknown %s value, unable to compensate value %s.", BusinessEntitySnapshot.SnapshotType.class.getName(), snapshot.getSnapshotType()));
            }
        }
        if (compensationContext == null) {
            businessEntitySnapshotDao.removeAllForCommandId(commandId);
        } else {
            compensationContext.afterCompensationCleanup();
        }
        return null;
    });
}
Also used : Serializable(java.io.Serializable) StatusAwareDao(org.ovirt.engine.core.dao.StatusAwareDao) Deserializer(org.ovirt.engine.core.utils.Deserializer) BusinessEntity(org.ovirt.engine.core.common.businessentities.BusinessEntity) TransientCompensationBusinessEntity(org.ovirt.engine.core.common.businessentities.TransientCompensationBusinessEntity) BusinessEntitySnapshot(org.ovirt.engine.core.common.businessentities.BusinessEntitySnapshot)

Aggregations

BusinessEntitySnapshot (org.ovirt.engine.core.common.businessentities.BusinessEntitySnapshot)4 Serializable (java.io.Serializable)1 Test (org.junit.Test)1 BusinessEntity (org.ovirt.engine.core.common.businessentities.BusinessEntity)1 TransientCompensationBusinessEntity (org.ovirt.engine.core.common.businessentities.TransientCompensationBusinessEntity)1 Guid (org.ovirt.engine.core.compat.Guid)1 StatusAwareDao (org.ovirt.engine.core.dao.StatusAwareDao)1 Deserializer (org.ovirt.engine.core.utils.Deserializer)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1