Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
e59bc45
test
Jun 21, 2020
d18e8de
test
Jun 23, 2020
b664d03
revert
mastertiller Jun 23, 2020
518ad5b
Optimize Code Structure
mastertiller Jul 10, 2020
5e75a8c
test for timer
mastertiller Aug 5, 2020
6b636a5
test for timer
mastertiller Aug 5, 2020
fd2272b
test for timer and counter
mastertiller Aug 5, 2020
6dd6afa
test for timer and counter
mastertiller Aug 5, 2020
d943508
test for timer and counter
mastertiller Aug 5, 2020
781a692
test for timer and counter
mastertiller Aug 5, 2020
c5404cd
test for timer and counter
mastertiller Aug 5, 2020
3a3c4fb
test for learning
mastertiller Aug 7, 2020
25bfe03
train User Peak demo
mastertiller Aug 10, 2020
bdd4c19
train User Peak demo
mastertiller Aug 10, 2020
0d5bf52
check FlowThreadDemo
mastertiller Aug 10, 2020
b885d9c
Construct q learning algorithm.
mastertiller Aug 10, 2020
ca50e9d
Optimize code structure.
mastertiller Aug 16, 2020
bda7440
delete unnecessary files.
mastertiller Aug 16, 2020
5773bcc
delete unuse function
mastertiller Aug 16, 2020
dcdafeb
revert necessary files
mastertiller Aug 16, 2020
f5077fa
delete unnecessary folders.
mastertiller Aug 16, 2020
af38db7
optimize code
mastertiller Aug 16, 2020
843d572
test for timer
mastertiller Aug 5, 2020
960c80d
test for timer
mastertiller Aug 5, 2020
0fd15e6
test for timer and counter
mastertiller Aug 5, 2020
3d17efe
test for timer and counter
mastertiller Aug 5, 2020
3662329
test for timer and counter
mastertiller Aug 5, 2020
eba0e3c
test for timer and counter
mastertiller Aug 5, 2020
99c50c2
test for timer and counter
mastertiller Aug 5, 2020
42a1545
test for learning
mastertiller Aug 7, 2020
22f0910
train User Peak demo
mastertiller Aug 10, 2020
1e819b2
train User Peak demo
mastertiller Aug 10, 2020
ec1da3c
check FlowThreadDemo
mastertiller Aug 10, 2020
f4a7b07
Construct q learning algorithm.
mastertiller Aug 10, 2020
0aa3f41
Optimize code structure.
mastertiller Aug 16, 2020
3352573
delete unnecessary files.
mastertiller Aug 16, 2020
4c85368
revert necessary files
mastertiller Aug 16, 2020
3567eda
delete unnecessary folders.
mastertiller Aug 16, 2020
7fad7dd
optimize code
mastertiller Aug 16, 2020
7e6851c
delete unuse function
mastertiller Aug 16, 2020
a4eed31
optimize the structure.
mastertiller Aug 17, 2020
7a902b7
Merge remote-tracking branch 'origin/QLearning' into QLearning
mastertiller Aug 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.qlearning;

import java.util.concurrent.atomic.AtomicInteger;

/**
* @author ZhouYanjun
*/
public class QInfo {

String state;
AtomicInteger action = new AtomicInteger(0);
volatile double utility;

public String getState() {
return this.state;
}

public void setState(String state) {
this.state = state;
}

public int getAction() {
return action.get();
}

public void setAction(int action) {
this.action.set(action);
}

public double getUtility() {
return utility;
}

public synchronized void setQInfo(String state, int action, double utility) {
this.state = state;
this.action.set(action);
this.utility = utility;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.qlearning;

import com.alibaba.csp.sentinel.Constants;
import com.alibaba.csp.sentinel.node.DefaultNode;
import com.alibaba.csp.sentinel.slotchain.ResourceWrapper;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.csp.sentinel.util.TimeUtil;

/**
* @author ZhouYanjun
*/
public class QLearningLearner {
QLearningMetric qLearningMetric = new QLearningMetric().getInstance();
private int bacthNum = 200;
private long batchTime = 20;

public synchronized void learn(ResourceWrapper resourceWrapper, DefaultNode node) throws SystemBlockException {
if (checkUpdate()) {
if (containsQInfo() && qLearningMetric.isTrain()) {
UpdateQ(node);
}
int bi = qLearningMetric.addBi();
QInfo qInfo = takeAction(node);
qLearningMetric.putHm(bi, qInfo);

}
if (qLearningMetric.getAction() == 0) {
throw new SystemBlockException(resourceWrapper.getName(), "q-learning");
}
}

private boolean checkUpdate() {
long ct = qLearningMetric.getCt();
long t = TimeUtil.currentTimeMillis();

qLearningMetric.addCn();

if (ct <= 0) {
qLearningMetric.setCt(TimeUtil.currentTimeMillis());

return true;
}
if (t - ct >= batchTime || qLearningMetric.getCn() >= bacthNum) {
qLearningMetric.setCt(TimeUtil.currentTimeMillis());
qLearningMetric.resetCn();

return true;
}

return false;
}

private boolean containsQInfo() {
int bi = qLearningMetric.getBi();

if (bi == 0) {
return false;
}

if (qLearningMetric.getHm().containsKey(bi)) {
return true;
}

return false;
}

private synchronized void UpdateQ(DefaultNode node) {

QInfo qInfo = qLearningMetric.pushQInfo();
String s = qInfo.getState();
int a = qInfo.getAction();
double u = qInfo.getUtility();

double nextUtility = qLearningMetric.calculateUtility(node.successQps(), node.avgRt());

int r = qLearningMetric.getReward(u, nextUtility);
double q = qLearningMetric.getQValue(s, a);
double maxQ = qLearningMetric.getMaxQ(SystemRuleManager.getCurrentCpuUsage(), node.passQps(), node.avgRt(), 0);
double qUpdated = qLearningMetric.updateQ(q, r, maxQ);

qLearningMetric.setQValue(s, a, qUpdated);
}

private synchronized QInfo takeAction(DefaultNode node) {

String state = qLearningMetric.locateState(SystemRuleManager.getCurrentCpuUsage(), node.passQps(), node.avgRt(), 0);
int action;
if (qLearningMetric.isTrain()) {
//随机选择
action = qLearningMetric.getRandomAction();
} else {
//从qtable中选择
action = qLearningMetric.policy(state);
}
qLearningMetric.setAction(action);

double utility = qLearningMetric.calculateUtility(node.successQps(), node.avgRt());
QInfo qInfo = new QInfo();
qInfo.setQInfo(state, action, utility);

return qInfo;
}


}
Loading