use of java.util.concurrent.Executor in project pulsar by yahoo.
the class ManagedLedgerTest method testOpenRaceCondition.
/**
* Reproduce a race condition between opening cursors and concurrent mark delete operations
*/
@Test(timeOut = 20000)
public void testOpenRaceCondition() throws Exception {
ManagedLedgerConfig config = new ManagedLedgerConfig();
config.setEnsembleSize(2).setAckQuorumSize(2).setMetadataEnsembleSize(2);
final ManagedLedger ledger = factory.open("my-ledger", config);
final ManagedCursor c1 = ledger.openCursor("c1");
final int N = 1000;
final Position position = ledger.addEntry("entry-0".getBytes());
Executor executor = Executors.newCachedThreadPool();
final CountDownLatch counter = new CountDownLatch(2);
executor.execute(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < N; i++) {
c1.markDelete(position);
}
counter.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
});
executor.execute(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < N; i++) {
ledger.openCursor("cursor-" + i);
}
counter.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
});
// If there is the race condition, this method will not complete triggering the test timeout
counter.await();
}
use of java.util.concurrent.Executor in project cobar by alibaba.
the class RollbackNodeHandler method rollback.
public void rollback() {
final int initCount = session.getTargetCount();
lock.lock();
try {
reset(initCount);
} finally {
lock.unlock();
}
if (session.closed()) {
decrementCountToZero();
return;
}
// 执行
Executor executor = session.getSource().getProcessor().getExecutor();
int started = 0;
for (final RouteResultsetNode node : session.getTargetKeys()) {
if (node == null) {
try {
logger.error("null is contained in RoutResultsetNodes, source = " + session.getSource());
} catch (Exception e) {
}
continue;
}
final MySQLConnection conn = session.getTarget(node);
if (conn != null) {
conn.setRunning(true);
executor.execute(new Runnable() {
@Override
public void run() {
if (isFail.get() || session.closed()) {
backendConnError(conn, "cancelled by other thread");
return;
}
conn.setResponseHandler(RollbackNodeHandler.this);
conn.rollback();
}
});
++started;
}
}
if (started < initCount && decrementCountBy(initCount - started)) {
/**
* assumption: only caused by front-end connection close. <br/>
* Otherwise, packet must be returned to front-end
*/
session.clearConnections();
}
}
use of java.util.concurrent.Executor in project cobar by alibaba.
the class RollbackExecutor method rollback.
/**
* 事务回滚
*/
public void rollback(final BlockingSession session) {
final ServerConnection source = session.getSource();
final ConcurrentMap<RouteResultsetNode, Channel> target = session.getTarget();
final int initNodeCount = target.size();
if (initNodeCount <= 0) {
ByteBuffer buffer = source.allocate();
source.write(source.writeToBuffer(OkPacket.OK, buffer));
return;
}
// 初始化
final ReentrantLock lock = this.lock;
lock.lock();
try {
this.isFail.set(false);
this.nodeCount = initNodeCount;
} finally {
lock.unlock();
}
if (source.isClosed()) {
decrementCountToZero();
return;
}
// 执行
Executor committer = source.getProcessor().getCommitter();
int started = 0;
for (RouteResultsetNode rrn : target.keySet()) {
final MySQLChannel mc = (MySQLChannel) target.get(rrn);
if (mc != null) {
mc.setRunning(true);
committer.execute(new Runnable() {
@Override
public void run() {
_rollback(mc, session);
}
});
++started;
}
}
if (started < initNodeCount) {
decrementCountBy(initNodeCount - started);
}
}
use of java.util.concurrent.Executor in project wildfly by wildfly.
the class InfinispanBeanManagerFactory method createBeanManager.
@Override
public BeanManager<I, T, TransactionBatch> createBeanManager(IdentifierFactory<I> identifierFactory, PassivationListener<T> passivationListener, RemoveListener<T> removeListener) {
MarshallingContext context = new SimpleMarshallingContextFactory().createMarshallingContext(this.configuration.getMarshallingConfigurationRepository(), this.configuration.getBeanContext().getClassLoader());
MarshalledValueFactory<MarshallingContext> factory = new SimpleMarshalledValueFactory(context);
Cache<BeanKey<I>, BeanEntry<I>> beanCache = this.configuration.getCache();
Cache<BeanGroupKey<I>, BeanGroupEntry<I, T>> groupCache = this.configuration.getCache();
final CacheProperties properties = new InfinispanCacheProperties(groupCache.getCacheConfiguration());
final String beanName = this.configuration.getBeanContext().getBeanName();
BeanGroupFactory<I, T> groupFactory = new InfinispanBeanGroupFactory<>(groupCache, beanCache, factory, context, properties);
Configuration<BeanGroupKey<I>, BeanGroupEntry<I, T>, BeanGroupFactory<I, T>> groupConfiguration = new SimpleConfiguration<>(groupCache, groupFactory);
BeanFactory<I, T> beanFactory = new InfinispanBeanFactory<>(beanName, groupFactory, beanCache, properties, this.configuration.getBeanContext().getTimeout(), properties.isPersistent() ? passivationListener : null);
Configuration<BeanKey<I>, BeanEntry<I>, BeanFactory<I, T>> beanConfiguration = new SimpleConfiguration<>(beanCache, beanFactory);
final NodeFactory<Address> nodeFactory = this.configuration.getNodeFactory();
final Registry<String, ?> registry = this.configuration.getRegistry();
final KeyAffinityServiceFactory affinityFactory = this.configuration.getKeyAffinityServiceFactory();
final CommandDispatcherFactory dispatcherFactory = this.configuration.getCommandDispatcherFactory();
final Time timeout = this.configuration.getBeanContext().getTimeout();
final ScheduledExecutorService scheduler = this.configuration.getScheduler();
final ExpirationConfiguration<T> expiration = new ExpirationConfiguration<T>() {
@Override
public Time getTimeout() {
return timeout;
}
@Override
public RemoveListener<T> getRemoveListener() {
return removeListener;
}
@Override
public ScheduledExecutorService getExecutor() {
return scheduler;
}
};
final Executor executor = this.configuration.getExecutor();
final BeanPassivationConfiguration passivationConfig = this.configuration.getPassivationConfiguration();
final PassivationConfiguration<T> passivation = new PassivationConfiguration<T>() {
@Override
public PassivationListener<T> getPassivationListener() {
return passivationListener;
}
@Override
public BeanPassivationConfiguration getConfiguration() {
return passivationConfig;
}
@Override
public Executor getExecutor() {
return executor;
}
};
InfinispanBeanManagerConfiguration<T> configuration = new InfinispanBeanManagerConfiguration<T>() {
@Override
public String getBeanName() {
return beanName;
}
@Override
public KeyAffinityServiceFactory getAffinityFactory() {
return affinityFactory;
}
@Override
public Registry<String, ?> getRegistry() {
return registry;
}
@Override
public NodeFactory<Address> getNodeFactory() {
return nodeFactory;
}
@Override
public CommandDispatcherFactory getCommandDispatcherFactory() {
return dispatcherFactory;
}
@Override
public ExpirationConfiguration<T> getExpirationConfiguration() {
return expiration;
}
@Override
public PassivationConfiguration<T> getPassivationConfiguration() {
return passivation;
}
@Override
public CacheProperties getProperties() {
return properties;
}
};
return new InfinispanBeanManager<>(configuration, identifierFactory, beanConfiguration, groupConfiguration);
}
use of java.util.concurrent.Executor in project intellij-community by JetBrains.
the class ProgressIndicatorUtils method scheduleWithWriteActionPriority.
@NotNull
public static CompletableFuture<?> scheduleWithWriteActionPriority(@NotNull final ProgressIndicator progressIndicator, @NotNull final Executor executor, @NotNull final ReadTask readTask) {
final Application application = ApplicationManager.getApplication();
// invoke later even if on EDT
// to avoid tasks eagerly restarting immediately, allocating many pooled threads
// which get cancelled too soon when a next write action arrives in the same EDT batch
// (can happen when processing multiple VFS events or writing multiple files on save)
// use SwingUtilities instead of application.invokeLater
// to tolerate any immediate modality changes (e.g. https://youtrack.jetbrains.com/issue/IDEA-135180)
CompletableFuture<?> future = new CompletableFuture<>();
//noinspection SSBasedInspection
EdtInvocationManager.getInstance().invokeLater(() -> {
if (application.isDisposed() || progressIndicator.isCanceled()) {
future.complete(null);
return;
}
final ApplicationAdapter listener = new ApplicationAdapter() {
@Override
public void beforeWriteActionStart(@NotNull Object action) {
if (!progressIndicator.isCanceled()) {
progressIndicator.cancel();
readTask.onCanceled(progressIndicator);
}
}
};
application.addApplicationListener(listener);
future.whenComplete((BiConsumer<Object, Throwable>) (o, throwable) -> application.removeApplicationListener(listener));
try {
executor.execute(new Runnable() {
@Override
public void run() {
final ReadTask.Continuation continuation;
try {
continuation = runUnderProgress(progressIndicator, readTask);
} catch (Throwable e) {
future.completeExceptionally(e);
throw e;
}
if (continuation == null) {
future.complete(null);
} else {
application.invokeLater(new Runnable() {
@Override
public void run() {
try {
if (!progressIndicator.isCanceled()) {
continuation.getAction().run();
}
} finally {
future.complete(null);
}
}
@Override
public String toString() {
return "continuation of " + readTask;
}
}, continuation.getModalityState());
}
}
@Override
public String toString() {
return readTask.toString();
}
});
} catch (RuntimeException | Error e) {
application.removeApplicationListener(listener);
future.completeExceptionally(e);
throw e;
}
});
return future;
}
Aggregations