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

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

Introduction

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

Prototype

protected Ticker() 

Source Link

Document

Constructor for use by subclasses.

Usage

From source file:org.apache.aurora.scheduler.async.preemptor.BiCache.java

@Inject
public BiCache(StatsProvider statsProvider, BiCacheSettings settings, final Clock clock) {

    requireNonNull(clock);//ww  w .  jav  a  2 s. co  m
    this.cache = CacheBuilder.newBuilder()
            .expireAfterWrite(settings.expireAfter.as(Time.MINUTES), TimeUnit.MINUTES).ticker(new Ticker() {
                @Override
                public long read() {
                    return clock.nowNanos();
                }
            }).removalListener(new RemovalListener<K, V>() {
                @Override
                public void onRemoval(RemovalNotification<K, V> notification) {
                    inverse.remove(notification.getValue(), notification.getKey());
                }
            }).build();

    statsProvider.makeGauge(settings.cacheSizeStatName, new Supplier<Long>() {
        @Override
        public Long get() {
            return cache.size();
        }
    });
}

From source file:org.apache.aurora.scheduler.async.preemptor.PreemptionSlotCache.java

@Inject
PreemptionSlotCache(StatsProvider statsProvider, @PreemptionSlotHoldDuration Amount<Long, Time> duration,
        final Clock clock) {

    requireNonNull(duration);//from  w  w  w. ja  va2s. co m
    requireNonNull(clock);
    this.slots = CacheBuilder.newBuilder().expireAfterWrite(duration.as(Time.MINUTES), TimeUnit.MINUTES)
            .ticker(new Ticker() {
                @Override
                public long read() {
                    return clock.nowNanos();
                }
            }).build();

    statsProvider.makeGauge(PREEMPTION_SLOT_CACHE_SIZE_STAT, new Supplier<Long>() {
        @Override
        public Long get() {
            return slots.size();
        }
    });
}

From source file:zipkin.storage.cassandra.DeduplicatingExecutor.java

/**
 * @param session which conditionally executes bound statements
 * @param ttl how long the results of statements are remembered, in milliseconds.
 *///w  w w . j a  v a 2  s . co  m
DeduplicatingExecutor(Session session, long ttl) {
    this.session = session;
    this.cache = CacheBuilder.newBuilder().expireAfterWrite(ttl, TimeUnit.MILLISECONDS).ticker(new Ticker() {
        @Override
        public long read() {
            return nanoTime();
        }
    })
            // TODO: maximum size or weight
            .build(new CacheLoader<BoundStatementKey, ListenableFuture<Void>>() {
                @Override
                public ListenableFuture<Void> load(final BoundStatementKey key) {
                    ListenableFuture<?> cassandraFuture = executeAsync(key.statement);

                    // Drop the cassandra future so that we don't hold references to cassandra state for
                    // long periods of time.
                    final SettableFuture<Void> disconnectedFuture = SettableFuture.create();
                    Futures.addCallback(cassandraFuture, new FutureCallback<Object>() {

                        @Override
                        public void onSuccess(Object result) {
                            disconnectedFuture.set(null);
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            cache.invalidate(key);
                            disconnectedFuture.setException(t);
                        }
                    });
                    return disconnectedFuture;
                }
            });
}

From source file:org.apache.aurora.scheduler.preemptor.BiCache.java

@Inject
public BiCache(StatsProvider statsProvider, BiCacheSettings settings, final Clock clock) {

    requireNonNull(clock);/*from  ww  w  .  ja v a 2  s .  c  o  m*/
    this.cache = CacheBuilder.newBuilder()
            .expireAfterWrite(settings.expireAfter.as(Time.MINUTES), TimeUnit.MINUTES).ticker(new Ticker() {
                @Override
                public long read() {
                    return clock.nowNanos();
                }
            }).removalListener(new RemovalListener<K, V>() {
                @Override
                public void onRemoval(RemovalNotification<K, V> notification) {
                    removalCounter.getAndIncrement();
                    if (notification.wasEvicted()) {
                        expirationCounter.incrementAndGet();
                    }
                    inverse.remove(notification.getValue(), notification.getKey());
                }
            }).build();

    statsProvider.makeGauge(settings.cacheName + "_cache_size", cache::size);
    removalCounter = statsProvider.makeCounter(settings.cacheName + "_cache_removals");
    expirationCounter = statsProvider.makeCounter(settings.cacheName + "_cache_expiration_removals");
    explictRemovalCounter = statsProvider.makeCounter(settings.cacheName + "_cache_explicit_removals");
}

From source file:com.facebook.presto.execution.TaskExecutorSimulator.java

public TaskExecutorSimulator() {
    executor = listeningDecorator(newCachedThreadPool(threadsNamed(getClass().getSimpleName() + "-%s")));

    taskExecutor = new TaskExecutor(24, 48, new Ticker() {
        private final long start = System.nanoTime();

        @Override//  w ww .j a v a  2  s  .  c  o  m
        public long read() {
            // run 10 times faster than reality
            long now = System.nanoTime();
            return (now - start) * 100;
        }
    });
    taskExecutor.start();
}

From source file:brooklyn.management.ha.HighAvailabilityManagerTestFixture.java

@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {
    currentTime = new AtomicLong(1000000000L);
    ticker = new Ticker() {
        // strictly not a ticker because returns millis UTC, but it works fine even so
        @Override/*from   ww  w  .  j  a  v  a 2  s.co m*/
        public long read() {
            return currentTime.get();
        }
    };
    promotionListener = new RecordingPromotionListener();
    managementContext = newLocalManagementContext();
    ownNodeId = managementContext.getManagementNodeId();
    objectStore = newPersistenceObjectStore();
    objectStore.injectManagementContext(managementContext);
    objectStore.prepareForSharedUse(PersistMode.CLEAN, HighAvailabilityMode.DISABLED);
    persister = new ManagementPlaneSyncRecordPersisterToObjectStore(managementContext, objectStore,
            classLoader);
    ((ManagementPlaneSyncRecordPersisterToObjectStore) persister).preferRemoteTimestampInMemento();
    BrooklynMementoPersisterToObjectStore persisterObj = new BrooklynMementoPersisterToObjectStore(objectStore,
            managementContext.getBrooklynProperties(), classLoader);
    managementContext.getRebindManager().setPersister(persisterObj,
            PersistenceExceptionHandlerImpl.builder().build());
    manager = ((HighAvailabilityManagerImpl) managementContext.getHighAvailabilityManager())
            .setPollPeriod(getPollPeriod()).setHeartbeatTimeout(Duration.THIRTY_SECONDS)
            .setPromotionListener(promotionListener).setLocalTicker(ticker).setRemoteTicker(getRemoteTicker())
            .setPersister(persister);
    persister.delta(ManagementPlaneSyncRecordDeltaImpl.builder()
            .node(newManagerMemento(ownNodeId, ManagementNodeState.HOT_STANDBY)).build());

}

From source file:com.rackspacecloud.blueflood.service.ScheduleContext.java

public Ticker asMillisecondsSinceEpochTicker() {
    return new Ticker() {
        @Override//w  ww  . j  av  a 2  s. c  om
        public long read() {
            return ScheduleContext.this.getCurrentTimeMillis();
        }
    };
}