Search in sources :

Example 16 with AbstractExceptionLogTask

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

the class ControllableFileAbstractTest method testEquals.

@Test
public void testEquals() throws FileNotFoundException, ExecutionException, InterruptedException {
    RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
    FileChannel channel = randomAccessFile.getChannel();
    byte[] data = randomString(randomInt(0, 1000)).getBytes();
    AtomicLong total = new AtomicLong(0);
    int readCount = 10;
    Semaphore write = new Semaphore(readCount);
    Semaphore read = new Semaphore(0);
    executors.execute(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            while (!Thread.currentThread().isInterrupted()) {
                write.acquire(readCount);
                byte[] data = randomString(randomInt(0, 1000)).getBytes();
                total.addAndGet(data.length);
                channel.write(ByteBuffer.wrap(data));
                read.release(readCount);
            }
        }
    });
    SettableFuture<Boolean> success = SettableFuture.create();
    for (int i = 0; i < readCount / 2; i++) {
        executors.execute(new AbstractExceptionLogTask() {

            @Override
            protected void doRun() throws Exception {
                while (!Thread.currentThread().isInterrupted()) {
                    read.acquire();
                    if (total.get() != file.length()) {
                        logger.info("[length not equals]");
                        success.set(false);
                    }
                    write.release();
                }
            }
        });
    }
    for (int i = readCount / 2; i < readCount; i++) {
        executors.execute(new AbstractExceptionLogTask() {

            @Override
            protected void doRun() throws Exception {
                while (!Thread.currentThread().isInterrupted()) {
                    read.acquire();
                    if (total.get() != channel.size()) {
                        logger.info("[channel not equals]");
                        success.set(false);
                    }
                    write.release();
                }
            }
        });
    }
    try {
        success.get(1, TimeUnit.SECONDS);
        Assert.fail();
    } catch (TimeoutException e) {
    }
    logger.info("{}", file.length());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) FileChannel(java.nio.channels.FileChannel) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) Semaphore(java.util.concurrent.Semaphore) TimeoutException(java.util.concurrent.TimeoutException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 17 with AbstractExceptionLogTask

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

the class DefaultControllableFileTest method testConcurrentRead.

@Test
public void testConcurrentRead() throws IOException, InterruptedException {
    int concurrent = 10;
    List<FileChannel> channels = new LinkedList<>();
    CountDownLatch latch = new CountDownLatch(concurrent);
    for (int i = 0; i < concurrent; i++) {
        executors.execute(new AbstractExceptionLogTask() {

            @Override
            protected void doRun() throws Exception {
                try {
                    FileChannel fileChannel = controllableFile.getFileChannel();
                    synchronized (channels) {
                        channels.add(fileChannel);
                    }
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    latch.await();
    logger.info("{}", channels.size());
    Assert.assertEquals(concurrent, channels.size());
    for (int i = 1; i < channels.size(); i++) {
        Assert.assertEquals(channels.get(0), channels.get(i));
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) IOException(java.io.IOException) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 18 with AbstractExceptionLogTask

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

the class NotificationManager method start.

@PostConstruct
public void start() {
    logger.info("Alert Notification Manager started");
    alertSender = XpipeThreadFactory.create("NotificationManager-alert-sender", true).newThread(new SendAlert());
    recoverAnnoucer = XpipeThreadFactory.create("NotificationManager-recover-annoucer", true).newThread(new AnnounceRecover());
    alertSender.start();
    recoverAnnoucer.start();
    schedule.scheduleAtFixedRate(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() {
            try {
                senderManager.sendAlerts(scheduledAlerts);
                scheduledAlerts = new ConcurrentHashMap<>();
            } catch (Exception e) {
                logger.error("[start][schedule]{}", e);
            }
        }
    }, 1, 30, TimeUnit.MINUTES);
}
Also used : AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) PostConstruct(javax.annotation.PostConstruct)

Example 19 with AbstractExceptionLogTask

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

the class AddKeeperCommandTest method testCheckStateCommandNoDelay.

@Test
public void testCheckStateCommandNoDelay() throws Exception {
    int sleepTime = 2000;
    SlaveRole keeperRole = new SlaveRole(SERVER_ROLE.KEEPER, "localhost", randomPort(), MASTER_STATE.REDIS_REPL_CONNECTED, 0);
    Server server = startServer(keeperPort, new Callable<String>() {

        @Override
        public String call() throws Exception {
            sleep(sleepTime);
            return ByteBufUtils.readToString(keeperRole.format());
        }
    });
    SettableFuture<Boolean> objectSettableFuture = SettableFuture.create();
    executors.execute(new AbstractExceptionLogTask() {

        @Override
        public void doRun() throws Exception {
            AddKeeperCommand.CheckStateCommand checkStateCommand = new AddKeeperCommand.CheckStateCommand(new KeeperMeta().setIp("127.0.0.1").setPort(server.getPort()), scheduled);
            checkStateCommand.doExecute();
            objectSettableFuture.set(true);
        }
    });
    // should return immediately
    objectSettableFuture.get(500, TimeUnit.MILLISECONDS);
}
Also used : Server(com.ctrip.xpipe.simpleserver.Server) ExecutionException(java.util.concurrent.ExecutionException) KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) Test(org.junit.Test) AbstractMetaServerTest(com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)

Example 20 with AbstractExceptionLogTask

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

the class DeleteKeeperCommandTest method testDeleteWaitTimeoutThenSuccess.

@Test
public void testDeleteWaitTimeoutThenSuccess() throws Exception {
    List<KeeperMeta> keepers = new LinkedList<>();
    keepers.add(keeperMeta);
    final Server server = startEchoServer(keeperMeta.getPort());
    scheduled.schedule(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            LifecycleHelper.stopIfPossible(server);
            LifecycleHelper.disposeIfPossible(server);
        }
    }, checkIntervalMili / 2, TimeUnit.MILLISECONDS);
    deleteKeeperCommand.execute().get();
}
Also used : Server(com.ctrip.xpipe.simpleserver.Server) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) LinkedList(java.util.LinkedList) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) AbstractMetaServerTest(com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)

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