Skip to content

Commit

Permalink
Modify the lock granularity used in statistics data store struct and …
Browse files Browse the repository at this point in the history
…improve coverage of code.
  • Loading branch information
xuri committed Nov 19, 2016
1 parent 888d5a9 commit a8be9a3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 67 deletions.
57 changes: 56 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,31 @@ func TestIndex(t *testing.T) {
if err != nil {
t.Log(err)
}
resp, err = http.PostForm(server+"/tube?server="+bstk+"&action=addSample",
url.Values{"tube": {"aurora_test"}, "addsamplejobid": {"1"}, "addsamplename": {""}, "tubes[aurora_test]": {"1"}})
if err != nil {
t.Log(err)
}
resp, err = http.PostForm(server+"/tube?server="+bstk+"&action=addSample",
url.Values{"tube": {"default"}, "addsamplejobid": {"1"}, "addsamplename": {"sample_1"}, "tubes[aurora_test]": {"1"}})
if err != nil {
t.Log(err)
}
resp, err = http.PostForm(server+"/tube?server=not_exist_server_addr&action=addSample",
url.Values{"tube": {"aurora_test"}, "addsamplejobid": {"1"}, "addsamplename": {"sample_2"}, "tubes[default]": {"1"}})
if err != nil {
t.Log(err)
}
resp, err = http.PostForm(server+"/tube?server="+bstk+"&action=addSample",
url.Values{"tube": {"aurora_test"}, "addsamplejobid": {""}, "addsamplename": {"sample_2"}, "tubes[aurora_test]": {"1"}})
if err != nil {
t.Log(err)
}
resp, err = http.PostForm(server+"/tube?server="+bstk+"&action=addSample",
url.Values{"tube": {"aurora_test"}, "addsamplejobid": {"not_int"}, "addsamplename": {"sample_2"}, "tubes[aurora_test]": {"1"}})
if err != nil {
t.Log(err)
}
for _, v := range urls {
req, err := http.NewRequest("GET", server+v, nil)
if err != nil {
Expand Down Expand Up @@ -305,7 +330,7 @@ func TestPrettyJSON(t *testing.T) {
func TestBase64Decode(t *testing.T) {
once.Do(testSetup)
base64Decode(`dGVzdA==`)
base64Decode(`test`)
base64Decode(`test-%?s`)
}

func TestRemoveServerInConfig(t *testing.T) {
Expand All @@ -327,10 +352,40 @@ func TestAddSampleTube(t *testing.T) {
once.Do(testSetup)
addSampleTube(`aurora_test_2`, `test`)
getSampleJobList()
getSampleJobNameByKey(`97ec882fd75855dfa1b4bd00d4a367d4`)
loadSample(``, `default`, `97ec882fd75855dfa1b4bd00d4a367d4`)
loadSample(bstk, `default`, `97ec882fd75855dfa1b4bd00d4a367d4`)
deleteSamples(`97ec882fd75855dfa1b4bd00d4a367d4`)
}

func TestBasicAuth(t *testing.T) {
once.Do(testSetup)
var err error
var req *http.Request
var client = &http.Client{}
pubConf.Auth.Enabled = true
http.HandleFunc("/test", basicAuth(handlerMain))
req, err = http.NewRequest("GET", server+"/test", nil)
if err != nil {
t.Log(err)
}
client = &http.Client{}
_, err = client.Do(req)
if err != nil {
t.Log(err)
}
req, err = http.NewRequest("GET", server+"/test", nil)
if err != nil {
t.Log(err)
}
req.SetBasicAuth(`admin`, `password`)
client = &http.Client{}
_, err = client.Do(req)
if err != nil {
t.Log(err)
}
}

func TestDeleteSamples(t *testing.T) {
once.Do(testSetup)
deleteSamples(``)
Expand Down
6 changes: 2 additions & 4 deletions statisticsUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func statistic() {
case <-notify:
break NOTIFY
case <-tick:
statisticsData.Lock()
for k, v := range statisticsData.Server {
for t := range v {
if selfConf.StatisticsCollection == 0 {
Expand All @@ -112,7 +111,6 @@ func statistic() {
}
}
}
statisticsData.Unlock()
}
}
}
Expand Down Expand Up @@ -141,6 +139,7 @@ func statisticAgent(server string, tube string) error {
if err != nil {
continue
}
statisticsData.Lock()
_, ok := statisticsData.Server[server][tube][k]
if !ok {
statisticsData.Server[server][tube][k] = list.New()
Expand All @@ -150,6 +149,7 @@ func statisticAgent(server string, tube string) error {
statisticsData.Server[server][tube][k].Remove(front)
}
statisticsData.Server[server][tube][k].PushFront([]int{t.Year(), int(t.Month()), t.Day(), t.Hour(), t.Minute(), t.Second(), stats})
statisticsData.Unlock()
}
}
bstkConn.Close()
Expand All @@ -160,7 +160,6 @@ func statisticAgent(server string, tube string) error {
func statisticWaitress(server string, tube string) string {
var buf, b, s, l bytes.Buffer
b.WriteString(`{`)
statisticsData.Lock()
for _, field := range statisticsFields {
for k := range field {
b.WriteString(`"`)
Expand All @@ -186,7 +185,6 @@ func statisticWaitress(server string, tube string) string {
b.WriteString(`],`)
}
}
statisticsData.Unlock()
buf.WriteString(strings.TrimSuffix(b.String(), `,`))
buf.WriteString(`}`)
return buf.String()
Expand Down
75 changes: 13 additions & 62 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,25 @@ const (
DefaultPriority = 1024 // most urgent: 0, least urgent: 4294967295.
DefaultTTR = 60 // 1 minute
DefaultTubePauseSeconds = 3600
TplHead = `<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!--[if IE]>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<![endif]-->
<meta name="description" content="Beanstalk Console">
<meta name="keywords" content="Beanstalk Console, beanstalk, console">
<meta content="always" name="referrer">
<meta name="language" content="en-US">
<meta name="category" content="Tools">
<meta name="summary" content="Beanstalk Console">
<meta name="apple-mobile-web-app-capable" content="yes"/>
<link rel="copyright" href="http://www.opensource.org/licenses/mit-license.php"/>
<link rel="icon" sizes="32x32" href="./images/aurora-32x32.ico">
<link rel="apple-touch-icon-precomposed" sizes="180x180" href="./images/apple-touch-icon-180x180-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="./images/apple-touch-icon-152x152-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="./images/apple-touch-icon-144x144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="./images/apple-touch-icon-120x120-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="./images/apple-touch-icon-114x114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="76x76" href="./images/apple-touch-icon-76x76-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="./images/apple-touch-icon-72x72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="./images/apple-touch-icon-precomposed-57x57.png">
<title>Beanstalk Console</title>
<!-- Bootstrap core CSS -->
<link href="./assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="./css/customer.css" rel="stylesheet">
<link href="./highlight/styles/magula.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="./js/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="./js/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>`
TplLinks = `<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown"> Links <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="https://github.com/kr/beanstalkd">Beanstalk (GitHub)</a></li>
<li><a href="https://github.com/Luxurioust/aurora">Aurora (GitHub)</a></li>
</ul>
</li>`
TplNoScript = `<noscript>
<div class="container">
<div class="alert alert-danger" role="alert">
Aurora beanstalk console requires JavaScript supports, please refresh after enable browser JavaScript support.
</div>
</div>
</noscript>`
UpdateURL = `https://api.github.com/repos/Luxurioust/aurora/tags`
Version = 1.5
TplHead = `<head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><![endif]--><meta name="description" content="Beanstalk Console"><meta name="keywords" content="Beanstalk Console, beanstalk, console"><meta content="always" name="referrer"><meta name="language" content="en-US"><meta name="category" content="Tools"><meta name="summary" content="Beanstalk Console"><meta name="apple-mobile-web-app-capable" content="yes"/><link rel="copyright" href="http://www.opensource.org/licenses/mit-license.php"/><link rel="icon" sizes="32x32" href="./images/aurora-32x32.ico"><link rel="apple-touch-icon-precomposed" sizes="180x180" href="./images/apple-touch-icon-180x180-precomposed.png"><link rel="apple-touch-icon-precomposed" sizes="152x152" href="./images/apple-touch-icon-152x152-precomposed.png"><link rel="apple-touch-icon-precomposed" sizes="144x144" href="./images/apple-touch-icon-144x144-precomposed.png"><link rel="apple-touch-icon-precomposed" sizes="120x120" href="./images/apple-touch-icon-120x120-precomposed.png"><link rel="apple-touch-icon-precomposed" sizes="114x114" href="./images/apple-touch-icon-114x114-precomposed.png"><link rel="apple-touch-icon-precomposed" sizes="76x76" href="./images/apple-touch-icon-76x76-precomposed.png"><link rel="apple-touch-icon-precomposed" sizes="72x72" href="./images/apple-touch-icon-72x72-precomposed.png"><link rel="apple-touch-icon-precomposed" href="./images/apple-touch-icon-precomposed-57x57.png"><title>Beanstalk Console</title><!-- Bootstrap core CSS --><link href="./assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet"><link href="./css/customer.css" rel="stylesheet"><link href="./highlight/styles/magula.css" rel="stylesheet"><!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --><!--[if lt IE 9]><script src="./js/libs/html5shiv/3.7.0/html5shiv.js"></script><script src="./js/libs/respond.js/1.4.2/respond.min.js"></script><![endif]--></head>`
TplLinks = `<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown"> Links <span class="caret"></span></a><ul class="dropdown-menu"><li><a href="https://github.com/kr/beanstalkd">Beanstalk (GitHub)</a></li><li><a href="https://github.com/Luxurioust/aurora">Aurora (GitHub)</a></li></ul></li>`
TplNoScript = `<noscript><div class="container"><div class="alert alert-danger" role="alert">Aurora beanstalk console requires JavaScript supports, please refresh after enable browser JavaScript support.</div></div></noscript>`
UpdateURL = `https://api.github.com/repos/Luxurioust/aurora/tags`
Version = 1.5
)

// Define server and tube stats fields.
var (
ConfigFile = `.` + string(os.PathSeparator) + `aurora.toml`
// Stdout is the io.Writer to which executed commands write standard output.
Stdout io.Writer = os.Stdout
// Stderr is the io.Writer to which executed commands write standard error.
Stderr io.Writer = os.Stderr
pubConf PubConfig
selfConf SelfConf
sampleJobs SampleJobs
statisticsDataServer = make(map[string]map[string]map[string]*list.List)
statisticsData = StatisticsData{
new(sync.RWMutex),
statisticsDataServer,
}
notify = make(chan bool, 1)
updateInfo = "uncheck"
selfConf SelfConf
Stderr io.Writer = os.Stderr // Stderr is the io.Writer to which executed commands write standard error.
Stdout io.Writer = os.Stdout // Stdout is the io.Writer to which executed commands write standard output.
ConfigFile = `.` + string(os.PathSeparator) + `aurora.toml`
statisticsData = StatisticsData{new(sync.RWMutex), statisticsDataServer}
statisticsDataServer = make(map[string]map[string]map[string]*list.List)
notify = make(chan bool, 1)
updateInfo = "uncheck"
// Server filter columns.
binlogStatsGroups = []map[string]string{
map[string]string{"binlog-current-index": "the index of the current binlog file being written to. If binlog is not active this value will be 0"},
Expand Down

0 comments on commit a8be9a3

Please sign in to comment.