registry/README.md

177 lines
7.7 KiB
Markdown
Raw Normal View History

2022-02-24 16:37:58 +03:00
# Registry
2021-05-28 12:51:21 +03:00
2022-07-20 19:21:05 +04:00
- [Registry](#registry)
- [Overview](#overview)
- [Why is it important?](#why-is-it-important)
- [What is it?](#what-is-it)
- [How to Use it in Aqua](#how-to-use-it-in-aqua)
- [How to import](#how-to-import)
- [How to create Resource](#how-to-create-resource)
- [How to register a service](#how-to-register-a-service)
- [How to unregister a service](#how-to-unregister-a-service)
- [How to resolve service records](#how-to-resolve-service-records)
- [How to execute a callback on Resource](#how-to-execute-a-callback-on-resource)
2022-07-20 19:21:05 +04:00
- [Notes](#notes)
- [Use cases](#use-cases)
- [Services discovery](#services-discovery)
- [Service high-availability](#service-high-availability)
- [Subnetwork discovery](#subnetwork-discovery)
- [Load balancer](#load-balancer)
- [API](#api)
- [References](#references)
- [Learn Aqua](#learn-aqua)
2021-07-13 11:02:46 -05:00
2022-07-20 19:21:05 +04:00
## Overview
2021-07-30 14:17:52 +03:00
2022-08-02 10:53:41 +02:00
There are many [services](https://doc.fluence.dev/docs/concepts#services) in the network on different peers, and there should be a way to find and resolve these services in runtime without prior knowledge about exact service providers. Such approach gives robustness and flexibility to our solutions in terms of discovery, redundancy and high availability.
In centralized systems, we can have centralized storage and routing, but in p2p decentralized environments, the problem becomes more challenging. Registry is our view on the solution for the problem.
2021-06-02 21:02:04 +03:00
2022-07-20 19:21:05 +04:00
![image](images/registry.png)
## Why is it important?
2022-08-02 10:53:41 +02:00
Scalability, redundancy and high availability are essential parts of a decentralized system, but they are not available out of the box. To enable them, information about services should be bound with peers providing them. Also, such networks are constantly changing, and those changes should be reflected and resolvable in runtime to provide uninterruptible access. So there's a need to have a decentralized protocol to update and resolve information about routing, both global and local.
2022-07-20 19:21:05 +04:00
## What is it?
2022-08-02 10:53:41 +02:00
Registry is available (built-in) on every Fluence node, and it provides service advertisement and discovery. The component allows of creating relationships between unique identifiers and groups of services on various peers. So service providers can either join or disconnect during runtime and be discoverable on the network.
2022-07-20 19:21:05 +04:00
However, Registry is not a plain KV-storage. Instead, it is a composition of the Registry service for each network participant and the scheduled scripts maintaining replication and garbage collection.
2022-08-02 10:53:41 +02:00
So, if you want to discover a group of services on different peers without prior knowledge in runtime, you should register a **Resource**. A resource is a group of services or peers united by some common feature. Please notice that resource lifetime is ~24 hours. However, if the resource has been accessed recently, its lifetime is prolonged, and it will not be garbage-collected for the next 24 hours from the last access.
2022-07-20 19:21:05 +04:00
Service is represented by a combination of `service_id` and `peer_id`, it is called **Record**.
2022-07-20 19:21:05 +04:00
![image](images/discovery.png)
![image](images/mapping.png)
2022-08-02 10:53:41 +02:00
There is no permissions management at the moment, but in the coming updates, a resource owner will be able to provide a challenge to check against.
2022-07-20 19:21:05 +04:00
## How to Use it in Aqua
### How to import
```rust
import "@fluencelabs/registry/resources-api.aqua"
import "@fluencelabs/registry/registry-service.aqua"
func my_function(resource_id: string) -> []Record, *Error:
result, error <- resolveResource(resource_id, 2)
2022-07-20 19:21:05 +04:00
<- result, error
```
### How to create Resource
- `createResource(label: string) -> ?ResourceId, *Error`
Let's register a resource with the label `sample` by `INIT_PEER_ID`:
```rust
func my_resource() -> ?ResourceId, *Error:
id, error <- createResource("sample")
<- id, error
```
2022-08-02 10:53:41 +02:00
- `label` is a unique string for the peer id
- creation is successful if a resource id is returned
- `*Error` accumulates errors from all the affected peers
### How to register a service
```
2022-10-03 18:30:33 +03:00
registerServiceRecord(resource_id: ResourceId, value: string, peer_id: string service_id: ?string) -> bool, *Error
```
2022-07-20 19:21:05 +04:00
2022-08-02 10:53:41 +02:00
Let's register a local service `greeting` and pass a random string `hi` as a value:
2022-07-20 19:21:05 +04:00
```rust
func register_local_service(resource_id: string) -> ?bool, *Error:
2022-10-03 18:30:33 +03:00
success, error <- registerServiceRecord(resource_id, "hi", INIT_PEER_ID, ?[greeting])
2022-07-20 19:21:05 +04:00
<- success, error
```
2022-08-02 10:53:41 +02:00
Let's register a service `echo` hosted on `peer_id` and pass a random string like `sample` as a value:
2022-07-20 19:21:05 +04:00
```rust
func register_external_service(resource_id: string, peer_id: string) -> ?bool, *Error:
2022-10-03 18:30:33 +03:00
success, error <- registerServiceRecord(resource_id, "hi", peer_id, ?[greeting])
2022-07-20 19:21:05 +04:00
<- success, error
```
- `value` is a user-defined string that can be used at the discretion of the user
- to update the service record, you should register it again to create a record with a newer timestamp
- service record will be automatically updated till deleted via `unregisterService`
2022-07-20 19:21:05 +04:00
### How to unregister a service
```
func unregisterService(resource_id: ResourceId, peer_id: PeerId) -> bool, *Error:
```
Let's remove a service record from a target node:
2022-07-20 19:21:05 +04:00
```rust
func stop_provide_external_service(resource_id: string, peer_id: string):
unregisterService(resource_id, peer_id)
2022-07-20 19:21:05 +04:00
```
- it will be removed from the target node and eventually from the network
2022-08-02 10:53:41 +02:00
### How to resolve service records
- `resolveResource(resource_id: ResourceId, ack: i16) -> []Record, *Error`
2022-07-20 19:21:05 +04:00
Let's resolve all service records for our resource_id:
2022-07-20 19:21:05 +04:00
```rust
func get_my_records(resource_id: string, consistency_level: i16) -> []Record, *Error:
records, error <- resolveResource(resource_id, consistency_level)
<- records, error
2022-07-20 19:21:05 +04:00
```
- `ack` represents a minimal number of peers that requested for known records
2022-07-20 19:21:05 +04:00
### How to execute a callback on Resource
- `executeOnResource(resource_id: ResourceId, ack: i16, call: Record -> ()) -> *Error`
2022-07-20 19:21:05 +04:00
```rust
func call_provider(p: Record):
-- topological move to a provider via relay
on p.peer_id via p.relay_id:
-- resolve and call your service on a provider
...
Op.noop()
-- call on every provider
func call_everyone(resource_id: String, ack: i16):
executeOnResource(resource_id, ack, call_provider)
2022-07-20 19:21:05 +04:00
```
- it is a combination of `resolveResource` and a `for` loop through records with the callback execution
2022-07-20 19:21:05 +04:00
- it can be useful in case of broadcasting events on providers
2022-08-02 10:53:41 +02:00
For more detailed example please take a look in the [docs](https://doc.fluence.dev/aqua-book/libraries/registry#call-a-function-on-resource-providers)
2022-07-20 19:21:05 +04:00
### Notes
You can redefine [`REPLICATION_FACTOR`](https://github.com/fluencelabs/registry/blob/main/aqua/resources-api.aqua#L10) and [`CONSISTENCY_LEVEL`](https://github.com/fluencelabs/registry/blob/main/aqua/resources-api.aqua#L11).
## Use cases
2022-08-02 10:53:41 +02:00
2022-07-20 19:21:05 +04:00
### Services discovery
Discover services without prior knowledge about exact peers and service identifiers.
### Service high-availability
2022-08-02 10:53:41 +02:00
A service provided by several peers still will be available for the client in case of disconnections and other providers' failures.
2022-07-20 19:21:05 +04:00
![image](images/availability.png)
### Subnetwork discovery
2022-08-02 10:53:41 +02:00
You can register a group of peers for a resource (without specifying any services). So you "tag" and group the nodes to create a subnetwork.
2022-07-20 19:21:05 +04:00
![image](images/subnetwork.png)
### Load balancer
If you have a list of service records updated in runtime, you can create a load-balancing service based on your preferred metrics.
2021-06-02 21:02:04 +03:00
2021-07-28 13:53:38 +03:00
## API
2021-06-02 21:02:04 +03:00
2022-08-02 10:53:41 +02:00
API is defined in the [resources-api.aqua](./aqua/resources-api.aqua) module. API Reference will be available in the documentation soon.
2022-07-20 19:21:05 +04:00
### Learn Aqua
2021-07-30 14:16:38 +03:00
2022-10-11 16:42:00 +03:00
* [Aqua Book](https://fluence.dev/docs/aqua-book/getting-started/)
2021-07-30 14:16:38 +03:00
* [Aqua Playground](https://github.com/fluencelabs/aqua-playground)
* [Aqua repo](https://github.com/fluencelabs/aqua)