Example usage for com.google.common.base Stopwatch Stopwatch

List of usage examples for com.google.common.base Stopwatch Stopwatch

Introduction

In this page you can find the example usage for com.google.common.base Stopwatch Stopwatch.

Prototype

Stopwatch() 

Source Link

Usage

From source file:co.cask.tephra.TransactionManager.java

public Transaction checkpoint(Transaction originalTx) throws TransactionNotInProgressException {
    txMetricsCollector.rate("checkpoint");
    Stopwatch timer = new Stopwatch().start();

    Transaction checkpointedTx = null;//  ww w .j a v  a 2  s . co  m
    long txId = originalTx.getTransactionId();
    long newWritePointer = 0;
    // guard against changes to the transaction log while processing
    this.logReadLock.lock();
    try {
        synchronized (this) {
            ensureAvailable();
            // check that the parent tx is in progress
            InProgressTx parentTx = inProgress.get(txId);
            if (parentTx == null) {
                if (invalid.contains(txId)) {
                    throw new TransactionNotInProgressException(String
                            .format("Transaction %d is not in progress because it was invalidated", txId));
                } else {
                    throw new TransactionNotInProgressException(
                            String.format("Transaction %d is not in progress", txId));
                }
            }
            newWritePointer = getNextWritePointer();
            doCheckpoint(newWritePointer, txId);
            // create a new transaction with the same read snapshot, plus the additional checkpoint write pointer
            // the same read snapshot is maintained to
            checkpointedTx = new Transaction(originalTx, newWritePointer,
                    parentTx.getCheckpointWritePointers().toLongArray());
        }
        // appending to WAL out of global lock for concurrent performance
        // we should still be able to arrive at the same state even if log entries are out of order
        appendToLog(TransactionEdit.createCheckpoint(newWritePointer, txId));
    } finally {
        this.logReadLock.unlock();
    }
    txMetricsCollector.histogram("checkpoint.latency", (int) timer.elapsedMillis());

    return checkpointedTx;
}

From source file:org.apache.tephra.TransactionManager.java

public void abort(Transaction tx) {
    // guard against changes to the transaction log while processing
    txMetricsCollector.rate("abort");
    Stopwatch timer = new Stopwatch().start();
    this.logReadLock.lock();
    try {/*  w  w  w .java  2 s. c  o  m*/
        synchronized (this) {
            ensureAvailable();
            doAbort(tx.getTransactionId(), tx.getCheckpointWritePointers(), tx.getType());
        }
        appendToLog(TransactionEdit.createAborted(tx.getTransactionId(), tx.getType(),
                tx.getCheckpointWritePointers()));
        txMetricsCollector.histogram("abort.latency", (int) timer.elapsedMillis());
    } finally {
        this.logReadLock.unlock();
    }
}

From source file:org.apache.tephra.TransactionManager.java

public boolean invalidate(long tx) {
    // guard against changes to the transaction log while processing
    txMetricsCollector.rate("invalidate");
    Stopwatch timer = new Stopwatch().start();
    this.logReadLock.lock();
    try {/*from  w ww  .j a  v a  2s .  c  om*/
        boolean success;
        synchronized (this) {
            ensureAvailable();
            success = doInvalidate(tx);
        }
        appendToLog(TransactionEdit.createInvalid(tx));
        txMetricsCollector.histogram("invalidate.latency", (int) timer.elapsedMillis());
        return success;
    } finally {
        this.logReadLock.unlock();
    }
}

From source file:org.apache.tephra.TransactionManager.java

/**
 * Removes the given transaction ids from the invalid list.
 * @param invalidTxIds transaction ids/*from w w w .  ja v a 2s . c  o  m*/
 * @return true if invalid list got changed, false otherwise
 */
public boolean truncateInvalidTx(Set<Long> invalidTxIds) {
    // guard against changes to the transaction log while processing
    txMetricsCollector.rate("truncateInvalidTx");
    Stopwatch timer = new Stopwatch().start();
    this.logReadLock.lock();
    try {
        boolean success;
        synchronized (this) {
            ensureAvailable();
            success = doTruncateInvalidTx(invalidTxIds);
        }
        appendToLog(TransactionEdit.createTruncateInvalidTx(invalidTxIds));
        txMetricsCollector.histogram("truncateInvalidTx.latency", (int) timer.elapsedMillis());
        return success;
    } finally {
        this.logReadLock.unlock();
    }
}

From source file:org.apache.tephra.TransactionManager.java

