Java Code Examples for com.google.common.base.Suppliers#memoizeWithExpiration()

The following examples show how to use com.google.common.base.Suppliers#memoizeWithExpiration() . 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: UniformNodeSelectorFactory.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public NodeSelector createNodeSelector(Optional<CatalogName> catalogName)
{
    requireNonNull(catalogName, "catalogName is null");

    // this supplier is thread-safe. TODO: this logic should probably move to the scheduler since the choice of which node to run in should be
    // done as close to when the the split is about to be scheduled
    Supplier<NodeMap> nodeMap = Suppliers.memoizeWithExpiration(
            () -> createNodeMap(catalogName),
            5, TimeUnit.SECONDS);

    return new UniformNodeSelector(
            nodeManager,
            nodeTaskMap,
            includeCoordinator,
            nodeMap,
            minCandidates,
            maxSplitsPerNode,
            maxPendingSplitsPerTask,
            optimizedLocalScheduling);
}
 
Example 2
Source File: TopologyAwareNodeSelectorFactory.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public NodeSelector createNodeSelector(Optional<CatalogName> catalogName)
{
    requireNonNull(catalogName, "catalogName is null");

    // this supplier is thread-safe. TODO: this logic should probably move to the scheduler since the choice of which node to run in should be
    // done as close to when the the split is about to be scheduled
    Supplier<NodeMap> nodeMap = Suppliers.memoizeWithExpiration(
            () -> createNodeMap(catalogName),
            5, TimeUnit.SECONDS);

    return new TopologyAwareNodeSelector(
            nodeManager,
            nodeTaskMap,
            includeCoordinator,
            nodeMap,
            minCandidates,
            maxSplitsPerNode,
            maxPendingSplitsPerTask,
            placementCounters,
            networkTopology);
}
 
Example 3
Source File: StandardStashReader.java    From emodb with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
StandardStashReader(URI stashRoot, AmazonS3 s3, long refreshLatestMs) {
    super(stashRoot, s3);

    Supplier<String> s3LatestRootSupplier = new Supplier<String>() {
        @Override
        public String get() {
            String latest = readLatestStash();
            return String.format("%s/%s", _rootPath, latest);
        }
    };

    if (refreshLatestMs > 0) {
        // Cache the latest stash directory and periodically recheck
        _latestRootSupplier = Suppliers.memoizeWithExpiration(s3LatestRootSupplier, refreshLatestMs, TimeUnit.MILLISECONDS);
    } else {
        _latestRootSupplier = s3LatestRootSupplier;
    }
}
 
Example 4
Source File: DefaultDataCenters.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Override
public void refresh() {
    _cache = Suppliers.memoizeWithExpiration(new Supplier<CachedInfo>() {
        @Override
        public CachedInfo get() {
            Map<String, DataCenter> dataCenters = _dataCenterDao.loadAll();
            if (!_ignoredDataCenters.isEmpty()) {
                ImmutableMap.Builder<String, DataCenter> dataCentersBuilder = ImmutableMap.builder();
                for (Map.Entry<String, DataCenter> entry : dataCenters.entrySet()) {
                    if (!_ignoredDataCenters.contains(entry.getKey())) {
                        dataCentersBuilder.put(entry);
                    } else if (_loggedIgnored.add(entry.getKey())) {
                        // Only want to log that we're ignoring the data center once
                        _log.info("Ignoring data center: {}", entry.getKey());
                    }
                }
                dataCenters = dataCentersBuilder.build();
            }
            return new CachedInfo(dataCenters);
        }
    }, 10, TimeUnit.MINUTES);
}
 
Example 5
Source File: GuavaMemoizerUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenMemoizedSupplierWithExpiration_whenGet_thenSubsequentGetsBeforeExpiredAreFast() throws InterruptedException {
    // given
    Supplier<BigInteger> memoizedSupplier;
    memoizedSupplier = Suppliers.memoizeWithExpiration(CostlySupplier::generateBigNumber, 3, TimeUnit.SECONDS);

    // when
    BigInteger expectedValue = new BigInteger("12345");
    assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D);

    // then
    assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D);
    // add one more second until memoized Supplier is evicted from memory
    TimeUnit.SECONDS.sleep(3);
    assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D);
}
 
