You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2018/03/29 21:16:57 UTC

[GitHub] eolivelli commented on a change in pull request #1309: Refactored OrderedSafeExecutor and OrderedScheduler

eolivelli commented on a change in pull request #1309: Refactored OrderedSafeExecutor and OrderedScheduler
URL: https://github.com/apache/bookkeeper/pull/1309#discussion_r178182068
 
 

 ##########
 File path: bookkeeper-common/src/main/java/org/apache/bookkeeper/common/util/OrderedExecutor.java
 ##########
 @@ -0,0 +1,529 @@
+/**
+ * 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.bookkeeper.common.util;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+
+import io.netty.util.concurrent.DefaultThreadFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.bookkeeper.stats.Gauge;
+import org.apache.bookkeeper.stats.NullStatsLogger;
+import org.apache.bookkeeper.stats.OpStatsLogger;
+import org.apache.bookkeeper.stats.StatsLogger;
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * This class provides 2 things over the java {@link ExecutorService}.
+ *
+ * <p>1. It takes {@link SafeRunnable objects} instead of plain Runnable objects.
+ * This means that exceptions in scheduled tasks wont go unnoticed and will be
+ * logged.
+ *
+ * <p>2. It supports submitting tasks with an ordering key, so that tasks submitted
+ * with the same key will always be executed in order, but tasks across
+ * different keys can be unordered. This retains parallelism while retaining the
+ * basic amount of ordering we want (e.g. , per ledger handle). Ordering is
+ * achieved by hashing the key objects to threads by their {@link #hashCode()}
+ * method.
+ */
+@Slf4j
+public class OrderedExecutor implements ExecutorService {
+    public static final int NO_TASK_LIMIT = -1;
+    protected static final long WARN_TIME_MICRO_SEC_DEFAULT = TimeUnit.SECONDS.toMicros(1);
+
+    final String name;
+    final ExecutorService threads[];
+    final long threadIds[];
+    final Random rand = new Random();
+    final OpStatsLogger taskExecutionStats;
+    final OpStatsLogger taskPendingStats;
+    final boolean traceTaskExecution;
+    final long warnTimeMicroSec;
+    final int maxTasksInQueue;
+
+
+    public static Builder newBuilder() {
+        return new Builder();
+    }
+
+    /**
+     * A builder class for an OrderedSafeExecutor.
+     */
+    public static class Builder extends AbstractBuilder<OrderedExecutor> {
+
+        @Override
+        public OrderedExecutor build() {
+            if (null == threadFactory) {
+                threadFactory = new DefaultThreadFactory("bookkeeper-ordered-safe-executor");
+            }
+            return new OrderedExecutor(name, numThreads, threadFactory, statsLogger,
+                                           traceTaskExecution, warnTimeMicroSec, maxTasksInQueue);
+        }
+    }
+
+    /**
+     * Builder to build ordered scheduler.
+     */
+    public static class SchedulerBuilder extends AbstractBuilder<OrderedExecutor> {}
+
+    /**
+     * Abstract builder class to build {@link OrderedScheduler}.
+     */
+    public abstract static class AbstractBuilder<T extends OrderedExecutor> {
 
 Review comment:
   Why public?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services