List of usage examples for com.google.common.base Stopwatch Stopwatch
Stopwatch()
From source file:co.cask.tephra.TransactionManager.java
/** * Start a short transaction with a given timeout. * @param timeoutInSeconds the time out period in seconds. *///from w ww . j a va 2s . c o m public Transaction startShort(int timeoutInSeconds) { Preconditions.checkArgument(timeoutInSeconds > 0, "timeout must be positive but is %s", timeoutInSeconds); txMetricsCollector.rate("start.short"); Stopwatch timer = new Stopwatch().start(); long expiration = getTxExpiration(timeoutInSeconds); Transaction tx = startTx(expiration, TransactionType.SHORT); txMetricsCollector.histogram("start.short.latency", (int) timer.elapsedMillis()); return tx; }
From source file:org.apache.tephra.TransactionManager.java
@Override public void doStop() { Stopwatch timer = new Stopwatch().start(); LOG.info("Shutting down gracefully..."); // signal the cleanup thread to stop if (cleanupThread != null) { cleanupThread.shutdown();/*from w ww. j av a2 s . c om*/ try { cleanupThread.join(30000L); } catch (InterruptedException ie) { LOG.warn("Interrupted waiting for cleanup thread to stop"); Thread.currentThread().interrupt(); } } if (metricsThread != null) { metricsThread.shutdown(); try { metricsThread.join(30000L); } catch (InterruptedException ie) { LOG.warn("Interrupted waiting for cleanup thread to stop"); Thread.currentThread().interrupt(); } } if (snapshotThread != null) { // this will trigger a final snapshot on stop snapshotThread.shutdown(); try { snapshotThread.join(30000L); } catch (InterruptedException ie) { LOG.warn("Interrupted waiting for snapshot thread to stop"); Thread.currentThread().interrupt(); } } persistor.stopAndWait(); txMetricsCollector.stop(); timer.stop(); LOG.info("Took " + timer + " to stop"); notifyStopped(); }
From source file:co.cask.tephra.TransactionManager.java
/** * Start a long transaction. Long transactions and do not participate in conflict detection. Also, aborting a long * transaction moves it to the invalid list because we assume that its writes cannot be rolled back. *//*from w ww . j ava 2 s . c o m*/ public Transaction startLong() { txMetricsCollector.rate("start.long"); Stopwatch timer = new Stopwatch().start(); long expiration = getTxExpiration(defaultLongTimeout); Transaction tx = startTx(expiration, TransactionType.LONG); txMetricsCollector.histogram("start.long.latency", (int) timer.elapsedMillis()); return tx; }
From source file:org.sleuthkit.autopsy.timeline.events.db.EventDB.java
/** * //TODO: update javadoc //TODO: split this into helper methods * * get a list of {@link AggregateEvent}s. * * General algorithm is as follows://from w w w. ja v a 2s.c o m * * - get all aggregate events, via one db query. * - sort them into a map from (type, description)-> aggevent * - for each key in map, merge the events and accumulate them in a list * to return * * * @param timeRange the Interval within in which all returned aggregate * events will be. * @param filter only events that pass the filter will be included in * aggregates events returned * @param zoomLevel only events of this level will be included * @param lod description level of detail to use when grouping events * * * @return a list of aggregate events within the given timerange, that pass * the supplied filter, aggregated according to the given event type and * description zoom levels */ private List<AggregateEvent> getAggregatedEvents(Interval timeRange, Filter filter, EventTypeZoomLevel zoomLevel, DescriptionLOD lod) { String descriptionColumn = getDescriptionColumn(lod); final boolean useSubTypes = (zoomLevel.equals(EventTypeZoomLevel.SUB_TYPE)); //get some info about the time range requested RangeDivisionInfo rangeInfo = RangeDivisionInfo.getRangeDivisionInfo(timeRange); //use 'rounded out' range long start = timeRange.getStartMillis() / 1000;//.getLowerBound(); long end = timeRange.getEndMillis() / 1000;//Millis();//rangeInfo.getUpperBound(); if (Objects.equals(start, end)) { end++; } //get a sqlite srtftime format string String strfTimeFormat = getStrfTimeFormat(rangeInfo.getPeriodSize()); //effectively map from type to (map from description to events) Map<EventType, SetMultimap<String, AggregateEvent>> typeMap = new HashMap<>(); //get all agregate events in this time unit dbReadLock(); String query = "select strftime('" + strfTimeFormat + "',time , 'unixepoch'" + (TimeLineController.getTimeZone().get().equals(TimeZone.getDefault()) ? ", 'localtime'" : "") + ") as interval, group_concat(event_id) as event_ids, Min(time), Max(time), " + descriptionColumn + ", " + (useSubTypes ? SUB_TYPE_COLUMN : BASE_TYPE_COLUMN) // NON-NLS + " from events where time >= " + start + " and time < " + end + " and " + getSQLWhere(filter) // NON-NLS + " group by interval, " + (useSubTypes ? SUB_TYPE_COLUMN : BASE_TYPE_COLUMN) + " , " + descriptionColumn // NON-NLS + " order by Min(time)"; // NON-NLS //System.out.println(query); ResultSet rs = null; try (Statement stmt = con.createStatement(); // scoop up requested events in groups organized by interval, type, and desription ) { Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); rs = stmt.executeQuery(query); stopwatch.stop(); //System.out.println(stopwatch.elapsedMillis() / 1000.0 + " seconds"); while (rs.next()) { EventType type = useSubTypes ? RootEventType.allTypes.get(rs.getInt(SUB_TYPE_COLUMN)) : BaseTypes.values()[rs.getInt(BASE_TYPE_COLUMN)]; AggregateEvent aggregateEvent = new AggregateEvent( new Interval(rs.getLong("Min(time)") * 1000, rs.getLong("Max(time)") * 1000, TimeLineController.getJodaTimeZone()), // NON-NLS type, Arrays.asList(rs.getString("event_ids").split(",")), // NON-NLS rs.getString(descriptionColumn), lod); //put events in map from type/descrition -> event SetMultimap<String, AggregateEvent> descrMap = typeMap.get(type); if (descrMap == null) { descrMap = HashMultimap.<String, AggregateEvent>create(); typeMap.put(type, descrMap); } descrMap.put(aggregateEvent.getDescription(), aggregateEvent); } } catch (SQLException ex) { Exceptions.printStackTrace(ex); } finally { try { rs.close(); } catch (SQLException ex) { Exceptions.printStackTrace(ex); } dbReadUnlock(); } //result list to return ArrayList<AggregateEvent> aggEvents = new ArrayList<>(); //save this for use when comparing gap size Period timeUnitLength = rangeInfo.getPeriodSize().getPeriod(); //For each (type, description) key, merge agg events for (SetMultimap<String, AggregateEvent> descrMap : typeMap.values()) { for (String descr : descrMap.keySet()) { //run through the sorted events, merging together adjacent events Iterator<AggregateEvent> iterator = descrMap.get(descr).stream() .sorted((AggregateEvent o1, AggregateEvent o2) -> Long .compare(o1.getSpan().getStartMillis(), o2.getSpan().getStartMillis())) .iterator(); AggregateEvent current = iterator.next(); while (iterator.hasNext()) { AggregateEvent next = iterator.next(); Interval gap = current.getSpan().gap(next.getSpan()); //if they overlap or gap is less one quarter timeUnitLength //TODO: 1/4 factor is arbitrary. review! -jm if (gap == null || gap.toDuration() .getMillis() <= timeUnitLength.toDurationFrom(gap.getStart()).getMillis() / 4) { //merge them current = AggregateEvent.merge(current, next); } else { //done merging into current, set next as new current aggEvents.add(current); current = next; } } aggEvents.add(current); } } //at this point we should have a list of aggregate events. //one per type/description spanning consecutive time units as determined in rangeInfo return aggEvents; }
From source file:co.cask.tephra.TransactionManager.java
public boolean canCommit(Transaction tx, Collection<byte[]> changeIds) throws TransactionNotInProgressException { txMetricsCollector.rate("canCommit"); Stopwatch timer = new Stopwatch().start(); if (inProgress.get(tx.getTransactionId()) == null) { // invalid transaction, either this has timed out and moved to invalid, or something else is wrong. if (invalid.contains(tx.getTransactionId())) { throw new TransactionNotInProgressException(String.format( "canCommit() is called for transaction %d that is not in progress (it is known to be invalid)", tx.getTransactionId())); } else {//from w w w. j a va2s .c o m throw new TransactionNotInProgressException(String.format( "canCommit() is called for transaction %d that is not in progress", tx.getTransactionId())); } } Set<ChangeId> set = Sets.newHashSetWithExpectedSize(changeIds.size()); for (byte[] change : changeIds) { set.add(new ChangeId(change)); } if (hasConflicts(tx, set)) { return false; } // guard against changes to the transaction log while processing this.logReadLock.lock(); try { synchronized (this) { ensureAvailable(); addCommittingChangeSet(tx.getTransactionId(), set); } appendToLog(TransactionEdit.createCommitting(tx.getTransactionId(), set)); } finally { this.logReadLock.unlock(); } txMetricsCollector.histogram("canCommit.latency", (int) timer.elapsedMillis()); return true; }
From source file:org.apache.tephra.TransactionManager.java
/** * Start a short transaction with a given timeout. * @param clientId id of the client requesting a transaction. * @param timeoutInSeconds the time out period in seconds. *///from w w w . j a va 2 s .com public Transaction startShort(String clientId, int timeoutInSeconds) { Preconditions.checkArgument(clientId != null, "clientId must not be null"); Preconditions.checkArgument(timeoutInSeconds > 0, "timeout must be positive but is %s seconds", timeoutInSeconds); Preconditions.checkArgument(timeoutInSeconds <= maxTimeout, "timeout must not exceed %s seconds but is %s seconds", maxTimeout, timeoutInSeconds); txMetricsCollector.rate("start.short"); Stopwatch timer = new Stopwatch().start(); long expiration = getTxExpiration(timeoutInSeconds); Transaction tx = startTx(expiration, TransactionType.SHORT, clientId); txMetricsCollector.histogram("start.short.latency", (int) timer.elapsedMillis()); return tx; }
From source file:co.cask.tephra.TransactionManager.java
public boolean commit(Transaction tx) throws TransactionNotInProgressException { txMetricsCollector.rate("commit"); Stopwatch timer = new Stopwatch().start(); Set<ChangeId> changeSet = null; boolean addToCommitted = true; long commitPointer; // guard against changes to the transaction log while processing this.logReadLock.lock(); try {/*w w w .j av a2 s . c om*/ synchronized (this) { ensureAvailable(); // we record commits at the first not-yet assigned transaction id to simplify clearing out change sets that // are no longer visible by any in-progress transactions commitPointer = lastWritePointer + 1; if (inProgress.get(tx.getTransactionId()) == null) { // invalid transaction, either this has timed out and moved to invalid, or something else is wrong. if (invalid.contains(tx.getTransactionId())) { throw new TransactionNotInProgressException( String.format("canCommit() is called for transaction %d that is not in progress " + "(it is known to be invalid)", tx.getTransactionId())); } else { throw new TransactionNotInProgressException( String.format("canCommit() is called for transaction %d that is not in progress", tx.getTransactionId())); } } // these should be atomic // NOTE: whether we succeed or not we don't need to keep changes in committing state: same tx cannot // be attempted to commit twice changeSet = committingChangeSets.remove(tx.getTransactionId()); if (changeSet != null) { // double-checking if there are conflicts: someone may have committed since canCommit check if (hasConflicts(tx, changeSet)) { return false; } } else { // no changes addToCommitted = false; } doCommit(tx.getTransactionId(), tx.getWritePointer(), changeSet, commitPointer, addToCommitted); } appendToLog(TransactionEdit.createCommitted(tx.getTransactionId(), changeSet, commitPointer, addToCommitted)); } finally { this.logReadLock.unlock(); } txMetricsCollector.histogram("commit.latency", (int) timer.elapsedMillis()); return true; }
From source file:org.apache.tephra.TransactionManager.java
/** * Starts a long transaction with a client id. *//*w w w. j av a 2s . co m*/ public Transaction startLong(String clientId) { Preconditions.checkArgument(clientId != null, "clientId must not be null"); txMetricsCollector.rate("start.long"); Stopwatch timer = new Stopwatch().start(); long expiration = getTxExpiration(defaultLongTimeout); Transaction tx = startTx(expiration, TransactionType.LONG, clientId); txMetricsCollector.histogram("start.long.latency", (int) timer.elapsedMillis()); return tx; }
From source file:org.apache.tephra.TransactionManager.java
public void canCommit(long txId, Collection<byte[]> changeIds) throws TransactionNotInProgressException, TransactionSizeException, TransactionConflictException { txMetricsCollector.rate("canCommit"); Stopwatch timer = new Stopwatch().start(); InProgressTx inProgressTx = inProgress.get(txId); if (inProgressTx == null) { synchronized (this) { // invalid transaction, either this has timed out and moved to invalid, or something else is wrong. if (invalidTxList.contains(txId)) { throw new TransactionNotInProgressException(String.format( "canCommit() is called for transaction %d that is not in progress (it is known to be invalid)", txId));//from ww w . j a va 2 s . co m } else { throw new TransactionNotInProgressException(String .format("canCommit() is called for transaction %d that is not in progress", txId)); } } } Set<ChangeId> set = validateChangeSet(txId, changeIds, inProgressTx.clientId != null ? inProgressTx.clientId : DEFAULT_CLIENTID); checkForConflicts(txId, set); // guard against changes to the transaction log while processing this.logReadLock.lock(); try { synchronized (this) { ensureAvailable(); addCommittingChangeSet(txId, inProgressTx.getClientId(), set); } appendToLog(TransactionEdit.createCommitting(txId, set)); } finally { this.logReadLock.unlock(); } txMetricsCollector.histogram("canCommit.latency", (int) timer.elapsedMillis()); }
From source file:org.apache.tephra.TransactionManager.java
public void commit(long txId, long writePointer) throws TransactionNotInProgressException, TransactionConflictException { txMetricsCollector.rate("commit"); Stopwatch timer = new Stopwatch().start(); ChangeSet changeSet;//w ww . ja va 2s . c o m boolean addToCommitted = true; long commitPointer; // guard against changes to the transaction log while processing this.logReadLock.lock(); try { synchronized (this) { ensureAvailable(); // we record commits at the first not-yet assigned transaction id to simplify clearing out change sets that // are no longer visible by any in-progress transactions commitPointer = lastWritePointer + 1; if (inProgress.get(txId) == null) { // invalid transaction, either this has timed out and moved to invalid, or something else is wrong. if (invalidTxList.contains(txId)) { throw new TransactionNotInProgressException( String.format("canCommit() is called for transaction %d that is not in progress " + "(it is known to be invalid)", txId)); } else { throw new TransactionNotInProgressException(String .format("canCommit() is called for transaction %d that is not in progress", txId)); } } // these should be atomic // NOTE: whether we succeed or not we don't need to keep changes in committing state: same tx cannot // be attempted to commit twice changeSet = committingChangeSets.remove(txId); if (changeSet != null) { // double-checking if there are conflicts: someone may have committed since canCommit check checkForConflicts(txId, changeSet.getChangeIds()); } else { // no changes addToCommitted = false; } doCommit(txId, writePointer, changeSet, commitPointer, addToCommitted); } appendToLog(TransactionEdit.createCommitted(txId, changeSet == null ? null : changeSet.getChangeIds(), commitPointer, addToCommitted)); } finally { this.logReadLock.unlock(); } txMetricsCollector.histogram("commit.latency", (int) timer.elapsedMillis()); }