diff --git a/cluster/client.go b/cluster/client.go index 93b057d..5a11a73 100644 --- a/cluster/client.go +++ b/cluster/client.go @@ -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, diff --git a/cluster/const.go b/cluster/const.go index b52fa51..4729459 100644 --- a/cluster/const.go +++ b/cluster/const.go @@ -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) } diff --git a/controller/common_config.go b/controller/common_config.go index b531f97..48416cb 100644 --- a/controller/common_config.go +++ b/controller/common_config.go @@ -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 } diff --git a/controller/config.go b/controller/config.go index 81db778..22c50b3 100644 --- a/controller/config.go +++ b/controller/config.go @@ -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 } diff --git a/controller/helpers.go b/controller/helpers.go index 0895ccc..32f851a 100644 --- a/controller/helpers.go +++ b/controller/helpers.go @@ -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, diff --git a/controller/module.go b/controller/module.go index 344f772..21be8f2 100644 --- a/controller/module.go +++ b/controller/module.go @@ -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 } diff --git a/store/store.go b/store/store.go index 42f3131..994b7da 100644 --- a/store/store.go +++ b/store/store.go @@ -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 ( diff --git a/subs/client.go b/subs/client.go index 2634489..1e4f3d3 100644 --- a/subs/client.go +++ b/subs/client.go @@ -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) } diff --git a/subs/helpers.go b/subs/helpers.go index 152a7c2..49d845a 100644 --- a/subs/helpers.go +++ b/subs/helpers.go @@ -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, diff --git a/subs/root.go b/subs/root.go index 54a160d..4879787 100644 --- a/subs/root.go +++ b/subs/root.go @@ -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) } }