Search in sources :

Example 1 with TransactionView

use of org.apache.ignite.spi.systemview.view.TransactionView in project ignite by apache.

the class FunctionalTest method testTransactionsWithLabel.

/**
 * Test transactions with label.
 */
@Test
public void testTransactionsWithLabel() throws Exception {
    try (IgniteEx ignite = (IgniteEx) Ignition.start(Config.getServerConfiguration());
        IgniteClient client = Ignition.startClient(getClientConfiguration())) {
        ClientCache<Integer, String> cache = client.createCache(new ClientCacheConfiguration().setName("cache").setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));
        SystemView<TransactionView> txsView = ignite.context().systemView().view(TXS_MON_LIST);
        cache.put(0, "value1");
        try (ClientTransaction tx = client.transactions().withLabel("label").txStart()) {
            cache.put(0, "value2");
            assertEquals(1, F.size(txsView.iterator()));
            TransactionView txv = txsView.iterator().next();
            assertEquals("label", txv.label());
            assertEquals("value2", cache.get(0));
        }
        assertEquals("value1", cache.get(0));
        try (ClientTransaction tx = client.transactions().withLabel("label1").withLabel("label2").txStart()) {
            cache.put(0, "value2");
            assertEquals(1, F.size(txsView.iterator()));
            TransactionView txv = txsView.iterator().next();
            assertEquals("label2", txv.label());
            tx.commit();
        }
        assertEquals("value2", cache.get(0));
        // Test concurrent with label and without label transactions.
        try (ClientTransaction tx = client.transactions().withLabel("label").txStart(PESSIMISTIC, READ_COMMITTED)) {
            CyclicBarrier barrier = new CyclicBarrier(2);
            cache.put(0, "value3");
            IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> {
                try (ClientTransaction tx1 = client.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
                    cache.put(1, "value3");
                    barrier.await();
                    assertEquals("value2", cache.get(0));
                    barrier.await();
                } catch (InterruptedException | BrokenBarrierException ignore) {
                // No-op.
                }
            });
            barrier.await();
            assertNull(cache.get(1));
            assertEquals(1, F.size(txsView.iterator(), txv -> txv.label() == null));
            assertEquals(1, F.size(txsView.iterator(), txv -> "label".equals(txv.label())));
            barrier.await();
            fut.get();
        }
        // Test nested transactions is not possible.
        try (ClientTransaction tx = client.transactions().withLabel("label1").txStart()) {
            try (ClientTransaction tx1 = client.transactions().txStart()) {
                fail();
            } catch (ClientException expected) {
            // No-op.
            }
            try (ClientTransaction tx1 = client.transactions().withLabel("label2").txStart()) {
                fail();
            } catch (ClientException expected) {
            // No-op.
            }
        }
    }
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy) BinaryObject(org.apache.ignite.binary.BinaryObject) Arrays(java.util.Arrays) AbstractBinaryArraysTest(org.apache.ignite.internal.binary.AbstractBinaryArraysTest) SERIALIZABLE(org.apache.ignite.transactions.TransactionIsolation.SERIALIZABLE) CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) Date(java.util.Date) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) TestEnum(org.apache.ignite.internal.processors.cache.CacheEnumOperationsAbstractTest.TestEnum) REPEATABLE_READ(org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ) AccessedExpiryPolicy(javax.cache.expiry.AccessedExpiryPolicy) CacheRebalanceMode(org.apache.ignite.cache.CacheRebalanceMode) GridTestUtils.runAsync(org.apache.ignite.testframework.GridTestUtils.runAsync) VAL3(org.apache.ignite.internal.processors.cache.CacheEnumOperationsAbstractTest.TestEnum.VAL3) VAL1(org.apache.ignite.internal.processors.cache.CacheEnumOperationsAbstractTest.TestEnum.VAL1) VAL2(org.apache.ignite.internal.processors.cache.CacheEnumOperationsAbstractTest.TestEnum.VAL2) Map(java.util.Map) QueryEntity(org.apache.ignite.cache.QueryEntity) ClientStatus(org.apache.ignite.internal.processors.platform.client.ClientStatus) GridTestUtils.assertThrowsAnyCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsAnyCause) CyclicBarrier(java.util.concurrent.CyclicBarrier) ImmutableSet(com.google.common.collect.ImmutableSet) CachePeekMode(org.apache.ignite.cache.CachePeekMode) SystemView(org.apache.ignite.spi.systemview.view.SystemView) TransactionView(org.apache.ignite.spi.systemview.view.TransactionView) Set(java.util.Set) OPTIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC) READ_COMMITTED(org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED) ClientListenerProcessor(org.apache.ignite.internal.processors.odbc.ClientListenerProcessor) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Stream(java.util.stream.Stream) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) IntStream(java.util.stream.IntStream) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) ClientServerError(org.apache.ignite.internal.client.thin.ClientServerError) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) PartitionLossPolicy(org.apache.ignite.cache.PartitionLossPolicy) PlatformExpiryPolicy(org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy) ClientProcessorMXBean(org.apache.ignite.mxbean.ClientProcessorMXBean) CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Timeout(org.junit.rules.Timeout) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) LinkedList(java.util.LinkedList) SimpleEntry(java.util.AbstractMap.SimpleEntry) Duration(javax.cache.expiry.Duration) F(org.apache.ignite.internal.util.typedef.F) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) Iterator(java.util.Iterator) TXS_MON_LIST(org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager.TXS_MON_LIST) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) T2(org.apache.ignite.internal.util.typedef.T2) TimeUnit(java.util.concurrent.TimeUnit) Ignition(org.apache.ignite.Ignition) Rule(org.junit.Rule) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Collections(java.util.Collections) QueryIndex(org.apache.ignite.cache.QueryIndex) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) CacheMode(org.apache.ignite.cache.CacheMode) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) IgniteEx(org.apache.ignite.internal.IgniteEx) TransactionView(org.apache.ignite.spi.systemview.view.TransactionView) AbstractBinaryArraysTest(org.apache.ignite.internal.binary.AbstractBinaryArraysTest) Test(org.junit.Test)

