Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Jie Huang committed Jan 22, 2019
1 parent 86cad39 commit c3ec81f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,65 @@ public class BlueThrottle {

Random rng;

public static final String CONNECTION_THROTTLE_TOKENS = "zookeeper.connection_throttle_tokens";
public static final int DEFAULT_CONNECTION_THROTTLE_TOKENS;

public static final String CONNECTION_THROTTLE_FILL_TIME = "zookeeper.connection_throttle_fill_time";
public static final int DEFAULT_CONNECTION_THROTTLE_FILL_TIME;

public static final String CONNECTION_THROTTLE_FILL_COUNT = "zookeeper.connection_throttle_fill_count";
public static final int DEFAULT_CONNECTION_THROTTLE_FILL_COUNT;

public static final String CONNECTION_THROTTLE_FREEZE_TIME = "zookeeper.connection_throttle_freeze_time";
public static final int DEFAULT_CONNECTION_THROTTLE_FREEZE_TIME;

public static final String CONNECTION_THROTTLE_DROP_INCREASE = "zookeeper.connection_throttle_drop_increase";
public static final double DEFAULT_CONNECTION_THROTTLE_DROP_INCREASE;

public static final String CONNECTION_THROTTLE_DROP_DECREASE = "zookeeper.connection_throttle_drop_decrease";
public static final double DEFAULT_CONNECTION_THROTTLE_DROP_DECREASE;

public static final String CONNECTION_THROTTLE_DECREASE_RATIO = "zookeeper.connection_throttle_decrease_ratio";
public static final double DEFAULT_CONNECTION_THROTTLE_DECREASE_RATIO;


static {
DEFAULT_CONNECTION_THROTTLE_TOKENS = Integer.getInteger(CONNECTION_THROTTLE_TOKENS, 0);
DEFAULT_CONNECTION_THROTTLE_FILL_TIME = Integer.getInteger(CONNECTION_THROTTLE_FILL_TIME, 1);
DEFAULT_CONNECTION_THROTTLE_FILL_COUNT = Integer.getInteger(CONNECTION_THROTTLE_FILL_COUNT, 1);

DEFAULT_CONNECTION_THROTTLE_FREEZE_TIME = Integer.getInteger(CONNECTION_THROTTLE_FREEZE_TIME, -1);
DEFAULT_CONNECTION_THROTTLE_DROP_INCREASE = getDoubleProp(CONNECTION_THROTTLE_DROP_INCREASE, 0.02);
DEFAULT_CONNECTION_THROTTLE_DROP_DECREASE = getDoubleProp(CONNECTION_THROTTLE_DROP_DECREASE, 0.002);
DEFAULT_CONNECTION_THROTTLE_DECREASE_RATIO = getDoubleProp(CONNECTION_THROTTLE_DECREASE_RATIO, 0);
}

/* Varation of Integer.getInteger for real number properties */
private static double getDoubleProp(String name, double def) {
String val = System.getProperty(name);
if(val != null) {
return Double.parseDouble(val);
}
else {
return def;
}
}


public BlueThrottle() {
// Disable throttling by default (maxTokens = 0)
this.maxTokens = 0;
this.fillTime = 1;
this.fillCount = 1;
this.maxTokens = DEFAULT_CONNECTION_THROTTLE_TOKENS;
this.fillTime = DEFAULT_CONNECTION_THROTTLE_FILL_TIME;
this.fillCount = DEFAULT_CONNECTION_THROTTLE_FILL_COUNT;
this.tokens = maxTokens;
this.lastTime = Time.currentElapsedTime();

// Disable BLUE throttling by default (freezeTime = -1)
this.freezeTime = -1;
this.freezeTime = DEFAULT_CONNECTION_THROTTLE_FREEZE_TIME;
this.lastFreeze = Time.currentElapsedTime();
this.dropIncrease = 0.02;
this.dropDecrease = 0.002;
this.decreasePoint = 0;
this.dropIncrease = DEFAULT_CONNECTION_THROTTLE_DROP_INCREASE;
this.dropDecrease = DEFAULT_CONNECTION_THROTTLE_DROP_DECREASE;
this.decreasePoint = DEFAULT_CONNECTION_THROTTLE_DECREASE_RATIO;
this.drop = 0;

this.rng = new Random();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,27 +203,6 @@ public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime,
readResponseCache = new ResponseCache();

connThrottle = new BlueThrottle();
connThrottle.setMaxTokens(
Integer.getInteger("zookeeper.connection_throttle_tokens", 0)
);
connThrottle.setFillTime(
Integer.getInteger("zookeeper.connection_throttle_fill_time", 1)
);
connThrottle.setFillCount(
Integer.getInteger("zookeeper.connection_throttle_fill_count", 1)
);
connThrottle.setFreezeTime(
Integer.getInteger("zookeeper.connection_throttle_freeze_time", -1)
);
connThrottle.setDropIncrease(
getDoubleProp("zookeeper.connection_throttle_drop_increase", 0.02)
);
connThrottle.setDropDecrease(
getDoubleProp("zookeeper.connection_throttle_drop_decrease", 0.002)
);
connThrottle.setDecreasePoint(
getDoubleProp("zookeeper.connection_throttle_decrease_ratio", 0)
);

LOG.info("Created server with tickTime " + tickTime
+ " minSessionTimeout " + getMinSessionTimeout()
Expand All @@ -232,17 +211,6 @@ public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime,
+ " snapdir " + txnLogFactory.getSnapDir());
}

/* Varation of Integer.getInteger for real number properties */
double getDoubleProp(String name, double def) {
String val = System.getProperty(name);
if(val != null) {
return Double.parseDouble(val);
}
else {
return def;
}
}

/**
* creates a zookeeperserver instance.
* @param txnLogFactory the file transaction snapshot logging class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ class MockRandom extends Random {
int flag = 0;
BlueThrottle throttle;

public MockRandom(BlueThrottle bt) {
this.throttle = bt;
}

@Override
public double nextDouble() {
if (throttle.getDropChance() > 0) {
Expand All @@ -49,9 +45,10 @@ public double nextDouble() {
}

class BlueThrottleWithMockRandom extends BlueThrottle {
public BlueThrottleWithMockRandom() {
public BlueThrottleWithMockRandom(MockRandom random) {
super();
this.rng = new MockRandom(this);
this.rng = random;
random.throttle = this;
}
}

Expand Down Expand Up @@ -86,7 +83,7 @@ public void testThrottleWithRefill() throws InterruptedException {
@Test
public void testThrottleWithoutRandomDropping() throws InterruptedException {
int maxTokens = 5;
BlueThrottle throttler = new BlueThrottleWithMockRandom();
BlueThrottle throttler = new BlueThrottleWithMockRandom(new MockRandom());
throttler.setMaxTokens(maxTokens);
throttler.setFillCount(maxTokens);
throttler.setFillTime(1000);
Expand Down Expand Up @@ -115,7 +112,7 @@ public void testThrottleWithoutRandomDropping() throws InterruptedException {
@Test
public void testThrottleWithRandomDropping() throws InterruptedException {
int maxTokens = 5;
BlueThrottle throttler = new BlueThrottleWithMockRandom();
BlueThrottle throttler = new BlueThrottleWithMockRandom(new MockRandom());
throttler.setMaxTokens(maxTokens);
throttler.setFillCount(maxTokens);
throttler.setFillTime(1000);
Expand Down Expand Up @@ -157,5 +154,4 @@ public void testThrottleWithRandomDropping() throws InterruptedException {
LOG.info("Send another {} requests, {} are accepted", maxTokens, accepted);
Assert.assertTrue("Later requests should have a chance", accepted > 0);
}

}

0 comments on commit c3ec81f

Please sign in to comment.