tzulitai commented on a change in pull request #33: [FLINK-16244] Flush async operations lazily URL: https://github.com/apache/flink-statefun/pull/33#discussion_r383921823
########## File path: statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/functions/PendingAsyncOperations.java ########## @@ -0,0 +1,125 @@ +/* + * 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.flink.statefun.flink.core.functions; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.function.Consumer; +import org.apache.flink.api.common.state.MapState; +import org.apache.flink.statefun.flink.core.di.Inject; +import org.apache.flink.statefun.flink.core.di.Label; +import org.apache.flink.statefun.flink.core.message.Message; +import org.apache.flink.statefun.flink.core.state.State; +import org.apache.flink.statefun.sdk.Address; + +final class PendingAsyncOperations { + + /** + * holds the currently in flight and not yet checkpointed async operations. + * + * <p>A key can be removed from this map in two ways: + * + * <ul> + * <li>If a remove method was called explicitly (as a result of an async operation completion) + * <li>A checkpoint happens while the async operation is in flight (flush is called). In that + * case the key would be removed from the memoryStore, and written to the backingStore. + * </ul> + */ + private final Map<Address, Map<Long, Message>> memoryStore = new HashMap<>(); + + /** the underlying backing state handle */ + private final MapState<Long, Message> backingStore; + + private final Consumer<Address> keySetter; + + @Inject + PendingAsyncOperations( + @Label("state") State state, + @Label("async-operations") MapState<Long, Message> backingStore) { + this.backingStore = Objects.requireNonNull(backingStore); + this.keySetter = state::setCurrentKey; + } + + /** + * Adds an uncompleted async operation. + * + * @param owningAddress the address that had registered the async operation + * @param futureId the futureId that is associated with that operation + * @param message the message that was registered with that operation + */ + void add(Address owningAddress, long futureId, Message message) { + Map<Long, Message> asyncOps = + memoryStore.computeIfAbsent(owningAddress, unused -> new HashMap<>()); + asyncOps.put(futureId, message); + } + + /** + * Removes the completed async operation. + * + * <p>NOTE: this method should be called with {@link + * org.apache.flink.statefun.flink.core.state.State#setCurrentKey(Address)} set on the + * owningAddress. This should be the case as it is called by {@link + * AsyncMessageDecorator#postApply()}. + */ + void remove(Address owningAddress, long futureId) { + Map<Long, Message> asyncOps = memoryStore.get(owningAddress); + if (asyncOps == null) { + // there are no async operations in the memory store, + // therefore it must have been previously flushed to the backing store. + removeFromTheBackingStore(owningAddress, futureId); + return; + } + Message message = asyncOps.remove(futureId); + if (message == null) { Review comment: so - IMO we should throw already in this branch, instead of calling `removeFromBackingStore` which would anyway always throw in this case as well. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
