Search in sources :

Example 1 with EofType

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

the class AbstractFakeRedisTest method sendInmemoryPsync.

protected InMemoryPsync sendInmemoryPsync(String ip, int port, String runid, long offset) throws Exception {
    SequenceCommandChain chain = new SequenceCommandChain(false);
    SimpleObjectPool<NettyClient> pool = getXpipeNettyClientKeyedObjectPool().getKeyPool(new InetSocketAddress(ip, port));
    NettyClient nettyClient = null;
    try {
        nettyClient = pool.borrowObject();
        SimpleObjectPool<NettyClient> clientPool = new FixedObjectPool<NettyClient>(nettyClient);
        chain.add(new Replconf(clientPool, ReplConfType.CAPA, scheduled, CAPA.EOF.toString()));
        InMemoryPsync psync = new InMemoryPsync(clientPool, runid, offset, scheduled);
        chain.add(psync);
        psync.addPsyncObserver(new PsyncObserver() {

            private long masterRdbOffset = 0;

            @Override
            public void reFullSync() {
            }

            @Override
            public void onFullSync() {
            }

            @Override
            public void onContinue(String requestReplId, String responseReplId) {
            }

            @Override
            public void endWriteRdb() {
                new Replconf(clientPool, ReplConfType.ACK, scheduled, String.valueOf(masterRdbOffset)).execute();
            }

            @Override
            public void beginWriteRdb(EofType eofType, long masterRdbOffset) throws IOException {
                this.masterRdbOffset = masterRdbOffset;
            }
        });
        chain.execute();
        return psync;
    } finally {
        if (nettyClient != null) {
            pool.returnObject(nettyClient);
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Replconf(com.ctrip.xpipe.redis.core.protocal.cmd.Replconf) EofType(com.ctrip.xpipe.redis.core.protocal.protocal.EofType) IOException(java.io.IOException) SequenceCommandChain(com.ctrip.xpipe.command.SequenceCommandChain) NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) FixedObjectPool(com.ctrip.xpipe.pool.FixedObjectPool) InMemoryPsync(com.ctrip.xpipe.redis.core.protocal.cmd.InMemoryPsync) PsyncObserver(com.ctrip.xpipe.redis.core.protocal.PsyncObserver)

Example 2 with EofType

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

the class DefaultReplicationStore method rdbUpdated.

@Override
public void rdbUpdated(DumpedRdbStore dumpedRdbStore) throws IOException {
    makeSureOpen();
    synchronized (lock) {
        rdbUpdateCount.incrementAndGet();
        File dumpedRdbFile = dumpedRdbStore.getRdbFile();
        if (!baseDir.equals(dumpedRdbFile.getParentFile())) {
            throw new IllegalStateException("update rdb error, filePath:" + dumpedRdbFile.getAbsolutePath() + ", baseDir:" + baseDir.getAbsolutePath());
        }
        EofType eofType = dumpedRdbStore.getEofType();
        long rdbOffset = dumpedRdbStore.rdbOffset();
        @SuppressWarnings("unused") ReplicationStoreMeta metaDup = metaStore.rdbUpdated(dumpedRdbFile.getName(), eofType, rdbOffset);
        dumpedRdbStore.addListener(new ReplicationStoreRdbFileListener(dumpedRdbStore));
        logger.info("[rdbUpdated] new file {}, eofType {}, rdbOffset {}", dumpedRdbFile, eofType, rdbOffset);
        RdbStore oldRdbStore = rdbStoreRef.get();
        rdbStoreRef.set(dumpedRdbStore);
        previousRdbStores.put(oldRdbStore, Boolean.TRUE);
    }
}
Also used : EofType(com.ctrip.xpipe.redis.core.protocal.protocal.EofType) LenEofType(com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType) File(java.io.File)

Example 3 with EofType

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

the class DefaultReplicationStoreTest method testReadWhileDestroy.

@Test
public void testReadWhileDestroy() throws Exception {
    store = new DefaultReplicationStore(baseDir, new DefaultKeeperConfig(), randomKeeperRunid(), createkeeperMonitor());
    store.getMetaStore().becomeActive();
    int dataLen = 1000;
    RdbStore rdbStore = store.beginRdb(randomKeeperRunid(), -1, new LenEofType(dataLen));
    rdbStore.writeRdb(Unpooled.wrappedBuffer(randomString(dataLen).getBytes()));
    rdbStore.endRdb();
    CountDownLatch latch = new CountDownLatch(2);
    AtomicBoolean result = new AtomicBoolean(true);
    executors.execute(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            try {
                sleep(2);
                store.close();
                store.destroy();
            } finally {
                latch.countDown();
            }
        }
    });
    executors.execute(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            try {
                store.fullSyncIfPossible(new FullSyncListener() {

                    @Override
                    public ChannelFuture onCommand(ReferenceFileRegion referenceFileRegion) {
                        return null;
                    }

                    @Override
                    public void beforeCommand() {
                    }

                    @Override
                    public void setRdbFileInfo(EofType eofType, long rdbFileKeeperOffset) {
                    }

                    @Override
                    public void onFileData(ReferenceFileRegion referenceFileRegion) throws IOException {
                        sleep(10);
                    }

                    @Override
                    public boolean isOpen() {
                        return true;
                    }

                    @Override
                    public void exception(Exception e) {
                        logger.info("[exception][fail]" + e.getMessage());
                        result.set(false);
                    }

                    @Override
                    public void beforeFileData() {
                    }
                });
            } catch (Exception e) {
                logger.info("[exception][fail]" + e.getMessage());
                result.set(false);
            } finally {
                latch.countDown();
            }
        }
    });
    latch.await(100, TimeUnit.MILLISECONDS);
    Assert.assertFalse(result.get());
}
Also used : RdbStore(com.ctrip.xpipe.redis.core.store.RdbStore) LenEofType(com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType) EofType(com.ctrip.xpipe.redis.core.protocal.protocal.EofType) LenEofType(com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ReferenceFileRegion(com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FullSyncListener(com.ctrip.xpipe.redis.core.store.FullSyncListener) DefaultKeeperConfig(com.ctrip.xpipe.redis.keeper.config.DefaultKeeperConfig) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) Test(org.junit.Test) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)

