func startHistoryServer(internalHistorySChan chan history.Scribe, server *engine.Server, exitChan chan bool) { scribeServer, err := history.NewFileScribe(cfg.HistoryDir, cfg.HistorySaveInterval) if err != nil { utils.Logger.Crit(fmt.Sprintf("<HistoryServer> Could not start, error: %s", err.Error())) exitChan <- true } server.RpcRegisterName("ScribeV1", scribeServer) internalHistorySChan <- scribeServer }
func startUsersServer(internalUserSChan chan engine.UserService, accountDb engine.AccountingStorage, server *engine.Server, exitChan chan bool) { userServer, err := engine.NewUserMap(accountDb, cfg.UserServerIndexes) if err != nil { utils.Logger.Crit(fmt.Sprintf("<UsersService> Could not start, error: %s", err.Error())) exitChan <- true return } server.RpcRegisterName("UsersV1", userServer) internalUserSChan <- userServer }
// ToDo: Make sure we are caching before starting this one func startAliasesServer(internalAliaseSChan chan engine.AliasService, accountDb engine.AccountingStorage, server *engine.Server, exitChan chan bool) { aliasesServer := engine.NewAliasHandler(accountDb) server.RpcRegisterName("AliasesV1", aliasesServer) if err := accountDb.CacheAccountingPrefixes(utils.ALIASES_PREFIX); err != nil { utils.Logger.Crit(fmt.Sprintf("<Aliases> Could not start, error: %s", err.Error())) exitChan <- true return } internalAliaseSChan <- aliasesServer }
func startRpc(server *engine.Server, internalRaterChan chan *engine.Responder, internalCdrSChan chan *engine.CdrServer, internalCdrStatSChan chan engine.StatsInterface, internalHistorySChan chan history.Scribe, internalPubSubSChan chan engine.PublisherSubscriber, internalUserSChan chan engine.UserService, internalAliaseSChan chan engine.AliasService) { select { // Any of the rpc methods will unlock listening to rpc requests case resp := <-internalRaterChan: internalRaterChan <- resp case cdrs := <-internalCdrSChan: internalCdrSChan <- cdrs case cdrstats := <-internalCdrStatSChan: internalCdrStatSChan <- cdrstats case hist := <-internalHistorySChan: internalHistorySChan <- hist case pubsubs := <-internalPubSubSChan: internalPubSubSChan <- pubsubs case users := <-internalUserSChan: internalUserSChan <- users case aliases := <-internalAliaseSChan: internalAliaseSChan <- aliases } go server.ServeJSON(cfg.RPCJSONListen) go server.ServeGOB(cfg.RPCGOBListen) go server.ServeHTTP(cfg.HTTPListen) }
func main() { flag.Parse() if *version { fmt.Println("CGRateS " + utils.VERSION) return } if *pidFile != "" { writePid() } if *singlecpu { runtime.GOMAXPROCS(1) // Having multiple cpus slows down computing due to CPU management, to be reviewed in future Go releases } if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } cfg, err = config.NewCGRConfigFromFolder(*cfgDir) if err != nil { utils.Logger.Crit(fmt.Sprintf("Could not parse config: %s exiting!", err)) return } config.SetCgrConfig(cfg) // Share the config object if *raterEnabled { cfg.RaterEnabled = *raterEnabled } if *schedEnabled { cfg.SchedulerEnabled = *schedEnabled } if *cdrsEnabled { cfg.CDRSEnabled = *cdrsEnabled } var ratingDb engine.RatingStorage var accountDb engine.AccountingStorage var logDb engine.LogStorage var loadDb engine.LoadStorage var cdrDb engine.CdrStorage if cfg.RaterEnabled || cfg.SchedulerEnabled { // Only connect to dataDb if necessary ratingDb, err = engine.ConfigureRatingStorage(cfg.TpDbType, cfg.TpDbHost, cfg.TpDbPort, cfg.TpDbName, cfg.TpDbUser, cfg.TpDbPass, cfg.DBDataEncoding) if err != nil { // Cannot configure getter database, show stopper utils.Logger.Crit(fmt.Sprintf("Could not configure dataDb: %s exiting!", err)) return } defer ratingDb.Close() engine.SetRatingStorage(ratingDb) accountDb, err = engine.ConfigureAccountingStorage(cfg.DataDbType, cfg.DataDbHost, cfg.DataDbPort, cfg.DataDbName, cfg.DataDbUser, cfg.DataDbPass, cfg.DBDataEncoding) if err != nil { // Cannot configure getter database, show stopper utils.Logger.Crit(fmt.Sprintf("Could not configure dataDb: %s exiting!", err)) return } defer accountDb.Close() engine.SetAccountingStorage(accountDb) } if cfg.RaterEnabled || cfg.CDRSEnabled || cfg.SchedulerEnabled { // Only connect to storDb if necessary logDb, err = engine.ConfigureLogStorage(cfg.StorDBType, cfg.StorDBHost, cfg.StorDBPort, cfg.StorDBName, cfg.StorDBUser, cfg.StorDBPass, cfg.DBDataEncoding, cfg.StorDBMaxOpenConns, cfg.StorDBMaxIdleConns) if err != nil { // Cannot configure logger database, show stopper utils.Logger.Crit(fmt.Sprintf("Could not configure logger database: %s exiting!", err)) return } defer logDb.Close() engine.SetStorageLogger(logDb) // loadDb,cdrDb and logDb are all mapped on the same stordb storage loadDb = logDb.(engine.LoadStorage) cdrDb = logDb.(engine.CdrStorage) engine.SetCdrStorage(cdrDb) } engine.SetRoundingDecimals(cfg.RoundingDecimals) stopHandled := false // Rpc/http server server := new(engine.Server) // Async starts here, will follow cgrates.json start order exitChan := make(chan bool) // Define internal connections via channels internalBalancerChan := make(chan *balancer2go.Balancer, 1) internalRaterChan := make(chan *engine.Responder, 1) internalSchedulerChan := make(chan *scheduler.Scheduler, 1) internalCdrSChan := make(chan *engine.CdrServer, 1) internalCdrStatSChan := make(chan engine.StatsInterface, 1) internalHistorySChan := make(chan history.Scribe, 1) internalPubSubSChan := make(chan engine.PublisherSubscriber, 1) internalUserSChan := make(chan engine.UserService, 1) internalAliaseSChan := make(chan engine.AliasService, 1) // Start balancer service if cfg.BalancerEnabled { go startBalancer(internalBalancerChan, &stopHandled, exitChan) // Not really needed async here but to cope with uniformity } // Start rater service if cfg.RaterEnabled { go startRater(internalRaterChan, internalBalancerChan, internalSchedulerChan, internalCdrStatSChan, internalHistorySChan, internalPubSubSChan, internalUserSChan, internalAliaseSChan, server, ratingDb, accountDb, loadDb, cdrDb, logDb, &stopHandled, exitChan) } // Start Scheduler if cfg.SchedulerEnabled { go startScheduler(internalSchedulerChan, ratingDb, exitChan) } // Start CDR Server if cfg.CDRSEnabled { go startCDRS(internalCdrSChan, logDb, cdrDb, internalRaterChan, internalPubSubSChan, internalUserSChan, internalAliaseSChan, internalCdrStatSChan, server, exitChan) } // Start CDR Stats server if cfg.CDRStatsEnabled { go startCdrStats(internalCdrStatSChan, ratingDb, accountDb, server) } // Start CDRC components if necessary go startCdrcs(internalCdrSChan, internalRaterChan, exitChan) // Start SM-FreeSWITCH if cfg.SmFsConfig.Enabled { go startSmFreeSWITCH(internalRaterChan, cdrDb, exitChan) // close all sessions on shutdown go shutdownSessionmanagerSingnalHandler(exitChan) } // Start SM-Kamailio if cfg.SmKamConfig.Enabled { go startSmKamailio(internalRaterChan, cdrDb, exitChan) } // Start SM-OpenSIPS if cfg.SmOsipsConfig.Enabled { go startSmOpenSIPS(internalRaterChan, cdrDb, exitChan) } // Register session manager service // FixMe: make sure this is thread safe if cfg.SmFsConfig.Enabled || cfg.SmKamConfig.Enabled || cfg.SmOsipsConfig.Enabled { // Register SessionManagerV1 service smRpc = new(v1.SessionManagerV1) server.RpcRegister(smRpc) } // Start HistoryS service if cfg.HistoryServerEnabled { go startHistoryServer(internalHistorySChan, server, exitChan) } // Start PubSubS service if cfg.PubSubServerEnabled { go startPubSubServer(internalPubSubSChan, accountDb, server) } // Start Aliases service if cfg.AliasesServerEnabled { go startAliasesServer(internalAliaseSChan, accountDb, server, exitChan) } // Start users service if cfg.UserServerEnabled { go startUsersServer(internalUserSChan, accountDb, server, exitChan) } // Serve rpc connections go startRpc(server, internalRaterChan, internalCdrSChan, internalCdrStatSChan, internalHistorySChan, internalPubSubSChan, internalUserSChan, internalAliaseSChan) <-exitChan if *pidFile != "" { if err := os.Remove(*pidFile); err != nil { utils.Logger.Warning("Could not remove pid file: " + err.Error()) } } utils.Logger.Info("Stopped all components. CGRateS shutdown!") }
func startPubSubServer(internalPubSubSChan chan engine.PublisherSubscriber, accountDb engine.AccountingStorage, server *engine.Server) { pubSubServer := engine.NewPubSub(accountDb, cfg.HttpSkipTlsVerify) server.RpcRegisterName("PubSubV1", pubSubServer) internalPubSubSChan <- pubSubServer }
func startCdrStats(internalCdrStatSChan chan engine.StatsInterface, ratingDb engine.RatingStorage, accountDb engine.AccountingStorage, server *engine.Server) { cdrStats := engine.NewStats(ratingDb, accountDb, cfg.CDRStatsSaveInterval) server.RpcRegister(cdrStats) server.RpcRegister(&v1.CDRStatsV1{CdrStats: cdrStats}) // Public APIs internalCdrStatSChan <- cdrStats }
func startCDRS(internalCdrSChan chan *engine.CdrServer, logDb engine.LogStorage, cdrDb engine.CdrStorage, internalRaterChan chan *engine.Responder, internalPubSubSChan chan engine.PublisherSubscriber, internalUserSChan chan engine.UserService, internalAliaseSChan chan engine.AliasService, internalCdrStatSChan chan engine.StatsInterface, server *engine.Server, exitChan chan bool) { utils.Logger.Info("Starting CGRateS CDRS service.") var err error var client *rpcclient.RpcClient // Rater connection init var raterConn engine.Connector if cfg.CDRSRater == utils.INTERNAL { responder := <-internalRaterChan // Wait for rater to come up before start querying raterConn = responder internalRaterChan <- responder // Put back the connection since there might be other entities waiting for it } else if len(cfg.CDRSRater) != 0 { client, err = rpcclient.NewRpcClient("tcp", cfg.CDRSRater, cfg.ConnectAttempts, cfg.Reconnects, utils.GOB) if err != nil { utils.Logger.Crit(fmt.Sprintf("<CDRS> Could not connect to rater: %s", err.Error())) exitChan <- true return } raterConn = &engine.RPCClientConnector{Client: client} } // Pubsub connection init var pubSubConn engine.PublisherSubscriber if cfg.CDRSPubSub == utils.INTERNAL { pubSubs := <-internalPubSubSChan pubSubConn = pubSubs internalPubSubSChan <- pubSubs } else if len(cfg.CDRSPubSub) != 0 { if cfg.CDRSRater == cfg.CDRSPubSub { pubSubConn = &engine.ProxyPubSub{Client: client} } else { client, err = rpcclient.NewRpcClient("tcp", cfg.CDRSPubSub, cfg.ConnectAttempts, cfg.Reconnects, utils.GOB) if err != nil { utils.Logger.Crit(fmt.Sprintf("<CDRS> Could not connect to pubsub server: %s", err.Error())) exitChan <- true return } pubSubConn = &engine.ProxyPubSub{Client: client} } } // Users connection init var usersConn engine.UserService if cfg.CDRSUsers == utils.INTERNAL { userS := <-internalUserSChan usersConn = userS internalUserSChan <- userS } else if len(cfg.CDRSUsers) != 0 { if cfg.CDRSRater == cfg.CDRSUsers { usersConn = &engine.ProxyUserService{Client: client} } else { client, err = rpcclient.NewRpcClient("tcp", cfg.CDRSUsers, cfg.ConnectAttempts, cfg.Reconnects, utils.GOB) if err != nil { utils.Logger.Crit(fmt.Sprintf("<CDRS> Could not connect to users server: %s", err.Error())) exitChan <- true return } usersConn = &engine.ProxyUserService{Client: client} } } // Aliases connection init var aliasesConn engine.AliasService if cfg.CDRSAliases == utils.INTERNAL { aliaseS := <-internalAliaseSChan aliasesConn = aliaseS internalAliaseSChan <- aliaseS } else if len(cfg.CDRSAliases) != 0 { if cfg.CDRSRater == cfg.CDRSAliases { aliasesConn = &engine.ProxyAliasService{Client: client} } else { client, err = rpcclient.NewRpcClient("tcp", cfg.CDRSAliases, cfg.ConnectAttempts, cfg.Reconnects, utils.GOB) if err != nil { utils.Logger.Crit(fmt.Sprintf("<CDRS> Could not connect to aliases server: %s", err.Error())) exitChan <- true return } aliasesConn = &engine.ProxyAliasService{Client: client} } } // Stats connection init var statsConn engine.StatsInterface if cfg.CDRSStats == utils.INTERNAL { statS := <-internalCdrStatSChan statsConn = statS internalCdrStatSChan <- statS } else if len(cfg.CDRSStats) != 0 { if cfg.CDRSRater == cfg.CDRSStats { statsConn = &engine.ProxyStats{Client: client} } else { client, err = rpcclient.NewRpcClient("tcp", cfg.CDRSStats, cfg.ConnectAttempts, cfg.Reconnects, utils.GOB) if err != nil { utils.Logger.Crit(fmt.Sprintf("<CDRS> Could not connect to stats server: %s", err.Error())) exitChan <- true return } statsConn = &engine.ProxyStats{Client: client} } } cdrServer, _ := engine.NewCdrServer(cfg, cdrDb, raterConn, pubSubConn, usersConn, aliasesConn, statsConn) utils.Logger.Info("Registering CDRS HTTP Handlers.") cdrServer.RegisterHanlersToServer(server) utils.Logger.Info("Registering CDRS RPC service.") cdrSrv := v1.CdrsV1{CdrSrv: cdrServer} server.RpcRegister(&cdrSrv) server.RpcRegister(&v2.CdrsV2{CdrsV1: cdrSrv}) // Make the cdr server available for internal communication responder := <-internalRaterChan // Retrieve again the responder responder.CdrSrv = cdrServer // Attach connection to cdrServer in responder, so it can be used later internalRaterChan <- responder // Put back the connection for the rest of the system internalCdrSChan <- cdrServer // Signal that cdrS is operational }
// Starts rater and reports on chan func startRater(internalRaterChan chan *engine.Responder, internalBalancerChan chan *balancer2go.Balancer, internalSchedulerChan chan *scheduler.Scheduler, internalCdrStatSChan chan engine.StatsInterface, internalHistorySChan chan history.Scribe, internalPubSubSChan chan engine.PublisherSubscriber, internalUserSChan chan engine.UserService, internalAliaseSChan chan engine.AliasService, server *engine.Server, ratingDb engine.RatingStorage, accountDb engine.AccountingStorage, loadDb engine.LoadStorage, cdrDb engine.CdrStorage, logDb engine.LogStorage, stopHandled *bool, exitChan chan bool) { waitTasks := make([]chan struct{}, 0) //Cache load cacheTaskChan := make(chan struct{}) waitTasks = append(waitTasks, cacheTaskChan) go func() { defer close(cacheTaskChan) if err := ratingDb.CacheRatingAll(); err != nil { utils.Logger.Crit(fmt.Sprintf("Cache rating error: %s", err.Error())) exitChan <- true return } if err := accountDb.CacheAccountingPrefixes(); err != nil { // Used to cache load history utils.Logger.Crit(fmt.Sprintf("Cache accounting error: %s", err.Error())) exitChan <- true return } }() // Retrieve scheduler for it's API methods var sched *scheduler.Scheduler // Need the scheduler in APIer if cfg.SchedulerEnabled { schedTaskChan := make(chan struct{}) waitTasks = append(waitTasks, schedTaskChan) go func() { defer close(schedTaskChan) select { case sched = <-internalSchedulerChan: internalSchedulerChan <- sched case <-time.After(cfg.InternalTtl): utils.Logger.Crit("<Rater>: Internal scheduler connection timeout.") exitChan <- true return } }() } // Connection to balancer var bal *balancer2go.Balancer if cfg.RaterBalancer != "" { balTaskChan := make(chan struct{}) waitTasks = append(waitTasks, balTaskChan) go func() { defer close(balTaskChan) if cfg.RaterBalancer == utils.INTERNAL { select { case bal = <-internalBalancerChan: internalBalancerChan <- bal // Put it back if someone else is interested about case <-time.After(cfg.InternalTtl): utils.Logger.Crit("<Rater>: Internal balancer connection timeout.") exitChan <- true return } } else { go registerToBalancer(exitChan) go stopRaterSignalHandler(internalCdrStatSChan, exitChan) *stopHandled = true } }() } // Connection to CDRStats var cdrStats engine.StatsInterface if cfg.RaterCdrStats != "" { cdrstatTaskChan := make(chan struct{}) waitTasks = append(waitTasks, cdrstatTaskChan) go func() { defer close(cdrstatTaskChan) if cfg.RaterCdrStats == utils.INTERNAL { select { case cdrStats = <-internalCdrStatSChan: internalCdrStatSChan <- cdrStats case <-time.After(cfg.InternalTtl): utils.Logger.Crit("<Rater>: Internal cdrstats connection timeout.") exitChan <- true return } } else if cdrStats, err = engine.NewProxyStats(cfg.RaterCdrStats, cfg.ConnectAttempts, -1); err != nil { utils.Logger.Crit(fmt.Sprintf("<Rater> Could not connect to cdrstats, error: %s", err.Error())) exitChan <- true return } }() } // Connection to HistoryS if cfg.RaterHistoryServer != "" { histTaskChan := make(chan struct{}) waitTasks = append(waitTasks, histTaskChan) go func() { defer close(histTaskChan) var scribeServer history.Scribe if cfg.RaterHistoryServer == utils.INTERNAL { select { case scribeServer = <-internalHistorySChan: internalHistorySChan <- scribeServer case <-time.After(cfg.InternalTtl): utils.Logger.Crit("<Rater>: Internal historys connection timeout.") exitChan <- true return } } else if scribeServer, err = history.NewProxyScribe(cfg.RaterHistoryServer, cfg.ConnectAttempts, -1); err != nil { utils.Logger.Crit(fmt.Sprintf("<Rater> Could not connect historys, error: %s", err.Error())) exitChan <- true return } engine.SetHistoryScribe(scribeServer) // ToDo: replace package sharing with connection based one }() } // Connection to pubsubs if cfg.RaterPubSubServer != "" { pubsubTaskChan := make(chan struct{}) waitTasks = append(waitTasks, pubsubTaskChan) go func() { defer close(pubsubTaskChan) var pubSubServer engine.PublisherSubscriber if cfg.RaterPubSubServer == utils.INTERNAL { select { case pubSubServer = <-internalPubSubSChan: internalPubSubSChan <- pubSubServer case <-time.After(cfg.InternalTtl): utils.Logger.Crit("<Rater>: Internal pubsub connection timeout.") exitChan <- true return } } else if pubSubServer, err = engine.NewProxyPubSub(cfg.RaterPubSubServer, cfg.ConnectAttempts, -1); err != nil { utils.Logger.Crit(fmt.Sprintf("<Rater> Could not connect to pubsubs: %s", err.Error())) exitChan <- true return } engine.SetPubSub(pubSubServer) // ToDo: replace package sharing with connection based one }() } // Connection to AliasService if cfg.RaterAliasesServer != "" { aliasesTaskChan := make(chan struct{}) waitTasks = append(waitTasks, aliasesTaskChan) go func() { defer close(aliasesTaskChan) var aliasesServer engine.AliasService if cfg.RaterAliasesServer == utils.INTERNAL { select { case aliasesServer = <-internalAliaseSChan: internalAliaseSChan <- aliasesServer case <-time.After(cfg.InternalTtl): utils.Logger.Crit("<Rater>: Internal aliases connection timeout.") exitChan <- true return } } else if aliasesServer, err = engine.NewProxyAliasService(cfg.RaterAliasesServer, cfg.ConnectAttempts, -1); err != nil { utils.Logger.Crit(fmt.Sprintf("<Rater> Could not connect to aliases, error: %s", err.Error())) exitChan <- true return } engine.SetAliasService(aliasesServer) // ToDo: replace package sharing with connection based one }() } // Connection to UserService var userServer engine.UserService if cfg.RaterUserServer != "" { usersTaskChan := make(chan struct{}) waitTasks = append(waitTasks, usersTaskChan) go func() { defer close(usersTaskChan) if cfg.RaterUserServer == utils.INTERNAL { select { case userServer = <-internalUserSChan: internalUserSChan <- userServer case <-time.After(cfg.InternalTtl): utils.Logger.Crit("<Rater>: Internal users connection timeout.") exitChan <- true return } } else if userServer, err = engine.NewProxyUserService(cfg.RaterUserServer, cfg.ConnectAttempts, -1); err != nil { utils.Logger.Crit(fmt.Sprintf("<Rater> Could not connect users, error: %s", err.Error())) exitChan <- true return } engine.SetUserService(userServer) }() } // Wait for all connections to complete before going further for _, chn := range waitTasks { <-chn } responder := &engine.Responder{Bal: bal, ExitChan: exitChan, Stats: cdrStats} apierRpcV1 := &v1.ApierV1{StorDb: loadDb, RatingDb: ratingDb, AccountDb: accountDb, CdrDb: cdrDb, LogDb: logDb, Sched: sched, Config: cfg, Responder: responder, CdrStatsSrv: cdrStats, Users: userServer} apierRpcV2 := &v2.ApierV2{ ApierV1: *apierRpcV1} // internalSchedulerChan shared here server.RpcRegister(responder) server.RpcRegister(apierRpcV1) server.RpcRegister(apierRpcV2) internalRaterChan <- responder // Rater done }