/
manage.go
167 lines (156 loc) · 3.56 KB
/
manage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package daemon
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path"
"runtime/pprof"
"sort"
"strings"
"github.com/masahide/gordb/core"
"github.com/masahide/gordb/input/csv"
"github.com/masahide/gordb/status"
)
type ManageCmd uint
const (
PostNode ManageCmd = 1 << iota
MoveNode
DelNode
GetNodeList
GetNodes
PPROF_FILE = "/tmp/gordb.pprof"
)
type ManageRequest struct {
Cmd ManageCmd
Name string
Path string
Node *core.Node
ResCh chan ManageResponse
From string
To string
}
type ManageResponse struct {
Body interface{}
Err error
}
func (d *Daemon) ManageHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST": // load
name := strings.TrimRight(path.Base(r.URL.Path), "/")
dirpath := r.PostForm.Get("path")
if name != "" {
name = strings.TrimRight(path.Base(dirpath), "/")
}
node, err := csv.Crawler(r.URL.Path)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err = fmt.Errorf("csv.Crawler err: %s", err)
fmt.Fprintln(w, err)
log.Println(err)
}
err = d.BroadcastManageReq(ManageRequest{Cmd: PostNode, Path: dirpath, Name: name, Node: node})
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, err)
}
case "PUT": // move
name := strings.TrimRight(path.Base(r.URL.Path), "/")
switch name {
case "move":
from := r.PostForm.Get("from")
to := r.PostForm.Get("to")
err := d.BroadcastManageReq(ManageRequest{Cmd: MoveNode, From: from, To: to})
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, err)
}
case "start_pprof":
f, err := os.Create(PPROF_FILE)
if err != nil {
log.Print(err)
break
}
pprof.StartCPUProfile(f)
log.Printf("StartCPUProfile(%s)", PPROF_FILE)
case "end_pprof":
pprof.StopCPUProfile()
log.Print("StopCPUProfile")
}
case "DELETE": //delete
name := strings.TrimRight(path.Base(r.URL.Path), "/")
err := d.BroadcastManageReq(ManageRequest{Cmd: DelNode, Name: name})
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, err)
}
case "GET":
switch r.URL.Path {
case "/list":
res := d.sendManageReq(ManageRequest{Cmd: GetNodeList})
if res.Err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, res.Err)
}
json.NewEncoder(w).Encode(res.Body)
case "/status":
stat := status.Get()
json.NewEncoder(w).Encode(stat)
}
}
}
func (d *Daemon) BroadcastManageReq(req ManageRequest) error {
for _, q := range d.MngQ {
mr := req
mr.ResCh = make(chan ManageResponse, 1)
q <- mr
res := <-mr.ResCh
if res.Err != nil {
return res.Err
}
}
return nil
}
func (d *Daemon) sendManageReq(req ManageRequest) ManageResponse {
q := d.MngQ[0]
mr := req
mr.ResCh = make(chan ManageResponse, 1)
q <- mr
res := <-mr.ResCh
return res
}
func (d *Daemon) manageWork(req ManageRequest, node *core.Node) ManageResponse {
res := ManageResponse{}
switch req.Cmd {
case PostNode:
node.Nodes[req.Name] = req.Node
return res
case DelNode:
_, ok := node.Nodes[req.Name]
if ok {
delete(node.Nodes, req.Name)
return res
}
log.Printf("Name Not found: %s", req.Name)
return res
case MoveNode:
n, ok := node.Nodes[req.From]
if ok {
node.Nodes[req.To] = n
delete(node.Nodes, req.From)
return res
}
log.Printf("Name Not found: %s", req.From)
return res
case GetNodeList:
list := make([]string, 0, len(node.Nodes))
for name, _ := range node.Nodes {
list = append(list, name)
}
sort.Strings(list)
res.Body = list
return res
}
return ManageResponse{Err: fmt.Errorf("Unkown ManageRequest:%v", req.Cmd)}
}