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    package org.apache.oozie.util;
019    
020    import org.apache.hadoop.conf.Configuration;
021    import org.apache.oozie.CoordinatorJobBean;
022    import org.apache.oozie.client.Job;
023    import org.apache.oozie.service.SchemaService;
024    import org.apache.oozie.service.Services;
025    import org.apache.oozie.service.StatusTransitService;
026    
027    public class StatusUtils {
028    
029        /**
030         * This Function transforms the statuses based on the name space of the coordinator App
031         *
032         * @param coordJob This will be the coordinator job bean for which we need to get the status based on version
033         * @return Job.Status This would be the new status based on the app version.
034         */
035        public static Job.Status getStatus(CoordinatorJobBean coordJob) {
036            Job.Status newStatus = null;
037            if (coordJob != null) {
038                newStatus = coordJob.getStatus();
039                Configuration conf = Services.get().getConf();
040                boolean backwardSupportForCoordStatus = conf.getBoolean(
041                        StatusTransitService.CONF_BACKWARD_SUPPORT_FOR_COORD_STATUS, false);
042                if (backwardSupportForCoordStatus) {
043                    if (coordJob.getAppNamespace() != null
044                            && coordJob.getAppNamespace().equals(SchemaService.COORDINATOR_NAMESPACE_URI_1)) {
045    
046                        if (coordJob.getStatus() == Job.Status.DONEWITHERROR) {
047                            newStatus = Job.Status.SUCCEEDED;
048                        }
049                        else if (coordJob.getStatus() == Job.Status.PAUSED) {
050                            newStatus = Job.Status.RUNNING;
051                        }
052                        else if (coordJob.getStatus() == Job.Status.RUNNING && coordJob.isDoneMaterialization()) {
053                            newStatus = Job.Status.SUCCEEDED;
054                        }
055                        else if (coordJob.getStatus() == Job.Status.PREPSUSPENDED) {
056                            newStatus = Job.Status.SUSPENDED;
057                        }
058                        else if (coordJob.getStatus() == Job.Status.PREPPAUSED) {
059                            newStatus = Job.Status.PREP;
060                        }
061                    }
062                }
063            }
064            return newStatus;
065        }
066    
067        /**
068         * This function changes back the status for coordinator rerun if the job was SUCCEEDED or SUSPENDED when rerun
069         * with backward support is true.
070         *
071         * @param coordJob This will be the coordinator job bean for which we need to get the status based on version
072         * @param prevStatus coordinator job previous status
073         * @return Job.Status This would be the new status based on the app version.
074         */
075        public static Job.Status getStatusForCoordRerun(CoordinatorJobBean coordJob, Job.Status prevStatus) {
076            Job.Status newStatus = null;
077            if (coordJob != null) {
078                newStatus = coordJob.getStatus();
079                Configuration conf = Services.get().getConf();
080                boolean backwardSupportForCoordStatus = conf.getBoolean(
081                        StatusTransitService.CONF_BACKWARD_SUPPORT_FOR_COORD_STATUS, false);
082                if (backwardSupportForCoordStatus) {
083                    if (coordJob.getAppNamespace() != null
084                            && coordJob.getAppNamespace().equals(SchemaService.COORDINATOR_NAMESPACE_URI_1)) {
085    
086                        if (prevStatus == Job.Status.SUSPENDED) {
087                            newStatus = Job.Status.SUSPENDED;
088                        }
089                        else if (coordJob.isDoneMaterialization() || prevStatus == Job.Status.SUCCEEDED) {
090                            newStatus = Job.Status.SUCCEEDED;
091                            coordJob.setDoneMaterialization();
092                        }
093                    }
094                }
095            }
096            return newStatus;
097        }
098    
099        /**
100         * This function check if eligible to do action input check  when running with backward support is true.
101         *
102         * @param coordJob This will be the coordinator job bean for which we need to get the status based on version
103         * @return true if eligible to do action input check
104         */
105        public static boolean getStatusForCoordActionInputCheck(CoordinatorJobBean coordJob) {
106            boolean ret = false;
107            if (coordJob != null) {
108                Configuration conf = Services.get().getConf();
109                boolean backwardSupportForCoordStatus = conf.getBoolean(
110                        StatusTransitService.CONF_BACKWARD_SUPPORT_FOR_COORD_STATUS, false);
111                if (backwardSupportForCoordStatus) {
112                    if (coordJob.getAppNamespace() != null
113                            && coordJob.getAppNamespace().equals(SchemaService.COORDINATOR_NAMESPACE_URI_1)) {
114    
115                        if (coordJob.getStatus() == Job.Status.SUCCEEDED) {
116                            ret = true;
117                        }
118                        else if (coordJob.getStatus() == Job.Status.SUSPENDED) {
119                            ret = true;
120                        }
121                    }
122                }
123            }
124            return ret;
125        }
126    
127        /**
128         * If namespace 0.1 is used and backward support is true, SUCCEEDED coord job can be killed
129         *
130         * @param coordJob the coordinator job
131         * @return true if namespace 0.1 is used and backward support is true, SUCCEEDED coord job can be killed
132         */
133        public static boolean isV1CoordjobKillable(CoordinatorJobBean coordJob) {
134            boolean ret = false;
135            if (coordJob != null) {
136                Configuration conf = Services.get().getConf();
137                boolean backwardSupportForCoordStatus = conf.getBoolean(
138                        StatusTransitService.CONF_BACKWARD_SUPPORT_FOR_COORD_STATUS, false);
139                if (backwardSupportForCoordStatus) {
140                    if (coordJob.getAppNamespace() != null
141                            && coordJob.getAppNamespace().equals(SchemaService.COORDINATOR_NAMESPACE_URI_1)) {
142                        if (coordJob.getStatus() == Job.Status.SUCCEEDED) {
143                            ret = true;
144                        }
145                    }
146                }
147            }
148            return ret;
149        }
150    }