Exemplo n.º 1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void givenClusterWithReadOnlySlaveWhenChangePropertyOnSlaveThenThrowException()
        public virtual void GivenClusterWithReadOnlySlaveWhenChangePropertyOnSlaveThenThrowException()
        {
            // Given
            ManagedCluster cluster = ClusterRule.startCluster();
            Node           node;
            HighlyAvailableGraphDatabase master = cluster.Master;

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

            // When
            HighlyAvailableGraphDatabase readOnlySlave = cluster.GetMemberByServerId(new InstanceId(2));

            try
            {
                using (Transaction tx = readOnlySlave.BeginTx())
                {
                    Node slaveNode = readOnlySlave.GetNodeById(node.Id);

                    // Then
                    slaveNode.SetProperty("foo", "bar");
                    tx.Success();
                    fail("Should have thrown exception");
                }
            }
            catch (WriteOperationsNotAllowedException)
            {
                // Ok!
            }
        }
Exemplo n.º 2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static void createClusterWithNode(org.neo4j.kernel.impl.ha.ClusterManager clusterManager) throws Throwable
        private static void CreateClusterWithNode(ClusterManager clusterManager)
        {
            try
            {
                clusterManager.Start();

                clusterManager.Cluster.await(allSeesAllAsAvailable());

                long nodeId;
                HighlyAvailableGraphDatabase master = clusterManager.Cluster.Master;
                using (Transaction tx = master.BeginTx())
                {
                    Node node = master.CreateNode();
                    nodeId = node.Id;
                    node.SetProperty("foo", "bar");
                    tx.Success();
                }

                HighlyAvailableGraphDatabase slave = clusterManager.Cluster.AnySlave;
                using (Transaction ignored = slave.BeginTx())
                {
                    Node node = slave.GetNodeById(nodeId);
                    assertThat(node.GetProperty("foo").ToString(), CoreMatchers.equalTo("bar"));
                }
            }
            finally
            {
                clusterManager.SafeShutdown();
            }
        }
Exemplo n.º 3
0
 private void CreateSingleTestLabeledNode(HighlyAvailableGraphDatabase master)
 {
     using (Transaction tx = master.BeginTx())
     {
         master.CreateNode(_testLabel);
         tx.Success();
     }
 }
Exemplo n.º 4
0
        private Node CreateNodeOnMaster(Label testLabel, HighlyAvailableGraphDatabase master)
        {
            Node node;

            using (Transaction transaction = master.BeginTx())
            {
                node = master.CreateNode(testLabel);
                transaction.Success();
            }
            return(node);
        }
Exemplo n.º 5
0
        private void CreateLabeledNodeOnMaster(ClusterManager.ManagedCluster cluster, Label label)
        {
            HighlyAvailableGraphDatabase master = cluster.Master;

            using (Transaction transaction = master.BeginTx())
            {
                Node masterNode = master.CreateNode();
                masterNode.AddLabel(label);
                transaction.Success();
            }
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
0
 private long CreateNode(HighlyAvailableGraphDatabase author, string key, object value, bool index)
 {
     using (Transaction tx = author.BeginTx())
     {
         Node node = author.CreateNode();
         node.SetProperty(key, value);
         if (index)
         {
             author.Index().forNodes(key).add(node, key, value);
         }
         tx.Success();
         return(node.Id);
     }
 }
Exemplo n.º 8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void givenClusterWithReadOnlySlaveWhenAddNewRelTypeOnSlaveThenThrowException()
        public virtual void GivenClusterWithReadOnlySlaveWhenAddNewRelTypeOnSlaveThenThrowException()
        {
            // Given
            ManagedCluster cluster = ClusterRule.startCluster();
            Node           node;
            Node           node2;
            HighlyAvailableGraphDatabase master = cluster.Master;

            using (Transaction tx = master.BeginTx())
            {
                node  = master.CreateNode();
                node2 = master.CreateNode();
                tx.Success();
            }

            // When
            HighlyAvailableGraphDatabase readOnlySlave = cluster.GetMemberByServerId(new InstanceId(2));

            try
            {
                using (Transaction tx = readOnlySlave.BeginTx())
                {
                    Node slaveNode  = readOnlySlave.GetNodeById(node.Id);
                    Node slaveNode2 = readOnlySlave.GetNodeById(node2.Id);

                    // Then
                    slaveNode.CreateRelationshipTo(slaveNode2, RelationshipType.withName("KNOWS"));
                    tx.Success();
                    fail("Should have thrown exception");
                }
            }
            catch (WriteOperationsNotAllowedException)
            {
                // Ok!
            }
        }
Exemplo n.º 9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void slaveMustConnectLockManagerToNewMasterAfterTwoOtherClusterMembersRoleSwitch() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void SlaveMustConnectLockManagerToNewMasterAfterTwoOtherClusterMembersRoleSwitch()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.kernel.ha.HighlyAvailableGraphDatabase initialMaster = cluster.getMaster();
            HighlyAvailableGraphDatabase initialMaster = _cluster.Master;
            HighlyAvailableGraphDatabase firstSlave    = _cluster.AnySlave;
            HighlyAvailableGraphDatabase secondSlave   = _cluster.getAnySlave(firstSlave);

            // Run a transaction on the slaves, to make sure that a master connection has been initialised in all
            // internal pools.
            using (Transaction tx = firstSlave.BeginTx())
            {
                firstSlave.CreateNode();
                tx.Success();
            }
            using (Transaction tx = secondSlave.BeginTx())
            {
                secondSlave.CreateNode();
                tx.Success();
            }
            _cluster.sync();

            ClusterManager.RepairKit failedMaster = _cluster.fail(initialMaster);
            _cluster.await(ClusterManager.masterAvailable(initialMaster));
            failedMaster.Repair();
            _cluster.await(ClusterManager.masterAvailable(initialMaster));
            _cluster.await(ClusterManager.allSeesAllAsAvailable());

            // The cluster has now switched the master role to one of the slaves.
            // The slave that didn't switch, should still have done the work to reestablish the connection to the new
            // master.
            HighlyAvailableGraphDatabase slave = _cluster.getAnySlave(initialMaster);

            using (Transaction tx = slave.BeginTx())
            {
                slave.CreateNode();
                tx.Success();
            }

            // We assert that the transaction above does not throw any exceptions, and that we have now created 3 nodes.
            HighlyAvailableGraphDatabase master = _cluster.Master;

            using (Transaction tx = master.BeginTx())
            {
                assertThat(Iterables.count(master.AllNodes), @is(3L));
            }
        }
