-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: P2P client interface #1924
Changes from 27 commits
022af8f
a49626a
346fbce
55e2f21
1c3f4e1
175c5a8
292150c
13f2fff
1cfdda3
c55ea7e
1ec5f38
e5b7b53
eb04aed
ab11b65
78ac2cb
f3887a4
5c2d60b
2a01990
7e6bb4f
94a7b8b
592e411
687f0e2
fa6b67e
8cca432
4be14e7
edb9249
b907f30
25139aa
580f016
4532bfd
6ec4577
76c9b5f
03d2a6e
a96746c
b967436
abf2af3
40b2603
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,57 +244,73 @@ When starting a node for the first time, a key pair is generated and stored in i | |
|
||
Each node has a unique `PeerID` generated from its public key. This ID allows other nodes to connect to it. | ||
|
||
To view your node's peer info: | ||
|
||
```shell | ||
defradb client p2p info | ||
``` | ||
|
||
There are two types of peer-to-peer relationships supported: **pubsub** peering and **replicator** peering. | ||
|
||
Pubsub peering *passively* synchronizes data between nodes by broadcasting *Document Commit* updates to the topic of the commit's document key. Nodes need to be listening on the pubsub channel to receive updates. This is for when two nodes *already* have share a document and want to keep them in sync. | ||
|
||
Replicator peering *actively* pushes changes from a specific collection *to* a target peer. | ||
|
||
### Pubsub example | ||
<details> | ||
<summary>Pubsub example</summary> | ||
|
||
Pubsub peers can be specified on the command line using the `--peers` flag, which accepts a comma-separated list of peer [multiaddresses](https://docs.libp2p.io/concepts/addressing/). For example, a node at IP `192.168.1.12` listening on 9000 with PeerID `12D3KooWNXm3dmrwCYSxGoRUyZstaKYiHPdt8uZH5vgVaEJyzU8B` would be referred to using the multiaddress `/ip4/192.168.1.12/tcp/9000/p2p/12D3KooWNXm3dmrwCYSxGoRUyZstaKYiHPdt8uZH5vgVaEJyzU8B`. | ||
|
||
Let's go through an example of two nodes (*nodeA* and *nodeB*) connecting with each other over pubsub, on the same machine. | ||
|
||
Start *nodeA* with a default configuration: | ||
|
||
``` | ||
```shell | ||
defradb start | ||
``` | ||
|
||
Obtain the PeerID from its console output. In this example, we use `12D3KooWNXm3dmrwCYSxGoRUyZstaKYiHPdt8uZH5vgVaEJyzU8B`, but locally it will be different. | ||
Obtain the node's peer info: | ||
|
||
```shell | ||
defradb client p2p info | ||
``` | ||
|
||
In this example, we use `12D3KooWNXm3dmrwCYSxGoRUyZstaKYiHPdt8uZH5vgVaEJyzU8B`, but locally it will be different. | ||
|
||
For *nodeB*, we provide the following configuration: | ||
|
||
``` | ||
defradb start --rootdir ~/.defradb-nodeB --url localhost:9182 --p2paddr /ip4/0.0.0.0/tcp/9172 --tcpaddr /ip4/0.0.0.0/tcp/9162 --peers /ip4/0.0.0.0/tcp/9171/p2p/12D3KooWNXm3dmrwCYSxGoRUyZstaKYiHPdt8uZH5vgVaEJyzU8B | ||
```shell | ||
defradb start --rootdir ~/.defradb-nodeB --url localhost:9182 --p2paddr /ip4/0.0.0.0/tcp/9172 --peers /ip4/0.0.0.0/tcp/9171/p2p/12D3KooWNXm3dmrwCYSxGoRUyZstaKYiHPdt8uZH5vgVaEJyzU8B | ||
``` | ||
|
||
About the flags: | ||
|
||
- `--rootdir` specifies the root dir (config and data) to use | ||
- `--url` is the address to listen on for the client HTTP and GraphQL API | ||
- `--p2paddr` is the multiaddress for the P2P networking to listen on | ||
- `--tcpaddr` is the multiaddress for the gRPC server to listen on | ||
- `--peers` is a comma-separated list of peer multiaddresses | ||
|
||
This starts two nodes and connects them via pubsub networking. | ||
</details> | ||
|
||
### Collection subscription example | ||
<details> | ||
<summary>Subscription example</summary> | ||
|
||
It is possible to subscribe to updates on a given collection by using its ID as the pubsub topic. The ID of a collection is found as the field `collectionID` in one of its documents. Here we use the collection ID of the `User` type we created above. After setting up 2 nodes as shown in the [Pubsub example](#pubsub-example) section, we can subscribe to collections updates on *nodeA* from *nodeB* by using the `rpc p2pcollection` command: | ||
It is possible to subscribe to updates on a given collection by using its ID as the pubsub topic. The ID of a collection is found as the field `collectionID` in one of its documents. Here we use the collection ID of the `User` type we created above. After setting up 2 nodes as shown in the [Pubsub example](#pubsub-example) section, we can subscribe to collections updates on *nodeA* from *nodeB* by using the following command: | ||
|
||
```shell | ||
defradb client rpc p2pcollection add --url localhost:9182 bafkreibpnvkvjqvg4skzlijka5xe63zeu74ivcjwd76q7yi65jdhwqhske | ||
defradb client p2p collection add --url localhost:9182 bafkreibpnvkvjqvg4skzlijka5xe63zeu74ivcjwd76q7yi65jdhwqhske | ||
``` | ||
|
||
Multiple collection IDs can be added at once. | ||
|
||
```shell | ||
defradb client rpc p2pcollection add --url localhost:9182 <collection1ID> <collection2ID> <collection3ID> | ||
defradb client p2p collection add --url localhost:9182 <collection1ID>,<collection2ID>,<collection3ID> | ||
``` | ||
</details> | ||
|
||
### Replicator example | ||
<details> | ||
<summary>Replicator example</summary> | ||
|
||
Replicator peering is targeted: it allows a node to actively send updates to another node. Let's go through an example of *nodeA* actively replicating to *nodeB*: | ||
|
||
|
@@ -334,14 +350,20 @@ defradb client schema add --url localhost:9182 ' | |
' | ||
``` | ||
|
||
Set *nodeA* to actively replicate the "Article" collection to *nodeB*: | ||
Then copy the peer info from *nodeB*: | ||
|
||
```shell | ||
defradb client rpc replicator set -c "Article" /ip4/0.0.0.0/tcp/9172/p2p/<peerID_of_nodeB> | ||
defradb client p2p info | ||
``` | ||
|
||
As we add or update documents in the "Article" collection on *nodeA*, they will be actively pushed to *nodeB*. Note that changes to *nodeB* will still be passively published back to *nodeA*, via pubsub. | ||
Set *nodeA* to actively replicate the Article collection to *nodeB*: | ||
|
||
```shell | ||
defradb client p2p replicator set -c Article <peer_info_of_nodeB> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: I had to check the help text of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated it to |
||
``` | ||
|
||
As we add or update documents in the Article collection on *nodeA*, they will be actively pushed to *nodeB*. Note that changes to *nodeB* will still be passively published back to *nodeA*, via pubsub. | ||
</details> | ||
|
||
## Securing the HTTP API with TLS | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,27 +11,40 @@ | |
package cli | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/sourcenetwork/defradb/client" | ||
) | ||
|
||
func MakeP2PReplicatorDeleteCommand() *cobra.Command { | ||
var collections []string | ||
var cmd = &cobra.Command{ | ||
Use: "delete <peer>", | ||
Use: "delete [-c, --collection] <peer>", | ||
Short: "Delete a replicator. It will stop synchronizing", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Should this (and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are referring to deleting multiple collections replicated to a single peer you are correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: Please change the short/long wording to reflect this :) |
||
Long: `Delete a replicator. It will stop synchronizing.`, | ||
Args: cobra.ExactArgs(1), | ||
Long: `Delete a replicator. It will stop synchronizing. | ||
|
||
Example: | ||
defradb client p2p replicator delete -c Users '{"ID": "12D3", "Addrs": ["/ip4/0.0.0.0/tcp/9171"]}' | ||
`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
store := mustGetStoreContext(cmd) | ||
p2p := mustGetP2PContext(cmd) | ||
|
||
addr, err := peer.AddrInfoFromString(args[0]) | ||
if err != nil { | ||
var info peer.AddrInfo | ||
if err := json.Unmarshal([]byte(args[0]), &info); err != nil { | ||
return err | ||
} | ||
return store.DeleteReplicator(cmd.Context(), client.Replicator{Info: *addr}) | ||
rep := client.Replicator{ | ||
Info: info, | ||
Schemas: collections, | ||
} | ||
return p2p.DeleteReplicator(cmd.Context(), rep) | ||
}, | ||
} | ||
cmd.Flags().StringSliceVarP(&collections, "collection", "c", | ||
[]string{}, "Collection(s) to stop replicating") | ||
return cmd | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: Thank you for adding this!