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     * @throws XException if the scope is not well formatted
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     * @param active to list only active (non-terminated) actions
083     * @return the list of Coordinator actions for the date range
084     * @throws XException if range is not well formatted or invalid
085     */
086    public static List<CoordinatorActionBean> getCoordActionsFromDateRange(String jobId, String range, boolean active)
087            throws XException {
088            String[] dateRange = range.split("::");
089            // This block checks for errors in the format of specifying date range
090            if (dateRange.length != 2) {
091                throw new XException(ErrorCode.E0308, "'" + range +
092                    "'. Date value expected on both sides of the scope resolution operator '::' to signify start and end of range");
093
094            }
095            Date start;
096            Date end;
097            try {
098            // Get the start and end dates for the range
099                start = DateUtils.parseDateOozieTZ(dateRange[0].trim());
100                end = DateUtils.parseDateOozieTZ(dateRange[1].trim());
101            }
102            catch (ParseException dx) {
103                throw new XException(ErrorCode.E0308, "Error in parsing start or end date. " + dx);
104            }
105            if (start.after(end)) {
106                throw new XException(ErrorCode.E0308, "'" + range + "'. Start date '" + start + "' is older than end date: '" + end
107                        + "'");
108            }
109            List<CoordinatorActionBean> listOfActions = getActionsFromDateRange(jobId, start, end, active);
110            return listOfActions;
111    }
112
113    /**
114     * Get the coordinator actions for a given date range
115     * @param jobId the coordinator job id
116     * @param range the date range separated by '::'
117     * @return the list of Coordinator actions for the date range
118     * @throws XException if range is not well formatted or invalid
119     */
120    public static List<String> getCoordActionIdsFromDateRange(String jobId, String range) throws XException{
121            String[] dateRange = range.split("::");
122            // This block checks for errors in the format of specifying date range
123            if (dateRange.length != 2) {
124                throw new XException(ErrorCode.E0308, "'" + range
125                  + "'. Date value expected on both sides of the scope resolution operator '::' to signify start and end of range");
126
127            }
128            Date start;
129            Date end;
130            try {
131            // Get the start and end dates for the range
132                start = DateUtils.parseDateOozieTZ(dateRange[0].trim());
133                end = DateUtils.parseDateOozieTZ(dateRange[1].trim());
134            }
135            catch (ParseException dx) {
136                throw new XException(ErrorCode.E0308, "Error in parsing start or end date. " + dx);
137            }
138            if (start.after(end)) {
139                throw new XException(ErrorCode.E0308, "'" + range + "'. Start date '" + start + "' is older than end date: '" + end
140+ "'");
141            }
142            List<CoordinatorActionBean> listOfActions = CoordActionQueryExecutor.getInstance().getList(
143                    CoordActionQuery.GET_TERMINATED_ACTIONS_FOR_DATES, jobId, start, end);
144            List<String> idsList = new ArrayList<String>();
145            for ( CoordinatorActionBean bean : listOfActions){
146                idsList.add(bean.getId());
147            }
148            return idsList;
149    }
150
151    /**
152     * Get coordinator action ids between given start and end time
153     *
154     * @param jobId coordinator job id
155     * @param start start time
156     * @param end end time
157     * @param active to list only active (non-terminated) actions
158     * @return a list of coordinator actions that correspond to the date range
159     * @throws XException if the actions can't be retrieved
160     */
161    private static List<CoordinatorActionBean> getActionsFromDateRange(String jobId, Date start, Date end,
162            boolean active) throws XException {
163        List<CoordinatorActionBean> list;
164        if (!active) {
165            list = CoordActionQueryExecutor.getInstance().getList(
166                    CoordActionQuery.GET_TERMINATED_ACTIONS_FOR_DATES, jobId, start, end);
167        }
168        else {
169            list = CoordActionQueryExecutor.getInstance().getList(
170                    CoordActionQuery.GET_ACTIVE_ACTIONS_FOR_DATES, jobId, start, end);
171        }
172        return list;
173    }
174
175    /**
176     * Gets the coordinator actions last modified date for range, if any action is running it return new date
177     *
178     * @param jobId the job id
179     * @param startAction the start action
180     * @param endAction the end action
181     * @return the coordinator actions last modified date
182     * @throws CommandException the command exception
183     */
184    public static Date getCoordActionsLastModifiedDate(String jobId, String startAction, String endAction)
185            throws CommandException {
186        JPAService jpaService = Services.get().get(JPAService.class);
187        ParamChecker.notEmpty(jobId, "jobId");
188        ParamChecker.notEmpty(startAction, "startAction");
189        ParamChecker.notEmpty(endAction, "endAction");
190
191        try {
192            long count = jpaService.execute(new CoordJobGetActionRunningCountForRangeJPAExecutor(jobId, startAction,
193                    endAction));
194            if (count == 0) {
195                return jpaService.execute(new CoordJobGetActionModifiedDateForRangeJPAExecutor(jobId, startAction, endAction));
196            }
197            else {
198                return new Date();
199            }
200        }
201        catch (JPAExecutorException je) {
202            throw new CommandException(je);
203        }
204    }
205
206}