Search in sources :

Example 1 with LongFunctionWithException

use of org.apache.flink.util.function.LongFunctionWithException in project flink by apache.

the class MemoryManager method getSharedMemoryResourceForManagedMemory.

// ------------------------------------------------------------------------
// Shared opaque memory resources
// ------------------------------------------------------------------------
/**
 * Acquires a shared memory resource, identified by a type string. If the resource already
 * exists, this returns a descriptor to the resource. If the resource does not yet exist, the
 * given memory fraction is reserved and the resource is initialized with that size.
 *
 * <p>The memory for the resource is reserved from the memory budget of this memory manager
 * (thus determining the size of the resource), but resource itself is opaque, meaning the
 * memory manager does not understand its structure.
 *
 * <p>The OpaqueMemoryResource object returned from this method must be closed once not used any
 * further. Once all acquisitions have closed the object, the resource itself is closed.
 *
 * <p><b>Important:</b> The failure semantics are as follows: If the memory manager fails to
 * reserve the memory, the external resource initializer will not be called. If an exception is
 * thrown when the opaque resource is closed (last lease is released), the memory manager will
 * still un-reserve the memory to make sure its own accounting is clean. The exception will need
 * to be handled by the caller of {@link OpaqueMemoryResource#close()}. For example, if this
 * indicates that native memory was not released and the process might thus have a memory leak,
 * the caller can decide to kill the process as a result.
 */
public <T extends AutoCloseable> OpaqueMemoryResource<T> getSharedMemoryResourceForManagedMemory(String type, LongFunctionWithException<T, Exception> initializer, double fractionToInitializeWith) throws Exception {
    // if we need to allocate the resource (no shared resource allocated, yet), this would be
    // the size to use
    final long numBytes = computeMemorySize(fractionToInitializeWith);
    // initializer and releaser as functions that are pushed into the SharedResources,
    // so that the SharedResources can decide in (thread-safely execute) when initialization
    // and release should happen
    final LongFunctionWithException<T, Exception> reserveAndInitialize = (size) -> {
        try {
            reserveMemory(type, size);
        } catch (MemoryReservationException e) {
            throw new MemoryAllocationException("Could not created the shared memory resource of size " + size + ". Not enough memory left to reserve from the slot's managed memory.", e);
        }
        try {
            return initializer.apply(size);
        } catch (Throwable t) {
            releaseMemory(type, size);
            throw t;
        }
    };
    final LongConsumer releaser = (size) -> releaseMemory(type, size);
    // This object identifies the lease in this request. It is used only to identify the release
    // operation.
    // Using the object to represent the lease is a bit nicer safer than just using a reference
    // counter.
    final Object leaseHolder = new Object();
    final SharedResources.ResourceAndSize<T> resource = sharedResources.getOrAllocateSharedResource(type, leaseHolder, reserveAndInitialize, numBytes);
    // the actual size may theoretically be different from what we requested, if allocated it
    // was by
    // someone else before with a different value for fraction (should not happen in practice,
    // though).
    final long size = resource.size();
    final ThrowingRunnable<Exception> disposer = () -> sharedResources.release(type, leaseHolder, releaser);
    return new OpaqueMemoryResource<>(resource.resourceHandle(), size, disposer);
}
Also used : MemorySegment(org.apache.flink.core.memory.MemorySegment) Logger(org.slf4j.Logger) ThrowingRunnable(org.apache.flink.util.function.ThrowingRunnable) Iterator(java.util.Iterator) MemorySegmentFactory.allocateOffHeapUnsafeMemory(org.apache.flink.core.memory.MemorySegmentFactory.allocateOffHeapUnsafeMemory) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Preconditions(org.apache.flink.util.Preconditions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LongConsumer(java.util.function.LongConsumer) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting) MathUtils(org.apache.flink.util.MathUtils) HashSet(java.util.HashSet) LongFunctionWithException(org.apache.flink.util.function.LongFunctionWithException) TaskManagerOptions(org.apache.flink.configuration.TaskManagerOptions) List(java.util.List) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) Nonnull(javax.annotation.Nonnull) ConcurrentModificationException(java.util.ConcurrentModificationException) LongFunctionWithException(org.apache.flink.util.function.LongFunctionWithException) NoSuchElementException(java.util.NoSuchElementException) ConcurrentModificationException(java.util.ConcurrentModificationException) LongConsumer(java.util.function.LongConsumer)

