# asi-uniovi/fcma architecture

> Architecture diagram of the GitHub repository asi-uniovi/fcma, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/asi-uniovi/fcma
- Repository: https://github.com/asi-uniovi/fcma

## Overview

##### Project Type and Purpose
- FCMA is essentially an algorithmic framework that solves allocation problems for Container as a Service (CaaS) running on Infrastructure as a Service (IaaS) clouds.
- The core goal is to optimize the cloud customer’s cost by computing efficient allocations of application containers to virtual machines (VMs), using a model where instance classes are arranged in families.
- The project uses a two-phase (pre-allocation and allocation) process to derive solutions and integrates an ILP solver (via PuLP) to manage optimization trade-offs under different speed and cost-performance levels.

##### Breakdown of the File Structure and Its Implications for Architecture
- Top-Level Organization:
- The main library is in the "fcma" directory which contains several essential modules:
- fcma.py – Likely the entry point for the main FCMA logic.
- helper.py – Contains supporting functions.
- model.py – Defines core data structures (VMs, container groups, allocation models, etc.).
- serialization.py – Manages input/output or persistence of problem definitions or solutions.
- visualization.py – Contains the SolutionPrinter and possibly other tools to render the output.
- "examples" folder shows practical usage cases (e.g., aws_eu_west_1.py provides instance class families and example problems).
- "perf" holds performance test cases and sample JSON inputs for benchmarking scenarios.
- "tests" directory encompasses unit tests, integration tests, and expected outputs to ensure correctness.
- Setup-related files (e.g., setup.py, requirements.txt) show standard package configuration and dependency management.

##### Core Architectural Components and Their Relationships
a. Input and Configuration:
- Input Definitions: Applications, workloads, and instance class families are defined in user code and examples (e.g., examples/example1.py). They form the core “problem definition” where each application has specific computational and performance requirements.
- System Configuration: A “System” object is built by mapping tuples of (application, instance class family) to their performance and resource requirements. This drives the core optimization model.

b. Solver and Allocation Engine:
- FCMA Solver: The “Fcma” class (in fcma/fcma.py) takes the system configuration and workloads and orchestrates the solution process.
- Pre-allocation Phase: Uses an ILP (e.g., via PuLP solvers) to compute a preliminary allocation. This phase (detailed in README) may ignore memory constraints initially.
- Allocation Phase: Post-processes the pre-allocation output. If needed, it promotes VMs (scaling up instance classes) or adds new VMs to meet resource bounds such as memory and performance.
- The solver is parameterized by a “speed_level” (1, 2, or 3) which affects the trade-off between cost accuracy and run time.

c. Output and Visualization:
- The solution produced includes detailed VM allocations, container grouping, resource utilization, and statistical metrics (costs before and after allocation, solution quality, etc.).
- The “visualization” module (SolutionPrinter in visualization.py) is used to render and output the solution. Its design is modular, enabling both terminal output and possibly graphical visualizations.

d. Supporting Components:
- Testing Suite: The tests folder contains unit and integration tests that validate individual modules (serialization, allocation logic, etc.) and system-wide behaviors.
- Performance Benchmarks: The “perf” directory contains sample problem data JSON files, which can be used to benchmark run times and allocation results.

##### Incorporating Architectural Patterns and Design Principles
- Modularity: The separation into distinct modules (core logic, visualization, serialization) makes the system easier to maintain and extend.
- Dependency Injection: The solver’s integration with an external ILP engine suggests a design that promotes pluggable solver components.
- Separation of Concerns: Differentiating between problem formulation, solution (model objects), and post-solution validation/visualization.

##### Elements
- Clear labels for each component, such as:
- Configuration/Input Definitions (Applications, Workloads, Instance Class Families)
- Problem Constructor (System object)
- Optimization Engine (with two sub-components: Pre-allocation Phase and Allocation Phase)
- ILP Solver Integration (PuLP, gapRel parameter, and speed_level variation)
- Output/Result Compiler (VM allocations, container groups, statistics)
- Visualization/Reporting Component (SolutionPrinter)
- Testing and Benchmarking Modules (Tests, Perf inputs)
- From input definitions to the problem constructor.
- From the constructed problem to the optimization engine.
- From optimization engine phases to the Compilation of results.
- From compiled results to the visualization module.
- Annotations to indicate architectural decisions such as the two-phase allocation process, the separation between application domain logic and optimization, and the use of encapsulated modules for extensibility.

