Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ at the quota gets 409. `DELETE /v1/private-servers/{id}` drains the server. An i
World persistence is spec only for now: a later version snapshots the instance directory to a template on
stop and restores it on the next start.

## Region cells

A pool with a `sharding` section splits its world into cells (`cells[]` with chunk bounds `minX/minZ/maxX/maxZ`,
inclusive; `cellSize` in chunks defaults to 32 and is only used once cells are generated). The controller gives
each cell to exactly one Ready instance of the pool and stores it as the instance label `grus.olf/cells`. An
assignment stays while its instance is Ready; cells of a failed or lost instance move to the Ready instance with
the fewest cells. Every change publishes `pool.cells`. Splitting and merging cells with the load is spec only.

```sh
grus cells world
curl -H "Authorization: Bearer $TOKEN" "$GRUS_URL/v1/pools/world/cells/at?x=520&z=10"
```

`GET /v1/pools/{pool}/cells` lists cell, bounds, instance and address; `GET /v1/pools/{pool}/cells/at?x=&z=` takes
block coordinates (chunk = block >> 4) and answers with the owning cell (404 when no cell covers the block, 409
while the cell has no Ready instance). The Velocity plugin adds `/region <x> <z>` (permission `grus.region`) for
the pool named in `GRUS_SHARD_POOL`.

## License

Apache License 2.0. See [LICENSE](LICENSE).
173 changes: 170 additions & 3 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,78 @@ components:
format: int64
type: integer
type: object
Cell:
additionalProperties: false
properties:
maxX:
description: Largest chunk x of the cell
format: int64
type: integer
maxZ:
description: Largest chunk z of the cell
format: int64
type: integer
minX:
description: Smallest chunk x of the cell
format: int64
type: integer
minZ:
description: Smallest chunk z of the cell
format: int64
type: integer
name:
description: Cell name; assigned to exactly one Ready instance
type: string
required:
- name
- minX
- minZ
- maxX
- maxZ
type: object
CellBounds:
additionalProperties: false
properties:
maxX:
format: int64
type: integer
maxZ:
format: int64
type: integer
minX:
format: int64
type: integer
minZ:
format: int64
type: integer
required:
- minX
- minZ
- maxX
- maxZ
type: object
CellInfo:
additionalProperties: false
properties:
$schema:
description: A URL to the JSON Schema for this object.
examples:
- https://example.com/schemas/CellInfo.json
format: uri
readOnly: true
type: string
address:
type: string
bounds:
$ref: "#/components/schemas/CellBounds"
cell:
type: string
instance:
type: string
required:
- cell
- bounds
type: object
Command:
additionalProperties: false
properties:
Expand Down Expand Up @@ -860,13 +932,13 @@ components:
additionalProperties: false
properties:
cellSize:
description: Cell edge length in chunks
description: Cell edge length in chunks, used when cells are generated (default 32)
format: int64
type: integer
cells:
description: Explicit cell names
description: Static cell list; bounds are chunk coordinates
items:
type: string
$ref: "#/components/schemas/Cell"
type:
- array
- "null"
Expand Down Expand Up @@ -1758,6 +1830,101 @@ paths:
security:
- bearer: []
summary: Create or replace a pool
/v1/pools/{pool}/cells:
get:
operationId: list-pool-cells
parameters:
- description: Pool name
in: path
name: pool
required: true
schema:
description: Pool name
type: string
responses:
"200":
content:
application/json:
schema:
items:
$ref: "#/components/schemas/CellInfo"
type:
- array
- "null"
description: OK
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
security:
- bearer: []
summary: List the cells of a sharded pool with the instance that serves each
/v1/pools/{pool}/cells/at:
get:
description: 404 when no cell covers the block, 409 when the cell has no Ready instance yet.
operationId: get-pool-cell-at
parameters:
- description: Pool name
in: path
name: pool
required: true
schema:
description: Pool name
type: string
- description: Block x coordinate
explode: false
in: query
name: x
required: true
schema:
description: Block x coordinate
format: int64
type: integer
- description: Block z coordinate
explode: false
in: query
name: z
required: true
schema:
description: Block z coordinate
format: int64
type: integer
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/CellInfo"
description: OK
"404":
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Not Found
"409":
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Conflict
"422":
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Unprocessable Entity
"500":
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Internal Server Error
security:
- bearer: []
summary: Find the cell and instance that own a block position
/v1/pools/{pool}/maintenance:
put:
operationId: set-pool-maintenance
Expand Down
10 changes: 9 additions & 1 deletion deploy/crds/pools.grus.onelitefeather.net.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@ spec:
cellSize: {type: integer}
cells:
type: array
items: {type: string}
items:
type: object
required: [name, minX, minZ, maxX, maxZ]
properties:
name: {type: string}
minX: {type: integer}
minZ: {type: integer}
maxX: {type: integer}
maxZ: {type: integer}
private:
type: object
properties:
Expand Down
86 changes: 86 additions & 0 deletions internal/api/cells_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package api

