use of org.apache.oozie.executor.jpa.JPAExecutorException in project oozie by apache.
the class JPAService method execute.
/**
* Execute a {@link JPAExecutor}.
*
* @param executor JPAExecutor to execute.
* @return return value of the JPAExecutor.
* @throws JPAExecutorException thrown if an jpa executor failed
*/
public <T> T execute(final JPAExecutor<T> executor) throws JPAExecutorException {
final EntityManager em = getEntityManager();
final Instrumentation.Cron cron = new Instrumentation.Cron();
try {
LOG.trace("Executing JPAExecutor [{0}]", executor.getName());
if (instr != null) {
instr.incr(INSTRUMENTATION_GROUP_JPA, executor.getName(), 1);
}
cron.start();
return retryHandler.executeWithRetry(new Callable<T>() {
@Override
public T call() throws Exception {
if (!em.getTransaction().isActive()) {
em.getTransaction().begin();
}
final T t = executor.execute(em);
checkAndCommit(em.getTransaction());
return t;
}
});
} catch (final Exception e) {
throw getTargetException(e);
} finally {
cron.stop();
if (instr != null) {
instr.addCron(INSTRUMENTATION_GROUP_JPA, executor.getName(), cron);
}
try {
if (em.getTransaction().isActive()) {
LOG.warn("JPAExecutor [{0}] ended with an active transaction, rolling back", executor.getName());
em.getTransaction().rollback();
}
} catch (final Exception ex) {
LOG.warn("Could not check/rollback transaction after JPAExecutor [{0}], {1}", executor.getName(), ex.getMessage(), ex);
}
try {
if (em.isOpen()) {
em.close();
} else {
LOG.warn("JPAExecutor [{0}] closed the EntityManager, it should not!", executor.getName());
}
} catch (final Exception ex) {
LOG.warn("Could not close EntityManager after JPAExecutor [{0}], {1}", executor.getName(), ex.getMessage(), ex);
}
}
}
use of org.apache.oozie.executor.jpa.JPAExecutorException in project oozie by apache.
the class AuthorizationService method authorizeForJobs.
/**
* Check if the user+group is authorized to operate on the specified jobs. <p> Checks if the user is a super-user or
* the one who started the jobs. <p> Read operations are allowed to all users.
*
* @param user user name.
* @param filter filter used to select jobs
* @param start starting index of the jobs in DB
* @param len maximum amount of jobs to select
* @param write indicates if the check is for read or write job tasks.
* @throws AuthorizationException thrown if the user is not authorized for the job.
*/
public void authorizeForJobs(String user, Map<String, List<String>> filter, String jobType, int start, int len, boolean write) throws AuthorizationException {
if (authorizationEnabled && write && !isAdmin(user)) {
try {
// handle workflow jobs
if (jobType.equals("wf")) {
List<WorkflowJobBean> jobBeans = new ArrayList<WorkflowJobBean>();
JPAService jpaService = Services.get().get(JPAService.class);
if (jpaService != null) {
try {
jobBeans = jpaService.execute(new WorkflowsJobGetJPAExecutor(filter, start, len)).getWorkflows();
} catch (JPAExecutorException je) {
throw new AuthorizationException(je);
}
} else {
throw new AuthorizationException(ErrorCode.E0610);
}
for (WorkflowJobBean jobBean : jobBeans) {
if (jobBean != null && !jobBean.getUser().equals(user)) {
if (!isUserInAcl(user, jobBean.getGroup())) {
incrCounter(INSTR_FAILED_AUTH_COUNTER, 1);
throw new AuthorizationException(ErrorCode.E0508, user, jobBean.getId());
}
}
}
} else // handle bundle jobs
if (jobType.equals("bundle")) {
List<BundleJobBean> jobBeans = new ArrayList<BundleJobBean>();
JPAService jpaService = Services.get().get(JPAService.class);
if (jpaService != null) {
try {
jobBeans = jpaService.execute(new BundleJobInfoGetJPAExecutor(filter, start, len)).getBundleJobs();
} catch (JPAExecutorException je) {
throw new AuthorizationException(je);
}
} else {
throw new AuthorizationException(ErrorCode.E0610);
}
for (BundleJobBean jobBean : jobBeans) {
if (jobBean != null && !jobBean.getUser().equals(user)) {
if (!isUserInAcl(user, jobBean.getGroup())) {
incrCounter(INSTR_FAILED_AUTH_COUNTER, 1);
throw new AuthorizationException(ErrorCode.E0509, user, jobBean.getId());
}
}
}
} else // handle coordinator jobs
{
List<CoordinatorJobBean> jobBeans = new ArrayList<CoordinatorJobBean>();
JPAService jpaService = Services.get().get(JPAService.class);
if (jpaService != null) {
try {
jobBeans = jpaService.execute(new CoordJobInfoGetJPAExecutor(filter, start, len)).getCoordJobs();
} catch (JPAExecutorException je) {
throw new AuthorizationException(je);
}
} else {
throw new AuthorizationException(ErrorCode.E0610);
}
for (CoordinatorJobBean jobBean : jobBeans) {
if (jobBean != null && !jobBean.getUser().equals(user)) {
if (!isUserInAcl(user, jobBean.getGroup())) {
incrCounter(INSTR_FAILED_AUTH_COUNTER, 1);
throw new AuthorizationException(ErrorCode.E0509, user, jobBean.getId());
}
}
}
}
} catch (IOException ex) {
throw new AuthorizationException(ErrorCode.E0501, ex.getMessage(), ex);
}
}
}
use of org.apache.oozie.executor.jpa.JPAExecutorException in project oozie by apache.
the class AuthorizationService method authorizeForJob.
/**
* Check if the user+group is authorized to operate on the specified job. <p> Checks if the user is a super-user or
* the one who started the job. <p> Read operations are allowed to all users.
*
* @param user user name.
* @param jobId job id.
* @param write indicates if the check is for read or write job tasks.
* @throws AuthorizationException thrown if the user is not authorized for the job.
*/
public void authorizeForJob(String user, String jobId, boolean write) throws AuthorizationException {
if (authorizationEnabled && write && !isAdmin(user)) {
try {
// handle workflow jobs
if (jobId.endsWith("-W")) {
WorkflowJobBean jobBean = null;
JPAService jpaService = Services.get().get(JPAService.class);
if (jpaService != null) {
try {
jobBean = WorkflowJobQueryExecutor.getInstance().get(WorkflowJobQuery.GET_WORKFLOW_USER_GROUP, jobId);
} catch (JPAExecutorException je) {
throw new AuthorizationException(je);
}
} else {
throw new AuthorizationException(ErrorCode.E0610);
}
if (jobBean != null && !jobBean.getUser().equals(user)) {
if (!isUserInAcl(user, jobBean.getGroup())) {
incrCounter(INSTR_FAILED_AUTH_COUNTER, 1);
throw new AuthorizationException(ErrorCode.E0508, user, jobId);
}
}
} else // handle bundle jobs
if (jobId.endsWith("-B")) {
BundleJobBean jobBean = null;
JPAService jpaService = Services.get().get(JPAService.class);
if (jpaService != null) {
try {
jobBean = jpaService.execute(new BundleJobGetJPAExecutor(jobId));
} catch (JPAExecutorException je) {
throw new AuthorizationException(je);
}
} else {
throw new AuthorizationException(ErrorCode.E0610);
}
if (jobBean != null && !jobBean.getUser().equals(user)) {
if (!isUserInAcl(user, jobBean.getGroup())) {
incrCounter(INSTR_FAILED_AUTH_COUNTER, 1);
throw new AuthorizationException(ErrorCode.E0509, user, jobId);
}
}
} else // handle coordinator jobs
{
CoordinatorJobBean jobBean = null;
JPAService jpaService = Services.get().get(JPAService.class);
if (jpaService != null) {
try {
jobBean = jpaService.execute(new CoordJobGetJPAExecutor(jobId));
} catch (JPAExecutorException je) {
throw new AuthorizationException(je);
}
} else {
throw new AuthorizationException(ErrorCode.E0610);
}
if (jobBean != null && !jobBean.getUser().equals(user)) {
if (!isUserInAcl(user, jobBean.getGroup())) {
incrCounter(INSTR_FAILED_AUTH_COUNTER, 1);
throw new AuthorizationException(ErrorCode.E0509, user, jobId);
}
}
}
} catch (IOException ex) {
throw new AuthorizationException(ErrorCode.E0501, ex.getMessage(), ex);
}
}
}
use of org.apache.oozie.executor.jpa.JPAExecutorException in project oozie by apache.
the class TestPurgeService method testPurgeServiceForCoordinator.
/**
* Tests the {@link org.apache.oozie.service.PurgeService}.
* </p>
* Creates a new coordinator job. Attempts to purge jobs older than a day.
* Verifies the presence of the job in the system.
* </p>
* Sets the end date for the same job to make it qualify for the purge criteria.
* Calls the purge service, and ensure the job does not exist in the system.
*/
public void testPurgeServiceForCoordinator() throws Exception {
String currentDatePlusMonth = XDataTestCase.getCurrentDateafterIncrementingInMonths(1);
Date start = DateUtils.parseDateOozieTZ(currentDatePlusMonth);
Date end = DateUtils.parseDateOozieTZ(currentDatePlusMonth);
CoordinatorJobBean job = addRecordToCoordJobTable(CoordinatorJob.Status.SUCCEEDED, start, end, false, false, 0);
final String jobId = job.getId();
CoordinatorActionBean action = addRecordToCoordActionTable(job.getId(), 1, CoordinatorAction.Status.SUCCEEDED, "coord-action-get.xml", 0);
JPAService jpaService = Services.get().get(JPAService.class);
assertNotNull(jpaService);
CoordJobGetJPAExecutor coordJobGetExecutor = new CoordJobGetJPAExecutor(job.getId());
CoordActionGetJPAExecutor coordActionGetExecutor = new CoordActionGetJPAExecutor(action.getId());
job = jpaService.execute(coordJobGetExecutor);
action = jpaService.execute(coordActionGetExecutor);
assertEquals(job.getStatus(), CoordinatorJob.Status.SUCCEEDED);
assertEquals(action.getStatus(), CoordinatorAction.Status.SUCCEEDED);
Runnable purgeRunnable = new PurgeRunnable(1, 1, 1, 100);
purgeRunnable.run();
final CoordinatorEngine engine = new CoordinatorEngine("u");
waitFor(10000, new Predicate() {
public boolean evaluate() throws Exception {
try {
engine.getCoordJob(jobId).getStatus();
} catch (Exception ex) {
return true;
}
return false;
}
});
try {
job = jpaService.execute(coordJobGetExecutor);
fail("Job should be purged. Should fail.");
} catch (JPAExecutorException je) {
// Job doesn't exist. Exception is expected.
}
try {
jpaService.execute(coordActionGetExecutor);
fail("Action should be purged. Should fail.");
} catch (JPAExecutorException je) {
// Job doesn't exist. Exception is expected.
}
}
use of org.apache.oozie.executor.jpa.JPAExecutorException in project oozie by apache.
the class TestPurgeService method addRecordToBundleJobTable.
protected BundleJobBean addRecordToBundleJobTable(Job.Status jobStatus, Date lastModifiedTime) throws Exception {
BundleJobBean bundle = createBundleJob(jobStatus, false);
bundle.setLastModifiedTime(lastModifiedTime);
try {
JPAService jpaService = Services.get().get(JPAService.class);
assertNotNull(jpaService);
BundleJobInsertJPAExecutor bundleInsertjpa = new BundleJobInsertJPAExecutor(bundle);
jpaService.execute(bundleInsertjpa);
} catch (JPAExecutorException je) {
je.printStackTrace();
fail("Unable to insert the test bundle job record to table");
throw je;
}
return bundle;
}
Aggregations