jhuan31 commented on a change in pull request #1116: ZOOKEEPER-3575: Moving sending packets in Learner to a separate thread URL: https://github.com/apache/zookeeper/pull/1116#discussion_r356949918
########## File path: zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSender.java ########## @@ -0,0 +1,82 @@ +/* + * 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.quorum; + +import java.io.IOException; +import java.util.concurrent.LinkedBlockingQueue; +import org.apache.zookeeper.server.ZooKeeperCriticalThread; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LearnerSender extends ZooKeeperCriticalThread { + private static final Logger LOG = LoggerFactory.getLogger(LearnerSender.class); + + private final LinkedBlockingQueue<QuorumPacket> queuedPackets = new LinkedBlockingQueue<>(); + private final QuorumPacket proposalOfDeath = new QuorumPacket(); + + Learner learner; + + public LearnerSender(Learner learner) { + super("LearnerSender:" + learner.zk.getServerId(), learner.zk.getZooKeeperServerListener()); + this.learner = learner; + } + + @Override + public void run() { + while (true) { + try { + QuorumPacket p = queuedPackets.poll(); + if (p == null) { + learner.bufferedOutput.flush(); + p = queuedPackets.take(); + } + + if (p == proposalOfDeath) { + // Packet of death! + break; + } + + learner.messageTracker.trackSent(p.getType()); Review comment: Without LearnerSender, there might be more than one thread writing to leaderOs so synchronization is required. With LearnerSender, there still might be more than one thread calling writePacket but they are writing to a BlockingQueue so it's fine. Reading from the queue and writing to leaderOs is one thread (the learner sender thread), so we don't need synchronization. It's like we synchronize on the blockingQueue implicitly. This is tricky. I may have missed some thing. Let me know and I'll fix the code. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
