private boolean hasGoneInactive() { return activityDeadlineMs != Aeron.NULL_VALUE && epochClock.time() > activityDeadlineMs; }
private void updateClocks(final long nowNs) { if (clockUpdateDeadlineNs - nowNs < 0) { clockUpdateDeadlineNs = nowNs + CLOCK_UPDATE_DURATION_NS; cachedNanoClock.update(nowNs); cachedEpochClock.update(epochClock.time()); } }
private static MappedByteBuffer waitForFileMapping( final File cncFile, final long deadlineMs, final EpochClock epochClock) { try (FileChannel fileChannel = FileChannel.open(cncFile.toPath(), READ, WRITE)) { while (fileChannel.size() < CncFileDescriptor.CNC_VERSION_FIELD_OFFSET + SIZE_OF_INT) { if (epochClock.time() > deadlineMs) { throw new AeronException("CnC file is created but not populated"); } sleep(Configuration.IDLE_SLEEP_MS); } return fileChannel.map(READ_WRITE, 0, fileChannel.size()); } catch (final IOException ex) { throw new AeronException("cannot open CnC file", ex); } }
/** * Called from the {@link LossDetector} when gap is detected by the {@link DriverConductor} thread. * * @see LossHandler */ public void onGapDetected(final int termId, final int termOffset, final int length) { final long changeNumber = beginLossChange + 1; beginLossChange = changeNumber; lossTermId = termId; lossTermOffset = termOffset; lossLength = length; endLossChange = changeNumber; if (null != reportEntry) { reportEntry.recordObservation(length, cachedEpochClock.time()); } else if (null != lossReport) { reportEntry = lossReport.createEntry( length, cachedEpochClock.time(), sessionId, streamId, channel(), sourceAddress.toString()); if (null == reportEntry) { lossReport = null; } } }
@SuppressWarnings("unused") void onCommitPosition(final long leadershipTermId, final long logPosition, final int leaderMemberId) { if (State.FOLLOWER_BALLOT == state && leadershipTermId > this.leadershipTermId) { if (this.logPosition > logPosition) { consensusModuleAgent.truncateLogEntry(logLeadershipTermId, logPosition); } else { catchupLogPosition = logPosition; state(State.FOLLOWER_REPLAY, ctx.epochClock().time()); } } else if (State.FOLLOWER_CATCHUP == state && NULL_POSITION != catchupLogPosition) { catchupLogPosition = Math.max(catchupLogPosition, logPosition); } }
private int checkLiveness(final long nowNs) { if ((timeOfLastKeepAliveNs + keepAliveIntervalNs) - nowNs < 0) { if (epochClock.time() > (driverProxy.timeOfLastDriverKeepaliveMs() + driverTimeoutMs)) { isTerminating = true; forceCloseResources(); Thread.yield(); throw new DriverTimeoutException("MediaDriver keepalive older than (ms): " + driverTimeoutMs); } driverProxy.sendClientKeepalive(); timeOfLastKeepAliveNs = nowNs; return 1; } return 0; }
private int waitForConnection() { int workCount = 0; if (activityDeadlineMs == Aeron.NULL_VALUE) { activityDeadlineMs = epochClock.time() + connectTimeoutMs; } else if (controlPublication.isConnected()) { activityDeadlineMs = Aeron.NULL_VALUE; state = State.ACTIVE; sendConnectResponse(); workCount += 1; } else if (hasGoneInactive()) { state = State.INACTIVE; } return workCount; }
@Test public void shouldTimeoutIfConnectSentButPublicationNotConnected() { when(mockEpochClock.time()).thenReturn(0L); when(mockControlPublication.isClosed()).thenReturn(false); when(mockControlPublication.isConnected()).thenReturn(false); session.doWork(); when(mockEpochClock.time()).thenReturn(CONNECT_TIMEOUT_MS + 1L); session.doWork(); assertTrue(session.isDone()); }
private int sendQueuedResponses() { int workCount = 0; if (!controlPublication.isConnected()) { state = State.INACTIVE; } else { if (!queuedResponses.isEmpty()) { if (sendFirst(queuedResponses)) { queuedResponses.pollFirst(); activityDeadlineMs = Aeron.NULL_VALUE; workCount++; } else if (activityDeadlineMs == Aeron.NULL_VALUE) { activityDeadlineMs = epochClock.time() + connectTimeoutMs; } else if (hasGoneInactive()) { state = State.INACTIVE; } } } return workCount; }
boolean pollForEndOfSnapshotLoad(final Counter recoveryStateCounter) { consensusModuleAdapter.poll(); if (ServiceAck.hasReachedPosition(expectedAckPosition, serviceAckId, serviceAcks)) { ++serviceAckId; recoveryStateCounter.close(); if (ConsensusModule.State.SUSPENDED != state) { state(ConsensusModule.State.ACTIVE); } timeOfLastLogUpdateMs = cachedTimeMs = epochClock.time(); leadershipTermId = recoveryPlan.lastLeadershipTermId; return true; } return false; }
public ArchiveMarkFile(final Archive.Context ctx) { this(new File(ctx.archiveDir(), FILENAME), alignedTotalFileLength(ctx), ctx.epochClock(), 0); encode(ctx); updateActivityTimestamp(ctx.epochClock().time()); signalReady(); }
private void awaitActiveLog() { idleStrategy.reset(); while (null == activeLogEvent) { serviceAdapter.poll(); checkInterruptedStatus(); heartbeatCounter.setOrdered(epochClock.time()); idleStrategy.idle(); } }
/** * Free the counter identified by counterId. * * @param counterId the counter to freed */ public void free(final int counterId) { final int recordOffset = metaDataOffset(counterId); metaDataBuffer.putLong( recordOffset + FREE_FOR_REUSE_DEADLINE_OFFSET, epochClock.time() + freeToReuseTimeoutMs); metaDataBuffer.putIntOrdered(recordOffset, RECORD_RECLAIMED); freeList.addInt(counterId); }
@Test public void shouldReadFirstObservation() { final ErrorConsumer consumer = mock(ErrorConsumer.class); final long timestamp = 7; final RuntimeException error = new RuntimeException("Test Error"); when(clock.time()).thenReturn(timestamp); log.record(error); assertThat(ErrorLogReader.read(buffer, consumer), is(1)); verify(consumer).accept(eq(1), eq(timestamp), eq(timestamp), any(String.class)); }
@Test public void shouldTimeoutIfConnectSentButPublicationFailsToSend() { when(mockEpochClock.time()).thenReturn(0L); when(mockControlPublication.isClosed()).thenReturn(false); when(mockControlPublication.isConnected()).thenReturn(true); session.doWork(); session.sendOkResponse(1L, mockProxy); session.doWork(); when(mockEpochClock.time()).thenReturn(CONNECT_TIMEOUT_MS + 1L); session.doWork(); assertTrue(session.isDone()); } }
@Test public void shouldFailToRecordWhenInsufficientSpace() { final long timestamp = 7; final RuntimeException error = new RuntimeException("Test Error"); when(clock.time()).thenReturn(timestamp); when(buffer.capacity()).thenReturn(32); assertFalse(log.record(error)); } }
void closeRecordingSession(final RecordingSession session) { final long recordingId = session.sessionId(); catalog.recordingStopped(recordingId, session.recordingPosition().get(), epochClock.time()); recordingSessionByIdMap.remove(recordingId); closeSession(session); }
@Test public void shouldReadSummarisedObservation() { final ErrorConsumer consumer = mock(ErrorConsumer.class); final long timestampOne = 7; final long timestampTwo = 10; final RuntimeException error = new RuntimeException("Test Error"); final StringWriter stringWriter = new StringWriter(); error.printStackTrace(new PrintWriter(stringWriter)); final String errorAsString = stringWriter.toString(); when(clock.time()).thenReturn(timestampOne).thenReturn(timestampTwo); log.record(error); log.record(error); assertThat(ErrorLogReader.read(buffer, consumer), is(1)); verify(consumer).accept(eq(2), eq(timestampOne), eq(timestampTwo), eq(errorAsString)); }
void onAppendedPosition(final long leadershipTermId, final long logPosition, final int followerMemberId) { final ClusterMember follower = clusterMemberByIdMap.get(followerMemberId); if (null != follower) { follower .logPosition(logPosition) .leadershipTermId(leadershipTermId) .timeOfLastAppendPositionMs(ctx.epochClock().time()); consensusModuleAgent.checkCatchupStop(follower); } }
@Test public void shouldReadTwoDistinctObservations() { final ErrorConsumer consumer = mock(ErrorConsumer.class); final long timestampOne = 7; final long timestampTwo = 10; final RuntimeException errorOne = new RuntimeException("Test Error One"); final IllegalStateException errorTwo = new IllegalStateException("Test Error Two"); when(clock.time()).thenReturn(timestampOne).thenReturn(timestampTwo); log.record(errorOne); log.record(errorTwo); assertThat(ErrorLogReader.read(buffer, consumer), is(2)); final InOrder inOrder = inOrder(consumer); inOrder.verify(consumer).accept(eq(1), eq(timestampOne), eq(timestampOne), any(String.class)); inOrder.verify(consumer).accept(eq(1), eq(timestampTwo), eq(timestampTwo), any(String.class)); }