Search in sources :

Example 1 with MetaStore

use of com.ctrip.xpipe.redis.core.store.MetaStore in project x-pipe by ctripcorp.

the class DefaultReplicationStoreManagerTest method test.

@Test
public void test() throws Exception {
    String keeperRunid = randomKeeperRunid();
    File baseDir = new File(getTestFileDir());
    String clusterId = "cluster1";
    String shardId = "shard1";
    DefaultReplicationStoreManager mgr = (DefaultReplicationStoreManager) createReplicationStoreManager(clusterId, shardId, keeperRunid, baseDir);
    LifecycleHelper.initializeIfPossible(mgr);
    ReplicationStore currentStore = mgr.getCurrent();
    assertNull(currentStore);
    currentStore = mgr.create();
    assertEquals(clusterId, mgr.getClusterName());
    assertEquals(shardId, mgr.getShardName());
    assertEquals(currentStore, mgr.getCurrent());
    DefaultReplicationStore newCurrentStore = (DefaultReplicationStore) mgr.create();
    assertEquals(newCurrentStore, mgr.getCurrent());
    assertNotEquals(currentStore, mgr.getCurrent());
    MetaStore metaStore = newCurrentStore.getMetaStore();
    metaStore.setMasterAddress(new DefaultEndPoint("redis://127.0.0.1:6379"));
    newCurrentStore.beginRdb("masterRunid", 0, new LenEofType(100));
    ByteBuf cmdBuf = Unpooled.buffer();
    cmdBuf.writeByte(9);
    newCurrentStore.getCommandStore().appendCommands(cmdBuf);
    DefaultReplicationStoreManager mgr2 = (DefaultReplicationStoreManager) createReplicationStoreManager(clusterId, shardId, keeperRunid, baseDir);
    assertEquals(metaStore.getReplId(), mgr2.getCurrent().getMetaStore().getReplId());
    assertEquals(metaStore.beginOffset(), mgr2.getCurrent().getMetaStore().beginOffset());
    assertEquals(metaStore.getMasterAddress(), mgr2.getCurrent().getMetaStore().getMasterAddress());
    assertEquals(metaStore.beginOffset(), mgr2.getCurrent().getMetaStore().beginOffset());
}
Also used : MetaStore(com.ctrip.xpipe.redis.core.store.MetaStore) DefaultEndPoint(com.ctrip.xpipe.endpoint.DefaultEndPoint) LenEofType(com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType) ReplicationStore(com.ctrip.xpipe.redis.core.store.ReplicationStore) ByteBuf(io.netty.buffer.ByteBuf) File(java.io.File) Test(org.junit.Test) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)

Example 2 with MetaStore

use of com.ctrip.xpipe.redis.core.store.MetaStore in project x-pipe by ctripcorp.

the class InfoHandler method replication.

private void replication(boolean isDefault, boolean isAll, String section, StringBuilder sb, RedisKeeperServer redisKeeperServer) {
    if (isDefault || isAll || "replication".equalsIgnoreCase(section)) {
        ReplicationStore replicationStore = redisKeeperServer.getReplicationStore();
        long slaveReplOffset = replicationStore.getEndOffset();
        KeeperRepl keeperRepl = redisKeeperServer.getKeeperRepl();
        sb.append("# Replication" + RedisProtocol.CRLF);
        sb.append("role:" + Server.SERVER_ROLE.SLAVE + RedisProtocol.CRLF);
        sb.append(RedisProtocol.KEEPER_ROLE_PREFIX + ":" + redisKeeperServer.role() + RedisProtocol.CRLF);
        sb.append("state:" + redisKeeperServer.getRedisKeeperServerState().keeperState() + RedisProtocol.CRLF);
        RedisMaster redisMaster = redisKeeperServer.getRedisMaster();
        String masterHost = redisMaster == null ? null : redisMaster.masterEndPoint().getHost();
        Integer masterPort = redisMaster == null ? null : redisMaster.masterEndPoint().getPort();
        if (masterHost != null) {
            sb.append("master_host:" + masterHost + RedisProtocol.CRLF);
            sb.append("master_port:" + masterPort + RedisProtocol.CRLF);
            /**
             * If not report master link status as up, then sentinal is found crashed
             * when sentinel is doing slaveof new_master_ip new_master_port
             */
            sb.append("master_link_status:up" + RedisProtocol.CRLF);
        }
        /**
         * To make sure keeper is the least option to be the new master when master is down
         */
        sb.append("slave_repl_offset:" + slaveReplOffset + RedisProtocol.CRLF);
        sb.append("slave_priority:0" + RedisProtocol.CRLF);
        Set<RedisSlave> slaves = redisKeeperServer.slaves();
        sb.append("connected_slaves:" + slaves.size() + RedisProtocol.CRLF);
        int slaveIndex = 0;
        for (RedisSlave slave : slaves) {
            sb.append(String.format("slave%d:%s" + RedisProtocol.CRLF, slaveIndex, slave.info()));
            slaveIndex++;
        }
        long beginOffset = keeperRepl.getBeginOffset();
        MetaStore metaStore = replicationStore.getMetaStore();
        String replid = metaStore == null ? ReplicationStoreMeta.EMPTY_REPL_ID : metaStore.getReplId();
        String replid2 = metaStore == null ? ReplicationStoreMeta.EMPTY_REPL_ID : metaStore.getReplId2();
        long secondReplIdOffset = metaStore == null ? ReplicationStoreMeta.DEFAULT_SECOND_REPLID_OFFSET : metaStore.getSecondReplIdOffset();
        if (replid == null) {
            replid = ReplicationStoreMeta.EMPTY_REPL_ID;
        }
        if (replid2 == null) {
            replid2 = ReplicationStoreMeta.EMPTY_REPL_ID;
        }
        sb.append("master_replid:" + replid + RedisProtocol.CRLF);
        sb.append("master_replid2:" + replid2 + RedisProtocol.CRLF);
        sb.append("master_repl_offset:" + keeperRepl.getEndOffset() + RedisProtocol.CRLF);
        sb.append("second_repl_offset:" + secondReplIdOffset + RedisProtocol.CRLF);
        sb.append("repl_backlog_active:1" + RedisProtocol.CRLF);
        sb.append("repl_backlog_first_byte_offset:" + beginOffset + RedisProtocol.CRLF);
        try {
            long endOffset = keeperRepl.getEndOffset();
            sb.append("master_repl_offset:" + endOffset + RedisProtocol.CRLF);
            sb.append("repl_backlog_size:" + (endOffset - beginOffset + 1) + RedisProtocol.CRLF);
            sb.append("repl_backlog_histlen:" + (endOffset - beginOffset + 1) + RedisProtocol.CRLF);
        } catch (Throwable ex) {
            sb.append("error_message:" + ex.getMessage() + RedisProtocol.CRLF);
            logger.info("Cannot calculate end offset", ex);
        }
    }
}
Also used : MetaStore(com.ctrip.xpipe.redis.core.store.MetaStore) ReplicationStore(com.ctrip.xpipe.redis.core.store.ReplicationStore)

Aggregations

MetaStore (com.ctrip.xpipe.redis.core.store.MetaStore)2 ReplicationStore (com.ctrip.xpipe.redis.core.store.ReplicationStore)2 DefaultEndPoint (com.ctrip.xpipe.endpoint.DefaultEndPoint)1 LenEofType (com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType)1 AbstractRedisKeeperTest (com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)1 ByteBuf (io.netty.buffer.ByteBuf)1 File (java.io.File)1 Test (org.junit.Test)1