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