/
storage.go
286 lines (253 loc) · 7.51 KB
/
storage.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
package storage
import (
"time"
"strconv"
"github.com/xiaonanln/go-xnsyncutil/xnsyncutil"
"github.com/xiaonanln/goworld/engine/common"
"github.com/xiaonanln/goworld/engine/config"
"github.com/xiaonanln/goworld/engine/consts"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/engine/opmon"
"github.com/xiaonanln/goworld/engine/post"
"github.com/xiaonanln/goworld/engine/storage/backend/filesystem"
"github.com/xiaonanln/goworld/engine/storage/backend/mongodb"
"github.com/xiaonanln/goworld/engine/storage/backend/mysql"
"github.com/xiaonanln/goworld/engine/storage/backend/redis"
"github.com/xiaonanln/goworld/engine/storage/backend/redis_cluster"
"github.com/xiaonanln/goworld/engine/storage/storage_common"
)
var (
storageEngine storagecommon.EntityStorage
operationQueue = xnsyncutil.NewSyncQueue()
storageRoutineTerminated = xnsyncutil.NewOneTimeCond()
)
type saveRequest struct {
TypeName string
EntityID common.EntityID
Data interface{}
Callback SaveCallbackFunc
}
type loadRequest struct {
TypeName string
EntityID common.EntityID
Callback LoadCallbackFunc
}
type existsRequest struct {
TypeName string
EntityID common.EntityID
Callback ExistsCallbackFunc
}
type listEntityIDsRequest struct {
TypeName string
Callback ListCallbackFunc
}
// SaveCallbackFunc is the callback type of storage Save
type SaveCallbackFunc func()
// LoadCallbackFunc is the callback type of storage Load
type LoadCallbackFunc func(data interface{}, err error)
// ExistsCallbackFunc is the callback type of storage Exists
type ExistsCallbackFunc func(exists bool, err error)
// ListCallbackFunc is the callback type of storage List
type ListCallbackFunc func([]common.EntityID, error)
// Save saves entity data to storage
func Save(typeName string, entityID common.EntityID, data interface{}, callback SaveCallbackFunc) {
operationQueue.Push(saveRequest{
TypeName: typeName,
EntityID: entityID,
Data: data,
Callback: callback,
})
checkOperationQueueLen()
}
// Load loads entity data from storage
func Load(typeName string, entityID common.EntityID, callback LoadCallbackFunc) {
operationQueue.Push(loadRequest{
TypeName: typeName,
EntityID: entityID,
Callback: callback,
})
checkOperationQueueLen()
}
// Exists checks if entity of specified ID exists in storage
func Exists(typeName string, entityID common.EntityID, callback ExistsCallbackFunc) {
operationQueue.Push(existsRequest{
TypeName: typeName,
EntityID: entityID,
Callback: callback,
})
checkOperationQueueLen()
}
// ListEntityIDs returns all entity IDs in storage
//
// Return values can be large for common entity types
func ListEntityIDs(typeName string, callback ListCallbackFunc) {
operationQueue.Push(listEntityIDsRequest{
TypeName: typeName,
Callback: callback,
})
checkOperationQueueLen()
}
var recentWarnedQueueLen = 0
func checkOperationQueueLen() {
qlen := operationQueue.Len()
if qlen > 100 && qlen%100 == 0 && recentWarnedQueueLen != qlen {
gwlog.Warnf("Storage operation queue length = %d", qlen)
recentWarnedQueueLen = qlen
}
}
// Shutdown storage module
func Shutdown() {
operationQueue.Close()
storageRoutineTerminated.Wait()
}
// Initialize is called by engine to initialize storage module
func Initialize() {
err := assureStorageEngineReady()
if err != nil {
gwlog.Fatalf("Storage engine is not ready: %s", err)
}
go storageRoutine()
}
func assureStorageEngineReady() (err error) {
if storageEngine != nil {
return
}
cfg := config.GetStorage()
if cfg.Type == "filesystem" {
storageEngine, err = entitystoragefilesystem.OpenDirectory(cfg.Directory)
} else if cfg.Type == "mongodb" {
storageEngine, err = entitystoragemongodb.OpenMongoDB(cfg.Url, cfg.DB)
} else if cfg.Type == "redis" {
var dbindex int = -1
if cfg.DB != "" {
if dbindex, err = strconv.Atoi(cfg.DB); err != nil {
return err
}
}
storageEngine, err = entitystorageredis.OpenRedis(cfg.Url, dbindex)
} else if cfg.Type == "redis_cluster" {
storageEngine, err = entitystoragerediscluster.OpenRedisCluster(cfg.StartNodes.ToList())
} else if cfg.Type == "sql" {
if cfg.Driver == "mysql" {
storageEngine, err = entitystoragemysql.OpenMySQL(cfg.Url)
} else {
gwlog.Panicf("unknown sql driver: %s", cfg.Driver)
}
} else {
gwlog.Panicf("unknown storage type: %s", cfg.Type)
}
return
}
func storageRoutine() {
defer func() {
err := recover()
if err != nil {
gwlog.TraceError("storage routine paniced: %s, restarting ...", err)
go storageRoutine() // restart the storage routine
} else {
// normal quit
storageEngine.Close()
storageRoutineTerminated.Signal()
}
}()
for {
err := assureStorageEngineReady()
if err != nil {
gwlog.Errorf("Storage engine is not ready: %s", err)
time.Sleep(time.Second)
continue
}
op := operationQueue.Pop()
if op == nil { // entity storage closed
break
}
var monop *opmon.Operation
if saveReq, ok := op.(saveRequest); ok {
// handle save request
monop = opmon.StartOperation("storage.save")
for {
if consts.DEBUG_SAVE_LOAD {
gwlog.Debugf("storage: SAVING %s %s ...", saveReq.TypeName, saveReq.EntityID)
}
err := assureStorageEngineReady()
if err != nil {
gwlog.Errorf("Storage engine is not ready: %s", err)
time.Sleep(time.Second) // wait for 1 second to retry
continue
}
if storageEngine == nil {
gwlog.Fatalf("storage engine is nil")
}
err = storageEngine.Write(saveReq.TypeName, saveReq.EntityID, saveReq.Data)
if err != nil {
// save failed ?
gwlog.Errorf("storage: save failed: %s", err)
if err != nil && storageEngine.IsEOF(err) {
storageEngine.Close()
storageEngine = nil
}
continue // always retry if fail
} else {
monop.Finish(time.Millisecond * 100)
if saveReq.Callback != nil {
post.Post(func() {
saveReq.Callback()
})
}
break
}
}
} else if loadReq, ok := op.(loadRequest); ok {
// handle load request
gwlog.Debugf("storage: LOADING %s %s ...", loadReq.TypeName, loadReq.EntityID)
monop = opmon.StartOperation("storage.load")
data, err := storageEngine.Read(loadReq.TypeName, loadReq.EntityID)
if err != nil {
// save failed ?
gwlog.TraceError("storage: load %s %s failed: %s", loadReq.TypeName, loadReq.EntityID, err)
data = nil
}
monop.Finish(time.Millisecond * 100)
if loadReq.Callback != nil {
post.Post(func() {
loadReq.Callback(data, err)
})
}
if err != nil && storageEngine.IsEOF(err) {
storageEngine.Close()
storageEngine = nil
}
} else if existsReq, ok := op.(existsRequest); ok {
monop = opmon.StartOperation("storage.exists")
exists, err := storageEngine.Exists(existsReq.TypeName, existsReq.EntityID)
monop.Finish(time.Millisecond * 100)
if existsReq.Callback != nil {
post.Post(func() {
existsReq.Callback(exists, err)
})
}
if err != nil && storageEngine.IsEOF(err) {
storageEngine.Close()
storageEngine = nil
}
} else if listReq, ok := op.(listEntityIDsRequest); ok {
monop = opmon.StartOperation("storage.list")
eids, err := storageEngine.List(listReq.TypeName)
if err != nil {
gwlog.TraceError("ListEntityIDs %s failed: %s", listReq.TypeName, err)
}
monop.Finish(time.Millisecond * 1000)
if listReq.Callback != nil {
post.Post(func() {
listReq.Callback(eids, err)
})
}
if err != nil && storageEngine.IsEOF(err) {
storageEngine.Close()
storageEngine = nil
}
} else {
gwlog.Panicf("storage: unknown operation: %v", op)
}
}
}