コード例 #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void oneOrTheOtherShouldDeadlock() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void OneOrTheOtherShouldDeadlock()
        {
            AtomicInteger deadlockCount         = new AtomicInteger();
            HighlyAvailableGraphDatabase master = _cluster.Master;
            Node masterA = CreateNodeOnMaster(_testLabel, master);
            Node masterB = CreateNodeOnMaster(_testLabel, master);

            HighlyAvailableGraphDatabase slave = _cluster.AnySlave;

            using (Transaction transaction = slave.BeginTx())
            {
                Node slaveA = slave.GetNodeById(masterA.Id);
                Node slaveB = slave.GetNodeById(masterB.Id);
                System.Threading.CountdownEvent latch = new System.Threading.CountdownEvent(1);

                transaction.AcquireWriteLock(slaveB);

                Thread masterTx = new Thread(() =>
                {
                    try
                    {
                        using (Transaction tx = master.BeginTx())
                        {
                            tx.acquireWriteLock(masterA);
                            latch.Signal();
                            tx.acquireWriteLock(masterB);
                        }
                    }
                    catch (DeadlockDetectedException)
                    {
                        deadlockCount.incrementAndGet();
                    }
                });
                masterTx.Start();
                latch.await();

                try
                {
                    transaction.AcquireWriteLock(slaveA);
                }
                catch (DeadlockDetectedException)
                {
                    deadlockCount.incrementAndGet();
                }
                masterTx.Join();
            }

            assertEquals(1, deadlockCount.get());
        }
コード例 #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void transactionShouldReleaseLocksWhenGraphDbIsBeingShutdown()
        public virtual void TransactionShouldReleaseLocksWhenGraphDbIsBeingShutdown()
        {
            // GIVEN
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.kernel.impl.locking.Locks locks = db.getDependencyResolver().resolveDependency(org.neo4j.kernel.impl.locking.Locks.class);
            Locks locks = _db.DependencyResolver.resolveDependency(typeof(Locks));

            assertEquals(0, LockCount(locks));
            Exception exceptionThrownByTxClose = null;

            // WHEN
            try
            {
                using (Transaction tx = _db.beginTx())
                {
                    Node node = _db.createNode();
                    tx.AcquireWriteLock(node);
                    assertThat(LockCount(locks), greaterThanOrEqualTo(1));

                    _db.shutdown();

                    _db.createNode();
                    tx.Success();
                }
            }
            catch (Exception e)
            {
                exceptionThrownByTxClose = e;
            }

            // THEN
            assertThat(exceptionThrownByTxClose, instanceOf(typeof(DatabaseShutdownException)));
            assertFalse(_db.isAvailable(1));
            assertEquals(0, LockCount(locks));
        }
コード例 #3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void releaseReleaseManually() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ReleaseReleaseManually()
        {
            string key  = "name";
            Node   node = GraphDb.createNode();

            Tx.success();
            Transaction current  = Tx.begin();
            Lock        nodeLock = current.AcquireWriteLock(node);

            _worker.beginTx();
            try
            {
                _worker.setProperty(node, key, "ksjd");
                fail("Shouldn't be able to grab it");
            }
            catch (Exception)
            {
            }
            nodeLock.Release();
            _worker.setProperty(node, key, "yo");

            try
            {
                _worker.finishTx();
            }
            catch (ExecutionException)
            {
                // Ok, interrupting the thread while it's waiting for a lock will lead to tx failure.
            }
        }
コード例 #4
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private org.neo4j.graphdb.Transaction takeExclusiveLock(org.neo4j.graphdb.Label testLabel, org.neo4j.kernel.ha.HighlyAvailableGraphDatabase db) throws org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException
        private Transaction TakeExclusiveLock(Label testLabel, HighlyAvailableGraphDatabase db)
        {
            Transaction transaction = Db.beginTx();
            Node        node        = GetNode(db, testLabel);

            transaction.AcquireWriteLock(node);
            return(transaction);
        }
コード例 #5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testMultipleCreate() throws InterruptedException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void TestMultipleCreate()
        {
            const int numThreads = 25;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String uuid = java.util.UUID.randomUUID().toString();
            string uuid = System.Guid.randomUUID().ToString();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.graphdb.Node commonNode;
            Node commonNode;

            using (Transaction tx = _graphDb.beginTx())
            {
                commonNode = _graphDb.createNode();
                tx.Success();
            }

            ExecutorCompletionService <Node> ecs = new ExecutorCompletionService <Node>(Executors.newFixedThreadPool(numThreads));

            for (int i = 0; i < numThreads; i++)
            {
                ecs.submit(() =>
                {
                    using (Transaction tx = _graphDb.beginTx())
                    {
                        Node node = _graphDb.createNode();
                        // Acquire lock
                        tx.AcquireWriteLock(commonNode);
                        Index <Node> index = _graphDb.index().forNodes("uuids");
                        Node existing      = index.get("uuid", uuid).Single;
                        if (existing != null)
                        {
                            throw new Exception("Node already exists");
                        }
                        node.setProperty("uuid", uuid);
                        index.add(node, "uuid", uuid);
                        tx.Success();
                        return(node);
                    }
                });
            }
            int numSucceeded = 0;

            for (int i = 0; i < numThreads; i++)
            {
                try
                {
                    ecs.take().get();
                    ++numSucceeded;
                }
                catch (ExecutionException)
                {
                }
            }
            assertEquals(1, numSucceeded);
        }
