-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli, checker): add key for local triggers ids (#967)
- Loading branch information
1 parent
ae71a96
commit f31ac9d
Showing
6 changed files
with
164 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/moira-alert/moira" | ||
) | ||
|
||
type unknownDBError struct { | ||
database reflect.Type | ||
} | ||
|
||
func makeUnknownDBError(database moira.Database) unknownDBError { | ||
return unknownDBError{ | ||
database: reflect.TypeOf(database), | ||
} | ||
} | ||
|
||
func (err unknownDBError) Error() string { | ||
return fmt.Sprintf("Unknown implementation of moira.Database: %s", err.database.Name()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/moira-alert/moira" | ||
"github.com/moira-alert/moira/database/redis" | ||
) | ||
|
||
func updateFrom29(logger moira.Logger, database moira.Database) error { | ||
logger.Info().Msg("Update 2.9 -> 2.10 was started") | ||
|
||
ctx := context.Background() | ||
err := createKeyForLocalTriggers(ctx, logger, database) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Info().Msg("Update 2.9 -> 2.10 was finished") | ||
return nil | ||
} | ||
|
||
func downgradeTo29(logger moira.Logger, database moira.Database) error { | ||
logger.Info().Msg("Downgrade 2.10 -> 2.9 started") | ||
|
||
ctx := context.Background() | ||
err := revertCreateKeyForLocalTriggers(ctx, logger, database) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Info().Msg("Downgrade 2.10 -> 2.9 was finished") | ||
return nil | ||
} | ||
|
||
var triggersListKey = "{moira-triggers-list}:moira-triggers-list" | ||
var localTriggersListKey = "{moira-triggers-list}:moira-local-triggers-list" | ||
var remoteTriggersListKey = "{moira-triggers-list}:moira-remote-triggers-list" | ||
var prometheusTriggersListKey = "{moira-triggers-list}:moira-prometheus-triggers-list" | ||
|
||
func createKeyForLocalTriggers(ctx context.Context, logger moira.Logger, database moira.Database) error { | ||
logger.Info().Msg("Start createKeyForLocalTriggers") | ||
|
||
switch d := database.(type) { | ||
case *redis.DbConnector: | ||
pipe := d.Client().TxPipeline() | ||
|
||
localTriggerIds, err := pipe.SDiff(ctx, triggersListKey, remoteTriggersListKey, prometheusTriggersListKey).Result() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Info().Msg("Finish getting local trigger IDs") | ||
|
||
_, err = pipe.SAdd(ctx, localTriggersListKey, localTriggerIds).Result() | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
return makeUnknownDBError(database) | ||
} | ||
|
||
logger.Info().Msg("Successfully finished createKeyForLocalTriggers") | ||
|
||
return nil | ||
} | ||
|
||
func revertCreateKeyForLocalTriggers(ctx context.Context, logger moira.Logger, database moira.Database) error { | ||
logger.Info().Msg("Start revertCreateKeyForLocalTriggers") | ||
|
||
switch d := database.(type) { | ||
case *redis.DbConnector: | ||
pipe := d.Client().TxPipeline() | ||
|
||
_, err := pipe.Del(ctx, localTriggersListKey).Result() | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
return makeUnknownDBError(database) | ||
} | ||
|
||
logger.Info().Msg("Successfully finished revertCreateKeyForLocalTriggers") | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.