-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathParallelPipelineExport.aspx.cs
More file actions
238 lines (208 loc) · 7.48 KB
/
Copy pathParallelPipelineExport.aspx.cs
File metadata and controls
238 lines (208 loc) · 7.48 KB
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
using MySqlConnector;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace System.pages
{
public partial class ParallelPipelineExport : System.Web.UI.Page
{
public static int globalRunningTaskId = 0;
public static ConcurrentDictionary<int, ParallelExportInfo> dicParallelExportTask = new ConcurrentDictionary<int, ParallelExportInfo>();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btStart_Click(object sender, EventArgs e)
{
globalRunningTaskId++;
// Fire and forget, run the task asynchronously in background
_ = Task.Run(() => RunTest(globalRunningTaskId, cbParallel.Checked));
panelSetup.Visible = false;
panelResult.Visible = true;
ltTaskId.Text = $"<script>var taskid = {globalRunningTaskId};</script>";
}
int thisTaskId = 0;
ParallelExportInfo p = null;
bool stopUpdateProgress = false;
void RunTest(int _taskid, bool enableParallelProcessing)
{
thisTaskId = _taskid;
stopUpdateProgress = false;
if (enableParallelProcessing)
{
RunParallel();
}
else
{
RunSingleThread();
}
}
void RunSingleThread()
{
string folder = Server.MapPath("~/App_Data/backup");
Directory.CreateDirectory(folder);
string filename = $"export-parallel-singlethread-{DateTime.Now:yyyy-MM-dd_HHmmss}.sql";
string filepath = Path.Combine(folder, filename);
p = new ParallelExportInfo()
{
id = thisTaskId,
filename = filename,
filepath = filepath,
is_completed = false,
percent_complete = 0,
is_parallel = false,
time_start = DateTime.Now
};
dicParallelExportTask[thisTaskId] = p;
try
{
using (var conn = config.GetNewConnection())
using (var cmd = conn.CreateCommand())
using (var mb = new MySqlBackup(cmd))
{
conn.Open();
mb.ExportInfo.RecordDumpTime = false;
mb.ExportInfo.EnableParallelProcessing = false;
mb.ExportInfo.IntervalForProgressReport = 500;
mb.ExportProgressChanged += Mb_ExportProgressChanged;
mb.ExportToFile(filepath);
}
stopUpdateProgress = true;
p.time_end = DateTime.Now;
p.has_error = false;
p.is_completed = true;
p.sha256 = Sha256.Compute(filepath);
if (!p.request_cancel && !p.has_error)
{
p.percent_complete = 100;
p.current_row = p.total_rows;
}
}
catch (Exception ex)
{
p.time_end = DateTime.Now;
p.has_error = true;
p.is_completed = true;
p.error_msg = ex.Message;
}
}
void RunParallel()
{
string folder = Server.MapPath("~/App_Data/backup");
Directory.CreateDirectory(folder);
string filename = $"export-parallel-multithread-{DateTime.Now:yyyy-MM-dd_HHmmss}.sql";
string filepath = Path.Combine(folder, filename);
p = new ParallelExportInfo()
{
id = thisTaskId,
filename = filename,
filepath = filepath,
is_completed = false,
percent_complete = 0,
is_parallel = true,
time_start = DateTime.Now
};
dicParallelExportTask[thisTaskId] = p;
try
{
using (var conn = config.GetNewConnection())
using (var cmd = conn.CreateCommand())
using (var mb = new MySqlBackup(cmd))
{
conn.Open();
mb.ExportInfo.RecordDumpTime = false;
mb.ExportInfo.EnableParallelProcessing = true;
mb.ExportInfo.IntervalForProgressReport = 500;
mb.ExportProgressChanged += Mb_ExportProgressChanged;
mb.ExportToFile(filepath);
}
stopUpdateProgress = true;
p.time_end = DateTime.Now;
p.has_error = false;
p.is_completed = true;
p.sha256 = Sha256.Compute(filepath);
if (!p.request_cancel && !p.has_error)
{
p.percent_complete = 100;
p.current_row = p.total_rows;
}
}
catch (Exception ex)
{
p.time_end = DateTime.Now;
p.has_error = true;
p.is_completed = true;
p.error_msg = ex.Message;
}
}
private void Mb_ExportProgressChanged(object sender, ExportProgressArgs e)
{
if (stopUpdateProgress)
return;
if (p.request_cancel)
{
if (p.is_parallel)
((MySqlBackup)sender).StopAllProcess();
else
((MySqlBackup)sender).StopAllProcess();
return;
}
if (!p.is_completed && !p.has_error)
{
p.current_row = e.CurrentRowIndexInAllTables;
p.total_rows = e.TotalRowsInAllTables;
if (!p.is_completed && e.CurrentRowIndexInAllTables > 0 && e.TotalRowsInAllTables > 0)
{
p.percent_complete = Convert.ToInt32(e.CurrentRowIndexInAllTables * 100L / e.TotalRowsInAllTables);
}
}
}
}
public class ParallelExportInfo
{
public int id { get; set; }
public long total_rows { get; set; }
public long current_row { get; set; }
public int percent_complete { get; set; }
public string filename { get; set; }
public string filepath { get; set; }
public bool is_completed { get; set; }
public bool has_error { get; set; }
public string error_msg { get; set; }
public bool request_cancel { get; set; }
public bool is_parallel { get; set; }
public int api_call_id { get; set; }
public DateTime time_start { get; set; }
public DateTime time_end { get; set; }
public string sha256 { get; set; }
public string time_start_display
{
get
{
return time_start.ToString("yyyy-MM-dd HH:mm:ss tt");
}
}
public string time_end_display
{
get
{
return time_end.ToString("yyyy-MM-dd HH:mm:ss tt");
}
}
[JsonPropertyName("time_used")]
public string time_used
{
get
{
var tu = time_end - time_start;
return $"{tu.Hours}h {tu.Minutes}m {tu.Seconds}s {tu.Milliseconds}ms";
}
}
}
}