forked from levinsv/pgadmin3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslNode.cpp
499 lines (414 loc) · 15.3 KB
/
slNode.cpp
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2016, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// slNode.cpp PostgreSQL Slony-I node
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "schema/pgObject.h"
#include "slony/slNode.h"
#include "slony/slCluster.h"
#include "slony/slPath.h"
#include "slony/slListen.h"
#include "slony/slSet.h"
#include "frm/frmMain.h"
slNode::slNode(slCluster *cl, const wxString &newName)
: slObject(cl, nodeFactory, newName)
{
pid = -1;
}
int slNode::GetIconId()
{
if (!GetActive())
return nodeFactory.GetDisabledIconId();
else if (GetSlId() == GetCluster()->GetLocalNodeID())
return nodeFactory.GetLocalIconId();
else
return nodeFactory.GetIconId();
}
wxMenu *slNode::GetNewMenu()
{
wxMenu *menu = pgObject::GetNewMenu();
// if (GetCreatePrivilege())
pathFactory.AppendMenu(menu);
listenFactory.AppendMenu(menu);
return menu;
}
bool slNode::CanDrop()
{
return (GetSlId() != GetCluster()->GetLocalNodeID());
}
bool slNode::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
{
return GetDatabase()->ExecuteVoid(
wxT("SELECT ") + GetCluster()->GetSchemaPrefix()
+ wxT("dropnode(") + NumToStr(GetSlId()) + wxT(");\n"));
}
wxString slNode::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on Slony node");
message += wxT(" ") + GetName();
break;
case REFRESHINGDETAILS:
message = _("Refreshing Slony node");
message += wxT(" ") + GetName();
break;
case DROPINCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop Slony node \"%s\" including all objects that depend on it?"),
GetFullIdentifier().c_str());
break;
case DROPEXCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop Slony node \"%s\"?"),
GetFullIdentifier().c_str());
break;
case DROPCASCADETITLE:
message = _("Drop Slony node cascaded?");
break;
case DROPTITLE:
message = _("Drop Slony node?");
break;
case PROPERTIESREPORT:
message = _("Slony node properties report");
message += wxT(" - ") + GetName();
break;
case PROPERTIES:
message = _("Slony node properties");
break;
case DDLREPORT:
message = _("Slony node DDL report");
message += wxT(" - ") + GetName();
break;
case DDL:
message = _("Slony node DDL");
break;
case DEPENDENCIESREPORT:
message = _("Slony node dependencies report");
message += wxT(" - ") + GetName();
break;
case DEPENDENCIES:
message = _("Slony node dependencies");
break;
case DEPENDENTSREPORT:
message = _("Slony node dependents report");
message += wxT(" - ") + GetName();
break;
case DEPENDENTS:
message = _("Slony node dependents");
break;
}
return message;
}
bool slNode::WaitForEvent(long evNode)
{
return true;
}
wxString slNode::GetSql(ctlTree *browser)
{
if (sql.IsNull())
{
sql = wxT("-- Create replication node ") + GetName() + wxT(".\n\n")
wxT("SELECT ") + GetCluster()->GetSchemaPrefix() + wxT("storenode(")
+ NumToStr(GetSlId()) + wxT(", ")
+ qtDbString(GetComment());
if (GetCluster()->ClusterMinimumVersion(1, 1) && !GetCluster()->ClusterMinimumVersion(2, 0))
sql += wxT(", ") + BoolToStr(GetSpool());
sql += wxT(");\n");
}
return sql;
}
long slNode::GetOutstandingAcks()
{
long l = StrToLong(GetDatabase()->ExecuteScalar(
wxT("SELECT SUM(st_lag_num_events) AS sumlagevents\n")
wxT(" FROM ") + GetCluster()->GetSchemaPrefix() + wxT("sl_status\n")
wxT(" WHERE st_origin = ") + NumToStr(GetCluster()->GetLocalNodeID()) + wxT("\n")
wxT(" GROUP BY st_last_event")));
return l;
}
bool slNode::CheckAcksAndContinue(wxFrame *frame)
{
long l = GetOutstandingAcks();
if (!l)
return true;
wxMessageDialog dlg(frame, wxString::Format(wxPLURAL("There are %ld event acknowledgement outstanding.\nContinue anyway?",
"There are %ld event acknowledgements outstanding.\nContinue anyway?", l), l),
_("Events pending"), wxYES_NO | wxNO_DEFAULT);
return dlg.ShowModal() == wxID_YES;
}
void slNode::ShowStatistics(frmMain *form, ctlListView *statistics)
{
CreateListColumns(statistics, _("Statistic"), _("Value"));
if (GetActive())
{
if (GetCluster()->GetLocalNodeID() == GetSlId())
{
pgSet *stats = GetDatabase()->ExecuteSet(
wxT("SELECT st_last_event,\n")
wxT(" MAX(st_last_received_ts - st_last_received_event_ts) AS roundtrip,\n")
wxT(" SUM(st_lag_num_events) AS sumlagevents, st_last_event - MAX(st_lag_num_events) as oldestlagevent,\n")
wxT(" MAX(st_last_event_ts - st_last_received_ts) AS maxeventlag\n")
wxT(" FROM ") + GetCluster()->GetSchemaPrefix() + wxT("sl_status\n")
wxT(" WHERE st_origin = ") + NumToStr(GetCluster()->GetLocalNodeID()) + wxT("\n")
wxT(" GROUP BY st_last_event"));
if (stats)
{
wxString roundtrip = stats->GetVal(wxT("roundtrip"));
long lags = stats->GetLong(wxT("sumlagevents"));
if (roundtrip.Left(6) == wxT("00:00:"))
roundtrip = NumToStr(StrToLong(roundtrip.Mid(6))) + roundtrip.Mid(8) + wxT(" s");
else if (roundtrip.Left(3) == wxT("00:"))
roundtrip = NumToStr(StrToLong(roundtrip.Mid(3))) + roundtrip.Mid(5);
statistics->AppendItem(_("Last event"), stats->GetLong(wxT("st_last_event")));
statistics->AppendItem(_("Max response time"), roundtrip);
statistics->AppendItem(_("Acks outstanding"), lags);
if (lags > 0)
{
long lagEvent = stats->GetLong(wxT("oldestlagevent"));
statistics->AppendItem(_("Oldest outstanding"), lagEvent);
statistics->AppendItem(_("Outstanding for"), stats->GetVal(wxT("maxeventlag")));
}
delete stats;
}
}
else
{
pgSet *stats = GetDatabase()->ExecuteSet(
wxT("SELECT st_last_event, st_last_event_ts, st_last_received, st_last_received_ts,\n")
wxT(" st_last_received_ts - st_last_received_event_ts AS roundtrip,\n")
wxT(" st_last_event_ts - st_last_received_ts AS eventlag,")
wxT(" ev_seqno, ev_type || ' ' || COALESCE(ev_data1, '') AS hanging\n")
wxT(" FROM ") + GetCluster()->GetSchemaPrefix() + wxT("sl_status\n")
wxT(" LEFT JOIN ") + GetCluster()->GetSchemaPrefix() + wxT("sl_event ON ev_origin=st_origin AND ev_seqno=\n")
wxT(" (SELECT MIN(ev_seqno) FROM ") + GetCluster()->GetSchemaPrefix() + wxT("sl_event WHERE ev_seqno > st_last_received)\n")
wxT(" WHERE st_origin = ") + NumToStr(GetCluster()->GetLocalNodeID()) + wxT("\n")
wxT(" AND st_received = ") + NumToStr(GetSlId()));
if (stats)
{
long evno = stats->GetLong(wxT("st_last_event"));
long ack = stats->GetLong(wxT("st_last_received"));
wxString roundtrip = stats->GetVal(wxT("roundtrip"));
if (roundtrip.Left(6) == wxT("00:00:"))
roundtrip = NumToStr(StrToLong(roundtrip.Mid(6))) + roundtrip.Mid(8) + wxT(" s");
else if (roundtrip.Left(3) == wxT("00:"))
roundtrip = NumToStr(StrToLong(roundtrip.Mid(3))) + roundtrip.Mid(5);
statistics->AppendItem(_("Last event"), evno);
statistics->AppendItem(_("Last event timestamp"), stats->GetDateTime(wxT("st_last_event_ts")));
statistics->AppendItem(_("Last acknowledged"), ack);
statistics->AppendItem(_("Last ack timestamp"), stats->GetDateTime(wxT("st_last_received_ts")));
statistics->AppendItem(_("Last response time"), roundtrip);
if (evno > ack)
{
statistics->AppendItem(_("Outstanding acks"), evno - ack);
statistics->AppendItem(_("No ack for"), stats->GetVal(wxT("eventlag")));
statistics->AppendItem(_("Hanging event"), stats->GetVal(wxT("ev_seqno")));
statistics->AppendItem(_("Command"), stats->GetVal(wxT("hanging")));
}
delete stats;
}
}
}
else
statistics->AppendItem(_("not active"), wxEmptyString);
}
void slNode::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
{
wxString pidcol = wxEmptyString;
pgConn *conn = GetCluster()->GetNodeConn(form, GetSlId(), pid < 0);
if (!expandedKids)
{
expandedKids = true;
browser->RemoveDummyChild(this);
// Log
wxLogInfo(wxT("Adding child object to node %s"), GetIdentifier().c_str());
browser->AppendCollection(this, pathFactory);
browser->AppendCollection(this, listenFactory);
}
if (properties)
{
CreateListColumns(properties);
properties->AppendItem(_("Name"), GetName());
properties->AppendItem(_("ID"), GetSlId());
if (GetCluster()->GetLocalNodeID() == GetSlId())
properties->AppendYesNoItem(_("Local node"), true);
properties->AppendYesNoItem(_("Active"), GetActive());
if (GetCluster()->ClusterMinimumVersion(1, 1))
properties->AppendYesNoItem(_("Log spooler"), GetSpool());
properties->AppendYesNoItem(_("Connected"), conn != NULL);
properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));
if (conn && pid < 0)
{
if (conn->BackendMinimumVersion(9, 0))
{
if (conn->BackendMinimumVersion(9, 2))
pidcol = wxT("pid");
else
pidcol = wxT("procpid");
pid = StrToLong(conn->ExecuteScalar(
wxT("SELECT nl_backendpid FROM ") + qtIdent(wxT("_") + GetCluster()->GetName()) + wxT(".sl_nodelock nl, ")
wxT("pg_stat_activity sa WHERE nl.nl_backendpid = sa.") + pidcol + wxT(" AND nl_nodeid = ")
+ NumToStr(GetCluster()->GetLocalNodeID())));
}
else
{
pid = StrToLong(conn->ExecuteScalar(
wxT("SELECT listenerpid FROM pg_listener WHERE relname=") + qtDbString(wxT("_") + GetCluster()->GetName() + wxT("_Restart"))));
}
}
if (conn)
{
if (GetPid())
properties->AppendItem(_("Running PID"), GetPid());
else
properties->AppendItem(_("Running PID"), _("not running"));
}
else
{
if (GetSlId() != GetCluster()->GetAdminNodeID())
properties->AppendItem(_("Running PID"), _("administrative node"));
}
}
}
pgObject *slNode::Refresh(ctlTree *browser, const wxTreeItemId item)
{
pgObject *node = 0;
pgCollection *coll = browser->GetParentCollection(item);
if (coll)
node = nodeFactory.CreateObjects(coll, 0, wxT(" WHERE no_id=") + NumToStr(GetSlId()) + wxT("\n"));
return node;
}
pgObject *slNodeFactory::CreateObjects(pgCollection *coll, ctlTree *browser, const wxString &restriction)
{
slObjCollection *collection = (slObjCollection *)coll;
slNode *node = 0;
pgSet *nodes = collection->GetDatabase()->ExecuteSet(
wxT("SELECT * FROM ") + collection->GetCluster()->GetSchemaPrefix() + wxT("sl_node\n")
+ restriction +
wxT(" ORDER BY no_id"));
if (nodes)
{
while (!nodes->Eof())
{
node = new slNode(collection->GetCluster(), nodes->GetVal(wxT("no_comment")).BeforeFirst('\n'));
node->iSetSlId(nodes->GetLong(wxT("no_id")));
node->iSetActive(nodes->GetBool(wxT("no_active")));
node->iSetComment(nodes->GetVal(wxT("no_comment")));
if (collection->GetCluster()->ClusterMinimumVersion(1, 1))
{
if (nodes->HasColumn(wxT("no_spool")))
node->iSetSpool(nodes->GetBool(wxT("no_spool")));
}
if (browser)
{
browser->AppendObject(collection, node);
nodes->MoveNext();
}
else
break;
}
delete nodes;
}
return node;
}
wxString slNodeCollection::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on Slony nodes");
break;
case REFRESHINGDETAILS:
message = _("Refreshing Slony nodes");
break;
case OBJECTSLISTREPORT:
message = _("Slony nodes list report");
break;
}
return message;
}
void slNodeCollection::ShowStatistics(frmMain *form, ctlListView *statistics)
{
wxLogInfo(wxT("Displaying statistics for nodes on Cluster %s"), GetCluster()->GetIdentifier().c_str());
// Add the statistics view columns
statistics->ClearAll();
statistics->AddColumn(_("Node"), 50);
statistics->AddColumn(_("Roundtrip"), 50);
statistics->AddColumn(_("Acks outstanding"), 50);
statistics->AddColumn(_("Outstanding time"), 50);
statistics->AddColumn(_("Event No"), 50);
statistics->AddColumn(_("Command"), 250);
pgSet *stats = GetDatabase()->ExecuteSet(
wxT("SELECT st_received, st_last_event, st_lag_num_events, st_last_event_ts, st_last_received, st_last_received_ts,\n")
wxT(" st_last_received_ts - st_last_received_event_ts AS roundtrip,\n")
wxT(" CASE WHEN st_lag_num_events > 0 THEN st_last_event_ts - st_last_received_ts END AS eventlag,")
wxT(" ev_seqno, ev_type || ' ' || COALESCE(ev_data1, '') AS hanging\n")
wxT(" FROM ") + GetCluster()->GetSchemaPrefix() + wxT("sl_status\n")
wxT(" LEFT JOIN ") + GetCluster()->GetSchemaPrefix() + wxT("sl_event ON ev_origin=st_origin AND ev_seqno=\n")
wxT(" (SELECT MIN(ev_seqno) FROM ") + GetCluster()->GetSchemaPrefix() + wxT("sl_event WHERE ev_seqno > st_last_received)\n")
wxT(" WHERE st_origin = ") + NumToStr(GetCluster()->GetLocalNodeID()));
if (stats)
{
long pos = 0;
while (!stats->Eof())
{
long lagEvents = stats->GetLong(wxT("st_lag_num_events"));
statistics->InsertItem(pos, NumToStr(stats->GetLong(wxT("st_received"))), nodeFactory.GetIconId());
statistics->SetItem(pos, 1, stats->GetVal(wxT("roundtrip")));
statistics->SetItem(pos, 2, NumToStr(lagEvents));
statistics->SetItem(pos, 3, stats->GetVal(wxT("eventlag")));
if (lagEvents)
{
statistics->SetItem(pos, 4, stats->GetVal(wxT("ev_seqno")));
statistics->SetItem(pos, 5, stats->GetVal(wxT("hanging")));
}
stats->MoveNext();
pos++;
}
delete stats;
}
}
///////////////////////////////////////////////////
#include "images/slnode.pngc"
#include "images/slnode-local.pngc"
#include "images/slnode-disabled.pngc"
#include "images/slnodes.pngc"
slNodeFactory::slNodeFactory()
: slObjFactory(__("Node"), __("New Node"), __("Create a new Node."), slnode_png_img)
{
localIconId = addIcon(slnode_local_png_img);
disabledIconId = addIcon(slnode_disabled_png_img);
metaType = SLM_NODE;
}
pgCollection *slNodeFactory::CreateCollection(pgObject *obj)
{
return new slNodeCollection(GetCollectionFactory(), (slCluster *)obj);
}
slNodeObject::slNodeObject(slNode *n, pgaFactory &factory, const wxString &newName)
: slObject(n->GetCluster(), factory, newName)
{
node = n;
}
slNodeObjCollection::slNodeObjCollection(pgaFactory *factory, slNode *n)
: slObjCollection(factory, n->GetCluster())
{
node = n;
iSetSlId(n->GetSlId());
}
pgCollection *slNodeObjFactory::CreateCollection(pgObject *obj)
{
return new slNodeObjCollection(GetCollectionFactory(), (slNode *)obj);
}
slNodeFactory nodeFactory;
static pgaCollectionFactory cf(&nodeFactory, __("Nodes"), slnodes_png_img);