Example 2 with TransactionView

use of org.apache.ignite.spi.systemview.view.TransactionView in project ignite by apache.

the class IgniteTxManager method start0.

/**
 * {@inheritDoc}
 */
@Override
protected void start0() throws IgniteCheckedException {
    txHnd = new IgniteTxHandler(cctx);
    deferredAckMsgSnd = new GridDeferredAckMessageSender<GridCacheVersion>(cctx.time(), cctx.kernalContext().closure()) {

        @Override
        public int getTimeout() {
            return DEFERRED_ONE_PHASE_COMMIT_ACK_REQUEST_TIMEOUT;
        }

        @Override
        public int getBufferSize() {
            return DEFERRED_ONE_PHASE_COMMIT_ACK_REQUEST_BUFFER_SIZE;
        }

        @Override
        public void finish(UUID nodeId, Collection<GridCacheVersion> vers) {
            GridDhtTxOnePhaseCommitAckRequest ackReq = new GridDhtTxOnePhaseCommitAckRequest(vers);
            cctx.kernalContext().gateway().readLock();
            try {
                cctx.io().send(nodeId, ackReq, GridIoPolicy.SYSTEM_POOL);
            } catch (ClusterTopologyCheckedException ignored) {
                if (log.isDebugEnabled())
                    log.debug("Failed to send one phase commit ack to backup node because it left grid: " + nodeId);
            } catch (IgniteCheckedException e) {
                log.error("Failed to send one phase commit ack to backup node [backup=" + nodeId + ']', e);
            } finally {
                cctx.kernalContext().gateway().readUnlock();
            }
        }
    };
    cctx.gridEvents().addDiscoveryEventListener(new TransactionRecoveryListener(), EVT_NODE_FAILED, EVT_NODE_LEFT);
    cctx.gridEvents().addDiscoveryEventListener(new DiscoveryEventListener() {

        @Override
        public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
            if (evt.type() == EVT_NODE_FAILED || evt.type() == EVT_NODE_LEFT) {
                UUID nodeId = evt.eventNode().id();
                for (TxDeadlockFuture fut : deadlockDetectFuts.values()) fut.onNodeLeft(nodeId);
                for (Map.Entry<GridCacheVersion, Object> entry : completedVersHashMap.entrySet()) {
                    Object obj = entry.getValue();
                    if (obj instanceof GridCacheReturnCompletableWrapper && nodeId.equals(((GridCacheReturnCompletableWrapper) obj).nodeId()))
                        removeTxReturn(entry.getKey());
                }
            }
            suspendResumeForPessimisticSupported = IgniteFeatures.allNodesSupports(cctx.discovery().remoteNodes(), IgniteFeatures.SUSPEND_RESUME_PESSIMISTIC_TX);
        }
    }, EVT_NODE_FAILED, EVT_NODE_LEFT, EVT_NODE_JOINED);
    this.txDeadlockDetection = new TxDeadlockDetection(cctx);
    cctx.gridIO().addMessageListener(TOPIC_TX, new DeadlockDetectionListener());
    cctx.txMetrics().onTxManagerStarted();
    keyCollisionsInfo = new KeyCollisionsHolder();
    distributedTransactionConfiguration = new DistributedTransactionConfiguration(cctx.kernalContext(), log, (String name, Long oldVal, Long newVal) -> {
        if (!Objects.equals(oldVal, newVal)) {
            scheduleDumpTask(IGNITE_LONG_OPERATIONS_DUMP_TIMEOUT, () -> cctx.kernalContext().closure().runLocalSafe(() -> cctx.kernalContext().cache().context().exchange().dumpLongRunningOperations(newVal)), newVal);
        }
    }, (String name, Integer oldVal, Integer newVal) -> {
        if (!Objects.equals(oldVal, newVal)) {
            scheduleDumpTask(IGNITE_DUMP_TX_COLLISIONS_INTERVAL, this::collectTxCollisionsInfo, newVal);
        }
    });
    cctx.kernalContext().systemView().registerView(TXS_MON_LIST, TXS_MON_LIST_DESC, new TransactionViewWalker(), new ReadOnlyCollectionView2X<>(idMap.values(), nearIdMap.values()), TransactionView::new);
}
Also used : DiscoCache(org.apache.ignite.internal.managers.discovery.DiscoCache) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) DistributedTransactionConfiguration(org.apache.ignite.internal.cluster.DistributedTransactionConfiguration) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) GridNearCacheEntry(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheEntry) GridCacheMapEntry(org.apache.ignite.internal.processors.cache.GridCacheMapEntry) GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TransactionViewWalker(org.apache.ignite.internal.managers.systemview.walker.TransactionViewWalker) TransactionView(org.apache.ignite.spi.systemview.view.TransactionView) UUID(java.util.UUID) DiscoveryEventListener(org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener) GridDhtTxOnePhaseCommitAckRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxOnePhaseCommitAckRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) GridCacheReturnCompletableWrapper(org.apache.ignite.internal.processors.cache.GridCacheReturnCompletableWrapper) TxDeadlockFuture(org.apache.ignite.internal.processors.cache.transactions.TxDeadlockDetection.TxDeadlockFuture) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 3 with TransactionView

