Skip to content

Commit 9baace5

Browse files
committed
Fix review issues
On-behalf-of: SAP Michal Krzyz <michal.krzyz@sap.com> Signed-off-by: Michal Krzyz <michalkrzyz@gmail.com>
1 parent 396400c commit 9baace5

9 files changed

Lines changed: 70 additions & 72 deletions

File tree

internal/app/heureka.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/cloudoperators/heureka/internal/app/user"
2828
"github.com/cloudoperators/heureka/internal/cache"
2929
"github.com/cloudoperators/heureka/internal/database"
30+
"github.com/cloudoperators/heureka/internal/database/mariadb"
3031
"github.com/cloudoperators/heureka/internal/openfga"
3132
"github.com/cloudoperators/heureka/internal/util"
3233
)
@@ -58,13 +59,16 @@ type HeurekaApp struct {
5859
wg *sync.WaitGroup
5960

6061
profiler *profiler.Profiler
62+
63+
mve *mariadb.MvEngine
6164
}
6265

6366
func NewHeurekaApp(
6467
ctx context.Context,
6568
wg *sync.WaitGroup,
6669
db database.Database,
6770
cfg util.Config,
71+
mve *mariadb.MvEngine,
6872
) *HeurekaApp {
6973
cache := NewAppCache(ctx, wg, cfg)
7074
enableLogs := true
@@ -117,6 +121,7 @@ func NewHeurekaApp(
117121
authz: handlerContext.Authz,
118122
wg: wg,
119123
profiler: profiler,
124+
mve: mve,
120125
}
121126

122127
heureka.SubscribeHandlers()
@@ -290,5 +295,5 @@ func (h HeurekaApp) GetCache() cache.Cache {
290295
}
291296

292297
func (h HeurekaApp) WaitPostMigrations() {
293-
h.database.WaitPostMigrations()
298+
h.mve.WaitForFirstRun()
294299
}

internal/database/interface.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,6 @@ type Database interface {
175175

176176
Autopatch(context.Context) (bool, error)
177177

178-
WaitPostMigrations()
179-
180178
// Batch pre-load methods for GetVulnerabilities query optimization
181179
GetMaxSeverityByIssueIDs(context.Context, []int64) (map[int64]string, error)
182180
GetEarliestRemediationByIssueIDs(context.Context, []int64) (map[int64]time.Time, error)

internal/database/mariadb/migration.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,3 @@ func RunMigrations(cfg util.Config) error {
128128

129129
return nil
130130
}
131-
132-
func (s *SqlDatabase) WaitPostMigrations() {
133-
WaitMVEForFirstRun()
134-
}

internal/database/mariadb/migrations/20260727093544_move_mv_procedures_to_app_code.down.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ BEGIN
601601
END;
602602

603603
DROP PROCEDURE IF EXISTS refresh_mvAllComponentsByServiceVulnerabilityCounts_proc;
604-
CREATE PROCEDURE refreshAllComponentsByServiceVulnerabilityCounts_proc()
604+
CREATE PROCEDURE refresh_mvAllComponentsByServiceVulnerabilityCounts_proc()
605605
BEGIN
606606
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
607607

internal/database/mariadb/migrations/20260727093544_move_mv_procedures_to_app_code.up.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ DROP PROCEDURE IF EXISTS call_registered_post_migration_procedures;
66
DROP PROCEDURE IF EXISTS add_post_migration_procedure;
77
DROP PROCEDURE IF EXISTS remove_post_migration_procedure;
88

9-
DROP PROCEDURE IF EXISTS refresh_mvServiceIssueCounts_proc;
109
DROP PROCEDURE IF EXISTS refresh_mvServiceIssueCounts_proc;
1110
DROP PROCEDURE IF EXISTS refresh_mvCountIssueRatingsServiceId_proc;
1211
DROP PROCEDURE IF EXISTS refresh_mvCountIssueRatingsUniqueService_proc;

internal/database/mariadb/mve.go

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ import (
1616
"github.com/sirupsen/logrus"
1717
)
1818

19+
type MvEngine struct {
20+
scheduler *gocron.Scheduler
21+
firstRunDone chan struct{}
22+
once sync.Once
23+
cfg util.Config
24+
}
25+
26+
func NewMvEngine(cfg util.Config) *MvEngine {
27+
return &MvEngine{
28+
scheduler: gocron.NewScheduler(time.UTC),
29+
firstRunDone: make(chan struct{}),
30+
cfg: cfg,
31+
}
32+
}
33+
1934
func TriggerMVE(cfg util.Config) error {
2035
db, err := NewDb(cfg)
2136
if err != nil {
@@ -25,18 +40,16 @@ func TriggerMVE(cfg util.Config) error {
2540
return runInBackground(db, MVProcedures).Wait()
2641
}
2742

28-
func StartMVEScheduler(cfg util.Config) {
29-
mve := getMVE()
30-
31-
periodMinutes := cfg.DBMvCalcPeriodMinutes
43+
func (mve *MvEngine) Start() {
44+
periodMinutes := mve.cfg.DBMvCalcPeriodMinutes
3245
if periodMinutes <= 0 {
3346
periodMinutes = 200
3447
}
3548

3649
logrus.Debugf("MVE scheduling period set to %d minutes", periodMinutes)
3750

3851
_, err := mve.scheduler.Every(periodMinutes).Minutes().SingletonMode().Do(func() {
39-
err := TriggerMVE(cfg)
52+
err := TriggerMVE(mve.cfg)
4053
if err != nil {
4154
logrus.WithError(err).Error("MVE Trigger error")
4255
}
@@ -55,47 +68,18 @@ func StartMVEScheduler(cfg util.Config) {
5568
mve.scheduler.StartAsync()
5669
}
5770

58-
func StopMVEScheduler() {
59-
mve := getMVE()
60-
mve.Shutdown()
71+
func (mve *MvEngine) Stop() {
72+
mve.scheduler.Clear()
73+
// The following method is not advisory as it may hang for a long time:
74+
// mve.scheduler.Stop()
6175
}
6276

63-
func WaitMVEForFirstRun() {
64-
mve := getMVE()
77+
func (mve *MvEngine) WaitForFirstRun() {
6578
<-mve.firstRunDone
6679
}
6780

6881
////////// Internals
6982

70-
var mvEngine *MvEngine
71-
72-
type MvEngine struct {
73-
scheduler *gocron.Scheduler
74-
firstRunDone chan struct{}
75-
once sync.Once
76-
}
77-
78-
func NewMvEngine() *MvEngine {
79-
return &MvEngine{
80-
scheduler: gocron.NewScheduler(time.UTC),
81-
firstRunDone: make(chan struct{}),
82-
}
83-
}
84-
85-
func getMVE() *MvEngine {
86-
if mvEngine == nil {
87-
mvEngine = NewMvEngine()
88-
}
89-
90-
return mvEngine
91-
}
92-
93-
func (mve *MvEngine) Shutdown() {
94-
mve.scheduler.Clear()
95-
// This is not advisory as it may hang for a long time:
96-
// mve.scheduler.Stop()
97-
}
98-
9983
type mveCtx struct {
10084
wg sync.WaitGroup
10185
mu sync.Mutex
@@ -126,13 +110,16 @@ func (mc *mveCtx) Wait() error {
126110
return nil
127111
}
128112

129-
func runInBackground(db Db, procs []MVProcedure) *mveCtx {
113+
func runInBackground(db Db, procs [][]MVProcedure) *mveCtx {
130114
mc := &mveCtx{}
131115

132-
for i, p := range procs {
116+
for i, pl := range procs {
133117
mc.wg.Go(func() {
134-
if err := TxCall(p, context.Background(), db); err != nil {
135-
mc.appendErrorMessage(fmt.Sprintf("(procIdx: %d): %v", i, err))
118+
for j, p := range pl {
119+
if err := TxCall(p, context.Background(), db); err != nil {
120+
mc.appendErrorMessage(fmt.Sprintf("(procIdx: %d:%d): %v", i, j, err))
121+
break
122+
}
136123
}
137124
})
138125
}

internal/database/mariadb/mvproc.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,26 @@ type DBTX interface {
1717

1818
type MVProcedure func(ctx context.Context, db DBTX) error
1919

20-
var MVProcedures []MVProcedure = []MVProcedure{
21-
// r! grep -E '^func\s' internal/database/mariadb/mvproc.go | sed -e 's@func\s\(.*\)[(].*@\1@' | sed -e 's/$/,/'
22-
RefreshMVServiceIssueCounts,
23-
RefreshMVCountIssueRatingsServiceId,
24-
RefreshMVCountIssueRatingsUniqueService,
25-
RefreshMVCountIssueRatingsOther, // 4 - this one is not tested in database nor in e2e
26-
RefreshMVCountIssueRatingsService,
27-
RefreshMVCountIssueRatingsServiceWithoutSupportGroup,
28-
RefreshMVCountIssueRatingsSupportGroup,
29-
RefreshMVCountIssueRatingsComponentVersion,
30-
RefreshMVVulnerabilityList,
31-
RefreshMVVulnerabilityService, // 10 - this one is not tested in database nor in e2e
32-
RefreshMVComponentService,
33-
RefreshMVSingleComponentByServiceVulnerabilityCounts,
34-
RefreshMVAllComponentsByServiceVulnerabilityCounts,
20+
var MVProcedures [][]MVProcedure = [][]MVProcedure{
21+
// Keep this list in sync with the Refresh* functions in this file.
22+
// vim helper:
23+
// r! grep -E '^func\s' internal/database/mariadb/mvproc.go | sed -e 's@func\s\(.*\)[(].*@\1@' | sed -e 's/$/,/'
24+
{RefreshMVServiceIssueCounts},
25+
{RefreshMVCountIssueRatingsServiceId},
26+
{RefreshMVCountIssueRatingsUniqueService},
27+
{RefreshMVCountIssueRatingsOther}, // 4 - this one is not tested in database nor in e2e
28+
{RefreshMVCountIssueRatingsService},
29+
{RefreshMVCountIssueRatingsServiceWithoutSupportGroup},
30+
{RefreshMVCountIssueRatingsSupportGroup},
31+
{RefreshMVCountIssueRatingsComponentVersion},
32+
// The following two have to be called in sequence
33+
{
34+
RefreshMVVulnerabilityList,
35+
RefreshMVVulnerabilityService, // 10 - this one is not tested in database nor in e2e
36+
},
37+
{RefreshMVComponentService},
38+
{RefreshMVSingleComponentByServiceVulnerabilityCounts},
39+
{RefreshMVAllComponentsByServiceVulnerabilityCounts},
3540
}
3641

3742
func RefreshMVServiceIssueCounts(ctx context.Context, db DBTX) error {

internal/e2e/db_migration_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,14 @@ func setDbMvTestTableMigration() {
8686
setDbTestMigration(&migrationMvTestTableMigrationFiles)
8787
}
8888

89-
func setMvProceduresInMVE(procs []mariadb.MVProcedure) {
89+
func setMvProceduresInMVE(procs [][]mariadb.MVProcedure) {
90+
prev := mariadb.MVProcedures
91+
DeferCleanup(func() { mariadb.MVProcedures = prev })
9092
mariadb.MVProcedures = procs
9193
}
9294

9395
func setMvTestTableInMVE() {
94-
setMvProceduresInMVE([]mariadb.MVProcedure{RefreshMVTestData})
96+
setMvProceduresInMVE([][]mariadb.MVProcedure{{RefreshMVTestData}})
9597
}
9698

9799
func extractVersion(filename string) string {
@@ -361,8 +363,10 @@ var _ = Describe(
361363
var migrationTest DbMigrationTest
362364
BeforeEach(func() {
363365
migrationTest.setup()
366+
storeProdMVProcedures()
364367
})
365368
AfterEach(func() {
369+
restoreProdMVProcedures()
366370
migrationTest.teardown()
367371
})
368372
When(

internal/server/server.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ type Server struct {
5151

5252
app *app.HeurekaApp
5353
sigs chan os.Signal
54+
55+
mve *mariadb.MvEngine
5456
}
5557

5658
func NewServer(cfg util.Config) *Server {
@@ -63,14 +65,15 @@ func NewServer(cfg util.Config) *Server {
6365
logrus.WithError(err).Fatalln("Error while Migrating Db")
6466
}
6567

66-
mariadb.StartMVEScheduler(cfg)
68+
mve := mariadb.NewMvEngine(cfg)
69+
mve.Start()
6770

6871
db, err := mariadb.NewSqlDatabase(cfg)
6972
if err != nil {
7073
logrus.WithError(err).Fatalln("Error while Creating Db")
7174
}
7275

73-
application := app.NewHeurekaApp(ctx, &wg, db, cfg)
76+
application := app.NewHeurekaApp(ctx, &wg, db, cfg, mve)
7477

7578
var ai *aiapi.AIAPI
7679

@@ -97,6 +100,7 @@ func NewServer(cfg util.Config) *Server {
97100
shutdownFunc: cancel,
98101
wg: &wg,
99102
sigs: make(chan os.Signal, 1),
103+
mve: mve,
100104
}
101105

102106
// kill (no param) default send syscanll.SIGTERM
@@ -220,7 +224,7 @@ func (s *Server) BlockingStop() {
220224
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
221225
defer cancel()
222226

223-
mariadb.StopMVEScheduler()
227+
s.mve.Stop()
224228

225229
if err := s.nonBlockingSrv.Shutdown(ctx); err != nil {
226230
log.Fatal("Server forced to shutdown: ", err)

0 commit comments

Comments
 (0)