Example 4 with EofType

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

the class AbstractRedisKeeperTest method readRdbFileTilEnd.

protected String readRdbFileTilEnd(RdbStore rdbStore) throws IOException, InterruptedException {
    final ByteArrayWritableByteChannel bachannel = new ByteArrayWritableByteChannel();
    final CountDownLatch latch = new CountDownLatch(1);
    rdbStore.readRdbFile(new RdbFileListener() {

        @Override
        public void setRdbFileInfo(EofType eofType, long rdbFileOffset) {
        }

        @Override
        public void onFileData(ReferenceFileRegion referenceFileRegion) throws IOException {
            if (referenceFileRegion == null) {
                latch.countDown();
                return;
            }
            referenceFileRegion.transferTo(bachannel, 0L);
        }

        @Override
        public boolean isOpen() {
            return true;
        }

        @Override
        public void exception(Exception e) {
            latch.countDown();
        }

        @Override
        public void beforeFileData() {
        }
    });
    latch.await(5, TimeUnit.SECONDS);
    return new String(bachannel.getResult());
}
Also used : ByteArrayWritableByteChannel(com.ctrip.xpipe.payload.ByteArrayWritableByteChannel) EofType(com.ctrip.xpipe.redis.core.protocal.protocal.EofType) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ReferenceFileRegion(com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion)

Aggregations

EofType (com.ctrip.xpipe.redis.core.protocal.protocal.EofType)4 IOException (java.io.IOException)3 ReferenceFileRegion (com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion)2 LenEofType (com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 SequenceCommandChain (com.ctrip.xpipe.command.SequenceCommandChain)1 AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)1 NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)1 ByteArrayWritableByteChannel (com.ctrip.xpipe.payload.ByteArrayWritableByteChannel)1 FixedObjectPool (com.ctrip.xpipe.pool.FixedObjectPool)1 PsyncObserver (com.ctrip.xpipe.redis.core.protocal.PsyncObserver)1 InMemoryPsync (com.ctrip.xpipe.redis.core.protocal.cmd.InMemoryPsync)1 Replconf (com.ctrip.xpipe.redis.core.protocal.cmd.Replconf)1 FullSyncListener (com.ctrip.xpipe.redis.core.store.FullSyncListener)1 RdbStore (com.ctrip.xpipe.redis.core.store.RdbStore)1 AbstractRedisKeeperTest (com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)1 DefaultKeeperConfig (com.ctrip.xpipe.redis.keeper.config.DefaultKeeperConfig)1 File (java.io.File)1 InetSocketAddress (java.net.InetSocketAddress)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1