Compare commits

...
2 Commits
Author SHA1 Message Date
Maxim Merzhanov 9d61ec49c3 improve logging, regenerate doc 2020-03-06 14:07:59 +03:00
Maxim Merzhanov 9dc598feff up .version file 2020-02-26 17:53:09 +03:00
10 changed files with 102 additions and 184 deletions

No files matched your search

+1 -1
View File
@@ -1 +1 @@
2.1.1
2.1.3
+3
View File
@@ -1,3 +1,6 @@
### v2.1.3
* improved logging
### v2.1.2
* leader ws client ConnectionReadLimit is now configured by WS.WsConnectionReadLimitKB config param and defaults to 4 MB
+41 -25
View File
@@ -2,6 +2,9 @@ package cluster
import (
"errors"
"sync"
"time"
"github.com/integration-system/isp-lib/structure"
"github.com/integration-system/isp-lib/utils"
log "github.com/integration-system/isp-log"
@@ -10,8 +13,6 @@ import (
"isp-config-service/entity"
"isp-config-service/raft"
"isp-config-service/store/state"
"sync"
"time"
)
var (
@@ -105,6 +106,12 @@ func (client *Client) SyncApplyOnLeader(command []byte) (*ApplyLogResponse, erro
func (client *Client) listenLeader() {
for n := range client.r.LeaderCh() {
client.leaderMu.Lock()
log.WithMetadata(map[string]interface{}{
"leader_elected": n.LeaderElected,
"current_leader": n.CurrentLeaderAddress,
"is_leader": n.IsLeader,
}).Info(codes.LeaderStateChanged, "leader state changed")
if client.leaderClient != nil {
log.Debugf(0, "close previous leader ws connection %s", client.leaderState.leaderAddr)
client.leaderClient.Close()
@@ -120,29 +127,10 @@ func (client *Client) listenLeader() {
}
client.leaderClient = leaderClient
go func(declaration structure.BackendDeclaration) {
response, err := leaderClient.SendDeclaration(declaration, defaultApplyTimeout)
if err != nil {
log.Warnf(codes.SendDeclarationToLeaderError, "send declaration to leader. err: %v", err)
} else if response != utils.WsOkResponse {
log.Warnf(codes.SendDeclarationToLeaderError, "send declaration to leader. response: %s", response)
}
}(client.declaration)
log.Info(codes.SendDeclarationToLeader, "sending declaration to leader through websocket")
go client.declareMyselfToLeader(leaderClient)
} else if n.LeaderElected && n.IsLeader {
go func(declaration structure.BackendDeclaration) {
now := state.GenerateDate()
module := entity.Module{
Id: state.GenerateId(),
Name: declaration.ModuleName,
CreatedAt: now,
LastConnectedAt: now,
}
command := PrepareModuleConnectedCommand(module)
syncApplyCommand(client, command, "ModuleConnectedCommand")
declarationCommand := PrepareUpdateBackendDeclarationCommand(declaration)
syncApplyCommand(client, declarationCommand, "UpdateBackendDeclarationCommand")
}(client.declaration)
go client.declareMyselfToCluster()
}
client.leaderState = leaderState{
leaderElected: n.LeaderElected,
@@ -153,6 +141,31 @@ func (client *Client) listenLeader() {
client.leaderMu.Unlock()
}
}
func (client *Client) declareMyselfToLeader(leaderClient *SocketLeaderClient) {
response, err := leaderClient.SendDeclaration(client.declaration, defaultApplyTimeout)
if err != nil {
log.Warnf(codes.SendDeclarationToLeader, "send declaration to leader. err: %v", err)
} else if response != utils.WsOkResponse {
log.Warnf(codes.SendDeclarationToLeader, "send declaration to leader. response: %s", response)
}
}
// used when a leader need to declare himself to a cluster
func (client *Client) declareMyselfToCluster() {
now := state.GenerateDate()
module := entity.Module{
Id: state.GenerateId(),
Name: client.declaration.ModuleName,
CreatedAt: now,
LastConnectedAt: now,
}
command := PrepareModuleConnectedCommand(module)
syncApplyCommand(client, command, "ModuleConnectedCommand")
declarationCommand := PrepareUpdateBackendDeclarationCommand(client.declaration)
syncApplyCommand(client, declarationCommand, "UpdateBackendDeclarationCommand")
}
type leaderState struct {
leaderElected bool
@@ -175,7 +188,10 @@ func NewRaftClusterClient(r *raft.Raft, declaration structure.BackendDeclaration
func syncApplyCommand(clusterClient *Client, command []byte, commandName string) {
applyLogResponse, err := clusterClient.SyncApply(command)
if err != nil {
log.Warnf(codes.SyncApplyError, "announce myself. apply %s: %v", commandName, err)
log.WithMetadata(map[string]interface{}{
"command": string(command),
"commandName": commandName,
}).Warnf(codes.SyncApplyError, "announce myself. apply command: %v", err)
}
if applyLogResponse != nil && applyLogResponse.ApplyError != "" {
log.WithMetadata(map[string]interface{}{
+4 -2
View File
@@ -84,7 +84,7 @@ func NewSocketLeaderClient(address string, leaderDisconnectionCallback func()) *
leaderClient.client.OnDisconnect(func(err error) {
log.WithMetadata(map[string]interface{}{
"leaderAddr": address,
}).Warn(codes.LeaderClientDisconnected, "leader client disconnected")
}).Warn(codes.LeaderClientDisconnected, "disconnected ws from leader")
leaderDisconnectionCallback()
})
@@ -92,7 +92,9 @@ func NewSocketLeaderClient(address string, leaderDisconnectionCallback func()) *
log.Warnf(codes.LeaderClientConnectionError, "leader client on error: %v", err)
})
leaderClient.client.OnConnect(func() {
log.Debug(0, "leader client connected")
log.WithMetadata(map[string]interface{}{
"leaderAddr": address,
}).Info(codes.LeaderClientConnected, "connected to new leader")
})
return leaderClient
}
+6 -3
View File
@@ -25,9 +25,12 @@ const (
SyncApplyError = 1015
// Raft Cluster
SendDeclarationToLeaderError = 1016
LeaderClientConnectionError = 1017
LeaderClientDisconnected = 1018
SendDeclarationToLeader = 1016
LeaderClientConnectionError = 1017
LeaderClientDisconnected = 1018
LeaderClientConnected = 1023
LeaderStateChanged = 1024
LeaderManualDeleteLeader = 1025
// Services
DiscoveryServiceSendModulesError = 1019
+35 -146
View File
@@ -2,7 +2,8 @@ basePath: /api/config
definitions:
domain.CommonConfigLinks:
additionalProperties:
items: {}
items:
type: string
type: array
type: object
domain.CompileConfigsRequest:
@@ -12,11 +13,11 @@ definitions:
type: string
type: array
data:
additionalProperties: true
type: object
type: object
domain.CompiledConfigResponse:
additionalProperties:
type: object
additionalProperties: true
type: object
domain.ConfigIdRequest:
properties:
@@ -204,8 +205,7 @@ definitions:
- name
type: object
entity.ConfigData:
additionalProperties:
type: object
additionalProperties: true
type: object
entity.ConfigSchema:
properties:
@@ -239,7 +239,8 @@ definitions:
- name
type: object
jsonschema.Definitions:
additionalProperties: {}
additionalProperties:
$ref: '#/definitions/jsonschema.Type'
type: object
jsonschema.Schema:
properties:
@@ -251,19 +252,19 @@ definitions:
type: string
additionalItems:
description: section 5.9
type: string
type: integer
additionalProperties:
description: section 5.18
type: string
allOf:
description: section 5.22
items:
$ref: '#/definitions/jsonschema.Type'
type: integer
type: array
anyOf:
description: section 5.23
items:
$ref: '#/definitions/jsonschema.Type'
type: integer
type: array
binaryEncoding:
description: section 4.3
@@ -275,6 +276,8 @@ definitions:
$ref: '#/definitions/jsonschema.Definitions'
type: object
dependencies:
additionalProperties:
$ref: '#/definitions/jsonschema.Type'
description: section 5.19
type: object
description:
@@ -296,7 +299,7 @@ definitions:
type: string
items:
description: section 5.9
type: string
type: integer
maxItems:
description: section 5.10
type: integer
@@ -311,7 +314,7 @@ definitions:
type: integer
media:
description: RFC draft-wright-json-schema-hyperschema-00, section 4
type: string
type: integer
minItems:
description: section 5.11
type: integer
@@ -329,19 +332,23 @@ definitions:
type: integer
not:
description: section 5.25
type: string
type: integer
oneOf:
description: section 5.24
items:
$ref: '#/definitions/jsonschema.Type'
type: integer
type: array
pattern:
description: section 5.8
type: string
patternProperties:
additionalProperties:
$ref: '#/definitions/jsonschema.Type'
description: section 5.17
type: object
properties:
additionalProperties:
$ref: '#/definitions/jsonschema.Type'
description: section 5.16
type: object
required:
@@ -369,19 +376,19 @@ definitions:
type: string
additionalItems:
description: section 5.9
type: string
type: integer
additionalProperties:
description: section 5.18
type: string
allOf:
description: section 5.22
items:
$ref: '#/definitions/jsonschema.Type'
type: integer
type: array
anyOf:
description: section 5.23
items:
$ref: '#/definitions/jsonschema.Type'
type: integer
type: array
binaryEncoding:
description: section 4.3
@@ -394,6 +401,8 @@ definitions:
description: section 5.26
type: object
dependencies:
additionalProperties:
$ref: '#/definitions/jsonschema.Type'
description: section 5.19
type: object
description:
@@ -415,7 +424,7 @@ definitions:
type: string
items:
description: section 5.9
type: string
type: integer
maxItems:
description: section 5.10
type: integer
@@ -430,7 +439,7 @@ definitions:
type: integer
media:
description: RFC draft-wright-json-schema-hyperschema-00, section 4
type: string
type: integer
minItems:
description: section 5.11
type: integer
@@ -448,19 +457,23 @@ definitions:
type: integer
not:
description: section 5.25
type: string
type: integer
oneOf:
description: section 5.24
items:
$ref: '#/definitions/jsonschema.Type'
type: integer
type: array
pattern:
description: section 5.8
type: string
patternProperties:
additionalProperties:
$ref: '#/definitions/jsonschema.Type'
description: section 5.17
type: object
properties:
additionalProperties:
$ref: '#/definitions/jsonschema.Type'
description: section 5.16
type: object
required:
@@ -479,123 +492,7 @@ definitions:
type: boolean
type: object
schema.Schema:
properties:
$ref:
description: section 7
type: string
$schema:
description: RFC draft-wright-json-schema-00
type: string
additionalItems:
description: section 5.9
type: string
additionalProperties:
description: section 5.18
type: string
allOf:
description: section 5.22
items:
$ref: '#/definitions/jsonschema.Type'
type: array
anyOf:
description: section 5.23
items:
$ref: '#/definitions/jsonschema.Type'
type: array
binaryEncoding:
description: section 4.3
type: string
default:
description: section 6.2
type: object
definitions:
$ref: '#/definitions/jsonschema.Definitions'
type: object
dependencies:
description: section 5.19
type: object
description:
description: section 6.1
type: string
enum:
description: section 5.20
items:
type: object
type: array
exclusiveMaximum:
description: section 5.3
type: boolean
exclusiveMinimum:
description: section 5.5
type: boolean
format:
description: section 7
type: string
items:
description: section 5.9
type: string
maxItems:
description: section 5.10
type: integer
maxLength:
description: section 5.6
type: integer
maxProperties:
description: section 5.13
type: integer
maximum:
description: section 5.2
type: integer
media:
description: RFC draft-wright-json-schema-hyperschema-00, section 4
type: string
minItems:
description: section 5.11
type: integer
minLength:
description: section 5.7
type: integer
minProperties:
description: section 5.14
type: integer
minimum:
description: section 5.4
type: integer
multipleOf:
description: RFC draft-wright-json-schema-validation-00, section 5
type: integer
not:
description: section 5.25
type: string
oneOf:
description: section 5.24
items:
$ref: '#/definitions/jsonschema.Type'
type: array
pattern:
description: section 5.8
type: string
patternProperties:
description: section 5.17
type: object
properties:
description: section 5.16
type: object
required:
description: section 5.15
items:
type: string
type: array
title:
description: RFC draft-wright-json-schema-validation-00, section 6, 7
type: string
type:
description: section 5.21
type: string
uniqueItems:
description: section 5.12
type: boolean
type: object
$ref: '#/definitions/jsonschema.Schema'
structure.AddressConfiguration:
properties:
ip:
@@ -622,6 +519,7 @@ definitions:
structure.EndpointDescriptor:
properties:
extra:
additionalProperties: true
type: object
inner:
type: boolean
@@ -664,7 +562,6 @@ paths:
required: true
schema:
$ref: '#/definitions/domain.CompileConfigsRequest'
type: object
produces:
- application/json
responses:
@@ -688,7 +585,6 @@ paths:
required: true
schema:
$ref: '#/definitions/entity.CommonConfig'
type: object
produces:
- application/json
responses:
@@ -720,7 +616,6 @@ paths:
required: true
schema:
$ref: '#/definitions/domain.ConfigIdRequest'
type: object
produces:
- application/json
responses:
@@ -766,7 +661,6 @@ paths:
required: true
schema:
$ref: '#/definitions/domain.ConfigIdRequest'
type: object
produces:
- application/json
responses:
@@ -790,7 +684,6 @@ paths:
required: true
schema:
$ref: '#/definitions/domain.CreateUpdateConfigRequest'
type: object
produces:
- application/json
responses:
@@ -853,7 +746,6 @@ paths:
required: true
schema:
$ref: '#/definitions/domain.GetByModuleNameRequest'
type: object
produces:
- application/json
responses:
@@ -885,7 +777,6 @@ paths:
required: true
schema:
$ref: '#/definitions/domain.ConfigIdRequest'
type: object
produces:
- application/json
responses:
@@ -944,7 +835,6 @@ paths:
required: true
schema:
$ref: '#/definitions/domain.GetByModuleNameRequest'
type: object
produces:
- application/json
responses:
@@ -1014,7 +904,6 @@ paths:
required: true
schema:
$ref: '#/definitions/domain.GetByModuleIdRequest'
type: object
produces:
- application/json
responses:
+1 -1
View File
@@ -170,7 +170,7 @@ func initRaft(listener net.Listener, clusterCfg conf.ClusterConfiguration, decla
addressConfiguration := structure.AddressConfiguration{Port: port, IP: addr.IP.String()}
back := structure.BackendDeclaration{ModuleName: cfg.ModuleName, Address: addressConfiguration}
log.WithMetadata(map[string]interface{}{"declaration": back}).
Debugf(0, "delete disconnected leader's declaration")
Infof(codes.LeaderManualDeleteLeader, "manually delete disconnected leader's declaration")
service.ClusterMeshService.HandleDeleteBackendDeclarationCommand(back, s)
})
})
+4 -3
View File
@@ -2,13 +2,14 @@ package raft
import (
"fmt"
"io"
"log"
"github.com/hashicorp/go-hclog"
isplog "github.com/integration-system/isp-log"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
"io"
"isp-config-service/codes"
"log"
)
type LoggerAdapter struct {
@@ -17,7 +18,7 @@ type LoggerAdapter struct {
func (l *LoggerAdapter) Log(level logrus.Level, msg string, args ...interface{}) {
if l.name != "" {
msg = fmt.Sprintf("%s:%s", l.name, msg)
msg = fmt.Sprintf("%s: %s", l.name, msg)
}
if len(args) != 0 {
metadata := make(map[string]interface{}, len(args)/2)
+5 -1
View File
@@ -3,6 +3,7 @@ package subs
import (
"context"
"errors"
etp "github.com/integration-system/isp-etp-go"
log "github.com/integration-system/isp-log"
"isp-config-service/codes"
@@ -21,7 +22,10 @@ func EmitConn(conn etp.Conn, event string, body []byte) {
func SyncApplyCommand(command []byte, commandName string) (interface{}, error) {
applyLogResponse, err := holder.ClusterClient.SyncApply(command)
if err != nil {
log.Warnf(codes.SyncApplyError, "apply %s: %v", commandName, err)
log.WithMetadata(map[string]interface{}{
"command": string(command),
"commandName": commandName,
}).Warnf(codes.SyncApplyError, "apply command: %v", err)
return nil, err
}
if applyLogResponse != nil && applyLogResponse.ApplyError != "" {
+2 -2
View File
@@ -37,7 +37,7 @@ func (h *SocketEventHandler) SubscribeAll() {
func (h *SocketEventHandler) handleConnect(conn etp.Conn) {
isClusterNode := IsConfigClusterNode(conn)
moduleName, err := Parameters(conn)
log.Debugf(0, "handleConnect: %s", moduleName) // REMOVE
log.Debugf(0, "handleConnect from %s", moduleName) // REMOVE
if err != nil {
EmitConn(conn, utils.ErrorConnection, FormatErrorConnection(err))
_ = conn.Close()
@@ -81,7 +81,7 @@ func (h *SocketEventHandler) handleConnect(conn etp.Conn) {
func (h *SocketEventHandler) handleDisconnect(conn etp.Conn, disconnectErr error) {
moduleName, _ := Parameters(conn)
log.Debugf(0, "handleDisconnect: %s", moduleName) // REMOVE
log.Debugf(0, "handleDisconnect from %s", moduleName) // REMOVE
if moduleName != "" {
holder.EtpServer.Rooms().Leave(conn, moduleName+service.ConfigWatchersRoomSuffix)
now := state.GenerateDate()