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.executor.jpa.CoordJobGetActionIdsForDateRangeJPAExecutor; 032 import org.apache.oozie.executor.jpa.CoordJobGetActionsForDatesJPAExecutor; 033 import org.apache.oozie.service.JPAService; 034 import org.apache.oozie.service.Services; 035 036 /** 037 * This class provides the utility of listing 038 * coordinator actions that were executed between a certain 039 * date range. This is helpful in turn for retrieving the 040 * required logs in that date range. 041 */ 042 public class CoordActionsInDateRange { 043 044 /** 045 * Get the list of Coordinator action Ids for given date ranges 046 * 047 * @param jobId coordinator job id 048 * @param scope the date range for log. format is comma-separated list of date ranges. 049 * Each date range element is specified with two dates separated by '::' 050 * @return the list of coordinator action Ids for the date range 051 * 052 * Internally involves a database operation by invoking method 'getActionIdsFromDateRange'. 053 */ 054 public static List<String> getCoordActionIdsFromDates(String jobId, String scope) throws XException { 055 ParamChecker.notEmpty(jobId, "jobId"); 056 ParamChecker.notEmpty(scope, "scope"); 057 // Use an ordered set to achieve reproducible behavior. 058 Set<String> actionSet = new LinkedHashSet<String>(); 059 String[] list = scope.split(","); 060 for (String s : list) { 061 s = s.trim(); 062 if (s.contains("::")) { 063 List<String> listOfActions = getCoordActionIdsFromDateRange(jobId, s); 064 actionSet.addAll(listOfActions); 065 } 066 else { 067 throw new XException(ErrorCode.E0308, "'" + s + "'. Separator '::' is missing for start and end dates of range"); 068 } 069 } 070 return new ArrayList<String>(actionSet); 071 } 072 073 /** 074 * Get the coordinator actions for a given date range 075 * @param jobId the coordinator job id 076 * @param range the date range separated by '::' 077 * @return the list of Coordinator actions for the date range 078 * @throws XException 079 */ 080 public static List<CoordinatorActionBean> getCoordActionsFromDateRange(String jobId, String range) throws XException{ 081 String[] dateRange = range.split("::"); 082 // This block checks for errors in the format of specifying date range 083 if (dateRange.length != 2) { 084 throw new XException(ErrorCode.E0308, "'" + range + 085 "'. Date value expected on both sides of the scope resolution operator '::' to signify start and end of range"); 086 087 } 088 Date start; 089 Date end; 090 try { 091 // Get the start and end dates for the range 092 start = DateUtils.parseDateOozieTZ(dateRange[0].trim()); 093 end = DateUtils.parseDateOozieTZ(dateRange[1].trim()); 094 } 095 catch (ParseException dx) { 096 throw new XException(ErrorCode.E0308, "Error in parsing start or end date. " + dx); 097 } 098 if (start.after(end)) { 099 throw new XException(ErrorCode.E0308, "'" + range + "'. Start date '" + start + "' is older than end date: '" + end 100 + "'"); 101 } 102 List<CoordinatorActionBean> listOfActions = getActionsFromDateRange(jobId, start, end); 103 return listOfActions; 104 } 105 106 /** 107 * Get the coordinator actions for a given date range 108 * @param jobId the coordinator job id 109 * @param range the date range separated by '::' 110 * @return the list of Coordinator actions for the date range 111 * @throws XException 112 */ 113 public static List<String> getCoordActionIdsFromDateRange(String jobId, String range) throws XException{ 114 String[] dateRange = range.split("::"); 115 // This block checks for errors in the format of specifying date range 116 if (dateRange.length != 2) { 117 throw new XException(ErrorCode.E0308, "'" + range 118 + "'. Date value expected on both sides of the scope resolution operator '::' to signify start and end of range"); 119 120 } 121 Date start; 122 Date end; 123 try { 124 // Get the start and end dates for the range 125 start = DateUtils.parseDateOozieTZ(dateRange[0].trim()); 126 end = DateUtils.parseDateOozieTZ(dateRange[1].trim()); 127 } 128 catch (ParseException dx) { 129 throw new XException(ErrorCode.E0308, "Error in parsing start or end date. " + dx); 130 } 131 if (start.after(end)) { 132 throw new XException(ErrorCode.E0308, "'" + range + "'. Start date '" + start + "' is older than end date: '" + end 133 + "'"); 134 } 135 List<String> list = null; 136 JPAService jpaService = Services.get().get(JPAService.class); 137 list = jpaService.execute(new CoordJobGetActionIdsForDateRangeJPAExecutor(jobId, start, end)); 138 return list; 139 } 140 141 /* 142 * Get coordinator action ids between given start and end time 143 * 144 * @param jobId coordinator job id 145 * @param start start time 146 * @param end end time 147 * @return a list of coordinator actions that correspond to the date range 148 */ 149 private static List<CoordinatorActionBean> getActionsFromDateRange(String jobId, Date start, Date end) throws XException{ 150 List<CoordinatorActionBean> list; 151 JPAService jpaService = Services.get().get(JPAService.class); 152 list = jpaService.execute(new CoordJobGetActionsForDatesJPAExecutor(jobId, start, end)); 153 return list; 154 } 155 }