org.apache.bookkeeper.util.MathUtils Java Examples

The following examples show how to use org.apache.bookkeeper.util.MathUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BKLogSegmentWriter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private synchronized  void keepAlive() {
    if (null != closeFuture) {
        // if the log segment is closing, skip sending any keep alive records.
        LOG.debug("Skip sending keepAlive control record since log segment {} is closing.",
                getFullyQualifiedLogSegment());
        return;
    }

    if (MathUtils.elapsedMSec(lastTransmitNanos) < periodicKeepAliveMs) {
        return;
    }

    LogRecord controlRec = new LogRecord(lastTxId, DistributedLogConstants.KEEPALIVE_RECORD_CONTENT);
    controlRec.setControl();
    asyncWrite(controlRec);
}
 
Example #2
Source File: ReadAheadWorker.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
void complete(boolean success) {
    long startTime = MathUtils.nowInNano();
    doComplete(success);
    if (success) {
        notificationExecutionStat.registerSuccessfulEvent(MathUtils.elapsedMicroSec(startTime));
    } else {
        notificationExecutionStat.registerFailedEvent(MathUtils.elapsedMicroSec(startTime));
    }
}
 
Example #3
Source File: MonitoredScheduledThreadPoolExecutor.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    long startNanos = MathUtils.nowInNano();
    long pendingMicros = TimeUnit.NANOSECONDS.toMicros(startNanos - enqueueNanos);
    taskPendingStats.registerSuccessfulEvent(pendingMicros);
    try {
        runnable.run();
    } finally {
        long executionMicros = TimeUnit.NANOSECONDS.toMicros(MathUtils.nowInNano() - startNanos);
        taskExecutionStats.registerSuccessfulEvent(executionMicros);
    }
}
 
Example #4
Source File: MonitoredScheduledThreadPoolExecutor.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
public T call() throws Exception {
    long startNanos = MathUtils.nowInNano();
    long pendingMicros = TimeUnit.NANOSECONDS.toMicros(startNanos - enqueueNanos);
    taskPendingStats.registerSuccessfulEvent(pendingMicros);
    try {
        return task.call();
    } finally {
        long executionMicros = TimeUnit.NANOSECONDS.toMicros(MathUtils.nowInNano() - startNanos);
        taskExecutionStats.registerSuccessfulEvent(executionMicros);
    }
}
 
Example #5
Source File: ClientCnxnAspect.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private void processEvent(ProceedingJoinPoint joinPoint) {
    long startTimeNano = getStartTime(joinPoint.getArgs()[0]);
    if (startTimeNano == -1) {
        // couldn't find start time
        return;
    }
    Record request = getEventType(joinPoint.getArgs()[0]);

    if (request != null) {
        long timeElapsed = (MathUtils.nowInNano() - startTimeNano);
        notifyListeners(checkType(request), TimeUnit.NANOSECONDS.toMicros(timeElapsed));
    }
}
 
Example #6
Source File: BKLogSegmentWriter.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
/**
 * Transmit the current buffer to bookkeeper.
 * Synchronised at the class. #write() and #flush()
 * are never called at the same time.
 * NOTE: This method should only throw known exceptions so that we don't accidentally
 *       add new code that throws in an inappropriate place.
 *
 * @return a transmit future for caller to wait for transmit result if we transmit successfully,
 *         null if no data to transmit
 * @throws BKTransmitException if the segment writer is already in error state
 * @throws LockingException if the segment writer lost lock before transmit
 * @throws WriteException if failed to create the envelope for the data to transmit
 * @throws InvalidEnvelopedEntryException when built an invalid enveloped entry
 */
