Metadata-Version: 2.1
Name: pyvega
Version: 1.0.0
Summary: Spark-compatible RDD engine: a C++ actor-based runtime with a PySpark-style Python API
Keywords: distributed,rdd,spark,pyspark,data-processing,actors
Author: The Vega Authors
License: MIT
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: C++
Classifier: Topic :: System :: Distributed Computing
Project-URL: Homepage, https://github.com/sphulinga/vega
Project-URL: Documentation, https://github.com/sphulinga/vega/tree/main/docs
Project-URL: Repository, https://github.com/sphulinga/vega
Project-URL: Issues, https://github.com/sphulinga/vega/issues
Project-URL: Changelog, https://github.com/sphulinga/vega/blob/main/CHANGELOG.md
Requires-Python: >=3.9
Requires-Dist: cloudpickle>=3.0
Provides-Extra: test
Requires-Dist: pytest>=7.0; extra == "test"
Description-Content-Type: text/markdown

# Vega

An in-memory distributed data processing engine written in C++17. Vega
provides a Spark-like RDD API with lazy evaluation, but replaces Spark's
centralized driver/scheduler with a **decentralized actor model** where
workers coordinate through message passing.

**PyVega** is the Python client: it connects to a C++ **session server** over TCP,
records transformations lazily, and executes them on an embedded actor runtime with
Python UDF support.

## Documentation

| Guide | Description |
|-------|-------------|
| [**Docs index**](docs/README.md) | Start here |
| [Getting started](docs/getting-started.md) | Build, install, first program |
| [Server, workers, and clients](docs/server-and-workers.md) | `vega_server`, worker threads, cluster peers |
| [Python RDD guide](docs/python-guide.md) | PyVega API reference with examples |
| [DataFrame-style operations](docs/dataframe-operations.md) | SQL-like patterns using RDDs |
| [PySpark compatibility](docs/pyspark-compat.md) | Run PySpark RDD code and upstream tests |
| [Vega extras](docs/vega-extras.md) | Operations beyond Spark's RDD API |
| [Releasing](docs/releasing.md) | Building wheels and publishing to PyPI |

## Quick start (Python)

```bash
pip install pyvega
```

Wheels for Linux (x86_64, aarch64) and macOS (arm64, x86_64), CPython 3.9-3.13,
include the compiled C++ server, so there is nothing else to build. From a source
checkout, `pip install .` compiles it (needs CMake >= 3.16 and a C++17 compiler).

```python
from pyvega import SparkContext

with SparkContext(4) as sc:
    print(sc.range(0, 10).map(lambda x: x * 2).collect())
    # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
```

**Manual server:** `vega-server --bind 127.0.0.1:9500` then
`SparkContext(remote="127.0.0.1:9500")`. See [server and clients](docs/server-and-workers.md).

## Performance

Measured with [`benchmarks/bench_pyvega_ops.py`](benchmarks/bench_pyvega_ops.py)
(best of 3, Linux, 4 cores, 4 workers):

| Operation (200k elements, 4 workers) | v0.5.0 (ms) | v0.6.0 (ms) | Speedup |
|---|---:|---:|---:|
| parallelize+collect (tuples) | 207.7 | 139.9 | 1.5x |
| map+filter python (tuples) | 184.6 | 45.3 | 4.1x |
| reduceByKey (tuples) | 107.8 | 16.5 | 6.5x |
| groupByKey (tuples) | 107.9 | 12.3 | 8.8x |
| distinct (strings) | 4092.0 | 62.5 | 65x |
| sortByKey (tuples) | 688.0 | 81.5 | 8.4x |
| mapPartitions | 207.8 | 8.7 | 24x |
| union x2 | 408.0 | 0.4 | 949x |
| repartition(16) | 232.0 | 3.9 | 60x |
| aggregateByKey | 172.0 | 29.4 | 5.8x |
| take(10) | 142.2 | 1.6 | 89x |
| first() | 148.5 | 1.5 | 98x |
| sum (ints) | 123.9 | 1.8 | 68x |
| aggregate | 72.5 | 32.7 | 2.2x |
| chained shuffle | failed | 33.2 | fixed |

Where the gains come from:

