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 */
018package org.apache.oozie.service;
019
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.oozie.util.ConfigUtils;
025import org.apache.oozie.util.Instrumentable;
026import org.apache.oozie.util.Instrumentation;
027import org.apache.oozie.util.ZKUtils;
028
029/**
030 * This Service helps coordinate other Services to prevent duplicate processing of Jobs if there are multiple Oozie Servers.  This
031 * implementation assumes that there are NO other Oozie Servers (i.e. not HA).
032 * The {@link ZKJobsConcurrencyService} provides a more meaningful implementation.
033 */
034public class JobsConcurrencyService implements Service, Instrumentable {
035
036    private static  Map<String, String> urls;
037
038    /**
039     * Initialize the jobs concurrency service
040     *
041     * @param services services instance.
042     */
043    @Override
044    public void init(Services services) throws ServiceException {
045        urls = new HashMap<String, String>();
046        urls.put(services.getConf().get(ZKUtils.OOZIE_INSTANCE_ID), ConfigUtils.getOozieEffectiveUrl());
047    }
048
049    /**
050     * Destroy the jobs concurrency service.
051     */
052    @Override
053    public void destroy() {
054    }
055
056    /**
057     * Return the public interface for the jobs concurrency services
058     *
059     * @return {@link JobsConcurrencyService}.
060     */
061    @Override
062    public Class<? extends Service> getInterface() {
063        return JobsConcurrencyService.class;
064    }
065
066    /**
067     * Instruments the jobs concurrency service.
068     *
069     * @param instr instance to instrument the jobs concurrency service to.
070     */
071    @Override
072    public void instrument(Instrumentation instr) {
073        // nothing to instrument
074    }
075
076    /**
077     * Check to see if this server is the first server.  This implementation always returns true.
078     *
079     * @return true
080     */
081    public boolean isLeader() {
082        return true;
083    }
084
085    /**
086     * Check to see if jobId should be processed by this server.  This implementation always returns true.
087     *
088     * @param jobId The jobId to check
089     * @return true
090     */
091    public boolean isJobIdForThisServer(String jobId) {
092        return true;
093    }
094
095    /**
096     * Filter out any job ids that should not be processed by this server.  This implementation always returns an unmodified list.
097     *
098     * @param ids The list of job ids to check
099     * @return ids
100     */
101    public List<String> getJobIdsForThisServer(List<String> ids) {
102        return ids;
103    }
104
105    /**
106     * Return a map of instance id to Oozie server URL.  This implementation always returns a map with a single entry where the key
107     * is the OOZIE_INSTANCE_ID env var and the value is the URL (of this Oozie server).
108     *
109     * @return A map of Oozie instance ids and URLs
110     * @throws Exception
111     */
112    public Map<String, String> getServerUrls() {
113        return urls;
114    }
115
116    /**
117     * Return a map of instance id to other Oozie servers URL in HA.  This implementation always returns a empty map.
118     *
119     * @return A map of Oozie instance ids and URLs
120     * @throws Exception
121     */
122    public Map<String, String> getOtherServerUrls() {
123        return new HashMap<String, String>();
124    }
125
126    /**
127     * Checks if rest request is for all server. This function always return
128     * false;
129     *
130     * @param params the HttpRequest param
131     * @return false.
132     */
133    public boolean isAllServerRequest(Map<String, String[]> params) {
134        return false;
135    }
136
137    /**
138     * Check if it is running in HA mode
139     * @return false
140     */
141    public boolean isHighlyAvailableMode(){
142        return false;
143    }
144}