001/*
002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.nuxeo.ecm.core.redis;
017
018import org.nuxeo.ecm.core.redis.retry.ExponentialBackofDelay;
019import org.nuxeo.ecm.core.redis.retry.Retry;
020import org.nuxeo.ecm.core.redis.retry.Retry.ContinueException;
021import org.nuxeo.ecm.core.redis.retry.Retry.FailException;
022
023import org.nuxeo.ecm.core.redis.retry.SimpleDelay;
024import redis.clients.jedis.Jedis;
025import redis.clients.jedis.JedisPubSub;
026import redis.clients.jedis.exceptions.JedisConnectionException;
027import redis.clients.util.Pool;
028
029public class RedisFailoverExecutor extends RedisAbstractExecutor {
030
031    protected final int timeout;
032
033    protected final RedisExecutor executor;
034
035    public RedisFailoverExecutor(int timeout, RedisExecutor base) {
036        this.timeout = timeout;
037        executor = base;
038    }
039
040    @Override
041    public <T> T execute(final RedisCallable<T> callable) throws JedisConnectionException {
042        // Retry the operation with an exponential backoff limited by a configurable timeout
043        return executeWithRetryPolicy(callable, new ExponentialBackofDelay(1, timeout));
044    }
045
046    @Override
047    public void subscribe(JedisPubSub subscriber, String channel) throws JedisConnectionException {
048        // Here it is a long running operation, we never give up retry every 2s
049        executeWithRetryPolicy(jedis -> {
050            jedis.subscribe(subscriber, channel);
051            return null;
052        }, new SimpleDelay(2000, Integer.MAX_VALUE));
053    }
054
055    protected <T> T executeWithRetryPolicy(final RedisCallable<T> callable, Retry.Policy policy) {
056        try {
057            return new Retry().retry(new Retry.Block<T>() {
058
059                @Override
060                public T retry() throws ContinueException, FailException {
061                    try {
062                        return executor.execute(callable);
063                    } catch (JedisConnectionException cause) {
064                        throw new ContinueException(cause);
065                    }
066                }
067
068            }, policy);
069        } catch (FailException cause) {
070            throw new JedisConnectionException("Cannot reconnect to jedis ..", cause);
071        }
072    }
073
074    @Override
075    public Pool<Jedis> getPool() {
076        return executor.getPool();
077    }
078
079}