# Periodic write-window overlap

This is a synthetic scheduling illustration, not a drive benchmark or a replication
of the VLDB simulator. Start with the [sourced definition](https://hesela.dev/terms/alternating-mirroring/).

## What is counted

A writes during [0, a) in each period T. B writes during [d, d+b), wrapping at T.
All times use the same unit. Durations lie in [0,T], and 0 <= d < T.
The output counts the intersection of those two windows over one cycle.

| T (s) | A (s) | B (s) | Offset d (s) | Both writing (s) |
| --- | --- | --- | --- | --- |
| 10 | 4 | 4 | 5 | 0 |
| 10 | 5 | 5 | 5 | 0 |
| 10 | 6 | 6 | 5 | 2 |
| 10 | 6 | 4 | 5 | 1 |
| 10 | 6 | 4 | 6 | 0 |

The fourth row has a+b=T but still overlaps: [5,6). It also leaves [9,10) idle
on both disks. A total time budget does not determine the placement of windows.
The fifth row removes overlap by changing that placement.

Half-open endpoints make exact handoffs non-overlapping in this idealization.
Real scheduling needs margin for variable completion times and handoff overhead.
An available disk can still have a read queue or an outdated copy. The complement
of the overlap fraction is therefore NOT a throughput, latency, freshness or
durability guarantee. No arrival process, buffer protocol, failures or recovery
procedure is simulated.

## What changing the phase can achieve

For this idealized interval model, the overlap length L satisfies

```text
max(0, a + b - T) <= L <= min(a, b).
```

This is a mathematical bound, not a measured storage result. Inclusion-exclusion
gives L=a+b-union_length, and the union fits inside T. No intersection can be
larger than its shorter window. Both bounds are attainable: d=0 attains the
upper bound; d=a attains the lower bound when a<T. If a=T, every phase gives L=b.

Consequently, some phase avoids overlap exactly when a+b<=T. That does not make
the paper's fixed half-cycle offset optimal for unequal batch durations. The
6+4 second example attains zero at d=6, but not at d=5. With two 6-second batches
and T=10, at least 2 seconds of overlap are unavoidable at any phase. Moving a
window cannot remove a time-budget deficit.

The tests exhaust all integer phases for periods 1 through 12 and check that
the observed minima and maxima attain these bounds, including empty and full
windows. Real completion jitter and handoff costs are outside this proof.

## Reproduce

Requires Node.js 22 or newer; no external dependencies.

```sh
node model.mjs
node --test model.test.mjs
```

Download [model.mjs](./model.mjs), [model.test.mjs](./model.test.mjs) and
[examples.json](./examples.json). The tests compare the interval calculation with
independent midpoint counting for every integer duration and offset at periods
1 through 12, plus invalid inputs and unit scaling. These checks validate only
this small mathematical model.

The published JSON is generated by model.mjs; the publication test requires
exact equality with freshly computed output. No random seed is needed.

## Numerical boundary

The calculation intersects interval lengths before dividing by T. Forming an
endpoint as d+b first can round a short nonzero window away: for T=1, a=1,
b=1e-20 and d=0.5, the overlap is 1e-20, although JavaScript rounds 0.5+1e-20
to 0.5. The regression suite covers this case, wrapping, full-period writes,
subnormal durations and values near the largest finite number.

Results use binary64 arithmetic, not arbitrary precision. A positive duration
can still have a fraction that underflows to zero, and its complementary fraction
can round to one. Use bothWriting to inspect the duration; do not interpret
a rounded fraction alone as proof of zero overlap. Input precision and ordinary
roundoff remain limitations. These numerical cases are not plausible HDD timings.
