Search in sources :

Example 16 with IgniteInClosure

use of org.apache.ignite.lang.IgniteInClosure in project ignite by apache.

the class PoolProcessor method start.

/**
 * {@inheritDoc}
 */
@Override
public void start() throws IgniteCheckedException {
    super.start();
    IgniteConfiguration cfg = ctx.config();
    UncaughtExceptionHandler oomeHnd = ctx.uncaughtExceptionHandler();
    UncaughtExceptionHandler excHnd = new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            ctx.failure().process(new FailureContext(FailureType.CRITICAL_ERROR, e));
        }
    };
    validateThreadPoolSize(cfg.getPublicThreadPoolSize(), "public");
    execSvc = createExecutorService("pub", cfg.getIgniteInstanceName(), cfg.getPublicThreadPoolSize(), cfg.getPublicThreadPoolSize(), DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.PUBLIC_POOL, oomeHnd);
    execSvc.allowCoreThreadTimeOut(true);
    validateThreadPoolSize(cfg.getServiceThreadPoolSize(), "service");
    svcExecSvc = createExecutorService("svc", cfg.getIgniteInstanceName(), cfg.getServiceThreadPoolSize(), cfg.getServiceThreadPoolSize(), DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.SERVICE_POOL, oomeHnd);
    svcExecSvc.allowCoreThreadTimeOut(true);
    validateThreadPoolSize(cfg.getSystemThreadPoolSize(), "system");
    sysExecSvc = createExecutorService("sys", cfg.getIgniteInstanceName(), cfg.getSystemThreadPoolSize(), cfg.getSystemThreadPoolSize(), DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.SYSTEM_POOL, oomeHnd);
    sysExecSvc.allowCoreThreadTimeOut(true);
    validateThreadPoolSize(cfg.getStripedPoolSize(), "stripedPool");
    WorkersRegistry workerRegistry = ctx.workersRegistry();
    stripedExecSvc = createStripedExecutor(cfg.getStripedPoolSize(), cfg.getIgniteInstanceName(), "sys", log, new IgniteInClosure<Throwable>() {

        @Override
        public void apply(Throwable t) {
            ctx.failure().process(new FailureContext(SYSTEM_WORKER_TERMINATION, t));
        }
    }, false, workerRegistry, cfg.getFailureDetectionTimeout());
    // Note that since we use 'LinkedBlockingQueue', number of
    // maximum threads has no effect.
    // Note, that we do not pre-start threads here as management pool may
    // not be needed.
    validateThreadPoolSize(cfg.getManagementThreadPoolSize(), "management");
    mgmtExecSvc = createExecutorService("mgmt", cfg.getIgniteInstanceName(), cfg.getManagementThreadPoolSize(), cfg.getManagementThreadPoolSize(), DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.MANAGEMENT_POOL, oomeHnd);
    mgmtExecSvc.allowCoreThreadTimeOut(true);
    // Note that since we use 'LinkedBlockingQueue', number of
    // maximum threads has no effect.
    // Note, that we do not pre-start threads here as class loading pool may
    // not be needed.
    validateThreadPoolSize(cfg.getPeerClassLoadingThreadPoolSize(), "peer class loading");
    p2pExecSvc = createExecutorService("p2p", cfg.getIgniteInstanceName(), cfg.getPeerClassLoadingThreadPoolSize(), cfg.getPeerClassLoadingThreadPoolSize(), DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.P2P_POOL, oomeHnd);
    p2pExecSvc.allowCoreThreadTimeOut(true);
    dataStreamerExecSvc = createStripedExecutor(cfg.getDataStreamerThreadPoolSize(), cfg.getIgniteInstanceName(), "data-streamer", log, new IgniteInClosure<Throwable>() {

        @Override
        public void apply(Throwable t) {
            ctx.failure().process(new FailureContext(SYSTEM_WORKER_TERMINATION, t));
        }
    }, true, workerRegistry, cfg.getFailureDetectionTimeout());
    // Note that we do not pre-start threads here as this pool may not be needed.
    validateThreadPoolSize(cfg.getAsyncCallbackPoolSize(), "async callback");
    callbackExecSvc = new IgniteStripedThreadPoolExecutor(cfg.getAsyncCallbackPoolSize(), cfg.getIgniteInstanceName(), "callback", oomeHnd, false, 0);
    if (cfg.getConnectorConfiguration() != null) {
        validateThreadPoolSize(cfg.getConnectorConfiguration().getThreadPoolSize(), "connector");
        restExecSvc = createExecutorService("rest", cfg.getIgniteInstanceName(), cfg.getConnectorConfiguration().getThreadPoolSize(), cfg.getConnectorConfiguration().getThreadPoolSize(), DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.UNDEFINED, oomeHnd);
        restExecSvc.allowCoreThreadTimeOut(true);
    }
    validateThreadPoolSize(cfg.getUtilityCacheThreadPoolSize(), "utility cache");
    utilityCacheExecSvc = createExecutorService("utility", cfg.getIgniteInstanceName(), cfg.getUtilityCacheThreadPoolSize(), cfg.getUtilityCacheThreadPoolSize(), cfg.getUtilityCacheKeepAliveTime(), new LinkedBlockingQueue<>(), GridIoPolicy.UTILITY_CACHE_POOL, oomeHnd);
    utilityCacheExecSvc.allowCoreThreadTimeOut(true);
    affExecSvc = createExecutorService("aff", cfg.getIgniteInstanceName(), 1, 1, DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.AFFINITY_POOL, oomeHnd);
    affExecSvc.allowCoreThreadTimeOut(true);
    if (IgniteComponentType.INDEXING.inClassPath()) {
        int cpus = Runtime.getRuntime().availableProcessors();
        idxExecSvc = createExecutorService("idx", cfg.getIgniteInstanceName(), cpus, cpus * 2, 3000L, new LinkedBlockingQueue<>(1000), GridIoPolicy.IDX_POOL, oomeHnd);
        int buildIdxThreadPoolSize = cfg.getBuildIndexThreadPoolSize();
        validateThreadPoolSize(buildIdxThreadPoolSize, "build-idx");
        buildIdxExecSvc = createExecutorService("build-idx-runner", cfg.getIgniteInstanceName(), buildIdxThreadPoolSize, buildIdxThreadPoolSize, DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.UNDEFINED, oomeHnd);
        buildIdxExecSvc.allowCoreThreadTimeOut(true);
    }
    validateThreadPoolSize(cfg.getQueryThreadPoolSize(), "query");
    qryExecSvc = createExecutorService("query", cfg.getIgniteInstanceName(), cfg.getQueryThreadPoolSize(), cfg.getQueryThreadPoolSize(), DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.QUERY_POOL, oomeHnd);
    qryExecSvc.allowCoreThreadTimeOut(true);
    schemaExecSvc = createExecutorService("schema", cfg.getIgniteInstanceName(), 2, 2, DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.SCHEMA_POOL, oomeHnd);
    schemaExecSvc.allowCoreThreadTimeOut(true);
    validateThreadPoolSize(cfg.getRebalanceThreadPoolSize(), "rebalance");
    rebalanceExecSvc = createExecutorService("rebalance", cfg.getIgniteInstanceName(), cfg.getRebalanceThreadPoolSize(), cfg.getRebalanceThreadPoolSize(), DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.UNDEFINED, excHnd);
    rebalanceExecSvc.allowCoreThreadTimeOut(true);
    if (CU.isPersistenceEnabled(ctx.config())) {
        snpExecSvc = createExecutorService(SNAPSHOT_RUNNER_THREAD_PREFIX, cfg.getIgniteInstanceName(), cfg.getSnapshotThreadPoolSize(), cfg.getSnapshotThreadPoolSize(), DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.UNDEFINED, excHnd);
        snpExecSvc.allowCoreThreadTimeOut(true);
        reencryptExecSvc = createExecutorService("reencrypt", ctx.igniteInstanceName(), 1, 1, DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.UNDEFINED, oomeHnd);
        reencryptExecSvc.allowCoreThreadTimeOut(true);
    }
    if (cfg.getClientConnectorConfiguration() != null) {
        thinClientExec = new IgniteThreadPoolExecutor("client-connector", cfg.getIgniteInstanceName(), cfg.getClientConnectorConfiguration().getThreadPoolSize(), cfg.getClientConnectorConfiguration().getThreadPoolSize(), 0, new LinkedBlockingQueue<>(), GridIoPolicy.UNDEFINED, oomeHnd);
    }
    rebalanceStripedExecSvc = createStripedThreadPoolExecutor(cfg.getRebalanceThreadPoolSize(), cfg.getIgniteInstanceName(), "rebalance-striped", excHnd, true, DFLT_THREAD_KEEP_ALIVE_TIME);
    if (!F.isEmpty(cfg.getExecutorConfiguration())) {
        validateCustomExecutorsConfiguration(cfg.getExecutorConfiguration());
        customExecs = new HashMap<>();
        for (ExecutorConfiguration execCfg : cfg.getExecutorConfiguration()) {
            ThreadPoolExecutor exec = createExecutorService(execCfg.getName(), cfg.getIgniteInstanceName(), execCfg.getSize(), execCfg.getSize(), DFLT_THREAD_KEEP_ALIVE_TIME, new LinkedBlockingQueue<>(), GridIoPolicy.UNDEFINED, oomeHnd);
            customExecs.put(execCfg.getName(), exec);
        }
    }
}
Also used : IgniteStripedThreadPoolExecutor(org.apache.ignite.thread.IgniteStripedThreadPoolExecutor) WorkersRegistry(org.apache.ignite.internal.worker.WorkersRegistry) IgniteThreadPoolExecutor(org.apache.ignite.thread.IgniteThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ExecutorConfiguration(org.apache.ignite.configuration.ExecutorConfiguration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) FailureContext(org.apache.ignite.failure.FailureContext) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) SecurityAwareStripedThreadPoolExecutor(org.apache.ignite.internal.processors.security.thread.SecurityAwareStripedThreadPoolExecutor) SecurityAwareThreadPoolExecutor(org.apache.ignite.internal.processors.security.thread.SecurityAwareThreadPoolExecutor) IgniteStripedThreadPoolExecutor(org.apache.ignite.thread.IgniteStripedThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) IgniteThreadPoolExecutor(org.apache.ignite.thread.IgniteThreadPoolExecutor) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 17 with IgniteInClosure

