zipkin-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-zipkin] adriancole commented on a change in pull request #2502: Adding storage-throttle module to address "over capacity" issues
Date Sat, 04 May 2019 00:59:06 GMT
adriancole commented on a change in pull request #2502: Adding storage-throttle module to address
"over capacity" issues
URL: https://github.com/apache/incubator-zipkin/pull/2502#discussion_r280962865
 
 

 ##########
 File path: zipkin-server/src/main/java/zipkin2/server/internal/throttle/ThrottledStorageComponent.java
 ##########
 @@ -0,0 +1,179 @@
+/*
+ * 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 zipkin2.server.internal.throttle;
+
+import com.netflix.concurrency.limits.Limit;
+import com.netflix.concurrency.limits.limit.Gradient2Limit;
+import com.netflix.concurrency.limits.limiter.AbstractLimiter;
+import io.micrometer.core.instrument.MeterRegistry;
+import java.io.IOException;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+import zipkin2.storage.SpanConsumer;
+import zipkin2.storage.SpanStore;
+import zipkin2.storage.StorageComponent;
+
+/**
+ * Delegating implementation that limits requests to the {@link #spanConsumer()} of another
+ * {@link StorageComponent}.  The theory here is that this class can be used to:
+ * <ul>
+ * <li>Prevent spamming the storage engine with excessive, spike requests when they
come in; thus preserving it's life.</li>
+ * <li>Optionally act as a buffer so that a fixed number requests can be queued for
execution when the throttle allows
+ *     for it.  This optional queue must be bounded in order to avoid running out of memory
from infinitely queueing.</li>
+ * </ul>
+ *
+ * @see ThrottledSpanConsumer
+ */
+public final class ThrottledStorageComponent extends StorageComponent {
+  final StorageComponent delegate;
+  final AbstractLimiter<Void> limiter;
+  final ThreadPoolExecutor executor;
+
+  public ThrottledStorageComponent(StorageComponent delegate,
+                                   MeterRegistry registry,
+                                   int minConcurrency,
+                                   int maxConcurrency,
+                                   int maxQueueSize) {
+    this.delegate = Objects.requireNonNull(delegate);
+
+    Limit limit = Gradient2Limit.newBuilder()
+        .minLimit(minConcurrency)
+        .initialLimit(minConcurrency) // Limiter will trend towards min until otherwise necessary
so may as well start there
+        .maxConcurrency(maxConcurrency)
+        .queueSize(0)
+        .build();
+    this.limiter = new Builder().limit(limit).build();
+
+    this.executor = new ThreadPoolExecutor(limit.getLimit(),
+                                           limit.getLimit(),
+                                           0,
+                                           TimeUnit.DAYS,
+                                           createQueue(maxQueueSize),
+                                           new ThottledThreadFactory(),
+                                           new ThreadPoolExecutor.AbortPolicy());
+    limit.notifyOnChange(new ThreadPoolExecutorResizer(executor));
+
+    if (registry != null) {
 
 Review comment:
   do you expect this to be null?

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


With regards,
Apache Git Services

Mime
View raw message