keith-turner commented on a change in pull request #969: [WIP] Issue-967 URL: https://github.com/apache/fluo/pull/969#discussion_r150892232
########## File path: modules/core/src/main/java/org/apache/fluo/core/impl/AsyncReader.java ########## @@ -0,0 +1,81 @@ +/* + * 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.fluo.core.impl; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.apache.fluo.api.data.Bytes; +import org.apache.fluo.api.data.Column; +import org.apache.fluo.api.data.RowColumn; +import org.apache.fluo.core.impl.TransactionImpl; + +public class AsyncReader { + private PriorityQueue<AsyncGet> getsQueue; + private ExecutorService executorService; + private TransactionImpl tx; + + public AsyncReader(TransactionImpl tx) { + this.tx = tx; + getsQueue = new PriorityQueue<>(); + executorService = Executors.newSingleThreadExecutor(); + } + + public CompletableFuture<String> gets(String row, Column column) { + return gets(row, column, null); + } + + public CompletableFuture<String> gets(String row, Column column, String defaultValue) { + AsyncGet curAsyncGet = new AsyncGet(row, column, defaultValue); + getsQueue.add(curAsyncGet); + executorService.submit(() -> { + int i = 0; Review comment: In the face of concurrency this lambda is not correct. I would recommend something like the following and I will try to explain why. ```java //use arraylist instead of hashset because it has better performance and no need for unique List <AsyncGet> gets = new ArrayList(); asyncGets.drainTo(gets); //not sure if I have the syntax below correct, but I think Guava has this method Collection<RowColumn> rowColumns = Collections2.transform(gets, ag -> ag.rc); Map<RowColumn, Bytes> getsMap = tx.get(rowColumns); //process everything we drained off the queue... do not want to touch the queue as new things may have been added. for(AsyncGet asynGet : gets) { Bytes result = getsMap.get(asyncGet); asyncGet.res.complete(result); //TODO handle default value } ``` With the current code if the execservice had multiple threads then multiple threads could process the same AsyncGet request. Using drainTo with LinkedBlockingQueue avoids this. This is not true for all collections, but this particular collection impl gets a lock in drainTo. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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
