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.event.listener;
020
021import org.apache.curator.framework.CuratorFramework;
022import org.apache.curator.framework.state.ConnectionState;
023import org.apache.curator.framework.state.ConnectionStateListener;
024import org.apache.oozie.service.ConfigurationService;
025import org.apache.oozie.service.Services;
026import org.apache.oozie.util.XLog;
027import org.apache.oozie.util.ZKUtils;
028
029/**
030 * ZKConnectionListener listens on ZK connection status.
031 */
032public class ZKConnectionListener implements ConnectionStateListener {
033
034    private XLog LOG = XLog.getLog(getClass());
035    private static ConnectionState connectionState;
036    public static final String CONF_SHUTDOWN_ON_TIMEOUT = "oozie.zookeeper.server.shutdown.ontimeout";
037
038    public ZKConnectionListener() {
039        LOG.info("ZKConnectionListener started");
040    }
041
042    @Override
043    public void stateChanged(final CuratorFramework client, final ConnectionState newState) {
044        connectionState = newState;
045        LOG.trace("ZK connection status  = " + newState.toString());
046        // if (newState == ConnectionState.CONNECTED) {
047        // ZK connected
048        // }
049        if (newState == ConnectionState.SUSPENDED) {
050            LOG.warn("ZK connection is suspended, waiting for reconnect. If connection doesn't reconnect before "
051                    + ZKUtils.getZKConnectionTimeout() + " (sec) Oozie server will shutdown itself");
052        }
053
054        if (newState == ConnectionState.RECONNECTED) {
055            // ZK connected is reconnected.
056            LOG.warn("ZK connection is reestablished");
057        }
058
059        if (newState == ConnectionState.LOST) {
060            LOG.fatal("ZK is not reconnected in " + ZKUtils.getZKConnectionTimeout());
061            if (ConfigurationService.getBoolean(CONF_SHUTDOWN_ON_TIMEOUT)) {
062                LOG.fatal("Shutting down Oozie server");
063                Services.get().destroy();
064                System.exit(1);
065            }
066        }
067    }
068
069    public static ConnectionState getZKConnectionState() {
070        return connectionState;
071    }
072}