Skip to content

Commit

Permalink
set minslaves config rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
wenchaomeng committed Jan 22, 2017
1 parent 0e9b3b4 commit 80f7cb9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ctrip.xpipe.api.command.Command;
import com.ctrip.xpipe.api.pool.SimpleObjectPool;
import com.ctrip.xpipe.netty.commands.NettyClient;
import com.ctrip.xpipe.pool.XpipeNettyClientKeyedObjectPool;
import com.ctrip.xpipe.redis.core.protocal.cmd.ConfigRewrite;
import com.ctrip.xpipe.redis.core.protocal.cmd.ConfigSetCommand.ConfigSetMinSlavesToWrite;
import com.ctrip.xpipe.redis.core.protocal.cmd.transaction.TransactionalCommand;
import com.ctrip.xpipe.redis.meta.server.dcchange.RedisReadonly;

/**
Expand All @@ -19,9 +24,9 @@ public class MinSlavesRedisReadOnly implements RedisReadonly{

private Logger logger = LoggerFactory.getLogger(MinSlavesRedisReadOnly.class);

private int READ_ONLY_NUMBER = Integer.MAX_VALUE;
public static int READ_ONLY_NUMBER = Integer.MAX_VALUE;

private int WRITABLE_NUMBER = 0;
public static int WRITABLE_NUMBER = 0;

private String ip;

Expand All @@ -41,18 +46,27 @@ public MinSlavesRedisReadOnly(String ip, int port, XpipeNettyClientKeyedObjectPo
@Override
public void makeReadOnly() throws Exception {

ConfigSetMinSlavesToWrite command = new ConfigSetMinSlavesToWrite(keyedObjectPool.getKeyPool(new InetSocketAddress(ip, port)), READ_ONLY_NUMBER, scheduled);
Boolean result = command.execute().get();
logger.info("[makeReadOnly]{}:{}, {}", ip, port, result);
Command<Object[]> command = createTransactionalCommand(READ_ONLY_NUMBER);
Object []result = command.execute().get();

logger.info("[makeReadOnly]{}:{}, {}", ip, port, (Object)result);

}

private Command<Object[]> createTransactionalCommand(int number){

SimpleObjectPool<NettyClient> clientPool = keyedObjectPool.getKeyPool(new InetSocketAddress(ip, port));
ConfigSetMinSlavesToWrite configSetMinSlavesToWrite = new ConfigSetMinSlavesToWrite(null, number, scheduled);

return new TransactionalCommand(clientPool, scheduled, configSetMinSlavesToWrite, new ConfigRewrite(null, scheduled));
}

@Override
public void makeWritable() throws Exception {

ConfigSetMinSlavesToWrite command = new ConfigSetMinSlavesToWrite(keyedObjectPool.getKeyPool(new InetSocketAddress(ip, port)), WRITABLE_NUMBER, scheduled);
Boolean result = command.execute().get();
logger.info("[makeWritable]{}:{}, {}", ip, port, result);
Command<Object[]> command = createTransactionalCommand(WRITABLE_NUMBER);
Object []result = command.execute().get();
logger.info("[makeWritable]{}:{}, {}", ip, port, (Object)result);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.ctrip.xpipe.redis.meta.server.dchange.impl;

import java.net.InetSocketAddress;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.ctrip.xpipe.redis.core.protocal.cmd.ConfigGetCommand.ConfigGetMinSlavesToWrite;
import com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest;
import com.ctrip.xpipe.redis.meta.server.dcchange.impl.MinSlavesRedisReadOnly;

/**
*
* @author wenchao.meng
*
* Jan 22, 2017
*/
public class MinSlavesRedisReadOnlyTest extends AbstractMetaServerTest{

private MinSlavesRedisReadOnly minSlavesRedisReadOnly;
private String host = "localhost";
private int port = 6379;


@Before
public void beforeMinSlavesRedisReadOnlyTest() throws Exception{

minSlavesRedisReadOnly = new MinSlavesRedisReadOnly(host, port, getXpipeNettyClientKeyedObjectPool(), scheduled);
}

@Test //manual start redis at host:port
public void testMark() throws Exception{

minSlavesRedisReadOnly.makeReadOnly();
int number = new ConfigGetMinSlavesToWrite(getXpipeNettyClientKeyedObjectPool().getKeyPool(new InetSocketAddress(host, port)), scheduled).execute().get();
Assert.assertEquals(MinSlavesRedisReadOnly.READ_ONLY_NUMBER, number);

minSlavesRedisReadOnly.makeWritable();
number = new ConfigGetMinSlavesToWrite(getXpipeNettyClientKeyedObjectPool().getKeyPool(new InetSocketAddress(host, port)), scheduled).execute().get();
Assert.assertEquals(MinSlavesRedisReadOnly.WRITABLE_NUMBER, number);
}

}

0 comments on commit 80f7cb9

Please sign in to comment.