placement_machines 0.1.4-aarch64-linux → 0.4.3-aarch64-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +54 -0
- data/README.md +63 -38
- data/ext/placement_machines_native/core/CHANGELOG.md +40 -0
- data/ext/placement_machines_native/core/README.md +53 -14
- data/lib/placement_machines/version.rb +1 -1
- data/lib/placement_machines.rb +37 -0
- data/lib/placement_machines_native/placement_machines_native.so +0 -0
- data/sig/placement_machines.rbs +52 -8
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 69dcd1427c3123b73bd2e05282c736f99db42a0f30d9a8593c06040c4a0c5144
|
|
4
|
+
data.tar.gz: 545e7b7b698e004509d0014a3344f38199372f6659ec3dd454421213d7bb99ea
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9ea007f6845127a753fa3535323027dee76f72286c52e2db6353176956c669cf85265f51e25e3e965ae79384d15c6d0ac3c65d5159bf6ce2b740c8ad512f821d
|
|
7
|
+
data.tar.gz: 68f1d557e3aedb1ba04ce4c32addfd44cddf090e06e4c2f0b3aa520a88191c6f357493ff5de5bf30b0c9812b254631f04ca4cf3fd6351461fe8a39f5f57f70f6
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,59 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.4.3](https://github.com/seuros/placement_machines/compare/placement_machines/v0.4.2...placement_machines/v0.4.3) (2026-11-16)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* drop slice indexing in replicas and rebuild on matryona 0.5 ([a67af13](https://github.com/seuros/placement_machines/commit/a67af13f16f487e2272ed2c449bd160ec1bd75f5))
|
|
9
|
+
|
|
10
|
+
## [0.4.2](https://github.com/seuros/placement_machines/compare/placement_machines/v0.4.1...placement_machines/v0.4.2) (2026-11-14)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* **deps:** require matryona-machines >= 0.4 ([6c43867](https://github.com/seuros/placement_machines/commit/6c43867a5096b135d1a296c95ce2a4156121d9b4))
|
|
16
|
+
|
|
17
|
+
## [0.4.1](https://github.com/seuros/placement_machines/compare/placement_machines/v0.4.0...placement_machines/v0.4.1) (2026-11-14)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* **deps:** require matryona-machines >= 0.3 ([a84ef2c](https://github.com/seuros/placement_machines/commit/a84ef2c12a9dee0585c42e0d1e9e317dd8fa7e5a))
|
|
23
|
+
|
|
24
|
+
## [0.4.0](https://github.com/seuros/placement_machines/compare/placement_machines/v0.3.0...placement_machines/v0.4.0) (2026-11-14)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### ⚠ BREAKING CHANGES
|
|
28
|
+
|
|
29
|
+
* build the binding on matryona-machines 0.2; read Node and RingPoint fields from the struct; compiler-checked refusal symbols; ship no test files
|
|
30
|
+
|
|
31
|
+
### Features
|
|
32
|
+
|
|
33
|
+
* build the binding on matryona-machines 0.2; read Node and RingPoint fields from the struct; compiler-checked refusal symbols; ship no test files ([cc5e869](https://github.com/seuros/placement_machines/commit/cc5e869c9a7d35f5a468b649abbe6c37a7b9a3e6))
|
|
34
|
+
|
|
35
|
+
## [0.3.0](https://github.com/seuros/placement_machines/compare/placement_machines/v0.2.0...placement_machines/v0.3.0) (2026-11-14)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
### ⚠ BREAKING CHANGES
|
|
39
|
+
|
|
40
|
+
* validate Node and RingPoint through the core, seal values, document replicas cost
|
|
41
|
+
|
|
42
|
+
### Features
|
|
43
|
+
|
|
44
|
+
* validate Node and RingPoint through the core, seal values, document replicas cost ([c1e59de](https://github.com/seuros/placement_machines/commit/c1e59de9b6f471287d8184dae5367651aefe60ad))
|
|
45
|
+
|
|
46
|
+
## [0.2.0](https://github.com/seuros/placement_machines/compare/placement_machines/v0.1.4...placement_machines/v0.2.0) (2026-11-13)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
### ⚠ BREAKING CHANGES
|
|
50
|
+
|
|
51
|
+
* converted to the fleet conventions. One exhaustive Refusal per crate, Result from every operation that can refuse, a refusal changes nothing, provable idempotent repeats are honoured, Ruby mirrors the core. No compatibility aliases. replicas(key, nodes, out) returns Result<usize, Refusal>; Ruby takes Node and RingPoint values.
|
|
52
|
+
|
|
53
|
+
### Features
|
|
54
|
+
|
|
55
|
+
* write replicas to an output buffer and refuse repeated node ids ([420b38e](https://github.com/seuros/placement_machines/commit/420b38eb0131db322e38d82728a84e0369ab98bf))
|
|
56
|
+
|
|
3
57
|
## [0.1.4](https://github.com/seuros/placement_machines/compare/placement_machines/v0.1.3...placement_machines/v0.1.4) (2026-11-06)
|
|
4
58
|
|
|
5
59
|
|
data/README.md
CHANGED
|
@@ -11,14 +11,16 @@ gem 'placement_machines'
|
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
```ruby
|
|
14
|
-
|
|
15
|
-
nodes = [[10, 1], [20, 1], [30, 2], [40, 2]]
|
|
14
|
+
Node = PlacementMachines::Node
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
# Node.new(id, failure_domain) for every node currently alive
|
|
17
|
+
nodes = [Node.new(10, 1), Node.new(20, 1), Node.new(30, 2), Node.new(40, 2)]
|
|
18
|
+
|
|
19
|
+
PlacementMachines.replicas(42, nodes, 1) # => [Node(id: 30, domain: 2)]
|
|
20
|
+
PlacementMachines.replicas(42, nodes, 3) # => [Node(id: 30, domain: 2), Node(id: 20, domain: 1)]
|
|
19
21
|
```
|
|
20
22
|
|
|
21
|
-
Node 30 owns key 42. If you want copies, the second goes to node 20, in a different domain.
|
|
23
|
+
Node 30 owns key 42. If you want copies, the second goes to node 20, in a different domain. You asked for three and got two, because there are only two domains and the gem refuses to put two copies in the same one.
|
|
22
24
|
|
|
23
25
|
## A Message to the Resistance
|
|
24
26
|
|
|
@@ -51,7 +53,7 @@ Here is the life of one key:
|
|
|
51
53
|
|
|
52
54
|
```mermaid
|
|
53
55
|
stateDiagram-v2
|
|
54
|
-
[*] --> owned_by_30:
|
|
56
|
+
[*] --> owned_by_30: replicas(42, nodes, 1)
|
|
55
57
|
owned_by_30 --> owned_by_30: some other node joins or leaves
|
|
56
58
|
owned_by_30 --> owned_by_20: node 30 leaves, next-best score wins
|
|
57
59
|
owned_by_20 --> owned_by_30: node 30 returns, its score still wins
|
|
@@ -66,44 +68,44 @@ sequenceDiagram
|
|
|
66
68
|
participant N30 as Node 30
|
|
67
69
|
participant N20 as Node 20
|
|
68
70
|
|
|
69
|
-
App->>P:
|
|
70
|
-
P-->>App: 30
|
|
71
|
+
App->>P: replicas(42, [10, 20, 30, 40], 1)
|
|
72
|
+
P-->>App: [node 30]
|
|
71
73
|
App->>N30: write key 42
|
|
72
74
|
Note over N30: reboots for a kernel patch
|
|
73
75
|
|
|
74
|
-
App->>P:
|
|
75
|
-
P-->>App: 20
|
|
76
|
+
App->>P: replicas(42, [10, 20, 40], 1)
|
|
77
|
+
P-->>App: [node 20]
|
|
76
78
|
App->>N20: write key 42
|
|
77
79
|
Note over App: keys owned by 10, 20 and 40 never moved
|
|
78
80
|
|
|
79
81
|
Note over N30: back online
|
|
80
|
-
App->>P:
|
|
81
|
-
P-->>App: 30
|
|
82
|
+
App->>P: replicas(42, [10, 20, 30, 40], 1)
|
|
83
|
+
P-->>App: [node 30]
|
|
82
84
|
```
|
|
83
85
|
|
|
84
86
|
## How Placement Works
|
|
85
87
|
|
|
86
88
|
### Rendezvous
|
|
87
89
|
|
|
88
|
-
Every node gets a score for every key. Highest score owns the key. The score depends only on the key and the node id, never on who else is in the list.
|
|
90
|
+
Every node gets a score for every key. Highest score owns the key. The score depends only on the key and the node id, never on who else is in the list. The owner is the first replica, so ask for one:
|
|
89
91
|
|
|
90
92
|
```ruby
|
|
91
|
-
|
|
93
|
+
Node = PlacementMachines::Node
|
|
94
|
+
nodes = [Node.new(10, 1), Node.new(20, 1), Node.new(30, 2), Node.new(40, 2)]
|
|
95
|
+
owner = ->(key, list) { PlacementMachines.replicas(key, list, 1).first }
|
|
92
96
|
|
|
93
|
-
|
|
94
|
-
|
|
97
|
+
owner.(42, nodes).id # => 30
|
|
98
|
+
owner.(42, nodes.reject { |node| node.id == 30 }).id # => 20
|
|
95
99
|
```
|
|
96
100
|
|
|
97
101
|
Node 30 drops out and key 42 goes to node 20, the runner-up. Measure it over a thousand keys and the point lands:
|
|
98
102
|
|
|
99
103
|
```ruby
|
|
100
104
|
before = nodes
|
|
101
|
-
after = nodes.reject { |
|
|
105
|
+
after = nodes.reject { |node| node.id == 30 }
|
|
102
106
|
|
|
103
|
-
moved
|
|
104
|
-
|
|
105
|
-
end
|
|
106
|
-
owned_by_30 = (0...1000).count { |key| PlacementMachines.rendezvous_owner(key, before) == 30 }
|
|
107
|
+
moved = (0...1000).count { |key| owner.(key, before) != owner.(key, after) }
|
|
108
|
+
owned_by_30 = (0...1000).count { |key| owner.(key, before).id == 30 }
|
|
107
109
|
|
|
108
110
|
moved == owned_by_30 # => true
|
|
109
111
|
```
|
|
@@ -121,25 +123,26 @@ Larger wins. Equal scores go to the lower node id, so there is never a tie.
|
|
|
121
123
|
|
|
122
124
|
### Replicas across failure domains
|
|
123
125
|
|
|
124
|
-
The second
|
|
126
|
+
The second field of a node is its failure domain: a rack, an availability zone, a power circuit, whatever you don't want to lose twice. `replicas(key, nodes, limit)` answers at most `limit` nodes, highest score first, and skips any node whose domain is already represented.
|
|
125
127
|
|
|
126
128
|
```ruby
|
|
127
|
-
nodes = [
|
|
128
|
-
PlacementMachines.replicas(42, nodes) # => [30, 20
|
|
129
|
+
nodes = [Node.new(10, 1), Node.new(20, 1), Node.new(30, 2), Node.new(40, 2)]
|
|
130
|
+
PlacementMachines.replicas(42, nodes, 3).map(&:id) # => [30, 20]
|
|
129
131
|
|
|
130
|
-
PlacementMachines.replicas(7, [
|
|
132
|
+
PlacementMachines.replicas(7, [Node.new(10, 1)], 3).map(&:id) # => [10]
|
|
131
133
|
```
|
|
132
134
|
|
|
133
|
-
Two domains, two replicas
|
|
135
|
+
Two domains, two replicas. One node, one replica. The gem never pads the answer with a node that doesn't exist or a node that shares a domain with an earlier pick. A short answer is information: you are under-replicated, and `limit - answer.size` says by how much.
|
|
134
136
|
|
|
135
|
-
|
|
137
|
+
You get the whole `Node` back, domain included, so you never have to look up where a replica lives.
|
|
136
138
|
|
|
137
139
|
### The ring
|
|
138
140
|
|
|
139
|
-
If you already run a hash ring with virtual nodes, hand it the points. Each point is `
|
|
141
|
+
If you already run a hash ring with virtual nodes, hand it the points. Each point is `RingPoint.new(position, node_id)`, in any order. The owner is the first point clockwise from the key, wrapping around at the top.
|
|
140
142
|
|
|
141
143
|
```ruby
|
|
142
|
-
|
|
144
|
+
RingPoint = PlacementMachines::RingPoint
|
|
145
|
+
ring = [RingPoint.new(100, 1), RingPoint.new(500, 2), RingPoint.new(900, 3)]
|
|
143
146
|
|
|
144
147
|
PlacementMachines.ring_owner(250, ring) # => 2
|
|
145
148
|
PlacementMachines.ring_owner(500, ring) # => 2, exact hit
|
|
@@ -153,26 +156,48 @@ A node may appear at many positions. Two points on the same position go to the l
|
|
|
153
156
|
No nodes means no owner, not an error:
|
|
154
157
|
|
|
155
158
|
```ruby
|
|
156
|
-
PlacementMachines.
|
|
157
|
-
PlacementMachines.
|
|
158
|
-
|
|
159
|
+
PlacementMachines.replicas(1, [], 3) # => []
|
|
160
|
+
PlacementMachines.ring_owner(1, []) # => nil
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### The same node twice
|
|
164
|
+
|
|
165
|
+
A membership that lists one node id twice is not a membership, and the gem will not guess which entry you meant. That is a refusal, and a refusal is a symbol, not an exception:
|
|
166
|
+
|
|
167
|
+
```ruby
|
|
168
|
+
PlacementMachines.replicas(1, [Node.new(1, 1), Node.new(1, 2)], 3) # => :repeated_node
|
|
159
169
|
```
|
|
160
170
|
|
|
161
171
|
## The Rules of the Tender
|
|
162
172
|
|
|
163
|
-
- **Everything is an Integer** in `0..2**64-1`: keys, node ids, domains, ring positions. Floats raise `TypeError`, negatives and overflow raise `RangeError`. Objects that merely respond to `to_int` are not integers.
|
|
164
|
-
- **Nodes are `
|
|
165
|
-
- **
|
|
166
|
-
- **
|
|
173
|
+
- **Everything is an Integer** in `0..2**64-1`: keys, node ids, domains, ring positions, limits. Floats raise `TypeError`, negatives and overflow raise `RangeError`. Objects that merely respond to `to_int` are not integers.
|
|
174
|
+
- **Nodes are `PlacementMachines::Node` values** (`id`, `domain`), ring points are `PlacementMachines::RingPoint` values (`position`, `node`), the same fields in the same order as the core's structs. A bare Array pair, or anything else, raises `TypeError`.
|
|
175
|
+
- **A value is valid or does not exist.** `Node.new` and `RingPoint.new` go through the core's constructors: `Node.new(1.0, 0)` raises `TypeError` and `Node.new(-1, 0)` raises `RangeError` on the spot, not later inside `replicas`. So does `with`. Both classes are sealed: subclassing raises `TypeError` and `allocate` is gone. That stops accidents, not reflection, so the binding checks too: every entry it reads must be an instance of exactly `Node` or `RingPoint` (`TypeError` otherwise, a subclass included) and its fields go through the core's constructor again (`RangeError`), so a value forged with `Data#initialize` or `Marshal` never reaches the core and never earns `:repeated_node`.
|
|
176
|
+
- **Exceptions are for malformed input only.** Wrong type is `TypeError`, an Integer outside `0..2**64-1` is `RangeError`, and `ArgumentError` is Ruby's own, for the wrong number of arguments.
|
|
177
|
+
- **Arguments are positional**, in the core's order. There are no keywords and no defaults: `limit` is the length of the core's output buffer, and you always say it.
|
|
178
|
+
- **Node ids are yours.** The gem issues no ids, so there is nothing to scope and nothing to restore after a restart. Nothing is stored between calls.
|
|
179
|
+
- **A refusal is a symbol, never an exception.** The same id twice in one list answers `:repeated_node`, even with different domains, whatever the limit.
|
|
180
|
+
- **No clock.** Nothing here is a time; no call reads, takes or records one, so no answer depends on an instant.
|
|
181
|
+
- **No ceilings, so you own the cost.** The core reads a slice of any length, so the gem does too: there is no maximum number of nodes or ring points. With `n` nodes and `k` picks (`k` is at most `limit`, the number of domains, and `n`), `replicas` first compares every pair of ids, about `n²/2` comparisons, then for each pick scores all `n` nodes and checks each against the picks so far: `k·n` scores and about `k²·n/2` domain comparisons. That is quadratic in `n` for a small `limit`, and cubic when `limit` and the number of domains grow with `n`. A thousand nodes with `limit` 3 is a few hundred thousand cheap comparisons; a thousand single-node domains with `limit` 1000 is hundreds of millions. `ring_owner` and `score` are linear and constant. Bound the membership yourself if callers can make it large.
|
|
167
182
|
- **Inputs are never touched.** Pass frozen arrays if you like. Results are fresh arrays you own.
|
|
168
|
-
|
|
183
|
+
|
|
184
|
+
### Every answer
|
|
185
|
+
|
|
186
|
+
A call that is honoured answers with a value. A call that is not honoured answers with a refusal symbol, the snake_case of the core's `Refusal` variant. There is no catch-all symbol.
|
|
187
|
+
|
|
188
|
+
| Call | Honoured | Refusals | Records the clock |
|
|
189
|
+
| --- | --- | --- | --- |
|
|
190
|
+
| `score(key, node)` | `Integer` in `0..2**64-1` | none, a query | no, there is none |
|
|
191
|
+
| `replicas(key, nodes, limit)` | `Array` of at most `limit` `Node`s, best first, one per failure domain; shorter, or empty, when there are fewer domains | `:repeated_node` | no |
|
|
192
|
+
| `Node.new(id, domain)`, `RingPoint.new(position, node)` | the value | none; malformed fields raise | no |
|
|
193
|
+
| `ring_owner(key, ring)` | `Integer` node id, or `nil` for an empty ring | none, a query | no |
|
|
169
194
|
|
|
170
195
|
## What You Think You're Doing vs Reality
|
|
171
196
|
|
|
172
197
|
### You Think: "Three replicas means I can lose two nodes."
|
|
173
198
|
### Reality: Three replicas on one rack means you can lose one power strip.
|
|
174
199
|
|
|
175
|
-
Spreading copies across nodes protects you from a node dying. It does nothing when the thing that dies is the rack, the zone, or the circuit all three nodes share. The domain is how you tell the placer what fails together. Give every node the same domain and `replicas` will honestly hand you one copy
|
|
200
|
+
Spreading copies across nodes protects you from a node dying. It does nothing when the thing that dies is the rack, the zone, or the circuit all three nodes share. The domain is how you tell the placer what fails together. Give every node the same domain and `replicas` will honestly hand you one copy when you asked for three, because that is what you actually have.
|
|
176
201
|
|
|
177
202
|
## What's Yours
|
|
178
203
|
|
|
@@ -1,5 +1,45 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.5.1](https://github.com/seuros/placement_machines/compare/placement-machines-v0.5.0...placement-machines-v0.5.1) (2026-11-16)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* drop slice indexing in replicas and rebuild on matryona 0.5 ([a67af13](https://github.com/seuros/placement_machines/commit/a67af13f16f487e2272ed2c449bd160ec1bd75f5))
|
|
9
|
+
|
|
10
|
+
## [0.5.0](https://github.com/seuros/placement_machines/compare/placement-machines-v0.4.0...placement-machines-v0.5.0) (2026-11-14)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### ⚠ BREAKING CHANGES
|
|
14
|
+
|
|
15
|
+
* build the binding on matryona-machines 0.2; read Node and RingPoint fields from the struct; compiler-checked refusal symbols; ship no test files
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* build the binding on matryona-machines 0.2; read Node and RingPoint fields from the struct; compiler-checked refusal symbols; ship no test files ([cc5e869](https://github.com/seuros/placement_machines/commit/cc5e869c9a7d35f5a468b649abbe6c37a7b9a3e6))
|
|
20
|
+
|
|
21
|
+
## [0.4.0](https://github.com/seuros/placement_machines/compare/placement-machines-v0.3.0...placement-machines-v0.4.0) (2026-11-14)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### ⚠ BREAKING CHANGES
|
|
25
|
+
|
|
26
|
+
* validate Node and RingPoint through the core, seal values, document replicas cost
|
|
27
|
+
|
|
28
|
+
### Features
|
|
29
|
+
|
|
30
|
+
* validate Node and RingPoint through the core, seal values, document replicas cost ([c1e59de](https://github.com/seuros/placement_machines/commit/c1e59de9b6f471287d8184dae5367651aefe60ad))
|
|
31
|
+
|
|
32
|
+
## [0.3.0](https://github.com/seuros/placement_machines/compare/placement-machines-v0.2.2...placement-machines-v0.3.0) (2026-11-13)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### ⚠ BREAKING CHANGES
|
|
36
|
+
|
|
37
|
+
* converted to the fleet conventions. One exhaustive Refusal per crate, Result from every operation that can refuse, a refusal changes nothing, provable idempotent repeats are honoured, Ruby mirrors the core. No compatibility aliases. replicas(key, nodes, out) returns Result<usize, Refusal>; Ruby takes Node and RingPoint values.
|
|
38
|
+
|
|
39
|
+
### Features
|
|
40
|
+
|
|
41
|
+
* write replicas to an output buffer and refuse repeated node ids ([420b38e](https://github.com/seuros/placement_machines/commit/420b38eb0131db322e38d82728a84e0369ab98bf))
|
|
42
|
+
|
|
3
43
|
## [0.2.2](https://github.com/seuros/placement_machines/compare/placement-machines-v0.2.1...placement-machines-v0.2.2) (2026-11-06)
|
|
4
44
|
|
|
5
45
|
|
|
@@ -10,24 +10,63 @@ not a cryptographic hash. Do not use these scores for adversarial admission or
|
|
|
10
10
|
security decisions. Ties favor lower node IDs, independent of input order.
|
|
11
11
|
|
|
12
12
|
Replica selection requires unique node IDs and chooses at most one node in each
|
|
13
|
-
failure domain. Missing replicas are
|
|
13
|
+
failure domain. Missing replicas are a shorter answer, never repeated nodes.
|
|
14
14
|
Weights, fallback within an exhausted domain, and topology hierarchies are not
|
|
15
15
|
implemented. Ring lookup accepts caller-generated virtual points, including
|
|
16
16
|
wraparound; ring construction and hash-version agreement are external contracts.
|
|
17
17
|
Placement does not define replica membership changes or consensus quorums.
|
|
18
18
|
|
|
19
19
|
```rust
|
|
20
|
-
use placement_machines::{Node, NodeId, replicas};
|
|
21
|
-
let nodes = [
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
#
|
|
31
|
-
# }
|
|
32
|
-
# assert_eq!(example(&nodes), Some(()));
|
|
20
|
+
use placement_machines::{Node, NodeId, Refusal, replicas};
|
|
21
|
+
let nodes = [Node::new(NodeId(1), 10), Node::new(NodeId(2), 20)];
|
|
22
|
+
let mut out = [Node::default(); 3];
|
|
23
|
+
let written = replicas(42, &nodes, &mut out)?;
|
|
24
|
+
// Two failure domains: two replicas, and the third entry was never written.
|
|
25
|
+
assert_eq!(written, 2);
|
|
26
|
+
assert_ne!(out[0].domain, out[1].domain);
|
|
27
|
+
|
|
28
|
+
let repeated = [nodes[0], nodes[0]];
|
|
29
|
+
assert_eq!(replicas(42, &repeated, &mut out), Err(Refusal::RepeatedNode));
|
|
30
|
+
# Ok::<(), Refusal>(())
|
|
33
31
|
```
|
|
32
|
+
|
|
33
|
+
## Contract
|
|
34
|
+
|
|
35
|
+
- **Stateless.** Three pure functions: `score`, `replicas`, `ring_owner`.
|
|
36
|
+
Nothing is stored between calls, so there is nothing to repeat, restore or
|
|
37
|
+
scope; the same inputs always give the same answer.
|
|
38
|
+
- **Output buffer.** `replicas(key, nodes, out: &mut [Node])` writes the first
|
|
39
|
+
`n` entries of `out`, best first, leaves the rest untouched and returns
|
|
40
|
+
`Ok(n)`. `n` is at most `out.len()` and is smaller when the membership has
|
|
41
|
+
fewer failure domains; `Ok(0)` for an empty membership or an empty buffer.
|
|
42
|
+
`out[0]` is the rendezvous owner of the key.
|
|
43
|
+
- **Refusals.** `Refusal` is the one refusal type and has one variant:
|
|
44
|
+
`RepeatedNode`, two entries of `nodes` carry the same `NodeId` (whatever
|
|
45
|
+
their domains). The membership is rejected whole and `out` is untouched,
|
|
46
|
+
whatever its length. `score` and `ring_owner` cannot refuse.
|
|
47
|
+
- **Placeholders.** `Node`, `NodeId` and `RingPoint` implement `Default` so a
|
|
48
|
+
buffer is `[Node::default(); N]`. The default is a placeholder, not a
|
|
49
|
+
sentinel: `NodeId(0)` is an ordinary id a caller may use. Read only the `n`
|
|
50
|
+
entries reported.
|
|
51
|
+
- **Constructors.** `Node::new(id, domain)` and `RingPoint::new(position,
|
|
52
|
+
node)` are `const` and infallible: every `u64` is a valid id, domain and
|
|
53
|
+
position, so there is no value to reject and no `Option`. A binding builds
|
|
54
|
+
its values through them. What can be wrong is a *membership* (a repeated
|
|
55
|
+
id), and that is `replicas`' refusal.
|
|
56
|
+
- **No capacity, so the caller owns the cost.** The functions read slices of
|
|
57
|
+
any length; there is no const-generic bound and no maximum membership or
|
|
58
|
+
ring size. For `n` nodes and `k` entries written (`k` is at most
|
|
59
|
+
`out.len()`, the number of domains, and `n`), `replicas` makes about `n²/2`
|
|
60
|
+
id comparisons to reject a repeated id, then `k·n` `score` calls and about
|
|
61
|
+
`k²·n/2` domain comparisons to pick: quadratic in `n` for a fixed buffer,
|
|
62
|
+
cubic when the buffer and the number of domains grow with `n`.
|
|
63
|
+
`ring_owner` is linear in the ring, `score` constant. Bound the membership
|
|
64
|
+
before calling if it comes from an untrusted source.
|
|
65
|
+
- **Ids are the caller's.** `NodeId(pub u64)` is supplied by the caller and
|
|
66
|
+
built directly; nothing here issues ids, so there are no scopes.
|
|
67
|
+
- **Queries.** `ring_owner` answers `None` only for an empty ring. Two points
|
|
68
|
+
on one position go to the lower node id.
|
|
69
|
+
- **No time.** Keys, ids, domains and ring positions are plain `u64` values;
|
|
70
|
+
no function reads, takes or records a clock, so no answer depends on `now`.
|
|
71
|
+
- **No unreachable arms.** No `panic!`, `assert!`, `unreachable!` or `expect`:
|
|
72
|
+
every branch is reachable through the public functions.
|
data/lib/placement_machines.rb
CHANGED
|
@@ -2,5 +2,42 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative 'placement_machines/version'
|
|
4
4
|
|
|
5
|
+
module PlacementMachines
|
|
6
|
+
# The core's `Node`: a node id (the core's `NodeId`, a caller-chosen Integer)
|
|
7
|
+
# and the failure domain it shares with every node carrying the same value.
|
|
8
|
+
# Built through the core's `Node::new`: both fields are Integers in
|
|
9
|
+
# 0..2**64-1, anything else raises TypeError (not an Integer) or RangeError.
|
|
10
|
+
Node = Data.define(:id, :domain) do
|
|
11
|
+
def initialize(id:, domain:)
|
|
12
|
+
id, domain = PlacementMachines.__send__(:__node, id, domain)
|
|
13
|
+
super
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# The core's `RingPoint`: a ring coordinate and the id of the node that owns
|
|
18
|
+
# it (the core's `NodeId`). Built through the core's `RingPoint::new`, under
|
|
19
|
+
# the same rule as Node.
|
|
20
|
+
RingPoint = Data.define(:position, :node) do
|
|
21
|
+
def initialize(position:, node:)
|
|
22
|
+
position, node = PlacementMachines.__send__(:__ring_point, position, node)
|
|
23
|
+
super
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
5
28
|
# There is no pure-Ruby policy fallback when the native extension is missing.
|
|
29
|
+
# It validates and builds the Data values above.
|
|
6
30
|
require 'placement_machines_native/placement_machines_native'
|
|
31
|
+
|
|
32
|
+
module PlacementMachines
|
|
33
|
+
private_class_method :__node, :__ring_point
|
|
34
|
+
|
|
35
|
+
# The values are final. `initialize` is where each validates, and a subclass
|
|
36
|
+
# could override it, or `allocate` build an instance that never ran it:
|
|
37
|
+
# subclassing raises TypeError and `allocate` is gone. This stops accidents,
|
|
38
|
+
# not reflection, so the binding also checks every value it reads.
|
|
39
|
+
[Node, RingPoint].each do |value|
|
|
40
|
+
value.singleton_class.undef_method(:allocate)
|
|
41
|
+
value.define_singleton_method(:inherited) { |_| raise TypeError, "#{value} cannot be subclassed" }
|
|
42
|
+
end
|
|
43
|
+
end
|
|
Binary file
|
data/sig/placement_machines.rbs
CHANGED
|
@@ -1,11 +1,55 @@
|
|
|
1
1
|
module PlacementMachines
|
|
2
2
|
VERSION: String
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
|
|
4
|
+
# The core's `Refusal`: the call answered nothing.
|
|
5
|
+
# :repeated_node, two entries of the membership carry the same node id.
|
|
6
|
+
type refusal = :repeated_node
|
|
7
|
+
|
|
8
|
+
# The core's `Node`, built through `Node::new`. `id` is the core's `NodeId`,
|
|
9
|
+
# a caller-chosen Integer in 0..2**64-1; `domain` is the failure domain, same
|
|
10
|
+
# range. `new` (and `with`) raises TypeError for a non-Integer and RangeError
|
|
11
|
+
# for an Integer outside the range: a Node that exists is valid. Sealed
|
|
12
|
+
# (subclassing raises TypeError, no `allocate`), and the binding reads only
|
|
13
|
+
# instances of exactly this class, re-checking their fields, so a value
|
|
14
|
+
# forged by reflection or Marshal raises TypeError/RangeError.
|
|
15
|
+
class Node < Data
|
|
16
|
+
attr_reader id: Integer
|
|
17
|
+
attr_reader domain: Integer
|
|
18
|
+
def self.new: (Integer id, Integer domain) -> Node
|
|
19
|
+
| (id: Integer, domain: Integer) -> Node
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# The core's `RingPoint`, built through `RingPoint::new`. `node` is the
|
|
23
|
+
# core's `NodeId` of the owner. Both fields are Integers in 0..2**64-1; `new`
|
|
24
|
+
# raises TypeError for a non-Integer and RangeError outside the range.
|
|
25
|
+
# Sealed and re-checked by the binding like Node.
|
|
26
|
+
class RingPoint < Data
|
|
27
|
+
attr_reader position: Integer
|
|
28
|
+
attr_reader node: Integer
|
|
29
|
+
def self.new: (Integer position, Integer node) -> RingPoint
|
|
30
|
+
| (position: Integer, node: Integer) -> RingPoint
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The core's `score(key, node)`: always an Integer in 0..2**64-1. Larger wins.
|
|
34
|
+
def self.score: (Integer key, Integer node) -> Integer
|
|
35
|
+
|
|
36
|
+
# The core's `replicas(key, nodes, out)`, with `limit` as the length of
|
|
37
|
+
# `out`. Honoured: an Array of at most `limit` Nodes, best first, one per
|
|
38
|
+
# failure domain, shorter (possibly empty) when the membership has fewer
|
|
39
|
+
# domains. Refused: :repeated_node. Never nil, and never raises for a
|
|
40
|
+
# refusal. Raises TypeError for a non-Array, a non-Node entry or a
|
|
41
|
+
# non-Integer; RangeError for an Integer outside 0..2**64-1. The core reads
|
|
42
|
+
# a slice of any length, so there is no ceiling on the membership.
|
|
43
|
+
def self.replicas: (Integer key, Array[Node] nodes, Integer limit) -> (Array[Node] | refusal)
|
|
44
|
+
|
|
45
|
+
# The core's `ring_owner(key, ring)`: the node id owning the first point
|
|
46
|
+
# clockwise from `key`, or nil for an empty ring. A query; it cannot refuse.
|
|
47
|
+
# Raises TypeError for a non-Array, a non-RingPoint entry or a non-Integer;
|
|
48
|
+
# RangeError for an Integer outside 0..2**64-1. No ceiling on the ring.
|
|
49
|
+
def self.ring_owner: (Integer key, Array[RingPoint] ring) -> Integer?
|
|
50
|
+
|
|
51
|
+
# Private native hooks behind `Node.new` and `RingPoint.new`: the fields as
|
|
52
|
+
# the core holds them, TypeError/RangeError for a value it has no type for.
|
|
53
|
+
private def self.__node: (Integer id, Integer domain) -> [Integer, Integer]
|
|
54
|
+
private def self.__ring_point: (Integer position, Integer node) -> [Integer, Integer]
|
|
11
55
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: placement_machines
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.3
|
|
5
5
|
platform: aarch64-linux
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A thin binding to the no_std placement-machines Rust core. Provides deterministic
|
|
14
14
|
rendezvous scores, ring ownership and distinct-domain replica choices without membership
|