-
Notifications
You must be signed in to change notification settings - Fork 0
/
zzm.go
441 lines (376 loc) · 14.3 KB
/
zzm.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package dm
import (
"bufio"
"io"
"os"
"runtime"
"strconv"
"strings"
"github.com/ganl/go-dm/util"
)
var LogDirDef, _ = os.Getwd()
var StatDirDef, _ = os.Getwd()
const (
DEFAULT_PORT int32 = 5236
//log level
LOG_OFF int = 0
LOG_ERROR int = 1
LOG_WARN int = 2
LOG_SQL int = 3
LOG_INFO int = 4
LOG_DEBUG int = 5
LOG_ALL int = 9
//stat
STAT_SQL_REMOVE_LATEST int = 0
STAT_SQL_REMOVE_OLDEST int = 1
// 编码字符集
ENCODING_UTF8 string = "UTF-8"
ENCODING_EUCKR string = "EUC-KR"
ENCODING_GB18030 string = "GB18030"
DbAliveCheckFreqDef = 0
LocaleDef = 0
// log
LogLevelDef = LOG_OFF // 日志级别:off, error, warn, sql, info, all
LogFlushFreqDef = 10 // 日志刷盘时间s (>=0)
LogFlushQueueSizeDef = 100 //日志队列大小
LogBufferSizeDef = 32 * 1024 // 日志缓冲区大小 (>0)
// stat
StatEnableDef = false //
StatFlushFreqDef = 3 // 日志刷盘时间s (>=0)
StatSlowSqlCountDef = 100 // 慢sql top行数,(0-1000)
StatHighFreqSqlCountDef = 100 // 高频sql top行数, (0-1000)
StatSqlMaxCountDef = 100000 // sql 统计最大值(0-100000)
StatSqlRemoveModeDef = STAT_SQL_REMOVE_LATEST // 记录sql数超过最大值时,sql淘汰方式
)
var (
DbAliveCheckFreq = DbAliveCheckFreqDef
Locale = LocaleDef // 0:简体中文 1:英文 2:繁体中文
// log
LogLevel = LogLevelDef // 日志级别:off, error, warn, sql, info, all
LogDir = LogDirDef
LogFlushFreq = LogFlushFreqDef // 日志刷盘时间s (>=0)
LogFlushQueueSize = LogFlushQueueSizeDef
LogBufferSize = LogBufferSizeDef // 日志缓冲区大小 (>0)
// stat
StatEnable = StatEnableDef //
StatDir = StatDirDef // jdbc工作目录,所有生成的文件都在该目录下
StatFlushFreq = StatFlushFreqDef // 日志刷盘时间s (>=0)
StatSlowSqlCount = StatSlowSqlCountDef // 慢sql top行数,(0-1000)
StatHighFreqSqlCount = StatHighFreqSqlCountDef // 高频sql top行数, (0-1000)
StatSqlMaxCount = StatSqlMaxCountDef // sql 统计最大值(0-100000)
StatSqlRemoveMode = StatSqlRemoveModeDef // 记录sql数超过最大值时,sql淘汰方式
/*---------------------------------------------------------------*/
ServerGroupMap = make(map[string]*epGroup)
GlobalProperties = NewProperties()
)
// filePath: dm_svc.conf 文件路径
func load(filePath string) {
if filePath == "" {
switch runtime.GOOS {
case "windows":
filePath = os.Getenv("SystemRoot") + "\\system32\\dm_svc.conf"
case "linux":
filePath = "/etc/dm_svc.conf"
default:
return
}
}
file, err := os.Open(filePath)
defer file.Close()
if err != nil {
return
}
fileReader := bufio.NewReader(file)
// GlobalProperties = NewProperties()
var groupProps *Properties
var line string //dm_svc.conf读取到的一行
for line, err = fileReader.ReadString('\n'); line != "" && (err == nil || err == io.EOF); line, err = fileReader.ReadString('\n') {
// 去除#标记的注释
if notesIndex := strings.IndexByte(line, '#'); notesIndex != -1 {
line = line[:notesIndex]
}
// 去除前后多余的空格
line = strings.TrimSpace(line)
if line == "" {
continue
}
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
groupName := strings.ToLower(line[1 : len(line)-1])
dbGroup, ok := ServerGroupMap[groupName]
if groupName == "" || !ok {
continue
}
groupProps = dbGroup.props
if groupProps.IsNil() {
groupProps = NewProperties()
groupProps.SetProperties(GlobalProperties)
dbGroup.props = groupProps
}
} else {
cfgInfo := strings.Split(line, "=")
if len(cfgInfo) < 2 {
continue
}
key := strings.TrimSpace(cfgInfo[0])
value := strings.TrimSpace(cfgInfo[1])
if strings.HasPrefix(value, "(") && strings.HasSuffix(value, ")") {
value = strings.TrimSpace(value[1 : len(value)-1])
}
if key == "" || value == "" {
continue
}
// 区分属性是全局的还是组的
var success bool
if groupProps.IsNil() {
success = SetServerGroupProperties(GlobalProperties, key, value)
} else {
success = SetServerGroupProperties(groupProps, key, value)
}
if !success {
var serverGroup = parseServerName(key, value)
if serverGroup != nil {
serverGroup.props = NewProperties()
serverGroup.props.SetProperties(GlobalProperties)
ServerGroupMap[strings.ToLower(key)] = serverGroup
}
}
}
}
}
func SetServerGroupProperties(props *Properties, key string, value string) bool {
if util.StringUtil.EqualsIgnoreCase(key, "ADDRESS_REMAP") {
tmp := props.GetString(AddressRemapKey, "")
props.Set(AddressRemapKey, tmp+"("+value+")")
} else if util.StringUtil.EqualsIgnoreCase(key, "ALWAYS_ALLOW_COMMIT") {
props.Set(AlwayseAllowCommitKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "APP_NAME") {
props.Set(AppNameKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "AUTO_COMMIT") {
props.Set(AutoCommitKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "BATCH_ALLOW_MAX_ERRORS") {
props.Set(BatchAllowMaxErrorsKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "BATCH_CONTINUE_ON_ERROR") ||
util.StringUtil.EqualsIgnoreCase(key, "CONTINUE_BATCH_ON_ERROR") {
props.Set(ContinueBatchOnErrorKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "BATCH_NOT_ON_CALL") {
props.Set(BatchNotOnCallKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "BATCH_TYPE") {
props.Set(BatchTypeKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "BUF_PREFETCH") {
props.Set(BufPrefetchKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "CIPHER_PATH") {
props.Set(CipherPathKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "CLUSTER") {
props.Set(ClusterKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "COLUMN_NAME_UPPER_CASE") {
props.Set(ColumnNameUpperCaseKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "COLUMN_NAME_CASE") {
props.Set(ColumnNameCaseKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "COMPATIBLE_MODE") {
props.Set(CompatibleModeKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "COMPRESS") ||
util.StringUtil.EqualsIgnoreCase(key, "COMPRESS_MSG") {
props.Set(CompressKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "COMPRESS_ID") {
props.Set(CompressIdKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "CONNECT_TIMEOUT") {
props.Set(ConnectTimeoutKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "DO_SWITCH") ||
util.StringUtil.EqualsIgnoreCase(key, "AUTO_RECONNECT") {
props.Set(DoSwitchKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "ENABLE_RS_CACHE") {
props.Set(EnRsCacheKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "EP_SELECTION") {
props.Set(EpSelectorKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "ESCAPE_PROCESS") {
props.Set(EscapeProcessKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "IS_BDTA_RS") {
props.Set(IsBdtaRSKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "KEY_WORDS") ||
util.StringUtil.EqualsIgnoreCase(key, "KEYWORDS") {
props.Set(KeywordsKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "LANGUAGE") {
props.Set(LanguageKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "LOB_MODE") {
props.Set(LobModeKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "LOG_DIR") {
props.Set(LogDirKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "LOG_FLUSH_FREQ") {
props.Set(LogFlushFreqKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "LOG_LEVEL") {
props.Set(LogLevelKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "LOGIN_DSC_CTRL") {
props.Set(LoginDscCtrlKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "LOGIN_ENCRYPT") {
props.Set(LoginEncryptKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "LOGIN_MODE") {
props.Set(LoginModeKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "LOGIN_STATUS") {
props.Set(LoginStatusKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "MAX_ROWS") {
props.Set(MaxRowsKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "MPP_LOCAL") {
props.Set(MppLocalKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "OS_NAME") {
props.Set(OsNameKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "RS_CACHE_SIZE") {
props.Set(RsCacheSizeKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "RS_REFRESH_FREQ") {
props.Set(RsRefreshFreqKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "RW_HA") {
props.Set(RwHAKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "RW_IGNORE_SQL") {
props.Set(RwIgnoreSqlKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "RW_PERCENT") {
props.Set(RwPercentKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "RW_SEPARATE") {
props.Set(RwSeparateKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "RW_STANDBY_RECOVER_TIME") {
props.Set(RwStandbyRecoverTimeKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "SCHEMA") {
props.Set(SchemaKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "SESS_ENCODE") {
if IsSupportedCharset(value) {
props.Set("sessEncode", value)
}
} else if util.StringUtil.EqualsIgnoreCase(key, "SESSION_TIMEOUT") {
props.Set(SessionTimeoutKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "SOCKET_TIMEOUT") {
props.Set(SocketTimeoutKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "SSL_FILES_PATH") {
props.Set(SslFilesPathKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "STAT_DIR") {
props.Set(StatDirKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "STAT_ENABLE") {
props.Set(StatEnableKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "STAT_FLUSH_FREQ") {
props.Set(StatFlushFreqKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "STAT_HIGH_FREQ_SQL_COUNT") {
props.Set(StatHighFreqSqlCountKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "STAT_SLOW_SQL_COUNT") {
props.Set(StatSlowSqlCountKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "STAT_SQL_MAX_COUNT") {
props.Set(StatSqlMaxCountKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "STAT_SQL_REMOVE_MODE") {
props.Set(StatSqlRemoveModeKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "SWITCH_INTERVAL") {
props.Set(SwitchIntervalKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "SWITCH_TIME") ||
util.StringUtil.EqualsIgnoreCase(key, "SWITCH_TIMES") {
props.Set(SwitchTimesKey, value)
} else if util.StringUtil.EqualsIgnoreCase(key, "TIME_ZONE") {
props.Set(TimeZoneKey, value)
props.Set("localTimezone", value)
} else if util.StringUtil.EqualsIgnoreCase(key, "USER_REMAP") {
tmp := props.GetString(UserRemapKey, "")
props.Set(UserRemapKey, tmp+"("+value+")")
} else {
return false
}
return true
}
func parseServerName(name string, value string) *epGroup {
values := strings.Split(value, ",")
var tmpVals []string
var tmpName string
var tmpPort int
var svrList = make([]*ep, 0, len(values))
for _, v := range values {
var tmp *ep
// 先查找IPV6,以[]包括
begin := strings.IndexByte(v, '[')
end := -1
if begin != -1 {
end = strings.IndexByte(v[begin:], ']')
}
if end != -1 {
tmpName = v[begin+1 : end]
// port
if portIndex := strings.IndexByte(v[end:], ':'); portIndex != -1 {
tmpPort, _ = strconv.Atoi(strings.TrimSpace(v[portIndex+1:]))
} else {
tmpPort = int(DEFAULT_PORT)
}
tmp = newEP(tmpName, int32(tmpPort))
svrList = append(svrList, tmp)
continue
}
// IPV4
tmpVals = strings.Split(v, ":")
tmpName = strings.TrimSpace(tmpVals[0])
if len(tmpVals) >= 2 {
tmpPort, _ = strconv.Atoi(tmpVals[1])
} else {
tmpPort = int(DEFAULT_PORT)
}
tmp = newEP(tmpName, int32(tmpPort))
svrList = append(svrList, tmp)
}
if len(svrList) == 0 {
return nil
}
return newEPGroup(name, svrList)
}
func setDriverAttributes(props *Properties) {
if props == nil || props.Len() == 0 {
return
}
parseLanguage(props.GetString(LanguageKey, "cn"))
DbAliveCheckFreq = props.GetInt(DbAliveCheckFreqKey, DbAliveCheckFreqDef, 1, int(INT32_MAX))
//// log
//LogLevel = ParseLogLevel(props)
//LogDir = util.StringUtil.FormatDir(props.GetTrimString(LogDirKey, LogDirDef))
//LogBufferSize = props.GetInt(LogBufferSizeKey, LogBufferSizeDef, 1, int(INT32_MAX))
//LogFlushFreq = props.GetInt(LogFlushFreqKey, LogFlushFreqDef, 1, int(INT32_MAX))
//LogFlushQueueSize = props.GetInt(LogFlusherQueueSizeKey, LogFlushQueueSizeDef, 1, int(INT32_MAX))
//
//// stat
//StatEnable = props.GetBool(StatEnableKey, StatEnableDef)
//StatDir = util.StringUtil.FormatDir(props.GetTrimString(StatDirKey, StatDirDef))
//StatFlushFreq = props.GetInt(StatFlushFreqKey, StatFlushFreqDef, 1, int(INT32_MAX))
//StatHighFreqSqlCount = props.GetInt(StatHighFreqSqlCountKey, StatHighFreqSqlCountDef, 0, 1000)
//StatSlowSqlCount = props.GetInt(StatSlowSqlCountKey, StatSlowSqlCountDef, 0, 1000)
//StatSqlMaxCount = props.GetInt(StatSqlMaxCountKey, StatSqlMaxCountDef, 0, 100000)
//parseStatSqlRemoveMode(props)
}
func parseLanguage(value string) {
if util.StringUtil.EqualsIgnoreCase("cn", value) {
Locale = 0
} else if util.StringUtil.EqualsIgnoreCase("en", value) {
Locale = 1
}
}
func IsSupportedCharset(charset string) bool {
if util.StringUtil.EqualsIgnoreCase(ENCODING_UTF8, charset) || util.StringUtil.EqualsIgnoreCase(ENCODING_GB18030, charset) || util.StringUtil.EqualsIgnoreCase(ENCODING_EUCKR, charset) {
return true
}
return false
}
func ParseLogLevel(props *Properties) int {
logLevel := LOG_OFF
value := props.GetString(LogLevelKey, "")
if value != "" && !util.StringUtil.IsDigit(value) {
if util.StringUtil.EqualsIgnoreCase("debug", value) {
logLevel = LOG_DEBUG
} else if util.StringUtil.EqualsIgnoreCase("info", value) {
logLevel = LOG_INFO
} else if util.StringUtil.EqualsIgnoreCase("sql", value) {
logLevel = LOG_SQL
} else if util.StringUtil.EqualsIgnoreCase("warn", value) {
logLevel = LOG_WARN
} else if util.StringUtil.EqualsIgnoreCase("error", value) {
logLevel = LOG_ERROR
} else if util.StringUtil.EqualsIgnoreCase("off", value) {
logLevel = LOG_OFF
} else if util.StringUtil.EqualsIgnoreCase("all", value) {
logLevel = LOG_ALL
}
} else {
logLevel = props.GetInt(LogLevelKey, logLevel, LOG_OFF, LOG_INFO)
}
return logLevel
}