Learn

Data services in the eLucid8 playground

A data source is data that already exists, which a program reads: a table, a file, a database. A data service is data made on request: at each point an outside program (an AI model, a web API, a simulator) is asked, and the answer is data stamped with the time it arrived. In a program both are inputs, and nothing in the program says which: the deployment does (the input's card in the playground, its entry in deploy.json). A data service is asked by navigation, as any value is read at a point:

model = input("model", amount : inf)
risk = model[amount = payments]

model is over a dimension amount, and risk asks it, at each point of payments, for its answer at amount = payments. The coordinates of the point asked are the question's inputs; an input has no arguments of its own. Which to use for a given kind of data, with examples, is in Connecting eLucid8 to your data.

The deadline (how long an answer may take), which failures to try again and whether an answer may be reused are settings of the input, not part of the program: on its card in the playground, and in a deployment's deploy.json (deadlineMs, retry, reuse; command reference). In the examples on this page, a comment line such as -- service sum_service: deadline 20 ms; retry overloaded up to 2; reuse successes gives them, -- service quick_sum: simulated sum; takes 30 ms says that a simulated program answers the input quick_sum, and answered by anomaly_detector names the built-in program that answers an input whose channel is not its name.

The playground supplies one service, sum_service. An input whose channel is sum_service is answered by it; the Data In/Out tab shows it as a card beside the program's data, with how long it takes (Takes) and its Deadline on the card, and the rest (reuse, retries) folded under More settings. Any other input needs data or a simulated program on its card: in the playground, an input with no data at all answers an error at every point (input "echoo" has no data: give it rows, or a provider that answers it), and a channel the deployment does not bind is refused before anything runs, so a misspelt name never returns a plausible number. The elucid8 command has two data services built in, sum_service and anomaly_detector, and can ask any other program through a small protocol (see Outside the playground).

The examples on this page are checked against the reference engine by packages/evaluator/test/services.test.ts, and against the elucid8 command's runtime by packages/evaluator/test/rust-eduction.test.ts.

What every question does

These come from the evaluator, whatever answers it:

The services

ServiceOne lineAvailable in
sum_serviceStands in for a data service, such as an AI model or a web API: it adds the coordinates it is asked at and answers after a delayEvery program in the playground; the elucid8 command
Simulated servicesStand in for any data service, under any name: they answer a sum, a score or a formula, optionally with a delay and a failure rateEvery program in the playground (on an input's card); the elucid8 command ("builtin": "simulated")
anomaly_detectorScores a reading against its neighbourhood mean, |reading − mean| roundedThe elucid8 command only; not the playground

sum_service

The playground's stand-in, for trying deadlines in programs you write.

InputsThe coordinates it is asked at; it adds the numbers among them and ignores anything else, such as texts.
OutputA number: their sum (0 if there are none).
Error codesNone: it always answers. Only a missed deadline makes the answer an Error (late).
TimingAnswers a set time after it is asked: 5 ms unless you change it (in the playground, Takes on its card).
Can be retriedYes (it never fails, so retries never happen).
Available inEvery program in the playground.
-- sum_service on time.
x = input("x", time : 3)
sum_service = input("sum_service", a : inf, b : inf)
total = sum_service[a = x, b = 1]
-- data x = #time
-- latency 5
-- service sum_service: deadline 20 ms
-- expect total[time = 2] = 3, at time 5 ms

Open in playground

With the latency set to 30 ms, the same answer comes at 30 ms, after its 20 ms deadline:

-- sum_service late.
x = input("x", time : 3)
sum_service = input("sum_service", a : inf, b : inf)
total = sum_service[a = x, b = 1]
-- data x = #time
-- latency 30
-- service sum_service: deadline 20 ms
-- expect total[time = 2] = error late from sum_service[a = 2, b = 1]: late (30 ms > deadline 20 ms), at time 20 ms

Open in playground

Simulated services

A simulated data service stands in for a data service under a name you choose: it answers a sum, a score or a formula, optionally with a delay and a failure rate. In the playground a new one answers the sum of the coordinates it is asked at after 5 ms and never fails; in deploy.json its latencyMs must be given (0 answers at once), and it never fails unless failRate is set. Its settings:

AnswersThe sum, mean or largest of the numbers among the coordinates it is asked at (the mean or largest of no numbers fails invalid_input); a score, a whole number from 0 to 100 drawn from them; or a formula over them, x1, x2, … in the order of the input's dimensions, with + - * /, parentheses, abs, min and max (each must be a number, else invalid_input).
TimingA fixed time, or a time drawn between two values for each attempt.
Error codesThe code you choose, for the share of attempts you choose (0% to 50%); invalid_input as above.
Same question, same answerIts times, failures and scores are drawn from the coordinates asked and the attempt, so the same program gives the same questions and answers, in the playground and in the elucid8 command. A retry may succeed where the first attempt failed.
Can be retriedYes.
Available inThe playground: on an input's card in the Data In/Out tab, with Takes and Deadline on it, and what it answers, how much its time varies, how often it fails, its code, retries and reuse folded under More settings. The elucid8 command: { "builtin": "simulated", "answer": "sum", "latencyMs": 5 } (command reference).

Settings belong to an input, so two deadlines for the same kind of data service are two inputs. Here quick_sum, answered by a simulated program that adds in 30 ms, has a deadline of only 20 ms, while sum_service has 40:

-- Two inputs with different deadlines.
x = input("x", time : 3)
sum_service = input("sum_service", n : inf)
score = sum_service[n = x]
quick_sum = input("quick_sum", n : inf)
hurried = quick_sum[n = x]
-- data x = 10 * #time
-- latency 30
-- service sum_service: deadline 40 ms
-- service quick_sum: simulated sum; takes 30 ms; deadline 20 ms
-- expect score[time = 2] = 20, at time 30 ms
-- expect hurried[time = 2] = error late from quick_sum[n = 20]: late (30 ms > deadline 20 ms), at time 20 ms

Open in playground

Two examples from the playground. The simplest data service answers with what it is asked: echo, a simulated data service whose formula is x1, the coordinate it is asked at. It takes 5 ms and has a 20 ms deadline; made to take 30 ms, its answer is late. This is the playground's example Calling a service: an echo:

-- echo: what comes in goes out.
reading = input("reading", t : 4)
echo = input("echo", n : inf)
answer = echo[n = reading]
slow_echo = input("slow_echo", n : inf)
slow = slow_echo[n = reading]
-- data reading = 10 * #t
-- service echo: answers formula x1; takes 5 ms; deadline 20 ms
-- service slow_echo: answers formula x1; takes 30 ms; deadline 20 ms
-- expect answer[t = 2] = 20, at time 5 ms
-- expect slow[t = 2] = error late from slow_echo[n = 20]: late (30 ms > deadline 20 ms), at time 20 ms

Open in playground

When an answer must come in time, the program says what to do without it. In A fraud check that can't wait, a model scores each payment for risk and must answer within 200 ms; when it is late or fails, a rule decides. iserror tests for the error, so approve is a value either way, and risk still shows the error:

-- A fraud check with a fallback rule.
payments = input("payments", t : 8)
fraud_model = input("fraud_model", amount : inf)
risk = fraud_model[amount = payments]
approve = if iserror(risk) then payments < 500 else risk < 70
-- data payments = if #t == 1 then 1250 else if #t == 6 then 310 else 42
-- service fraud_model: answers score; takes 50 to 300 ms; fails 15% with overloaded; seed 3816; deadline 200 ms
-- expect risk[t = 0] = 28, at time 198 ms
-- expect risk[t = 1] = error late from fraud_model[amount = 1250]: late (257 ms > deadline 200 ms), at time 200 ms
-- expect approve[t = 1] = false, at time 200 ms
-- expect risk[t = 6] = error failed from fraud_model[amount = 310]: service error: overloaded, at time 125 ms
-- expect approve[t = 6] = true, at time 125 ms

Open in playground

In the playground the answer line says why in plain words ("risk at t = 1 is late: fraud_model answered after 257 ms, past its 200 ms deadline."), and the trace says which branch approve took past the error.

anomaly_detector

A deterministic detector (deterministicDetector in packages/evaluator/demo/workload.ts), with timings and failures that depend on the point, so that some answers are late and some need a retry.

InputsThe input it answers is over time, x, y and two more dimensions, a reading and its neighbourhood mean, in that order, which are the question's inputs ("arguments" in deploy.json). Nothing is converted: a text is not a number.
OutputA whole number: |reading − mean|, rounded.
Error codesinvalid_input: the question's inputs are not exactly two numbers, or the input is not over time, x and y. It fails as the attempt starts, on every attempt, so a retry never helps. overloaded: on the first attempt only, at points where x + y + time is a multiple of 11. It fails 3 ms after the attempt starts, and a retry succeeds.
Timing4 + ((7x + 13y + 5·time) mod 23) ms after each attempt starts, so 4–26 ms. The delay set for sum_service does not apply.
Can be retriedYes.
Available inThe elucid8 command, an input's provider in a deployment's configuration, { "builtin": "anomaly_detector" } (command reference). The playground does not supply it: an input named anomaly_detector there needs data or a simulated program on its card.
-- anomaly_detector: a hot spot, a retry and a late answer.
field = input("field", time : 4, x : 16, y : 16)
smooth = mean field over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end
anomaly_detector = input("anomaly_detector", time : inf, x : inf, y : inf, reading : inf, mean : inf)
score = anomaly_detector[reading = field, mean = smooth]
-- data field = if #x == 5 and #y == 5 then 50 else 10
-- service anomaly_detector: arguments reading, mean; deadline 20 ms; retry overloaded up to 2
-- expect score[time = 1, x = 5, y = 6] = 4, at time 7 ms
-- expect score[time = 1, x = 5, y = 5] = 36, at time 20 ms
-- expect score[time = 2, x = 4, y = 4] = error late from anomaly_detector[time = 2, x = 4, y = 4, reading = 10, mean = 14.444444444444445]: late (25 ms > deadline 20 ms), at time 20 ms

How to read it:

Without the retry setting, the failure is the result:

-- anomaly_detector: a failure without a retry.
field = input("field", time : 4, x : 16, y : 16)
smooth = mean field over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end
anomaly_detector = input("anomaly_detector", time : inf, x : inf, y : inf, reading : inf, mean : inf)
score = anomaly_detector[reading = field, mean = smooth]
-- data field = if #x == 5 and #y == 5 then 50 else 10
-- service anomaly_detector: arguments reading, mean; deadline 20 ms
-- expect score[time = 1, x = 5, y = 5] = error failed from anomaly_detector[time = 1, x = 5, y = 5, reading = 50, mean = 14.444444444444445]: service error: overloaded, at time 3 ms

The detector is strict about its inputs: anything other than two numbers fails invalid_input. Here single is answered by the detector with one input only, and row by one over no y:

-- anomaly_detector: inputs that are not two numbers.
field = input("field", time : 2, x : 4, y : 4)
single_detector = input("single_detector", time : inf, x : inf, y : inf, reading : inf)
single = single_detector[reading = field]
row_detector = input("row_detector", time : inf, x : inf, reading : inf, mean : inf)
row = row_detector[reading = field[y = 0], mean = 1]
-- data field = 10
-- service single_detector: answered by anomaly_detector; arguments reading; deadline 20 ms
-- service row_detector: answered by anomaly_detector; arguments reading, mean; deadline 20 ms
-- expect single[time = 1, x = 2, y = 3] = error failed from single_detector[time = 1, x = 2, y = 3, reading = 10]: service error: invalid_input
-- expect row[time = 1, x = 2] = error failed from row_detector[time = 1, x = 2, reading = 10, mean = 1]: service error: invalid_input

row_detector has no y, so the detector fails it too, whatever its inputs.

Outside the playground

The Rust runtime (the elucid8 command) has both data services above built in, with the answers, timings and failures described here, and asks any other program through a small out-of-process protocol: service-protocol.md.