-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhooks.go
173 lines (154 loc) · 4.39 KB
/
hooks.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
package coredns_mysql_extend
import (
"database/sql"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
)
func (m *Mysql) rePing() {
for {
if err := m.db.Ping(); err != nil {
time.Sleep(m.failHeartbeatTime)
m.db.Close()
newDB, err := m.openDB()
if err == nil {
m.db = newDB
}
logger.Errorf("Failed to ping database: %s", err)
dbPingCount.With(prometheus.Labels{"status": "fail"}).Inc()
continue
}
time.Sleep(m.successHeartbeatTime)
logger.Debug("Success to ping database")
dbPingCount.With(prometheus.Labels{"status": "success"}).Inc()
}
}
func (m *Mysql) reGetZone() {
for {
zoneMap := make(map[string]int, 0)
rows, err := m.db.Query(m.queryZoneSQL)
if err != nil {
logger.Errorf("Failed to query zones: %s", err)
dbGetZoneCount.With(prometheus.Labels{"status": "fail"}).Inc()
time.Sleep(m.failHeartbeatTime)
continue
}
for rows.Next() {
var zoneRecord zoneRecord
err := rows.Scan(&zoneRecord.id, &zoneRecord.name)
if err != nil {
logger.Error(err)
}
zoneMap[zoneRecord.name] = zoneRecord.id
}
m.zoneMap = zoneMap
logger.Debugf("Success to query zones: %#v", zoneMap)
dbGetZoneCount.With(prometheus.Labels{"status": "success"}).Inc()
time.Sleep(m.successHeartbeatTime)
}
}
func (m *Mysql) loadLocalData() {
cache := make(map[record]dnsRecordInfo, zero)
m.degradeCache = cache
pureRecords := make([]pureRecord, zero)
content, err := os.ReadFile(m.dumpFile)
if err != nil {
logger.Errorf("Failed to load data from file: %s", err)
loadLocalData.With(prometheus.Labels{"status": "fail"}).Inc()
return
}
err = json.Unmarshal(content, &pureRecords)
if err != nil {
logger.Errorf("Failed to load data from file: %s", err)
loadLocalData.With(prometheus.Labels{"status": "fail"}).Inc()
return
}
for _, rMap := range pureRecords {
for queryKey, rrStrings := range rMap {
var response []dns.RR
queryKeySlice := strings.Split(queryKey, keySeparator)
fqdn, qType := queryKeySlice[0], queryKeySlice[1]
record := record{fqdn: fqdn, qType: qType}
for _, rrString := range rrStrings {
rr, err := dns.NewRR(rrString)
if err != nil {
continue
}
response = append(response, rr)
}
dnsRecordInfo := dnsRecordInfo{rrStrings: rrStrings, response: response}
cache[record] = dnsRecordInfo
}
}
// TODO add lock
logger.Debugf("Load degrade data from local file %#v", cache)
loadLocalData.With(prometheus.Labels{"status": "success"}).Inc()
m.degradeCache = cache
}
func (m *Mysql) dump2LocalData() {
pureRecord := make([]pureRecord, zero)
for record, dnsRecordInfo := range m.degradeCache {
logger.Debugf("Record %#v", record)
pureRecord = append(pureRecord, map[string][]string{
fmt.Sprintf("%s%s%s", record.fqdn, keySeparator, record.qType): dnsRecordInfo.rrStrings,
})
}
content, err := json.Marshal(pureRecord)
if err != nil {
logger.Errorf("Failed to dump data to local: %s", err)
dumpLocalData.With(prometheus.Labels{"status": "fail"}).Inc()
return
}
if err := os.WriteFile(m.dumpFile, content, safeMode); err != nil {
logger.Error(err)
logger.Errorf("Failed to dump data to local: %s", err)
dumpLocalData.With(prometheus.Labels{"status": "fail"}).Inc()
return
}
logger.Debugf("Success to dump data to local: %#v", pureRecord)
dumpLocalData.With(prometheus.Labels{"status": "success"}).Inc()
}
func (m *Mysql) openDB() (*sql.DB, error) {
db, err := sql.Open("mysql", m.dsn)
if err != nil {
openMysqlCount.With(prometheus.Labels{"status": "fail"}).Inc()
logger.Errorf("Failed to open database: %s", err)
} else {
// Config db connection pool
db.SetConnMaxIdleTime(m.connMaxIdleTime)
db.SetConnMaxLifetime(m.connMaxLifetime)
db.SetMaxIdleConns(m.maxIdleConns)
db.SetMaxOpenConns(m.maxOpenConns)
openMysqlCount.With(prometheus.Labels{"status": "success"}).Inc()
logger.Debug("Success to open database")
}
return db, err
}
func (m *Mysql) onStartup() error {
logger.Debug("On start up")
// Initialize database connection pool
db, _ := m.openDB()
m.db = db
// Start rePing loop
go m.rePing()
// start reGetZone loop
go m.reGetZone()
// Load local file data
m.loadLocalData()
// Create tables
m.createTables()
return nil
}
func (m *Mysql) onShutdown() error {
logger.Debug("on shutdown")
if m.db != nil {
m.db.Close()
}
// Dump memory data to local file
m.dump2LocalData()
return nil
}