##### Putting It All Together
- The Input Layer: shows definitions of applications, workloads, and instance class families (with a reference to examples, like aws_eu_west_1.py).
- The System Setup Layer: where these inputs are mapped into the System object using classes from fcma/model.py.
- The Optimization Engine Layer: representing both phases of the solver. Illustrate the entry point in Fcma class (fcma.py) and branch it into pre-allocation (partial ILP and node aggregation) and then the allocation phase (VM promotion/addition).
- The Output and Post-Processing Layer: depicting the solution structure (VM allocations, container groups, performance statistics) along with the visualization tool (SolutionPrinter).
- Peripheral Components: include test modules and performance files as verification and benchmarking pieces.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Input and Configuration Layer
    subgraph "Input and Configuration Layer"
        A["Input Definitions<br/>(Applications, Workloads,<br/>Instance Classes)"]:::input
        B["Examples (Problem Definitions)"]:::input
        C["Serialization (I/O Management)"]:::serialization
    end

    %% System Setup Layer
    subgraph "System Setup Layer"
        D["System Object<br/>(Data Models)"]:::model
        E["Helper Functions"]:::helper
    end

    %% Optimization Engine Layer
    subgraph "Optimization Engine Layer"
        F["FCMA Solver"]:::solver
        G["Pre-allocation Phase<br/>(ILP Solver Integration)"]:::phase
        H["Allocation Phase<br/>(VM Promotion/Additions)"]:::phase
    end

    %% Output and Post-Processing Layer
    subgraph "Output and Post-Processing Layer"
        I["Result Compilation<br/>(VM Allocations, Container Grouping)"]:::result
        J["Visualization<br/>(SolutionPrinter)"]:::visualization
    end

    %% Testing and Benchmarking
    subgraph "Testing and Benchmarking"
        K["Performance Benchmarks<br/>(Sample Problem Data)"]:::perf
        L["Testing Suite<br/>(Unit & Integration Tests)"]:::test
    end

    %% Relationships
    A --> D
    B --> D
    A --> C
    B --> C
    D --> F
    E --> F
    F --> G
    F --> H
    G --> I
    H --> I
    I --> J
    C --> I
    K --> I
    L --> I
    F ---|"ILP_Dependency"| G
    H ---|"Refines_Preallocation"| G

    %% Click Events
    click F "https://github.com/asi-uniovi/fcma/blob/main/fcma/fcma.py"
    click E "https://github.com/asi-uniovi/fcma/blob/main/fcma/helper.py"
    click D "https://github.com/asi-uniovi/fcma/blob/main/fcma/model.py"
    click C "https://github.com/asi-uniovi/fcma/blob/main/fcma/serialization.py"
    click J "https://github.com/asi-uniovi/fcma/blob/main/fcma/visualization.py"
    click B "https://github.com/asi-uniovi/fcma/tree/main/examples/"
    click K "https://github.com/asi-uniovi/fcma/tree/main/perf/problem_data/"
    click L "https://github.com/asi-uniovi/fcma/tree/main/tests/"

    %% Styles
    classDef input fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef serialization fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef model fill:#D5F5E3,stroke:#196F3D,stroke-width:2px;
    classDef helper fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
    classDef solver fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
    classDef phase fill:#D6EAF8,stroke:#2471A3,stroke-width:2px;
    classDef result fill:#E8DAEF,stroke:#6C3483,stroke-width:2px;
    classDef visualization fill:#FDEBD0,stroke:#A04000,stroke-width:2px;
    classDef perf fill:#FADBD8,stroke:#C0392B,stroke-width:2px;
    classDef test fill:#D7BDE2,stroke:#6C3483,stroke-width:2px;
```
