Skip to content
Merged
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
93 changes: 93 additions & 0 deletions infra/relay/src/environments/ManagedEndpointAllocations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { describe, expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";

import * as RelayDb from "../db.ts";
import { relayManagedEndpointAllocations } from "../persistence/schema.ts";
import * as ManagedEndpointAllocations from "./ManagedEndpointAllocations.ts";

const layerWithDb = (db: RelayDb.RelayDb["Service"]) =>
ManagedEndpointAllocations.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, db)));

describe("ManagedEndpointAllocations", () => {
it.effect("retains database failures with allocation operation and identity", () => {
const cause = new Error("database unavailable");
const fakeDb = {
select: () => ({
from: (table: unknown) => {
expect(table).toBe(relayManagedEndpointAllocations);
return {
where: () => ({
limit: () => Effect.fail(cause),
}),
};
},
}),
} as unknown as RelayDb.RelayDb["Service"];

return Effect.gen(function* () {
const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations;
const error = yield* Effect.flip(
allocations.get({ userId: "user-1", environmentId: "environment-1" }),
);

expect(error).toMatchObject({
_tag: "ManagedEndpointAllocationPersistenceError",
operation: "get",
stage: "database-request",
userId: "user-1",
environmentId: "environment-1",
});
expect(error.cause).toBe(cause);
}).pipe(Effect.provide(layerWithDb(fakeDb)));
});

it.effect("reports an unresolved reservation without manufacturing a cause", () => {
const fakeDb = {
insert: (table: unknown) => {
expect(table).toBe(relayManagedEndpointAllocations);
return {
values: () => ({
onConflictDoNothing: () => ({
returning: () => Effect.succeed([]),
}),
}),
};
},
select: () => ({
from: (table: unknown) => {
expect(table).toBe(relayManagedEndpointAllocations);
return {
where: () => ({
limit: () => Effect.succeed([]),
}),
};
},
}),
} as unknown as RelayDb.RelayDb["Service"];

return Effect.gen(function* () {
const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations;
const error = yield* Effect.flip(
allocations.reserve({
userId: "user-1",
environmentId: "environment-1",
hostname: "environment-1.example.test",
tunnelName: "environment-1-tunnel",
}),
);

expect(error).toMatchObject({
_tag: "ManagedEndpointAllocationPersistenceError",
operation: "reserve",
stage: "resolve-reservation",
userId: "user-1",
environmentId: "environment-1",
hostname: "environment-1.example.test",
tunnelName: "environment-1-tunnel",
});
expect(error.cause).toBeUndefined();
expect(error.message).toContain("'resolve-reservation'");
}).pipe(Effect.provide(layerWithDb(fakeDb)));
});
});
133 changes: 110 additions & 23 deletions infra/relay/src/environments/ManagedEndpointAllocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,29 @@ export function resolveReadyManagedEndpoint(input: {

export class ManagedEndpointAllocationPersistenceError extends Schema.TaggedErrorClass<ManagedEndpointAllocationPersistenceError>()(
"ManagedEndpointAllocationPersistenceError",
{ cause: Schema.Defect() },
{
operation: Schema.Literals([
"get",
"reserve",
"record-tunnel",
"record-dns",
"mark-ready",
"remove",
]),
stage: Schema.Literals(["database-request", "resolve-reservation"]),
userId: Schema.String,
environmentId: Schema.String,
hostname: Schema.optionalKey(Schema.String),
tunnelName: Schema.optionalKey(Schema.String),
tunnelId: Schema.optionalKey(Schema.String),
dnsRecordId: Schema.optionalKey(Schema.String),
cause: Schema.optional(Schema.Defect()),
},
) {
override get message(): string {
return "Failed to persist managed endpoint allocation";
return `Managed endpoint allocation '${this.operation}' failed during '${this.stage}' for user '${this.userId}', environment '${this.environmentId}'`;
}
}
const isManagedEndpointAllocationPersistenceError = Schema.is(
ManagedEndpointAllocationPersistenceError,
);

interface ManagedEndpointAllocationKey {
readonly userId: string;
Expand Down Expand Up @@ -106,11 +120,6 @@ const whereAllocation = (input: ManagedEndpointAllocationKey) =>
eq(relayManagedEndpointAllocations.environmentId, input.environmentId),
);

const persistenceError = (cause: unknown) =>
isManagedEndpointAllocationPersistenceError(cause)
? cause
: new ManagedEndpointAllocationPersistenceError({ cause });

const make = Effect.gen(function* () {
const db = yield* RelayDb.RelayDb;

Expand All @@ -125,7 +134,15 @@ const make = Effect.gen(function* () {
.limit(1)
.pipe(
Effect.map((rows) => rows[0] ?? null),
Effect.mapError(persistenceError),
Effect.mapError(
(cause) =>
new ManagedEndpointAllocationPersistenceError({
operation: "get",
stage: "database-request",
...input,
cause,
}),
),
);
}),
reserve: Effect.fn("relay.managed_endpoint_allocations.reserve")(function* (
Expand All @@ -140,7 +157,18 @@ const make = Effect.gen(function* () {
updatedAt: now,
})
.onConflictDoNothing()
.returning(allocationSelection);
.returning(allocationSelection)
.pipe(
Effect.mapError(
(cause) =>
new ManagedEndpointAllocationPersistenceError({
operation: "reserve",
stage: "database-request",
...input,
cause,
}),
),
);

const allocation =
inserted[0] ??
Expand All @@ -149,16 +177,29 @@ const make = Effect.gen(function* () {
.from(relayManagedEndpointAllocations)
.where(whereAllocation(input))
.limit(1)
.pipe(Effect.map((rows) => rows[0])));
.pipe(
Effect.map((rows) => rows[0]),
Effect.mapError(
(cause) =>
new ManagedEndpointAllocationPersistenceError({
operation: "reserve",
stage: "database-request",
...input,
cause,
}),
),
));

if (allocation === undefined) {
return yield* new ManagedEndpointAllocationPersistenceError({
cause: new Error("Managed endpoint allocation was not persisted."),
operation: "reserve",
stage: "resolve-reservation",
...input,
});
}

return allocation;
}, Effect.mapError(persistenceError)),
}),
recordTunnel: Effect.fn("relay.managed_endpoint_allocations.record_tunnel")(function* (
input: RecordManagedEndpointTunnelInput,
) {
Expand All @@ -168,8 +209,19 @@ const make = Effect.gen(function* () {
tunnelId: input.tunnelId,
updatedAt: DateTime.formatIso(yield* DateTime.now),
})
.where(whereAllocation(input));
}, Effect.mapError(persistenceError)),
.where(whereAllocation(input))
.pipe(
Effect.mapError(
(cause) =>
new ManagedEndpointAllocationPersistenceError({
operation: "record-tunnel",
stage: "database-request",
...input,
cause,
}),
),
);
}),
recordDns: Effect.fn("relay.managed_endpoint_allocations.record_dns")(function* (
input: RecordManagedEndpointDnsInput,
) {
Expand All @@ -179,8 +231,19 @@ const make = Effect.gen(function* () {
dnsRecordId: input.dnsRecordId,
updatedAt: DateTime.formatIso(yield* DateTime.now),
})
.where(whereAllocation(input));
}, Effect.mapError(persistenceError)),
.where(whereAllocation(input))
.pipe(
Effect.mapError(
(cause) =>
new ManagedEndpointAllocationPersistenceError({
operation: "record-dns",
stage: "database-request",
...input,
cause,
}),
),
);
}),
markReady: Effect.fn("relay.managed_endpoint_allocations.mark_ready")(function* (
input: ManagedEndpointAllocationKey,
) {
Expand All @@ -191,13 +254,37 @@ const make = Effect.gen(function* () {
readyAt: now,
updatedAt: now,
})
.where(whereAllocation(input));
}, Effect.mapError(persistenceError)),
.where(whereAllocation(input))
.pipe(
Effect.mapError(
(cause) =>
new ManagedEndpointAllocationPersistenceError({
operation: "mark-ready",
stage: "database-request",
...input,
cause,
}),
),
);
}),
remove: Effect.fn("relay.managed_endpoint_allocations.remove")(function* (
input: ManagedEndpointAllocationKey,
) {
yield* db.delete(relayManagedEndpointAllocations).where(whereAllocation(input));
}, Effect.mapError(persistenceError)),
yield* db
.delete(relayManagedEndpointAllocations)
.where(whereAllocation(input))
.pipe(
Effect.mapError(
(cause) =>
new ManagedEndpointAllocationPersistenceError({
operation: "remove",
stage: "database-request",
...input,
cause,
}),
),
);
}),
});
});

Expand Down
Loading
Loading