Sentinel support. #30
parent
7abe00e911
commit
56254719ff
@ -0,0 +1,11 @@
|
|||||||
|
package com.lambdaworks.redis;
|
||||||
|
|
||||||
|
public class RedisConnectionException extends RedisException {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 4007817232147176510L;
|
||||||
|
|
||||||
|
public RedisConnectionException(String msg, Throwable e) {
|
||||||
|
super(msg, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
// Copyright (C) 2011 - Will Glozer. All rights reserved.
|
||||||
|
|
||||||
|
package com.lambdaworks.redis.output;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.lambdaworks.redis.codec.RedisCodec;
|
||||||
|
import com.lambdaworks.redis.protocol.CommandOutput;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link Map} of keys and values output.
|
||||||
|
*
|
||||||
|
* @param <K> Key type.
|
||||||
|
* @param <V> Value type.
|
||||||
|
*
|
||||||
|
* @author Will Glozer
|
||||||
|
*/
|
||||||
|
public class ListMapOutput<K, V> extends CommandOutput<K, V, List<Map<K, V>>> {
|
||||||
|
private K key;
|
||||||
|
private int index = 0;
|
||||||
|
|
||||||
|
public ListMapOutput(RedisCodec<K, V> codec) {
|
||||||
|
super(codec, new ArrayList<Map<K, V>>());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(ByteBuffer bytes) {
|
||||||
|
if (key == null) {
|
||||||
|
key = codec.decodeMapKey(bytes);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
V value = (bytes == null) ? null : codec.decodeMapValue(bytes);
|
||||||
|
if (output.isEmpty()) {
|
||||||
|
output.add(new HashMap<K, V>());
|
||||||
|
}
|
||||||
|
Map<K, V> map = output.get(index);
|
||||||
|
if (map == null) {
|
||||||
|
map = new HashMap<K, V>();
|
||||||
|
output.add(map);
|
||||||
|
}
|
||||||
|
if (map.get(key) != null) {
|
||||||
|
index++;
|
||||||
|
map = new HashMap<K, V>();
|
||||||
|
output.add(map);
|
||||||
|
}
|
||||||
|
map.put(key, value);
|
||||||
|
key = null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.redisson;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SentinelConnectionConfig {
|
||||||
|
|
||||||
|
private List<URI> sentinelAddresses = new ArrayList<URI>();
|
||||||
|
|
||||||
|
private String masterName;
|
||||||
|
|
||||||
|
public SentinelConnectionConfig() {
|
||||||
|
}
|
||||||
|
|
||||||
|
SentinelConnectionConfig(SentinelConnectionConfig config) {
|
||||||
|
setSentinelAddresses(config.getSentinelAddresses());
|
||||||
|
setMasterName(config.getMasterName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public SentinelConnectionConfig setMasterName(String masterName) {
|
||||||
|
this.masterName = masterName;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public String getMasterName() {
|
||||||
|
return masterName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SentinelConnectionConfig addSentinelAddress(String ... addresses) {
|
||||||
|
for (String address : addresses) {
|
||||||
|
try {
|
||||||
|
sentinelAddresses.add(new URI("//" + address));
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
throw new IllegalArgumentException("Can't parse " + address);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public List<URI> getSentinelAddresses() {
|
||||||
|
return sentinelAddresses;
|
||||||
|
}
|
||||||
|
void setSentinelAddresses(List<URI> sentinelAddresses) {
|
||||||
|
this.sentinelAddresses = sentinelAddresses;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.redisson.codec;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
import com.lambdaworks.redis.codec.Utf8StringCodec;
|
||||||
|
|
||||||
|
public class StringCodec implements RedissonCodec {
|
||||||
|
|
||||||
|
private final Utf8StringCodec codec = new Utf8StringCodec();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object decodeKey(ByteBuffer bytes) {
|
||||||
|
return codec.decodeKey(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object decodeValue(ByteBuffer bytes) {
|
||||||
|
return codec.decodeValue(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] encodeKey(Object key) {
|
||||||
|
return codec.encodeKey((String)key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] encodeValue(Object value) {
|
||||||
|
return codec.encodeValue((String)value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] encodeMapValue(Object value) {
|
||||||
|
return codec.encodeMapValue((String)value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] encodeMapKey(Object key) {
|
||||||
|
return codec.encodeMapKey((String)key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object decodeMapValue(ByteBuffer bytes) {
|
||||||
|
return codec.decodeMapValue(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object decodeMapKey(ByteBuffer bytes) {
|
||||||
|
return codec.decodeMapKey(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,124 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.redisson.connection;
|
||||||
|
|
||||||
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
import org.redisson.Config;
|
||||||
|
import org.redisson.MasterSlaveConnectionConfig;
|
||||||
|
import org.redisson.Redisson;
|
||||||
|
import org.redisson.SentinelConnectionConfig;
|
||||||
|
import org.redisson.codec.StringCodec;
|
||||||
|
import org.redisson.core.MessageListener;
|
||||||
|
import org.redisson.core.RTopic;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.lambdaworks.redis.RedisAsyncConnection;
|
||||||
|
import com.lambdaworks.redis.RedisClient;
|
||||||
|
|
||||||
|
public class SentinelConnectionManager extends MasterSlaveConnectionManager {
|
||||||
|
|
||||||
|
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
|
private final List<Redisson> sentinels = new ArrayList<Redisson>();
|
||||||
|
|
||||||
|
public SentinelConnectionManager(final SentinelConnectionConfig cfg, Config config) {
|
||||||
|
init(cfg, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init(final SentinelConnectionConfig cfg, final Config config) {
|
||||||
|
final MasterSlaveConnectionConfig c = new MasterSlaveConnectionConfig();
|
||||||
|
for (URI addr : cfg.getSentinelAddresses()) {
|
||||||
|
RedisClient client = new RedisClient(new NioEventLoopGroup(1), addr.getHost(), addr.getPort());
|
||||||
|
RedisAsyncConnection<String, String> connection = client.connectAsync();
|
||||||
|
|
||||||
|
// TODO async
|
||||||
|
List<String> master = connection.getMasterAddrByKey(cfg.getMasterName()).awaitUninterruptibly().getNow();
|
||||||
|
String masterHost = master.get(0) + ":" + master.get(1);
|
||||||
|
c.setMasterAddress(masterHost);
|
||||||
|
log.info("master: {}", masterHost);
|
||||||
|
|
||||||
|
// TODO async
|
||||||
|
List<Map<String, String>> slaves = connection.slaves(cfg.getMasterName()).awaitUninterruptibly().getNow();
|
||||||
|
for (Map<String, String> map : slaves) {
|
||||||
|
String ip = map.get("ip");
|
||||||
|
String port = map.get("port");
|
||||||
|
log.info("slave: {}:{}", ip, port);
|
||||||
|
c.addSlaveAddress(ip + ":" + port);
|
||||||
|
}
|
||||||
|
if (slaves.isEmpty()) {
|
||||||
|
log.info("master added as slave");
|
||||||
|
c.addSlaveAddress(masterHost);
|
||||||
|
}
|
||||||
|
|
||||||
|
client.shutdown();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
final AtomicReference<String> master = new AtomicReference<String>();
|
||||||
|
for (final URI addr : cfg.getSentinelAddresses()) {
|
||||||
|
Config sc = new Config();
|
||||||
|
sc.setCodec(new StringCodec());
|
||||||
|
sc.useSingleConnection().setAddress(addr.getHost() + ":" + addr.getPort());
|
||||||
|
Redisson r = Redisson.create(sc);
|
||||||
|
sentinels.add(r);
|
||||||
|
|
||||||
|
final RTopic<String> t = r.getTopic("+switch-master");
|
||||||
|
t.addListener(new MessageListener<String>() {
|
||||||
|
@Override
|
||||||
|
public void onMessage(String msg) {
|
||||||
|
String[] parts = msg.split(" ");
|
||||||
|
|
||||||
|
if (parts.length > 3) {
|
||||||
|
if (cfg.getMasterName().equals(parts[0])) {
|
||||||
|
String ip = parts[3];
|
||||||
|
String port = parts[4];
|
||||||
|
|
||||||
|
String current = master.get();
|
||||||
|
String newMaster = ip + ":" + port;
|
||||||
|
if (!newMaster.equals(current)
|
||||||
|
&& master.compareAndSet(current, newMaster)) {
|
||||||
|
log.debug("changing master to {}:{}", ip, port);
|
||||||
|
changeMaster(ip, Integer.valueOf(port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.error("Invalid message: {} from Sentinel({}:{}) on channel {}", msg, addr.getHost(), addr.getPort(), t.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
init(c, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void shutdown() {
|
||||||
|
for (Redisson sentinel : sentinels) {
|
||||||
|
sentinel.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
super.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,40 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
Copyright 2012 Nikita Koksharov
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
|
||||||
|
-->
|
||||||
|
<configuration>
|
||||||
|
|
||||||
|
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{yyyy.MM.dd HH:mm:ss.SSS} %-5level %c{0} : %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="org.redisson" additivity="true">
|
||||||
|
<level value="trace"/>
|
||||||
|
</logger>
|
||||||
|
|
||||||
|
<logger name="org.jboss.netty" additivity="true">
|
||||||
|
<level value="debug"/>
|
||||||
|
</logger>
|
||||||
|
|
||||||
|
<root>
|
||||||
|
<level value="debug"/>
|
||||||
|
<appender-ref ref="CONSOLE"/>
|
||||||
|
</root>
|
||||||
|
|
||||||
|
</configuration>
|
Loading…
Reference in New Issue