Skip to content

ACE Framework

The platform is structured around the ADAPTIVE Communication Environment (ACE) Reactor pattern. A single server binary is launched once per role (prepaid, billing, CDR loader, CRM, bill formatter, mediation, roaming); the role is determined by the TOML configuration file passed via -i. Each role activates a different mix of ACE_Event_Handler subclasses and ACE_Acceptor instances on the shared ACE_Reactor::instance(). The same binary therefore expresses every subsystem; only configuration changes the runtime topology.

Component Topology

graph TB
    subgraph Process["server process (one per role)"]
        Reactor[["ACE_Reactor::instance()"]]

        subgraph Handlers["Event handlers"]
            Acceptor["Acceptor&lt;PrepaidServer, SOCK_Acceptor&gt;<br/>(DIAMETER TCP)"]
            CDRLoader["CDRLoader<br/>: ACE_Event_Handler<br/>(timer)"]
            Billing["BillingHandler<br/>: ACE_Event_Handler<br/>(timer)"]
            BillFmt["BillFormatterHandler<br/>: ACE_Event_Handler<br/>(timer)"]
            CRMSrv["CRMServer<br/>: ACE_Svc_Handler<br/>(detached gRPC thread)"]
            MedDaemon["MediationDaemon<br/>: ACE_Event_Handler<br/>(timer)"]
            RoamDaemon["RoamingDaemon<br/>: ACE_Event_Handler<br/>(timer)"]
            Sig["SignalHandler<br/>: ACE_Event_Handler<br/>(SIGINT/TERM/HUP)"]
        end

        subgraph Singletons["ACE_Singleton instances"]
            UTILS[("UTILS<br/>(TOML + license)")]
            REFDATA[("REFDATA<br/>(price plans, services,<br/>statuses, billcycles,<br/>currencies, taxes)")]
            POOL[("THREAD_POOL<br/>(worker queue)")]
        end

        subgraph Workers["Per-handler worker threads"]
            PrepThr["PrepaidServer threads<br/>(THR_DETACHED, 1 per CCR session)"]
            BillThr["Billing worker threads<br/>(thread pool)"]
            FmtThr["Bill formatter threads<br/>(thread pool)"]
            LoaderThr["CDR loader thread<br/>(thread pool, atomic guard)"]
            MedThr["Mediation thread<br/>(thread pool, atomic guard)"]
            RoamThr["Roaming thread<br/>(thread pool, atomic guard)"]
        end
    end

    DB[("MySQL X DevAPI<br/>session per worker")]

    Acceptor --> PrepThr
    Reactor --> Acceptor
    Reactor --> CDRLoader
    Reactor --> Billing
    Reactor --> BillFmt
    Reactor --> CRMSrv
    Reactor --> MedDaemon
    Reactor --> RoamDaemon
    Reactor --> Sig

    CDRLoader --> LoaderThr
    Billing --> BillThr
    BillFmt --> FmtThr
    MedDaemon --> MedThr
    RoamDaemon --> RoamThr

    PrepThr --> DB
    BillThr --> DB
    FmtThr --> DB
    LoaderThr --> DB
    MedThr --> DB
    RoamThr --> DB
    CRMSrv --> DB
    REFDATA --> DB

    PrepThr -.-> REFDATA
    BillThr -.-> REFDATA
    LoaderThr -.-> REFDATA
    CRMSrv -.-> REFDATA

    Sig -. "shutdown callback" .-> CRMSrv

Design Patterns in Use

Reactor + Acceptor + Service Handler

The prepaid module uses the canonical ACE triad: ACE_Reactor multiplexes the listening socket; Acceptor<PrepaidServer, _SOCK_Acceptor> accepts incoming TCP connections; each accepted connection spawns a PrepaidServer (an ACE_Svc_Handler<_SOCK_Stream, ACE_MT_SYNCH>) which calls activate(THR_DETACHED, 1, 0) to run its DIAMETER read loop in a dedicated thread. The handler self-destructs when the peer closes the connection.