Example 2 with LongFunctionWithException

use of org.apache.flink.util.function.LongFunctionWithException in project flink by apache.

the class RocksDBOperationUtils method allocateSharedCachesIfConfigured.

@Nullable
public static OpaqueMemoryResource<RocksDBSharedResources> allocateSharedCachesIfConfigured(RocksDBMemoryConfiguration memoryConfig, MemoryManager memoryManager, double memoryFraction, Logger logger) throws IOException {
    if (!memoryConfig.isUsingFixedMemoryPerSlot() && !memoryConfig.isUsingManagedMemory()) {
        return null;
    }
    final double highPriorityPoolRatio = memoryConfig.getHighPriorityPoolRatio();
    final double writeBufferRatio = memoryConfig.getWriteBufferRatio();
    final boolean usingPartitionedIndexFilters = memoryConfig.isUsingPartitionedIndexFilters();
    final LongFunctionWithException<RocksDBSharedResources, Exception> allocator = (size) -> RocksDBMemoryControllerUtils.allocateRocksDBSharedResources(size, writeBufferRatio, highPriorityPoolRatio, usingPartitionedIndexFilters);
    try {
        if (memoryConfig.isUsingFixedMemoryPerSlot()) {
            assert memoryConfig.getFixedMemoryPerSlot() != null;
            logger.info("Getting fixed-size shared cache for RocksDB.");
            return memoryManager.getExternalSharedMemoryResource(FIXED_SLOT_MEMORY_RESOURCE_ID, allocator, memoryConfig.getFixedMemoryPerSlot().getBytes());
        } else {
            logger.info("Getting managed memory shared cache for RocksDB.");
            return memoryManager.getSharedMemoryResourceForManagedMemory(MANAGED_MEMORY_RESOURCE_ID, allocator, memoryFraction);
        }
    } catch (Exception e) {
        throw new IOException("Failed to acquire shared cache resource for RocksDB", e);
    }
}
Also used : OpaqueMemoryResource(org.apache.flink.runtime.memory.OpaqueMemoryResource) Arrays(java.util.Arrays) MemoryManager(org.apache.flink.runtime.memory.MemoryManager) LoggerFactory(org.slf4j.LoggerFactory) RocksDbTtlCompactFiltersManager(org.apache.flink.contrib.streaming.state.ttl.RocksDbTtlCompactFiltersManager) Function(java.util.function.Function) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) ArrayList(java.util.ArrayList) ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) RocksDB(org.rocksdb.RocksDB) Map(java.util.Map) ConfigConstants(org.apache.flink.configuration.ConfigConstants) RocksDBException(org.rocksdb.RocksDBException) MERGE_OPERATOR_NAME(org.apache.flink.contrib.streaming.state.RocksDBKeyedStateBackend.MERGE_OPERATOR_NAME) Nullable(javax.annotation.Nullable) ReadOptions(org.rocksdb.ReadOptions) IOUtils(org.apache.flink.util.IOUtils) Logger(org.slf4j.Logger) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) DBOptions(org.rocksdb.DBOptions) IOException(java.io.IOException) OperatingSystem(org.apache.flink.util.OperatingSystem) Preconditions(org.apache.flink.util.Preconditions) LongFunctionWithException(org.apache.flink.util.function.LongFunctionWithException) List(java.util.List) RegisteredStateMetaInfoBase(org.apache.flink.runtime.state.RegisteredStateMetaInfoBase) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) IOException(java.io.IOException) RocksDBException(org.rocksdb.RocksDBException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) IOException(java.io.IOException) LongFunctionWithException(org.apache.flink.util.function.LongFunctionWithException) Nullable(javax.annotation.Nullable)

Example 3 with LongFunctionWithException

use of org.apache.flink.util.function.LongFunctionWithException in project flink by apache.

the class BeamPythonFunctionRunner method open.