Example 6
Source File: ConfigService.java    From nifi-minifi with Apache License 2.0 6 votes vote down vote up
public ConfigService(List<ConfigurationProvider> configurationProviders, Authorizer authorizer, long maximumCacheSize, long cacheTtlMillis) {
    this.authorizer = authorizer;
    this.objectMapper = new ObjectMapper();
    if (configurationProviders == null || configurationProviders.size() == 0) {
        throw new IllegalArgumentException("Expected at least one configuration provider");
    }
    this.configurationProviderInfo = Suppliers.memoizeWithExpiration(() -> initContentTypeInfo(configurationProviders), cacheTtlMillis, TimeUnit.MILLISECONDS);
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    if (maximumCacheSize >= 0) {
        cacheBuilder = cacheBuilder.maximumSize(maximumCacheSize);
    }
    if (cacheTtlMillis >= 0) {
        cacheBuilder = cacheBuilder.refreshAfterWrite(cacheTtlMillis, TimeUnit.MILLISECONDS);
    }
    this.configurationCache = cacheBuilder
            .build(new CacheLoader<ConfigurationProviderKey, ConfigurationProviderValue>() {
                @Override
                public ConfigurationProviderValue load(ConfigurationProviderKey key) throws Exception {
                    return initConfigurationProviderValue(key);
                }
            });
}
 
Example 7
Source File: SuppliersExamples.java    From StubbornJava with MIT License 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // {{start:memoize}}
    log.info("Memoized");
    Supplier<String> memoized = Suppliers.memoize(SuppliersExamples::helloWorldSupplier);
    log.info(memoized.get());
    log.info(memoized.get());
    // {{end:memoize}}

    // {{start:memoizeWithExpiration}}
    log.info("Memoized with Expiration");
    Supplier<String> memoizedExpiring = Suppliers.memoizeWithExpiration(
        SuppliersExamples::helloWorldSupplier, 50, TimeUnit.MILLISECONDS);
    log.info(memoizedExpiring.get());
    log.info(memoizedExpiring.get());
    log.info("sleeping");
    TimeUnit.MILLISECONDS.sleep(100);
    log.info(memoizedExpiring.get());
    log.info(memoizedExpiring.get());
    log.info("sleeping");
    TimeUnit.MILLISECONDS.sleep(100);
    log.info(memoizedExpiring.get());
    log.info(memoizedExpiring.get());
    // {{end:memoizeWithExpiration}}
}
 
Example 8
Source File: InMemorySitemap.java    From StubbornJava with MIT License 5 votes vote down vote up
public static InMemorySitemap fromSupplierWithExpiration(
        Supplier<Map<String, List<String>>> supplier,
        long duration,
        TimeUnit unit) {
    Supplier<Map<String, String>> sup = mapSupplier(supplier);
    Supplier<Map<String, String>> memoized = Suppliers.memoizeWithExpiration(sup::get, duration, unit);
    return new InMemorySitemap(memoized);
}
 
Example 9
Source File: ShardRowContext.java    From crate with Apache License 2.0 5 votes vote down vote up
public ShardRowContext(IndexShard indexShard, ClusterService clusterService) {
    this(indexShard, null, clusterService, Suppliers.memoizeWithExpiration(() -> {
        try {
            StoreStats storeStats = indexShard.storeStats();
            return storeStats.getSizeInBytes();
        } catch (AlreadyClosedException e) {
            return 0L;
        }
    }, 10, TimeUnit.SECONDS));
}
 
Example 10
Source File: PrometheusClient.java    From presto with Apache License 2.0 5 votes vote down vote up
@Inject
public PrometheusClient(PrometheusConnectorConfig config, JsonCodec<Map<String, Object>> metricCodec, TypeManager typeManager)
        throws URISyntaxException
{
    requireNonNull(config, "config is null");
    requireNonNull(metricCodec, "metricCodec is null");
    requireNonNull(typeManager, "typeManager is null");

    tableSupplier = Suppliers.memoizeWithExpiration(metricsSupplier(metricCodec, getPrometheusMetricsURI(config)),
            (long) config.getCacheDuration().getValue(), config.getCacheDuration().getUnit());
    this.typeManager = typeManager;
    this.config = config;
}
 
