Add connections through ConnManager

This commit is contained in:
TeoV
2019-12-09 11:41:57 -05:00
parent 18150825bb
commit 4bd4ae3ee9
129 changed files with 593 additions and 1173 deletions

View File

@@ -32,23 +32,21 @@ import (
// NewApierV1Service returns the ApierV1 Service
func NewApierV1Service(cfg *config.CGRConfig, dm *DataDBService,
storDB *StorDBService, filterSChan chan *engine.FilterS,
server *utils.Server, cacheSChan, schedChan, attrsChan,
dispatcherChan chan rpcclient.ClientConnector,
server *utils.Server,
schedService *SchedulerService,
responderService *ResponderService) *ApierV1Service {
responderService *ResponderService,
internalAPIerV1Chan chan rpcclient.RpcClientConnection,
connMgr *engine.ConnManager) *ApierV1Service {
return &ApierV1Service{
connChan: make(chan rpcclient.ClientConnector, 1),
connChan: internalAPIerV1Chan,
cfg: cfg,
dm: dm,
storDB: storDB,
filterSChan: filterSChan,
server: server,
cacheSChan: cacheSChan,
schedChan: schedChan,
attrsChan: attrsChan,
dispatcherChan: dispatcherChan,
schedService: schedService,
responderService: responderService,
connMgr: connMgr,
}
}
@@ -60,12 +58,9 @@ type ApierV1Service struct {
storDB *StorDBService
filterSChan chan *engine.FilterS
server *utils.Server
cacheSChan chan rpcclient.ClientConnector
schedChan chan rpcclient.ClientConnector
attrsChan chan rpcclient.ClientConnector
dispatcherChan chan rpcclient.ClientConnector
schedService *SchedulerService
responderService *ResponderService
connMgr *engine.ConnManager
api *v1.ApierV1
connChan chan rpcclient.ClientConnector
@@ -84,28 +79,6 @@ func (api *ApierV1Service) Start() (err error) {
api.Lock()
defer api.Unlock()
// create cache connection
var cacheSrpc, schedulerSrpc, attributeSrpc rpcclient.ClientConnector
if cacheSrpc, err = NewConnection(api.cfg, api.cacheSChan, api.dispatcherChan, api.cfg.ApierCfg().CachesConns); err != nil {
utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to %s, error: %s",
utils.ApierV1, utils.CacheS, err.Error()))
return
}
// create scheduler connection
if schedulerSrpc, err = NewConnection(api.cfg, api.schedChan, api.dispatcherChan, api.cfg.ApierCfg().SchedulerConns); err != nil {
utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to %s, error: %s",
utils.ApierV1, utils.SchedulerS, err.Error()))
return
}
// create scheduler connection
if attributeSrpc, err = NewConnection(api.cfg, api.attrsChan, api.dispatcherChan, api.cfg.ApierCfg().AttributeSConns); err != nil {
utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to %s, error: %s",
utils.ApierV1, utils.AttributeS, err.Error()))
return
}
api.api = &v1.ApierV1{
DataManager: api.dm.GetDM(),
CdrDb: api.storDB.GetDM(),
@@ -115,10 +88,9 @@ func (api *ApierV1Service) Start() (err error) {
SchedulerService: api.schedService,
HTTPPoster: engine.NewHTTPPoster(api.cfg.GeneralCfg().HttpSkipTlsVerify,
api.cfg.GeneralCfg().ReplyTimeout),
FilterS: filterS,
CacheS: cacheSrpc,
SchedulerS: schedulerSrpc,
AttributeS: attributeSrpc}
FilterS: filterS,
ConnMgr: api.connMgr,
}
if !api.cfg.DispatcherSCfg().Enabled {
api.server.RpcRegister(api.api)
@@ -141,33 +113,10 @@ func (api *ApierV1Service) GetIntenternalChan() (conn chan rpcclient.ClientConne
// Reload handles the change of config
func (api *ApierV1Service) Reload() (err error) {
var cacheSrpc, schedulerSrpc, attributeSrpc rpcclient.ClientConnector
if cacheSrpc, err = NewConnection(api.cfg, api.cacheSChan, api.dispatcherChan, api.cfg.ApierCfg().CachesConns); err != nil {
utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to %s, error: %s",
utils.ApierV1, utils.CacheS, err.Error()))
return
}
// create scheduler connection
if schedulerSrpc, err = NewConnection(api.cfg, api.schedChan, api.dispatcherChan, api.cfg.ApierCfg().SchedulerConns); err != nil {
utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to %s, error: %s",
utils.ApierV1, utils.SchedulerS, err.Error()))
return
}
// create scheduler connection
if attributeSrpc, err = NewConnection(api.cfg, api.attrsChan, api.dispatcherChan, api.cfg.ApierCfg().AttributeSConns); err != nil {
utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to %s, error: %s",
utils.ApierV1, utils.AttributeS, err.Error()))
return
}
api.Lock()
if api.storDB.WasReconnected() { // rewrite the connection if was changed
api.api.SetStorDB(api.storDB.GetDM())
}
api.api.SetAttributeSConnection(attributeSrpc)
api.api.SetCacheSConnection(cacheSrpc)
api.api.SetSchedulerSConnection(schedulerSrpc)
api.Unlock()
return
}