// ------------------------------------------------------------------------
@Override
public void open(PythonConfig config) throws Exception {
    this.bundleStarted = false;
    this.resultBuffer = new LinkedBlockingQueue<>();
    this.reusableResultTuple = new Tuple2<>();
    // The creation of stageBundleFactory depends on the initialized environment manager.
    environmentManager.open();
    PortablePipelineOptions portableOptions = PipelineOptionsFactory.as(PortablePipelineOptions.class);
    if (jobOptions.containsKey(PythonOptions.STATE_CACHE_SIZE.key())) {
        portableOptions.as(ExperimentalOptions.class).setExperiments(Collections.singletonList(ExperimentalOptions.STATE_CACHE_SIZE + "=" + jobOptions.get(PythonOptions.STATE_CACHE_SIZE.key())));
    }
    Struct pipelineOptions = PipelineOptionsTranslation.toProto(portableOptions);
    if (memoryManager != null && config.isUsingManagedMemory()) {
        Preconditions.checkArgument(managedMemoryFraction > 0 && managedMemoryFraction <= 1.0, "The configured managed memory fraction for Python worker process must be within (0, 1], was: %s. " + "It may be because the consumer type \"Python\" was missing or set to 0 for the config option \"taskmanager.memory.managed.consumer-weights\"." + managedMemoryFraction);
        final LongFunctionWithException<PythonSharedResources, Exception> initializer = (size) -> new PythonSharedResources(createJobBundleFactory(pipelineOptions), createPythonExecutionEnvironment(size));
        sharedResources = memoryManager.getSharedMemoryResourceForManagedMemory(MANAGED_MEMORY_RESOURCE_ID, initializer, managedMemoryFraction);
        LOG.info("Obtained shared Python process of size {} bytes", sharedResources.getSize());
        sharedResources.getResourceHandle().addPythonEnvironmentManager(environmentManager);
        JobBundleFactory jobBundleFactory = sharedResources.getResourceHandle().getJobBundleFactory();
        RunnerApi.Environment environment = sharedResources.getResourceHandle().getEnvironment();
        stageBundleFactory = createStageBundleFactory(jobBundleFactory, environment);
    } else {
        // there is no way to access the MemoryManager for the batch job of old planner,
        // fallback to the way that spawning a Python process for each Python operator
        jobBundleFactory = createJobBundleFactory(pipelineOptions);
        stageBundleFactory = createStageBundleFactory(jobBundleFactory, createPythonExecutionEnvironment(-1));
    }
    progressHandler = getProgressHandler(flinkMetricContainer);
}
Also used : PythonOptions(org.apache.flink.python.PythonOptions) OpaqueMemoryResource(org.apache.flink.runtime.memory.OpaqueMemoryResource) Arrays(java.util.Arrays) WindowedValue(org.apache.beam.sdk.util.WindowedValue) Tuple2(org.apache.flink.api.java.tuple.Tuple2) PortablePipelineOptions(org.apache.beam.sdk.options.PortablePipelineOptions) LoggerFactory(org.slf4j.LoggerFactory) TimerInternals(org.apache.beam.runners.core.TimerInternals) UserStateReference(org.apache.beam.runners.core.construction.graph.UserStateReference) PythonFunctionRunner(org.apache.flink.python.PythonFunctionRunner) WINDOW_CODER_ID(org.apache.flink.python.Constants.WINDOW_CODER_ID) SideInputReference(org.apache.beam.runners.core.construction.graph.SideInputReference) JobBundleFactory(org.apache.beam.runners.fnexecution.control.JobBundleFactory) Iterables(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables) Map(java.util.Map) TimerReference(org.apache.beam.runners.core.construction.graph.TimerReference) GlobalWindow(org.apache.beam.sdk.transforms.windowing.GlobalWindow) FlinkFnApi(org.apache.flink.fnexecution.v1.FlinkFnApi) JobInfo(org.apache.beam.runners.fnexecution.provisioning.JobInfo) TimerReceiverFactory(org.apache.beam.runners.fnexecution.control.TimerReceiverFactory) TimerRegistration(org.apache.flink.streaming.api.operators.python.timer.TimerRegistration) INPUT_COLLECTION_ID(org.apache.flink.python.Constants.INPUT_COLLECTION_ID) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) StageBundleFactory(org.apache.beam.runners.fnexecution.control.StageBundleFactory) PythonEnvironment(org.apache.flink.python.env.PythonEnvironment) FnDataReceiver(org.apache.beam.sdk.fn.data.FnDataReceiver) Collection(java.util.Collection) ImmutableExecutableStage(org.apache.beam.runners.core.construction.graph.ImmutableExecutableStage) BundleProgressHandler(org.apache.beam.runners.fnexecution.control.BundleProgressHandler) FlinkMetricContainer(org.apache.flink.python.metric.FlinkMetricContainer) BeamFnApi(org.apache.beam.model.fnexecution.v1.BeamFnApi) ExecutableStage(org.apache.beam.runners.core.construction.graph.ExecutableStage) Preconditions(org.apache.flink.util.Preconditions) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) ModelCoders(org.apache.beam.runners.core.construction.ModelCoders) LongFunctionWithException(org.apache.flink.util.function.LongFunctionWithException) List(java.util.List) WINDOW_STRATEGY(org.apache.flink.python.Constants.WINDOW_STRATEGY) Optional(java.util.Optional) OUTPUT_COLLECTION_ID(org.apache.flink.python.Constants.OUTPUT_COLLECTION_ID) MemoryManager(org.apache.flink.runtime.memory.MemoryManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ExperimentalOptions(org.apache.beam.sdk.options.ExperimentalOptions) Coder(org.apache.beam.sdk.coders.Coder) ProcessPythonEnvironmentManager(org.apache.flink.python.env.process.ProcessPythonEnvironmentManager) PipelineOptionsTranslation(org.apache.beam.runners.core.construction.PipelineOptionsTranslation) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) Environments(org.apache.beam.runners.core.construction.Environments) WRAPPER_TIMER_CODER_ID(org.apache.flink.python.Constants.WRAPPER_TIMER_CODER_ID) RemoteBundle(org.apache.beam.runners.fnexecution.control.RemoteBundle) BiConsumer(java.util.function.BiConsumer) DefaultJobBundleFactory(org.apache.beam.runners.fnexecution.control.DefaultJobBundleFactory) StateRequestHandler(org.apache.beam.runners.fnexecution.state.StateRequestHandler) Nullable(javax.annotation.Nullable) RunnerApi(org.apache.beam.model.pipeline.v1.RunnerApi) Logger(org.slf4j.Logger) ProtoUtils.createCoderProto(org.apache.flink.streaming.api.utils.ProtoUtils.createCoderProto) OutputReceiverFactory(org.apache.beam.runners.fnexecution.control.OutputReceiverFactory) ProcessPythonEnvironment(org.apache.flink.python.env.process.ProcessPythonEnvironment) IOException(java.io.IOException) KeyedStateBackend(org.apache.flink.runtime.state.KeyedStateBackend) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting) Timer(org.apache.beam.runners.core.construction.Timer) ByteArrayCoder(org.apache.beam.sdk.coders.ByteArrayCoder) BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) PipelineNode(org.apache.beam.runners.core.construction.graph.PipelineNode) TIMER_CODER_ID(org.apache.flink.python.Constants.TIMER_CODER_ID) Internal(org.apache.flink.annotation.Internal) Struct(org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.Struct) PythonConfig(org.apache.flink.python.PythonConfig) Collections(java.util.Collections) BeamUrns.getUrn(org.apache.beam.runners.core.construction.BeamUrns.getUrn) JobBundleFactory(org.apache.beam.runners.fnexecution.control.JobBundleFactory) DefaultJobBundleFactory(org.apache.beam.runners.fnexecution.control.DefaultJobBundleFactory) RunnerApi(org.apache.beam.model.pipeline.v1.RunnerApi) PortablePipelineOptions(org.apache.beam.sdk.options.PortablePipelineOptions) ExperimentalOptions(org.apache.beam.sdk.options.ExperimentalOptions) LongFunctionWithException(org.apache.flink.util.function.LongFunctionWithException) IOException(java.io.IOException) Struct(org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.Struct)

Aggregations

List (java.util.List)3 Map (java.util.Map)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Nullable (javax.annotation.Nullable)2 Preconditions (org.apache.flink.util.Preconditions)2 LongFunctionWithException (org.apache.flink.util.function.LongFunctionWithException)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Collections (java.util.Collections)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1