Exemplo n.º 10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void givenClusterWhenMasterGoesDownAndTxIsRunningThenDontWaitToSwitch() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void GivenClusterWhenMasterGoesDownAndTxIsRunningThenDontWaitToSwitch()
        {
            ClusterManager clusterManager = (new ClusterManager.Builder(TestDirectory.directory(TestName.MethodName))).withCluster(ClusterManager.clusterOfSize(3)).build();

            try
            {
                clusterManager.Start();
                ClusterManager.ManagedCluster cluster = clusterManager.Cluster;
                cluster.Await(allSeesAllAsAvailable());

                HighlyAvailableGraphDatabase slave = cluster.AnySlave;

                Transaction tx = slave.BeginTx();
                // Do a little write operation so that all "write" aspects of this tx is initializes properly
                slave.CreateNode();

                // Shut down master while we're keeping this transaction open
                cluster.Shutdown(cluster.Master);

                cluster.Await(masterAvailable());
                cluster.Await(masterSeesSlavesAsAvailable(1));
                // Ending up here means that we didn't wait for this transaction to complete

                tx.Success();

                try
                {
                    tx.Close();
                    fail("Exception expected");
                }
                catch (Exception e)
                {
                    assertThat(e, instanceOf(typeof(TransientTransactionFailureException)));
                    Exception rootCause = rootCause(e);
                    assertThat(rootCause, instanceOf(typeof(TransactionTerminatedException)));
                    assertThat((( TransactionTerminatedException )rootCause).status(), Matchers.equalTo(Org.Neo4j.Kernel.Api.Exceptions.Status_General.DatabaseUnavailable));
                }
            }
            finally
            {
                clusterManager.Stop();
            }
        }
Exemplo n.º 11
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void givenClusterWithReadOnlySlaveWhenWriteTxOnSlaveThenCommitFails()
        public virtual void GivenClusterWithReadOnlySlaveWhenWriteTxOnSlaveThenCommitFails()
        {
            // When
            ManagedCluster cluster = ClusterRule.startCluster();
            HighlyAvailableGraphDatabase readOnlySlave = cluster.GetMemberByServerId(new InstanceId(2));

            try
            {
                using (Transaction tx = readOnlySlave.BeginTx())
                {
                    readOnlySlave.CreateNode();
                    tx.Success();
                    fail("Should have thrown exception");
                }
            }
            catch (WriteOperationsNotAllowedException)
            {
                // Then
            }
        }
Exemplo n.º 12
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void doNotAcquireSharedLocksDuringSlaveReadTx()
        public virtual void DoNotAcquireSharedLocksDuringSlaveReadTx()
        {
            HighlyAvailableGraphDatabase anySlave = _managedCluster.AnySlave;
            HighlyAvailableGraphDatabase master   = _managedCluster.Master;

            using (Transaction tx = master.BeginTx())
            {
                Node node = master.CreateNode(_testLabel);
                node.SetProperty(TEST_PROPERTY, "a");
                tx.Success();
            }

            CreateIndex(master, _testLabel, TEST_PROPERTY);

            using (Transaction transaction = anySlave.BeginTx())
            {
                assertEquals(1, Iterables.count(anySlave.Schema().getIndexes(_testLabel)));
                transaction.Success();
            }
            assertTrue(GetRequestedLocks(anySlave).Count == 0);
        }
