forked from weberhong/goose
/
GooseSearch.go
296 lines (242 loc) · 6.65 KB
/
GooseSearch.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package goose
import (
"fmt"
"github.com/getwe/goose/config"
. "github.com/getwe/goose/database"
log "github.com/getwe/goose/log"
. "github.com/getwe/goose/utils"
"net"
"runtime"
"sync"
"time"
)
// Goose检索程序.核心工作是提供检索服务,同时支持动态插入索引.
type GooseSearch struct {
conf config.Conf
// 支持检索的db,同时提供动态插入索引功能
searchDB *DBSearcher
// 动态索引生成器
varIndexer *VarIndexer
// 检索流程
searcher *Searcher
}
func (this *GooseSearch) Run() error {
// read conf
log.Debug("GooseSearch Run begin")
searchGoroutineNum := this.conf.Int64("GooseSearch.Search.GoroutineNum")
searchSvrPort := this.conf.Int64("GooseSearch.Search.ServerPort")
indexSvrPort := this.conf.Int64("GooseSearch.Index.ServerPort")
searchReqBufSize := this.conf.Int64("GooseSearch.Search.RequestBufferSize")
searchResBufSize := this.conf.Int64("GooseSearch.Search.ResponseBufferSize")
indexReqBufSize := this.conf.Int64("GooseSearch.Index.RequestBufferSize")
//indexResBufSize := this.conf.GetInt("GooseSearch.Index.ResponseBufferSize")
refreshSleepTime := this.conf.Int64("GooseSearch.Refresh.SleepTime")
log.Debug("Read Conf searchGoroutineNum[%d] searchSvrPort[%d] "+
"indexSvrPort[%d] searchReqBufSize[%d] searchResBufSize[%d] "+
"indexReqBufSize[%d] refreshSleepTime[%d]", searchGoroutineNum,
searchSvrPort, indexSvrPort, searchReqBufSize, searchResBufSize,
indexReqBufSize, refreshSleepTime)
err := this.runSearchServer(int(searchGoroutineNum), int(searchSvrPort),
int(searchReqBufSize), int(searchResBufSize))
if err != nil {
return err
}
err = this.runIndexServer(int(indexSvrPort), int(indexReqBufSize))
if err != nil {
return err
}
err = this.runRefreshServer(int(refreshSleepTime))
if err != nil {
return err
}
neverReturn := sync.WaitGroup{}
neverReturn.Add(1)
neverReturn.Wait()
return nil
}
func (this *GooseSearch) runSearchServer(routineNum int, listenPort int,
requestBufSize int, responseBufSize int) error {
if 0 == routineNum || 0 == listenPort || 0 == requestBufSize || 0 == responseBufSize {
return log.Error("arg error routineNum[%d] listenPort[%d] "+
"requestBufSize[%d] responseBufSize[%d]", routineNum, listenPort,
requestBufSize, responseBufSize)
}
listener, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", listenPort))
if err != nil {
log.Error("runSearchServer listen fail : %s", err.Error())
return err
}
for i := 0; i < routineNum; i++ {
go func() {
reqbuf := make([]byte, requestBufSize)
resbuf := make([]byte, responseBufSize)
context := NewStyContext()
for {
var reqlen int
var reslen int
var t1, t2 int64
// clear buf
context.Clear()
conn, err := listener.Accept()
if err != nil {
log.Warn("SearchServer accept fail : %s", err.Error())
goto LabelError
}
context.Log.Info("IP", conn.RemoteAddr().String())
// receive data
reqlen, err = conn.Read(reqbuf)
if err != nil {
log.Warn("SearchServer read fail : %s receive len[%d]", err.Error(), reqlen)
goto LabelError
}
context.Log.Info("reqlen", reqlen)
// do search
t1 = time.Now().UnixNano()
reslen, err = this.searcher.Search(context, reqbuf, resbuf)
t2 = time.Now().UnixNano()
if err != nil {
log.Warn("SearchServer Search fail : %s", err.Error())
goto LabelError
}
context.Log.Info("time(ms)", Ns2Ms(t2-t1))
// write data
_, err = conn.Write(resbuf[:reslen])
if err != nil {
log.Warn("SearchServer conn write fail : %s", err.Error())
goto LabelError
}
LabelError:
conn.Close()
context.Log.PrintAllInfo()
}
}()
}
return nil
}
func (this *GooseSearch) runIndexServer(listenPort int, requestBufSize int) error {
if 0 == listenPort || 0 == requestBufSize {
return log.Error("arg error istenPort[%d] requestBufSize[%d]",
listenPort, requestBufSize)
}
if this.varIndexer == nil {
return nil
}
listener, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", listenPort))
if err != nil {
log.Error("runIndexServer listen fail : %s", err.Error())
return err
}
// 简单一个协程完成接受请求和完成处理.索引更新不要求高并发性.
go func() {
reqbuf := make([]byte, requestBufSize)
for {
var reqlen int
conn, err := listener.Accept()
if err != nil {
log.Warn("IndexServer accept fail : %s", err.Error())
goto LabelError
}
// receive data
reqlen, err = conn.Read(reqbuf)
if err != nil {
log.Warn("IndexSearcher read fail : %s", err.Error())
goto LabelError
}
// index
err = this.varIndexer.BuildIndex(NewBufferIterOnce(reqbuf[:reqlen]))
if err != nil {
log.Warn("IndexSearcher BuildIndex fail : %s", err.Error())
goto LabelError
}
LabelError:
conn.Close()
}
}()
return nil
}
func (this *GooseSearch) runRefreshServer(sleeptime int) error {
if 0 == sleeptime {
return log.Error("arg error sleeptime[%d]", sleeptime)
}
go func() {
for {
time.Sleep(time.Duration(sleeptime) * time.Second)
log.Debug("refresh now")
// sync search db
err := this.searchDB.Sync()
if err != nil {
log.Warn(err)
}
}
}()
return nil
}
func (this *GooseSearch) Init(confPath string,
indexSty IndexStrategy, searchSty SearchStrategy) (err error) {
defer func() {
if r := recover(); r != nil {
err = log.Error(r)
}
}()
// load conf
this.conf, err = config.NewConf(confPath)
if err != nil {
return
}
// set max procs
maxProcs := int(this.conf.Int64("GooseSearch.MaxProcs"))
if maxProcs <= 0 {
maxProcs = runtime.NumCPU()
}
runtime.GOMAXPROCS(maxProcs)
log.Debug("set max procs [%d]", maxProcs)
// init dbsearcher
dbPath := this.conf.String("GooseBuild.DataBase.DbPath")
log.Debug("init db [%s]", dbPath)
this.searchDB = NewDBSearcher()
err = this.searchDB.Init(dbPath)
if err != nil {
return
}
log.Debug("init db [%s]", dbPath)
// index strategy global init
if indexSty != nil {
err = indexSty.Init(this.conf)
if err != nil {
return
}
}
log.Debug("index strategy init finish")
// search strategy global init
if searchSty != nil {
err = searchSty.Init(this.conf)
if err != nil {
return
}
}
log.Debug("search strategy init finish")
// var indexer
if indexSty != nil {
this.varIndexer, err = NewVarIndexer(this.searchDB, indexSty)
if err != nil {
return
}
}
log.Debug("VarIndexer init finish")
// searcher
if searchSty != nil {
this.searcher, err = NewSearcher(this.searchDB, searchSty)
if err != nil {
return
}
}
log.Debug("Searcher init finish")
return
}
func NewGooseSearch() *GooseSearch {
s := GooseSearch{}
s.searchDB = nil
s.searcher = nil
s.varIndexer = nil
return &s
}