Skip to content

Commit 3a476fd

Browse files
AchoArnoldCopilot
andcommitted
feat: switch heartbeat repositories to use MongoDB directly
Replace the hedging dual-write pattern with direct MongoDB usage for heartbeat monitors and heartbeats. When HEARTBEAT_DB_BACKEND is set to 'hedging', it now routes to MongoDB instead of the dual-write hedging repository. This is the first migration step before switching to MongoDB completely. - Remove hedging_heartbeat_monitor_repository.go - Remove hedging_heartbeat_repository.go - Remove HedgingFailureCounter from DI container - Route 'hedging' env value to MongoDB in both repository factories Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 28ff1d8 commit 3a476fd

5 files changed

Lines changed: 16 additions & 244 deletions

File tree

api/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ require (
4141
github.com/redis/go-redis/extra/redisotel/v9 v9.19.0
4242
github.com/redis/go-redis/v9 v9.19.0
4343
github.com/rs/zerolog v1.35.1
44+
github.com/schollz/progressbar/v3 v3.19.0
4445
github.com/stretchr/testify v1.11.1
4546
github.com/swaggo/swag v1.16.6
4647
github.com/thedevsaddam/govalidator v1.9.10
@@ -141,6 +142,7 @@ require (
141142
github.com/mattn/go-isatty v0.0.22 // indirect
142143
github.com/mattn/go-runewidth v0.0.23 // indirect
143144
github.com/mattn/go-sqlite3 v1.14.44 // indirect
145+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
144146
github.com/mitchellh/copystructure v1.2.0 // indirect
145147
github.com/mitchellh/reflectwalk v1.0.2 // indirect
146148
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
@@ -153,6 +155,7 @@ require (
153155
github.com/redis/go-redis/extra/rediscmd/v9 v9.19.0 // indirect
154156
github.com/richardlehane/mscfb v1.0.6 // indirect
155157
github.com/richardlehane/msoleps v1.0.6 // indirect
158+
github.com/rivo/uniseg v0.4.7 // indirect
156159
github.com/segmentio/asm v1.2.1 // indirect
157160
github.com/shopspring/decimal v1.4.0 // indirect
158161
github.com/spiffe/go-spiffe/v2 v2.6.0 // indirect
@@ -193,6 +196,7 @@ require (
193196
golang.org/x/net v0.53.0 // indirect
194197
golang.org/x/oauth2 v0.36.0 // indirect
195198
golang.org/x/sys v0.44.0 // indirect
199+
golang.org/x/term v0.43.0 // indirect
196200
golang.org/x/text v0.37.0 // indirect
197201
golang.org/x/time v0.15.0 // indirect
198202
golang.org/x/tools v0.44.0 // indirect

api/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1x
7878
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
7979
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
8080
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
81+
github.com/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7mk9/PwM=
82+
github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY=
8183
github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSEFgwIwO+UVM8=
8284
github.com/clipperhouse/displaywidth v0.11.0/go.mod h1:bkrFNkf81G8HyVqmKGxsPufD3JhNl3dSqnGhOoSD/o0=
8385
github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=
@@ -245,6 +247,8 @@ github.com/mattn/go-runewidth v0.0.23 h1:7ykA0T0jkPpzSvMS5i9uoNn2Xy3R383f9HDx3Ry
245247
github.com/mattn/go-runewidth v0.0.23/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
246248
github.com/mattn/go-sqlite3 v1.14.44 h1:3VSe+xafpbzsLbdr2AWlAZk9yRHiBhTBakioXaCKTF8=
247249
github.com/mattn/go-sqlite3 v1.14.44/go.mod h1:pjEuOr8IwzLJP2MfGeTb0A35jauH+C2kbHKBr7yXKVQ=
250+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
251+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
248252
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
249253
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
250254
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
@@ -291,10 +295,14 @@ github.com/richardlehane/mscfb v1.0.6 h1:eN3bvvZCp00bs7Zf52bxNwAx5lJDBK1tCuH19qq
291295
github.com/richardlehane/mscfb v1.0.6/go.mod h1:pe0+IUIc0AHh0+teNzBlJCtSyZdFOGgV4ZK9bsoV+Jo=
292296
github.com/richardlehane/msoleps v1.0.6 h1:9BvkpjvD+iUBalUY4esMwv6uBkfOip/Lzvd93jvR9gg=
293297
github.com/richardlehane/msoleps v1.0.6/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
298+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
299+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
294300
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
295301
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
296302
github.com/rs/zerolog v1.35.1 h1:m7xQeoiLIiV0BCEY4Hs+j2NG4Gp2o2KPKmhnnLiazKI=
297303
github.com/rs/zerolog v1.35.1/go.mod h1:EjML9kdfa/RMA7h/6z6pYmq1ykOuA8/mjWaEvGI+jcw=
304+
github.com/schollz/progressbar/v3 v3.19.0 h1:Ea18xuIRQXLAUidVDox3AbwfUhD0/1IvohyTutOIFoc=
305+
github.com/schollz/progressbar/v3 v3.19.0/go.mod h1:IsO3lpbaGuzh8zIMzgY3+J8l4C8GjO0Y9S69eFvNsec=
298306
github.com/segmentio/asm v1.2.1 h1:DTNbBqs57ioxAD4PrArqftgypG4/qNpXoJx8TVXxPR0=
299307
github.com/segmentio/asm v1.2.1/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
300308
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
@@ -477,6 +485,8 @@ golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
477485
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
478486
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
479487
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
488+
golang.org/x/term v0.43.0 h1:S4RLU2sB31O/NCl+zFN9Aru9A/Cq2aqKpTZJ6B+DwT4=
489+
golang.org/x/term v0.43.0/go.mod h1:lrhlHNdQJHO+1qVYiHfFKVuVioJIheAc3fBSMFYEIsk=
480490
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
481491
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
482492
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=

api/pkg/di/container.go

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -310,23 +310,6 @@ func (container *Container) MongoDB() *mongoDriver.Database {
310310
return container.mongoDB
311311
}
312312

313-
// HedgingFailureCounter creates an OTel counter for hedging secondary write failures
314-
func (container *Container) HedgingFailureCounter() otelMetric.Int64Counter {
315-
meter := otel.GetMeterProvider().Meter(
316-
container.projectID,
317-
otelMetric.WithInstrumentationVersion(otel.Version()),
318-
)
319-
counter, err := meter.Int64Counter(
320-
"hedging.secondary.write.failures",
321-
otelMetric.WithUnit("1"),
322-
otelMetric.WithDescription("Number of failed secondary writes in hedging repositories"),
323-
)
324-
if err != nil {
325-
container.logger.Fatal(stacktrace.Propagate(err, "cannot create hedging failure counter"))
326-
}
327-
return counter
328-
}
329-
330313
// DBWithoutMigration creates an instance of gorm.DB if it has not been created already
331314
func (container *Container) DBWithoutMigration() (db *gorm.DB) {
332315
if container.db != nil {
@@ -922,22 +905,13 @@ func (container *Container) MessageThreadRepository() (repository repositories.M
922905
// HeartbeatMonitorRepository creates a new instance of repositories.HeartbeatMonitorRepository
923906
func (container *Container) HeartbeatMonitorRepository() (repository repositories.HeartbeatMonitorRepository) {
924907
switch os.Getenv("HEARTBEAT_DB_BACKEND") {
925-
case "mongodb":
908+
case "mongodb", "hedging":
926909
container.logger.Debug("creating MongoDB repositories.HeartbeatMonitorRepository")
927910
return repositories.NewMongoHeartbeatMonitorRepository(
928911
container.Logger(),
929912
container.Tracer(),
930913
container.MongoDB(),
931914
)
932-
case "hedging":
933-
container.logger.Debug("creating hedging repositories.HeartbeatMonitorRepository")
934-
return repositories.NewHedgingHeartbeatMonitorRepository(
935-
container.Logger(),
936-
container.Tracer(),
937-
repositories.NewGormHeartbeatMonitorRepository(container.Logger(), container.Tracer(), container.DedicatedDB()),
938-
repositories.NewMongoHeartbeatMonitorRepository(container.Logger(), container.Tracer(), container.MongoDB()),
939-
container.HedgingFailureCounter(),
940-
)
941915
default:
942916
container.logger.Debug("creating GORM repositories.HeartbeatMonitorRepository")
943917
return repositories.NewGormHeartbeatMonitorRepository(
@@ -1760,22 +1734,13 @@ func (container *Container) RegisterSwaggerRoutes() {
17601734
// HeartbeatRepository registers a new instance of repositories.HeartbeatRepository
17611735
func (container *Container) HeartbeatRepository() repositories.HeartbeatRepository {
17621736
switch os.Getenv("HEARTBEAT_DB_BACKEND") {
1763-
case "mongodb":
1737+
case "mongodb", "hedging":
17641738
container.logger.Debug("creating MongoDB repositories.HeartbeatRepository")
17651739
return repositories.NewMongoHeartbeatRepository(
17661740
container.Logger(),
17671741
container.Tracer(),
17681742
container.MongoDB(),
17691743
)
1770-
case "hedging":
1771-
container.logger.Debug("creating hedging repositories.HeartbeatRepository")
1772-
return repositories.NewHedgingHeartbeatRepository(
1773-
container.Logger(),
1774-
container.Tracer(),
1775-
repositories.NewGormHeartbeatRepository(container.Logger(), container.Tracer(), container.DedicatedDB()),
1776-
repositories.NewMongoHeartbeatRepository(container.Logger(), container.Tracer(), container.MongoDB()),
1777-
container.HedgingFailureCounter(),
1778-
)
17791744
default:
17801745
container.logger.Debug("creating GORM repositories.HeartbeatRepository")
17811746
return repositories.NewGormHeartbeatRepository(

api/pkg/repositories/hedging_heartbeat_monitor_repository.go

Lines changed: 0 additions & 128 deletions
This file was deleted.

api/pkg/repositories/hedging_heartbeat_repository.go

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)