Exemplo n.º 13
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void givenClusterWithReadOnlySlaveWhenCreatingNodeOnMasterThenSlaveShouldBeAbleToPullUpdates()
        public virtual void GivenClusterWithReadOnlySlaveWhenCreatingNodeOnMasterThenSlaveShouldBeAbleToPullUpdates()
        {
            ManagedCluster cluster = ClusterRule.startCluster();
            HighlyAvailableGraphDatabase master = cluster.Master;
            Label label = Label.label("label");

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

            IEnumerable <HighlyAvailableGraphDatabase> allMembers = cluster.AllMembers;

            foreach (HighlyAvailableGraphDatabase member in allMembers)
            {
                using (Transaction tx = member.BeginTx())
                {
                    long count = count(member.FindNodes(label));
                    tx.Success();
                    assertEquals(1, count);
                }
            }
        }
Exemplo n.º 14
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldPullUpdatesOnStartupNoMatterWhat() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldPullUpdatesOnStartupNoMatterWhat()
        {
            HighlyAvailableGraphDatabase slave  = null;
            HighlyAvailableGraphDatabase master = null;

            try
            {
                File testRootDir       = ClusterRule.cleanDirectory("shouldPullUpdatesOnStartupNoMatterWhat");
                File masterDir         = ClusterRule.TestDirectory.databaseDir("master");
                int  masterClusterPort = PortAuthority.allocatePort();
                master = ( HighlyAvailableGraphDatabase )(new TestHighlyAvailableGraphDatabaseFactory()).newEmbeddedDatabaseBuilder(masterDir).setConfig(ClusterSettings.server_id, "1").setConfig(ClusterSettings.cluster_server, "127.0.0.1:" + masterClusterPort).setConfig(ClusterSettings.initial_hosts, "localhost:" + masterClusterPort).setConfig(HaSettings.ha_server, "127.0.0.1:" + PortAuthority.allocatePort()).setConfig(OnlineBackupSettings.online_backup_enabled, Settings.FALSE).newGraphDatabase();

                // Copy the store, then shutdown, so update pulling later makes sense
                File slaveDir = ClusterRule.TestDirectory.databaseDir("slave");
                slave = ( HighlyAvailableGraphDatabase )(new TestHighlyAvailableGraphDatabaseFactory()).newEmbeddedDatabaseBuilder(slaveDir).setConfig(ClusterSettings.server_id, "2").setConfig(ClusterSettings.cluster_server, "127.0.0.1:" + PortAuthority.allocatePort()).setConfig(ClusterSettings.initial_hosts, "localhost:" + masterClusterPort).setConfig(HaSettings.ha_server, "127.0.0.1:" + PortAuthority.allocatePort()).setConfig(OnlineBackupSettings.online_backup_enabled, Settings.FALSE).newGraphDatabase();

                // Required to block until the slave has left for sure
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.CountDownLatch slaveLeftLatch = new java.util.concurrent.CountDownLatch(1);
                System.Threading.CountdownEvent slaveLeftLatch = new System.Threading.CountdownEvent(1);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.cluster.client.ClusterClient masterClusterClient = master.getDependencyResolver().resolveDependency(org.neo4j.cluster.client.ClusterClient.class);
                ClusterClient masterClusterClient = master.DependencyResolver.resolveDependency(typeof(ClusterClient));
                masterClusterClient.AddClusterListener(new ClusterListener_AdapterAnonymousInnerClass(this, slaveLeftLatch, masterClusterClient));

                master.DependencyResolver.resolveDependency(typeof(LogService)).getInternalLog(this.GetType()).info("SHUTTING DOWN SLAVE");
                slave.Shutdown();
                slave = null;

                // Make sure that the slave has left, because shutdown() may return before the master knows
                assertTrue("Timeout waiting for slave to leave", slaveLeftLatch.await(60, TimeUnit.SECONDS));

                long nodeId;
                using (Transaction tx = master.BeginTx())
                {
                    Node node = master.CreateNode();
                    node.SetProperty("from", "master");
                    nodeId = node.Id;
                    tx.Success();
                }

                // Store is already in place, should pull updates
                slave = ( HighlyAvailableGraphDatabase )(new TestHighlyAvailableGraphDatabaseFactory()).newEmbeddedDatabaseBuilder(slaveDir).setConfig(ClusterSettings.server_id, "2").setConfig(ClusterSettings.cluster_server, "127.0.0.1:" + PortAuthority.allocatePort()).setConfig(ClusterSettings.initial_hosts, "localhost:" + masterClusterPort).setConfig(HaSettings.ha_server, "127.0.0.1:" + PortAuthority.allocatePort()).setConfig(HaSettings.pull_interval, "0").setConfig(OnlineBackupSettings.online_backup_enabled, Settings.FALSE).newGraphDatabase();

                slave.BeginTx().close();                         // Make sure switch to slave completes and so does the update pulling on startup

                using (Transaction tx = slave.BeginTx())
                {
                    assertEquals("master", slave.GetNodeById(nodeId).getProperty("from"));
                    tx.Success();
                }
            }
            finally
            {
                if (slave != null)
                {
                    slave.Shutdown();
                }
                if (master != null)
                {
                    master.Shutdown();
                }
            }
        }