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    package org.apache.oozie.util;
020    
021    import java.text.ParseException;
022    import java.util.ArrayList;
023    import java.util.Date;
024    import java.util.LinkedHashSet;
025    import java.util.List;
026    import java.util.Set;
027    
028    import org.apache.oozie.CoordinatorActionBean;
029    import org.apache.oozie.ErrorCode;
030    import org.apache.oozie.XException;
031    import org.apache.oozie.command.CommandException;
032    import org.apache.oozie.executor.jpa.CoordJobGetActionModifiedDateForRangeJPAExecutor;
033    import org.apache.oozie.executor.jpa.CoordJobGetActionIdsForDateRangeJPAExecutor;
034    import org.apache.oozie.executor.jpa.CoordJobGetActionRunningCountForRangeJPAExecutor;
035    import org.apache.oozie.executor.jpa.CoordJobGetActionsByDatesForKillJPAExecutor;
036    import org.apache.oozie.executor.jpa.CoordJobGetActionsForDatesJPAExecutor;
037    import org.apache.oozie.executor.jpa.JPAExecutorException;
038    import org.apache.oozie.service.JPAService;
039    import 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     */
047    public 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<String> list = null;
142                JPAService jpaService = Services.get().get(JPAService.class);
143                list = jpaService.execute(new CoordJobGetActionIdsForDateRangeJPAExecutor(jobId, start, end));
144                return list;
145        }
146    
147        /**
148         * Get coordinator action ids between given start and end time
149         *
150         * @param jobId coordinator job id
151         * @param start start time
152         * @param end end time
153         * @return a list of coordinator actions that correspond to the date range
154         */
155        private static List<CoordinatorActionBean> getActionsFromDateRange(String jobId, Date start, Date end,
156                boolean active) throws XException {
157            List<CoordinatorActionBean> list;
158            JPAService jpaService = Services.get().get(JPAService.class);
159            if (!active) {
160                list = jpaService.execute(new CoordJobGetActionsForDatesJPAExecutor(jobId, start, end));
161            }
162            else {
163                list = jpaService.execute(new CoordJobGetActionsByDatesForKillJPAExecutor(jobId, start, end));
164            }
165            return list;
166        }
167    
168        /**
169         * Gets the coordinator actions last modified date for range, if any action is running it return new date
170         *
171         * @param jobId the job id
172         * @param startAction the start action
173         * @param endAction the end action
174         * @return the coordinator actions last modified date
175         * @throws CommandException the command exception
176         */
177        public static Date getCoordActionsLastModifiedDate(String jobId, String startAction, String endAction)
178                throws CommandException {
179            JPAService jpaService = Services.get().get(JPAService.class);
180            ParamChecker.notEmpty(jobId, "jobId");
181            ParamChecker.notEmpty(startAction, "startAction");
182            ParamChecker.notEmpty(endAction, "endAction");
183    
184            try {
185                long count = jpaService.execute(new CoordJobGetActionRunningCountForRangeJPAExecutor(jobId, startAction,
186                        endAction));
187                if (count == 0) {
188                    return jpaService.execute(new CoordJobGetActionModifiedDateForRangeJPAExecutor(jobId, startAction, endAction));
189                }
190                else {
191                    return new Date();
192                }
193            }
194            catch (JPAExecutorException je) {
195                throw new CommandException(je);
196            }
197        }
198    
199    }