-
Notifications
You must be signed in to change notification settings - Fork 1
/
blem_lib.go
200 lines (162 loc) · 4.96 KB
/
blem_lib.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
package main
import (
"database/sql"
"golang.org/x/net/context"
"github.com/siddontang/go-mysql/client"
"github.com/siddontang/go-mysql/mysql"
"github.com/siddontang/go-mysql/replication"
)
/*
Map key structure for holding stats per event
*/
type TypeKeyEvent struct {
TableID uint64
//Table string // Slices can't be on the index key []byte
//Schema string //[]byte
Event replication.EventType
}
/*
Map key for the Table mapping
*/
type TypeTableName struct {
Table string
Schema string
}
/*
Map value for per event stats
*/
type TypeDataEvent struct {
AccumSize uint64
Counted uint64
}
func IsValidMode(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func getCoordinates(conn *client.Conn) (p mysql.Position) {
r, _ := conn.Execute("SHOW MASTER STATUS")
binFile, _ := r.GetString(0, 0)
binPos, _ := r.GetInt(0, 1)
p.Name = binFile
p.Pos = uint32(binPos)
return p
}
/*
Standard feedingThread using Maps. Will be deprecated as I'm planning to use SQLite
for a more extendeable aggregations.
*/
func feedingThread(streamer *replication.BinlogStreamer, TableMap map[uint64]TypeTableName, MapStats map[TypeKeyEvent]TypeDataEvent) {
for {
ev, _ := streamer.GetEvent(context.Background())
switch ev.Header.EventType {
case replication.WRITE_ROWS_EVENTv0,
replication.UPDATE_ROWS_EVENTv0,
replication.DELETE_ROWS_EVENTv0,
replication.WRITE_ROWS_EVENTv1,
replication.DELETE_ROWS_EVENTv1,
replication.UPDATE_ROWS_EVENTv1,
replication.WRITE_ROWS_EVENTv2,
replication.UPDATE_ROWS_EVENTv2,
replication.DELETE_ROWS_EVENTv2:
tableMap_ := (*replication.TableMapEvent)(ev.Event.(*replication.RowsEvent).Table)
key_ := TypeKeyEvent{tableMap_.TableID, ev.Header.EventType}
TableMap[key_.TableID] = TypeTableName{string(tableMap_.Schema), string(tableMap_.Table)}
//MapTable := ev.Event.(*replication.RowEvent).tables
bufferStats := MapStats[key_]
bufferStats.Counted++
bufferStats.AccumSize = bufferStats.AccumSize + (uint64)(ev.Header.EventSize)
MapStats[key_] = TypeDataEvent{bufferStats.AccumSize, bufferStats.Counted}
//case replication.QUERY_EVENT:
// need a new map or implement the SQLite
// ev.Event.(*replication.RowEvent).Query
// ev.Event.(*replication.RowEvent).Schema
}
}
}
/*
Standard feedingThread using Maps. Will be deprecated as I'm planning to use SQLite
for a more extendeable aggregations.
DB is *driver.Conn type
We keep TableMap map[uint64]TypeTableName for other parts of the code
statsdb := InitDB(*dbpath)
defer statsdb.Close()
InitTables(statsdb)
go feedSQLiteThread(streamer, statsdb, TableMap)
if *removeStatsDB {
statsdb.Close()
os.Remove(*dbpath)
}
*/
func feedSQLiteThread(streamer *replication.BinlogStreamer, DB *sql.DB, TableMap map[uint64]TypeTableName) {
for {
ev, _ := streamer.GetEvent(context.Background())
switch ev.Header.EventType {
case replication.WRITE_ROWS_EVENTv0,
replication.UPDATE_ROWS_EVENTv0,
replication.DELETE_ROWS_EVENTv0,
replication.WRITE_ROWS_EVENTv1,
replication.DELETE_ROWS_EVENTv1,
replication.UPDATE_ROWS_EVENTv1,
replication.WRITE_ROWS_EVENTv2,
replication.UPDATE_ROWS_EVENTv2,
replication.DELETE_ROWS_EVENTv2:
tableMap_ := (*replication.TableMapEvent)(ev.Event.(*replication.RowsEvent).Table)
key_ := TypeKeyEvent{tableMap_.TableID, ev.Header.EventType}
TableMap[key_.TableID] = TypeTableName{string(tableMap_.Schema), string(tableMap_.Table)}
insertTableMap(TypeTableName{string(tableMap_.Schema), string(tableMap_.Table)}, DB)
//MapTable := ev.Event.(*replication.RowEvent).tables
//bufferStats.Counted++
//bufferStats.AccumSize = bufferStats.AccumSize + (uint64)(ev.Header.EventSize)
}
}
}
// Stealing Code section :p . Taken from siddontang's go-mysql package.
/*
I want to support almost all the events in a pluggable way.
case QUERY_EVENT:
e = &QueryEvent{}
case XID_EVENT:
e = &XIDEvent{}
case TABLE_MAP_EVENT:
te := &TableMapEvent{}
if p.format.EventTypeHeaderLengths[TABLE_MAP_EVENT-1] == 6 {
te.tableIDSize = 4
} else {
te.tableIDSize = 6
}
e = te
case WRITE_ROWS_EVENTv0,
UPDATE_ROWS_EVENTv0,
DELETE_ROWS_EVENTv0,
WRITE_ROWS_EVENTv1,
DELETE_ROWS_EVENTv1,
UPDATE_ROWS_EVENTv1,
WRITE_ROWS_EVENTv2,
UPDATE_ROWS_EVENTv2,
DELETE_ROWS_EVENTv2:
e = p.newRowsEvent(h)
case ROWS_QUERY_EVENT:
e = &RowsQueryEvent{}
case GTID_EVENT:
e = >IDEvent{}
case BEGIN_LOAD_QUERY_EVENT:
e = &BeginLoadQueryEvent{}
case EXECUTE_LOAD_QUERY_EVENT:
e = &ExecuteLoadQueryEvent{}
case MARIADB_ANNOTATE_ROWS_EVENT:
e = &MariadbAnnotaeRowsEvent{}
case MARIADB_BINLOG_CHECKPOINT_EVENT:
e = &MariadbBinlogCheckPointEvent{}
case MARIADB_GTID_LIST_EVENT:
e = &MariadbGTIDListEvent{}
case MARIADB_GTID_EVENT:
ee := &MariadbGTIDEvent{}
ee.GTID.ServerID = h.ServerID
e = ee
default:
e = &GenericEvent{}
*/