Skip to content

Commit

Permalink
Fix for ZOOKEEPER-3072
Browse files Browse the repository at this point in the history
Making the throttle check before passing over the request to the next thread will prevent the possibility of throttling code running after unthrottle
  • Loading branch information
Botond Hejj committed Jul 6, 2018
1 parent 25fd549 commit e27f95a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ public void processPacket(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOE
}
return;
} else {
cnxn.incrOutstandingRequests(h);
if (h.getType() == OpCode.sasl) {
Record rsp = processSasl(incomingBuffer,cnxn);
ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.OK.intValue());
Expand All @@ -1140,7 +1141,6 @@ public void processPacket(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOE
submitRequest(si);
}
}
cnxn.incrOutstandingRequests(h);
}

private Record processSasl(ByteBuffer incomingBuffer, ServerCnxn cnxn) throws IOException {
Expand Down
104 changes: 104 additions & 0 deletions src/java/test/org/apache/zookeeper/test/AsyncHammerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.ZKTestCase;
import org.apache.zookeeper.AsyncCallback.DataCallback;
import org.apache.zookeeper.AsyncCallback.StatCallback;
import org.apache.zookeeper.AsyncCallback.StringCallback;
import org.apache.zookeeper.AsyncCallback.VoidCallback;
import org.apache.zookeeper.ZooDefs.Ids;
Expand Down Expand Up @@ -165,6 +166,109 @@ public void processResult(int rc, String path, Object ctx) {
}
}

/**
* Send exists /zookeeper requests asynchronously, max 30 outstanding
*/
class HammerThreadExists extends Thread implements StatCallback {
private static final int MAX_OUTSTANDING = 30;

private TestableZooKeeper zk;
private int outstanding;

private volatile boolean failed = false;

public HammerThreadExists(String name) {
super(name);
}

public void run() {
try {
CountdownWatcher watcher = new CountdownWatcher();
zk = new TestableZooKeeper(qb.hostPort, CONNECTION_TIMEOUT,
watcher);
watcher.waitForConnected(CONNECTION_TIMEOUT);
while(bang) {
incOutstanding(); // before create otw race
zk.exists("/zookeeper", false, this, null);
}
} catch (InterruptedException e) {
if (bang) {
LOG.error("sanity check Assert.failed!!!"); // sanity check
return;
}
} catch (Exception e) {
LOG.error("Client create operation Assert.failed", e);
return;
} finally {
if (zk != null) {
try {
if (!zk.close(CONNECTION_TIMEOUT)) {
failed = true;
LOG.error("Client did not shutdown");
}
} catch (InterruptedException e) {
LOG.info("Interrupted", e);
}
}
}
}

private synchronized void incOutstanding() throws InterruptedException {
outstanding++;
while(outstanding > MAX_OUTSTANDING) {
wait();
}
}

private synchronized void decOutstanding() {
outstanding--;
Assert.assertTrue("outstanding >= 0", outstanding >= 0);
notifyAll();
}

public void process(WatchedEvent event) {
// ignore for purposes of this test
}

public void processResult(int rc, String path, Object ctx, Stat stat) {
if (rc != KeeperException.Code.OK.intValue()) {
if (bang) {
failed = true;
LOG.error("Exists Assert.failed for 0x"
+ Long.toHexString(zk.getSessionId())
+ "with rc:" + rc + " path:" + path);
}
decOutstanding();
return;
}
decOutstanding();
}
}

@Test
public void testExistsHammer() throws Exception {
System.setProperty("zookeeper.globalOutstandingLimit", "1");
setUp(false);
bang = true;
LOG.info("Starting hammers");
HammerThreadExists[] hammers = new HammerThreadExists[100];
for (int i = 0; i < hammers.length; i++) {
hammers[i] = new HammerThreadExists("HammerThread-" + i);
hammers[i].start();
}
LOG.info("Started hammers");
Thread.sleep(30000); // allow the clients to run for max 30sec
bang = false;
LOG.info("Stopping hammers");
for (int i = 0; i < hammers.length; i++) {
hammers[i].interrupt();
verifyThreadTerminated(hammers[i], 60000);
Assert.assertFalse(hammers[i].failed);
}
System.setProperty("zookeeper.globalOutstandingLimit", "1000");
tearDown();
}

@Test
public void testHammer() throws Exception {
setUp(false);
Expand Down

0 comments on commit e27f95a

Please sign in to comment.