The CRM gRPC server reuses the ACE_Svc_Handler lifecycle for symmetry with the prepaid module, even though gRPC owns its own listening socket. CRMServer::open() builds the grpc::Server, then activate() runs grpc_server_->Wait() in a detached thread. This gives the gRPC server the same registration, signal handling, and shutdown story as the rest of the platform — SignalHandler::registerShutdownCallback() invokes CRMServer::shutdown() with a 5-second deadline before the reactor stops, draining in-flight RPCs.

Timer-driven event handlers

CDRLoader, BillingHandler, BillFormatterHandler, MediationDaemon, and RoamingDaemon are scheduled with ACE_Reactor::instance()->schedule_timer(handler, nullptr, initial_delay, interval). Their handle_timeout is invoked on every tick. MediationDaemon and RoamingDaemon follow the same off-reactor pattern as CDRLoader: each tick enqueues the file-processing cycle onto the thread pool under an atomic is_processing overlap guard, and refreshes REFDATA / ROAMING_TARIFF via refreshIfStale.

Heavy work is kept off the reactor thread. The in-memory reference-data reload (a clear+reload of ten tables, fanned out over eight async DB sessions) does not run on every tick: each handler calls Refdata::refreshIfStale(refdata_ttl_s), which only checks an atomic timestamp on the reactor thread and, when the data is older than [app] refdata_ttl_s seconds (default 30; 0 = every tick), enqueues the actual reload onto the singleton thread pool under a single-flight guard. Runtime-inserted reference rows therefore become visible within the TTL rather than on the next 5 s tick; the billing paths that require freshness-now (BillingHandler::billOne, billCycleSync) still call refresh() unconditionally just before they read the data. CDRLoader runs its file-ingestion cycle on the pool, guarded against overlap by std::atomic<bool> is_processing, and BillingHandler dispatches each discovered activity onto the pool.

Design note: BillingHandler::handle_timeout creates a DB_layer and runs the watchdog eviction + the get_new_activity poll on the reactor thread before dispatching the discovered work onto the pool. The poll touches the shared futures vector, so it stays on the reactor thread where that access needs no extra synchronisation.

Singleton

Three ACE_Singleton<T, ACE_Recursive_Thread_Mutex> typedefs provide process-wide shared state:

  • UTILS — TOML configuration (parsed once via tomlplusplus) and the active LicenseValidator.
  • REFDATA — In-memory caches of all reference tables (REF_Price_plan, REF_Service, REF_Resource, REF_Status, REF_Resource_status, REF_Event_status, REF_Billcycle, REF_voucher_status, REF_Currency, REF_Tax). Loaded at startup; reads guarded by std::shared_mutex. The currency and tax maps drive display-time conversion and VAT stamping respectively — see Convergent Rating and Invoicing.
  • THREAD_POOL — A C++20 thread pool with a std::queue<std::function<void()>> task queue and std::condition_variable notification, sized by system.thread_pool_size.

Adapter layer

diameter::DiameterAdapter keeps the DIAMETER protocol stack (usr/include/diameter/) free of any billing::* types. Only the prepaid PrepaidServer::svc() loop binds the two: the adapter pulls MSISDN, B-number, service id, requested units, and used units out of a DiameterCCR; BalanceReserve::fromCCR() and Charge::fromCCR() factory methods produce the internal protobuf objects. The DIAMETER stack is delivered as a separate shared library (usr/lib/libdiameter.so, .dylib on macOS) and is reused by the bundled mock client built from usr/src/diameter/diameter_client.cpp (bin/diameter_client).

Service handler thread isolation

Every worker thread that touches the database creates its own std::unique_ptr<DB_layer>. DB_layer holds a std::unique_ptr<mysqlx::Session>, so each thread owns its own X DevAPI session — there is no shared connection across threads. BillingHandler::handle_timeout instantiates one DB_layer to scan for activities; the worker spawned for each activity instantiates a second one inside the lambda body.

