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());
}
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));
}
}
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);
}
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);
}
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();
}
Aggregations