public double getConnectionDropChance() { return connThrottle.getDropChance(); }
public int getConnectionMaxTokens() { return zks.connThrottle().getMaxTokens(); }
public void setConnectionDropIncrease(double val) { zks.connThrottle().setDropIncrease(val); }
int maxTokens = 5; BlueThrottle throttler = new BlueThrottleWithMockRandom(new MockRandom()); throttler.setMaxTokens(maxTokens); throttler.setFillCount(maxTokens); throttler.setFillTime(1000); throttler.setFreezeTime(100); throttler.setDropIncrease(0.5); throttler.checkLimit(1); Assert.assertEquals("All tokens should be used up by now", throttler.getMaxTokens(), throttler.getDeficit()); throttler.checkLimit(1); Assert.assertTrue("Dropping probability should be increased", throttler.getDropChance()>0); LOG.info("Dropping probability is {}", throttler.getDropChance()); if (throttler.checkLimit(1)) { accepted ++; if (throttler.checkLimit(1)) { accepted ++;
@Test public void testThrottleWithoutRandomDropping() throws InterruptedException { int maxTokens = 5; BlueThrottle throttler = new BlueThrottleWithMockRandom(new MockRandom()); throttler.setMaxTokens(maxTokens); throttler.setFillCount(maxTokens); throttler.setFillTime(1000); for (int i=0;i<maxTokens;i++) { throttler.checkLimit(1); } Assert.assertEquals("All tokens should be used up by now", throttler.getMaxTokens(), throttler.getDeficit()); Thread.sleep(110); throttler.checkLimit(1); Assert.assertFalse("Dropping probability should still be zero", throttler.getDropChance()>0); //allow bucket to be refilled Thread.sleep(1500); for (int i=0;i<maxTokens;i++) { Assert.assertTrue("The first " + maxTokens + " requests should be allowed", throttler.checkLimit(1)); } for (int i=0;i<maxTokens;i++) { Assert.assertFalse("The latter " + maxTokens + " requests should be denied", throttler.checkLimit(1)); } }
@Test public void testThrottleWithoutRefill() { BlueThrottle throttler = new BlueThrottle(); throttler.setMaxTokens(1); throttler.setFillTime(2000); Assert.assertTrue("First request should be allowed", throttler.checkLimit(1)); Assert.assertFalse("Second request should be denied", throttler.checkLimit(1)); }
@Test public void testThrottleDisabled() { BlueThrottle throttler = new BlueThrottle(); Assert.assertTrue("Throttle should be disabled by default", throttler.checkLimit(1)); }
public void processConnectRequest(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOException, ClientCnxnLimitException { if (connThrottle.checkLimit(1) == false) { throw new ClientCnxnLimitException(); ServerMetrics.CONNECTION_TOKEN_DEFICIT.add(connThrottle.getDeficit());
public void setConnectionTokenFillTime(int val) { zks.connThrottle().setFillTime(val); }
public void setConnectionMaxTokens(int val) { zks.connThrottle().setMaxTokens(val); }
public void setConnectionFreezeTime(int val) { zks.connThrottle().setFreezeTime(val); }
public void setConnectionTokenFillCount(int val) { zks.connThrottle().setFillCount(val); }
/** * Creates a ZooKeeperServer instance. It sets everything up, but doesn't * actually start listening for clients until run() is invoked. * * @param dataDir the directory to put the data */ public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, ZKDatabase zkDb) { serverStats = new ServerStats(this); this.txnLogFactory = txnLogFactory; this.txnLogFactory.setServerStats(this.serverStats); this.zkDb = zkDb; this.tickTime = tickTime; setMinSessionTimeout(minSessionTimeout); setMaxSessionTimeout(maxSessionTimeout); listener = new ZooKeeperServerListenerImpl(this); readResponseCache = new ResponseCache(); connThrottle = new BlueThrottle(); LOG.info("Created server with tickTime " + tickTime + " minSessionTimeout " + getMinSessionTimeout() + " maxSessionTimeout " + getMaxSessionTimeout() + " datadir " + txnLogFactory.getDataDir() + " snapdir " + txnLogFactory.getSnapDir()); }
@Test public void testThrottleWithRefill() throws InterruptedException { BlueThrottle throttler = new BlueThrottle(); throttler.setMaxTokens(1); throttler.setFillTime(500); Assert.assertTrue("First request should be allowed", throttler.checkLimit(1)); Assert.assertFalse("Second request should be denied", throttler.checkLimit(1)); //wait for the bucket to be refilled Thread.sleep(750); Assert.assertTrue("Third request should be allowed since we've got a new token", throttler.checkLimit(1)); }
@Override public double nextDouble() { if (throttle.getDropChance() > 0) { flag = 1 - flag; return flag; } else { return 1; } } }