Search in sources :

Example 91 with Executor

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();
}
Also used : Executor(java.util.concurrent.Executor) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) IOException(java.io.IOException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 92 with Executor

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();
    }
}
Also used : Executor(java.util.concurrent.Executor) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) MySQLConnection(com.alibaba.cobar.mysql.nio.MySQLConnection)

Example 93 with Executor

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);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Executor(java.util.concurrent.Executor) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) Channel(com.alibaba.cobar.mysql.bio.Channel) ServerConnection(com.alibaba.cobar.server.ServerConnection) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) ByteBuffer(java.nio.ByteBuffer)

Example 94 with Executor

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);
}
Also used : InfinispanBeanGroupFactory(org.wildfly.clustering.ejb.infinispan.group.InfinispanBeanGroupFactory) Address(org.infinispan.remoting.transport.Address) Time(org.wildfly.clustering.ejb.Time) BeanPassivationConfiguration(org.wildfly.clustering.ejb.BeanPassivationConfiguration) InfinispanCacheProperties(org.wildfly.clustering.ee.infinispan.InfinispanCacheProperties) CacheProperties(org.wildfly.clustering.ee.infinispan.CacheProperties) Executor(java.util.concurrent.Executor) SimpleMarshallingContextFactory(org.wildfly.clustering.marshalling.jboss.SimpleMarshallingContextFactory) InfinispanBeanFactory(org.wildfly.clustering.ejb.infinispan.bean.InfinispanBeanFactory) CommandDispatcherFactory(org.wildfly.clustering.dispatcher.CommandDispatcherFactory) MarshallingContext(org.wildfly.clustering.marshalling.jboss.MarshallingContext) InfinispanBeanGroupFactory(org.wildfly.clustering.ejb.infinispan.group.InfinispanBeanGroupFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InfinispanBeanFactory(org.wildfly.clustering.ejb.infinispan.bean.InfinispanBeanFactory) BeanPassivationConfiguration(org.wildfly.clustering.ejb.BeanPassivationConfiguration) KeyAffinityServiceFactory(org.wildfly.clustering.infinispan.spi.affinity.KeyAffinityServiceFactory) SimpleMarshalledValueFactory(org.wildfly.clustering.marshalling.jboss.SimpleMarshalledValueFactory) InfinispanCacheProperties(org.wildfly.clustering.ee.infinispan.InfinispanCacheProperties)

Example 95 with Executor

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;
}
Also used : ProgressManager(com.intellij.openapi.progress.ProgressManager) Application(com.intellij.openapi.application.Application) Executor(java.util.concurrent.Executor) ModalityState(com.intellij.openapi.application.ModalityState) CompletableFuture(java.util.concurrent.CompletableFuture) EdtInvocationManager(com.intellij.util.ui.EdtInvocationManager) Disposable(com.intellij.openapi.Disposable) ApplicationEx(com.intellij.openapi.application.ex.ApplicationEx) Nullable(org.jetbrains.annotations.Nullable) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ApplicationAdapter(com.intellij.openapi.application.ApplicationAdapter) BiConsumer(java.util.function.BiConsumer) ApplicationManager(com.intellij.openapi.application.ApplicationManager) ApplicationManagerEx(com.intellij.openapi.application.ex.ApplicationManagerEx) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) PooledThreadExecutor(org.jetbrains.ide.PooledThreadExecutor) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) NotNull(org.jetbrains.annotations.NotNull) CompletableFuture(java.util.concurrent.CompletableFuture) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) ApplicationAdapter(com.intellij.openapi.application.ApplicationAdapter) Application(com.intellij.openapi.application.Application) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Executor (java.util.concurrent.Executor)302 Test (org.junit.Test)127 ArrayList (java.util.ArrayList)35 CountDownLatch (java.util.concurrent.CountDownLatch)32 IOException (java.io.IOException)29 List (java.util.List)27 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)22 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)17 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Map (java.util.Map)15 Timeouts (org.neo4j.cluster.timeout.Timeouts)15 ExecutorService (java.util.concurrent.ExecutorService)13 InstanceId (org.neo4j.cluster.InstanceId)13 Config (org.neo4j.kernel.configuration.Config)13 File (java.io.File)12 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)12 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)12 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)12 InetSocketAddress (java.net.InetSocketAddress)10