示例#1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") private SwitchToSlaveCopyThenBranch newSwitchToSlaveSpy(org.neo4j.io.pagecache.PageCache pageCacheMock, org.neo4j.com.storecopy.StoreCopyClient storeCopyClient)
        private SwitchToSlaveCopyThenBranch NewSwitchToSlaveSpy(PageCache pageCacheMock, StoreCopyClient storeCopyClient)
        {
            ClusterMembers clusterMembers = mock(typeof(ClusterMembers));
            ClusterMember  master         = mock(typeof(ClusterMember));

            when(master.StoreId).thenReturn(_storeId);
            when(master.HARole).thenReturn(HighAvailabilityModeSwitcher.MASTER);
            when(master.HasRole(eq(HighAvailabilityModeSwitcher.MASTER))).thenReturn(true);
            when(master.InstanceId).thenReturn(new InstanceId(1));
            when(clusterMembers.Members).thenReturn(asList(master));

            Dependencies         resolver             = new Dependencies();
            DatabaseAvailability databaseAvailability = mock(typeof(DatabaseAvailability));

            when(databaseAvailability.Started).thenReturn(true);
            resolver.SatisfyDependencies(_requestContextFactory, clusterMembers, mock(typeof(TransactionObligationFulfiller)), mock(typeof(OnlineBackupKernelExtension)), mock(typeof(IndexConfigStore)), mock(typeof(TransactionCommittingResponseUnpacker)), mock(typeof(DataSourceManager)), mock(typeof(StoreLockerLifecycleAdapter)), mock(typeof(FileSystemWatcherService)), databaseAvailability);

            NeoStoreDataSource dataSource = mock(typeof(NeoStoreDataSource));

            when(dataSource.StoreId).thenReturn(_storeId);
            when(dataSource.DependencyResolver).thenReturn(resolver);

            DatabaseTransactionStats transactionCounters = mock(typeof(DatabaseTransactionStats));

            when(transactionCounters.NumberOfActiveTransactions).thenReturn(0L);

            Response <HandshakeResult> response = mock(typeof(Response));

            when(response.ResponseConflict()).thenReturn(new HandshakeResult(42, 2));
            when(_masterClient.handshake(anyLong(), any(typeof(StoreId)))).thenReturn(response);
            when(_masterClient.ProtocolVersion).thenReturn(Org.Neo4j.Kernel.ha.com.slave.MasterClient_Fields.Current);

            TransactionIdStore transactionIdStoreMock = mock(typeof(TransactionIdStore));

            // note that the checksum (the second member of the array) is the same as the one in the handshake mock above
            when(transactionIdStoreMock.LastCommittedTransaction).thenReturn(new TransactionId(42, 42, 42));

            MasterClientResolver masterClientResolver = mock(typeof(MasterClientResolver));

            when(masterClientResolver.Instantiate(anyString(), anyInt(), anyString(), any(typeof(Monitors)), argThat(_storeId => true), any(typeof(LifeSupport)))).thenReturn(_masterClient);

            return(spy(new SwitchToSlaveCopyThenBranch(TestDirectory.databaseLayout(), NullLogService.Instance, ConfigMock(), mock(typeof(HaIdGeneratorFactory)), mock(typeof(DelegateInvocationHandler)), mock(typeof(ClusterMemberAvailability)), _requestContextFactory, _pullerFactory, masterClientResolver, mock(typeof(SwitchToSlave.Monitor)), storeCopyClient, Suppliers.singleton(dataSource), Suppliers.singleton(transactionIdStoreMock), slave =>
            {
                SlaveServer server = mock(typeof(SlaveServer));
                InetSocketAddress inetSocketAddress = InetSocketAddress.createUnresolved("localhost", 42);

                when(server.SocketAddress).thenReturn(inetSocketAddress);
                return server;
            }, _updatePuller, pageCacheMock, mock(typeof(Monitors)), () => transactionCounters)));
        }