コード例 #6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void takeExclusiveLockOnSameNodeAfterSwitch(org.neo4j.graphdb.Label testLabel, org.neo4j.kernel.ha.HighlyAvailableGraphDatabase master, org.neo4j.kernel.ha.HighlyAvailableGraphDatabase db) throws org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException
        private void TakeExclusiveLockOnSameNodeAfterSwitch(Label testLabel, HighlyAvailableGraphDatabase master, HighlyAvailableGraphDatabase db)
        {
            using (Transaction transaction = Db.beginTx())
            {
                Node node = GetNode(master, testLabel);
                transaction.AcquireWriteLock(node);
                node.SetProperty("key", "value");
                transaction.Success();
            }
        }
コード例 #7
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private <T extends org.neo4j.graphdb.PropertyContainer> Resource<T> test(System.Func<T> setup, String... queries) throws InterruptedException, java.util.concurrent.ExecutionException
        private Resource <T> Test <T>(System.Func <T> setup, params string[] queries) where T : Org.Neo4j.Graphdb.PropertyContainer
        {
            System.Threading.CountdownEvent resourceLocked     = new System.Threading.CountdownEvent(1);
            System.Threading.CountdownEvent listQueriesLatch   = new System.Threading.CountdownEvent(1);
            System.Threading.CountdownEvent finishQueriesLatch = new System.Threading.CountdownEvent(1);
            T resource;

            using (Transaction tx = _db.beginTx())
            {
                resource = setup();
                tx.Success();
            }
            _threads.execute(parameter =>
            {
                using (Transaction tx = _db.beginTx())
                {
                    tx.AcquireWriteLock(resource);
                    resourceLocked.Signal();
                    listQueriesLatch.await();
                }
                return(null);
            }, null);
            resourceLocked.await();

            _threads.executeAndAwait(parameter =>
            {
                try
                {
                    using (Transaction tx = _db.beginTx())
                    {
                        foreach (string query in queries)
                        {
                            _db.execute(query).close();
                        }
                        tx.Success();
                    }
                }
                catch (Exception t)
                {
                    Console.WriteLine(t.ToString());
                    Console.Write(t.StackTrace);
                    throw new Exception(t);
                }
                finally
                {
                    finishQueriesLatch.Signal();
                }
                return(null);
            }, null, waitingWhileIn(typeof(GraphDatabaseFacade), "execute"), SECONDS_TIMEOUT, SECONDS);

            return(new Resource <T>(listQueriesLatch, finishQueriesLatch, resource));
        }
コード例 #8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void givenClusterWhenShutdownMasterThenCannotStartTransactionOnSlave() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void GivenClusterWhenShutdownMasterThenCannotStartTransactionOnSlave()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.kernel.ha.HighlyAvailableGraphDatabase master = cluster.getMaster();
            HighlyAvailableGraphDatabase master = _cluster.Master;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.kernel.ha.HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
            HighlyAvailableGraphDatabase slave = _cluster.AnySlave;

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long nodeId;
            long nodeId;

            using (Transaction tx = master.BeginTx())
            {
                nodeId = master.CreateNode().Id;
                tx.Success();
            }

            _cluster.sync();

            // When
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.FutureTask<bool> result = new java.util.concurrent.FutureTask<>(() ->
            FutureTask <bool> result = new FutureTask <bool>(() =>
            {
                try
                {
                    using (Transaction tx = slave.BeginTx())
                    {
                        tx.AcquireWriteLock(slave.GetNodeById(nodeId));
                    }
                }
                catch (Exception e)
                {
                    return(contains(e, typeof(TransactionFailureException)));
                }
                // Fail otherwise
                return(false);
            });

            DatabaseAvailabilityGuard masterGuard = master.DependencyResolver.resolveDependency(typeof(DatabaseAvailabilityGuard));

            masterGuard.AddListener(new UnavailabilityListener(result));

            master.Shutdown();

            // Then
            assertThat(result.get(), equalTo(true));
        }
コード例 #9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(timeout = 30_000) public void terminatingTransactionMustEagerlyReleaseTheirLocks() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void TerminatingTransactionMustEagerlyReleaseTheirLocks()
        {
            AtomicBoolean nodeLockAcquired = new AtomicBoolean();
            AtomicBoolean lockerDone       = new AtomicBoolean();
            BinaryLatch   lockerPause      = new BinaryLatch();
            long          nodeId;

            using (Transaction tx = Database.beginTx())
            {
                nodeId = Database.createNode().Id;
                tx.Success();
            }
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: java.util.concurrent.Future<?> locker = executor.submit(() ->
            Future <object> locker = _executor.submit(() =>
            {
                using (Transaction tx = Database.beginTx())
                {
                    Node node = Database.getNodeById(nodeId);
                    tx.AcquireReadLock(node);
                    nodeLockAcquired.set(true);
                    lockerPause.Await();
                }
                lockerDone.set(true);
            });

            bool proceed;

            do
            {
                proceed = nodeLockAcquired.get();
            } while (!proceed);

            TerminateOngoingTransaction();

            assertFalse(lockerDone.get());                 // but the thread should still be blocked on the latch
            // Yet we should be able to proceed and grab the locks they once held
            using (Transaction tx = Database.beginTx())
            {
                // Write-locking is only possible if their shared lock was released
                tx.AcquireWriteLock(Database.getNodeById(nodeId));
                tx.Success();
            }
            // No exception from our lock client being stopped (e.g. we ended up blocked for too long) or from timeout
            lockerPause.Release();
            locker.get();
            assertTrue(lockerDone.get());
        }
コード例 #10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void canOnlyReleaseOnce()
        public virtual void CanOnlyReleaseOnce()
        {
            Node node = GraphDb.createNode();

            Tx.success();
            Transaction current  = Tx.begin();
            Lock        nodeLock = current.AcquireWriteLock(node);

            nodeLock.Release();
            try
            {
                nodeLock.Release();
                fail("Shouldn't be able to release more than once");
            }
            catch (System.InvalidOperationException)
            {               // Good
            }
        }