-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add P2P collection topic subscription (#1086)
Relevant issue(s) Resolves #1081 Description This PR adds the option to subscribe to events on a given collection by using the collectionID (in reality schemaID for now as there is no globalSchemaID or anything similar) as the pubsub topic. It adds commands to the CLI to add, remove and list the P2P collection topics.
- Loading branch information
1 parent
deb65e7
commit ce5a429
Showing
25 changed files
with
2,682 additions
and
443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2022 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var p2pCollectionCmd = &cobra.Command{ | ||
Use: "p2pcollection", | ||
Short: "Interact with the P2P collection system", | ||
Long: "Add, delete, or get the list of P2P collections", | ||
} | ||
|
||
func init() { | ||
rpcCmd.AddCommand(p2pCollectionCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2022 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
|
||
"github.com/sourcenetwork/defradb/errors" | ||
"github.com/sourcenetwork/defradb/logging" | ||
netclient "github.com/sourcenetwork/defradb/net/api/client" | ||
) | ||
|
||
var addP2PCollectionCmd = &cobra.Command{ | ||
Use: "add [collectionID]", | ||
Short: "Add P2P collections", | ||
Long: `Use this command if you wish to add new P2P collections to the pubsub topics`, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil { | ||
return errors.New("must specify at least one collectionID") | ||
} | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cred := insecure.NewCredentials() | ||
client, err := netclient.NewClient(cfg.Net.RPCAddress, grpc.WithTransportCredentials(cred)) | ||
if err != nil { | ||
return errors.Wrap("failed to create RPC client", err) | ||
} | ||
|
||
rpcTimeoutDuration, err := cfg.Net.RPCTimeoutDuration() | ||
if err != nil { | ||
return errors.Wrap("failed to parse RPC timeout duration", err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(cmd.Context(), rpcTimeoutDuration) | ||
defer cancel() | ||
|
||
err = client.AddP2PCollections(ctx, args...) | ||
if err != nil { | ||
return errors.Wrap("failed to add p2p collections, request failed", err) | ||
} | ||
log.FeedbackInfo(ctx, "Successfully added p2p collections", logging.NewKV("Collections", args)) | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
replicatorCmd.AddCommand(addP2PCollectionCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2022 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
|
||
"github.com/sourcenetwork/defradb/errors" | ||
"github.com/sourcenetwork/defradb/logging" | ||
netclient "github.com/sourcenetwork/defradb/net/api/client" | ||
) | ||
|
||
var getAllP2PCollectionCmd = &cobra.Command{ | ||
Use: "getall", | ||
Short: "Get all P2P collections", | ||
Long: `Use this command if you wish to get all P2P collections in the pubsub topics`, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if err := cobra.NoArgs(cmd, args); err != nil { | ||
return errors.New("must specify no argument") | ||
} | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cred := insecure.NewCredentials() | ||
client, err := netclient.NewClient(cfg.Net.RPCAddress, grpc.WithTransportCredentials(cred)) | ||
if err != nil { | ||
return errors.Wrap("failed to create RPC client", err) | ||
} | ||
|
||
rpcTimeoutDuration, err := cfg.Net.RPCTimeoutDuration() | ||
if err != nil { | ||
return errors.Wrap("failed to parse RPC timeout duration", err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(cmd.Context(), rpcTimeoutDuration) | ||
defer cancel() | ||
|
||
collections, err := client.GetAllP2PCollections(ctx) | ||
if err != nil { | ||
return errors.Wrap("failed to add p2p collections, request failed", err) | ||
} | ||
|
||
if len(collections) > 0 { | ||
log.FeedbackInfo(ctx, "Successfully got all P2P collections") | ||
for _, col := range collections { | ||
log.FeedbackInfo(ctx, col.Name, logging.NewKV("CollectionID", col.ID)) | ||
} | ||
} else { | ||
log.FeedbackInfo(ctx, "No P2P collection found") | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
replicatorCmd.AddCommand(getAllP2PCollectionCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2022 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
|
||
"github.com/sourcenetwork/defradb/errors" | ||
"github.com/sourcenetwork/defradb/logging" | ||
netclient "github.com/sourcenetwork/defradb/net/api/client" | ||
) | ||
|
||
var removeP2PCollectionCmd = &cobra.Command{ | ||
Use: "remove [collectionID]", | ||
Short: "Add P2P collections", | ||
Long: `Use this command if you wish to remove P2P collections from the pubsub topics`, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil { | ||
return errors.New("must specify at least one collectionID") | ||
} | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cred := insecure.NewCredentials() | ||
client, err := netclient.NewClient(cfg.Net.RPCAddress, grpc.WithTransportCredentials(cred)) | ||
if err != nil { | ||
return errors.Wrap("failed to create RPC client", err) | ||
} | ||
|
||
rpcTimeoutDuration, err := cfg.Net.RPCTimeoutDuration() | ||
if err != nil { | ||
return errors.Wrap("failed to parse RPC timeout duration", err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(cmd.Context(), rpcTimeoutDuration) | ||
defer cancel() | ||
|
||
err = client.RemoveP2PCollections(ctx, args...) | ||
if err != nil { | ||
return errors.Wrap("failed to remove p2p collections, request failed", err) | ||
} | ||
log.FeedbackInfo(ctx, "Successfully removed p2p collections", logging.NewKV("Collections", args)) | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
replicatorCmd.AddCommand(removeP2PCollectionCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2022 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package client | ||
|
||
import "context" | ||
|
||
type P2P interface { | ||
// SetReplicator adds a replicator to the persisted list or adds | ||
// schemas if the replicator already exists. | ||
SetReplicator(ctx context.Context, rep Replicator) error | ||
// DeleteReplicator deletes a replicator from the persisted list | ||
// or specific schemas if they are specified. | ||
DeleteReplicator(ctx context.Context, rep Replicator) error | ||
// GetAllReplicators returns the full list of replicators with their | ||
// subscribed schemas. | ||
GetAllReplicators(ctx context.Context) ([]Replicator, error) | ||
|
||
// AddP2PCollection adds the given collection ID that the P2P system | ||
// subscribes to to the the persisted list. It will error if the provided | ||
// collection ID is invalid. | ||
AddP2PCollection(ctx context.Context, collectionID string) error | ||
// RemoveP2PCollection removes the given collection ID that the P2P system | ||
// subscribes to from the the persisted list. It will error if the provided | ||
// collection ID is invalid. | ||
RemoveP2PCollection(ctx context.Context, collectionID string) error | ||
// GetAllP2PCollections returns the list of persisted collection IDs that | ||
// the P2P system subscribes to. | ||
GetAllP2PCollections(ctx context.Context) ([]string, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.