use of org.apache.ignite.lang.IgniteInClosure in project ignite by apache.

the class ServiceDeploymentTask method onReceiveFullDeploymentsMessage.

/**
 * Handles received full services map message.
 *
 * @param msg Full services map message.
 */
protected void onReceiveFullDeploymentsMessage(ServiceClusterDeploymentResultBatch msg) {
    assert depId.equals(msg.deploymentId()) : "Wrong message's deployment process id, msg=" + msg;
    initTaskFut.listen((IgniteInClosure<IgniteInternalFuture<?>>) fut -> {
        if (isCompleted())
            return;
        ctx.closure().runLocalSafe((GridPlainRunnable) () -> {
            try {
                ServiceDeploymentActions depResults = msg.servicesDeploymentActions();
                assert depResults != null : "Services deployment actions should be attached.";
                final Map<IgniteUuid, Map<UUID, Integer>> fullTops = depResults.deploymentTopologies();
                final Map<IgniteUuid, Collection<byte[]>> fullErrors = depResults.deploymentErrors();
                depActions.deploymentTopologies(fullTops);
                depActions.deploymentErrors(fullErrors);
                srvcProc.updateServicesTopologies(fullTops);
                final Map<IgniteUuid, ServiceInfo> services = srvcProc.deployedServices();
                fullTops.forEach((srvcId, top) -> {
                    Integer expCnt = top.getOrDefault(ctx.localNodeId(), 0);
                    if (expCnt < srvcProc.localInstancesCount(srvcId)) {
                        ServiceInfo desc = services.get(srvcId);
                        assert desc != null;
                        ServiceConfiguration cfg = desc.configuration();
                        try {
                            srvcProc.redeploy(srvcId, cfg, top);
                        } catch (IgniteCheckedException e) {
                            log.error("Error occured during cancel exceed service instances: " + "[srvcId=" + srvcId + ", name=" + desc.name() + ']', e);
                        }
                    }
                });
                completeSuccess();
            } catch (Throwable t) {
                log.error("Failed to process services full deployments message, msg=" + msg, t);
                completeError(t);
            }
        });
    });
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) DiscoveryCustomEvent(org.apache.ignite.internal.events.DiscoveryCustomEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) EVT_NODE_LEFT(org.apache.ignite.events.EventType.EVT_NODE_LEFT) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) U(org.apache.ignite.internal.util.typedef.internal.U) EVT_DISCOVERY_CUSTOM_EVT(org.apache.ignite.internal.events.DiscoveryCustomEvent.EVT_DISCOVERY_CUSTOM_EVT) HashMap(java.util.HashMap) IgniteLogger(org.apache.ignite.IgniteLogger) ArrayList(java.util.ArrayList) GridKernalContext(org.apache.ignite.internal.GridKernalContext) HashSet(java.util.HashSet) ClusterNode(org.apache.ignite.cluster.ClusterNode) SERVICE_POOL(org.apache.ignite.internal.managers.communication.GridIoPolicy.SERVICE_POOL) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) Map(java.util.Map) S(org.apache.ignite.internal.util.typedef.internal.S) ServiceConfiguration(org.apache.ignite.services.ServiceConfiguration) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) F(org.apache.ignite.internal.util.typedef.F) EVT_NODE_JOINED(org.apache.ignite.events.EventType.EVT_NODE_JOINED) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) GridToStringExclude(org.apache.ignite.internal.util.tostring.GridToStringExclude) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) Set(java.util.Set) UUID(java.util.UUID) TOPIC_SERVICES(org.apache.ignite.internal.GridTopic.TOPIC_SERVICES) GridToStringInclude(org.apache.ignite.internal.util.tostring.GridToStringInclude) Nullable(org.jetbrains.annotations.Nullable) EVT_NODE_FAILED(org.apache.ignite.events.EventType.EVT_NODE_FAILED) TreeMap(java.util.TreeMap) GridPlainRunnable(org.apache.ignite.internal.util.lang.GridPlainRunnable) NotNull(org.jetbrains.annotations.NotNull) IgniteUuid(org.apache.ignite.lang.IgniteUuid) CacheAffinityChangeMessage(org.apache.ignite.internal.processors.cache.CacheAffinityChangeMessage) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridPlainRunnable(org.apache.ignite.internal.util.lang.GridPlainRunnable) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ServiceConfiguration(org.apache.ignite.services.ServiceConfiguration) IgniteUuid(org.apache.ignite.lang.IgniteUuid) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 18 with IgniteInClosure