View File

@@ -30,10 +30,11 @@ import (
// NewApierV2Service returns the ApierV2 Service
func NewApierV2Service(apiv1 *ApierV1Service, cfg *config.CGRConfig,
server *utils.Server) *ApierV2Service {
server *utils.Server,
internalAPIerV1Chan chan rpcclient.RpcClientConnection) *ApierV2Service {
return &ApierV2Service{
apiv1: apiv1,
connChan: make(chan rpcclient.ClientConnector, 1),
connChan: internalAPIerV1Chan,
cfg: cfg,
server: server,
}

View File

@@ -70,10 +70,9 @@ func TestCdrsReload(t *testing.T) {
stordb := NewStorDBService(cfg)
chrS := NewChargerService(cfg, db, chS, filterSChan, server, nil, nil)
schS := NewSchedulerService(cfg, db, chS, filterSChan, server, make(chan rpcclient.RpcClientConnection, 1), nil)
tS := NewThresholdService(cfg, db, chS, filterSChan, server, make(chan rpcclient.RpcClientConnection, 1))
ralS := NewRalService(cfg, db, stordb, chS, filterSChan, server,
tS.GetIntenternalChan(), internalChan, cacheSChan, internalChan, internalChan,
internalChan, schS, engineShutdown)
make(chan rpcclient.RpcClientConnection, 1), make(chan rpcclient.RpcClientConnection, 1), make(chan rpcclient.RpcClientConnection, 1), make(chan rpcclient.RpcClientConnection, 1),
schS, engineShutdown, nil)
cdrS := NewCDRServer(cfg, db, stordb, filterSChan, server,
make(chan rpcclient.ClientConnector, 1),
chrS.GetIntenternalChan(), ralS.GetResponder().GetIntenternalChan(),

View File

@@ -33,19 +33,21 @@ import (
// NewRalService returns the Ral Service
func NewRalService(cfg *config.CGRConfig, dm *DataDBService,
storDB *StorDBService, cacheS *engine.CacheS, filterSChan chan *engine.FilterS, server *utils.Server,
thsChan, stsChan, cacheSChan, schedChan, attrsChan, dispatcherChan chan rpcclient.ClientConnector,
schedulerService *SchedulerService, exitChan chan bool) *RalService {
resp := NewResponderService(cfg, server, thsChan, stsChan, dispatcherChan, exitChan)
apiv1 := NewApierV1Service(cfg, dm, storDB, filterSChan, server, cacheSChan, schedChan, attrsChan, dispatcherChan, schedulerService, resp)
apiv2 := NewApierV2Service(apiv1, cfg, server)
internalRALsChan, internalResponderChan, internalAPIerV1Chan, internalAPIerV2Chan chan rpcclient.RpcClientConnection,
schedulerService *SchedulerService, exitChan chan bool,
connMgr *engine.ConnManager) *RalService {
resp := NewResponderService(cfg, server, internalResponderChan, exitChan)
apiv1 := NewApierV1Service(cfg, dm, storDB, filterSChan, server, schedulerService, resp, internalAPIerV1Chan, connMgr)
apiv2 := NewApierV2Service(apiv1, cfg, server, internalAPIerV2Chan)
return &RalService{
connChan: make(chan rpcclient.ClientConnector, 1),
connChan: internalRALsChan,
cfg: cfg,
cacheS: cacheS,
server: server,
apiv1: apiv1,
apiv2: apiv2,
responder: resp,
connMgr: connMgr,
}
}
@@ -60,6 +62,7 @@ type RalService struct {
apiv2 *ApierV2Service
responder *ResponderService
connChan chan rpcclient.ClientConnector
connMgr *engine.ConnManager
}
// Start should handle the sercive start
@@ -68,7 +71,7 @@ func (rals *RalService) Start() (err error) {
if rals.IsRunning() {
return fmt.Errorf("service aleady running")
}
engine.SetConnManager(rals.connMgr)
engine.SetRpSubjectPrefixMatching(rals.cfg.RalsCfg().RpSubjectPrefixMatching)
rals.Lock()
defer rals.Unlock()

View File

@@ -70,8 +70,8 @@ func TestRalsReload(t *testing.T) {
schS := NewSchedulerService(cfg, db, chS, filterSChan, server, make(chan rpcclient.RpcClientConnection, 1), nil)
tS := NewThresholdService(cfg, db, chS, filterSChan, server, make(chan rpcclient.RpcClientConnection, 1))
ralS := NewRalService(cfg, db, stordb, chS, filterSChan, server,
tS.GetIntenternalChan(), internalChan, cacheSChan, internalChan, internalChan,
internalChan, schS, engineShutdown)
make(chan rpcclient.RpcClientConnection, 1), make(chan rpcclient.RpcClientConnection, 1), make(chan rpcclient.RpcClientConnection, 1), make(chan rpcclient.RpcClientConnection, 1),
schS, engineShutdown, nil)
srvMngr.AddServices(NewConnManagerService(cfg, nil), ralS, schS, tS, NewLoaderService(cfg, db, filterSChan, server, cacheSChan, nil, engineShutdown), db, stordb)
if err = srvMngr.StartServices(); err != nil {
t.Error(err)

View File

@@ -30,28 +30,22 @@ import (
// NewResponderService returns the Resonder Service
func NewResponderService(cfg *config.CGRConfig, server *utils.Server,
thsChan, stsChan, dispatcherChan chan rpcclient.ClientConnector,
internalRALsChan chan rpcclient.RpcClientConnection,
exitChan chan bool) *ResponderService {
return &ResponderService{
connChan: make(chan rpcclient.ClientConnector, 1),
cfg: cfg,
server: server,
thsChan: thsChan,
stsChan: stsChan,
dispatcherChan: dispatcherChan,
exitChan: exitChan,
connChan: internalRALsChan,
cfg: cfg,
server: server,
exitChan: exitChan,
}
}
// ResponderService implements Service interface
type ResponderService struct {
sync.RWMutex
cfg *config.CGRConfig
server *utils.Server
thsChan chan rpcclient.ClientConnector
stsChan chan rpcclient.ClientConnector
dispatcherChan chan rpcclient.ClientConnector
exitChan chan bool
cfg *config.CGRConfig
server *utils.Server
exitChan chan bool
resp *engine.Responder
connChan chan rpcclient.ClientConnector
@@ -64,23 +58,6 @@ func (resp *ResponderService) Start() (err error) {
return fmt.Errorf("service aleady running")
}
var thdS, stats rpcclient.ClientConnector
if thdS, err = NewConnection(resp.cfg, resp.thsChan, resp.dispatcherChan, resp.cfg.RalsCfg().ThresholdSConns); err != nil {
utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to %s, error: %s",
utils.RALService, utils.ThresholdS, err.Error()))
return
}
if stats, err = NewConnection(resp.cfg, resp.stsChan, resp.dispatcherChan, resp.cfg.RalsCfg().StatSConns); err != nil {
utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to %s, error: %s",
utils.RALService, utils.StatS, err.Error()))
return
}
if thdS != nil {
engine.SetThresholdS(thdS) // temporary architectural fix until we will have separate AccountS
}
if stats != nil {
engine.SetStatS(stats)
}
resp.Lock()
defer resp.Unlock()
resp.resp = &engine.Responder{
@@ -105,25 +82,8 @@ func (resp *ResponderService) GetIntenternalChan() (conn chan rpcclient.ClientCo
// Reload handles the change of config
func (resp *ResponderService) Reload() (err error) {
var thdS, stats rpcclient.ClientConnector
if thdS, err = NewConnection(resp.cfg, resp.thsChan, resp.dispatcherChan, resp.cfg.RalsCfg().ThresholdSConns); err != nil {
utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to %s, error: %s",
utils.RALService, utils.ThresholdS, err.Error()))
return
}
if stats, err = NewConnection(resp.cfg, resp.stsChan, resp.dispatcherChan, resp.cfg.RalsCfg().StatSConns); err != nil {
utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to %s, error: %s",
utils.RALService, utils.StatS, err.Error()))
return
}
resp.Lock()
resp.resp.MaxComputedUsage = resp.cfg.RalsCfg().MaxComputedUsage // this may cause concurrency problems
if thdS != nil {
engine.SetThresholdS(thdS) // temporary architectural fix until we will have separate AccountS
}
if stats != nil {
engine.SetStatS(stats)
}
resp.Unlock()
return
}

View File

@@ -33,34 +33,32 @@ import (
// NewSchedulerService returns the Scheduler Service
func NewSchedulerService(cfg *config.CGRConfig, dm *DataDBService,
cacheS *engine.CacheS, fltrSChan chan *engine.FilterS,
server *utils.Server, internalCDRServerChan,
dispatcherChan chan rpcclient.ClientConnector) *SchedulerService {
server *utils.Server, internalSchedulerrSChan chan rpcclient.RpcClientConnection,
connMgr *engine.ConnManager) *SchedulerService {
return &SchedulerService{
connChan: make(chan rpcclient.ClientConnector, 1),
cfg: cfg,
dm: dm,
cacheS: cacheS,
fltrSChan: fltrSChan,
server: server,
cdrSChan: internalCDRServerChan,
dispatcherChan: dispatcherChan,
connChan: internalSchedulerrSChan,
cfg: cfg,
dm: dm,
cacheS: cacheS,
fltrSChan: fltrSChan,
server: server,
connMgr: connMgr,
}
}
// SchedulerService implements Service interface
type SchedulerService struct {
sync.RWMutex
cfg *config.CGRConfig
dm *DataDBService
cacheS *engine.CacheS
fltrSChan chan *engine.FilterS
server *utils.Server
cdrSChan chan rpcclient.ClientConnector
dispatcherChan chan rpcclient.ClientConnector
cfg *config.CGRConfig
dm *DataDBService
cacheS *engine.CacheS
fltrSChan chan *engine.FilterS
server *utils.Server
schS *scheduler.Scheduler
rpc *v1.SchedulerSv1
connChan chan rpcclient.ClientConnector
connMgr *engine.ConnManager
}
// Start should handle the sercive start
@@ -86,16 +84,6 @@ func (schS *SchedulerService) Start() (err error) {
}
schS.connChan <- schS.rpc
// Create connection to CDR Server and share it in engine(used for *cdrlog action)
cdrsConn, err := NewConnection(schS.cfg, schS.cdrSChan, schS.dispatcherChan, schS.cfg.SchedulerCfg().CDRsConns)
if err != nil {
utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to CDRServer: %s", utils.SchedulerS, err.Error()))
return
}
// ToDo: this should be send to scheduler
engine.SetSchedCdrsConns(cdrsConn)
return
}
@@ -106,13 +94,6 @@ func (schS *SchedulerService) GetIntenternalChan() (conn chan rpcclient.ClientCo
// Reload handles the change of config
func (schS *SchedulerService) Reload() (err error) {
cdrsConn, err := NewConnection(schS.cfg, schS.cdrSChan, schS.dispatcherChan, schS.cfg.SchedulerCfg().CDRsConns)
if err != nil {
utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to CDRServer: %s", utils.SchedulerS, err.Error()))
return
}
// ToDo: this should be send to scheduler
engine.SetSchedCdrsConns(cdrsConn)
schS.Lock()
schS.schS.Reload()
schS.Unlock()

View File

@@ -45,11 +45,10 @@ func TestSchedulerSReload(t *testing.T) {
close(chS.GetPrecacheChannel(utils.CacheActionPlans))
server := utils.NewServer()
srvMngr := servmanager.NewServiceManager(cfg, engineShutdown)
internalCdrSChan := make(chan rpcclient.ClientConnector, 1)
internalCdrSChan <- nil
db := NewDataDBService(cfg)
schS := NewSchedulerService(cfg, db, chS, filterSChan, server, internalCdrSChan, nil)
srvMngr.AddServices(NewConnManagerService(cfg, nil), schS, NewLoaderService(cfg, db, filterSChan, server, nil, nil, engineShutdown), db)
schS := NewSchedulerService(cfg, db, chS, filterSChan, server, make(chan rpcclient.RpcClientConnection, 1), nil)
srvMngr.AddServices(NewConnManagerService(cfg, nil), schS,
NewLoaderService(cfg, db, filterSChan, server, nil, nil, engineShutdown), db)
if err = srvMngr.StartServices(); err != nil {
t.Error(err)
}

View File

@@ -73,8 +73,8 @@ func TestSessionSReload(t *testing.T) {
chrS := NewChargerService(cfg, db, chS, filterSChan, server, make(chan rpcclient.RpcClientConnection, 1), nil)
schS := NewSchedulerService(cfg, db, chS, filterSChan, server, make(chan rpcclient.RpcClientConnection, 1), nil)
ralS := NewRalService(cfg, db, stordb, chS, filterSChan, server,
/*tS*/ internalChan, internalChan, cacheSChan, internalChan, internalChan,
internalChan, schS, engineShutdown)
make(chan rpcclient.RpcClientConnection, 1), make(chan rpcclient.RpcClientConnection, 1), make(chan rpcclient.RpcClientConnection, 1), make(chan rpcclient.RpcClientConnection, 1),
schS, engineShutdown, nil)
cdrS := NewCDRServer(cfg, db, stordb, filterSChan, server,
make(chan rpcclient.ClientConnector, 1),
chrS.GetIntenternalChan(), ralS.GetResponder().GetIntenternalChan(),