Skip to content

Commit

Permalink
optimize code style
Browse files Browse the repository at this point in the history
  • Loading branch information
chengshiwen committed Aug 13, 2024
1 parent 9b77e67 commit d59c447
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 84 deletions.
8 changes: 4 additions & 4 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ func (ib *Backend) GetHealth(ic *Circle, withStats bool) interface{} {
go func(db string) {
defer wg.Done()
inplace, incorrect := 0, 0
measurements := ib.GetMeasurements(db)
for _, meas := range measurements {
key := ic.getKeyFn(db, meas)
mms := ib.GetMeasurements(db)
for _, mm := range mms {
key := ic.getKeyFn(db, mm)
nb := ic.GetBackend(key)
if nb.Url == ib.Url {
inplace++
Expand All @@ -349,7 +349,7 @@ func (ib *Backend) GetHealth(ic *Circle, withStats bool) interface{} {
}
}
smap.Store(db, map[string]int{
"measurements": len(measurements),
"measurements": len(mms),
"inplace": inplace,
"incorrect": incorrect,
})
Expand Down
24 changes: 12 additions & 12 deletions backend/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func query(w http.ResponseWriter, req *http.Request, ip *Proxy, key string, fn f
return nil, ErrBackendsUnavailable
}

func ReadProm(w http.ResponseWriter, req *http.Request, ip *Proxy, db, meas string) (err error) {
// all circles -> backend by key(db,meas) -> select or show
key := ip.GetKey(db, meas)
func ReadProm(w http.ResponseWriter, req *http.Request, ip *Proxy, db, mm string) (err error) {
// all circles -> backend by key(db,mm) -> select or show
key := ip.GetKey(db, mm)
fn := func(be *Backend, req *http.Request, w http.ResponseWriter) ([]byte, error) {
err = be.ReadProm(req, w)
return nil, err
Expand All @@ -70,9 +70,9 @@ func ReadProm(w http.ResponseWriter, req *http.Request, ip *Proxy, db, meas stri
return
}

func QueryFlux(w http.ResponseWriter, req *http.Request, ip *Proxy, bucket, meas string) (err error) {
// all circles -> backend by key(org,bucket,meas) -> query flux
key := ip.GetKey(bucket, meas)
func QueryFlux(w http.ResponseWriter, req *http.Request, ip *Proxy, bucket, measurement string) (err error) {
// all circles -> backend by key(bucket,measurement) -> query flux
key := ip.GetKey(bucket, measurement)
fn := func(be *Backend, req *http.Request, w http.ResponseWriter) ([]byte, error) {
err = be.QueryFlux(req, w)
return nil, err
Expand All @@ -82,12 +82,12 @@ func QueryFlux(w http.ResponseWriter, req *http.Request, ip *Proxy, bucket, meas
}

func QueryFromQL(w http.ResponseWriter, req *http.Request, ip *Proxy, tokens []string, db string) (body []byte, err error) {
// all circles -> backend by key(db,meas) -> select or show
meas, err := GetMeasurementFromTokens(tokens)
// all circles -> backend by key(db,mm) -> select or show
mm, err := GetMeasurementFromTokens(tokens)
if err != nil {
return nil, ErrGetMeasurement
}
key := ip.GetKey(db, meas)
key := ip.GetKey(db, mm)
fn := func(be *Backend, req *http.Request, w http.ResponseWriter) ([]byte, error) {
qr := be.Query(req, w, false)
return qr.Body, qr.Err
Expand Down Expand Up @@ -151,12 +151,12 @@ func QueryShowQL(w http.ResponseWriter, req *http.Request, ip *Proxy, tokens []s
}

func QueryDeleteOrDropQL(w http.ResponseWriter, req *http.Request, ip *Proxy, tokens []string, db string) (body []byte, err error) {
// all circles -> backend by key(db,meas) -> delete or drop measurement/series
meas, err := GetMeasurementFromTokens(tokens)
// all circles -> backend by key(db,mm) -> delete or drop measurement/series
mm, err := GetMeasurementFromTokens(tokens)
if err != nil {
return nil, err
}
key := ip.GetKey(db, meas)
key := ip.GetKey(db, mm)
backends := ip.GetBackends(key)
return QueryBackends(backends, req, w)
}
Expand Down
12 changes: 6 additions & 6 deletions backend/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,13 @@ func (hb *HttpBackend) GetMeasurements(db string) []string {
return hb.GetSeriesValues(db, "show measurements")
}

func (hb *HttpBackend) GetTagKeys(db, rp, meas string) []string {
return hb.GetSeriesValues(db, fmt.Sprintf("show tag keys from \"%s\".\"%s\"", util.EscapeIdentifier(rp), util.EscapeIdentifier(meas)))
func (hb *HttpBackend) GetTagKeys(db, rp, mm string) []string {
return hb.GetSeriesValues(db, fmt.Sprintf("show tag keys from \"%s\".\"%s\"", util.EscapeIdentifier(rp), util.EscapeIdentifier(mm)))
}

func (hb *HttpBackend) GetFieldKeys(db, rp, meas string) map[string][]string {
func (hb *HttpBackend) GetFieldKeys(db, rp, mm string) map[string][]string {
fieldKeys := make(map[string][]string)
q := fmt.Sprintf("show field keys from \"%s\".\"%s\"", util.EscapeIdentifier(rp), util.EscapeIdentifier(meas))
q := fmt.Sprintf("show field keys from \"%s\".\"%s\"", util.EscapeIdentifier(rp), util.EscapeIdentifier(mm))
qr := hb.Query(NewQueryRequest("GET", db, q, ""), nil, true)
if qr.Err != nil {
return fieldKeys
Expand All @@ -484,8 +484,8 @@ func (hb *HttpBackend) GetFieldKeys(db, rp, meas string) map[string][]string {
return fieldKeys
}

func (hb *HttpBackend) DropMeasurement(db, meas string) ([]byte, error) {
q := fmt.Sprintf("drop measurement \"%s\"", util.EscapeIdentifier(meas))
func (hb *HttpBackend) DropMeasurement(db, mm string) ([]byte, error) {
q := fmt.Sprintf("drop measurement \"%s\"", util.EscapeIdentifier(mm))
qr := hb.Query(NewQueryRequest("POST", db, q, ""), nil, true)
return qr.Body, qr.Err
}
Expand Down
28 changes: 14 additions & 14 deletions backend/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func NewProxy(cfg *ProxyConfig) (ip *Proxy) {
return
}

func (ip *Proxy) GetKey(db, meas string) string {
return ip.sTpl.GetKey(db, meas)
func (ip *Proxy) GetKey(db, mm string) string {
return ip.sTpl.GetKey(db, mm)
}

func (ip *Proxy) GetBackends(key string) []*Backend {
Expand Down Expand Up @@ -88,11 +88,11 @@ func (ip *Proxy) IsForbiddenDB(db string) bool {
}

func (ip *Proxy) QueryFlux(w http.ResponseWriter, req *http.Request, qr *QueryRequest) (err error) {
var bucket, meas string
var bucket, measurement string
if qr.Query != "" {
bucket, meas, err = ScanQuery(qr.Query)
bucket, measurement, err = ScanQuery(qr.Query)
} else if qr.Spec != nil {
bucket, meas, err = ScanSpec(qr.Spec)
bucket, measurement, err = ScanSpec(qr.Spec)
}
if err != nil {
return
Expand All @@ -102,10 +102,10 @@ func (ip *Proxy) QueryFlux(w http.ResponseWriter, req *http.Request, qr *QueryRe
} else if ip.IsForbiddenDB(bucket) {
return fmt.Errorf("database forbidden: %s", bucket)
}
if meas == "" {
if measurement == "" {
return ErrGetMeasurement
}
return QueryFlux(w, req, ip, bucket, meas)
return QueryFlux(w, req, ip, bucket, measurement)
}

func (ip *Proxy) Query(w http.ResponseWriter, req *http.Request) (body []byte, err error) {
Expand Down Expand Up @@ -177,20 +177,20 @@ func (ip *Proxy) Write(p []byte, db, rp, precision string) (err error) {

func (ip *Proxy) WriteRow(line []byte, db, rp, precision string) {
nanoLine := AppendNano(line, precision)
meas, err := ScanKey(nanoLine)
mm, err := ScanKey(nanoLine)
if err != nil {
log.Printf("scan key error: %s", err)
return
}
if !RapidCheck(nanoLine[len(meas):]) {
if !RapidCheck(nanoLine[len(mm):]) {
log.Printf("invalid format, db: %s, rp: %s, precision: %s, line: %s", db, rp, precision, string(line))
return
}

key := ip.GetKey(db, meas)
key := ip.GetKey(db, mm)
backends := ip.GetBackends(key)
if len(backends) == 0 {
log.Printf("write data error: can't get backends, db: %s, meas: %s", db, meas)
log.Printf("write data error: can't get backends, db: %s, mm: %s", db, mm)
return
}

Expand All @@ -206,11 +206,11 @@ func (ip *Proxy) WriteRow(line []byte, db, rp, precision string) {
func (ip *Proxy) WritePoints(points []models.Point, db, rp string) error {
var err error
for _, pt := range points {
meas := string(pt.Name())
key := ip.GetKey(db, meas)
mm := string(pt.Name())
key := ip.GetKey(db, mm)
backends := ip.GetBackends(key)
if len(backends) == 0 {
log.Printf("write point error: can't get backends, db: %s, meas: %s", db, meas)
log.Printf("write point error: can't get backends, db: %s, mm: %s", db, mm)
err = ErrEmptyBackends
continue
}
Expand Down
6 changes: 3 additions & 3 deletions backend/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func newShardTpl(tpl string) *shardTpl {
return sTpl
}

func (sTpl *shardTpl) GetKey(db, meas string) string {
func (sTpl *shardTpl) GetKey(db, mm string) string {
var b strings.Builder
b.Grow(len(sTpl.tpl) + (len(db)-len(ShardKeyVarDb))*sTpl.dbCnt + (len(meas)-len(ShardKeyVarMm))*sTpl.mmCnt)
b.Grow(len(sTpl.tpl) + (len(db)-len(ShardKeyVarDb))*sTpl.dbCnt + (len(mm)-len(ShardKeyVarMm))*sTpl.mmCnt)
for _, item := range sTpl.items {
if item == ShardKeyVarDb {
b.WriteString(db)
} else if item == ShardKeyVarMm {
b.WriteString(meas)
b.WriteString(mm)
} else {
b.WriteString(item)
}
Expand Down
12 changes: 8 additions & 4 deletions service/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,13 @@ func (hs *HttpService) HandlerReplica(w http.ResponseWriter, req *http.Request)
}

db := req.URL.Query().Get("db")
meas := req.URL.Query().Get("meas")
if db != "" && meas != "" {
key := hs.ip.GetKey(db, meas)
mm := req.URL.Query().Get("mm")
if mm == "" {
// compatible with version <= 2.5.11
mm = req.URL.Query().Get("meas")
}
if db != "" && mm != "" {
key := hs.ip.GetKey(db, mm)
backends := hs.ip.GetBackends(key)
data := make([]map[string]interface{}, len(backends))
for i, b := range backends {
Expand All @@ -302,7 +306,7 @@ func (hs *HttpService) HandlerReplica(w http.ResponseWriter, req *http.Request)
}
hs.Write(w, req, http.StatusOK, data)
} else {
hs.WriteError(w, req, http.StatusBadRequest, "invalid db or meas")
hs.WriteError(w, req, http.StatusBadRequest, "invalid db or mm")
}
}

Expand Down
Loading

0 comments on commit d59c447

Please sign in to comment.