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
019
020package org.apache.oozie.util;
021
022import java.text.ParseException;
023import java.util.ArrayList;
024import java.util.Date;
025import java.util.LinkedHashSet;
026import java.util.List;
027import java.util.Set;
028
029import org.apache.oozie.CoordinatorActionBean;
030import org.apache.oozie.ErrorCode;
031import org.apache.oozie.XException;
032import org.apache.oozie.command.CommandException;
033import org.apache.oozie.executor.jpa.CoordActionQueryExecutor;
034import org.apache.oozie.executor.jpa.CoordJobGetActionModifiedDateForRangeJPAExecutor;
035import org.apache.oozie.executor.jpa.CoordJobGetActionRunningCountForRangeJPAExecutor;
036import org.apache.oozie.executor.jpa.JPAExecutorException;
037import org.apache.oozie.executor.jpa.CoordActionQueryExecutor.CoordActionQuery;
038import org.apache.oozie.service.JPAService;
039import org.apache.oozie.service.Services;
040
041/**
042 * This class provides the utility of listing
043 * coordinator actions that were executed between a certain
044 * date range. This is helpful in turn for retrieving the
045 * required logs in that date range.
046 */
047public class CoordActionsInDateRange {
048
049    /**
050     * Get the list of Coordinator action Ids for given date ranges
051     *
052     * @param jobId coordinator job id
053     * @param scope the date range for log. format is comma-separated list of date ranges.
054     * Each date range element is specified with two dates separated by '::'
055     * @return the list of coordinator action Ids for the date range
056     *
057     * Internally involves a database operation by invoking method 'getActionIdsFromDateRange'.
058     */
059    public static List<String> getCoordActionIdsFromDates(String jobId, String scope) throws XException {
060        ParamChecker.notEmpty(jobId, "jobId");
061        ParamChecker.notEmpty(scope, "scope");
062        // Use an ordered set to achieve reproducible behavior.
063        Set<String> actionSet = new LinkedHashSet<String>();
064        String[] list = scope.split(",");
065        for (String s : list) {
066            s = s.trim();
067            if (s.contains("::")) {
068                List<String> listOfActions = getCoordActionIdsFromDateRange(jobId, s);
069                actionSet.addAll(listOfActions);
070            }
071            else {
072                throw new XException(ErrorCode.E0308, "'" + s + "'. Separator '::' is missing for start and end dates of range");
073            }
074        }
075        return new ArrayList<String>(actionSet);
076    }
077
078    /**
079     * Get the coordinator actions for a given date range
080     * @param jobId the coordinator job id
081     * @param range the date range separated by '::'
082     * @return the list of Coordinator actions for the date range
083     * @throws XException
084     */
085    public static List<CoordinatorActionBean> getCoordActionsFromDateRange(String jobId, String range, boolean active)
086            throws XException {
087            String[] dateRange = range.split("::");
088            // This block checks for errors in the format of specifying date range
089            if (dateRange.length != 2) {
090                throw new XException(ErrorCode.E0308, "'" + range +
091                    "'. Date value expected on both sides of the scope resolution operator '::' to signify start and end of range");
092
093            }
094            Date start;
095            Date end;
096            try {
097            // Get the start and end dates for the range
098                start = DateUtils.parseDateOozieTZ(dateRange[0].trim());
099                end = DateUtils.parseDateOozieTZ(dateRange[1].trim());
100            }
101            catch (ParseException dx) {
102                throw new XException(ErrorCode.E0308, "Error in parsing start or end date. " + dx);
103            }
104            if (start.after(end)) {
105                throw new XException(ErrorCode.E0308, "'" + range + "'. Start date '" + start + "' is older than end date: '" + end
106                        + "'");
107            }
108            List<CoordinatorActionBean> listOfActions = getActionsFromDateRange(jobId, start, end, active);
109            return listOfActions;
110    }
111
112    /**
113     * Get the coordinator actions for a given date range
114     * @param jobId the coordinator job id
115     * @param range the date range separated by '::'
116     * @return the list of Coordinator actions for the date range
117     * @throws XException
118     */
119    public static List<String> getCoordActionIdsFromDateRange(String jobId, String range) throws XException{
120            String[] dateRange = range.split("::");
121            // This block checks for errors in the format of specifying date range
122            if (dateRange.length != 2) {
123                throw new XException(ErrorCode.E0308, "'" + range
124                  + "'. Date value expected on both sides of the scope resolution operator '::' to signify start and end of range");
125
126            }
127            Date start;
128            Date end;
129            try {
130            // Get the start and end dates for the range
131                start = DateUtils.parseDateOozieTZ(dateRange[0].trim());
132                end = DateUtils.parseDateOozieTZ(dateRange[1].trim());
133            }
134            catch (ParseException dx) {
135                throw new XException(ErrorCode.E0308, "Error in parsing start or end date. " + dx);
136            }
137            if (start.after(end)) {
138                throw new XException(ErrorCode.E0308, "'" + range + "'. Start date '" + start + "' is older than end date: '" + end
139+ "'");
140            }
141            List<CoordinatorActionBean> listOfActions = CoordActionQueryExecutor.getInstance().getList(
142                    CoordActionQuery.GET_TERMINATED_ACTIONS_FOR_DATES, jobId, start, end);
143            List<String> idsList = new ArrayList<String>();
144            for ( CoordinatorActionBean bean : listOfActions){
145                idsList.add(bean.getId());
146            }
147            return idsList;
148    }
149
150    /**
151     * Get coordinator action ids between given start and end time
152     *
153     * @param jobId coordinator job id
154     * @param start start time
155     * @param end end time
156     * @return a list of coordinator actions that correspond to the date range
157     */
158    private static List<CoordinatorActionBean> getActionsFromDateRange(String jobId, Date start, Date end,
159            boolean active) throws XException {
160        List<CoordinatorActionBean> list;
161        if (!active) {
162            list = CoordActionQueryExecutor.getInstance().getList(
163                    CoordActionQuery.GET_TERMINATED_ACTIONS_FOR_DATES, jobId, start, end);
164        }
165        else {
166            list = CoordActionQueryExecutor.getInstance().getList(
167                    CoordActionQuery.GET_ACTIVE_ACTIONS_FOR_DATES, jobId, start, end);
168        }
169        return list;
170    }
171
172    /**
173     * Gets the coordinator actions last modified date for range, if any action is running it return new date
174     *
175     * @param jobId the job id
176     * @param startAction the start action
177     * @param endAction the end action
178     * @return the coordinator actions last modified date
179     * @throws CommandException the command exception
180     */
181    public static Date getCoordActionsLastModifiedDate(String jobId, String startAction, String endAction)
182            throws CommandException {
183        JPAService jpaService = Services.get().get(JPAService.class);
184        ParamChecker.notEmpty(jobId, "jobId");
185        ParamChecker.notEmpty(startAction, "startAction");
186        ParamChecker.notEmpty(endAction, "endAction");
187
188        try {
189            long count = jpaService.execute(new CoordJobGetActionRunningCountForRangeJPAExecutor(jobId, startAction,
190                    endAction));
191            if (count == 0) {
192                return jpaService.execute(new CoordJobGetActionModifiedDateForRangeJPAExecutor(jobId, startAction, endAction));
193            }
194            else {
195                return new Date();
196            }
197        }
198        catch (JPAExecutorException je) {
199            throw new CommandException(je);
200        }
201    }
202
203}