use of org.apache.ignite.lang.IgniteInClosure in project ignite by apache.

the class ClientSlowDiscoveryTransactionRemapTest method parameters.

/**
 */
@Parameterized.Parameters(name = "isolation = {0}, concurrency = {1}, operation = {2}")
public static List<Object[]> parameters() {
    ArrayList<Object[]> params = new ArrayList<>();
    List<IgniteInClosure<TestTransaction<Integer, Integer>>> operations = new ArrayList<>();
    operations.add(new NamedClosure<>(putRemoveSameKey, "putRemoveSameKey"));
    operations.add(new NamedClosure<>(putRemoveDifferentKey, "putRemoveDifferentKey"));
    operations.add(new NamedClosure<>(getPutSameKey, "getPutSameKey"));
    operations.add(new NamedClosure<>(getPutDifferentKey, "getPutDifferentKey"));
    operations.add(new NamedClosure<>(putAllRemoveAllSameKeys, "putAllRemoveAllSameKeys"));
    operations.add(new NamedClosure<>(putAllRemoveAllDifferentKeys, "putAllRemoveAllDifferentKeys"));
    operations.add(new NamedClosure<>(randomOperation, "random"));
    for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (TransactionIsolation isolation : TransactionIsolation.values()) {
            if (!shouldBeTested(concurrency, isolation))
                continue;
            for (IgniteInClosure<TestTransaction<Integer, Integer>> operation : operations) params.add(new Object[] { concurrency, isolation, operation });
        }
    }
    return params;
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) ArrayList(java.util.ArrayList) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure)

