Search in sources :

Example 41 with AbstractExceptionLogTask

use of com.ctrip.xpipe.concurrent.AbstractExceptionLogTask in project x-pipe by ctripcorp.

the class OneDcKeepers method sendMessageForever.

@Test
public void sendMessageForever() throws IOException {
    executors.execute(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            while (!Thread.interrupted()) {
                sendMessageToMaster();
            }
        }
    });
    waitForAnyKeyToExit();
}
Also used : AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) IOException(java.io.IOException) Test(org.junit.Test)

Example 42 with AbstractExceptionLogTask

use of com.ctrip.xpipe.concurrent.AbstractExceptionLogTask in project x-pipe by ctripcorp.

the class PubSubMode method startProducerJob.

@Override
protected void startProducerJob() {
    super.startProducerJob();
    scheduled.scheduleAtFixedRate(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            try (Jedis resource = masterPool.getResource()) {
                long currentNanos = System.nanoTime();
                String currentNanosStr = String.valueOf(currentNanos);
                logger.debug("[publish]{}", currentNanosStr);
                resource.publish(pubChannel, currentNanosStr);
            }
        }
    }, pubIntervalMilli, pubIntervalMilli, TimeUnit.MILLISECONDS);
}
Also used : Jedis(redis.clients.jedis.Jedis) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)

Example 43 with AbstractExceptionLogTask

use of com.ctrip.xpipe.concurrent.AbstractExceptionLogTask in project x-pipe by ctripcorp.

the class DefaultCommandStoreTest method testLengthEqual.

@Test
public void testLengthEqual() throws InterruptedException {
    final int runTimes = 1000;
    final AtomicLong realLength = new AtomicLong();
    final AtomicReference<Boolean> result = new AtomicReference<Boolean>(true);
    final Semaphore read = new Semaphore(0);
    final Semaphore write = new Semaphore(1);
    final AtomicBoolean finished = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(2);
    executors.execute(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            try {
                for (int i = 0; i < runTimes; i++) {
                    write.acquire();
                    int randomLength = randomInt(0, 1 << 8);
                    commandStore.appendCommands(Unpooled.wrappedBuffer(randomString(randomLength).getBytes()));
                    realLength.addAndGet(randomLength);
                    read.release();
                }
            } finally {
                finished.set(true);
                read.release();
                latch.countDown();
            }
        }
    });
    executors.execute(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            try {
                while (!finished.get()) {
                    read.acquire();
                    long len = commandStore.totalLength();
                    if (len != realLength.get()) {
                        result.set(false);
                    }
                    write.release();
                }
            } finally {
                latch.countDown();
            }
        }
    });
    latch.await();
    Assert.assertTrue(result.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)

Example 44 with AbstractExceptionLogTask

use of com.ctrip.xpipe.concurrent.AbstractExceptionLogTask 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 45 with AbstractExceptionLogTask

use of com.ctrip.xpipe.concurrent.AbstractExceptionLogTask in project x-pipe by ctripcorp.

the class AbstractClusterServers method doStart.

@Override
protected void doStart() throws Exception {
    CuratorFramework client = zkClient.get();
    serversCache = new PathChildrenCache(client, MetaZkConfig.getMetaServerRegisterPath(), true, XpipeThreadFactory.create(String.format("PathChildrenCache(%d)", currentServer.getServerId())));
    serversCache.getListenable().addListener(new ChildrenChanged());
    serversCache.start();
    future = scheduled.scheduleWithFixedDelay(new AbstractExceptionLogTask() {

        @Override
        public void doRun() {
            try {
                childrenChanged();
            } catch (Throwable th) {
                logger.error("[doStart]", th);
            }
        }
    }, 1000, metaServerConfig.getClusterServersRefreshMilli(), TimeUnit.MILLISECONDS);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)

Aggregations

AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)46 IOException (java.io.IOException)19 Test (org.junit.Test)18 CountDownLatch (java.util.concurrent.CountDownLatch)9 PostConstruct (javax.annotation.PostConstruct)9 ExecutionException (java.util.concurrent.ExecutionException)8 AbstractRedisKeeperTest (com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)6 AbstractTest (com.ctrip.xpipe.AbstractTest)5 MigrationShard (com.ctrip.xpipe.redis.console.migration.model.MigrationShard)5 LinkedList (java.util.LinkedList)5 KeeperMeta (com.ctrip.xpipe.redis.core.entity.KeeperMeta)3 FileChannel (java.nio.channels.FileChannel)3 Semaphore (java.util.concurrent.Semaphore)3 TimeoutException (java.util.concurrent.TimeoutException)3 ReferenceFileRegion (com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion)2 RedisMeta (com.ctrip.xpipe.redis.core.entity.RedisMeta)2 RedisKeeperRuntimeException (com.ctrip.xpipe.redis.keeper.exception.RedisKeeperRuntimeException)2 AbstractMetaServerTest (com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)2 Server (com.ctrip.xpipe.simpleserver.Server)2 List (java.util.List)2