weisong44 commented on a change in pull request #1031: SAMZA-2191: Batch support for table APIs URL: https://github.com/apache/samza/pull/1031#discussion_r284049530
########## File path: samza-core/src/main/java/org/apache/samza/table/batching/BatchProcessor.java ########## @@ -0,0 +1,163 @@ +/* + * 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.table.batching; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; +import org.apache.samza.table.batch.BatchPolicy; + + +/** + * Place a sequence of operations into a {@link Batch}. When a batch is not allowed to accept more + * operations, it will handled by a {@link BatchHandler}. Meanwhile, a new {@link Batch} will be + * created and a timer will be set for it. + * + * @param <K> The type of the key associated with the {@link Operation} + * @param <V> The type of the value associated with the {@link Operation} + */ +public class BatchProcessor<K, V> { + private final ScheduledExecutorService scheduledExecutorService; + private final ReentrantLock lock = new ReentrantLock(); + private final BatchPolicy batchPolicy; + private final BatchHandler<K, V> batchHandler; + private Batch<K, V> batch; + private ScheduledFuture<?> scheduledFuture; + + /** + * @param batchHandler Defines how each batch will be processed. + * @param batchPolicy The batch configurations to be used. + * @param scheduledExecutorService A scheduled executor service to set timers for the managed batches. + */ + public BatchProcessor(BatchHandler<K, V> batchHandler, BatchPolicy batchPolicy, + ScheduledExecutorService scheduledExecutorService) { + Preconditions.checkNotNull(batchHandler); + Preconditions.checkNotNull(batchPolicy); + Preconditions.checkNotNull(scheduledExecutorService); + + this.batchHandler = batchHandler; + this.scheduledExecutorService = scheduledExecutorService; + this.batchPolicy = batchPolicy; + batch = BatchFactory.getBatch(batchHandler, batchPolicy); + setBatchTimer(batch); + } + + private CompletableFuture<Void> addOperation(Operation<K, V> operation) { + if (batch.isEmpty()) { + setBatchTimer(batch); + } else if (batch.isClosed()) { + processBatch(); + } + batch.addOperation(operation); + final CompletableFuture<Void> res = batch.getCompletableFuture(); Review comment: If it's GET, shouldn't we return the future in the GET operation? ---------------------------------------------------------------- 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