import (
"encoding/json"
"fmt"
"net/http"
"testing"

"github.com/OneLiteFeatherNET/grus/internal/controller"
"github.com/OneLiteFeatherNET/grus/internal/model"
)

func cellFixture(t *testing.T) fixture {
fx := newFixture(t, Config{}, nil)
fx.svc.cells = []model.CellInfo{{Cell: "r0", Bounds: model.CellBounds{MinX: 0, MinZ: 0, MaxX: 31, MaxZ: 31}, Instance: "world-a", Address: "10.0.0.1:25565"}}
return fx
}

func TestListPoolCellsReturnsTheAssignment(t *testing.T) {
fx := cellFixture(t)

rec := fx.call(t, http.MethodGet, "/v1/pools/world/cells", adminToken, "", "")

if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body %s", rec.Code, rec.Body)
}
var got []model.CellInfo
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil || len(got) != 1 || got[0].Instance != "world-a" || got[0].Bounds.MaxX != 31 {
t.Fatalf("body = %s (%v), want the cell r0 owned by world-a", rec.Body, err)
}
if fx.svc.cellsPool != "world" {
t.Errorf("pool = %q, want world", fx.svc.cellsPool)
}
}

func TestCellAtPassesBlockCoordinates(t *testing.T) {
fx := cellFixture(t)

rec := fx.call(t, http.MethodGet, "/v1/pools/world/cells/at?x=-100&z=250", adminToken, "", "")

if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body %s", rec.Code, rec.Body)
}
if fx.svc.cellX != -100 || fx.svc.cellZ != 250 {
t.Errorf("service got x=%d z=%d, want -100 and 250", fx.svc.cellX, fx.svc.cellZ)
}
}

func TestCellAtRequiresBothCoordinates(t *testing.T) {
fx := cellFixture(t)

rec := fx.call(t, http.MethodGet, "/v1/pools/world/cells/at?x=1", adminToken, "", "")

if rec.Code != http.StatusUnprocessableEntity {
t.Fatalf("status = %d, want 422", rec.Code)
}
}

func TestCellAtMapsServiceErrors(t *testing.T) {
for _, tc := range []struct {
err error
want int
}{
{controller.ErrNotFound, http.StatusNotFound},
{fmt.Errorf("%w: cell r0 has no ready instance", controller.ErrInvalidState), http.StatusConflict},
} {
fx := cellFixture(t)
fx.svc.cellsErr = tc.err

rec := fx.call(t, http.MethodGet, "/v1/pools/world/cells/at?x=1&z=1", adminToken, "", "")

if rec.Code != tc.want {
t.Errorf("%v: status = %d, want %d", tc.err, rec.Code, tc.want)
}
}
}

func TestProxyTokenMayResolveCells(t *testing.T) {
fx := cellFixture(t)

rec := fx.call(t, http.MethodGet, "/v1/pools/world/cells/at?x=1&z=1", proxyToken, "", "")

if rec.Code != http.StatusOK {
t.Fatalf("proxy token status = %d, want 200 (instances:read)", rec.Code)
}
}
18 changes: 18 additions & 0 deletions internal/api/fakes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ type fakeService struct {
privateErr error
privateCreated bool
privateOwnerFilter string

cells []model.CellInfo
cellsErr error
cellsPool string
cellX, cellZ int
}

func newFakeService() *fakeService {
Expand Down Expand Up @@ -178,6 +183,19 @@ func (f *fakeService) StopPrivateServer(_ context.Context, id string) (model.Ins
return model.Instance{ID: id}, f.stopErr
}

func (f *fakeService) PoolCells(_ context.Context, pool string) ([]model.CellInfo, error) {
f.cellsPool = pool
return f.cells, f.cellsErr
}

func (f *fakeService) CellAt(_ context.Context, pool string, x, z int) (model.CellInfo, error) {
f.cellsPool, f.cellX, f.cellZ = pool, x, z
if f.cellsErr != nil || len(f.cells) == 0 {
return model.CellInfo{}, f.cellsErr
}
return f.cells[0], nil
}

func (f *fakeService) Command(context.Context, string, string) error { return nil }

func (f *fakeService) AuthenticateInstance(_ context.Context, token string) (model.Instance, error) {
Expand Down
Loading