//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldNotifyCancelListeners() public virtual void ShouldNotifyCancelListeners() { // GIVEN CentralJobScheduler centralJobScheduler = new CentralJobScheduler(); centralJobScheduler.Init(); // WHEN AtomicBoolean halted = new AtomicBoolean(); ThreadStart job = () => { while (!halted.get()) { LockSupport.parkNanos(MILLISECONDS.toNanos(10)); } }; JobHandle handle = centralJobScheduler.Schedule(Group.INDEX_POPULATION, job); handle.RegisterCancelListener(mayBeInterrupted => halted.set(true)); handle.Cancel(false); // THEN assertTrue(halted.get()); centralJobScheduler.Shutdown(); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: private void assertThreadIsWaitingForLock(LockAcquisition lockAcquisition) throws Exception private void AssertThreadIsWaitingForLock(LockAcquisition lockAcquisition) { for (int i = 0; i < 30 && !Suite.isAwaitingLockAcquisition(lockAcquisition.Executor.waitUntilWaiting()); i++) { LockSupport.parkNanos(MILLISECONDS.toNanos(100)); } assertFalse("locking thread completed", lockAcquisition.Completed()); }
private void AwaitTransactionsClosedWithinTimeout() { long deadline = _clock.millis() + _awaitActiveTransactionDeadlineMillis; while (_transactionCounters.NumberOfActiveTransactions > 0 && _clock.millis() < deadline) { parkNanos(MILLISECONDS.toNanos(10)); } }
public static string NanosToString(long nanos) { Debug.Assert(nanos >= 0); long nanoSeconds = nanos; StringBuilder timeString = new StringBuilder(); long days = DAYS.convert(nanoSeconds, NANOSECONDS); if (days > 0) { nanoSeconds -= DAYS.toNanos(days); timeString.Append(days).Append('d'); } long hours = HOURS.convert(nanoSeconds, NANOSECONDS); if (hours > 0) { nanoSeconds -= HOURS.toNanos(hours); timeString.Append(hours).Append('h'); } long minutes = MINUTES.convert(nanoSeconds, NANOSECONDS); if (minutes > 0) { nanoSeconds -= MINUTES.toNanos(minutes); timeString.Append(minutes).Append('m'); } long seconds = SECONDS.convert(nanoSeconds, NANOSECONDS); if (seconds > 0) { nanoSeconds -= SECONDS.toNanos(seconds); timeString.Append(seconds).Append('s'); } long milliseconds = MILLISECONDS.convert(nanoSeconds, NANOSECONDS); if (milliseconds > 0) { nanoSeconds -= MILLISECONDS.toNanos(milliseconds); timeString.Append(milliseconds).Append("ms"); } long microseconds = MICROSECONDS.convert(nanoSeconds, NANOSECONDS); if (microseconds > 0) { nanoSeconds -= MICROSECONDS.toNanos(microseconds); timeString.Append(microseconds).Append("μs"); } if (nanoSeconds > 0 || timeString.Length == 0) { timeString.Append(nanoSeconds).Append("ns"); } return(timeString.ToString()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldHandleTheWholeWorkloadShebang() throws Throwable //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldHandleTheWholeWorkloadShebang() { // GIVEN const int size = 1_000; const long bufferTime = 3; VerifyingConsumer consumer = new VerifyingConsumer(size); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.time.Clock clock = org.neo4j.time.Clocks.systemClock(); Clock clock = Clocks.systemClock(); System.Func <long> chunkThreshold = clock.millis; System.Predicate <long> safeThreshold = time => clock.millis() - bufferTime >= time; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final DelayedBuffer<long> buffer = new DelayedBuffer<>(chunkThreshold, safeThreshold, 10, consumer); DelayedBuffer <long> buffer = new DelayedBuffer <long>(chunkThreshold, safeThreshold, 10, consumer); MaintenanceThread maintenance = new MaintenanceThread(buffer, 5); Race adders = new Race(); const int numberOfAdders = 20; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final byte[] offeredIds = new byte[size]; sbyte[] offeredIds = new sbyte[size]; for (int i = 0; i < numberOfAdders; i++) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int finalI = i; int finalI = i; adders.AddContestant(() => { for (int j = 0; j < size; j++) { if (j % numberOfAdders == finalI) { buffer.Offer(j); offeredIds[j] = 1; parkNanos(MILLISECONDS.toNanos(current().Next(2))); } } }); } // WHEN (multi-threaded) offering of ids adders.Go(); // ... ensuring the test is sane itself (did we really offer all these IDs?) for (int i = 0; i < size; i++) { assertEquals("ID " + i, ( sbyte )1, offeredIds[i]); } maintenance.Halt(); buffer.Close(); // THEN consumer.AssertHaveOnlySeenRange(0, size - 1); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: private void awaitSchemaStateCleared(String keyForProbing) throws org.neo4j.internal.kernel.api.exceptions.TransactionFailureException private void AwaitSchemaStateCleared(string keyForProbing) { using (Transaction transaction = _kernel.beginTransaction(@implicit, AUTH_DISABLED)) { while (transaction.SchemaRead().schemaStateGetOrCreate(keyForProbing, ignored => null) != null) { LockSupport.parkNanos(MILLISECONDS.toNanos(10)); } transaction.Success(); } }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are ignored unless the option to convert to C# 7.2 'in' parameters is selected: //ORIGINAL LINE: private Runnable endAfterMax(final int time, final java.util.concurrent.TimeUnit unit, final java.util.concurrent.atomic.AtomicBoolean end) private ThreadStart EndAfterMax(int time, TimeUnit unit, AtomicBoolean end) { return(() => { long endTime = currentTimeMillis() + unit.toMillis(time); while (currentTimeMillis() < endTime && !end.get()) { parkNanos(MILLISECONDS.toNanos(50)); } end.set(true); }); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test void shouldDoProcessingInitializationInOrder() throws Throwable //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: internal virtual void ShouldDoProcessingInitializationInOrder() { // GIVEN //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final org.neo4j.test.Race race = new org.neo4j.test.Race(); Race race = new Race(); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.util.concurrent.atomic.AtomicInteger coordination = new java.util.concurrent.atomic.AtomicInteger(-1); AtomicInteger coordination = new AtomicInteger(-1); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.util.concurrent.atomic.AtomicInteger expected = new java.util.concurrent.atomic.AtomicInteger(); AtomicInteger expected = new AtomicInteger(); const int threads = 30; //JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @SuppressWarnings("unchecked") final RecordCheckWorker<int>[] workers = new RecordCheckWorker[threads]; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': RecordCheckWorker <int>[] workers = new RecordCheckWorker[threads]; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final RecordProcessor<int> processor = new RecordProcessor_Adapter<int>() RecordProcessor <int> processor = new RecordProcessor_AdapterAnonymousInnerClass(this, expected); for (int id = 0; id < threads; id++) { ArrayBlockingQueue <int> queue = new ArrayBlockingQueue <int>(10); race.AddContestant(workers[id] = new RecordCheckWorker <int>(id, coordination, queue, processor)); } race.AddContestant(() => { try { long end = currentTimeMillis() + SECONDS.toMillis(100); while (currentTimeMillis() < end && expected.get() < threads) { parkNanos(MILLISECONDS.toNanos(10)); } assertEquals(threads, expected.get()); } finally { foreach (RecordCheckWorker <int> worker in workers) { worker.Done(); } } }); // WHEN race.Go(); }
/// <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); }
private static void ParkAWhile() { LockSupport.parkNanos(MILLISECONDS.toNanos(100)); }
public override long CpuTimeNanos(long threadId) { Iteration++; return(MILLISECONDS.toNanos(Iteration * threadId)); }
private static void ParkARandomWhile() { LockSupport.parkNanos(MILLISECONDS.toNanos(ThreadLocalRandom.current().Next(10))); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldBeAbleToSnapshotDuringHeavyLoad() throws Throwable //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldBeAbleToSnapshotDuringHeavyLoad() { // GIVEN //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final KernelTransactions transactions = newKernelTransactions(); KernelTransactions transactions = NewKernelTransactions(); Race race = new Race(); const int threads = 50; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.util.concurrent.atomic.AtomicBoolean end = new java.util.concurrent.atomic.AtomicBoolean(); AtomicBoolean end = new AtomicBoolean(); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.util.concurrent.atomic.AtomicReferenceArray<KernelTransactionsSnapshot> snapshots = new java.util.concurrent.atomic.AtomicReferenceArray<>(threads); AtomicReferenceArray <KernelTransactionsSnapshot> snapshots = new AtomicReferenceArray <KernelTransactionsSnapshot>(threads); // Representing "transaction" threads for (int i = 0; i < threads; i++) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int threadIndex = i; int threadIndex = i; race.AddContestant(() => { ThreadLocalRandom random = ThreadLocalRandom.current(); while (!end.get()) { try { using (KernelTransaction transaction = GetKernelTransaction(transactions)) { KernelTransactionsSnapshot snapshot = null; try { parkNanos(MILLISECONDS.toNanos(random.Next(3))); if (snapshots.get(threadIndex) == null) { requireNonNull(transactions, "transactions is null"); snapshot = requireNonNull(transactions.Get(), "transactions.get() returned null"); snapshots.set(threadIndex, snapshot); parkNanos(MILLISECONDS.toNanos(random.Next(3))); } } catch (Exception e) { StringBuilder sb = (new StringBuilder("Gotcha!\n")).Append("threadIndex=").Append(threadIndex).Append('\n').Append("transaction=").Append(transaction).Append('\n').Append("snapshots=").Append(snapshots).Append('\n').Append("snapshot=").Append(snapshot).Append('\n').Append("end=").Append(end); throw new Exception(sb.ToString(), e); } } } catch (TransactionFailureException e) { throw new Exception(e); } } }); } // Just checks snapshots race.AddContestant(() => { ThreadLocalRandom random = ThreadLocalRandom.current(); int snapshotsLeft = 1_000; while (snapshotsLeft > 0) { int threadIndex = random.Next(threads); KernelTransactionsSnapshot snapshot = snapshots.get(threadIndex); if (snapshot != null && snapshot.AllClosed()) { snapshotsLeft--; snapshots.set(threadIndex, null); } } // End condition of this test can be described as: // when 1000 snapshots have been seen as closed. // setting this boolean to true will have all other threads end as well so that race.go() will end end.set(true); }); // WHEN race.Go(); }