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.
/*
* Copyright (c) 2008-2024, Hazelcast, Inc. All Rights Reserved.
*
* Licensed 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 com.hazelcast.jet.impl.processor;
import com.hazelcast.function.BiFunctionEx;
import com.hazelcast.function.FunctionEx;
import com.hazelcast.internal.metrics.Probe;
import com.hazelcast.internal.util.concurrent.ManyToOneConcurrentArrayQueue;
import com.hazelcast.internal.util.counters.Counter;
import com.hazelcast.internal.util.counters.SwCounter;
import com.hazelcast.jet.JetException;
import com.hazelcast.jet.Traverser;
import com.hazelcast.jet.Traversers;
import com.hazelcast.jet.core.BroadcastKey;
import com.hazelcast.jet.core.Processor;
import com.hazelcast.jet.core.ProcessorSupplier;
import com.hazelcast.jet.core.ResettableSingletonTraverser;
import com.hazelcast.jet.core.Watermark;
import com.hazelcast.jet.datamodel.Tuple2;
import com.hazelcast.jet.datamodel.Tuple3;
import com.hazelcast.jet.pipeline.ServiceFactory;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import static com.hazelcast.internal.util.ExceptionUtil.withTryCatch;
import static com.hazelcast.jet.Traversers.traverseIterable;
import static com.hazelcast.jet.Util.entry;
import static com.hazelcast.jet.core.BroadcastKey.broadcastKey;
import static com.hazelcast.jet.datamodel.Tuple2.tuple2;
import static com.hazelcast.jet.datamodel.Tuple3.tuple3;
import static com.hazelcast.jet.impl.processor.ProcessorSupplierWithService.supplierWithService;
/**
* Processor which, for each received item, emits all the items from the
* traverser returned by the given async item-to-traverser function, using a
* service.
*
* This processors might reorder items: results are emitted as they are
* asynchronously delivered. However, this processor doesn't reorder items with
* respect to the watermarks that followed them. That is, a watermark is
* guaranteed to be emitted after results for all items that occurred
* before it are emitted.
*
* @param service type
* @param received item type
* @param extracted key type
* @param emitted item type
*/
public final class AsyncTransformUsingServiceUnorderedP extends AbstractAsyncTransformUsingServiceP {
/*
HOW IT WORKS
When an event is received, we submit the async op, and when response is received, we add it to resultQueue.
In the resultQueue we also store the last received WM value for each key.
Separately, we track watermark counts for each key, which we increment for each WM key when an event is received,
and decrement when a response is processed. The count is the count of events received _since_ that WM, _before_
the next WM. When the count gets to 0, we know we can emit the next watermark, because all the responses
for events received before it were already sent.
Snapshot contains in-flight elements at the time of taking the snapshot.
They are replayed when state is restored from the snapshot.
*/
private final BiFunctionEx super S, ? super T, ? extends CompletableFuture>> callAsyncFn;
private final Function super T, ? extends K> extractKeyFn;
private ManyToOneConcurrentArrayQueue> resultQueue;
/**
* Each watermark count map contains:
*
*
key: watermark timestamp or {@link Long#MIN_VALUE} for items before first watermark
*
value: number of items received _after_ this WM and _before_ next WM (if any)
* that are still being processed.
*
*/
// TODO we can use more efficient structure: we only remove from the beginning and add to the end
@SuppressWarnings("unchecked")
private SortedMap[] watermarkCounts = new SortedMap[0];
/**
* Current in-flight items.
*
* Invariants:
*
*
for each key, value > 0. Finished items are immediately removed
*
sum of all values in {@link #inFlightItems} is equal to
* {@link #asyncOpsCounter}, and to the sum of values in every map in the
* {@link #watermarkCounts} array.
*
*
* This is {@link IdentityHashMap} but after restoring from snapshot objects
* that used single shared instance (e.g. {@link String})
* may no longer be the same shared instance.
*/
private final Map inFlightItems = new IdentityHashMap<>();
private Traverser