Example 11
Source File: GSuiteGroupAuthorizationFilter.java    From g-suite-identity-sync with Apache License 2.0 5 votes vote down vote up
public GSuiteGroupAuthorizationFilter(final GSuiteDirectoryService gsuiteDirService, Configuration config,
        AppConfiguration appConfig, eu.hlavki.identity.services.google.config.Configuration googleConfig) {

    this.gsuiteDirService = gsuiteDirService;
    this.externalUsersCache = Suppliers.memoizeWithExpiration(
            () -> appConfig.getExternalAccountsGroup().map(g -> getGroupMembers(g)).orElse(emptySet()),
            5, TimeUnit.MINUTES);
    this.adminUsersCache = Suppliers.memoizeWithExpiration(() -> getGroupMembers(config.getAdminGroup()),
            5, TimeUnit.MINUTES);
}
 
Example 12
Source File: BlobShardSizeExpression.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Inject
public BlobShardSizeExpression(final BlobShard blobShard) {
    totalUsageSupplier = Suppliers.memoizeWithExpiration(new Supplier<Long>() {
        @Override
        public Long get() {
            return blobShard.blobStats().totalUsage();
        }
    }, 10, TimeUnit.SECONDS);
}
 
Example 13
Source File: BrowseStateContributor.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Inject
public BrowseStateContributor(
    final BrowseNodeConfiguration browseNodeConfiguration,
    final TaskScheduler taskScheduler,
    @Named("${nexus.coreui.state.rebuildingRepositoryTasksCacheTTL:-60}") long rebuildingRepositoriesCacheTTL)
{
  this.browseNodeConfiguration = checkNotNull(browseNodeConfiguration);
  this.taskScheduler = checkNotNull(taskScheduler);
  if (rebuildingRepositoriesCacheTTL < 0) {
    rebuildingRepositoriesCacheTTL = DEFAULT_REBUILDING_REPOSITORIES_CACHE_TTL;
  }
  rebuildingRepositoriesCache = Suppliers
      .memoizeWithExpiration(this::getRepositoryNamesForRunningTasks, Math.abs(rebuildingRepositoriesCacheTTL),
          TimeUnit.SECONDS);
}
 
Example 14
Source File: CachingCollector.java    From cassandra-exporter with Apache License 2.0 5 votes vote down vote up
private CachingCollector(final MBeanGroupMetricFamilyCollector delegate, final long duration, final TimeUnit unit) {
    this.delegate = delegate;
    this.duration = duration;
    this.unit = unit;

    this.cachedCollect = Suppliers.memoizeWithExpiration(() -> {
        return delegate.collect().map(MetricFamily::cachedCopy).collect(Collectors.toList());
    }, duration, unit);
}
 
Example 15
Source File: S3SecurityMappingConfigurationProvider.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Supplier<S3SecurityMappings> getMappings(S3SecurityMappingConfig config)
{
    File configFile = config.getConfigFile().orElseThrow(() -> new IllegalArgumentException("config file not set"));
    Supplier<S3SecurityMappings> supplier = () -> parseJson(configFile.toPath(), S3SecurityMappings.class);
    if (config.getRefreshPeriod().isEmpty()) {
        return Suppliers.memoize(supplier::get);
    }
    return Suppliers.memoizeWithExpiration(
            () -> {
                log.info("Refreshing S3 security mapping configuration from %s", configFile);
                return supplier.get();
            },
            config.getRefreshPeriod().get().toMillis(),
            MILLISECONDS);
}
 
Example 16
Source File: StatusResource.java    From keywhiz with Apache License 2.0 4 votes vote down vote up
@Inject public StatusResource(KeywhizConfig keywhizConfig, Environment environment) {
  Duration cacheExpiry = keywhizConfig.getStatusCacheExpiry();
  memoizedCheck = Suppliers.memoizeWithExpiration(() -> environment.healthChecks().runHealthChecks(),
      cacheExpiry.toMillis(), TimeUnit.MILLISECONDS);
}
 
Example 17
Source File: AbstractTimelineMetricsSink.java    From ambari-metrics with Apache License 2.0 4 votes vote down vote up
/**
 * Find appropriate write shard for this sink using the {@link org.apache.hadoop.metrics2.sink.timeline.availability.MetricSinkWriteShardStrategy}
 *
 * 1. Use configured collector(s) to discover available collectors
 * 2. If configured collector(s) are unresponsive check Zookeeper to find live hosts
 * 3. Refresh known collector list using ZK
 * 4. Default: Return configured collector with no side effect due to discovery.
 *
 * throws {#link MetricsSinkInitializationException} if called before
 * initialization, not other side effect
 *
 * @return String Collector hostname
 */