示例#2
0
        internal ModularDatabaseCreationContext(string databaseName, PlatformModule platformModule, DatabaseEditionContext editionContext, Procedures procedures, GraphDatabaseFacade facade)
        {
            this._databaseName = databaseName;
            this._config       = platformModule.Config;
            DatabaseIdContext idContext = editionContext.IdContext;

            this._idGeneratorFactory = idContext.IdGeneratorFactory;
            this._idController       = idContext.IdController;
            this._databaseLayout     = platformModule.StoreLayout.databaseLayout(databaseName);
            this._logService         = platformModule.Logging;
            this._scheduler          = platformModule.JobScheduler;
            this._globalDependencies = platformModule.Dependencies;
            this._tokenHolders       = editionContext.CreateTokenHolders();
            this._tokenNameLookup    = new NonTransactionalTokenNameLookup(_tokenHolders);
            this._locks = editionContext.CreateLocks();
            this._statementLocksFactory    = editionContext.CreateStatementLocksFactory();
            this._schemaWriteGuard         = editionContext.SchemaWriteGuard;
            this._transactionEventHandlers = new TransactionEventHandlers(facade);
            this._monitors = new Monitors(platformModule.Monitors);
            this._indexingServiceMonitor = _monitors.newMonitor(typeof(IndexingService.Monitor));
            this._physicalLogMonitor     = _monitors.newMonitor(typeof(LogFileCreationMonitor));
            this._fs = platformModule.FileSystem;
            this._transactionStats = editionContext.CreateTransactionMonitor();
            this._databaseHealth   = new DatabaseHealth(platformModule.PanicEventGenerator, _logService.getInternalLog(typeof(DatabaseHealth)));
            this._transactionHeaderInformationFactory = editionContext.HeaderInformationFactory;
            this._commitProcessFactory  = editionContext.CommitProcessFactory;
            this._autoIndexing          = new InternalAutoIndexing(platformModule.Config, _tokenHolders.propertyKeyTokens());
            this._indexConfigStore      = new IndexConfigStore(_databaseLayout, _fs);
            this._explicitIndexProvider = new DefaultExplicitIndexProvider();
            this._pageCache             = platformModule.PageCache;
            this._constraintSemantics   = editionContext.ConstraintSemantics;
            this._tracers    = platformModule.Tracers;
            this._procedures = procedures;
            this._ioLimiter  = editionContext.IoLimiter;
            this._clock      = platformModule.Clock;
            this._databaseAvailabilityGuard    = editionContext.CreateDatabaseAvailabilityGuard(_clock, _logService, _config);
            this._databaseAvailability         = new DatabaseAvailability(_databaseAvailabilityGuard, _transactionStats, platformModule.Clock, AwaitActiveTransactionDeadlineMillis);
            this._coreAPIAvailabilityGuard     = new CoreAPIAvailabilityGuard(_databaseAvailabilityGuard, editionContext.TransactionStartTimeout);
            this._accessCapability             = editionContext.AccessCapability;
            this._storeCopyCheckPointMutex     = new StoreCopyCheckPointMutex();
            this._recoveryCleanupWorkCollector = platformModule.RecoveryCleanupWorkCollector;
            this._databaseInfo               = platformModule.DatabaseInfo;
            this._versionContextSupplier     = platformModule.VersionContextSupplier;
            this._collectionsFactorySupplier = platformModule.CollectionsFactorySupplier;
            this._kernelExtensionFactories   = platformModule.KernelExtensionFactories;
            this._watcherServiceFactory      = editionContext.WatcherServiceFactory;
            this._facade          = facade;
            this._engineProviders = platformModule.EngineProviders;
        }