- **Everything runs on the server.** `distinct`, `union`, `partitionBy`,
  `repartition`, `coalesce`, `sortByKey`, `mapPartitions`, `zip`, `cartesian`,
  joins, sampling and file I/O used to pull data back into the client and
  re-upload it. They are now server-side operators.
- **No 40 ms stall per RPC.** Server sockets use `TCP_NODELAY` and send each
  frame with one `sendmsg` call. Before, Nagle's algorithm plus delayed ACKs
  added about 44 ms to every request.
- **Partial jobs.** `take`/`first`/`isEmpty` scan only as many partitions as
  needed, and `toLocalIterator` computes one partition at a time.
- **Multi-stage DAG with shuffle reuse.** Chained shuffles work, and a
  materialized shuffle is reused by later actions (Spark's skipped stages).
- **Map-side combine** for `reduceByKey`/`combineByKey`/`aggregateByKey`.
- **Cheaper UDF calls.** Python functions are invoked through the C API
  (vectorcall) without a wrapper frame, and data is pickled with the C pickler.
- **Native numeric actions.** `sum`/`mean`/`stats`/`max`/`min` on int/float RDDs
  run in C++ (integer sums are exact, using 128-bit accumulators).

> **Note:** PyVega provides **RDD** APIs, not Spark SQL DataFrames. See
> [DataFrame-style operations](docs/dataframe-operations.md) for RDD equivalents.

## Architecture

```
                      User Code (Python or C++)
                                |
                    SparkContext / Cluster
                                |
                          +-----------+
                          |  JobActor |      (builds DAG, distributes tasks)
                          +-----------+
                           /    |     \
                   +------+ +------+ +------+
                   |Worker| |Worker| |Worker|   (execute partitions in parallel)
                   +------+ +------+ +------+
                       \       |       /
                    +-------------------------+
                    |   Actor System          |   (thread pool, message routing)
                    +-------------------------+
                    |   Transport Layer       |   (Local / TCP + Gossip)
                    +-------------------------+
```

For PyVega, the actor system runs **inside `vega_server`**. The Python process is a thin
RPC client; worker threads are not separate OS processes.

**Key difference from Spark:** there is no central scheduler in cluster mode. Peers
discover each other via gossip and coordinate execution directly. In single-node /
session-server mode, the JobActor still orchestrates stages but all workers are local
threads.

### Actor framework

Vega includes a lightweight actor framework inspired by
[rotor](https://github.com/basiliscos/cpp-rotor):

- **Message-based communication** — typed messages dispatched to registered handlers
- **Thread pool execution** — actors process messages in parallel across worker threads
- **Sequential per-actor** — each actor processes one message at a time
- **Python GIL integration** — worker threads acquire the GIL when executing Python UDFs

## Build (C++)

```bash
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j$(nproc)
```

Targets include `vega_server`, `vega_worker`, examples, and C++ tests.

## Python API (PyVega)

```python
from pyvega import SparkContext

with SparkContext(4) as sc:
    rdd = sc.parallelize([1, 2, 3, 4, 5], num_partitions=2)

    result = (
        rdd.filter(lambda x: x > 2)
        .map(lambda x: x * 10)
        .collect()
    )
    # [30, 40, 50]

    # Word count
    lines = ["hello world hello", "hello vega"]
    counts = (
        sc.parallelize(lines)
        .flat_map(lambda line: line.split())
        .map(lambda word: (word, 1))
        .reduce_by_key(lambda a, b: a + b)
        .collect()
    )
```

Full API: [Python RDD guide](docs/python-guide.md).

## Running on a cluster (C++)

`vega_worker` runs **equal peers** (no special driver role). PyVega does not connect
to cluster peers yet; use this for C++ multi-node examples.

```bash
# Terminal 1
./build/vega_worker --bind 127.0.0.1:9001 --id peer-1

# Terminal 2
./build/vega_worker --bind 127.0.0.1:9002 --id peer-2 --seeds 127.0.0.1:9001

# Terminal 3 — submitter (same binary, not a "driver")
./build/examples/cluster_word_count --bind 127.0.0.1:9003 --id peer-3 \
    --seeds 127.0.0.1:9001 --expect-peers 3
```

Single-node demo: `./build/examples/cluster_word_count --local`

| Flag | Description |
|------|-------------|
| `--bind HOST:PORT` | Listen address |
| `--seeds H1:P1,H2:P2` | Gossip seed peers |
| `--id NAME` | Node identifier |
| `--expect-peers N` | Wait for N peers before running (examples) |

Details: [Server, workers, and clients](docs/server-and-workers.md).

## C++ API

```cpp
#include <vega/vega.hpp>

int main() {
    vega::SparkContext ctx(4);
    auto rdd = ctx.parallelize(std::vector<int>{1, 2, 3, 4, 5}, 2);
    auto result = rdd
        .filter([](const int& x) { return x > 2; })
        .map([](const int& x) { return x * 10; })
        .collect();
}
```

### Available operations

The C++ API covers `map`, `filter`, `flatMap`, `reduceByKey`, `groupByKey`
and the `collect` / `count` / `reduce` / `foreach` actions.

PyVega implements the PySpark RDD API: element-wise and partition
transformations, all key/value operations (`reduceByKey`, `combineByKey`,
`aggregateByKey`, `groupByKey`, `join` and the outer joins, `cogroup`,
`subtractByKey`), set operations, `sortBy`/`sortByKey`, sampling, numeric
actions (`stats`, `mean`, `stdev`, `histogram`, ...), approximate actions,
`cache`/`persist`, accumulators, broadcast variables and file I/O. See the
[Python RDD guide](docs/python-guide.md), and [Vega extras](docs/vega-extras.md)
for operations Spark does not have (`explain`, `map_batches`, `quantiles`,
`heavy_hitters`, `sliding`, ...).

## Project structure

```
vega/
  docs/                      User guides (start at docs/README.md)
  include/vega/              C++ header-only library
    core/                    RDD, SparkContext, shuffle
    actors/                  JobActor, WorkerActor, messages
    execution/               DAG builder, stages
    cluster/                 P2P cluster, gossip
    network/                 TCP framing, routing
  src/
    vega_server.cpp          TCP session server (PyVega backend)
    server_session.cpp       Session RPC + embedded Python UDFs
    vega_worker.cpp          Cluster peer daemon
  src/server_module.cpp      Server as a Python extension (shipped in wheels)
  python/pyvega/             PyVega client package
  scripts/
    pyspark_compat/          PySpark RDD API shim
    run_pyspark_tests.py     Upstream Spark RDD tests vs PyVega
  third_party/spark/         Vendored Spark test sources
  examples/                  word_count, cluster_word_count, benchmarks
  tests/                     C++ unit tests + Spark RDD suite ports
```

## Spark compatibility testing

```bash
# C++ Spark RDD logic ports
cmake --build build --target test_spark_rdd_suite
cd build && ctest -R SparkRDDSuite --output-on-failure

# Upstream PySpark RDD tests (53/57 pass; 4 require a JVM)
VEGA_SERVER=build/vega_server \
  python scripts/run_pyspark_tests.py --filter pyspark.tests.test_rdd
```

See [PySpark compatibility](docs/pyspark-compat.md).

## Execution flow

### PyVega (session server)

1. Client builds an RDD chain lazily (`map`, `filter`, …)
2. First action sends fused stages over TCP
3. Server materializes lineage and submits a job to JobActor
4. WorkerActors compute partitions (native C++ or embedded Python)
5. Shuffles run as multi-stage DAGs (any number of chained shuffles), and a
   shuffle's materialized output is reused by later actions
6. Results return to the Python client

### Multi-node C++ cluster

1. Peers start and discover each other via gossip
2. Submitter distributes partition data and task assignments over TCP
3. Peers execute tasks and return serialized results

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md) for build/test instructions and PR
guidelines, and our [Code of Conduct](CODE_OF_CONDUCT.md). Report security
issues privately per [SECURITY.md](SECURITY.md).

## License

Licensed under the [MIT License](LICENSE).

Vega is **not affiliated with the Apache Software Foundation**. "Apache Spark"
and "PySpark" are trademarks of the ASF; Vega offers a Spark-like API for
familiarity and compatibility testing only. See [NOTICE](NOTICE) for
third-party attributions (Apache Spark test sources, pybind11, GoogleTest,
cloudpickle).
