From 08baa2c1dfcf8b84f4db47f29cb255781c565fc5 Mon Sep 17 00:00:00 2001 From: Dave Nusbaum Date: Tue, 10 Nov 2026 08:58:14 -0600 Subject: [PATCH 1/2] inst: create instance caches synchronously before raft restores or applies initializeInstanceDao() runs as a goroutine started from init() and only creates forgetInstanceKeys (and the other instance caches) once it has been scheduled after the configuration is loaded. With RaftEnabled, raft.Setup() restores the latest snapshot and replays the log during startup, and both paths can call InstanceIsForgotten() before that goroutine has run: - SnapshotDataCreatorApplier.Restore() -> ReadAllInstanceKeys() logs "QueryRowsMap unexpected error: runtime error: invalid memory address or nil pointer dereference", then WriteInstance() -> writeManyInstances() panics in InstanceIsForgotten(). - fsm.Apply() of a "discover" command -> DiscoverInstance() panics in InstanceIsForgotten(). The caches are now created by InitializeInstanceDaoCaches() under a sync.Once, called from main() right after MarkConfigurationLoaded(), and again (a no-op) from the background goroutine. A unit test covers the synchronous creation. Signed-off-by: Dave Nusbaum Co-Authored-By: Claude Opus 5.5 --- go/cmd/orchestrator/main.go | 1 + go/inst/instance_dao.go | 19 +++++++++++++++---- go/inst/instance_dao_test.go | 13 +++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/go/cmd/orchestrator/main.go b/go/cmd/orchestrator/main.go index e392681db..71f2a1c64 100644 --- a/go/cmd/orchestrator/main.go +++ b/go/cmd/orchestrator/main.go @@ -158,6 +158,7 @@ func main() { } config.RuntimeCLIFlags.ConfiguredVersion = AppVersion config.MarkConfigurationLoaded() + inst.InitializeInstanceDaoCaches() if len(flag.Args()) == 0 && *command == "" { // No command, no argument: just prompt diff --git a/go/inst/instance_dao.go b/go/inst/instance_dao.go index 6933fd6d2..4c029b692 100644 --- a/go/inst/instance_dao.go +++ b/go/inst/instance_dao.go @@ -168,12 +168,23 @@ func init() { go initializeInstanceDao() } +var instanceDaoCachesOnce sync.Once + +// InitializeInstanceDaoCaches creates the instance write buffer and the instance caches. +// It is called synchronously once the configuration is loaded, before anything (such as a +// raft snapshot restore) can read the caches; later calls do nothing. +func InitializeInstanceDaoCaches() { + instanceDaoCachesOnce.Do(func() { + instanceWriteBuffer = make(chan instanceUpdateObject, config.Config.InstanceWriteBufferSize) + instanceKeyInformativeClusterName = cache.New(time.Duration(config.Config.InstancePollSeconds/2)*time.Second, time.Second) + forgetInstanceKeys = cache.New(time.Duration(config.Config.InstancePollSeconds*3)*time.Second, time.Second) + clusterInjectedPseudoGTIDCache = cache.New(time.Minute, time.Second) + }) +} + func initializeInstanceDao() { config.WaitForConfigurationToBeLoaded() - instanceWriteBuffer = make(chan instanceUpdateObject, config.Config.InstanceWriteBufferSize) - instanceKeyInformativeClusterName = cache.New(time.Duration(config.Config.InstancePollSeconds/2)*time.Second, time.Second) - forgetInstanceKeys = cache.New(time.Duration(config.Config.InstancePollSeconds*3)*time.Second, time.Second) - clusterInjectedPseudoGTIDCache = cache.New(time.Minute, time.Second) + InitializeInstanceDaoCaches() // spin off instance write buffer flushing go func() { flushTick := time.Tick(time.Duration(config.Config.InstanceFlushIntervalMilliseconds) * time.Millisecond) diff --git a/go/inst/instance_dao_test.go b/go/inst/instance_dao_test.go index cc1593f18..0476c7f92 100644 --- a/go/inst/instance_dao_test.go +++ b/go/inst/instance_dao_test.go @@ -146,3 +146,16 @@ func fmtArgs(args []interface{}) string { } return b.String() } + +func TestInitializeInstanceDaoCaches(t *testing.T) { + // The configuration is never marked loaded in unit tests, so the background + // initializeInstanceDao() goroutine never creates the caches; only the + // synchronous call can. + InitializeInstanceDaoCaches() + InitializeInstanceDaoCaches() + test.S(t).ExpectNotNil(forgetInstanceKeys) + test.S(t).ExpectNotNil(instanceKeyInformativeClusterName) + test.S(t).ExpectNotNil(clusterInjectedPseudoGTIDCache) + test.S(t).ExpectNotNil(instanceWriteBuffer) + test.S(t).ExpectFalse(InstanceIsForgotten(&i710k)) +} From be87e3e8c74661950b665a0d5b7ff146dcd1f226 Mon Sep 17 00:00:00 2001 From: Dave Nusbaum Date: Fri, 13 Nov 2026 10:38:01 -0600 Subject: [PATCH 2/2] inst: compare the instance caches with nil directly in the init test ExpectNotNil takes an interface{}, and a nil *cache.Cache or channel wrapped in one is not nil, so the test passed even when a cache had not been created. Compare each value with nil before asserting. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Dave Nusbaum --- go/inst/instance_dao_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go/inst/instance_dao_test.go b/go/inst/instance_dao_test.go index 0476c7f92..668535983 100644 --- a/go/inst/instance_dao_test.go +++ b/go/inst/instance_dao_test.go @@ -153,9 +153,9 @@ func TestInitializeInstanceDaoCaches(t *testing.T) { // synchronous call can. InitializeInstanceDaoCaches() InitializeInstanceDaoCaches() - test.S(t).ExpectNotNil(forgetInstanceKeys) - test.S(t).ExpectNotNil(instanceKeyInformativeClusterName) - test.S(t).ExpectNotNil(clusterInjectedPseudoGTIDCache) - test.S(t).ExpectNotNil(instanceWriteBuffer) + test.S(t).ExpectTrue(forgetInstanceKeys != nil) + test.S(t).ExpectTrue(instanceKeyInformativeClusterName != nil) + test.S(t).ExpectTrue(clusterInjectedPseudoGTIDCache != nil) + test.S(t).ExpectTrue(instanceWriteBuffer != nil) test.S(t).ExpectFalse(InstanceIsForgotten(&i710k)) }