Concurrency Model

Threads

Thread Owner Lifetime
Main / reactor ACE_Reactor::instance()->run_reactor_event_loop() in main.cpp Process lifetime; exits when a shutdown signal is delivered.
Prepaid worker PrepaidServer::activate(THR_DETACHED) per accepted connection One per DIAMETER session; exits on recv_n returning <= 0.
Thread-pool workers THREAD_POOL::instance()->init(size) Process lifetime; size from system.thread_pool_size.
gRPC workers gRPC's internal pool, owned by grpc::Server Built in CRMServer::open(); drained by Shutdown(deadline).

Signal handling

SIGINT, SIGTERM, and SIGHUP are registered on the reactor via ACE_Sig_Set. CRMServer::setupGRPCServer() blocks these three signals with pthread_sigmask(SIG_BLOCK, ...) before BuildAndStart() so gRPC's worker threads inherit the mask and cannot swallow them, then restores the mask in the main thread so the reactor handler still receives them. SIGTSTP is intentionally excluded — it is a job-control signal whose default behaviour caused the entire signal handler to be de-registered.

Reference-data refresh

Refdata exposes refresh() to re-load every reference table inside a std::shared_mutex write lock. The timer handlers drive freshness automatically via refreshIfStale(refdata_ttl_s), which enqueues that reload onto the thread pool at most once per TTL; the freshness-now billing paths (billOne, billCycleSync) call refresh() directly. CRM admin actions may also trigger refresh() explicitly after editing reference data.

Because refresh() runs on a pool worker — concurrently with rating/charging on acceptor and loader threads — the getters (getPP, getSV, getBC, getCurrency, getTax, …) return the looked-up row by value wrapped in std::optional. The copy is taken while the reader's std::shared_lock is held, so a caller never holds a pointer into a map that refresh()'s clear()/reload could invalidate underneath it; std::nullopt is the "not found" sentinel. Dereferences that could otherwise be unguarded — notably getBC(billcycle)->day() in billOneAtomic (a missing billcycle row) and the postpaid Rater::rate plan/service lookups — are explicit has_value error paths that fail the single bill/CDR rather than the process.

Module Composition by Role

main.cpp reads the [modules] table from TOML and conditionally activates each subsystem. The same binary can host any combination, but the canonical deployment runs one process per role to isolate failures and to allow independent restarts.

Role TOML Modules activated
Prepaid OCS prepaid.toml prepaid
CDR loader cdr_loader.toml cdr_loader
Billing (per cycle) billing1.toml … billing4.toml billing (one process per billing.billcycle)
Bill formatter bill_formatter.toml bill_formatter
CRM crm.toml crm
Mediation mediation.toml mediation (Direction-1 roaming ingest)
Roaming roaming.toml roaming (Direction-1 charging + Direction-2 metering / TAP-out)
Mass prepaid rating prepaid_mass_rating.toml billing + prepaid_mass_rating (developer / load-test only)

var/lib/license/platform.lic (the [license] license_key path) is checked at startup by every role; failure of any of the five validation steps (signature, magic, hardware ID, expiry, LKGT) terminates the process before any module is initialised.

Generated Code and the gRPC Surface

usr/src/proto/types.proto is the single source of truth for both the wire schema and the in-memory data model. CMake's add_custom_command (in tmp/CMakeLists.txt) invokes protoc with the C++ and gRPC plugins to produce types.pb.cc / types.grpc.pb.cc in usr/src/generated/ and the matching headers in usr/include/generated/ (both gitignored build output). If grpc_php_plugin is on the path, an additional generate_php_proto target writes the PHP stubs into var/www/generated/ for the CRM front-end (sbin/generate-proto.sh does the same standalone). The proto file defines the billing package — every internal data structure (CDR, Charge, Balance_reserve, Subscriber, Bill, Voucher, etc.) and every CRM RPC.