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

add api of deleting one index #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion g/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
// 1.4.1 add last item counter, add proc for connpool
// 1.4.2 rm nil items in http.responses
// 1.4.3 spell check, make config consistent with previous
// 1.4.4 add api of deleting one index

const (
VERSION = "1.4.3"
VERSION = "1.4.4"
)

func init() {
Expand Down
3 changes: 1 addition & 2 deletions g/git.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package g

const (
COMMIT = "a5ad45c"
COMMIT = "ea558f8"
)
60 changes: 60 additions & 0 deletions graph/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package graph

import (
"errors"
"fmt"
"time"

cmodel "github.com/open-falcon/common/model"
spool "github.com/toolkits/pool/simple_conn_pool"

"github.com/open-falcon/query/g"
)

func DeleteIndex(para *cmodel.GraphCounter) (*cmodel.SimpleRpcResponse, error) {
endpoint, counter := para.Endpoint, para.Counter
if len(endpoint) < 1 || len(counter) < 1 {
return nil, fmt.Errorf("empty")
}

pool, addr, err := selectPool(endpoint, counter)
if err != nil {
return nil, err
}

conn, err := pool.Fetch()
if err != nil {
return nil, err
}

rpcConn := conn.(spool.RpcClient)
if rpcConn.Closed() {
pool.ForceClose(conn)
return nil, errors.New("conn closed")
}

type ChResult struct {
Err error
Resp *cmodel.SimpleRpcResponse
}
ch := make(chan *ChResult, 1)
go func() {
resp := &cmodel.SimpleRpcResponse{}
err := rpcConn.Call("Graph.DeleteIndex", para, resp)
ch <- &ChResult{Err: err, Resp: resp}
}()

select {
case <-time.After(time.Duration(g.Config().Graph.CallTimeout) * time.Millisecond):
pool.ForceClose(conn)
return nil, fmt.Errorf("%s, call timeout. proc: %s", addr, pool.Proc())
case r := <-ch:
if r.Err != nil {
pool.ForceClose(conn)
return nil, fmt.Errorf("%s, call failed, err %v. proc: %s", addr, r.Err, pool.Proc())
} else {
pool.Release(conn)
return r.Resp, nil
}
}
}
35 changes: 35 additions & 0 deletions http/graph_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,39 @@ func configGraphRoutes() {
StdRender(w, data, nil)
})

// 彻底删除 监控指标的索引
http.HandleFunc("/graph/index/delete", httpHandler_deleteIndex)
}

func httpHandler_deleteIndex(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
RenderMsgJson(w, "bad http method, use post")
return
}

var body []*cmodel.GraphCounter
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&body)
if err != nil {
RenderMsgJson(w, err.Error())
return
}
if len(body) < 1 {
RenderMsgJson(w, "empty")
return
}

counter := body[0]
resp, err := graph.DeleteIndex(counter)
if err != nil {
RenderMsgJson(w, err.Error())
return
}

if resp.Code != 0 {
RenderMsgJson(w, resp.Msg)
return

}
RenderDataJson(w, "")
}
30 changes: 30 additions & 0 deletions scripts/index.delete
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash
## test home
testdir=$(cd $(dirname $0)/; pwd)
## word home
workdir=$(dirname $testdir)
cd $workdir

cfg=./cfg.json
httpport=80
if [ -f $cfg ]; then
httpport=`cat $cfg | grep -A3 "\"http\":" | grep "\"listen\"" | cut -d\" -f4 | cut -d: -f2`
fi
httpprex="127.0.0.1:$httpport"

if [ $# != 2 ];then
printf "format:./query \"endpoint\" \"counter\"\n"
exit 1
fi

# args
endpoint=$1
counter=$2

# form request body
req="[{\"endpoint\":\"$endpoint\", \"counter\":\"$counter\"}]"

# request
url="$httpprex/graph/index/delete"

curl -s -X POST -d "$req" "$url" | python -m json.tool