From 5c9947fcbde649995a50ce0db5ec679f5ac70011 Mon Sep 17 00:00:00 2001 From: Doctor Vince Date: Thu, 22 Jun 2023 12:40:17 -0400 Subject: [PATCH 1/2] only set replicas when we have more than 1 --- cmd/run.go | 2 +- internal/worker/liveness.go | 13 ++++++++++++- internal/worker/worker.go | 6 +++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index d307168f..4fe5c9ac 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -107,7 +107,7 @@ func init() { cmdRun.PersistentFlags().BoolVarP(&dryrun, "dry-run", "", false, "In dryrun mode, the worker actions the task without installing firmware") cmdRun.PersistentFlags().BoolVarP(&useStatusKV, "use-kv", "", false, "when this is true, flasher writes status to a NATS KV store instead of sending reply messages") cmdRun.PersistentFlags().BoolVarP(&faultInjection, "fault-injection", "", false, "Tasks can include a Fault attribute to allow fault injection for development purposes") - cmdRun.PersistentFlags().IntVarP(&replicas, "nats-replicas", "r", 1, "the default number of replicas to use for NATS data") + cmdRun.PersistentFlags().IntVarP(&replicas, "replica-count", "r", 3, "the number of replicas to use for NATS data") if err := cmdRun.MarkPersistentFlagRequired("store"); err != nil { log.Fatal(err) diff --git a/internal/worker/liveness.go b/internal/worker/liveness.go index 98cf1cce..6a67b14e 100644 --- a/internal/worker/liveness.go +++ b/internal/worker/liveness.go @@ -6,6 +6,7 @@ import ( "time" "go.hollow.sh/toolbox/events" + "go.hollow.sh/toolbox/events/pkg/kv" "go.hollow.sh/toolbox/events/registry" "github.com/nats-io/nats.go" @@ -14,6 +15,7 @@ import ( var ( once sync.Once checkinCadence = 30 * time.Second + livenessTTL = 3 * time.Minute ) // This starts a go-routine to peridocally check in with the NATS kv @@ -26,7 +28,16 @@ func (w *Worker) startWorkerLivenessCheckin(ctx context.Context) { return } - if err := registry.InitializeActiveControllerRegistry(natsJS); err != nil { + opts := []kv.Option{ + kv.WithTTL(livenessTTL), + } + + // any setting of replicas (even 1) chokes NATS in non-clustered mode + if w.replicaCount != 1 { + opts = append(opts, kv.WithReplicas(w.replicaCount)) + } + + if err := registry.InitializeRegistryWithOptions(natsJS, opts...); err != nil { w.logger.WithError(err).Error("unable to initialize active worker registry") return } diff --git a/internal/worker/worker.go b/internal/worker/worker.go index f35f5262..fdb76388 100644 --- a/internal/worker/worker.go +++ b/internal/worker/worker.go @@ -310,7 +310,11 @@ func (o *Worker) runTaskWithMonitor(ctx context.Context, task *model.Task, asset func (o *Worker) getStatusPublisher() sm.Publisher { if o.useStatusKV { - return NewStatusKVPublisher(o.stream, o.logger, kv.WithReplicas(o.replicaCount)) + var opts []kv.Option + if o.replicaCount > 1 { + opts = append(opts, kv.WithReplicas(o.replicaCount)) + } + return NewStatusKVPublisher(o.stream, o.logger, opts...) } return &statusEmitter{o.stream, o.logger} } From 2222a33a89219a4e3454ce01ac5235f430689777 Mon Sep 17 00:00:00 2001 From: Doctor Vince Date: Thu, 22 Jun 2023 19:54:27 -0400 Subject: [PATCH 2/2] goodbye gomnd --- .golangci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 67b443a8..3933d68d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -68,7 +68,6 @@ linters: - goerr113 - goimports - revive - - gomnd - misspell - noctx - stylecheck