Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ linters:
- copyloopvar
- dupl
- errcheck
- errorlint
- funlen
- gocritic
- govet
Expand All @@ -25,6 +26,10 @@ linters:
settings:
dupl:
threshold: 400
errorlint:
errorf: true
asserts: true
comparison: true
funlen:
lines: 400
statements: 200
Expand Down
3 changes: 2 additions & 1 deletion internal/app/active_nodes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"errors"
"fmt"
"slices"
"sort"
Expand All @@ -15,7 +16,7 @@ func (app *App) GetActiveNodes() ([]string, error) {
var activeNodes []string
err := app.dcs.Get(pathActiveNodes, &activeNodes)
if err != nil {
if err == dcs.ErrNotFound {
if errors.Is(err, dcs.ErrNotFound) {
return nil, nil
}
return nil, fmt.Errorf("get active nodes from dcs: %s", err.Error())
Expand Down
8 changes: 5 additions & 3 deletions internal/app/candidate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app

import (
"errors"

"github.com/yandex/rdsync/internal/dcs"
)

Expand All @@ -20,7 +22,7 @@ func (app *App) stateCandidate() appState {
app.logger.Info().Msgf("Shard state: %v", shardState)
}
maintenance, err := app.GetMaintenance()
if err != nil && err != dcs.ErrNotFound {
if err != nil && !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msg("Candidate: failed to get maintenance from DCS")
return stateCandidate
}
Expand All @@ -29,7 +31,7 @@ func (app *App) stateCandidate() appState {
}

poisonPill, err := app.getPoisonPill()
if err != nil && err != dcs.ErrNotFound {
if err != nil && !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msg("Candidate: failed to get poison pill from DCS")
return stateCandidate
}
Expand All @@ -46,7 +48,7 @@ func (app *App) stateCandidate() appState {

var master string
err = app.dcs.Get(pathMasterNode, &master)
if err != nil && err != dcs.ErrNotFound {
if err != nil && !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msg("Candidate: failed to get current master from DCS")
return stateCandidate
}
Expand Down
37 changes: 19 additions & 18 deletions internal/app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"bufio"
"context"
"errors"
"fmt"

"os"
Expand Down Expand Up @@ -69,7 +70,7 @@ func (app *App) CliInfo(verbose bool) int {
err = app.dcs.Get(path, &switchover)
if err == nil {
data[path] = switchover.String()
} else if err != dcs.ErrNotFound {
} else if !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msgf("Failed to get %s", path)
return 1
}
Expand All @@ -79,7 +80,7 @@ func (app *App) CliInfo(verbose bool) int {
err = app.dcs.Get(pathMaintenance, &maintenance)
if err == nil {
data[pathMaintenance] = maintenance.String()
} else if err != dcs.ErrNotFound {
} else if !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msgf("Failed to get %s", pathMaintenance)
return 1
}
Expand All @@ -88,22 +89,22 @@ func (app *App) CliInfo(verbose bool) int {
err = app.dcs.Get(pathPoisonPill, &poisonPill)
if err == nil {
data[pathPoisonPill] = poisonPill.String()
} else if err != dcs.ErrNotFound {
} else if !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msgf("Failed to get %s", pathPoisonPill)
return 1
}

var manager dcs.LockOwner
err = app.dcs.Get(pathManagerLock, &manager)
if err != nil && err != dcs.ErrNotFound {
if err != nil && !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msgf("Failed to get %s", pathManagerLock)
return 1
}
data[pathManagerLock] = manager.Hostname

var master string
err = app.dcs.Get(pathMasterNode, &master)
if err != nil && err != dcs.ErrNotFound {
if err != nil && !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msgf("Failed to get %s", pathMasterNode)
return 1
}
Expand Down Expand Up @@ -287,7 +288,7 @@ func (app *App) CliSwitch(switchFrom, switchTo string, waitTimeout time.Duration
app.logger.Error().Msgf("Another switchover in progress %v", switchover)
return 2
}
if err != dcs.ErrNotFound {
if !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msg("Unable to get current switchover status")
return 2
}
Expand All @@ -307,7 +308,7 @@ func (app *App) CliSwitch(switchFrom, switchTo string, waitTimeout time.Duration
}

err = app.dcs.Create(pathCurrentSwitch, switchover)
if err == dcs.ErrExists {
if errors.Is(err, dcs.ErrExists) {
app.logger.Error().Msg("Another switchover in progress")
return 2
}
Expand Down Expand Up @@ -364,7 +365,7 @@ func (app *App) CliEnableMaintenance(waitTimeout time.Duration) int {
InitiatedAt: time.Now(),
}
err = app.dcs.Create(pathMaintenance, maintenance)
if err != nil && err != dcs.ErrExists {
if err != nil && !errors.Is(err, dcs.ErrExists) {
app.logger.Error().Err(err).Msg("Unable to create maintenance path in dcs")
return 1
}
Expand Down Expand Up @@ -410,7 +411,7 @@ func (app *App) CliDisableMaintenance(waitTimeout time.Duration) int {

maintenance := &Maintenance{}
err = app.dcs.Get(pathMaintenance, maintenance)
if err == dcs.ErrNotFound {
if errors.Is(err, dcs.ErrNotFound) {
fmt.Println("maintenance disabled")
return 0
} else if err != nil {
Expand All @@ -432,7 +433,7 @@ func (app *App) CliDisableMaintenance(waitTimeout time.Duration) int {
select {
case <-ticker.C:
err = app.dcs.Get(pathMaintenance, maintenance)
if err == dcs.ErrNotFound {
if errors.Is(err, dcs.ErrNotFound) {
maintenance = nil
break Out
}
Expand Down Expand Up @@ -466,15 +467,15 @@ func (app *App) CliGetMaintenance() int {

var maintenance Maintenance
err = app.dcs.Get(pathMaintenance, &maintenance)
switch err {
case nil:
switch {
case err == nil:
if maintenance.RdSyncPaused {
fmt.Println("on")
} else {
fmt.Println("scheduled")
}
return 0
case dcs.ErrNotFound:
case errors.Is(err, dcs.ErrNotFound):
fmt.Println("off")
return 0
default:
Expand All @@ -494,7 +495,7 @@ func (app *App) CliAbort() int {
app.dcs.Initialize()

err = app.dcs.Get(pathCurrentSwitch, new(Switchover))
if err == dcs.ErrNotFound {
if errors.Is(err, dcs.ErrNotFound) {
fmt.Println("no active switchover")
return 0
}
Expand Down Expand Up @@ -578,7 +579,7 @@ func (app *App) CliHostAdd(host string, priority *int, dryRun bool, skipValkeyCh

// root path probably does not exist
err = app.dcs.Create(dcs.JoinPath(pathHANodes), nil)
if err != nil && err != dcs.ErrExists {
if err != nil && !errors.Is(err, dcs.ErrExists) {
return 1
}

Expand All @@ -598,7 +599,7 @@ func (app *App) CliHostAdd(host string, priority *int, dryRun bool, skipValkeyCh

if !dryRun && priority == nil {
err = app.dcs.Set(dcs.JoinPath(pathHANodes, host), *valkey.DefaultNodeConfiguration())
if err != nil && err != dcs.ErrExists {
if err != nil && !errors.Is(err, dcs.ErrExists) {
app.logger.Error().Err(err).Msgf("Unable to create dcs path for %s", host)
return 1
}
Expand Down Expand Up @@ -632,7 +633,7 @@ func (app *App) CliHostRemove(host string) int {
app.dcs.Initialize()

err = app.dcs.Delete(dcs.JoinPath(pathHANodes, host))
if err != nil && err != dcs.ErrNotFound {
if err != nil && !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msgf("Unable to delete dcs path for %s", host)
return 1
}
Expand Down Expand Up @@ -668,7 +669,7 @@ func (app *App) processPriority(priority *int, dryRun bool, host string) (change
}

err = app.dcs.Set(dcs.JoinPath(pathHANodes, host), targetConf)
if err != nil && err != dcs.ErrExists {
if err != nil && !errors.Is(err, dcs.ErrExists) {
return false, err
}

Expand Down
3 changes: 2 additions & 1 deletion internal/app/failover.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -56,7 +57,7 @@ func (app *App) approveFailover(shardState map[string]*HostState, activeNodes []

var lastSwitchover Switchover
err := app.dcs.Get(pathLastSwitch, &lastSwitchover)
if err != dcs.ErrNotFound {
if !errors.Is(err, dcs.ErrNotFound) {
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions internal/app/maintenance.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -94,10 +95,10 @@ func (app *App) stateMaintenance() appState {
app.createMaintenanceFile()
}
maintenance, err := app.GetMaintenance()
if err != nil && err != dcs.ErrNotFound {
if err != nil && !errors.Is(err, dcs.ErrNotFound) {
return stateMaintenance
}
if err == dcs.ErrNotFound || maintenance.ShouldLeave {
if errors.Is(err, dcs.ErrNotFound) || maintenance.ShouldLeave {
if app.dcs.AcquireLock(pathManagerLock) {
app.logger.Info().Msg("Leaving maintenance")
err := app.leaveMaintenance()
Expand Down
9 changes: 5 additions & 4 deletions internal/app/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"context"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -50,7 +51,7 @@ func (app *App) stateManager() appState {
app.logger.Info().Msgf("DCS shard state: %v", shardStateDcs)

maintenance, err := app.GetMaintenance()
if err != nil && err != dcs.ErrNotFound {
if err != nil && !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msg("Failed to get maintenance from dcs")
return stateManager
}
Expand Down Expand Up @@ -94,7 +95,7 @@ func (app *App) stateManager() appState {
return stateManager
}
err = app.performSwitchover(shardState, activeNodes, &switchover, master)
if app.dcs.Get(pathCurrentSwitch, new(Switchover)) == dcs.ErrNotFound {
if errors.Is(app.dcs.Get(pathCurrentSwitch, new(Switchover)), dcs.ErrNotFound) {
app.logger.Error().Msg("Switchover was aborted")
} else {
if err != nil {
Expand All @@ -110,12 +111,12 @@ func (app *App) stateManager() appState {
}
}
return stateManager
} else if err != dcs.ErrNotFound {
} else if !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msg("Getting current switchover failed")
return stateManager
}
poisonPill, err := app.getPoisonPill()
if err != nil && err != dcs.ErrNotFound {
if err != nil && !errors.Is(err, dcs.ErrNotFound) {
app.logger.Error().Err(err).Msg("Manager: failed to get poison pill from DCS")
return stateManager
}
Expand Down
7 changes: 4 additions & 3 deletions internal/app/master.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"errors"
"fmt"
"time"

Expand All @@ -14,8 +15,8 @@ func (app *App) getNumReplicasToWrite(activeNodes []string) int {
func (app *App) getCurrentMaster(shardState map[string]*HostState) (string, error) {
var master string
err := app.dcs.Get(pathMasterNode, &master)
if err != nil && err != dcs.ErrNotFound {
return "", fmt.Errorf("failed to get current master from dcs: %s", err)
if err != nil && !errors.Is(err, dcs.ErrNotFound) {
return "", fmt.Errorf("failed to get current master from dcs: %w", err)
}
if master != "" {
stateMaster, err := app.getMasterHost(shardState)
Expand Down Expand Up @@ -84,7 +85,7 @@ func (app *App) ensureCurrentMaster(shardState map[string]*HostState) (string, e
}
err = app.dcs.Set(pathMasterNode, master)
if err != nil {
return "", fmt.Errorf("failed to set current master in dcs: %s", err)
return "", fmt.Errorf("failed to set current master in dcs: %w", err)
}
return master, nil
}
Expand Down
3 changes: 2 additions & 1 deletion internal/app/replication.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"errors"
"fmt"
"slices"
"time"
Expand Down Expand Up @@ -80,7 +81,7 @@ func (app *App) closeStaleReplica(master string) error {
app.logger.Debug().Msgf("Skipping staleness close due to switchover in progress: %v.", switchover)
return nil
}
if err != dcs.ErrNotFound {
if !errors.Is(err, dcs.ErrNotFound) {
return err
}
shardState, err := app.getShardStateFromDcs()
Expand Down
3 changes: 2 additions & 1 deletion internal/app/state.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -201,7 +202,7 @@ func (app *App) getShardStateFromDcs() (map[string]*HostState, error) {
getter := func(host string) (*HostState, error) {
var state HostState
err := app.dcs.Get(dcs.JoinPath(pathHealthPrefix, host), &state)
if err != nil && err != dcs.ErrNotFound {
if err != nil && !errors.Is(err, dcs.ErrNotFound) {
return nil, err
}
return &state, nil
Expand Down
Loading
Loading