Search in sources :

Example 26 with AbstractExceptionLogTask

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

the class DefaultRedisSessionManager method postConstruct.

@PostConstruct
public void postConstruct() {
    int corePoolSize = 30 * OsUtils.getCpuCount();
    int maxPoolSize = 512;
    DefaultExecutorFactory executorFactory = new DefaultExecutorFactory("RedisSession", corePoolSize, maxPoolSize, new ThreadPoolExecutor.AbortPolicy());
    executors = executorFactory.createExecutorService();
    int fixedPoolSize = OsUtils.getCpuCount();
    pingAndDelayExecutor = new DefaultExecutorFactory("Ping-Delay-Executor", fixedPoolSize, fixedPoolSize, new ThreadPoolExecutor.CallerRunsPolicy()).createExecutorService();
    scheduled.scheduleAtFixedRate(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            try {
                removeUnusedRedises();
            } catch (Exception e) {
                logger.error("[removeUnusedRedises]", e);
            }
            for (RedisSession redisSession : sessions.values()) {
                try {
                    redisSession.check();
                } catch (Exception e) {
                    logger.error("[check]" + redisSession, e);
                }
            }
        }
    }, 5, 5, TimeUnit.SECONDS);
}
Also used : AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) DefaultExecutorFactory(com.ctrip.xpipe.concurrent.DefaultExecutorFactory) PostConstruct(javax.annotation.PostConstruct)

Example 27 with AbstractExceptionLogTask

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

the class AbstractNewMasterChooser method getMasters.

protected Pair<List<RedisMeta>, List<RedisMeta>> getMasters(List<RedisMeta> allRedises) {
    List<RedisMeta> masters = new LinkedList<>();
    List<RedisMeta> tmpAliveServers = new LinkedList<>();
    CountDownLatch latch = new CountDownLatch(allRedises.size());
    for (RedisMeta redisMeta : allRedises) {
        executors.execute(new AbstractExceptionLogTask() {

            @Override
            protected void doRun() throws Exception {
                try {
                    SERVER_ROLE role = serverRole(redisMeta);
                    if (role == SERVER_ROLE.MASTER) {
                        synchronized (masters) {
                            masters.add(redisMeta);
                        }
                    }
                    if (role != SERVER_ROLE.UNKNOWN) {
                        synchronized (tmpAliveServers) {
                            tmpAliveServers.add(redisMeta);
                        }
                    }
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    try {
        latch.await(CHECK_NEW_MASTER_TIMEOUT_SECONDS * 2, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        logger.error("[getMasters]" + allRedises, e);
    }
    List<RedisMeta> aliveServers = sortAccording(allRedises, tmpAliveServers);
    return new Pair<>(masters, aliveServers);
}
Also used : RedisMeta(com.ctrip.xpipe.redis.core.entity.RedisMeta) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) SERVER_ROLE(com.ctrip.xpipe.api.server.Server.SERVER_ROLE) LinkedList(java.util.LinkedList) ChooseNewMasterFailException(com.ctrip.xpipe.redis.meta.server.dcchange.exception.ChooseNewMasterFailException) Pair(com.ctrip.xpipe.tuple.Pair)

Example 28 with AbstractExceptionLogTask

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

the class AbstractMigrationMigratingState method doMigrateOtherDc.

protected void doMigrateOtherDc() {
    logger.debug("[doMigrateOtherDc]{}", this);
    MigrationCluster migrationCluster = getHolder();
    String clusterName = migrationCluster.clusterName();
    for (MigrationShard migrationShard : migrationCluster.getMigrationShards()) {
        executors.execute(new AbstractExceptionLogTask() {

            @Override
            public void doRun() {
                String shardName = migrationShard.shardName();
                logger.info("[doOtherDcMigrate][start]{},{}", clusterName, shardName);
                migrationShard.doMigrateOtherDc();
                logger.info("[doOtherDcMigrate][done]{},{}", clusterName, shardName);
            }
        });
    }
}
Also used : MigrationShard(com.ctrip.xpipe.redis.console.migration.model.MigrationShard) MigrationCluster(com.ctrip.xpipe.redis.console.migration.model.MigrationCluster) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)

Example 29 with AbstractExceptionLogTask

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

the class ReferenceFileChannelTest method testConcurrentRead.

@Test
public void testConcurrentRead() throws InterruptedException, IOException {
    int concurrentCount = 10;
    final LinkedBlockingQueue<ReferenceFileRegion> fileRegions = new LinkedBlockingQueue<>();
    final CountDownLatch latch = new CountDownLatch(concurrentCount);
    for (int i = 0; i < concurrentCount; i++) {
        executors.execute(new AbstractExceptionLogTask() {

            private int count = 0;

            @Override
            protected void doRun() throws Exception {
                try {
                    while (true) {
                        count++;
                        ReferenceFileRegion referenceFileRegion = referenceFileChannel.readTilEnd(1);
                        fileRegions.offer(referenceFileRegion);
                        if (count > totalFileLen) {
                            logger.info("{}", referenceFileRegion);
                        }
                        if (referenceFileRegion.count() == 0) {
                            break;
                        }
                        if (referenceFileRegion.count() < 0) {
                            logger.error("{}", referenceFileRegion);
                        }
                    }
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    latch.await();
    referenceFileChannel.close();
    Assert.assertFalse(referenceFileChannel.isFileChannelClosed());
    long realTotalLen = 0;
    Set<Long> starts = new HashSet<>();
    while (true) {
        ReferenceFileRegion referenceFileRegion = fileRegions.poll(100, TimeUnit.MILLISECONDS);
        if (referenceFileRegion == null) {
            break;
        }
        if (referenceFileRegion.position() != totalFileLen) {
            Assert.assertTrue(starts.add(referenceFileRegion.position()));
        }
        realTotalLen += referenceFileRegion.count();
        referenceFileRegion.release();
    }
    Assert.assertEquals(totalFileLen, realTotalLen);
    Assert.assertTrue(referenceFileChannel.isFileChannelClosed());
}
Also used : AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) HashSet(java.util.HashSet) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 30 with AbstractExceptionLogTask

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

the class DefaultRedisConfManager method postConstruct.

@PostConstruct
public void postConstruct() {
    int initialDelay = 5;
    int period = 1;
    scheduled.scheduleAtFixedRate(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() {
            String eventType = "Redis.Server.Version";
            try {
                for (ConcurrentMap.Entry entry : configs.entrySet()) {
                    CatEventMonitor.DEFAULT.logEvent(eventType, entry.getValue().toString());
                }
                logger.debug("Current Redis Conf Manager Cache:\n {}", this.toString());
            } catch (Exception e) {
                logger.error("[postConstruct]{}", e);
            }
        }
    }, initialDelay, period, TimeUnit.MINUTES);
}
Also used : AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) PostConstruct(javax.annotation.PostConstruct)

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