use of org.apache.ignite.spi.systemview.view.TransactionView in project ignite by apache.

the class SystemViewSelfTest method testTransactions.

/**
 */
@Test
public void testTransactions() throws Exception {
    try (IgniteEx g = startGrid(0)) {
        IgniteCache<Integer, Integer> cache1 = g.createCache(new CacheConfiguration<Integer, Integer>("c1").setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));
        IgniteCache<Integer, Integer> cache2 = g.createCache(new CacheConfiguration<Integer, Integer>("c2").setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));
        SystemView<TransactionView> txs = g.context().systemView().view(TXS_MON_LIST);
        assertEquals(0, F.size(txs.iterator(), alwaysTrue()));
        CountDownLatch latch = new CountDownLatch(1);
        try {
            AtomicInteger cntr = new AtomicInteger();
            GridTestUtils.runMultiThreadedAsync(() -> {
                try (Transaction tx = g.transactions().withLabel("test").txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    cache1.put(cntr.incrementAndGet(), cntr.incrementAndGet());
                    cache1.put(cntr.incrementAndGet(), cntr.incrementAndGet());
                    latch.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }, 5, "xxx");
            boolean res = waitForCondition(() -> txs.size() == 5, 10_000L);
            assertTrue(res);
            TransactionView txv = txs.iterator().next();
            assertEquals(g.localNode().id(), txv.localNodeId());
            assertEquals(txv.isolation(), REPEATABLE_READ);
            assertEquals(txv.concurrency(), PESSIMISTIC);
            assertEquals(txv.state(), ACTIVE);
            assertNotNull(txv.xid());
            assertFalse(txv.system());
            assertFalse(txv.implicit());
            assertFalse(txv.implicitSingle());
            assertTrue(txv.near());
            assertFalse(txv.dht());
            assertTrue(txv.colocated());
            assertTrue(txv.local());
            assertEquals("test", txv.label());
            assertFalse(txv.onePhaseCommit());
            assertFalse(txv.internal());
            assertEquals(0, txv.timeout());
            assertTrue(txv.startTime() <= System.currentTimeMillis());
            assertEquals(String.valueOf(cacheId(cache1.getName())), txv.cacheIds());
            // Only pessimistic transactions are supported when MVCC is enabled.
            if (Objects.equals(System.getProperty(IgniteSystemProperties.IGNITE_FORCE_MVCC_MODE_IN_TESTS), "true"))
                return;
            GridTestUtils.runMultiThreadedAsync(() -> {
                try (Transaction tx = g.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache1.put(cntr.incrementAndGet(), cntr.incrementAndGet());
                    cache1.put(cntr.incrementAndGet(), cntr.incrementAndGet());
                    cache2.put(cntr.incrementAndGet(), cntr.incrementAndGet());
                    latch.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }, 5, "xxx");
            res = waitForCondition(() -> txs.size() == 10, 10_000L);
            assertTrue(res);
            for (TransactionView tx : txs) {
                if (PESSIMISTIC == tx.concurrency())
                    continue;
                assertEquals(g.localNode().id(), tx.localNodeId());
                assertEquals(tx.isolation(), SERIALIZABLE);
                assertEquals(tx.concurrency(), OPTIMISTIC);
                assertEquals(tx.state(), ACTIVE);
                assertNotNull(tx.xid());
                assertFalse(tx.system());
                assertFalse(tx.implicit());
                assertFalse(tx.implicitSingle());
                assertTrue(tx.near());
                assertFalse(tx.dht());
                assertTrue(tx.colocated());
                assertTrue(tx.local());
                assertNull(tx.label());
                assertFalse(tx.onePhaseCommit());
                assertFalse(tx.internal());
                assertEquals(0, tx.timeout());
                assertTrue(tx.startTime() <= System.currentTimeMillis());
                String s1 = cacheId(cache1.getName()) + "," + cacheId(cache2.getName());
                String s2 = cacheId(cache2.getName()) + "," + cacheId(cache1.getName());
                assertTrue(s1.equals(tx.cacheIds()) || s2.equals(tx.cacheIds()));
            }
        } finally {
            latch.countDown();
        }
        boolean res = waitForCondition(() -> txs.size() == 0, 10_000L);
        assertTrue(res);
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) TransactionView(org.apache.ignite.spi.systemview.view.TransactionView) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Test(org.junit.Test) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)

Aggregations

UUID (java.util.UUID)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TransactionView (org.apache.ignite.spi.systemview.view.TransactionView)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1