示例#3
0
        /// <summary>
        /// Performs a switch to the slave state. Starts the communication endpoints, switches components to the slave state
        /// and ensures that the current database is appropriate for this cluster. It also broadcasts the appropriate
        /// Slave Is Available event
        /// </summary>
        /// <param name="haCommunicationLife"> The LifeSupport instance to register the network facilities required for
        ///                            communication with the rest of the cluster </param>
        /// <param name="me"> The URI this instance must bind to </param>
        /// <param name="masterUri"> The URI of the master for which this instance must become slave to </param>
        /// <param name="cancellationRequest"> A handle for gracefully aborting the switch </param>
        /// <returns> The URI that was broadcasted as the slave endpoint or null if the task was cancelled </returns>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public java.net.URI switchToSlave(org.neo4j.kernel.lifecycle.LifeSupport haCommunicationLife, java.net.URI me, java.net.URI masterUri, org.neo4j.helpers.CancellationRequest cancellationRequest) throws Throwable
//JAVA TO C# CONVERTER NOTE: Members cannot have the same name as their enclosing type:
        public virtual URI SwitchToSlaveConflict(LifeSupport haCommunicationLife, URI me, URI masterUri, CancellationRequest cancellationRequest)
        {
            URI  slaveUri;
            bool success = false;

            Monitor.switchToSlaveStarted();

            // Wait a short while for current transactions to stop first, just to be nice.
            // We can't wait forever since switching to our designated role is quite important.
            Clock clock    = Clocks.systemClock();
            long  deadline = clock.millis() + Config.get(HaSettings.internal_state_switch_timeout).toMillis();
            DatabaseTransactionStats transactionStats = _transactionStatsSupplier.get();

            while (transactionStats.NumberOfActiveTransactions > 0 && clock.millis() < deadline)
            {
                parkNanos(MILLISECONDS.toNanos(10));
            }

            try
            {
                InstanceId myId = Config.get(ClusterSettings.server_id);

                UserLog.info("ServerId %s, moving to slave for master %s", myId, masterUri);

                Debug.Assert(masterUri != null);                           // since we are here it must already have been set from outside

                _idGeneratorFactory.switchToSlave();

                CopyStoreFromMasterIfNeeded(masterUri, me, cancellationRequest);

                /*
                 * The following check is mandatory, since the store copy can be cancelled and if it was actually
                 * happening then we can't continue, as there is no store in place
                 */
                if (cancellationRequest.CancellationRequested())
                {
                    MsgLog.info("Switch to slave cancelled during store copy if no local store is present.");
                    return(null);
                }

                /*
                 * We get here either with a fresh store from the master copy above so we need to
                 * start the ds or we already had a store, so we have already started the ds. Either way,
                 * make sure it's there.
                 */
                NeoStoreDataSource neoDataSource = _neoDataSourceSupplier.get();
                neoDataSource.AfterModeSwitch();
                StoreId myStoreId = neoDataSource.StoreId;

                bool consistencyChecksExecutedSuccessfully = ExecuteConsistencyChecks(_transactionIdStoreSupplier.get(), masterUri, me, myStoreId, cancellationRequest);

                if (!consistencyChecksExecutedSuccessfully)
                {
                    MsgLog.info("Switch to slave cancelled due to consistency check failure.");
                    return(null);
                }

                if (cancellationRequest.CancellationRequested())
                {
                    MsgLog.info("Switch to slave cancelled after consistency checks.");
                    return(null);
                }

                // no exception were thrown and we can proceed
                slaveUri = StartHaCommunication(haCommunicationLife, neoDataSource, me, masterUri, myStoreId, cancellationRequest);
                if (slaveUri == null)
                {
                    MsgLog.info("Switch to slave unable to connect.");
                    return(null);
                }

                success = true;
                UserLog.info("ServerId %s, successfully moved to slave for master %s", myId, masterUri);
            }
            finally
            {
                Monitor.switchToSlaveCompleted(success);
            }

            return(slaveUri);
        }
