bkonold commented on a change in pull request #1367: URL: https://github.com/apache/samza/pull/1367#discussion_r437760212
########## File path: samza-core/src/main/java/org/apache/samza/storage/TaskSideInputHandler.java ########## @@ -0,0 +1,278 @@ +/* + * 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.samza.storage; + + +import com.google.common.annotations.VisibleForTesting; +import java.io.File; +import java.util.AbstractMap; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; +import org.apache.samza.Partition; +import org.apache.samza.SamzaException; +import org.apache.samza.container.TaskName; +import org.apache.samza.job.model.TaskMode; +import org.apache.samza.storage.kv.Entry; +import org.apache.samza.storage.kv.KeyValueStore; +import org.apache.samza.system.IncomingMessageEnvelope; +import org.apache.samza.system.StreamMetadataCache; +import org.apache.samza.system.SystemAdmins; +import org.apache.samza.system.SystemStream; +import org.apache.samza.system.SystemStreamMetadata; +import org.apache.samza.system.SystemStreamPartition; +import org.apache.samza.util.Clock; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import scala.collection.JavaConverters; + + +/** + * This class encapsulates all processing logic / state for all side input SSPs within a task. + */ +public class TaskSideInputHandler { + private static final Logger LOG = LoggerFactory.getLogger(TaskSideInputHandler.class); + + private final StorageManagerUtil storageManagerUtil = new StorageManagerUtil(); + + private final TaskName taskName; + private final TaskSideInputStorageManager taskSideInputStorageManager; + private final Map<SystemStreamPartition, Set<String>> sspToStores; + private final Map<String, SideInputsProcessor> storeToProcessor; + private final SystemAdmins systemAdmins; + private final StreamMetadataCache streamMetadataCache; + private final Map<SystemStreamPartition, String> lastProcessedOffsets = new ConcurrentHashMap<>(); + + private Map<SystemStreamPartition, String> startingOffsets; + + public TaskSideInputHandler( + TaskName taskName, + TaskMode taskMode, + File storeBaseDir, + Map<String, StorageEngine> storeToStorageEngines, + Map<String, Set<SystemStreamPartition>> storeToSSPs, + Map<String, SideInputsProcessor> storeToProcessor, + SystemAdmins systemAdmins, + StreamMetadataCache streamMetadataCache, + Clock clock) { + this.taskName = taskName; + this.systemAdmins = systemAdmins; + this.streamMetadataCache = streamMetadataCache; + this.storeToProcessor = storeToProcessor; + + this.sspToStores = storeToSSPs.entrySet().stream() + .flatMap(storeAndSSPs -> storeAndSSPs.getValue().stream() + .map(ssp -> new AbstractMap.SimpleImmutableEntry<>(ssp, storeAndSSPs.getKey()))) + .collect(Collectors.groupingBy( + Map.Entry::getKey, + Collectors.mapping(Map.Entry::getValue, Collectors.toSet()))); + + this.taskSideInputStorageManager = new TaskSideInputStorageManager(taskName, + taskMode, + storeBaseDir, + storeToStorageEngines, + storeToSSPs, + clock); + + validateProcessorConfiguration(); + } + + /** + * The {@link TaskName} associated with this {@link TaskSideInputHandler} + * + * @return the task name for this handler + */ + public TaskName getTaskName() { + return this.taskName; + } + + /** + * Initializes the underlying {@link TaskSideInputStorageManager} and determines starting offsets for each SSP. + */ + public void init() { + this.taskSideInputStorageManager.init(); + + Map<SystemStreamPartition, String> fileOffsets = this.taskSideInputStorageManager.getFileOffsets(); + LOG.info("File offsets for the task {}: {}", taskName, fileOffsets); + + this.lastProcessedOffsets.putAll(fileOffsets); + LOG.info("Last processed offsets for the task {}: {}", taskName, lastProcessedOffsets); + + this.startingOffsets = getStartingOffsets(fileOffsets, getOldestOffsets()); + LOG.info("Starting offsets for the task {}: {}", taskName, startingOffsets); + } + + /** + * Processes the incoming side input message envelope and updates the last processed offset for its SSP. + * Synchronized inorder to be exclusive with flush(). + * + * @param envelope incoming envelope to be processed + */ + public synchronized void process(IncomingMessageEnvelope envelope) { Review comment: will revisit in subsequent PR ---------------------------------------------------------------- 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]
