Skip to content

Commit

Permalink
[1.2.0] Public Log Interface [API-872] (#686)
Browse files Browse the repository at this point in the history
* move logger interface to outer log package

* add custom logger extension

* modify CanLog functionality and refactor usages and tests

* minor fixes

* refactor public log interface

* quick fix

* quick fix

* fix logger usage in test

* add critical logging level

* revert logger package name change since it is not backward compatible

* fix import statements

* minor fixes

* remove intermediate interface

* change field ordering for struct alignment

* enable error logging for Error() and Errorf()

* remove SetLogger api from client

* shorten function body

* apply review suggestions

* test fix

* apply review suggestions

* reformat log level comments

* change critical level to fatal

* small fix

* refactor for log weight

* provide package level documentation and example

* rename custom logger config field

* update old documentation
  • Loading branch information
utku-caglayan authored Dec 20, 2021
1 parent 04a3f35 commit 40b05ce
Show file tree
Hide file tree
Showing 27 changed files with 340 additions and 316 deletions.
46 changes: 28 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func StartNewClientWithConfig(ctx context.Context, config Config) (*Client, erro
// It connects to one or more of the cluster members and delegates all cluster wide operations to them.
type Client struct {
invocationHandler invocation.Handler
logger ilogger.Logger
logger ilogger.LogAdaptor
membershipListenerMapMu *sync.Mutex
connectionManager *icluster.ConnectionManager
clusterService *icluster.Service
Expand All @@ -88,8 +88,8 @@ type Client struct {
heartbeatService *icluster.HeartbeatService
clusterConfig *cluster.Config
membershipListenerMap map[types.UUID]int64
lifecyleListenerMap map[types.UUID]int64
lifecyleListenerMapMu *sync.Mutex
lifecycleListenerMap map[types.UUID]int64
lifecycleListenerMapMu *sync.Mutex
name string
state int32
}
Expand All @@ -107,24 +107,23 @@ func newClient(config Config) (*Client, error) {
if name == "" {
name = fmt.Sprintf("hz.client_%d", id)
}
logLevel, err := ilogger.GetLogLevel(config.Logger.Level)
serializationService, err := serialization.NewService(&config.Serialization)
if err != nil {
return nil, err
}
clientLogger := ilogger.NewWithLevel(logLevel)
serializationService, err := serialization.NewService(&config.Serialization)
clientLogger, err := loggerFromConf(config)
if err != nil {
return nil, err
}
clientLogger.Trace(func() string { return fmt.Sprintf("creating new client: %s", name) })
c := &Client{
name: name,
logger: clientLogger,
clusterConfig: &config.Cluster,
serializationService: serializationService,
eventDispatcher: event.NewDispatchService(clientLogger),
logger: clientLogger,
lifecyleListenerMap: map[types.UUID]int64{},
lifecyleListenerMapMu: &sync.Mutex{},
lifecycleListenerMap: map[types.UUID]int64{},
lifecycleListenerMapMu: &sync.Mutex{},
membershipListenerMap: map[types.UUID]int64{},
membershipListenerMapMu: &sync.Mutex{},
}
Expand All @@ -133,6 +132,17 @@ func newClient(config Config) (*Client, error) {
return c, nil
}

func loggerFromConf(config Config) (ilogger.LogAdaptor, error) {
if config.Logger.CustomLogger != nil {
return ilogger.LogAdaptor{Logger: config.Logger.CustomLogger}, nil
}
logger, err := ilogger.NewWithLevel(config.Logger.Level)
if err != nil {
return ilogger.LogAdaptor{}, err
}
return ilogger.LogAdaptor{Logger: logger}, nil
}

// Name returns client's name
// Use config.Name to set the client name.
// If not set manually, an automatically generated name is used.
Expand Down Expand Up @@ -286,9 +296,9 @@ func (c *Client) AddLifecycleListener(handler LifecycleStateChangeHandler) (type
uuid := types.NewUUID()
subscriptionID := event.NextSubscriptionID()
c.addLifecycleListener(subscriptionID, handler)
c.lifecyleListenerMapMu.Lock()
c.lifecyleListenerMap[uuid] = subscriptionID
c.lifecyleListenerMapMu.Unlock()
c.lifecycleListenerMapMu.Lock()
c.lifecycleListenerMap[uuid] = subscriptionID
c.lifecycleListenerMapMu.Unlock()
return uuid, nil
}

Expand All @@ -297,12 +307,12 @@ func (c *Client) RemoveLifecycleListener(subscriptionID types.UUID) error {
if atomic.LoadInt32(&c.state) >= stopping {
return hzerrors.ErrClientNotActive
}
c.lifecyleListenerMapMu.Lock()
if intID, ok := c.lifecyleListenerMap[subscriptionID]; ok {
c.lifecycleListenerMapMu.Lock()
if intID, ok := c.lifecycleListenerMap[subscriptionID]; ok {
c.eventDispatcher.Unsubscribe(eventLifecycleEventStateChanged, intID)
delete(c.lifecyleListenerMap, subscriptionID)
delete(c.lifecycleListenerMap, subscriptionID)
}
c.lifecyleListenerMapMu.Unlock()
c.lifecycleListenerMapMu.Unlock()
return nil
}

Expand Down Expand Up @@ -410,7 +420,7 @@ func (c *Client) addConfigEvents(config *Config) {
for uuid, handler := range config.lifecycleListeners {
subscriptionID := event.NextSubscriptionID()
c.addLifecycleListener(subscriptionID, handler)
c.lifecyleListenerMap[uuid] = subscriptionID
c.lifecycleListenerMap[uuid] = subscriptionID
}
for uuid, handler := range config.membershipListeners {
subscriptionID := event.NextSubscriptionID()
Expand Down Expand Up @@ -532,7 +542,7 @@ func (c *Client) handleClusterEvent(e event.Event) {
}
}

func addrProviderTranslator(config *cluster.Config, logger ilogger.Logger) (icluster.AddressProvider, icluster.AddressTranslator) {
func addrProviderTranslator(config *cluster.Config, logger ilogger.LogAdaptor) (icluster.AddressProvider, icluster.AddressTranslator) {
if config.Cloud.Enabled {
dc := cloud.NewDiscoveryClient(&config.Cloud, logger)
return cloud.NewAddressProvider(dc), cloud.NewAddressTranslator(dc)
Expand Down
1 change: 1 addition & 0 deletions client_internals_it_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build hazelcastinternal
// +build hazelcastinternal

/*
Expand Down
4 changes: 2 additions & 2 deletions internal/cloud/cloud_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import (
const envCoordinatorBaseURL = "HZ_CLOUD_COORDINATOR_BASE_URL"

type DiscoveryClient struct {
logger logger.Logger
logger logger.LogAdaptor
httpClient *rest.HTTPClient
token string
}

func NewDiscoveryClient(config *cluster.CloudConfig, logger logger.Logger) *DiscoveryClient {
func NewDiscoveryClient(config *cluster.CloudConfig, logger logger.LogAdaptor) *DiscoveryClient {
return &DiscoveryClient{
token: config.Token,
httpClient: rest.NewHTTPClient(),
Expand Down
14 changes: 7 additions & 7 deletions internal/cluster/cluster_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (
pubcluster "github.com/hazelcast/hazelcast-go-client/cluster"
"github.com/hazelcast/hazelcast-go-client/internal/event"
"github.com/hazelcast/hazelcast-go-client/internal/invocation"
ilogger "github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/proto"
"github.com/hazelcast/hazelcast-go-client/internal/proto/codec"
"github.com/hazelcast/hazelcast-go-client/types"
)

type Service struct {
logger ilogger.Logger
logger logger.LogAdaptor
config *pubcluster.Config
eventDispatcher *event.DispatchService
partitionService *PartitionService
Expand All @@ -44,7 +44,7 @@ type Service struct {
}

type CreationBundle struct {
Logger ilogger.Logger
Logger logger.LogAdaptor
InvocationFactory *ConnectionInvocationFactory
EventDispatcher *event.DispatchService
PartitionService *PartitionService
Expand All @@ -62,8 +62,8 @@ func (b CreationBundle) Check() {
if b.PartitionService == nil {
panic("PartitionService is nil")
}
if b.Logger == nil {
panic("Logger is nil")
if b.Logger.Logger == nil {
panic("LogAdaptor is nil")
}
if b.Config == nil {
panic("Config is nil")
Expand Down Expand Up @@ -175,7 +175,7 @@ func (a AddrSet) Addrs() []pubcluster.Address {
}

type membersMap struct {
logger ilogger.Logger
logger logger.LogAdaptor
failoverService *FailoverService
members map[types.UUID]*pubcluster.MemberInfo
addrToMemberUUID map[pubcluster.Address]types.UUID
Expand All @@ -184,7 +184,7 @@ type membersMap struct {
version int32
}

func newMembersMap(failoverService *FailoverService, lg ilogger.Logger) membersMap {
func newMembersMap(failoverService *FailoverService, lg logger.LogAdaptor) membersMap {
mm := membersMap{
membersMu: &sync.RWMutex{},
failoverService: failoverService,
Expand Down
10 changes: 5 additions & 5 deletions internal/cluster/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/hazelcast/hazelcast-go-client/internal/event"
ihzerrors "github.com/hazelcast/hazelcast-go-client/internal/hzerrors"
"github.com/hazelcast/hazelcast-go-client/internal/invocation"
ilogger "github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/proto"
"github.com/hazelcast/hazelcast-go-client/internal/proto/codec"
"github.com/hazelcast/hazelcast-go-client/types"
Expand All @@ -60,7 +60,7 @@ type Connection struct {
socket net.Conn
bWriter *bufio.Writer
endpoint atomic.Value
logger ilogger.Logger
logger logger.LogAdaptor
lastRead atomic.Value
clusterConfig *pubcluster.Config
eventDispatcher *event.DispatchService
Expand Down Expand Up @@ -142,13 +142,13 @@ func (c *Connection) dialToAddressWithTimeout(addr pubcluster.Address, conTimeou
} else {
tcpConn := conn.(*net.TCPConn)
if err = tcpConn.SetNoDelay(false); err != nil {
c.logger.Warnf("error setting tcp no delay: %w", err)
c.logger.Warnf("error setting tcp no delay: %v", err)
}
if err = tcpConn.SetReadBuffer(socketBufferSize); err != nil {
c.logger.Warnf("error setting read buffer: %w", err)
c.logger.Warnf("error setting read buffer: %v", err)
}
if err = tcpConn.SetWriteBuffer(socketBufferSize); err != nil {
c.logger.Warnf("error setting write buffer: %w", err)
c.logger.Warnf("error setting write buffer: %v", err)
}
return tcpConn, nil
}
Expand Down
10 changes: 5 additions & 5 deletions internal/cluster/connection_invocation_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import (
pubcluster "github.com/hazelcast/hazelcast-go-client/cluster"
ihzerrors "github.com/hazelcast/hazelcast-go-client/internal/hzerrors"
"github.com/hazelcast/hazelcast-go-client/internal/invocation"
ilogger "github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/logger"
)

var errPartitionOwnerNotAssigned = errors.New("partition owner not assigned")

type ConnectionInvocationHandlerCreationBundle struct {
ConnectionManager *ConnectionManager
ClusterService *Service
Logger ilogger.Logger
Logger logger.LogAdaptor
Config *pubcluster.Config
}

Expand All @@ -42,16 +42,16 @@ func (b ConnectionInvocationHandlerCreationBundle) Check() {
if b.ClusterService == nil {
panic("ClusterService is nil")
}
if b.Logger == nil {
panic("Logger is nil")
if b.Logger.Logger == nil {
panic("LogAdaptor is nil")
}
if b.Config == nil {
panic("Config is nil")
}
}

type ConnectionInvocationHandler struct {
logger ilogger.Logger
logger logger.LogAdaptor
connectionManager *ConnectionManager
clusterService *Service
smart bool
Expand Down
4 changes: 2 additions & 2 deletions internal/cluster/connection_listener_binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type listenerRegistration struct {
}

type ConnectionListenerBinder struct {
logger logger.Logger
logger logger.LogAdaptor
connectionManager *ConnectionManager
invocationFactory *ConnectionInvocationFactory
eventDispatcher *event.DispatchService
Expand All @@ -57,7 +57,7 @@ func NewConnectionListenerBinder(
invocationService *invocation.Service,
invocationFactory *ConnectionInvocationFactory,
eventDispatcher *event.DispatchService,
logger logger.Logger,
logger logger.LogAdaptor,
smart bool) *ConnectionListenerBinder {
binder := &ConnectionListenerBinder{
connectionManager: connManager,
Expand Down
10 changes: 5 additions & 5 deletions internal/cluster/connection_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
ihzerrors "github.com/hazelcast/hazelcast-go-client/internal/hzerrors"
"github.com/hazelcast/hazelcast-go-client/internal/invocation"
"github.com/hazelcast/hazelcast-go-client/internal/lifecycle"
ilogger "github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/proto"
"github.com/hazelcast/hazelcast-go-client/internal/proto/codec"
"github.com/hazelcast/hazelcast-go-client/internal/security"
Expand Down Expand Up @@ -65,7 +65,7 @@ var connectionManagerSubID = event.NextSubscriptionID()
type connectMemberFunc func(ctx context.Context, m *ConnectionManager, addr pubcluster.Address) (pubcluster.Address, error)

type ConnectionManagerCreationBundle struct {
Logger ilogger.Logger
Logger logger.LogAdaptor
PartitionService *PartitionService
InvocationFactory *ConnectionInvocationFactory
ClusterConfig *pubcluster.Config
Expand All @@ -80,8 +80,8 @@ type ConnectionManagerCreationBundle struct {
}

func (b ConnectionManagerCreationBundle) Check() {
if b.Logger == nil {
panic("Logger is nil")
if b.Logger.Logger == nil {
panic("LogAdaptor is nil")
}
if b.ClusterService == nil {
panic("ClusterService is nil")
Expand Down Expand Up @@ -117,7 +117,7 @@ func (b ConnectionManagerCreationBundle) Check() {

type ConnectionManager struct {
nextConnID int64 // This field should be at the top: https://pkg.go.dev/sync/atomic#pkg-note-BUG
logger ilogger.Logger
logger logger.LogAdaptor
isClientShutDown func() bool
failoverConfig *pubcluster.FailoverConfig
partitionService *PartitionService
Expand Down
6 changes: 3 additions & 3 deletions internal/cluster/failover_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"sync/atomic"

pubcluster "github.com/hazelcast/hazelcast-go-client/cluster"
ilogger "github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/security"
)

Expand All @@ -39,9 +39,9 @@ type CandidateCluster struct {
ClusterName string
}

type addrFun func(*pubcluster.Config, ilogger.Logger) (AddressProvider, AddressTranslator)
type addrFun func(*pubcluster.Config, logger.LogAdaptor) (AddressProvider, AddressTranslator)

func NewFailoverService(logger ilogger.Logger, maxTries int, rootConfig pubcluster.Config, foConfigs []pubcluster.Config, addrFn addrFun) *FailoverService {
func NewFailoverService(logger logger.LogAdaptor, maxTries int, rootConfig pubcluster.Config, foConfigs []pubcluster.Config, addrFn addrFun) *FailoverService {
candidates := []CandidateCluster{}
configs := []pubcluster.Config{}
if len(foConfigs) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions internal/cluster/heartbeat_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ type HeartbeatService struct {
invFactory *ConnectionInvocationFactory
invService *invocation.Service
doneCh chan struct{}
logger logger.Logger
logger logger.LogAdaptor
state int32
}

func NewHeartbeatService(cm *ConnectionManager, f *ConnectionInvocationFactory, invService *invocation.Service, logger logger.Logger) *HeartbeatService {
func NewHeartbeatService(cm *ConnectionManager, f *ConnectionInvocationFactory, invService *invocation.Service, logger logger.LogAdaptor) *HeartbeatService {
return &HeartbeatService{
cm: cm,
invFactory: f,
Expand Down
10 changes: 5 additions & 5 deletions internal/cluster/partition_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/hazelcast/hazelcast-go-client/hzerrors"
"github.com/hazelcast/hazelcast-go-client/internal/event"
ilogger "github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/murmur"
"github.com/hazelcast/hazelcast-go-client/internal/proto"
iserialization "github.com/hazelcast/hazelcast-go-client/internal/serialization"
Expand All @@ -33,20 +33,20 @@ import (

type PartitionServiceCreationBundle struct {
EventDispatcher *event.DispatchService
Logger ilogger.Logger
Logger logger.LogAdaptor
}

func (b PartitionServiceCreationBundle) Check() {
if b.EventDispatcher == nil {
panic("EventDispatcher is nil")
}
if b.Logger == nil {
panic("Logger is nil")
if b.Logger.Logger == nil {
panic("LogAdaptor is nil")
}
}

type PartitionService struct {
logger ilogger.Logger
logger logger.LogAdaptor
eventDispatcher *event.DispatchService
partitionTable partitionTable
partitionCount int32
Expand Down
Loading

0 comments on commit 40b05ce

Please sign in to comment.