private CompletableFuture<Integer> transmit()
    throws BKTransmitException, LockingException, WriteException, InvalidEnvelopedEntryException {
    EntryBuffer recordSetToTransmit;
    transmitLock.lock();
    try {
        synchronized (this) {
            checkWriteLock();
            // If transmitResult is anything other than BKException.Code.OK, it means that the
            // stream has encountered an error and cannot be written to.
            if (!transmitResult.compareAndSet(BKException.Code.OK,
                                              BKException.Code.OK)) {
                LOG.error("Log Segment {} Trying to write to an errored stream; Error is {}",
                          fullyQualifiedLogSegment,
                          BKException.getMessage(transmitResult.get()));
                throw new BKTransmitException("Trying to write to an errored stream;"
                                                      + " Error code : (" + transmitResult.get() + ") "
                        + BKException.getMessage(transmitResult.get()), transmitResult.get());
            }

            if (recordSetWriter.getNumRecords() == 0) {
                // Control flushes always have at least the control record to flush
                transmitDataMisses.inc();
                return null;
            }

            recordSetToTransmit = recordSetWriter;
            recordSetWriter = newRecordSetWriter();
            outstandingBytes = 0;

            if (recordSetToTransmit.hasUserRecords()) {
                numBytes += recordSetToTransmit.getNumBytes();
                numFlushesSinceRestart++;
            }
        }

        ByteBuf toSend;
        try {
            toSend = recordSetToTransmit.getBuffer();
            FailpointUtils.checkFailPoint(FailpointUtils.FailPointName.FP_TransmitFailGetBuffer);
        } catch (IOException e) {
            if (e instanceof InvalidEnvelopedEntryException) {
                alertStatsLogger.raise("Invalid enveloped entry for segment {} : ", fullyQualifiedLogSegment, e);
            }
            LOG.error("Exception while enveloping entries for segment: {}",
                      new Object[] {fullyQualifiedLogSegment}, e);
            // If a write fails here, we need to set the transmit result to an error so that
            // no future writes go through and violate ordering guarantees.
            transmitResult.set(BKException.Code.WriteException);
            if (e instanceof InvalidEnvelopedEntryException) {
                alertStatsLogger.raise("Invalid enveloped entry for segment {} : ", fullyQualifiedLogSegment, e);
                throw (InvalidEnvelopedEntryException) e;
            } else {
                throw new WriteException(streamName, "Envelope Error");
            }
        }

        synchronized (this) {
            // update the transmit timestamp
            lastTransmitNanos = MathUtils.nowInNano();

            BKTransmitPacket packet = new BKTransmitPacket(recordSetToTransmit);
            packetPrevious = packet;
            entryWriter.asyncAddEntry(toSend, this, packet);

            if (recordSetToTransmit.hasUserRecords()) {
                transmitDataSuccesses.inc();
            } else {
                transmitControlSuccesses.inc();
            }

            lastTransmit.reset().start();
            outstandingTransmits.incrementAndGet();
            controlFlushNeeded = false;
            return packet.getTransmitFuture();
        }
    } finally {
        transmitLock.unlock();
    }
}
 
Example #7
Source File: ReadAheadWorker.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
InterruptibleScheduledRunnable(Runnable task) {
    this.task = task;
    this.startNanos = MathUtils.nowInNano();
}
 
Example #8
Source File: ReadAheadWorker.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public void notifyOnError() {
    longPollInterruptionStat.registerFailedEvent(MathUtils.elapsedMicroSec(startNanos));
    execute();
}
 
Example #9
Source File: ReadAheadWorker.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public void notifyOnOperationComplete() {
    longPollInterruptionStat.registerSuccessfulEvent(MathUtils.elapsedMicroSec(startNanos));
    execute();
}
 
Example #10
Source File: ReadAheadWorker.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
LongPollNotification(long lac, T cb, Object ctx) {
    this.lac = lac;
    this.cb = cb;
    this.ctx = ctx;
    this.startNanos = MathUtils.nowInNano();
}
 
Example #11
Source File: ReadAheadWorker.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public void notifyOnError() {
    longPollInterruptionStat.registerFailedEvent(MathUtils.elapsedMicroSec(startNanos));
    complete(false);
}
 
Example #12
Source File: ReadAheadWorker.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public void notifyOnOperationComplete() {
    longPollInterruptionStat.registerSuccessfulEvent(MathUtils.elapsedMicroSec(startNanos));
    complete(true);
}
 
Example #13
Source File: OrderedScheduler.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
protected MonitoredScheduledThreadPoolExecutor chooseExecutor(Object key) {
    return corePoolSize == 1 ? executors[0] :
            executors[MathUtils.signSafeMod(Objects.hashCode(key), corePoolSize)];
}
 
Example #14
Source File: OrderedScheduler.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
protected FuturePool chooseFuturePool(Object key) {
    return corePoolSize == 1 ? futurePools[0] :
            futurePools[MathUtils.signSafeMod(Objects.hashCode(key), corePoolSize)];
}
 
Example #15
Source File: MonitoredScheduledThreadPoolExecutor.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
TimedRunnable(Runnable runnable) {
    this.runnable = runnable;
    this.enqueueNanos = MathUtils.nowInNano();
}
 
Example #16
Source File: MonitoredScheduledThreadPoolExecutor.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
TimedCallable(Callable<T> task) {
    this.task = task;
    this.enqueueNanos = MathUtils.nowInNano();
}