/**
 * Removes all transaction ids started before the given time from invalid list.
 * @param time time in milliseconds//from ww w.  j  a  v  a  2  s . c o  m
 * @return true if invalid list got changed, false otherwise
 * @throws InvalidTruncateTimeException if there are any in-progress transactions started before given time
 */
public boolean truncateInvalidTxBefore(long time) throws InvalidTruncateTimeException {
    // guard against changes to the transaction log while processing
    txMetricsCollector.rate("truncateInvalidTxBefore");
    Stopwatch timer = new Stopwatch().start();
    this.logReadLock.lock();
    try {
        boolean success;
        synchronized (this) {
            ensureAvailable();
            success = doTruncateInvalidTxBefore(time);
        }
        appendToLog(TransactionEdit.createTruncateInvalidTxBefore(time));
        txMetricsCollector.histogram("truncateInvalidTxBefore.latency", (int) timer.elapsedMillis());
        return success;
    } finally {
        this.logReadLock.unlock();
    }
}

From source file:net.myrrix.client.ClientRecommender.java

@Override
public boolean await(long time, TimeUnit unit) throws TasteException, InterruptedException {
    Preconditions.checkArgument(time >= 0L, "time must be positive: {}", time);
    Preconditions.checkNotNull(unit);/*from w  w w. java 2  s  . c om*/
    long waitForMS = TimeUnit.MILLISECONDS.convert(time, unit);
    long waitIntervalMS = FastMath.min(1000L, waitForMS);
    Stopwatch stopwatch = new Stopwatch().start();
    while (!isReady()) {
        if (stopwatch.elapsed(TimeUnit.MILLISECONDS) > waitForMS) {
            return false;
        }
        Thread.sleep(waitIntervalMS);
    }
    return true;
}

From source file:org.apache.tephra.TransactionManager.java

public Transaction checkpoint(Transaction originalTx) throws TransactionNotInProgressException {
    txMetricsCollector.rate("checkpoint");
    Stopwatch timer = new Stopwatch().start();

    Transaction checkpointedTx;/*from  w  w  w.j a  v  a  2s.  com*/
    long txId = originalTx.getTransactionId();
    long newWritePointer;
    // guard against changes to the transaction log while processing
    this.logReadLock.lock();
    try {
        synchronized (this) {
            ensureAvailable();
            // check that the parent tx is in progress
            InProgressTx parentTx = inProgress.get(txId);
            if (parentTx == null) {
                if (invalidTxList.contains(txId)) {
                    throw new TransactionNotInProgressException(String
                            .format("Transaction %d is not in progress because it was invalidated", txId));
                } else {
                    throw new TransactionNotInProgressException(
                            String.format("Transaction %d is not in progress", txId));
                }
            }
            newWritePointer = getNextWritePointer();
            doCheckpoint(newWritePointer, txId);
            // create a new transaction with the same read snapshot, plus the additional checkpoint write pointer
            // the same read snapshot is maintained to
            checkpointedTx = new Transaction(originalTx, newWritePointer,
                    parentTx.getCheckpointWritePointers().toLongArray());
        }
        // appending to WAL out of global lock for concurrent performance
        // we should still be able to arrive at the same state even if log entries are out of order
        appendToLog(TransactionEdit.createCheckpoint(newWritePointer, txId));
    } finally {
        this.logReadLock.unlock();
    }
    txMetricsCollector.histogram("checkpoint.latency", (int) timer.elapsedMillis());

    return checkpointedTx;
}

From source file:org.rhq.enterprise.server.cloud.StorageNodeManagerBean.java

private Stopwatch stopwatchStart() {
    if (log.isDebugEnabled()) {
        return new Stopwatch().start();
    }
    return null;
}

From source file:org.apache.tephra.TransactionManager.java

private void appendToLog(TransactionEdit edit) {
    try {//from  w ww .jav a2 s . c  om
        Stopwatch timer = new Stopwatch().start();
        currentLog.append(edit);
        txMetricsCollector.rate("wal.append.count");
        txMetricsCollector.histogram("wal.append.latency", (int) timer.elapsedMillis());
    } catch (IOException ioe) {
        abortService("Error appending to transaction log", ioe);
    }
}

From source file:org.apache.tephra.TransactionManager.java

private void appendToLog(List<TransactionEdit> edits) {
    try {//from w  w  w  .  j  ava2s. c  om
        Stopwatch timer = new Stopwatch().start();
        currentLog.append(edits);
        txMetricsCollector.rate("wal.append.count", edits.size());
        txMetricsCollector.histogram("wal.append.latency", (int) timer.elapsedMillis());
    } catch (IOException ioe) {
        abortService("Error appending to transaction log", ioe);
    }
}