Search in sources :

Example 1 with LenEofType

use of com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType 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 LenEofType

use of com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType in project x-pipe by ctripcorp.

the class DefaultReplicationStoreTest method testInterruptedException.

@Test
public void testInterruptedException() throws IOException {
    String keeperRunid = randomKeeperRunid();
    int dataLen = 100;
    store = new DefaultReplicationStore(baseDir, new DefaultKeeperConfig(), keeperRunid, createkeeperMonitor());
    RdbStore rdbStore = store.beginRdb(randomKeeperRunid(), -1, new LenEofType(dataLen));
    rdbStore.writeRdb(Unpooled.wrappedBuffer(randomString(dataLen).getBytes()));
    rdbStore.endRdb();
    Thread.currentThread().interrupt();
    store = new DefaultReplicationStore(baseDir, new DefaultKeeperConfig(), keeperRunid, createkeeperMonitor());
    // clear interrupt
    Thread.interrupted();
    store.appendCommands(Unpooled.wrappedBuffer(randomString(dataLen).getBytes()));
    store = new DefaultReplicationStore(baseDir, new DefaultKeeperConfig(), keeperRunid, createkeeperMonitor());
}
Also used : RdbStore(com.ctrip.xpipe.redis.core.store.RdbStore) DefaultKeeperConfig(com.ctrip.xpipe.redis.keeper.config.DefaultKeeperConfig) LenEofType(com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType) Test(org.junit.Test) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)

Example 3 with LenEofType

use of com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType in project x-pipe by ctripcorp.

the class PsyncTest method testPsync2Spaces.

@Test
public void testPsync2Spaces() throws XpipeException, IOException, InterruptedException {
    isPartial = true;
    String newReplId = RunidGenerator.DEFAULT.generateRunid();
    String[] data = new String[] { "+" + DefaultPsync.PARTIAL_SYNC + "   " + newReplId + "  \r\n", commandContent };
    // create store
    replicationStore.beginRdb(masterId, masterOffset, new LenEofType(0));
    replicationStore.getRdbStore().endRdb();
    Long secondReplIdOffset = replicationStore.getEndOffset() + 1;
    runData(data);
    Assert.assertEquals(newReplId, replicationStore.getMetaStore().getReplId());
    Assert.assertEquals(masterId, replicationStore.getMetaStore().getReplId2());
    Assert.assertEquals(secondReplIdOffset, replicationStore.getMetaStore().getSecondReplIdOffset());
}
Also used : LenEofType(com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType) Test(org.junit.Test) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)

Example 4 with LenEofType

use of com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType in project x-pipe by ctripcorp.

the class PsyncTest method testPsync2SplitTcp.

@Test
public void testPsync2SplitTcp() throws XpipeException, IOException, InterruptedException {
    isPartial = true;
    String newReplId = RunidGenerator.DEFAULT.generateRunid();
    String[] data = new String[] { "+" + DefaultPsync.PARTIAL_SYNC, "  " + newReplId + "  \r\n", commandContent };
    // create store
    replicationStore.beginRdb(masterId, masterOffset, new LenEofType(0));
    replicationStore.getRdbStore().endRdb();
    Long secondReplIdOffset = replicationStore.getEndOffset() + 1;
    runData(data);
    Assert.assertEquals(newReplId, replicationStore.getMetaStore().getReplId());
    Assert.assertEquals(masterId, replicationStore.getMetaStore().getReplId2());
    Assert.assertEquals(secondReplIdOffset, replicationStore.getMetaStore().getSecondReplIdOffset());
}
Also used : LenEofType(com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType) Test(org.junit.Test) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)

Example 5 with LenEofType

use of com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType in project x-pipe by ctripcorp.

the class DefaultRdbStore method createControllableFile.

private ControllableFile createControllableFile() throws IOException {
    if (eofType instanceof LenEofType) {
        return new DefaultControllableFile(file);
    } else if (eofType instanceof EofMarkType) {
        return new SizeControllableFile(file, new FileSize() {

            @Override
            public long getSize(LongSupplier realSizeProvider) {
                long realSize = 0;
                synchronized (truncateLock) {
                    // truncate may make size wrong
                    realSize = realSizeProvider.getAsLong();
                }
                if (status.get() == Status.Writing) {
                    long ret = realSize - ((EofMarkType) eofType).getTag().length();
                    logger.debug("[getSize][writing]{}, {}", DefaultRdbStore.this, ret);
                    return ret < 0 ? 0 : ret;
                }
                return realSize;
            }
        });
    } else {
        throw new IllegalStateException("unknown eoftype:" + eofType.getClass() + "," + eofType);
    }
}
Also used : DefaultControllableFile(com.ctrip.xpipe.utils.DefaultControllableFile) SizeControllableFile(com.ctrip.xpipe.utils.SizeControllableFile) EofMarkType(com.ctrip.xpipe.redis.core.protocal.protocal.EofMarkType) LenEofType(com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType) LongSupplier(java.util.function.LongSupplier) FileSize(com.ctrip.xpipe.api.utils.FileSize)

Aggregations

LenEofType (com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType)11 AbstractRedisKeeperTest (com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)9 Test (org.junit.Test)9 DefaultKeeperConfig (com.ctrip.xpipe.redis.keeper.config.DefaultKeeperConfig)3 RdbStore (com.ctrip.xpipe.redis.core.store.RdbStore)2 ByteBuf (io.netty.buffer.ByteBuf)2 File (java.io.File)2 FileSize (com.ctrip.xpipe.api.utils.FileSize)1 AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)1 DefaultEndPoint (com.ctrip.xpipe.endpoint.DefaultEndPoint)1 ReferenceFileRegion (com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion)1 EofMarkType (com.ctrip.xpipe.redis.core.protocal.protocal.EofMarkType)1 EofType (com.ctrip.xpipe.redis.core.protocal.protocal.EofType)1 FullSyncListener (com.ctrip.xpipe.redis.core.store.FullSyncListener)1 MetaStore (com.ctrip.xpipe.redis.core.store.MetaStore)1 ReplicationStore (com.ctrip.xpipe.redis.core.store.ReplicationStore)1 DefaultControllableFile (com.ctrip.xpipe.utils.DefaultControllableFile)1 SizeControllableFile (com.ctrip.xpipe.utils.SizeControllableFile)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1