示例#4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void whenHAModeSwitcherSwitchesToSlaveTheOtherModeSwitcherDoNotGetTheOldMasterClient() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void WhenHAModeSwitcherSwitchesToSlaveTheOtherModeSwitcherDoNotGetTheOldMasterClient()
        {
            InstanceId me      = new InstanceId(1);
            StoreId    storeId = newStoreIdForCurrentVersion();
            HighAvailabilityMemberContext context = mock(typeof(HighAvailabilityMemberContext));

            when(context.MyId).thenReturn(me);
            AvailabilityGuard      guard        = mock(typeof(DatabaseAvailabilityGuard));
            ObservedClusterMembers members      = mock(typeof(ObservedClusterMembers));
            ClusterMember          masterMember = mock(typeof(ClusterMember));

            when(masterMember.HARole).thenReturn("master");
            when(masterMember.HasRole("master")).thenReturn(true);
            when(masterMember.InstanceId).thenReturn(new InstanceId(2));
            when(masterMember.StoreId).thenReturn(storeId);
            ClusterMember self = new ClusterMember(me);

            when(members.Members).thenReturn(Arrays.asList(self, masterMember));
            when(members.CurrentMember).thenReturn(self);
            DependencyResolver    dependencyResolver = mock(typeof(DependencyResolver));
            FileSystemAbstraction fs = mock(typeof(FileSystemAbstraction));

            when(fs.FileExists(any(typeof(File)))).thenReturn(true);
            when(dependencyResolver.ResolveDependency(typeof(FileSystemAbstraction))).thenReturn(fs);
            when(dependencyResolver.ResolveDependency(typeof(Monitors))).thenReturn(new Monitors());
            NeoStoreDataSource dataSource = mock(typeof(NeoStoreDataSource));

            when(dataSource.DependencyResolver).thenReturn(dependencyResolver);
            when(dataSource.StoreId).thenReturn(storeId);
            when(dependencyResolver.ResolveDependency(typeof(NeoStoreDataSource))).thenReturn(dataSource);
            when(dependencyResolver.ResolveDependency(typeof(TransactionIdStore))).thenReturn(new SimpleTransactionIdStore());
            when(dependencyResolver.ResolveDependency(typeof(ObservedClusterMembers))).thenReturn(members);
            UpdatePuller updatePuller = mock(typeof(UpdatePuller));

            when(updatePuller.TryPullUpdates()).thenReturn(true);
            when(dependencyResolver.ResolveDependency(typeof(UpdatePuller))).thenReturn(updatePuller);

            ClusterMemberAvailability clusterMemberAvailability = mock(typeof(ClusterMemberAvailability));
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final TriggerableClusterMemberEvents events = new TriggerableClusterMemberEvents();
            TriggerableClusterMemberEvents events = new TriggerableClusterMemberEvents();

            Election election = mock(typeof(Election));
            HighAvailabilityMemberStateMachine stateMachine = new HighAvailabilityMemberStateMachine(context, guard, members, events, election, NullLogProvider.Instance);

            ClusterMembers clusterMembers = new ClusterMembers(members, stateMachine);

            when(dependencyResolver.ResolveDependency(typeof(ClusterMembers))).thenReturn(clusterMembers);

            stateMachine.Init();
            stateMachine.Start();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.kernel.ha.DelegateInvocationHandler<org.neo4j.kernel.ha.com.master.Master> handler = new org.neo4j.kernel.ha.DelegateInvocationHandler<>(org.neo4j.kernel.ha.com.master.Master.class);
            DelegateInvocationHandler <Master> handler = new DelegateInvocationHandler <Master>(typeof(Master));

            MasterClientResolver masterClientResolver = mock(typeof(MasterClientResolver));
            MasterClient         masterClient         = mock(typeof(MasterClient));

            when(masterClient.ProtocolVersion).thenReturn(MasterClient214.PROTOCOL_VERSION);
            when(masterClient.Handshake(anyLong(), any(typeof(StoreId)))).thenReturn(new ResponseAnonymousInnerClass(this, storeId, mock(typeof(ResourceReleaser)), handler));
            when(masterClient.ToString()).thenReturn("TheExpectedMasterClient!");
            when(masterClientResolver.Instantiate(anyString(), anyInt(), anyString(), any(typeof(Monitors)), any(typeof(StoreId)), any(typeof(LifeSupport)))).thenReturn(masterClient);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.CountDownLatch latch = new java.util.concurrent.CountDownLatch(2);
            System.Threading.CountdownEvent latch = new System.Threading.CountdownEvent(2);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicBoolean switchedSuccessfully = new java.util.concurrent.atomic.AtomicBoolean();
            AtomicBoolean switchedSuccessfully = new AtomicBoolean();

            SwitchToSlave.Monitor monitor = new MonitorAnonymousInnerClass(this, latch, switchedSuccessfully);

            Config config = Config.defaults(ClusterSettings.server_id, me.ToString());

            DatabaseTransactionStats transactionCounters = mock(typeof(DatabaseTransactionStats));

            when(transactionCounters.NumberOfActiveTransactions).thenReturn(0L);

            PageCache pageCacheMock = mock(typeof(PageCache));
            PagedFile pagedFileMock = mock(typeof(PagedFile));

            when(pagedFileMock.LastPageId).thenReturn(1L);
            when(pageCacheMock.Map(any(typeof(File)), anyInt())).thenReturn(pagedFileMock);

            TransactionIdStore transactionIdStoreMock = mock(typeof(TransactionIdStore));

            when(transactionIdStoreMock.LastCommittedTransaction).thenReturn(new TransactionId(0, 0, 0));
            SwitchToSlaveCopyThenBranch switchToSlave = new SwitchToSlaveCopyThenBranch(DatabaseLayout.of(new File("")), NullLogService.Instance, mock(typeof(FileSystemAbstraction)), config, mock(typeof(HaIdGeneratorFactory)), handler, mock(typeof(ClusterMemberAvailability)), mock(typeof(RequestContextFactory)), mock(typeof(PullerFactory), RETURNS_MOCKS), Iterables.empty(), masterClientResolver, monitor, new Org.Neo4j.com.storecopy.StoreCopyClientMonitor_Adapter(), Suppliers.singleton(dataSource), Suppliers.singleton(transactionIdStoreMock), slave =>
            {
                SlaveServer mock = mock(typeof(SlaveServer));
                when(mock.SocketAddress).thenReturn(new InetSocketAddress("localhost", 123));
                return(mock);
            }, updatePuller, pageCacheMock, mock(typeof(Monitors)), () => transactionCounters);

            ComponentSwitcherContainer   switcherContainer = new ComponentSwitcherContainer();
            HighAvailabilityModeSwitcher haModeSwitcher    = new HighAvailabilityModeSwitcher(switchToSlave, mock(typeof(SwitchToMaster)), election, clusterMemberAvailability, mock(typeof(ClusterClient)), storeSupplierMock(), me, switcherContainer, NeoStoreDataSourceSupplierMock(), NullLogService.Instance);

            haModeSwitcher.Init();
            haModeSwitcher.Start();
            haModeSwitcher.ListeningAt(URI.create("http://localhost:12345"));

            stateMachine.AddHighAvailabilityMemberListener(haModeSwitcher);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicReference<org.neo4j.kernel.ha.com.master.Master> ref = new java.util.concurrent.atomic.AtomicReference<>(null);
            AtomicReference <Master> @ref = new AtomicReference <Master>(null);

            //noinspection unchecked
            AbstractComponentSwitcher <object> otherModeSwitcher = new AbstractComponentSwitcherAnonymousInnerClass(this, mock(typeof(DelegateInvocationHandler)), handler, latch, @ref);

            switcherContainer.Add(otherModeSwitcher);
            // When
            events.SwitchToSlave(me);

            // Then
            latch.await();
            assertTrue("mode switch failed", switchedSuccessfully.get());
            Master actual = @ref.get();

            // let's test the toString()s since there are too many wrappers of proxies
            assertEquals(masterClient.ToString(), actual.ToString());

            stateMachine.Stop();
            stateMachine.Shutdown();
            haModeSwitcher.Stop();
            haModeSwitcher.Shutdown();
        }