Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.beam.fn.harness;
import java.util.Arrays;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.Function;
import org.apache.beam.fn.harness.Cache.Shrinkable;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.SdkHarnessOptions;
import org.apache.beam.sdk.util.Weighted;
import org.apache.beam.sdk.util.WeightedValue;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.CacheBuilder;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.CacheStats;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.RemovalListener;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.RemovalNotification;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.Weigher;
import org.github.jamm.MemoryMeter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Utility methods used to instantiate and operate over cache instances. */
@SuppressWarnings("nullness")
public final class Caches {
private static final Logger LOG = LoggerFactory.getLogger(Caches.class);
/**
* Object sizes will always be rounded up to the next multiple of {@code 2^WEIGHT_RATIO} when
* stored in the cache. This allows us to work around the limit on the Guava cache method which
* only allows int weights by scaling object sizes appropriately.
*/
@VisibleForTesting static final int WEIGHT_RATIO = 6;
/** All objects less than or equal to this size will account for 1. */
private static final long MIN_OBJECT_SIZE = 1 << WEIGHT_RATIO;
/**
* Objects which change in this amount should always update the cache.
*
*
The limit of 2^16 is chosen to be small enough such that objects will be close enough if
* they change frequently. Future work could scale these ratios based upon the configured cache
* size.
*/
private static final long CACHE_SIZE_CHANGE_LIMIT_BYTES = 1 << 16;
private static final MemoryMeter MEMORY_METER = MemoryMeter.builder().build();
/** The size of a reference. */
public static final long REFERENCE_SIZE = 8;
/** Returns the amount of memory in bytes the provided object consumes. */
public static long weigh(Object o) {
if (o == null) {
return REFERENCE_SIZE;
}
try {
return MEMORY_METER.measureDeep(o);
} catch (RuntimeException e) {
// Checking for RuntimeException since java.lang.reflect.InaccessibleObjectException is only
// available starting Java 9
LOG.warn("JVM prevents jamm from accessing subgraph - cache sizes may be underestimated", e);
return MEMORY_METER.measure(o);
}
}
/**
* Returns whether the cache should be updated in the case where the objects size has changed.
*
*
Note that this should only be used in the case where the cache is being updated very often
* in a tight loop and is not a good fit for cases where the object being cached is the result of
* an expensive operation like a disk read or remote service call.
*/
public static boolean shouldUpdateOnSizeChange(long oldSize, long newSize) {
/*
Our strategy is three fold:
- tiny objects don't impact the cache accounting and count as a size of `1` in the cache.
- large changes (>= CACHE_SIZE_CHANGE_LIMIT_BYTES) should always update the size
- all others if the size changed by a factor of 2
*/
return (oldSize > MIN_OBJECT_SIZE || newSize > MIN_OBJECT_SIZE)
&& ((newSize - oldSize >= CACHE_SIZE_CHANGE_LIMIT_BYTES)
|| Long.highestOneBit(oldSize) != Long.highestOneBit(newSize));
}
/** An eviction listener that reduces the size of entries that are {@link Shrinkable}. */
@VisibleForTesting
static class ShrinkOnEviction implements RemovalListener> {
private final org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.Cache<
CompositeKey, WeightedValue