Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine migrate #876

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6cf3aff
run test case in docker
laiwei Nov 20, 2018
9d3d33b
fix env in .travis-ci config
laiwei Nov 20, 2018
3089ce3
add api doc for update_specific_user
laiwei Apr 14, 2019
37c2079
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei Apr 30, 2019
f8ab1d1
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei Apr 30, 2019
0d3360d
code fmt check
laiwei Apr 30, 2019
ac6ba7b
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei Apr 30, 2019
cbbbde2
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei Apr 30, 2019
d5133e8
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei Apr 30, 2019
fad16ef
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei May 13, 2019
7ba08bd
use local time zone in mysql config
laiwei May 13, 2019
5c76102
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei May 13, 2019
9a54fda
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei May 15, 2019
94cfa5b
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei May 29, 2019
b413e30
v0.3, bugfix and enhancement
laiwei May 30, 2019
7cb4d9b
Merge branch 'master' of github.com:laiwei/falcon-plus
laiwei Jul 10, 2019
f5bb85c
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei Oct 8, 2019
4be9d31
make modules version more consistent
laiwei Oct 15, 2019
75121a7
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei Oct 15, 2019
41815f9
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei Nov 14, 2019
80f621d
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei Dec 13, 2019
a768010
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei Dec 24, 2019
163ca7c
Merge branch 'master' of github.com:open-falcon/falcon-plus
laiwei Dec 27, 2019
feb3072
refine migrate
laiwei Feb 14, 2020
ae99a75
update vendor rrdlite
laiwei Feb 14, 2020
cc1922a
fix typo
laiwei Apr 10, 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
66 changes: 48 additions & 18 deletions modules/api/test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/masato25/resty"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -555,31 +556,60 @@ func TestGraph(t *testing.T) {

rc := resty.New()
rc.SetHeader("Apitoken", api_token)
var rr *[]map[string]interface{} = &[]map[string]interface{}{}

Convey("Get endpoint list: GET /graph/endpoint", t, func() {
r := []map[string]interface{}{}
resp, _ := rc.R().SetQueryParam("q", ".+").
SetResult(&r).
SetResult(rr).
Get(fmt.Sprintf("%s/graph/endpoint", api_v1))
So(resp.StatusCode(), ShouldEqual, 200)
So(len(r), ShouldBeGreaterThanOrEqualTo, 0)
So(len(*rr), ShouldBeGreaterThanOrEqualTo, 0)

if len(r) == 0 {
return
}
})

eid := r[0]["id"]
r = []map[string]interface{}{}
Convey("Get counter list: GET /graph/endpoint_counter", func() {
resp, _ := rc.R().
SetQueryParam("eid", fmt.Sprintf("%v", eid)).
SetQueryParam("metricQuery", ".+").
SetQueryParam("limit", "1").
SetResult(&r).
Get(fmt.Sprintf("%s/graph/endpoint_counter", api_v1))
So(resp.StatusCode(), ShouldEqual, 200)
So(r, ShouldNotBeEmpty)
})
if len(*rr) == 0 {
return
}

eid := (*rr)[0]["id"]
endpoint := (*rr)[0]["endpoint"]
Convey("Get counter list: GET /graph/endpoint_counter", t, func() {
resp, _ := rc.R().
SetQueryParam("eid", fmt.Sprintf("%v", eid)).
SetQueryParam("metricQuery", ".+").
SetQueryParam("limit", "1").
SetResult(rr).
Get(fmt.Sprintf("%s/graph/endpoint_counter", api_v1))
So(resp.StatusCode(), ShouldEqual, 200)
So(*rr, ShouldNotBeEmpty)
})

if len(*rr) == 0 {
return
}

counter := (*rr)[0]["counter"]
step := (*rr)[0]["step"]

now := time.Now()
start_ts := now.Add(time.Duration(-1) * time.Hour).Unix()
end_ts := now.Unix()

Convey("Query counter history: POST /graph/history", t, func() {
resp, _ := rc.R().
SetBody(map[string]interface{}{
"step": step,
"consol_fun": "AVERAGE",
"start_time": start_ts,
"end_time": end_ts,
"hostnames": []string{endpoint.(string)},
"counters": []string{counter.(string)},
}).
SetResult(rr).
Post(fmt.Sprintf("%s/graph/history", api_v1))
log.Info(resp)
So(resp.StatusCode(), ShouldEqual, 200)
So(*rr, ShouldNotBeEmpty)
})
}

