001/*******************************************************************************
002 * Copyright (c) 2006-2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 ******************************************************************************/
009package org.nuxeo.ecm.core.redis;
010
011import java.util.HashSet;
012import java.util.Set;
013
014import org.nuxeo.common.xmap.annotation.XNode;
015import org.nuxeo.common.xmap.annotation.XNodeList;
016import org.nuxeo.common.xmap.annotation.XObject;
017
018import redis.clients.jedis.JedisPoolConfig;
019import redis.clients.jedis.JedisSentinelPool;
020
021@XObject("sentinel")
022public class RedisSentinelDescriptor extends RedisPoolDescriptor {
023
024    @XNodeList(value = "host", type = RedisHostDescriptor[].class, componentType = RedisHostDescriptor.class)
025    public RedisHostDescriptor[] hosts = new RedisHostDescriptor[0];
026
027    @XNode("master")
028    public String master = "master";
029
030    @XNode("failoverTimeout")
031    public int failoverTimeout = 300;
032
033    @Override
034    public RedisExecutor newExecutor() throws RuntimeException {
035        RedisExecutor base = new RedisPoolExecutor(new JedisSentinelPool(master, toSentinels(hosts),
036                new JedisPoolConfig(), timeout, password, database));
037        return new RedisFailoverExecutor(failoverTimeout, base);
038    }
039
040    protected Set<String> toSentinels(RedisHostDescriptor[] hosts) {
041        Set<String> sentinels = new HashSet<String>();
042        for (RedisHostDescriptor host : hosts) {
043            sentinels.add(host.name + ":" + host.port);
044        }
045        return sentinels;
046    }
047}