Skip to content

Commit

Permalink
support stickySessionTime
Browse files Browse the repository at this point in the history
  • Loading branch information
junwen12221 committed May 18, 2022
1 parent a829442 commit 2b60170
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
32 changes: 28 additions & 4 deletions common/src/main/java/io/mycat/StickyDataSourceNearnessImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class StickyDataSourceNearnessImpl implements DataSourceNearness {
final DataSourceNearness dataSourceNearness;
long stickySessionTime;
boolean lastMaster = false;
long lastTime = System.currentTimeMillis();
long lastTime = 0;

public StickyDataSourceNearnessImpl(DataSourceNearness dataSourceNearness, long stickySessionTime) {
this.dataSourceNearness = dataSourceNearness;
Expand All @@ -13,6 +13,27 @@ public StickyDataSourceNearnessImpl(DataSourceNearness dataSourceNearness, long

@Override
public String getDataSourceByTargetName(String targetName, boolean master, ReplicaBalanceType replicaBalanceType) {
String dataSourceByTargetName;
if (isOpenStickySessionTime()) {
dataSourceByTargetName = stickSessionTimeMode(targetName, master, replicaBalanceType);
} else {
dataSourceByTargetName = stickSessionNearnessMode(targetName, master, replicaBalanceType);
}
lastMaster = master;
return dataSourceByTargetName;
}

private String stickSessionNearnessMode(String targetName, boolean master, ReplicaBalanceType replicaBalanceType) {
String dataSourceByTargetName;
if (this.lastMaster) {
dataSourceByTargetName = this.dataSourceNearness.getDataSourceByTargetName(targetName, true, replicaBalanceType);
} else {
dataSourceByTargetName = this.dataSourceNearness.getDataSourceByTargetName(targetName, master, replicaBalanceType);
}
return dataSourceByTargetName;
}

private String stickSessionTimeMode(String targetName, boolean master, ReplicaBalanceType replicaBalanceType) {
String dataSourceByTargetName;
if (master) {
dataSourceByTargetName = this.dataSourceNearness.getDataSourceByTargetName(targetName, true, replicaBalanceType);
Expand All @@ -21,15 +42,18 @@ public String getDataSourceByTargetName(String targetName, boolean master, Repli
if (lastMaster && isConsecutiveTime()) {
dataSourceByTargetName = this.dataSourceNearness.getDataSourceByTargetName(targetName, true, replicaBalanceType);
} else {
dataSourceByTargetName = this.dataSourceNearness.getDataSourceByTargetName(targetName, false, replicaBalanceType);
dataSourceByTargetName = this.dataSourceNearness.getDataSourceByTargetName(targetName, master, replicaBalanceType);
}
}
lastMaster = master;
return dataSourceByTargetName;
}

private boolean isConsecutiveTime() {
return (System.currentTimeMillis()- lastTime) < stickySessionTime;
return (System.currentTimeMillis() - lastTime) < stickySessionTime;
}

private boolean isOpenStickySessionTime() {
return stickySessionTime > 0;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion config/src/main/java/io/mycat/config/ServerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class ServerConfig {
private int rewriteInsertBatchedStatementBatch = 1000;
private boolean promoteUnsignedType = true;
private boolean stickySession = true;
private long stickySessionTime = 3;
private long stickySessionTime = 0;
public static void main(String[] args) {
System.out.println(JsonUtil.toJson(new ServerConfig()));
}
Expand Down

0 comments on commit 2b60170

Please sign in to comment.