forked from benapetr/wikimedia-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySQL.cs
343 lines (324 loc) · 12.4 KB
/
MySQL.cs
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
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// Created by Petr Bena <benapetr@gmail.com>
using MySql.Data.MySqlClient;
using System.Xml;
using System;
using System.Xml.Serialization;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Text;
namespace wmib
{
/// <summary>
/// Mysql
/// </summary>
public class WMIBMySQL : Database
{
[Serializable]
public class Unwritten
{
public List<SerializedRow> PendingRows = new List<SerializedRow>();
}
[Serializable]
public class SerializedRow
{
public Row row;
public string table;
public SerializedRow()
{
row = null;
table = null;
}
public SerializedRow(string name, Row _row)
{
row = _row;
table = name;
}
}
private Thread reco = null;
private bool Recovering = false;
private Unwritten unwritten = new Unwritten();
private MySql.Data.MySqlClient.MySqlConnection Connection = null;
/// <summary>
/// Return true if mysql is connected to server
/// </summary>
public override bool IsConnected
{
get
{
return connected;
}
}
private bool connected = false;
public WMIBMySQL()
{
string file = Variables.ConfigurationDirectory + Path.DirectorySeparatorChar + "unwrittensql.xml";
Core.RecoverFile(file);
if (File.Exists(file))
{
Syslog.WarningLog("There is a mysql dump file from previous run containing mysql rows that were never successfuly inserted, trying to recover them");
XmlDocument document = new XmlDocument();
TextReader sr = new StreamReader(file);
document.Load(sr);
XmlNodeReader reader = new XmlNodeReader(document.DocumentElement);
XmlSerializer xs = new XmlSerializer(typeof(Unwritten));
Unwritten un = (Unwritten)xs.Deserialize(reader);
reader.Close();
sr.Close();
lock (unwritten.PendingRows)
{
unwritten.PendingRows.AddRange(un.PendingRows);
}
}
reco = new Thread(Exec);
reco.Name = "Recovery";
Core.ThreadManager.RegisterThread(reco);
reco.Start();
}
private void Exec()
{
try
{
Thread.Sleep(8000);
if (unwritten.PendingRows.Count == 0 && File.Exists (Variables.ConfigurationDirectory +
Path.DirectorySeparatorChar + "unwrittensql.xml"))
{
File.Delete (Variables.ConfigurationDirectory + Path.DirectorySeparatorChar + "unwrittensql.xml");
}
while (Core.IsRunning)
{
if (unwritten.PendingRows.Count > 0)
{
int count = 0;
Syslog.WarningLog("Performing recovery of " + unwritten.PendingRows.Count.ToString() + " MySQL rows");
Recovering = true;
List<SerializedRow> rows = new List<SerializedRow>();
lock (unwritten.PendingRows)
{
count = unwritten.PendingRows.Count;
rows.AddRange(unwritten.PendingRows);
unwritten.PendingRows.Clear();
}
int recovered = 0;
foreach (SerializedRow row in rows)
{
if (InsertRow(row.table, row.row))
{
recovered++;
} else
{
Syslog.DebugLog("Failed to recover 1 row", 2);
}
}
Syslog.WarningLog("Recovery finished, recovered " + recovered.ToString() + " of total " + count.ToString());
Recovering = false;
FlushRows();
Thread.Sleep(200000);
if (unwritten.PendingRows.Count == 0 && File.Exists(Variables.ConfigurationDirectory +
Path.DirectorySeparatorChar + "unwrittensql.xml"))
{
File.Delete(Variables.ConfigurationDirectory + Path.DirectorySeparatorChar + "unwrittensql.xml");
}
}
Thread.Sleep(200);
}
} catch (Exception fail)
{
Core.HandleException(fail);
Syslog.ErrorLog("Recovery thread for Mysql is down");
}
}
public override string Select(string table, string rows, string query, int columns, char separator = '|')
{
string sql = "";
string result = "";
lock (DatabaseLock)
{
if (!IsConnected)
{
ErrorBuffer = "Not connected";
return null;
}
sql = "SELECT " + rows + " FROM " + table + " " + query;
MySqlCommand xx = Connection.CreateCommand();
xx.CommandText = sql;
MySqlDataReader r = xx.ExecuteReader();
while (r.Read())
{
int i = 0;
while (i < columns)
{
if (result == "")
{
result += r.GetString(i);
}
else
{
result += separator.ToString() + r.GetString(i);
}
i++;
}
}
return result;
}
}
/// <summary>
/// Insert row
/// </summary>
/// <param name="table"></param>
/// <param name="row"></param>
/// <returns></returns>
public override bool InsertRow(string table, Row row)
{
string sql = "";
lock(DatabaseLock)
{
try
{
if (!IsConnected)
{
Syslog.DebugLog("Postponing request to insert a row into database which is not connected");
lock(unwritten.PendingRows)
{
unwritten.PendingRows.Add(new SerializedRow(table, row));
}
FlushRows();
return false;
}
MySqlCommand xx = Connection.CreateCommand();
sql = "INSERT INTO " + table + " VALUES (";
foreach (Database.Row.Value value in row.Values)
{
switch (value.Type)
{
case DataType.Boolean:
case DataType.Integer:
sql += value.Data + ", ";
break;
case DataType.Varchar:
case DataType.Text:
case DataType.Date:
sql += "'" + MySql.Data.MySqlClient.MySqlHelper.EscapeString(value.Data) + "', ";
break;
}
}
if (sql.EndsWith(", "))
{
sql = sql.Substring(0, sql.Length - 2);
}
sql += ");";
xx.CommandText = sql;
xx.ExecuteNonQuery();
return true;
} catch (MySqlException me)
{
ErrorBuffer = me.Message;
Syslog.Log("Error while storing a row to DB " + me.ToString(), true);
Syslog.DebugLog("SQL: " + sql);
lock(unwritten.PendingRows)
{
unwritten.PendingRows.Add(new SerializedRow(table, row));
}
FlushRows();
return false;
}
}
}
public override int CacheSize()
{
return unwritten.PendingRows.Count;
}
private void FlushRows()
{
if (Recovering)
{
return;
}
// prevent multiple threads calling this function at same time
lock(this)
{
string file = Variables.ConfigurationDirectory + Path.DirectorySeparatorChar + "unwrittensql.xml";
if (File.Exists(file))
{
Core.BackupData(file);
if (!File.Exists(Configuration.TempName(file)))
{
Syslog.WarningLog("Unable to create backup file for " + file);
return;
}
}
try
{
File.Delete(file);
XmlSerializer xs = new XmlSerializer(typeof(Unwritten));
StreamWriter writer = File.AppendText(file);
lock(unwritten)
{
xs.Serialize(writer, unwritten);
}
writer.Close();
if (File.Exists(Configuration.TempName(file)))
{
File.Delete(Configuration.TempName(file));
}
} catch (Exception fail)
{
Core.HandleException(fail);
Syslog.WarningLog("Recovering the mysql unwritten dump because of exception to: " + file);
Core.RecoverFile(file);
}
}
}
/// <summary>
/// Disconnect mysql
/// </summary>
public override void Disconnect()
{
lock (DatabaseLock)
{
if (IsConnected)
{
Connection.Close();
connected = false;
}
}
}
/// <summary>
/// Connect mysql
/// </summary>
public override void Connect()
{
lock (DatabaseLock)
{
if (IsConnected)
{
return;
}
try
{
Connection = new MySql.Data.MySqlClient.MySqlConnection("Server=" + Configuration.MySQL.MysqlHost + ";" +
"Database=" + Configuration.MySQL.Mysqldb + ";" +
"User ID=" + Configuration.MySQL.MysqlUser + ";" +
"Password=" + Configuration.MySQL.MysqlPw + ";" +
"port=" + Configuration.MySQL.MysqlPort + ";" +
"Pooling=false");
Connection.Open();
connected = true;
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
Syslog.Log("MySQL: Unable to connect to server: " + ex.ToString(), true);
connected = false;
}
}
}
}
}