From notifications-return-926-archive-asf-public=cust-asf.ponee.io@zookeeper.apache.org Sat Jul 27 05:00:55 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 80AB2180595 for ; Sat, 27 Jul 2019 07:00:55 +0200 (CEST) Received: (qmail 6311 invoked by uid 500); 27 Jul 2019 05:00:55 -0000 Mailing-List: contact notifications-help@zookeeper.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zookeeper.apache.org Delivered-To: mailing list notifications@zookeeper.apache.org Received: (qmail 6302 invoked by uid 99); 27 Jul 2019 05:00:54 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 27 Jul 2019 05:00:54 +0000 From: GitBox To: notifications@zookeeper.apache.org Subject: [GitHub] [zookeeper] hanm commented on a change in pull request #989: ZOOKEEPER-3430: Observability improvement: provide top N read / write path queries. Message-ID: <156420365484.29118.18133510947545765924.gitbox@gitbox.apache.org> Date: Sat, 27 Jul 2019 05:00:54 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit hanm commented on a change in pull request #989: ZOOKEEPER-3430: Observability improvement: provide top N read / write path queries. URL: https://github.com/apache/zookeeper/pull/989#discussion_r307953303 ########## File path: zookeeper-server/src/main/java/org/apache/zookeeper/server/util/RequestPathMetricsCollector.java ########## @@ -0,0 +1,385 @@ +/** + * 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.zookeeper.server.util; + +import java.io.PrintWriter; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.StringTokenizer; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; +import java.util.function.Predicate; + +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.server.Request; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.zookeeper.ZooDefs.OpCode.checkWatches; +import static org.apache.zookeeper.ZooDefs.OpCode.create; +import static org.apache.zookeeper.ZooDefs.OpCode.create2; +import static org.apache.zookeeper.ZooDefs.OpCode.createContainer; +import static org.apache.zookeeper.ZooDefs.OpCode.delete; +import static org.apache.zookeeper.ZooDefs.OpCode.deleteContainer; +import static org.apache.zookeeper.ZooDefs.OpCode.exists; +import static org.apache.zookeeper.ZooDefs.OpCode.getACL; +import static org.apache.zookeeper.ZooDefs.OpCode.getChildren; +import static org.apache.zookeeper.ZooDefs.OpCode.getChildren2; +import static org.apache.zookeeper.ZooDefs.OpCode.getData; +import static org.apache.zookeeper.ZooDefs.OpCode.removeWatches; +import static org.apache.zookeeper.ZooDefs.OpCode.setACL; +import static org.apache.zookeeper.ZooDefs.OpCode.setData; +import static org.apache.zookeeper.ZooDefs.OpCode.sync; + +/** + * This class holds the requests path ( up till a certain depth) stats per request type + */ +public class RequestPathMetricsCollector { + private static final Logger LOG = LoggerFactory.getLogger(RequestPathMetricsCollector.class); + // How many seconds does each slot represent, default is 15 seconds. + private final int REQUEST_STATS_SLOT_DURATION; + // How many slots we keep, default is 60 so it's 15 minutes total history. + private final int REQUEST_STATS_SLOT_CAPACITY; + // How far down the path we keep, default is 6. + private final int REQUEST_PREPROCESS_PATH_DEPTH; + // Sample rate, default is 0.1 (10%). + private final float REQUEST_PREPROCESS_SAMPLE_RATE; + private final long COLLECTOR_INITIAL_DELAY; + private final long COLLECTOR_DELAY; + private final int REQUEST_PREPROCESS_TOPPATH_MAX; + private final boolean disabled; + + public static final String PATH_STATS_SLOT_CAPACITY = "zookeeper.pathStats.slotCapacity"; + public static final String PATH_STATS_SLOT_DURATION = "zookeeper.pathStats.slotDuration"; + public static final String PATH_STATS_MAX_DEPTH = "zookeeper.pathStats.maxDepth"; + public static final String PATH_STATS_SAMPLE_RATE = "zookeeper.pathStats.sampleRate"; + public static final String PATH_STATS_COLLECTOR_INITIAL_DELAY = "zookeeper.pathStats.initialDelay"; + public static final String PATH_STATS_COLLECTOR_DELAY = "zookeeper.pathStats.delay"; + public static final String PATH_STATS_TOP_PATH_MAX = "zookeeper.pathStats.topPathMax"; + public static final String PATH_STATS_DISABLE = "zookeeper.pathStats.disable"; + + private final Map immutableRequestsMap; + private final Random sampler; + private final ScheduledThreadPoolExecutor scheduledExecutor; + private final boolean accurateMode; + + public RequestPathMetricsCollector() { + this(false); + } + + public RequestPathMetricsCollector(boolean accurateMode) { + final HashMap requestsMap = new HashMap<>(); + this.sampler = new Random(System.currentTimeMillis()); + this.accurateMode = accurateMode; + + REQUEST_PREPROCESS_TOPPATH_MAX = Integer.getInteger(PATH_STATS_TOP_PATH_MAX, 20); + REQUEST_STATS_SLOT_DURATION = Integer.getInteger(PATH_STATS_SLOT_DURATION, 15); + REQUEST_STATS_SLOT_CAPACITY = Integer.getInteger(PATH_STATS_SLOT_CAPACITY, 60); + REQUEST_PREPROCESS_PATH_DEPTH = Integer.getInteger(PATH_STATS_MAX_DEPTH, 6); + REQUEST_PREPROCESS_SAMPLE_RATE = Float.parseFloat( + System.getProperty(PATH_STATS_SAMPLE_RATE, "0.1")); + COLLECTOR_INITIAL_DELAY = Long.getLong(PATH_STATS_COLLECTOR_INITIAL_DELAY, 5); + COLLECTOR_DELAY = Long.getLong(PATH_STATS_COLLECTOR_DELAY, 5); + disabled = Boolean.getBoolean(PATH_STATS_DISABLE); + + LOG.info("{} = {}", PATH_STATS_SLOT_CAPACITY, REQUEST_STATS_SLOT_CAPACITY); + LOG.info("{} = {}", PATH_STATS_SLOT_DURATION, REQUEST_STATS_SLOT_DURATION); + LOG.info("{} = {}", PATH_STATS_MAX_DEPTH, REQUEST_PREPROCESS_PATH_DEPTH); + LOG.info("{} = {}", PATH_STATS_COLLECTOR_INITIAL_DELAY, COLLECTOR_INITIAL_DELAY); + LOG.info("{} = {}", PATH_STATS_COLLECTOR_DELAY, COLLECTOR_DELAY); + LOG.info("{} = {}", PATH_STATS_DISABLE, disabled); + + this.scheduledExecutor = (ScheduledThreadPoolExecutor) Executors + .newScheduledThreadPool(Runtime.getRuntime().availableProcessors()); + scheduledExecutor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); + scheduledExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); + requestsMap.put(Request.op2String(create), new PathStatsQueue(create)); + requestsMap.put(Request.op2String(create2), new PathStatsQueue(create2)); + requestsMap.put(Request.op2String(createContainer), new PathStatsQueue(createContainer)); + requestsMap.put(Request.op2String(deleteContainer), new PathStatsQueue(deleteContainer)); + requestsMap.put(Request.op2String(delete), new PathStatsQueue(delete)); + requestsMap.put(Request.op2String(exists), new PathStatsQueue(exists)); + requestsMap.put(Request.op2String(setData), new PathStatsQueue(setData)); + requestsMap.put(Request.op2String(getData), new PathStatsQueue(getData)); + requestsMap.put(Request.op2String(getACL), new PathStatsQueue(getACL)); + requestsMap.put(Request.op2String(setACL), new PathStatsQueue(setACL)); + requestsMap.put(Request.op2String(getChildren), new PathStatsQueue(getChildren)); + requestsMap.put(Request.op2String(getChildren2), new PathStatsQueue(getChildren2)); + requestsMap.put(Request.op2String(checkWatches), new PathStatsQueue(checkWatches)); + requestsMap.put(Request.op2String(removeWatches), new PathStatsQueue(removeWatches)); + requestsMap.put(Request.op2String(sync), new PathStatsQueue(sync)); + this.immutableRequestsMap = java.util.Collections.unmodifiableMap(requestsMap); + } + + static boolean isWriteOp(int requestType) { + switch (requestType) { + case ZooDefs.OpCode.sync: + case ZooDefs.OpCode.create: + case ZooDefs.OpCode.create2: + case ZooDefs.OpCode.createContainer: + case ZooDefs.OpCode.delete: + case ZooDefs.OpCode.deleteContainer: + case ZooDefs.OpCode.setData: + case ZooDefs.OpCode.reconfig: + case ZooDefs.OpCode.setACL: + case ZooDefs.OpCode.multi: + case ZooDefs.OpCode.check: + return true; + } + return false; + } + + static String trimPathDepth(String path, int maxDepth) { + int count = 0; + String pathSeparator = "/"; + StringBuilder sb = new StringBuilder(); + StringTokenizer pathTokenizer = new StringTokenizer(path, pathSeparator); + while (pathTokenizer.hasMoreElements() && count++ < maxDepth) { + sb.append(pathSeparator); + sb.append(pathTokenizer.nextToken()); + } + path = sb.toString(); + return path; + } + + @Override + protected void finalize() { + shutdown(); + } + + public void shutdown(){ + if (disabled) return; + + LOG.info("shutdown scheduledExecutor"); + scheduledExecutor.shutdownNow(); + } + + public void start() { + if (disabled) return; + + LOG.info("Start the RequestPath collector"); + immutableRequestsMap.forEach((opType, pathStatsQueue) -> pathStatsQueue.start()); + + // Schedule to log the top used read/write paths every 5 mins + scheduledExecutor.scheduleWithFixedDelay(() -> { + LOG.info("%nHere are the top Read paths:"); + logTopPaths(aggregatePaths(4, queue -> !queue.isWriteOperation()), + entry -> LOG.info(entry.getKey() + " : " + entry.getValue())); + LOG.info("%nHere are the top Write paths:"); + logTopPaths(aggregatePaths(4, queue -> queue.isWriteOperation()), + entry -> LOG.info(entry.getKey() + " : " + entry.getValue())); + }, COLLECTOR_INITIAL_DELAY, COLLECTOR_DELAY, TimeUnit.MINUTES); + } + + /** + * The public interface of the buffer. FinalRequestHandler will call into this for + * each request that has a path and this needs to be fast. we sample the path so that + * we don't have to store too many paths in memory + */ + public void registerRequest(int type, String path) { + if (sampler.nextFloat() <= REQUEST_PREPROCESS_SAMPLE_RATE) { + PathStatsQueue pathStatsQueue = immutableRequestsMap.get(Request.op2String(type)); + if (pathStatsQueue != null) { + pathStatsQueue.registerRequest(path); + } else { + LOG.error("We should not handle {}", type); + } + } + } + + public void dumpTopRequestPath(PrintWriter pwriter, String requestTypeName, int queryMaxDepth) { + if (queryMaxDepth < 1) return; Review comment: done ---------------------------------------------------------------- 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