refactor sync apply helpers
This commit is contained in:
1 parent
b56b6e0e42
commit
94cb067214
10 files changed
+37
-24
No files matched your search
+5
-3
@@ -160,10 +160,10 @@ func (client *Client) declareMyselfToCluster() {
|
||||
LastConnectedAt: now,
|
||||
}
|
||||
command := PrepareModuleConnectedCommand(module)
|
||||
syncApplyCommand(client, command, "ModuleConnectedCommand")
|
||||
syncApplyCommand(client, command)
|
||||
|
||||
declarationCommand := PrepareUpdateBackendDeclarationCommand(client.declaration)
|
||||
syncApplyCommand(client, declarationCommand, "UpdateBackendDeclarationCommand")
|
||||
syncApplyCommand(client, declarationCommand)
|
||||
}
|
||||
|
||||
type leaderState struct {
|
||||
@@ -184,15 +184,17 @@ func NewRaftClusterClient(r *raft.Raft, declaration structure.BackendDeclaration
|
||||
return client
|
||||
}
|
||||
|
||||
func syncApplyCommand(clusterClient *Client, command []byte, commandName string) {
|
||||
func syncApplyCommand(clusterClient *Client, command []byte) {
|
||||
applyLogResponse, err := clusterClient.SyncApply(command)
|
||||
if err != nil {
|
||||
commandName := ParseCommand(command).String()
|
||||
log.WithMetadata(map[string]interface{}{
|
||||
"command": string(command),
|
||||
"commandName": commandName,
|
||||
}).Warnf(codes.SyncApplyError, "announce myself. apply command: %v", err)
|
||||
}
|
||||
if applyLogResponse != nil && applyLogResponse.ApplyError != "" {
|
||||
commandName := ParseCommand(command).String()
|
||||
log.WithMetadata(map[string]interface{}{
|
||||
"result": string(applyLogResponse.Result),
|
||||
"applyError": applyLogResponse.ApplyError,
|
||||
|
||||
@@ -22,6 +22,7 @@ type Command uint64
|
||||
|
||||
// needed: go get golang.org/x/tools/cmd/stringer
|
||||
//go:generate stringer -type=Command -output const_string.go
|
||||
|
||||
const (
|
||||
_ Command = iota
|
||||
UpdateBackendDeclarationCommand
|
||||
@@ -43,6 +44,10 @@ const (
|
||||
BroadcastEventCommand
|
||||
)
|
||||
|
||||
func ParseCommand(data []byte) Command {
|
||||
return Command(binary.BigEndian.Uint64(data[:CommandSizeBytes]))
|
||||
}
|
||||
|
||||
func PrepareUpdateBackendDeclarationCommand(backend structure.BackendDeclaration) []byte {
|
||||
return prepareCommand(UpdateBackendDeclarationCommand, backend)
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ func (c *commonConfig) CreateUpdateConfig(config entity.CommonConfig) (*entity.C
|
||||
upsertConfig.Create = true
|
||||
}
|
||||
command := cluster.PrepareUpsertCommonConfigCommand(upsertConfig)
|
||||
err := PerformSyncApplyWithError(command, "UpsertCommonConfigCommand", &response)
|
||||
err := PerformSyncApplyWithError(command, &response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func (c *commonConfig) CreateUpdateConfig(config entity.CommonConfig) (*entity.C
|
||||
func (c *commonConfig) DeleteConfigs(req domain.ConfigIdRequest) (*domain.DeleteCommonConfigResponse, error) {
|
||||
var response domain.DeleteCommonConfigResponse
|
||||
command := cluster.PrepareDeleteCommonConfigsCommand(req.Id)
|
||||
err := PerformSyncApplyWithError(command, "DeleteCommonConfigsCommand", &response)
|
||||
err := PerformSyncApplyWithError(command, &response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ func (c *config) CreateUpdateConfig(config domain.CreateUpdateConfigRequest) (*d
|
||||
upsertConfig.Create = true
|
||||
}
|
||||
command := cluster.PrepareUpsertConfigCommand(upsertConfig)
|
||||
err := PerformSyncApplyWithError(command, "UpsertConfigCommand", &response)
|
||||
err := PerformSyncApplyWithError(command, &response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if response.ErrorDetails != nil {
|
||||
@@ -98,7 +98,7 @@ func (c *config) CreateUpdateConfig(config domain.CreateUpdateConfigRequest) (*d
|
||||
func (c *config) MarkConfigAsActive(identity domain.ConfigIdRequest) (*entity.Config, error) {
|
||||
var response entity.Config
|
||||
command := cluster.PrepareActivateConfigCommand(identity.Id, state.GenerateDate())
|
||||
err := PerformSyncApplyWithError(command, "ActivateConfigCommand", &response)
|
||||
err := PerformSyncApplyWithError(command, &response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -125,7 +125,7 @@ func (c *config) DeleteConfigs(identities []string) (*domain.DeleteResponse, err
|
||||
}
|
||||
var response domain.DeleteResponse
|
||||
command := cluster.PrepareDeleteConfigsCommand(identities)
|
||||
err := PerformSyncApply(command, "DeleteConfigsCommand", &response)
|
||||
err := PerformSyncApply(command, &response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -17,13 +17,15 @@ func unknownError() error {
|
||||
return status.Error(codes.Unknown, utils.ServiceError)
|
||||
}
|
||||
|
||||
func PerformSyncApply(command []byte, commandName string, responsePtr interface{}) error {
|
||||
func PerformSyncApply(command []byte, responsePtr interface{}) error {
|
||||
applyLogResponse, err := holder.ClusterClient.SyncApply(command)
|
||||
if err != nil {
|
||||
log.Warnf(codes2.SyncApplyError, "apply %s: %v", commandName, err)
|
||||
cmd := cluster.ParseCommand(command)
|
||||
log.Warnf(codes2.SyncApplyError, "apply %s: %v", cmd, err)
|
||||
return unknownError()
|
||||
}
|
||||
if applyLogResponse != nil && applyLogResponse.ApplyError != "" {
|
||||
commandName := cluster.ParseCommand(command).String()
|
||||
log.WithMetadata(map[string]interface{}{
|
||||
"result": string(applyLogResponse.Result),
|
||||
"applyError": applyLogResponse.ApplyError,
|
||||
@@ -40,13 +42,15 @@ func PerformSyncApply(command []byte, commandName string, responsePtr interface{
|
||||
return nil
|
||||
}
|
||||
|
||||
func PerformSyncApplyWithError(command []byte, commandName string, responsePtr interface{}) error {
|
||||
func PerformSyncApplyWithError(command []byte, responsePtr interface{}) error {
|
||||
applyLogResponse, err := holder.ClusterClient.SyncApply(command)
|
||||
if err != nil {
|
||||
log.Warnf(codes2.SyncApplyError, "apply %s: %v", commandName, err)
|
||||
cmd := cluster.ParseCommand(command)
|
||||
log.Warnf(codes2.SyncApplyError, "apply %s: %v", cmd, err)
|
||||
return unknownError()
|
||||
}
|
||||
if applyLogResponse != nil && applyLogResponse.ApplyError != "" {
|
||||
commandName := cluster.ParseCommand(command).String()
|
||||
log.WithMetadata(map[string]interface{}{
|
||||
"result": string(applyLogResponse.Result),
|
||||
"applyError": applyLogResponse.ApplyError,
|
||||
|
||||
@@ -56,7 +56,7 @@ func (*module) DeleteModules(identities []string) (*domain.DeleteResponse, error
|
||||
|
||||
var deleteResponse domain.DeleteResponse
|
||||
command := cluster.PrepareDeleteModulesCommand(identities)
|
||||
err := PerformSyncApply(command, "DeleteCommonConfigsCommand", &deleteResponse)
|
||||
err := PerformSyncApply(command, &deleteResponse)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -96,7 +96,7 @@ func (c *module) GetModuleByName(req domain.GetByModuleNameRequest) (*entity.Mod
|
||||
// @Router /module/broadcast_event [POST]
|
||||
func (c *module) BroadcastEvent(req cluster.BroadcastEvent) error {
|
||||
command := cluster.PrepareBroadcastEventCommand(req)
|
||||
err := PerformSyncApply(command, "BroadcastEventCommand", nil)
|
||||
err := PerformSyncApply(command, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-2
@@ -1,7 +1,6 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
@@ -33,7 +32,7 @@ func (s *Store) Apply(l *raft.Log) interface{} {
|
||||
if len(l.Data) < cluster.CommandSizeBytes {
|
||||
log.Errorf(codes.ApplyLogCommandError, "invalid log data command: %s", l.Data)
|
||||
}
|
||||
command := cluster.Command(binary.BigEndian.Uint64(l.Data[:cluster.CommandSizeBytes]))
|
||||
command := cluster.ParseCommand(l.Data)
|
||||
log.Debugf(0, "Apply %s. Data: %s", command, l.Data)
|
||||
|
||||
var (
|
||||
|
||||
+3
-3
@@ -31,7 +31,7 @@ func (h *SocketEventHandler) handleModuleReady(conn etp.Conn, data []byte) []byt
|
||||
}
|
||||
SetBackendDeclaration(conn, declaration)
|
||||
command := cluster.PrepareUpdateBackendDeclarationCommand(declaration)
|
||||
_, err = SyncApplyCommand(command, "UpdateBackendDeclarationCommand")
|
||||
_, err = SyncApplyCommand(command)
|
||||
if err != nil {
|
||||
return []byte(err.Error())
|
||||
}
|
||||
@@ -90,7 +90,7 @@ func (h *SocketEventHandler) handleConfigSchema(conn etp.Conn, data []byte) []by
|
||||
UpdatedAt: now,
|
||||
}
|
||||
command := cluster.PrepareUpdateConfigSchemaCommand(schema)
|
||||
_, _ = SyncApplyCommand(command, "UpdateConfigSchemaCommand")
|
||||
_, _ = SyncApplyCommand(command)
|
||||
|
||||
var configs []entity.Config
|
||||
h.store.VisitReadonlyState(func(readState state.ReadonlyState) {
|
||||
@@ -115,7 +115,7 @@ func (h *SocketEventHandler) handleConfigSchema(conn etp.Conn, data []byte) []by
|
||||
}
|
||||
|
||||
command := cluster.PrepareUpsertConfigCommand(upsertConfig)
|
||||
_, _ = SyncApplyCommand(command, "UpsertConfigCommand")
|
||||
_, _ = SyncApplyCommand(command)
|
||||
}
|
||||
return []byte(utils.WsOkResponse)
|
||||
}
|
||||
+4
-1
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
etp "github.com/integration-system/isp-etp-go/v2"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"isp-config-service/cluster"
|
||||
"isp-config-service/codes"
|
||||
"isp-config-service/holder"
|
||||
)
|
||||
@@ -19,9 +20,10 @@ func EmitConn(conn etp.Conn, event string, body []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
func SyncApplyCommand(command []byte, commandName string) (interface{}, error) {
|
||||
func SyncApplyCommand(command []byte) (interface{}, error) {
|
||||
applyLogResponse, err := holder.ClusterClient.SyncApply(command)
|
||||
if err != nil {
|
||||
commandName := cluster.ParseCommand(command).String()
|
||||
log.WithMetadata(map[string]interface{}{
|
||||
"command": string(command),
|
||||
"commandName": commandName,
|
||||
@@ -29,6 +31,7 @@ func SyncApplyCommand(command []byte, commandName string) (interface{}, error) {
|
||||
return nil, err
|
||||
}
|
||||
if applyLogResponse != nil && applyLogResponse.ApplyError != "" {
|
||||
commandName := cluster.ParseCommand(command).String()
|
||||
log.WithMetadata(map[string]interface{}{
|
||||
"result": string(applyLogResponse.Result),
|
||||
"applyError": applyLogResponse.ApplyError,
|
||||
|
||||
+4
-4
@@ -52,7 +52,7 @@ func (h *SocketEventHandler) handleConnect(conn etp.Conn) {
|
||||
LastConnectedAt: now,
|
||||
}
|
||||
command := cluster.PrepareModuleConnectedCommand(module)
|
||||
_, err = SyncApplyCommand(command, "ModuleConnectedCommand")
|
||||
_, err = SyncApplyCommand(command)
|
||||
if err != nil && !isClusterNode {
|
||||
EmitConn(conn, utils.ErrorConnection, FormatErrorConnection(err))
|
||||
_ = conn.Close()
|
||||
@@ -79,7 +79,7 @@ func (h *SocketEventHandler) handleConnect(conn etp.Conn) {
|
||||
EmitConn(conn, utils.ConfigSendConfigWhenConnected, data)
|
||||
}
|
||||
|
||||
func (h *SocketEventHandler) handleDisconnect(conn etp.Conn, disconnectErr error) {
|
||||
func (h *SocketEventHandler) handleDisconnect(conn etp.Conn, _ error) {
|
||||
moduleName, _ := Parameters(conn)
|
||||
log.Debugf(0, "handleDisconnect from %s", moduleName) // REMOVE
|
||||
if moduleName != "" {
|
||||
@@ -93,7 +93,7 @@ func (h *SocketEventHandler) handleDisconnect(conn etp.Conn, disconnectErr error
|
||||
LastDisconnectedAt: now,
|
||||
}
|
||||
command := cluster.PrepareModuleDisconnectedCommand(module)
|
||||
_, _ = SyncApplyCommand(command, "ModuleDisconnectedCommand")
|
||||
_, _ = SyncApplyCommand(command)
|
||||
}
|
||||
|
||||
service.DiscoveryService.HandleDisconnect(conn.ID())
|
||||
@@ -101,7 +101,7 @@ func (h *SocketEventHandler) handleDisconnect(conn etp.Conn, disconnectErr error
|
||||
backend, ok := ExtractBackendDeclaration(conn)
|
||||
if ok {
|
||||
command := cluster.PrepareDeleteBackendDeclarationCommand(backend)
|
||||
_, _ = SyncApplyCommand(command, "DeleteBackendDeclarationCommand")
|
||||
_, _ = SyncApplyCommand(command)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user