File size: 4,675 Bytes
e271174
e2dcb4d
 
 
 
e271174
e2dcb4d
e271174
 
e2dcb4d
e271174
 
e2dcb4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
---
title: Virtual Machine Placement (Python)
emoji: πŸ‘€
colorFrom: gray
colorTo: green
sdk: docker
app_port: 8080
pinned: false
license: apache-2.0
short_description: SolverForge Quickstart for the Vehicle Routing problem
---


# VM Placement Quickstart

A SolverForge quickstart demonstrating **constraint-based virtual machine placement optimization**.

## The Problem

You manage a datacenter with physical servers organized in racks, and must place virtual machines (VMs) onto those servers. Each server has limited CPU cores, memory, and storage capacity.

**The challenge**: Place all VMs while:
- Never exceeding any server's CPU, memory, or storage capacity
- Keeping database replicas on separate servers (anti-affinity)
- Placing related services together when possible (affinity)
- Minimizing the number of active servers (consolidation)
- Balancing load across active servers

## Why Constraint Solving?

With constraints, you describe *what* a valid placement looks like, not *how* to compute one. Adding a new business rule (e.g., "GPU workloads need GPU servers") is a single constraint functionβ€”not a rewrite of your algorithm.

## Quick Start

```bash
# 1. Create and activate virtual environment
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# 2. Install dependencies
pip install -e .

# 3. Run the application
run-app

# 4. Open http://localhost:8080 in your browser
```

**Requirement:** JDK 17+ must be installed (solverforge-legacy uses JPype to bridge Python and Java).

## Running Tests

```bash
# Run all tests
pytest

# Run with verbose output
pytest -v

# Run specific test file
pytest tests/test_constraints.py
```

## Project Structure

```
vm-placement-fast/
β”œβ”€β”€ src/vm_placement/
β”‚   β”œβ”€β”€ domain.py          # Server, VM, and VMPlacementPlan
β”‚   β”œβ”€β”€ constraints.py     # Hard and soft placement constraints
β”‚   β”œβ”€β”€ solver.py          # SolverForge configuration
β”‚   β”œβ”€β”€ demo_data.py       # Sample infrastructure and VMs
β”‚   β”œβ”€β”€ rest_api.py        # FastAPI endpoints
β”‚   └── converters.py      # Domain ↔ REST model conversion
β”œβ”€β”€ tests/
β”‚   └── test_constraints.py  # Unit tests for each constraint
β”œβ”€β”€ static/
β”‚   β”œβ”€β”€ index.html         # Web UI with rack visualization
β”‚   β”œβ”€β”€ app.js             # Frontend logic
β”‚   └── config.js          # Advanced settings sliders
└── pyproject.toml         # Dependencies
```

## Constraints

### Hard Constraints (must be satisfied)

1. **CPU Capacity**: Server CPU cannot be exceeded by assigned VMs
2. **Memory Capacity**: Server memory cannot be exceeded by assigned VMs
3. **Storage Capacity**: Server storage cannot be exceeded by assigned VMs
4. **Anti-Affinity**: VMs in the same anti-affinity group (e.g., database replicas) must be on different servers

### Soft Constraints (optimize for)

5. **Affinity**: VMs in the same affinity group (e.g., web tier) should be on the same server
6. **Minimize Servers Used**: Consolidate VMs onto fewer servers to reduce costs
7. **Balance Utilization**: Distribute load evenly across active servers
8. **Prioritize Placement**: Higher-priority VMs should be placed first

## API Endpoints

| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | `/demo-data` | List available datasets |
| GET | `/demo-data/{id}` | Load demo data |
| POST | `/demo-data/generate` | Generate custom infrastructure |
| POST | `/placements` | Submit for optimization |
| GET | `/placements/{id}` | Get current solution |
| GET | `/placements/{id}/status` | Get solving status |
| DELETE | `/placements/{id}` | Stop solving |
| PUT | `/placements/analyze` | Analyze placement score |

API documentation available at http://localhost:8080/q/swagger-ui

## Advanced Settings

The web UI includes configurable sliders for:

- **Racks**: Number of server racks (1-8)
- **Servers per Rack**: Servers in each rack (2-10)
- **VMs**: Number of VMs to place (5-200)
- **Solver Time**: How long to optimize (5s-2min)

Click "Generate New Data" to create custom scenarios.

## VM Placement Concepts

| Term | Definition |
|------|------------|
| **Server** | Physical machine with CPU, memory, and storage capacity |
| **VM** | Virtual machine requiring resources from a server |
| **Rack** | Physical grouping of servers in a datacenter |
| **Affinity** | VMs that should run on the same server |
| **Anti-Affinity** | VMs that must run on different servers |
| **Consolidation** | Using fewer servers to reduce power/cooling costs |

## Learn More

- [SolverForge Documentation](https://solverforge.org/docs)