| 1 |
--- |
| 2 |
title: "nanonext - Scalability Protocols" |
| 3 |
vignette: > |
| 4 |
%\VignetteIndexEntry{nanonext - Scalability Protocols} |
| 5 |
%\VignetteEngine{litedown::vignette} |
| 6 |
%\VignetteEncoding{UTF-8} |
| 7 |
--- |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
``` r |
| 13 |
library(nanonext) |
| 14 |
``` |
| 15 |
|
| 16 |
### 1. Request Reply Protocol |
| 17 |
|
| 18 |
`nanonext` implements remote procedure calls (RPC) using NNG's req/rep protocol for distributed computing. Use this for computationally-expensive calculations or I/O-bound operations in separate server processes. |
| 19 |
|
| 20 |
**[S] Server process:** `reply()` waits for a message, applies a function, and sends back the result. Started in a background 'mirai' process. |
| 21 |
|
| 22 |
|
| 23 |
``` r |
| 24 |
m <- mirai::mirai({ |
| 25 |
library(nanonext) |
| 26 |
rep <- socket("rep", listen = "tcp://127.0.0.1:6556") |
| 27 |
reply(context(rep), execute = rnorm, send_mode = "raw") |
| 28 |
Sys.sleep(2) # linger period to flush system socket send |
| 29 |
}) |
| 30 |
|
| 31 |
``` |
| 32 |
|
| 33 |
**[C] Client process:** `request()` performs async send/receive, returning immediately with a `recvAio` object. |
| 34 |
|
| 35 |
|
| 36 |
``` r |
| 37 |
req <- socket("req", dial = "tcp://127.0.0.1:6556") |
| 38 |
aio <- request(context(req), data = 1e8, recv_mode = "double") |
| 39 |
|
| 40 |
``` |
| 41 |
The client can now run additional code while the server processes the request. |
| 42 |
|
| 43 |
|
| 44 |
``` r |
| 45 |
# do more... |
| 46 |
``` |
| 47 |
|
| 48 |
When the result is needed, call the recvAio using `call_aio()` to retrieve the value at `$data`. |
| 49 |
|
| 50 |
|
| 51 |
``` r |
| 52 |
call_aio(aio)$data |> str() |
| 53 |
#> num [1:100000000] -0.63 0.883 1.134 -0.474 -0.237 ... |
| 54 |
``` |
| 55 |
|
| 56 |
Since `call_aio()` blocks, alternatively query `aio$data` directly, which returns 'unresolved' (logical NA) if incomplete. |
| 57 |
|
| 58 |
For server-side operations (e.g., writing to disk), calling or querying the value confirms completion and provides the function's return value (typically NULL or an exit code). |
| 59 |
|
| 60 |
The [`mirai`](https://doi.org/10.5281/zenodo.7912722) package (<https://mirai.r-lib.org/>) uses `nanonext` as the back-end to provide asynchronous execution of arbitrary R code using the RPC model. |
| 61 |
|
| 62 |
### 2. Publisher Subscriber Protocol |
| 63 |
|
| 64 |
`nanonext` implements NNG's pub/sub protocol. Subscribers can subscribe to one or multiple topics broadcast by a publisher. |
| 65 |
|
| 66 |
|
| 67 |
``` r |
| 68 |
pub <- socket("pub", listen = "inproc://nanobroadcast") |
| 69 |
sub <- socket("sub", dial = "inproc://nanobroadcast") |
| 70 |
|
| 71 |
sub |> subscribe(topic = "examples") |
| 72 |
|
| 73 |
pub |> send(c("examples", "this is an example"), mode = "raw") |
| 74 |
#> [1] 0 |
| 75 |
sub |> recv(mode = "character") |
| 76 |
#> [1] "examples" "this is an example" |
| 77 |
|
| 78 |
pub |> send("examples at the start of a single text message", mode = "raw") |
| 79 |
#> [1] 0 |
| 80 |
sub |> recv(mode = "character") |
| 81 |
#> [1] "examples at the start of a single text message" |
| 82 |
|
| 83 |
pub |> send(c("other", "this other topic will not be received"), mode = "raw") |
| 84 |
#> [1] 0 |
| 85 |
sub |> recv(mode = "character") |
| 86 |
#> 'errorValue' int 8 | Try again |
| 87 |
|
| 88 |
# specify NULL to subscribe to ALL topics |
| 89 |
sub |> subscribe(topic = NULL) |
| 90 |
pub |> send(c("newTopic", "this is a new topic"), mode = "raw") |
| 91 |
#> [1] 0 |
| 92 |
sub |> recv("character") |
| 93 |
#> [1] "newTopic" "this is a new topic" |
| 94 |
|
| 95 |
sub |> unsubscribe(topic = NULL) |
| 96 |
pub |> send(c("newTopic", "this topic will now not be received"), mode = "raw") |
| 97 |
#> [1] 0 |
| 98 |
sub |> recv("character") |
| 99 |
#> 'errorValue' int 8 | Try again |
| 100 |
|
| 101 |
# however the topics explicitly subscribed to are still received |
| 102 |
pub |> send(c("examples will still be received"), mode = "raw") |
| 103 |
#> [1] 0 |
| 104 |
sub |> recv(mode = "character") |
| 105 |
#> [1] "examples will still be received" |
| 106 |
``` |
| 107 |
|
| 108 |
The subscribed topic can be of any atomic type (not just character), allowing integer, double, logical, complex and raw vectors to be sent and received. |
| 109 |
|
| 110 |
|
| 111 |
``` r |
| 112 |
sub |> subscribe(topic = 1) |
| 113 |
pub |> send(c(1, 10, 10, 20), mode = "raw") |
| 114 |
#> [1] 0 |
| 115 |
sub |> recv(mode = "double") |
| 116 |
#> [1] 1 10 10 20 |
| 117 |
pub |> send(c(2, 10, 10, 20), mode = "raw") |
| 118 |
#> [1] 0 |
| 119 |
sub |> recv(mode = "double") |
| 120 |
#> 'errorValue' int 8 | Try again |
| 121 |
|
| 122 |
close(pub) |
| 123 |
close(sub) |
| 124 |
|
| 125 |
``` |
| 126 |
|
| 127 |
### 3. Surveyor Respondent Protocol |
| 128 |
|
| 129 |
Useful for service discovery and similar applications. A surveyor broadcasts a survey to all respondents, who may reply within a timeout period. Late responses are discarded. |
| 130 |
|
| 131 |
|
| 132 |
``` r |
| 133 |
sur <- socket("surveyor", listen = "inproc://nanoservice") |
| 134 |
res1 <- socket("respondent", dial = "inproc://nanoservice") |
| 135 |
res2 <- socket("respondent", dial = "inproc://nanoservice") |
| 136 |
|
| 137 |
# sur sets a survey timeout, applying to this and subsequent surveys |
| 138 |
sur |> survey_time(value = 500) |
| 139 |
|
| 140 |
# sur sends a message and then requests 2 async receives |
| 141 |
sur |> send("service check") |
| 142 |
#> [1] 0 |
| 143 |
aio1 <- sur |> recv_aio() |
| 144 |
aio2 <- sur |> recv_aio() |
| 145 |
|
| 146 |
# res1 receives the message and replies using an aio send function |
| 147 |
res1 |> recv() |
| 148 |
#> [1] "service check" |
| 149 |
res1 |> send_aio("res1") |
| 150 |
|
| 151 |
# res2 receives the message but fails to reply |
| 152 |
res2 |> recv() |
| 153 |
#> [1] "service check" |
| 154 |
|
| 155 |
# checking the aio - only the first will have resolved |
| 156 |
aio1$data |
| 157 |
#> [1] "res1" |
| 158 |
aio2$data |
| 159 |
#> 'unresolved' logi NA |
| 160 |
|
| 161 |
# after the survey expires, the second resolves into a timeout error |
| 162 |
msleep(500) |
| 163 |
aio2$data |
| 164 |
#> 'errorValue' int 5 | Timed out |
| 165 |
|
| 166 |
close(sur) |
| 167 |
close(res1) |
| 168 |
close(res2) |
| 169 |
|
| 170 |
``` |
| 171 |
|
| 172 |
`msleep()` is an uninterruptible sleep function (using NNG) that takes a time in milliseconds. |
| 173 |
|
| 174 |
The final value resolves to a timeout error (integer 5 classed as 'errorValue'). All error codes are classed as 'errorValue' for easy distinction from integer message values. |