protected synchronized String findPreferredCollectHost() {
  if (!isInitializedForHA) {
    init();
  }

  shardExpired = false;
  // Auto expire and re-calculate after 1 hour
  if (targetCollectorHostSupplier != null) {
    String targetCollector = targetCollectorHostSupplier.get();
    if (targetCollector != null) {
      return targetCollector;
    }
  }

  // Reach out to all configured collectors before Zookeeper
  Collection<String> collectorHosts = getConfiguredCollectorHosts();
  refreshCollectorsFromConfigured(collectorHosts);

  // Lookup Zookeeper for live hosts - max 10 seconds wait time
  long currentTime = System.currentTimeMillis();
  if (allKnownLiveCollectors.size() == 0 && getZookeeperQuorum() != null
    && (currentTime - lastFailedZkRequestTime) > zookeeperBackoffTimeMillis) {

    LOG.debug("No live collectors from configuration. Requesting zookeeper...");
    allKnownLiveCollectors.addAll(collectorHAHelper.findLiveCollectorHostsFromZNode());
    boolean noNewCollectorFromZk = true;
    for (String collectorHostFromZk : allKnownLiveCollectors) {
      if (!collectorHosts.contains(collectorHostFromZk)) {
        noNewCollectorFromZk = false;
        break;
      }
    }
    if (noNewCollectorFromZk) {
      LOG.debug("No new collector was found from Zookeeper. Will not request zookeeper for " + zookeeperBackoffTimeMillis + " millis");
      lastFailedZkRequestTime = System.currentTimeMillis();
    }
  }

  if (allKnownLiveCollectors.size() != 0) {
    targetCollectorHostSupplier = Suppliers.memoizeWithExpiration(
      new Supplier<String>() {
        @Override
        public String get() {
          //shardExpired flag is used to determine if the Supplier.get() is invoked through the
          // findPreferredCollectHost method (No need to refresh collector hosts
          // OR
          // through Expiry (Refresh needed to pick up dead collectors that might have not become alive).
          if (shardExpired) {
            refreshCollectorsFromConfigured(getConfiguredCollectorHosts());
          }
          return metricSinkWriteShardStrategy.findCollectorShard(new ArrayList<>(allKnownLiveCollectors));
        }
      },  // random.nextInt(max - min + 1) + min # (60 to 75 minutes)
      rand.nextInt(COLLECTOR_HOST_CACHE_MAX_EXPIRATION_MINUTES
        - COLLECTOR_HOST_CACHE_MIN_EXPIRATION_MINUTES + 1)
        + COLLECTOR_HOST_CACHE_MIN_EXPIRATION_MINUTES,
      TimeUnit.MINUTES
    );

    String collectorHost = targetCollectorHostSupplier.get();
    shardExpired = true;
    return collectorHost;
  }
  LOG.debug("Couldn't find any live collectors. Returning null");
  shardExpired = true;
  return null;
}
 
Example 18
Source File: FirmwareChecks.java    From vespa with Apache License 2.0 4 votes vote down vote up
private void createCache() {
    checkAfter = Suppliers.memoizeWithExpiration(database::readFirmwareCheck, cacheExpiry.toMillis(), TimeUnit.MILLISECONDS);
}
 
Example 19
Source File: DockerImages.java    From vespa with Apache License 2.0 4 votes vote down vote up
private void createCache() {
    this.dockerImages = Suppliers.memoizeWithExpiration(() -> Collections.unmodifiableMap(db.readDockerImages()),
                                                        cacheTtl.toMillis(), TimeUnit.MILLISECONDS);
}
 
Example 20
Source File: PermitSource.java    From Singularity with Apache License 2.0 4 votes vote down vote up
public PermitSource(Supplier<Integer> concurrentRequestLimit) {
  this.concurrentRequestLimit =
    Suppliers.memoizeWithExpiration(concurrentRequestLimit::get, 1, TimeUnit.MINUTES);
  this.concurrentRequests = new AtomicInteger();
}