Skip to content

Commit fda0a0d

Browse files
authored
Merge pull request #133 from cgalibern/dev
dev
2 parents 0dceba8 + 74f12d1 commit fda0a0d

10 files changed

Lines changed: 216 additions & 116 deletions

cdb/db.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/go-sql-driver/mysql"
14-
"github.com/prometheus/client_golang/prometheus"
1514
)
1615

1716
type (
@@ -35,43 +34,6 @@ type (
3534
Metrics *Metrics
3635
}
3736

38-
Metrics struct {
39-
ExecErr prometheus.Counter
40-
ExecOk prometheus.Counter
41-
BeginTxErr prometheus.Counter
42-
BeginTxOk prometheus.Counter
43-
CommitErr prometheus.Counter
44-
CommitOk prometheus.Counter
45-
RollbackErr prometheus.Counter
46-
RollbackOk prometheus.Counter
47-
ExecTxErr prometheus.Counter
48-
ExecTxOk prometheus.Counter
49-
ExecTxDeadlock prometheus.Counter
50-
51-
ExecTxRetry prometheus.Counter
52-
ExecTxFailed prometheus.Counter
53-
54-
HbStatusUpdate prometheus.Counter
55-
HbStatusLogChange prometheus.Counter
56-
HbStatusLogExtend prometheus.Counter
57-
HbStatusLogInsert prometheus.Counter
58-
59-
ObjectStatusUpdate prometheus.Counter
60-
ObjectStatusLogChange prometheus.Counter
61-
ObjectStatusLogExtend prometheus.Counter
62-
ObjectStatusLogInsert prometheus.Counter
63-
64-
ResourceStatusUpdate prometheus.Counter
65-
ResourceStatusLogChange prometheus.Counter
66-
ResourceStatusLogExtend prometheus.Counter
67-
ResourceStatusLogInsert prometheus.Counter
68-
69-
InstanceStatusUpdate prometheus.Counter
70-
InstanceStatusLogChange prometheus.Counter
71-
InstanceStatusLogExtend prometheus.Counter
72-
InstanceStatusLogInsert prometheus.Counter
73-
}
74-
7537
// DBLocker combines a database connection and a sync.Locker
7638
// for managing concurrent access.
7739
DBLocker struct {

cdb/db_actions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ func (oDb *DB) InsertSvcAction(ctx context.Context, svcID, nodeID uuid.UUID, act
238238
return id, err
239239
} else if rowsAffected > 0 {
240240
oDb.SetChange("svcactions")
241+
oDb.Metrics.InstanceActionInsert.Inc()
241242
}
242243

243244
return id, nil
@@ -260,6 +261,7 @@ func (oDb *DB) UpdateSvcAction(ctx context.Context, svcActionID int64, end time.
260261
return err
261262
} else if rowsAffected > 0 {
262263
oDb.SetChange("svcactions")
264+
oDb.Metrics.InstanceActionUpdate.Inc()
263265
}
264266
return nil
265267
}

cdb/db_dashboard_object_flex.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (oDb *DB) DashboardUpdateObjectFlexStartedBatch(ctx context.Context, l ...*
146146
AND d.dash_type = "flex error"
147147
AND d.dash_fmt LIKE "%%instances started%%"
148148
AND still_errors.svc_id IS NULL`,
149-
placeholders(len(svcIDs)), placeholders(len(svcIDs)))
149+
getPlaceholders(len(svcIDs)), getPlaceholders(len(svcIDs)))
150150

151151
// Combine args for the two IN clauses
152152
deleteArgs := append(svcIDs, svcIDs...)
@@ -158,7 +158,7 @@ func (oDb *DB) DashboardUpdateObjectFlexStartedBatch(ctx context.Context, l ...*
158158
}
159159

160160
// Helper to generate (?, ?, ?) strings
161-
func placeholders(n int) string {
161+
func getPlaceholders(n int) string {
162162
ps := make([]string, n)
163163
for i := range ps {
164164
ps[i] = "?"

cdb/db_diskinfo.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ func (oDb *DB) UpdateDiskinfoArrayID(ctx context.Context, diskID, arrayID string
8080
)
8181
if count, err := oDb.execCountContext(ctx, query, diskID, arrayID); err != nil {
8282
return false, fmt.Errorf("update diskinfo: %w", err)
83-
} else {
84-
return count > 0, nil
83+
} else if count > 0 {
84+
oDb.Metrics.NodeDiskUpdate.Inc()
85+
return true, nil
8586
}
87+
return false, nil
8688
}
8789

8890
func (oDb *DB) UpdateDiskinfoArrayAndDevIDsAndSize(ctx context.Context, diskID, arrayID, devID string, size int32) (bool, error) {
@@ -93,9 +95,11 @@ func (oDb *DB) UpdateDiskinfoArrayAndDevIDsAndSize(ctx context.Context, diskID,
9395
)
9496
if count, err := oDb.execCountContext(ctx, query, diskID, arrayID, devID, size); err != nil {
9597
return false, fmt.Errorf("update diskinfo: %w", err)
96-
} else {
97-
return count > 0, nil
98+
} else if count > 0 {
99+
oDb.Metrics.NodeDiskUpdate.Inc()
100+
return true, nil
98101
}
102+
return false, nil
99103
}
100104

101105
func (oDb *DB) UpdateDiskinfoForDiskSize(ctx context.Context, diskID string, size int32) (bool, error) {
@@ -106,9 +110,11 @@ func (oDb *DB) UpdateDiskinfoForDiskSize(ctx context.Context, diskID string, siz
106110
)
107111
if count, err := oDb.execCountContext(ctx, query, diskID, size); err != nil {
108112
return false, fmt.Errorf("update diskinfo: %w", err)
109-
} else {
110-
return count > 0, nil
113+
} else if count > 0 {
114+
oDb.Metrics.NodeDiskUpdate.Inc()
115+
return true, nil
111116
}
117+
return false, nil
112118
}
113119

114120
func (oDb *DB) UpdateDiskinfoSetMissingArrayID(ctx context.Context, diskID, arrayID string) (bool, error) {

cdb/db_heartbeat.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ func (oDb *DB) HBUpdate(ctx context.Context, l ...*DBHeartbeat) error {
134134
if len(l) == 0 {
135135
return nil
136136
}
137-
oDb.Metrics.HbStatusUpdate.Add(float64(len(l)))
138137
placeholders := strings.Repeat(valueList+", ", len(l)-1) + valueList
139138
args := make([]any, 0, 9*len(l))
140139
for _, v := range l {
@@ -145,11 +144,12 @@ func (oDb *DB) HBUpdate(ctx context.Context, l ...*DBHeartbeat) error {
145144
insertColList, placeholders, onDuplicateAssignment)
146145

147146
// TODO check vs v2
148-
_, err := oDb.ExecContext(ctx, query, args...)
149-
if err != nil {
147+
if count, err := oDb.execCountContext(ctx, query, args...); err != nil {
150148
return err
149+
} else if count > 0 {
150+
oDb.SetChange("hbmon")
151+
oDb.Metrics.HbStatusUpdate.Add(float64(count))
151152
}
152-
oDb.SetChange("hbmon")
153153
return nil
154154
}
155155

@@ -205,7 +205,6 @@ func (oDb *DB) HBLogLastUpdate(ctx context.Context, l ...*DBHeartbeatLog) error
205205
if len(l) == 0 {
206206
return nil
207207
}
208-
oDb.Metrics.HbStatusLogInsert.Add(float64(len(l)))
209208
placeholders := strings.Repeat(valueList+", ", len(l)-1) + valueList
210209

211210
query := fmt.Sprintf("INSERT INTO `hbmon_log_last` %s VALUES %s ON DUPLICATE KEY UPDATE %s", insertColList, placeholders, onDuplicateAssignment)
@@ -223,7 +222,6 @@ func (oDb *DB) HBLogLastExtend(ctx context.Context, l ...*DBHeartbeatLog) error
223222
if len(l) == 0 {
224223
return nil
225224
}
226-
oDb.Metrics.HbStatusLogExtend.Add(float64(len(l)))
227225
placeholders := strings.Repeat("(?,?,?),", len(l)-1) + "(?,?,?)"
228226

229227
query := fmt.Sprintf("UPDATE `hbmon_log_last` SET `end` = NOW() WHERE (`node_id`,`peer_node_id`,`name`) IN %s)", placeholders)
@@ -232,8 +230,12 @@ func (oDb *DB) HBLogLastExtend(ctx context.Context, l ...*DBHeartbeatLog) error
232230
args = append(args, v.NodeID, v.PeerNodeID, v.Name)
233231
}
234232

235-
_, err := oDb.ExecContext(ctx, query, args...)
236-
return err
233+
if count, err := oDb.execCountContext(ctx, query, args...); err != nil {
234+
return err
235+
} else if count > 0 {
236+
oDb.Metrics.HbStatusLogExtend.Add(float64(count))
237+
}
238+
return nil
237239
}
238240

239241
func (oDb *DB) HBLogUpdate(ctx context.Context, l ...*DBHeartbeatLog) error {
@@ -245,7 +247,6 @@ func (oDb *DB) HBLogUpdate(ctx context.Context, l ...*DBHeartbeatLog) error {
245247
if len(l) == 0 {
246248
return nil
247249
}
248-
oDb.Metrics.HbStatusLogChange.Add(float64(len(l)))
249250
placeholders := strings.Repeat(valueList+", ", len(l)-1) + valueList
250251

251252
query := fmt.Sprintf("INSERT INTO `hbmon_log` %s VALUES %s", insertColList, placeholders)
@@ -254,8 +255,12 @@ func (oDb *DB) HBLogUpdate(ctx context.Context, l ...*DBHeartbeatLog) error {
254255
args = append(args, v.ClusterID, v.NodeID, v.PeerNodeID, v.Name, v.State, v.Beating, v.Begin)
255256
}
256257

257-
_, err := oDb.ExecContext(ctx, query, args...)
258-
return err
258+
if count, err := oDb.execCountContext(ctx, query, args...); err != nil {
259+
return err
260+
} else if count > 0 {
261+
oDb.Metrics.HbStatusLogChange.Add(float64(count))
262+
}
263+
return nil
259264
}
260265

261266
func (o *DBHeartbeat) SameAsLog(logStatus *DBHeartbeatLog) bool {

cdb/db_instances.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ func (oDb *DB) SvcmonLogLastUpdate(ctx context.Context, l ...*DBInstanceStatusLo
360360
if len(l) == 0 {
361361
return nil
362362
}
363-
oDb.Metrics.InstanceStatusLogInsert.Add(float64(len(l)))
364363
placeholders := strings.Repeat(valueList+", ", len(l)-1) + valueList
365364

366365
query := fmt.Sprintf("INSERT INTO `svcmon_log_last` %s VALUES %s ON DUPLICATE KEY UPDATE %s", insertColList, placeholders, onDuplicateAssignment)
@@ -379,7 +378,6 @@ func (oDb *DB) SvcmonLogLastExtend(ctx context.Context, nodeID string, objectIDs
379378
if len(objectIDs) == 0 {
380379
return nil
381380
}
382-
oDb.Metrics.InstanceStatusLogExtend.Add(float64(len(objectIDs)))
383381
placeholders := strings.Repeat("?,", len(objectIDs)-1) + "?"
384382

385383
query := fmt.Sprintf("UPDATE `svcmon_log_last` SET `mon_end` = NOW() WHERE `node_id` = ? AND `svc_id` in (%s)", placeholders)
@@ -389,8 +387,12 @@ func (oDb *DB) SvcmonLogLastExtend(ctx context.Context, nodeID string, objectIDs
389387
args[i+1] = v
390388
}
391389

392-
_, err := oDb.ExecContext(ctx, query, args...)
393-
return err
390+
if count, err := oDb.execCountContext(ctx, query, args...); err != nil {
391+
return err
392+
} else if count > 0 {
393+
oDb.Metrics.InstanceStatusLogExtend.Add(float64(count))
394+
}
395+
return nil
394396
}
395397

396398
func (oDb *DB) SvcmonLogUpdate(ctx context.Context, l ...*DBInstanceStatusLog) error {
@@ -403,7 +405,6 @@ func (oDb *DB) SvcmonLogUpdate(ctx context.Context, l ...*DBInstanceStatusLog) e
403405
if len(l) == 0 {
404406
return nil
405407
}
406-
oDb.Metrics.InstanceStatusLogChange.Add(float64(len(l)))
407408
placeholders := strings.Repeat(valueList+", ", len(l)-1) + valueList
408409

409410
query := fmt.Sprintf("INSERT INTO `svcmon_log` %s VALUES %s", insertColList, placeholders)
@@ -413,8 +414,12 @@ func (oDb *DB) SvcmonLogUpdate(ctx context.Context, l ...*DBInstanceStatusLog) e
413414
v.MonFsStatus, v.MonDiskStatus, v.MonShareStatus, v.MonContainerStatus, v.MonAppStatus, v.MonBeginAt)
414415
}
415416

416-
_, err := oDb.ExecContext(ctx, query, args...)
417-
return err
417+
if count, err := oDb.execCountContext(ctx, query, args...); err != nil {
418+
return err
419+
} else if count > 0 {
420+
oDb.Metrics.InstanceStatusLogChange.Add(float64(count))
421+
}
422+
return nil
418423
}
419424

420425
func (oDb *DB) SvcmonUpdate(ctx context.Context, l ...*DBInstanceStatus) error {
@@ -445,7 +450,6 @@ func (oDb *DB) SvcmonUpdate(ctx context.Context, l ...*DBInstanceStatus) error {
445450
if len(l) == 0 {
446451
return nil
447452
}
448-
oDb.Metrics.InstanceStatusUpdate.Add(float64(len(l)))
449453
placeholders := strings.Repeat(valueList+", ", len(l)-1) + valueList
450454
args := make([]any, 0, 17*len(l))
451455
for _, v := range l {
@@ -460,11 +464,12 @@ func (oDb *DB) SvcmonUpdate(ctx context.Context, l ...*DBInstanceStatus) error {
460464
insertColList, placeholders, onDuplicateAssignment)
461465

462466
// TODO check vs v2
463-
_, err := oDb.ExecContext(ctx, query, args...)
464-
if err != nil {
467+
if count, err := oDb.execCountContext(ctx, query, args...); err != nil {
465468
return fmt.Errorf("failed with instance status count %d query len %d: %s: %w", len(l), len(query), query, err)
469+
} else if count > 0 {
470+
oDb.SetChange("svcmon")
471+
oDb.Metrics.InstanceStatusUpdate.Add(float64(count))
466472
}
467-
oDb.SetChange("svcmon")
468473
return nil
469474
}
470475

@@ -485,6 +490,7 @@ func (oDb *DB) DeleteNodeIDResmonInstances(ctx context.Context, nodeID string, o
485490
return fmt.Errorf("DeleteNodeIDResmonInstances %s [%v]: %w", nodeID, objectIDs, err)
486491
} else if count > 0 {
487492
oDb.SetChange("resmon")
493+
oDb.Metrics.ResourceStatusDelete.Add(float64(count))
488494
}
489495
return nil
490496
}
@@ -500,6 +506,9 @@ func (oDb *DB) InstanceResourcesDeleteObsolete(ctx context.Context, svcID, nodeI
500506
return fmt.Errorf("InstanceResourcesDeleteObsolete: %w", err)
501507
} else {
502508
slog.Debug(fmt.Sprintf("InstanceResourcesDeleteObsolete %s@%s: %d", svcID, nodeID, count))
509+
if count > 0 {
510+
oDb.Metrics.ResourceStatusDelete.Add(float64(count))
511+
}
503512
}
504513
return nil
505514
}
@@ -537,10 +546,12 @@ func (oDb *DB) InstanceResourceInfoUpdate(ctx context.Context, svcID, nodeID str
537546
query := fmt.Sprintf("INSERT INTO `resinfo` %s VALUES %s ON DUPLICATE KEY UPDATE %s",
538547
insertColList, placeholders, onDuplicateAssignment)
539548

540-
if _, err := oDb.ExecContext(ctx, query, args...); err != nil {
549+
if count, err := oDb.execCountContext(ctx, query, args...); err != nil {
541550
return err
551+
} else if count > 0 {
552+
oDb.SetChange("resinfo")
553+
oDb.Metrics.ResourceInfoUpdate.Add(float64(count))
542554
}
543-
oDb.SetChange("resinfo")
544555
return nil
545556
}
546557

@@ -551,6 +562,7 @@ func (oDb *DB) InstanceResourceInfoDelete(ctx context.Context, svcID, nodeID str
551562
return fmt.Errorf("query %s: %w", query, err)
552563
} else if count > 0 {
553564
oDb.SetChange("resinfo")
565+
oDb.Metrics.ResourceInfoDelete.Add(float64(count))
554566
}
555567
return nil
556568
}
@@ -637,13 +649,15 @@ func (oDb *DB) InstanceResourceLogUpdate(ctx context.Context, res *DBInstanceRes
637649
if _, err := oDb.ExecContext(ctx, queryExtendIntervalOfCurrent, res.SvcID, res.NodeID, res.RID); err != nil {
638650
return fmt.Errorf("extend services_log_last: %w", err)
639651
}
652+
oDb.Metrics.ResourceStatusLogExtend.Inc()
640653
return nil
641654
} else {
642655
// the avail value will change, save interval of previous status, log value before change
643656
if _, err := oDb.ExecContext(ctx, querySaveIntervalOfPreviousBeforeTransition,
644657
res.SvcID, res.NodeID, res.RID, previousBegin, previousStatus, previousLog); err != nil {
645658
return fmt.Errorf("add services_log change: %w", err)
646659
}
660+
oDb.Metrics.ResourceStatusLogChange.Inc()
647661
// reset begin and end interval for new status, log
648662
return setLogLast()
649663
}

cdb/db_nodes.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"time"
1111

1212
"github.com/google/uuid"
13+
14+
"github.com/opensvc/oc3/util/logkey"
1315
)
1416

1517
type (
@@ -599,6 +601,9 @@ func (oDb *DB) InsertNode(ctx context.Context, nodename, teamResponsible, app, n
599601
return fmt.Errorf("InsertNode: %w", err)
600602
}
601603
oDb.SetChange("nodes")
602-
oDb.Session.NotifyChanges(ctx)
604+
oDb.Metrics.NodeConfigInsert.Inc()
605+
if err := oDb.Session.NotifyChanges(ctx); err != nil {
606+
slog.Debug("insert node can't notify changes", logkey.Error, err, logkey.Nodename, nodename, logkey.NodeID, nodeID)
607+
}
603608
return nil
604609
}

0 commit comments

Comments
 (0)