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.CoordJobGetActionModifiedDateForRangeJPAExecutor; 034import org.apache.oozie.executor.jpa.CoordJobGetActionIdsForDateRangeJPAExecutor; 035import org.apache.oozie.executor.jpa.CoordJobGetActionRunningCountForRangeJPAExecutor; 036import org.apache.oozie.executor.jpa.CoordJobGetActionsByDatesForKillJPAExecutor; 037import org.apache.oozie.executor.jpa.CoordJobGetActionsForDatesJPAExecutor; 038import org.apache.oozie.executor.jpa.JPAExecutorException; 039import org.apache.oozie.service.JPAService; 040import org.apache.oozie.service.Services; 041 042/** 043 * This class provides the utility of listing 044 * coordinator actions that were executed between a certain 045 * date range. This is helpful in turn for retrieving the 046 * required logs in that date range. 047 */ 048public class CoordActionsInDateRange { 049 050 /** 051 * Get the list of Coordinator action Ids for given date ranges 052 * 053 * @param jobId coordinator job id 054 * @param scope the date range for log. format is comma-separated list of date ranges. 055 * Each date range element is specified with two dates separated by '::' 056 * @return the list of coordinator action Ids for the date range 057 * 058 * Internally involves a database operation by invoking method 'getActionIdsFromDateRange'. 059 */ 060 public static List<String> getCoordActionIdsFromDates(String jobId, String scope) throws XException { 061 ParamChecker.notEmpty(jobId, "jobId"); 062 ParamChecker.notEmpty(scope, "scope"); 063 // Use an ordered set to achieve reproducible behavior. 064 Set<String> actionSet = new LinkedHashSet<String>(); 065 String[] list = scope.split(","); 066 for (String s : list) { 067 s = s.trim(); 068 if (s.contains("::")) { 069 List<String> listOfActions = getCoordActionIdsFromDateRange(jobId, s); 070 actionSet.addAll(listOfActions); 071 } 072 else { 073 throw new XException(ErrorCode.E0308, "'" + s + "'. Separator '::' is missing for start and end dates of range"); 074 } 075 } 076 return new ArrayList<String>(actionSet); 077 } 078 079 /** 080 * Get the coordinator actions for a given date range 081 * @param jobId the coordinator job id 082 * @param range the date range separated by '::' 083 * @return the list of Coordinator actions for the date range 084 * @throws XException 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 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<String> list = null; 143 JPAService jpaService = Services.get().get(JPAService.class); 144 list = jpaService.execute(new CoordJobGetActionIdsForDateRangeJPAExecutor(jobId, start, end)); 145 return list; 146 } 147 148 /** 149 * Get coordinator action ids between given start and end time 150 * 151 * @param jobId coordinator job id 152 * @param start start time 153 * @param end end time 154 * @return a list of coordinator actions that correspond to the date range 155 */ 156 private static List<CoordinatorActionBean> getActionsFromDateRange(String jobId, Date start, Date end, 157 boolean active) throws XException { 158 List<CoordinatorActionBean> list; 159 JPAService jpaService = Services.get().get(JPAService.class); 160 if (!active) { 161 list = jpaService.execute(new CoordJobGetActionsForDatesJPAExecutor(jobId, start, end)); 162 } 163 else { 164 list = jpaService.execute(new CoordJobGetActionsByDatesForKillJPAExecutor(jobId, start, end)); 165 } 166 return list; 167 } 168 169 /** 170 * Gets the coordinator actions last modified date for range, if any action is running it return new date 171 * 172 * @param jobId the job id 173 * @param startAction the start action 174 * @param endAction the end action 175 * @return the coordinator actions last modified date 176 * @throws CommandException the command exception 177 */ 178 public static Date getCoordActionsLastModifiedDate(String jobId, String startAction, String endAction) 179 throws CommandException { 180 JPAService jpaService = Services.get().get(JPAService.class); 181 ParamChecker.notEmpty(jobId, "jobId"); 182 ParamChecker.notEmpty(startAction, "startAction"); 183 ParamChecker.notEmpty(endAction, "endAction"); 184 185 try { 186 long count = jpaService.execute(new CoordJobGetActionRunningCountForRangeJPAExecutor(jobId, startAction, 187 endAction)); 188 if (count == 0) { 189 return jpaService.execute(new CoordJobGetActionModifiedDateForRangeJPAExecutor(jobId, startAction, endAction)); 190 } 191 else { 192 return new Date(); 193 } 194 } 195 catch (JPAExecutorException je) { 196 throw new CommandException(je); 197 } 198 } 199 200}