Example 19 with IgniteInClosure

use of org.apache.ignite.lang.IgniteInClosure in project ignite by apache.

the class CacheMvccAbstractTest method accountsTxReadAll.

/**
 * @param srvs Number of server nodes.
 * @param clients Number of client nodes.
 * @param cacheBackups Number of cache backups.
 * @param cacheParts Number of cache partitions.
 * @param cfgC Optional closure applied to cache configuration.
 * @param withRmvs If {@code true} then in addition to puts tests also executes removes.
 * @param readMode Read mode.
 * @param writeMode Write mode.
 * @param testTime Test time.
 * @throws Exception If failed.
 */
final void accountsTxReadAll(final int srvs, final int clients, int cacheBackups, int cacheParts, @Nullable IgniteInClosure<CacheConfiguration> cfgC, final boolean withRmvs, final ReadMode readMode, final WriteMode writeMode, long testTime, RestartMode restartMode) throws Exception {
    final int ACCOUNTS = 20;
    final int ACCOUNT_START_VAL = 1000;
    final int writers = 4;
    final int readers = 4;
    final IgniteInClosure<IgniteCache<Object, Object>> init = new IgniteInClosure<IgniteCache<Object, Object>>() {

        @Override
        public void apply(IgniteCache<Object, Object> cache) {
            final IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
            if (writeMode == WriteMode.PUT) {
                Map<Integer, MvccTestAccount> accounts = new HashMap<>();
                for (int i = 0; i < ACCOUNTS; i++) accounts.put(i, new MvccTestAccount(ACCOUNT_START_VAL, 1));
                try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    cache.putAll(accounts);
                    tx.commit();
                }
            } else if (writeMode == WriteMode.DML) {
                try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    SqlFieldsQuery qry = new SqlFieldsQuery("insert into MvccTestAccount(_key, val, updateCnt) values " + "(?," + ACCOUNT_START_VAL + ",1)");
                    for (int i = 0; i < ACCOUNTS; i++) {
                        try (FieldsQueryCursor<List<?>> cur = cache.query(qry.setArgs(i))) {
                            assertEquals(1L, cur.iterator().next().get(0));
                        }
                        tx.commit();
                    }
                }
            } else
                assert false : "Unknown write mode";
        }
    };
    final RemovedAccountsTracker rmvdTracker = new RemovedAccountsTracker(ACCOUNTS);
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            int cnt = 0;
            while (!stop.get()) {
                TestCache<Integer, MvccTestAccount> cache = randomCache(caches, rnd);
                try {
                    IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
                    cnt++;
                    int i1 = rnd.nextInt(ACCOUNTS), i2 = rnd.nextInt(ACCOUNTS);
                    while (i2 == i1) i2 = rnd.nextInt(ACCOUNTS);
                    Integer id1 = Math.min(i1, i2);
                    Integer id2 = Math.max(i1, i2);
                    Set<Integer> keys = new HashSet<>();
                    keys.add(id1);
                    keys.add(id2);
                    Integer cntr1 = null;
                    Integer cntr2 = null;
                    Integer rmvd = null;
                    Integer inserted = null;
                    MvccTestAccount a1;
                    MvccTestAccount a2;
                    try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        tx.timeout(TX_TIMEOUT);
                        Map<Integer, MvccTestAccount> accounts = null;
                        if (writeMode == WriteMode.PUT)
                            accounts = cache.cache.getAll(keys);
                        else if (writeMode == WriteMode.DML)
                            accounts = getAllSql(cache);
                        else
                            assert false : "Unknown write mode";
                        a1 = accounts.get(id1);
                        a2 = accounts.get(id2);
                        if (!withRmvs) {
                            assertNotNull(a1);
                            assertNotNull(a2);
                            cntr1 = a1.updateCnt + 1;
                            cntr2 = a2.updateCnt + 1;
                            if (writeMode == WriteMode.PUT) {
                                cache.cache.put(id1, new MvccTestAccount(a1.val + 1, cntr1));
                                cache.cache.put(id2, new MvccTestAccount(a2.val - 1, cntr2));
                            } else if (writeMode == WriteMode.DML) {
                                updateSql(cache, id1, a1.val + 1, cntr1);
                                updateSql(cache, id2, a2.val - 1, cntr2);
                            } else
                                assert false : "Unknown write mode";
                        } else {
                            if (a1 != null || a2 != null) {
                                if (a1 != null && a2 != null) {
                                    if (rnd.nextInt(10) == 0) {
                                        if (rmvdTracker.size() < ACCOUNTS / 2) {
                                            rmvd = rnd.nextBoolean() ? id1 : id2;
                                            assertTrue(rmvdTracker.markRemoved(rmvd));
                                        }
                                    }
                                    if (rmvd != null) {
                                        if (writeMode == WriteMode.PUT) {
                                            if (rmvd.equals(id1)) {
                                                cache.cache.remove(id1);
                                                cache.cache.put(id2, new MvccTestAccount(a1.val + a2.val, 1));
                                            } else {
                                                cache.cache.put(id1, new MvccTestAccount(a1.val + a2.val, 1));
                                                cache.cache.remove(id2);
                                            }
                                        } else if (writeMode == WriteMode.DML) {
                                            if (rmvd.equals(id1)) {
                                                removeSql(cache, id1);
                                                updateSql(cache, id2, a1.val + a2.val, 1);
                                            } else {
                                                updateSql(cache, id1, a1.val + a2.val, 1);
                                                removeSql(cache, id2);
                                            }
                                        } else
                                            assert false : "Unknown write mode";
                                    } else {
                                        if (writeMode == WriteMode.PUT) {
                                            cache.cache.put(id1, new MvccTestAccount(a1.val + 1, 1));
                                            cache.cache.put(id2, new MvccTestAccount(a2.val - 1, 1));
                                        } else if (writeMode == WriteMode.DML) {
                                            updateSql(cache, id1, a1.val + 1, 1);
                                            updateSql(cache, id2, a2.val - 1, 1);
                                        } else
                                            assert false : "Unknown write mode";
                                    }
                                } else {
                                    if (a1 == null) {
                                        inserted = id1;
                                        if (writeMode == WriteMode.PUT) {
                                            cache.cache.put(id1, new MvccTestAccount(100, 1));
                                            cache.cache.put(id2, new MvccTestAccount(a2.val - 100, 1));
                                        } else if (writeMode == WriteMode.DML) {
                                            insertSql(cache, id1, 100, 1);
                                            updateSql(cache, id2, a2.val - 100, 1);
                                        } else
                                            assert false : "Unknown write mode";
                                    } else {
                                        inserted = id2;
                                        if (writeMode == WriteMode.PUT) {
                                            cache.cache.put(id1, new MvccTestAccount(a1.val - 100, 1));
                                            cache.cache.put(id2, new MvccTestAccount(100, 1));
                                        } else if (writeMode == WriteMode.DML) {
                                            updateSql(cache, id1, a1.val - 100, 1);
                                            insertSql(cache, id2, 100, 1);
                                        } else
                                            assert false : "Unknown write mode";
                                    }
                                }
                            }
                        }
                        tx.commit();
                        // In case of tx success mark inserted.
                        if (inserted != null) {
                            assert withRmvs;
                            assertTrue(rmvdTracker.unmarkRemoved(inserted));
                        }
                    } catch (Throwable e) {
                        if (rmvd != null) {
                            assert withRmvs;
                            // If tx fails, unmark removed.
                            assertTrue(rmvdTracker.unmarkRemoved(rmvd));
                        }
                        throw e;
                    }
                    if (!withRmvs) {
                        Map<Integer, MvccTestAccount> accounts = null;
                        if (writeMode == WriteMode.PUT)
                            accounts = cache.cache.getAll(keys);
                        else if (writeMode == WriteMode.DML)
                            accounts = getAllSql(cache);
                        else
                            assert false : "Unknown write mode";
                        a1 = accounts.get(id1);
                        a2 = accounts.get(id2);
                        assertNotNull(a1);
                        assertNotNull(a2);
                        assertTrue(a1.updateCnt >= cntr1);
                        assertTrue(a2.updateCnt >= cntr2);
                    }
                } catch (Exception e) {
                    handleTxException(e);
                } finally {
                    cache.readUnlock();
                }
            }
            info("Writer finished, updates: " + cnt);
        }
    };
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            Set<Integer> keys = new LinkedHashSet<>();
            Map<Integer, Integer> lastUpdateCntrs = new HashMap<>();
            SqlFieldsQuery sumQry = new SqlFieldsQuery("select sum(val) from MvccTestAccount");
            while (!stop.get()) {
                while (keys.size() < ACCOUNTS) keys.add(rnd.nextInt(ACCOUNTS));
                TestCache<Integer, MvccTestAccount> cache = randomCache(caches, rnd);
                Map<Integer, MvccTestAccount> accounts = null;
                try {
                    switch(readMode) {
                        case GET:
                            {
                                accounts = cache.cache.getAll(keys);
                                break;
                            }
                        case SCAN:
                            {
                                accounts = new HashMap<>();
                                Iterator<Cache.Entry<Integer, MvccTestAccount>> it = cache.cache.iterator();
                                try {
                                    for (; it.hasNext(); ) {
                                        IgniteCache.Entry<Integer, MvccTestAccount> e = it.next();
                                        MvccTestAccount old = accounts.put(e.getKey(), e.getValue());
                                        assertNull("new=" + e + ", old=" + old, old);
                                    }
                                } finally {
                                    U.closeQuiet((AutoCloseable) it);
                                }
                                break;
                            }
                        case SQL:
                            {
                                accounts = new HashMap<>();
                                if (rnd.nextBoolean()) {
                                    SqlQuery<Integer, MvccTestAccount> qry = new SqlQuery<>(MvccTestAccount.class, "_key >= 0");
                                    for (IgniteCache.Entry<Integer, MvccTestAccount> e : cache.cache.query(qry).getAll()) {
                                        MvccTestAccount old = accounts.put(e.getKey(), e.getValue());
                                        assertNull(old);
                                    }
                                } else {
                                    SqlFieldsQuery qry = new SqlFieldsQuery("select _key, val from MvccTestAccount");
                                    for (List<?> row : cache.cache.query(qry).getAll()) {
                                        Integer id = (Integer) row.get(0);
                                        Integer val = (Integer) row.get(1);
                                        MvccTestAccount old = accounts.put(id, new MvccTestAccount(val, 1));
                                        assertNull(old);
                                    }
                                }
                                break;
                            }
                        case SQL_SUM:
                            {
                                Long sum;
                                if (rnd.nextBoolean()) {
                                    List<List<?>> res = cache.cache.query(sumQry).getAll();
                                    assertEquals(1, res.size());
                                    sum = (Long) res.get(0).get(0);
                                } else {
                                    Map res = readAllByMode(cache.cache, keys, readMode, ACCOUNT_CODEC);
                                    sum = (Long) ((Map.Entry) res.entrySet().iterator().next()).getValue();
                                }
                                assertEquals(ACCOUNT_START_VAL * ACCOUNTS, sum.intValue());
                                break;
                            }
                        default:
                            {
                                fail();
                                return;
                            }
                    }
                } finally {
                    cache.readUnlock();
                }
                if (accounts != null) {
                    if (!withRmvs)
                        assertEquals(ACCOUNTS, accounts.size());
                    int sum = 0;
                    for (int i = 0; i < ACCOUNTS; i++) {
                        MvccTestAccount account = accounts.get(i);
                        if (account != null) {
                            sum += account.val;
                            Integer cntr = lastUpdateCntrs.get(i);
                            if (cntr != null)
                                assertTrue(cntr <= account.updateCnt);
                            lastUpdateCntrs.put(i, cntr);
                        } else
                            assertTrue(withRmvs);
                    }
                    assertEquals(ACCOUNTS * ACCOUNT_START_VAL, sum);
                }
            }
            if (idx == 0) {
                TestCache<Integer, MvccTestAccount> cache = randomCache(caches, rnd);
                Map<Integer, MvccTestAccount> accounts;
                ReadMode readMode0 = readMode == SQL_SUM ? SQL : readMode;
                try {
                    accounts = readAllByMode(cache.cache, keys, readMode0, ACCOUNT_CODEC);
                    ;
                } finally {
                    cache.readUnlock();
                }
                int sum = 0;
                for (int i = 0; i < ACCOUNTS; i++) {
                    MvccTestAccount account = accounts.get(i);
                    assertTrue(account != null || withRmvs);
                    info("Account [id=" + i + ", val=" + (account != null ? account.val : null) + ']');
                    if (account != null)
                        sum += account.val;
                }
                info("Sum: " + sum);
            }
        }
    };
    readWriteTest(restartMode, srvs, clients, cacheBackups, cacheParts, writers, readers, testTime, cfgC, init, writer, reader);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IgniteTransactions(org.apache.ignite.IgniteTransactions) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) Iterator(java.util.Iterator) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteCache(org.apache.ignite.IgniteCache) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteFutureCancelledCheckedException(org.apache.ignite.internal.IgniteFutureCancelledCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) CacheException(javax.cache.CacheException) TransactionException(org.apache.ignite.transactions.TransactionException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 20 with IgniteInClosure

use of org.apache.ignite.lang.IgniteInClosure in project ignite by apache.

the class CacheMvccTransactionsTest method doImplicitPartsScanTest.

/**
 * @param srvs Number of server nodes.
 * @param clients Number of client nodes.
 * @param cacheBackups Number of cache backups.
 * @param cacheParts Number of cache partitions.
 * @throws Exception If failed.
 */
private void doImplicitPartsScanTest(final int srvs, final int clients, int cacheBackups, int cacheParts) throws Exception {
    final int KEYS_PER_PART = 20;
    final int writers = 4;
    final int readers = 4;
    Map<Integer, List<Integer>> keysByParts = new HashMap<>();
    final IgniteInClosure<IgniteCache<Object, Object>> init = new IgniteInClosure<IgniteCache<Object, Object>>() {

        @Override
        public void apply(IgniteCache<Object, Object> cache) {
            final IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
            for (int i = 0; i < cacheParts; i++) {
                List<Integer> keys = new ArrayList<>();
                keysByParts.put(i, keys);
            }
            Affinity aff = affinity(cache);
            int cntr = 0;
            int key = 0;
            while (cntr < KEYS_PER_PART * cacheParts) {
                int part = aff.partition(key);
                List<Integer> keys = keysByParts.get(part);
                if (keys.size() < KEYS_PER_PART) {
                    keys.add(key);
                    cntr++;
                }
                key++;
            }
            try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                for (List<Integer> keys : keysByParts.values()) for (Integer k : keys) cache.put(k, new MvccTestAccount(0, 1));
                tx.commit();
            }
        }
    };
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            while (!stop.get()) {
                int part = rnd.nextInt(cacheParts);
                List<Integer> partKeys = keysByParts.get(part);
                TestCache<Integer, MvccTestAccount> cache = randomCache(caches, rnd);
                IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
                Integer k1 = partKeys.get(rnd.nextInt(KEYS_PER_PART));
                Integer k2 = partKeys.get(rnd.nextInt(KEYS_PER_PART));
                while (k1.equals(k2)) k2 = partKeys.get(rnd.nextInt(KEYS_PER_PART));
                if (k1 > k2) {
                    int tmp = k1;
                    k1 = k2;
                    k2 = tmp;
                }
                TreeSet<Integer> keys = new TreeSet<>();
                keys.add(k1);
                keys.add(k2);
                try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    Map<Integer, MvccTestAccount> accs = cache.cache.getAll(keys);
                    MvccTestAccount acc1 = accs.get(k1);
                    MvccTestAccount acc2 = accs.get(k2);
                    assertNotNull(acc1);
                    assertNotNull(acc2);
                    cache.cache.put(k1, new MvccTestAccount(acc1.val + 1, acc1.updateCnt + 1));
                    cache.cache.put(k2, new MvccTestAccount(acc2.val - 1, acc2.updateCnt + 1));
                    tx.commit();
                } catch (CacheException ex) {
                    MvccFeatureChecker.assertMvccWriteConflict(ex);
                } finally {
                    cache.readUnlock();
                }
            }
        }
    };
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            while (!stop.get()) {
                int part = rnd.nextInt(cacheParts);
                TestCache<Integer, Integer> cache = randomCache(caches, rnd);
                try {
                    Affinity aff = affinity(cache.cache);
                    ScanQuery<Integer, MvccTestAccount> qry = new ScanQuery<>(part);
                    List<Cache.Entry<Integer, MvccTestAccount>> res = cache.cache.query(qry).getAll();
                    int sum = 0;
                    for (Cache.Entry<Integer, MvccTestAccount> entry : res) {
                        Integer key = entry.getKey();
                        MvccTestAccount acc = entry.getValue();
                        assertEquals(part, aff.partition(key));
                        sum += acc.val;
                    }
                    assertEquals(0, sum);
                } finally {
                    cache.readUnlock();
                }
                if (idx == 0) {
                    cache = randomCache(caches, rnd);
                    try {
                        ScanQuery<Integer, MvccTestAccount> qry = new ScanQuery<>();
                        List<Cache.Entry<Integer, MvccTestAccount>> res = cache.cache.query(qry).getAll();
                        int sum = 0;
                        for (Cache.Entry<Integer, MvccTestAccount> entry : res) {
                            Integer key = entry.getKey();
                            MvccTestAccount acc = entry.getValue();
                            sum += acc.val;
                        }
                        assertEquals(0, sum);
                    } finally {
                        cache.readUnlock();
                    }
                }
            }
        }
    };
    readWriteTest(null, srvs, clients, cacheBackups, cacheParts, writers, readers, SCALED_10SEC_TEST_TIME, null, init, writer, reader);
}
Also used : HashMap(java.util.HashMap) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteTransactions(org.apache.ignite.IgniteTransactions) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) MutableEntry(javax.cache.processor.MutableEntry) GridCacheMapEntry(org.apache.ignite.internal.processors.cache.GridCacheMapEntry) TreeSet(java.util.TreeSet) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) List(java.util.List) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) Affinity(org.apache.ignite.cache.affinity.Affinity) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)39 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)25 List (java.util.List)22 ArrayList (java.util.ArrayList)20 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)18 HashMap (java.util.HashMap)17 Map (java.util.Map)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 Ignite (org.apache.ignite.Ignite)15 Test (org.junit.Test)15 UUID (java.util.UUID)14 IgniteCache (org.apache.ignite.IgniteCache)14 IgniteException (org.apache.ignite.IgniteException)13 Collection (java.util.Collection)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)12 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)12 U (org.apache.ignite.internal.util.typedef.internal.U)12 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)12 Nullable (org.jetbrains.annotations.Nullable)12