Expand Down
8 changes: 6 additions & 2 deletions modules/graph/api/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (this *Graph) GetRrd(key string, rrdfile *g.File) (err error) {

items := store.GraphItems.PopAll(key)
if len(items) > 0 {
rrdtool.FlushFile(rrdfile.Filename, md5, items)
rrdtool.CommitFile(rrdfile.Filename, md5, items)
}

rrdfile.Body, err = rrdtool.ReadFile(rrdfile.Filename, md5)
Expand Down Expand Up @@ -178,7 +178,11 @@ func (this *Graph) Query(param cmodel.GraphQueryParam, resp *cmodel.GraphQueryRe
// read data from rrd file
// 从RRD中获取数据不包含起始时间点
// 例: start_ts=1484651400,step=60,则第一个数据时间为1484651460)
datas, _ = rrdtool.Fetch(filename, md5, param.ConsolFun, start_ts-int64(step), end_ts, step)
var err error
datas, err = rrdtool.Fetch(filename, md5, param.ConsolFun, start_ts-int64(step), end_ts, step)
if err != nil {
log.Debugf("rrdtool fetch %s error: %v", filename, err)
}
datas_size = len(datas)
}

Expand Down
8 changes: 4 additions & 4 deletions modules/graph/api/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package api

import (
"container/list"
"log"
log "github.com/sirupsen/logrus"
"net"
"net/rpc"
"sync"
Expand Down Expand Up @@ -52,7 +52,7 @@ func init() {

func Start() {
if !g.Config().Rpc.Enabled {
log.Println("rpc.Start warning, not enabled")
log.Info("rpc.Start warning, not enabled")
return
}
addr := g.Config().Rpc.Listen
Expand All @@ -65,7 +65,7 @@ func Start() {
if err != nil {
log.Fatalf("rpc.Start error, listen %s failed, %s", addr, err)
} else {
log.Println("rpc.Start ok, listening on", addr)
log.Info("rpc.Start ok, listening on", addr)
}

rpc.Register(new(Graph))
Expand Down Expand Up @@ -99,7 +99,7 @@ func Start() {

select {
case <-Close_chan:
log.Println("rpc, recv sigout and exiting...")
log.Info("rpc, recv sigout and exiting...")
listener.Close()
Close_done_chan <- 1

Expand Down
6 changes: 3 additions & 3 deletions modules/graph/cron/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
package cron

import (
"log"
log "github.com/sirupsen/logrus"
"strings"
"time"

Expand Down Expand Up @@ -85,7 +85,7 @@ func DeleteInvalidItems() int {

pfc.Gauge("GraphItemsCacheCnt", int64(currentCnt))
pfc.Gauge("GraphItemsCacheInvalidCnt", int64(deleteCnt))
log.Printf("GraphItemsCache: Count=>%d, DeleteInvalid=>%d", currentCnt, deleteCnt)
log.Infof("GraphItemsCache: Count=>%d, DeleteInvalid=>%d", currentCnt, deleteCnt)

return deleteCnt
}
Expand All @@ -107,7 +107,7 @@ func DeleteInvalidHistory() int {

pfc.Gauge("HistoryCacheCnt", int64(currentCnt))
pfc.Gauge("HistoryCacheInvalidCnt", int64(deleteCnt))
log.Printf("HistoryCache: Count=>%d, DeleteInvalid=>%d", currentCnt, deleteCnt)
log.Infof("HistoryCache: Count=>%d, DeleteInvalid=>%d", currentCnt, deleteCnt)

return deleteCnt
}
12 changes: 6 additions & 6 deletions modules/graph/g/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package g

import (
"encoding/json"
"log"
log "github.com/sirupsen/logrus"
"strconv"
"sync/atomic"
"unsafe"
Expand Down Expand Up @@ -77,24 +77,24 @@ func Config() *GlobalConfig {

func ParseConfig(cfg string) {
if cfg == "" {
log.Fatalln("config file not specified: use -c $filename")
log.Fatal("config file not specified: use -c $filename")
}

if !file.IsExist(cfg) {
log.Fatalln("config file specified not found:", cfg)
log.Fatal("config file specified not found:", cfg)
}

ConfigFile = cfg

configContent, err := file.ToTrimString(cfg)
if err != nil {
log.Fatalln("read config file", cfg, "error:", err.Error())
log.Fatal("read config file", cfg, "error:", err.Error())
}

var c GlobalConfig
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
log.Fatalln("parse config file", cfg, "error:", err.Error())
log.Fatal("parse config file", cfg, "error:", err.Error())
}

if c.Migrate.Enabled && len(c.Migrate.Cluster) == 0 {
Expand All @@ -112,5 +112,5 @@ func ParseConfig(cfg string) {
// set config
atomic.StorePointer(&ptr, unsafe.Pointer(&c))

log.Println("g.ParseConfig ok, file", cfg)
log.Info("g.ParseConfig ok, file", cfg)
}
6 changes: 3 additions & 3 deletions modules/graph/g/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package g
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"log"
log "github.com/sirupsen/logrus"
"sync"
)

Expand All @@ -33,11 +33,11 @@ func InitDB() {
var err error
DB, err = makeDbConn()
if DB == nil || err != nil {
log.Fatalln("g.InitDB, get db conn fail", err)
log.Fatal("g.InitDB, get db conn fail", err)
}

dbConnMap = make(map[string]*sql.DB)
log.Println("g.InitDB ok")
log.Info("g.InitDB ok")
}

func GetDbConn(connName string) (c *sql.DB, e error) {
Expand Down
3 changes: 0 additions & 3 deletions modules/graph/g/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package g

import (
"log"
"runtime"
)

Expand Down Expand Up @@ -55,7 +54,6 @@ const (
DEFAULT_STEP = 60 //s
MIN_STEP = 30 //s
CLEAN_CACHE = 86400 //s the step that clean GraphItems/historyCache Cache
CACHE_DELAY = 1800 //s
CACHE_TIME = 1800000 //ms
FLUSH_DISK_STEP = 1000 //ms
FLUSH_MIN_COUNT = 6 // flush counter to disk when its number of monitoring data greater than FLUSH_MIN_COUNT
Expand All @@ -71,5 +69,4 @@ const (

func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
17 changes: 17 additions & 0 deletions modules/graph/http/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ package http

import (
"github.com/open-falcon/falcon-plus/modules/graph/g"
"github.com/open-falcon/falcon-plus/modules/graph/rrdtool"
"github.com/open-falcon/falcon-plus/modules/graph/store"
"github.com/toolkits/file"

"fmt"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"strconv"
"time"
)
Expand Down Expand Up @@ -73,4 +76,18 @@ func configCommonRoutes() {
}
JSONR(c, 200, rt)
})

router.GET("/api/v2/counter/migrate", func(c *gin.Context) {
counter := rrdtool.GetCounterV2()
log.Debug("migrating counter v2:", fmt.Sprintf("%+v", counter))
c.JSON(200, counter)
})

//compatible with open-falcon v0.1
router.GET("/counter/migrate", func(c *gin.Context) {
cnt := rrdtool.GetCounter()
log.Debug("migrating counter:", cnt)
c.JSON(200, gin.H{"msg": "ok", "counter": cnt})
})

}
15 changes: 0 additions & 15 deletions modules/graph/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package http

import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"net"
"net/http"
Expand All @@ -25,7 +24,6 @@ import (

"github.com/gin-gonic/gin"
"github.com/open-falcon/falcon-plus/modules/graph/g"
"github.com/open-falcon/falcon-plus/modules/graph/rrdtool"
)

type Dto struct {
Expand Down Expand Up @@ -102,19 +100,6 @@ func Start() {
configProcRoutes()
configIndexRoutes()

router.GET("/api/v2/counter/migrate", func(c *gin.Context) {
counter := rrdtool.GetCounterV2()
log.Debug("migrating counter v2:", fmt.Sprintf("%+v", counter))
c.JSON(200, counter)
})

//compatible with open-falcon v0.1
router.GET("/counter/migrate", func(c *gin.Context) {
cnt := rrdtool.GetCounter()
log.Debug("migrating counter:", cnt)
c.JSON(200, gin.H{"msg": "ok", "counter": cnt})
})

addr := g.Config().Http.Listen
if addr == "" {
return
Expand Down
6 changes: 3 additions & 3 deletions modules/graph/index/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package index
import (
"database/sql"
"fmt"
"log"
log "github.com/sirupsen/logrus"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -97,7 +97,7 @@ func GetEndpointFromCache(endpoint string) (int64, bool) {
var id int64 = -1
err := g.DB.QueryRow("SELECT id FROM endpoint WHERE endpoint = ?", endpoint).Scan(&id)
if err != nil && err != sql.ErrNoRows {
log.Println("query endpoint id fail,", err)
log.Error("query endpoint id fail,", err)
return -1, false
}

Expand Down Expand Up @@ -132,7 +132,7 @@ func GetCounterFromCache(endpointId int64, counter string) (dsType string, step
err = g.DB.QueryRow("SELECT type, step FROM endpoint_counter WHERE endpoint_id = ? and counter = ?",
endpointId, counter).Scan(&dsType, &step)
if err != nil && err != sql.ErrNoRows {
log.Println("query type and step fail", err)
log.Error("query type and step fail", err)
return
}

Expand Down
Loading