001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.util;
016    
017    import org.apache.hadoop.conf.Configuration;
018    import org.apache.oozie.CoordinatorJobBean;
019    import org.apache.oozie.client.Job;
020    import org.apache.oozie.service.SchemaService;
021    import org.apache.oozie.service.Services;
022    import org.apache.oozie.service.StatusTransitService;
023    
024    public class StatusUtils {
025    
026        /**
027         * This Function transforms the statuses based on the name space of the coordinator App
028         *
029         * @param coordJob This will be the coordinator job bean for which we need to get the status based on version
030         * @return Job.Status This would be the new status based on the app version.
031         */
032        public static Job.Status getStatus(CoordinatorJobBean coordJob) {
033            Job.Status newStatus = null;
034            if (coordJob != null) {
035                newStatus = coordJob.getStatus();
036                Configuration conf = Services.get().getConf();
037                boolean backwardSupportForCoordStatus = conf.getBoolean(
038                        StatusTransitService.CONF_BACKWARD_SUPPORT_FOR_COORD_STATUS, false);
039                if (backwardSupportForCoordStatus) {
040                    if (coordJob.getAppNamespace() != null
041                            && coordJob.getAppNamespace().equals(SchemaService.COORDINATOR_NAMESPACE_URI_1)) {
042    
043                        if (coordJob.getStatus() == Job.Status.DONEWITHERROR) {
044                            newStatus = Job.Status.SUCCEEDED;
045                        }
046                        else if (coordJob.getStatus() == Job.Status.PAUSED) {
047                            newStatus = Job.Status.RUNNING;
048                        }
049                        else if (coordJob.getStatus() == Job.Status.RUNNING && coordJob.isDoneMaterialization()) {
050                            newStatus = Job.Status.SUCCEEDED;
051                        }
052                        else if (coordJob.getStatus() == Job.Status.PREPSUSPENDED) {
053                            newStatus = Job.Status.SUSPENDED;
054                        }
055                        else if (coordJob.getStatus() == Job.Status.PREPPAUSED) {
056                            newStatus = Job.Status.PREP;
057                        }
058                    }
059                }
060            }
061            return newStatus;
062        }
063    
064        /**
065         * This function changes back the status for coordinator rerun if the job was SUCCEEDED or SUSPENDED when rerun
066         * with backward support is true.
067         *
068         * @param coordJob This will be the coordinator job bean for which we need to get the status based on version
069         * @param prevStatus coordinator job previous status
070         * @return Job.Status This would be the new status based on the app version.
071         */
072        public static Job.Status getStatusForCoordRerun(CoordinatorJobBean coordJob, Job.Status prevStatus) {
073            Job.Status newStatus = null;
074            if (coordJob != null) {
075                newStatus = coordJob.getStatus();
076                Configuration conf = Services.get().getConf();
077                boolean backwardSupportForCoordStatus = conf.getBoolean(
078                        StatusTransitService.CONF_BACKWARD_SUPPORT_FOR_COORD_STATUS, false);
079                if (backwardSupportForCoordStatus) {
080                    if (coordJob.getAppNamespace() != null
081                            && coordJob.getAppNamespace().equals(SchemaService.COORDINATOR_NAMESPACE_URI_1)) {
082    
083                        if (prevStatus == Job.Status.SUSPENDED) {
084                            newStatus = Job.Status.SUSPENDED;
085                        }
086                        else if (coordJob.isDoneMaterialization() || prevStatus == Job.Status.SUCCEEDED) {
087                            newStatus = Job.Status.SUCCEEDED;
088                            coordJob.setDoneMaterialization();
089                        }
090                    }
091                }
092            }
093            return newStatus;
094        }
095    
096        /**
097         * This function check if eligible to do action input check  when running with backward support is true.
098         *
099         * @param coordJob This will be the coordinator job bean for which we need to get the status based on version
100         * @return true if eligible to do action input check
101         */
102        public static boolean getStatusForCoordActionInputCheck(CoordinatorJobBean coordJob) {
103            boolean ret = false;
104            if (coordJob != null) {
105                Configuration conf = Services.get().getConf();
106                boolean backwardSupportForCoordStatus = conf.getBoolean(
107                        StatusTransitService.CONF_BACKWARD_SUPPORT_FOR_COORD_STATUS, false);
108                if (backwardSupportForCoordStatus) {
109                    if (coordJob.getAppNamespace() != null
110                            && coordJob.getAppNamespace().equals(SchemaService.COORDINATOR_NAMESPACE_URI_1)) {
111    
112                        if (coordJob.getStatus() == Job.Status.SUCCEEDED) {
113                            ret = true;
114                        }
115                        else if (coordJob.getStatus() == Job.Status.SUSPENDED) {
116                            ret = true;
117                        }
118                    }
119                }
120            }
121            return ret;
122        }
123    
124        /**
125         * If namespace 0.1 is used and backward support is true, SUCCEEDED coord job can be killed
126         *
127         * @param coordJob the coordinator job
128         * @return true if namespace 0.1 is used and backward support is true, SUCCEEDED coord job can be killed
129         */
130        public static boolean isV1CoordjobKillable(CoordinatorJobBean coordJob) {
131            boolean ret = false;
132            if (coordJob != null) {
133                Configuration conf = Services.get().getConf();
134                boolean backwardSupportForCoordStatus = conf.getBoolean(
135                        StatusTransitService.CONF_BACKWARD_SUPPORT_FOR_COORD_STATUS, false);
136                if (backwardSupportForCoordStatus) {
137                    if (coordJob.getAppNamespace() != null
138                            && coordJob.getAppNamespace().equals(SchemaService.COORDINATOR_NAMESPACE_URI_1)) {
139                        if (coordJob.getStatus() == Job.Status.SUCCEEDED) {
140                            ret = true;
141                        }
142                    }
143                }
144            }
145            return ret;
146        }
147    }