Search in sources :

Example 11 with ReplicationStore

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

the class DefaultReplicationStoreManager method destroy.

@Override
public void destroy() throws Exception {
    logger.info("[destroy]{}", this);
    ReplicationStore currentReplicationStore = getCurrent();
    if (currentReplicationStore != null) {
        try {
            currentReplicationStore.destroy();
        } catch (Throwable th) {
            logger.error("[destroy]", th);
        }
    }
    FileUtils.recursiveDelete(this.baseDir);
}
Also used : ReplicationStore(com.ctrip.xpipe.redis.core.store.ReplicationStore)

Example 12 with ReplicationStore

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

the class DefaultReplicationStoreManager method gc.

protected synchronized void gc() throws IOException {
    logger.debug("[gc]{}", this);
    gcCount.incrementAndGet();
    Properties meta = currentMeta(true);
    final String currentDirName;
    if (meta != null) {
        currentDirName = meta.getProperty(LATEST_STORE_DIR);
        File[] replicationStoreDirs = baseDir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File path) {
                return path.isDirectory() && !currentDirName.equals(path.getName());
            }
        });
        if (replicationStoreDirs != null && replicationStoreDirs.length > 0) {
            logger.info("[GC][old replicationstore]newest:{}", currentDirName);
            for (File dir : replicationStoreDirs) {
                if (System.currentTimeMillis() - dir.lastModified() > keeperConfig.getReplicationStoreMinTimeMilliToGcAfterCreate()) {
                    logger.info("[GC] directory {}", dir.getCanonicalPath());
                    FileUtils.recursiveDelete(dir);
                } else {
                    logger.warn("[GC][directory is created too short, do not gc]{}, {}", dir, new Date(dir.lastModified()));
                }
            }
        }
    }
    // gc current ReplicationStore
    ReplicationStore replicationStore = getCurrent();
    if (replicationStore != null) {
        replicationStore.gc();
    }
}
Also used : Properties(java.util.Properties) ReplicationStore(com.ctrip.xpipe.redis.core.store.ReplicationStore) Date(java.util.Date)

Example 13 with ReplicationStore

use of com.ctrip.xpipe.redis.core.store.ReplicationStore 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)

Example 14 with ReplicationStore

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

the class RedisKeeperServerStateActive method masterChanged.

@SuppressWarnings("deprecation")
public RedisMeta masterChanged(long keeperOffset, DefaultEndPoint newMasterEndpoint, String newMasterRunid, long newMasterReplOffset) throws IOException {
    ReplicationStore replicationStore = redisKeeperServer.getReplicationStore();
    replicationStore.getMetaStore().masterChanged(keeperOffset, newMasterEndpoint, newMasterRunid, newMasterReplOffset);
    RedisMeta redisMeta = new RedisMeta();
    redisMeta.setIp(newMasterEndpoint.getHost());
    redisMeta.setPort(newMasterEndpoint.getPort());
    return redisMeta;
}
Also used : RedisMeta(com.ctrip.xpipe.redis.core.entity.RedisMeta) ReplicationStore(com.ctrip.xpipe.redis.core.store.ReplicationStore)

Example 15 with ReplicationStore

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

the class DefaultRedisKeeperServerConnectToFakeRedisTest method testReplicationData.

@Test
public void testReplicationData() throws Exception {
    RedisKeeperServer redisKeeperServer = startRedisKeeperServerAndConnectToFakeRedis();
    sleep(1500);
    logger.info(remarkableMessage("[testReplicationData][read replication store]"));
    ReplicationStore replicationStore = redisKeeperServer.getReplicationStore();
    String rdbContent = readRdbFileTilEnd(replicationStore);
    Assert.assertEquals(fakeRedisServer.getRdbContent(), rdbContent);
    String commands = readCommandFileTilEnd(replicationStore, fakeRedisServer.currentCommands().length());
    Assert.assertEquals(fakeRedisServer.currentCommands(), commands);
}
Also used : DefaultRedisKeeperServer(com.ctrip.xpipe.redis.keeper.impl.DefaultRedisKeeperServer) RedisKeeperServer(com.ctrip.xpipe.redis.keeper.RedisKeeperServer) DefaultReplicationStore(com.ctrip.xpipe.redis.keeper.store.DefaultReplicationStore) ReplicationStore(com.ctrip.xpipe.redis.core.store.ReplicationStore) Test(org.junit.Test)

Aggregations

ReplicationStore (com.ctrip.xpipe.redis.core.store.ReplicationStore)15 IOException (java.io.IOException)4 XpipeRuntimeException (com.ctrip.xpipe.exception.XpipeRuntimeException)2 MetaStore (com.ctrip.xpipe.redis.core.store.MetaStore)2 RedisKeeperServer (com.ctrip.xpipe.redis.keeper.RedisKeeperServer)2 Properties (java.util.Properties)2 Test (org.junit.Test)2 Endpoint (com.ctrip.xpipe.api.endpoint.Endpoint)1 DefaultEndPoint (com.ctrip.xpipe.endpoint.DefaultEndPoint)1 RedisMeta (com.ctrip.xpipe.redis.core.entity.RedisMeta)1 KeeperState (com.ctrip.xpipe.redis.core.meta.KeeperState)1 LenEofType (com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType)1 AbstractRedisKeeperTest (com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)1 RedisMaster (com.ctrip.xpipe.redis.keeper.RedisMaster)1 RedisSlavePromotionException (com.ctrip.xpipe.redis.keeper.exception.RedisSlavePromotionException)1 DefaultRedisKeeperServer (com.ctrip.xpipe.redis.keeper.impl.DefaultRedisKeeperServer)1 DefaultReplicationStore (com.ctrip.xpipe.redis.keeper.store.DefaultReplicationStore)1 ByteBuf (io.netty.buffer.ByteBuf)1 File (java.io.File)1 Date (java.util.Date)1