From a11779ec1bcee496ecc3086a41ac650ae9637c53 Mon Sep 17 00:00:00 2001 From: niean Date: Sun, 24 Apr 2016 07:24:14 +0000 Subject: [PATCH] add api of deleting one index --- g/g.go | 3 ++- g/git.go | 3 +-- graph/index.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ http/graph_http.go | 35 ++++++++++++++++++++++++++ scripts/index.delete | 30 ++++++++++++++++++++++ 5 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 graph/index.go create mode 100755 scripts/index.delete diff --git a/g/g.go b/g/g.go index 1b4e402..92fbe7b 100644 --- a/g/g.go +++ b/g/g.go @@ -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() { diff --git a/g/git.go b/g/git.go index 0924b43..0f198ce 100644 --- a/g/git.go +++ b/g/git.go @@ -1,5 +1,4 @@ package g - const ( - COMMIT = "a5ad45c" + COMMIT = "ea558f8" ) diff --git a/graph/index.go b/graph/index.go new file mode 100644 index 0000000..11c33ff --- /dev/null +++ b/graph/index.go @@ -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 + } + } +} diff --git a/http/graph_http.go b/http/graph_http.go index 91667e7..a372b01 100644 --- a/http/graph_http.go +++ b/http/graph_http.go @@ -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, "") } diff --git a/scripts/index.delete b/scripts/index.delete new file mode 100755 index 0000000..4244f6d --- /dev/null +++ b/scripts/index.delete @@ -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