You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by gi...@git.apache.org on 2017/09/20 14:27:24 UTC

[GitHub] eolivelli commented on a change in pull request #518: Issue 517: port useful utilities from distributredlog

eolivelli commented on a change in pull request #518: Issue 517: port useful utilities from distributredlog
URL: https://github.com/apache/bookkeeper/pull/518#discussion_r139984919
 
 

 ##########
 File path: bookkeeper-common/src/main/java/org/apache/bookkeeper/common/util/SharedResourceManager.java
 ##########
 @@ -0,0 +1,164 @@
+/*
+ * 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 static com.google.common.base.Preconditions.checkState;
+
+import java.util.IdentityHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
+
+/**
+ * A holder for shared resource singletons.
+ *
+ * <p>Components like clients and servers need cetain resources, e.g. a scheduler,
+ * to run. If the user has not provided such resources, these components will use
+ * a default one, which is shared as a static resource. This class holds these default
+ * resources and manages their lifecycles.
+ *
+ * <p>A resource is identified by the reference of a {@link Resource} object, which
+ * is typically a singleton, provided to the get() and release() methods. Each resource
+ * object (not its class) maps to an object cached in the holder.
+ *
+ * <p>Resources are ref-counted and shut down after a delay when the refcount reaches zero.
+ */
+public class SharedResourceManager {
+
+    static final long DESTROY_DELAY_SECONDS = 1;
+
+    private static final SharedResourceManager SHARED = create();
+
+    public static SharedResourceManager shared() {
+        return SHARED;
+    }
+
+    public static SharedResourceManager create() {
+        return create(() -> Executors.newSingleThreadScheduledExecutor(
+            ExecutorUtils.getThreadFactory("zstream-shared-destroyer-%d", true)));
 
 Review comment:
   bkstream ?
 
----------------------------------------------------------------
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