From dev-return-75319-archive-asf-public=cust-asf.ponee.io@zookeeper.apache.org Wed Oct 31 22:34:36 2018 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 [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id E1D3F180668 for ; Wed, 31 Oct 2018 22:34:35 +0100 (CET) Received: (qmail 8054 invoked by uid 500); 31 Oct 2018 21:20:34 -0000 Mailing-List: contact dev-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 dev@zookeeper.apache.org Received: (qmail 5743 invoked by uid 99); 31 Oct 2018 21:20:12 -0000 Received: from Unknown (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 31 Oct 2018 21:20:12 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 30B53DFD77; Wed, 31 Oct 2018 21:19:41 +0000 (UTC) From: lvfangmin To: dev@zookeeper.apache.org Reply-To: dev@zookeeper.apache.org References: In-Reply-To: Subject: [GitHub] zookeeper pull request #684: ZOOKEEPER-3180: Add response cache to improve t... Content-Type: text/plain Message-Id: <20181031211941.30B53DFD77@git1-us-west.apache.org> Date: Wed, 31 Oct 2018 21:19:41 +0000 (UTC) Github user lvfangmin commented on a diff in the pull request: https://github.com/apache/zookeeper/pull/684#discussion_r229870463 --- Diff: zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerCnxn.java --- @@ -68,29 +70,74 @@ private volatile boolean stale = false; + final ZooKeeperServer zkServer; + + public ServerCnxn(final ZooKeeperServer zkServer) { + this.zkServer = zkServer; + } + abstract int getSessionTimeout(); abstract void close(); + public abstract void sendResponse(ReplyHeader h, Record r, + String tag, String cacheKey, Stat stat) throws IOException; + public void sendResponse(ReplyHeader h, Record r, String tag) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - // Make space for length + sendResponse(h, r, tag, null, null); + } + + protected byte[] serializeRecord(Record record) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream( + ZooKeeperServer.intBufferStartingSizeBytes); BinaryOutputArchive bos = BinaryOutputArchive.getArchive(baos); - try { - baos.write(fourBytes); - bos.writeRecord(h, "header"); - if (r != null) { - bos.writeRecord(r, tag); + bos.writeRecord(record, null); + return baos.toByteArray(); + } + + protected ByteBuffer[] serialize(ReplyHeader h, Record r, String tag, + String cacheKey, Stat stat) throws IOException { + byte[] header = serializeRecord(h); + byte[] data = null; + if (r != null) { + ResponseCache cache = zkServer.getReadResponseCache(); + if (cache != null && stat != null && cacheKey != null && + !cacheKey.endsWith(Quotas.statNode)) { + // Use cache to get serialized data. + // + // NB: Tag is ignored both during cache lookup and serialization, + // since is is not used in read responses, which are being cached. + data = cache.get(cacheKey, stat); + if (data == null) { + // Cache miss, serialize the response and put it in cache. + data = serializeRecord(r); --- End diff -- We may hit the race condition to serialize the same record multiple times, but we made a trade off, instead of having write lock every time, this might be more efficient. ---