001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.oozie.executor.jpa;
019
020 import java.sql.Timestamp;
021 import java.util.ArrayList;
022 import java.util.List;
023
024 import javax.persistence.EntityManager;
025 import javax.persistence.Query;
026
027 import org.apache.oozie.CoordinatorActionBean;
028 import org.apache.oozie.ErrorCode;
029 import org.apache.oozie.StringBlob;
030 import org.apache.oozie.client.CoordinatorAction;
031 import org.apache.oozie.util.ParamChecker;
032
033 public class CoordActionsGetForRecoveryJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
034
035 private long checkAgeSecs = 0;
036
037 public CoordActionsGetForRecoveryJPAExecutor(final long checkAgeSecs) {
038 ParamChecker.notNull(checkAgeSecs, "checkAgeSecs");
039 this.checkAgeSecs = checkAgeSecs;
040 }
041
042 /* (non-Javadoc)
043 * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
044 */
045 @Override
046 public String getName() {
047 return "CoordActionsGetForRecoveryJPAExecutor";
048 }
049
050 /* (non-Javadoc)
051 * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
052 */
053 @SuppressWarnings("unchecked")
054 @Override
055 public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
056 List<CoordinatorActionBean> allActions = new ArrayList<CoordinatorActionBean>();
057
058 try {
059 Query q = em.createNamedQuery("GET_COORD_ACTIONS_FOR_RECOVERY_OLDER_THAN");
060 Timestamp ts = new Timestamp(System.currentTimeMillis() - this.checkAgeSecs * 1000);
061 q.setParameter("lastModifiedTime", ts);
062 List<Object[]> objectArrList = q.getResultList();
063 for (Object[] arr : objectArrList) {
064 CoordinatorActionBean caa = getBeanForCoordinatorActionFromArrayForRecovery(arr);
065 allActions.add(caa);
066 }
067
068 q = em.createNamedQuery("GET_COORD_ACTIONS_WAITING_SUBMITTED_OLDER_THAN");
069 q.setParameter("lastModifiedTime", ts);
070 objectArrList = q.getResultList();
071 for (Object[] arr : objectArrList) {
072 CoordinatorActionBean caa = getBeanForCoordinatorActionFromArrayForWaiting(arr);
073 allActions.add(caa);
074 }
075
076 return allActions;
077 }
078 catch (IllegalStateException e) {
079 throw new JPAExecutorException(ErrorCode.E0601, e.getMessage(), e);
080 }
081 }
082
083 private CoordinatorActionBean getBeanForCoordinatorActionFromArrayForRecovery(Object[] arr) {
084 CoordinatorActionBean bean = new CoordinatorActionBean();
085 if (arr[0] != null) {
086 bean.setId((String) arr[0]);
087 }
088 if (arr[1] != null){
089 bean.setJobId((String) arr[1]);
090 }
091 if (arr[2] != null) {
092 bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[2]));
093 }
094 if (arr[3] != null) {
095 bean.setExternalId((String) arr[3]);
096 }
097 if (arr[4] != null) {
098 bean.setPending((Integer) arr[4]);
099 }
100 return bean;
101 }
102
103
104 private CoordinatorActionBean getBeanForCoordinatorActionFromArrayForWaiting(Object[] arr){
105 CoordinatorActionBean bean = new CoordinatorActionBean();
106 if (arr[0] != null) {
107 bean.setId((String) arr[0]);
108 }
109 if (arr[1] != null){
110 bean.setJobId((String) arr[1]);
111 }
112 if (arr[2] != null) {
113 bean.setStatus(CoordinatorAction.Status.valueOf((String) arr[2]));
114 }
115 if (arr[3] != null) {
116 bean.setExternalId((String) arr[3]);
117 }
118 if (arr[4] != null) {
119 bean.setPushMissingDependenciesBlob((StringBlob) arr[4]);
120 }
121 return bean;
122 }
123
124 }