public ListenableFuture<Object> invoke(Optional<String> addressSelectionContext, Map<String, String> headers, List<Object> parameters) { if (!headerParameters.isEmpty()) { headers = new LinkedHashMap<>(headers); for (Entry<Integer, ThriftHeaderParameter> entry : headerParameters.entrySet()) { headers.put(entry.getValue().getName(), (String) parameters.get(entry.getKey())); } ImmutableList.Builder<Object> newParameters = ImmutableList.builder(); for (int index = 0; index < parameters.size(); index++) { if (!headerParameters.containsKey(index)) { newParameters.add(parameters.get(index)); } } parameters = newParameters.build(); } return createDriftMethodInvocation(invoker, metadata, headers, parameters, retryPolicy, addressSelector, addressSelectionContext, stat, Ticker.systemTicker()); }
static <A extends Address> DriftMethodInvocation<A> createDriftMethodInvocation( MethodInvoker invoker, MethodMetadata metadata, Map<String, String> headers, List<Object> parameters, RetryPolicy retryPolicy, AddressSelector<A> addressSelector, Optional<String> addressSelectionContext, MethodInvocationStat stat, Ticker ticker) { DriftMethodInvocation<A> invocation = new DriftMethodInvocation<>( invoker, metadata, headers, parameters, retryPolicy, addressSelector, addressSelectionContext, stat, ticker); // invocation can not be started from constructor, because it may start threads that can call back into the unpublished object invocation.nextAttempt(); return invocation; }
private static DriftMethodInvocation<?> createDriftMethodInvocation( RetryPolicy retryPolicy, TestingMethodInvocationStat stat, MockMethodInvoker invoker, AddressSelector<?> addressSelector, Ticker ticker) { return DriftMethodInvocation.createDriftMethodInvocation( invoker, METHOD_METADATA, ImmutableMap.of(), ImmutableList.of(), retryPolicy, addressSelector, Optional.empty(), stat, ticker); }
private ManualSerializationProxy( Strength keyStrength, Strength valueStrength, Equivalence<Object> keyEquivalence, Equivalence<Object> valueEquivalence, long expireAfterWriteNanos, long expireAfterAccessNanos, long maxWeight, Weigher<K, V> weigher, int concurrencyLevel, RemovalListener<? super K, ? super V> removalListener, Ticker ticker, CacheLoader<? super K, V> loader) { this.keyStrength = keyStrength; this.valueStrength = valueStrength; this.keyEquivalence = keyEquivalence; this.valueEquivalence = valueEquivalence; this.expireAfterWriteNanos = expireAfterWriteNanos; this.expireAfterAccessNanos = expireAfterAccessNanos; this.maxWeight = maxWeight; this.weigher = weigher; this.concurrencyLevel = concurrencyLevel; this.removalListener = removalListener; this.ticker = (ticker == Ticker.systemTicker() || ticker == NULL_TICKER) ? null : ticker; this.loader = loader; }
LocalCache(CacheBuilder<? super K, ? super V> builder, CacheLoader<? super K, V> loader) { this.loader = loader; this.removalListener = builder.removalListener; this.expireAfterAccess = builder.expireAfterAccessNanos; this.expireAfterWrite = builder.expireAfterWriteNanos; this.statsCounter = builder.getStatsCounterSupplier().get(); /* Implements size-capped LinkedHashMap */ final long maximumSize = builder.maximumSize; this.cachingHashMap = new CapacityEnforcingLinkedHashMap<K, V>( builder.getInitialCapacity(), 0.75f, (builder.maximumSize != UNSET_INT), builder.maximumSize, statsCounter, removalListener); this.ticker = firstNonNull(builder.ticker, Ticker.systemTicker()); }
@Test public void testHandleForwardedRemoteReadRequest() throws Exception { final TestProbe probe = createProbe(); final ReadTransactionRequest request = new ReadTransactionRequest(TRANSACTION_ID, 0L, probe.ref(), PATH_1, true); final Consumer<Response<?, ?>> callback = createCallbackMock(); setupExecuteInActor(); transaction.handleReplayedRemoteRequest(request, callback, Ticker.systemTicker().read()); final ArgumentCaptor<Response> captor = ArgumentCaptor.forClass(Response.class); verify(callback).accept(captor.capture()); final Response<?, ?> value = captor.getValue(); Assert.assertTrue(value instanceof ReadTransactionSuccess); final ReadTransactionSuccess success = (ReadTransactionSuccess) value; Assert.assertTrue(success.getData().isPresent()); Assert.assertEquals(DATA_1, success.getData().get()); }
@Test public void testHandleForwardedRemoteExistsRequest() throws Exception { final TestProbe probe = createProbe(); final ExistsTransactionRequest request = new ExistsTransactionRequest(TRANSACTION_ID, 0L, probe.ref(), PATH_1, true); final Consumer<Response<?, ?>> callback = createCallbackMock(); setupExecuteInActor(); transaction.handleReplayedRemoteRequest(request, callback, Ticker.systemTicker().read()); final ArgumentCaptor<Response> captor = ArgumentCaptor.forClass(Response.class); verify(callback).accept(captor.capture()); final Response<?, ?> value = captor.getValue(); Assert.assertTrue(value instanceof ExistsTransactionSuccess); final ExistsTransactionSuccess success = (ExistsTransactionSuccess) value; Assert.assertTrue(success.getExists()); }
private void applyModifyTransactionRequest(final boolean coordinated) { final TestProbe probe = createProbe(); final ModifyTransactionRequestBuilder builder = new ModifyTransactionRequestBuilder(TRANSACTION_ID, probe.ref()); final TransactionModification write = new TransactionWrite(PATH_1, DATA_1); final TransactionModification merge = new TransactionMerge(PATH_2, DATA_2); final TransactionModification delete = new TransactionDelete(PATH_3); builder.addModification(write); builder.addModification(merge); builder.addModification(delete); builder.setSequence(0L); builder.setCommit(coordinated); final ModifyTransactionRequest request = builder.build(); final Consumer<Response<?, ?>> callback = createCallbackMock(); transaction.replayModifyTransactionRequest(request, callback, Ticker.systemTicker().read()); verify(modification).write(PATH_1, DATA_1); verify(modification).merge(PATH_2, DATA_2); verify(modification).delete(PATH_3); final CommitLocalTransactionRequest commitRequest = getTester().expectTransactionRequest(CommitLocalTransactionRequest.class); Assert.assertEquals(modification, commitRequest.getModification()); Assert.assertEquals(coordinated, commitRequest.isCoordinated()); }
public Client(String serviceName, CheckAggregationOptions checkOptions, ReportAggregationOptions reportOptions, QuotaAggregationOptions quotaOptions, ServiceControl transport, ThreadFactory threads, SchedulerFactory schedulers, int statsLogFrequency, @Nullable Ticker ticker) { ticker = ticker == null ? Ticker.systemTicker() : ticker; this.checkAggregator = new CheckRequestAggregator(serviceName, checkOptions, null, ticker); this.reportAggregator = new ReportRequestAggregator(serviceName, reportOptions, null, ticker); this.quotaAggregator = new QuotaRequestAggregator(serviceName, quotaOptions, ticker); this.serviceName = serviceName; this.ticker = ticker; this.transport = transport; this.threads = threads; this.schedulers = schedulers; this.scheduler = null; // the scheduler is assigned when start is invoked this.schedulerThread = null; this.statsLogFrequency = statsLogFrequency; this.statistics = new Statistics(); }
/** * Creates a {@link Cache} configured by this instance. * * @param <T> * the type of the value stored in the Cache * @param out * a concurrent {@code Deque} to which the cached values are * added as they are removed from the cache * @param ticker * the time source used to determine expiration * @return a {@link Cache} corresponding to this instance's values or * {@code null} unless {@code #numEntries} is positive. */ @Nullable public <T> Cache<String, T> createCache(final ConcurrentLinkedDeque<T> out, Ticker ticker) { Preconditions.checkNotNull(out, "The out deque cannot be null"); Preconditions.checkNotNull(ticker, "The ticker cannot be null"); if (numEntries <= 0) { return null; } final RemovalListener<String, T> listener = new RemovalListener<String, T>() { @Override public void onRemoval(RemovalNotification<String, T> notification) { out.addFirst(notification.getValue()); } }; CacheBuilder<String, T> b = CacheBuilder.newBuilder().maximumSize(numEntries).ticker(ticker) .removalListener(listener); if (expirationMillis >= 0) { b.expireAfterWrite(expirationMillis, TimeUnit.MILLISECONDS); } return b.build(); }
/** * Creates a {@link Cache} configured by this instance. * * @param <T> * the type of the value stored in the Cache * @param out * a concurrent {@code Deque} to which cached values are added as * they are removed from the cache * @param ticker * the time source used to determine expiration * @return a {@link Cache} corresponding to this instance's values or * {@code null} unless {@code #numEntries} is positive. */ @Nullable public <T> Cache<String, T> createCache(final ConcurrentLinkedDeque<T> out, Ticker ticker) { Preconditions.checkNotNull(out, "The out deque cannot be null"); Preconditions.checkNotNull(ticker, "The ticker cannot be null"); if (numEntries <= 0) { return null; } final RemovalListener<String, T> listener = new RemovalListener<String, T>() { @Override public void onRemoval(RemovalNotification<String, T> notification) { out.addFirst(notification.getValue()); } }; CacheBuilder<String, T> b = CacheBuilder.newBuilder().maximumSize(numEntries).ticker(ticker) .removalListener(listener); if (flushCacheEntryIntervalMillis >= 0) { b.expireAfterWrite(flushCacheEntryIntervalMillis, TimeUnit.MILLISECONDS); } return b.build(); }
@Before public void setUp() throws IOException { testTicker = new FakeTicker(); checkStub = mock(Check.class); reportStub = mock(Report.class); services = mock(Services.class); transport = mock(ServiceControl.class); threads = mock(ThreadFactory.class); aThread = mock(Thread.class); schedulers = mock(Client.SchedulerFactory.class); checkOptions = new CheckAggregationOptions(); reportOptions = new ReportAggregationOptions(); quotaOptions = new QuotaAggregationOptions(); client = new Client(TEST_SERVICE_NAME, checkOptions, reportOptions, quotaOptions, transport, threads, schedulers, 1 /* ensure stats dumping code is touched */, testTicker); when(threads.newThread(any(Runnable.class))).thenReturn(aThread); when(schedulers.create(any(Ticker.class))).thenReturn(new Client.Scheduler(testTicker)); when(transport.services()).thenReturn(services); when(services.check(eq(TEST_SERVICE_NAME), any(CheckRequest.class))).thenReturn(checkStub); when(services.report(eq(TEST_SERVICE_NAME), any(ReportRequest.class))).thenReturn(reportStub); when(checkStub.execute()).thenReturn(CheckResponse.newBuilder().build()); when(reportStub.execute()).thenReturn(ReportResponse.newBuilder().build()); }
@Test public void testLoadingCacheExpireAfterWrite() throws Exception { TradeAccountRemovalListener removalListener = new TradeAccountRemovalListener(); LoadingCache<String, TradeAccount> tradeAccountCache = CacheBuilder.newBuilder() .expireAfterWrite(5L, TimeUnit.MILLISECONDS) .maximumSize(5000L) .removalListener(removalListener) .ticker(Ticker.systemTicker()) .build(new CacheLoader<String, TradeAccount>() { @Override public TradeAccount load(String key) throws Exception { return tradeAccountService.getTradeAccountById(key); } }); //223,"Rogers, Jim",250000.12 TradeAccount tradeAccount = tradeAccountCache.get("223"); assertThat(tradeAccount.getBalance(), is(250000.12)); Thread.sleep(10L); tradeAccountCache.get("223"); assertThat(removalListener.getRemovalCause(), is(RemovalCause.EXPIRED)); assertThat(removalListener.getRemovedValue(), is(tradeAccount)); }
@Test public void testRefreshingCacheValues() throws Exception { TradeAccountRemovalListener removalListener = new TradeAccountRemovalListener(); LoadingCache<String, TradeAccount> tradeAccountCache = CacheBuilder.newBuilder() .concurrencyLevel(10) .refreshAfterWrite(5L, TimeUnit.MILLISECONDS) .removalListener(removalListener) .ticker(Ticker.systemTicker()) .recordStats() .build(new CacheLoader<String, TradeAccount>() { @Override public TradeAccount load(String key) throws Exception { return tradeAccountService.getTradeAccountById(key); } }); //223,"Rogers, Jim",250000.12 TradeAccount tradeAccount = tradeAccountCache.get("223"); assertThat(tradeAccount.getBalance(), is(250000.12)); Thread.sleep(10L); tradeAccountCache.get("223"); CacheStats stats = tradeAccountCache.stats(); assertThat(stats.loadSuccessCount(),is(2l)); assertThat(removalListener.getRemovalCause(), is(RemovalCause.REPLACED)); assertThat(removalListener.getRemovedValue(), is(tradeAccount)); }
@VisibleForTesting public TaskExecutor(int runnerThreads, int minDrivers, Ticker ticker) { checkArgument(runnerThreads > 0, "runnerThreads must be at least 1"); // we manages thread pool size directly, so create an unlimited pool this.executor = newCachedThreadPool(threadsNamed("task-processor-%s")); this.executorMBean = new ThreadPoolExecutorMBean((ThreadPoolExecutor) executor); this.runnerThreads = runnerThreads; this.ticker = requireNonNull(ticker, "ticker is null"); this.minimumNumberOfDrivers = minDrivers; this.pendingSplits = new PriorityBlockingQueue<>(Runtime.getRuntime().availableProcessors() * 10); this.tasks = new LinkedList<>(); }
public Backoff(Duration maxFailureInterval, Ticker ticker, Duration... backoffDelayIntervals) { requireNonNull(maxFailureInterval, "maxFailureInterval is null"); requireNonNull(ticker, "ticker is null"); requireNonNull(backoffDelayIntervals, "backoffDelayIntervals is null"); checkArgument(backoffDelayIntervals.length > 0, "backoffDelayIntervals must contain at least one entry"); this.maxFailureIntervalNanos = maxFailureInterval.roundTo(NANOSECONDS); this.ticker = ticker; this.backoffDelayIntervalsNanos = new long[backoffDelayIntervals.length]; for (int i = 0; i < backoffDelayIntervals.length; i++) { this.backoffDelayIntervalsNanos[i] = backoffDelayIntervals[i].roundTo(NANOSECONDS); } this.lastSuccessTime = this.ticker.read(); }
public TaskExecutorSimulator() { executor = listeningDecorator(newCachedThreadPool(threadsNamed(getClass().getSimpleName() + "-%s"))); taskExecutor = new TaskExecutor(24, 48, new Ticker() { private final long start = System.nanoTime(); @Override public long read() { // run 10 times faster than reality long now = System.nanoTime(); return (now - start) * 100; } }); taskExecutor.start(); }
@Generates private Ticker generateTicker() { return new Ticker() { @Override public long read() { return 0; } final String string = paramString(Ticker.class, generateInt()); @Override public String toString() { return string; } }; }
ThreadContextImpl(Transaction transaction, @Nullable TraceEntryImpl parentTraceEntry, @Nullable TraceEntryImpl parentThreadContextPriorEntry, MessageSupplier messageSupplier, TimerName rootTimerName, long startTick, boolean captureThreadStats, int maxAggregateQueriesPerType, int maxAggregateServiceCallsPerType, @Nullable ThreadAllocatedBytes threadAllocatedBytes, boolean limitExceededAuxThreadContext, Ticker ticker, ThreadContextThreadLocal.Holder threadContextHolder, @Nullable ServletRequestInfo servletRequestInfo) { this.transaction = transaction; this.parentTraceEntry = parentTraceEntry; rootTimer = TimerImpl.createRootTimer(castInitialized(this), (TimerNameImpl) rootTimerName); rootTimer.start(startTick); traceEntryComponent = new TraceEntryComponent(castInitialized(this), messageSupplier, rootTimer, startTick); this.parentThreadContextPriorEntry = parentThreadContextPriorEntry; threadId = Thread.currentThread().getId(); threadStatsComponent = captureThreadStats ? new ThreadStatsComponent(threadAllocatedBytes) : null; this.maxAggregateQueriesPerType = maxAggregateQueriesPerType; this.maxAggregateServiceCallsPerType = maxAggregateServiceCallsPerType; this.limitExceededAuxThreadContext = limitExceededAuxThreadContext; this.ticker = ticker; this.threadContextHolder = threadContextHolder; this.servletRequestInfo = servletRequestInfo; this.outerTransactionThreadContext = (ThreadContextImpl) threadContextHolder.get(); }
public Weaver(Supplier<List<Advice>> advisors, List<ShimType> shimTypes, List<MixinType> mixinTypes, AnalyzedWorld analyzedWorld, TransactionRegistry transactionRegistry, Ticker ticker, TimerNameCache timerNameCache, final ConfigService configService) { this.advisors = advisors; this.shimTypes = ImmutableList.copyOf(shimTypes); this.mixinTypes = ImmutableList.copyOf(mixinTypes); this.analyzedWorld = analyzedWorld; this.transactionRegistry = transactionRegistry; this.ticker = ticker; configService.addConfigListener(new ConfigListener() { @Override public void onChange() { weavingTimerEnabled = configService.getAdvancedConfig().weavingTimer(); } }); this.timerName = timerNameCache.getTimerName(OnlyForTheTimerName.class); }
@Before public void beforeEachTest() throws Exception { ConfigService configService = mock(ConfigService.class); AdvancedConfig advancedConfig = ImmutableAdvancedConfig.builder().mbeanGaugeNotFoundDelaySeconds(60).build(); when(configService.getAdvancedConfig()).thenReturn(advancedConfig); Collector collector = mock(Collector.class); lazyPlatformMBeanServer = mock(LazyPlatformMBeanServer.class); clock = mock(Clock.class); ticker = mock(Ticker.class); logger = mock(Logger.class); setLogger(GaugeCollector.class, logger); gaugeCollector = new GaugeCollector(configService, collector, lazyPlatformMBeanServer, clock, ticker); }
@Before public void beforeEachTest() throws Exception { dataSource = new DataSource(); if (dataSource.tableExists("overall_point")) { dataSource.execute("drop table overall_point"); } if (dataSource.tableExists("transaction_point")) { dataSource.execute("drop table transaction_point"); } cappedFile = File.createTempFile("glowroot-test-", ".capped.db"); cappedDatabase = new CappedDatabase(cappedFile, 1000000, Ticker.systemTicker()); ConfigRepositoryImpl configRepository = mock(ConfigRepositoryImpl.class); when(configRepository.getAdvancedConfig(AGENT_ID)) .thenReturn(AdvancedConfig.getDefaultInstance()); ImmutableList<RollupConfig> rollupConfigs = ImmutableList.<RollupConfig>of( ImmutableRollupConfig.of(1000, 0), ImmutableRollupConfig.of(15000, 3600000), ImmutableRollupConfig.of(900000000, 8 * 3600000)); when(configRepository.getRollupConfigs()).thenReturn(rollupConfigs); aggregateDao = new AggregateDao( dataSource, ImmutableList.<CappedDatabase>of(cappedDatabase, cappedDatabase, cappedDatabase, cappedDatabase), configRepository, mock(TransactionTypeDao.class), mock(FullQueryTextDao.class)); }
private void shouldWrapAndResize(int newSizeKb) throws Exception { // when // because of compression, use somewhat random text and loop until wrap occurs String text = createRandomText(); cappedDatabase.write(ByteSource.wrap(text.getBytes(Charsets.UTF_8)), "test"); cappedDatabase.write(ByteSource.wrap(text.getBytes(Charsets.UTF_8)), "test"); cappedDatabase.write(ByteSource.wrap(text.getBytes(Charsets.UTF_8)), "test"); long cappedId = cappedDatabase.write(ByteSource.wrap(text.getBytes(Charsets.UTF_8)), "test"); cappedDatabase.resize(newSizeKb); // then String text2 = cappedDatabase.read(cappedId).read(); assertThat(text2).isEqualTo(text); // also test close and re-open cappedDatabase.close(); cappedDatabase = new CappedDatabase(tempFile, 2, Ticker.systemTicker()); text2 = cappedDatabase.read(cappedId).read(); assertThat(text2).isEqualTo(text); }
private void shouldResizeAndWrap(int newSizeKb) throws Exception { // when cappedDatabase.resize(newSizeKb); // because of compression, use somewhat random text and loop until wrap occurs String text = createRandomText(); cappedDatabase.write(ByteSource.wrap(text.getBytes(Charsets.UTF_8)), "test"); cappedDatabase.write(ByteSource.wrap(text.getBytes(Charsets.UTF_8)), "test"); cappedDatabase.write(ByteSource.wrap(text.getBytes(Charsets.UTF_8)), "test"); long cappedId = cappedDatabase.write(ByteSource.wrap(text.getBytes(Charsets.UTF_8)), "test"); // then String text2 = cappedDatabase.read(cappedId).read(); assertThat(text2).isEqualTo(text); // also test close and re-open cappedDatabase.close(); cappedDatabase = new CappedDatabase(tempFile, 2, Ticker.systemTicker()); text2 = cappedDatabase.read(cappedId).read(); assertThat(text2).isEqualTo(text); }
private ManualSerializationProxy(Strength keyStrength, Strength valueStrength, Equivalence<Object> keyEquivalence, Equivalence<Object> valueEquivalence, long expireAfterWriteNanos, long expireAfterAccessNanos, long maxWeight, Weigher<K, V> weigher, int concurrencyLevel, RemovalListener<? super K, ? super V> removalListener, Ticker ticker, CacheLoader<? super K, V> loader) { this.keyStrength = keyStrength; this.valueStrength = valueStrength; this.keyEquivalence = keyEquivalence; this.valueEquivalence = valueEquivalence; this.expireAfterWriteNanos = expireAfterWriteNanos; this.expireAfterAccessNanos = expireAfterAccessNanos; this.maxWeight = maxWeight; this.weigher = weigher; this.concurrencyLevel = concurrencyLevel; this.removalListener = removalListener; this.ticker = (ticker == Ticker.systemTicker() || ticker == NULL_TICKER) ? null : ticker; this.loader = loader; }
private DriftMethodInvocation( MethodInvoker invoker, MethodMetadata metadata, Map<String, String> headers, List<Object> parameters, RetryPolicy retryPolicy, AddressSelector<A> addressSelector, Optional<String> addressSelectionContext, MethodInvocationStat stat, Ticker ticker) { this.invoker = requireNonNull(invoker, "methodHandler is null"); this.metadata = requireNonNull(metadata, "metadata is null"); this.headers = requireNonNull(headers, "headers is null"); this.parameters = requireNonNull(parameters, "parameters is null"); this.retryPolicy = requireNonNull(retryPolicy, "retryPolicy is null"); this.addressSelector = requireNonNull(addressSelector, "addressSelector is null"); this.addressSelectionContext = requireNonNull(addressSelectionContext, "addressSelectionContext is null"); this.stat = requireNonNull(stat, "stat is null"); this.ticker = requireNonNull(ticker, "ticker is null"); this.startTime = ticker.read(); // if this invocation is canceled, cancel the tasks super.addListener(() -> { if (super.isCancelled()) { onCancel(wasInterrupted()); } }, directExecutor()); }