-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
syncv2: pairwise set reconciliation (#6350)
## Motivation Existing sync is causing a lot of resource usage, including memory consumption and network traffic, and can't replace gossip which can also be unreliable at times.
- Loading branch information
Showing
34 changed files
with
5,364 additions
and
20 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,69 @@ | ||
package rangesync | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"sync" | ||
|
||
"github.com/libp2p/go-libp2p/core/host" | ||
"go.uber.org/zap" | ||
|
||
"github.com/spacemeshos/go-spacemesh/p2p/server" | ||
) | ||
|
||
// Handler is a function that handles a request for a Dispatcher. | ||
type Handler func(ctx context.Context, s io.ReadWriter) error | ||
|
||
// Dispatcher multiplexes a P2P Server to multiple set reconcilers. | ||
type Dispatcher struct { | ||
*server.Server | ||
mtx sync.Mutex | ||
logger *zap.Logger | ||
handlers map[string]Handler | ||
} | ||
|
||
// NewDispatcher creates a new Dispatcher. | ||
func NewDispatcher(logger *zap.Logger) *Dispatcher { | ||
return &Dispatcher{ | ||
logger: logger, | ||
handlers: make(map[string]Handler), | ||
} | ||
} | ||
|
||
// SetupServer creates a new P2P Server for the Dispatcher. | ||
func (d *Dispatcher) SetupServer( | ||
host host.Host, | ||
proto string, | ||
opts ...server.Opt, | ||
) *server.Server { | ||
d.Server = server.New(host, proto, d.Dispatch, opts...) | ||
return d.Server | ||
} | ||
|
||
// Register registers a handler with a Dispatcher. | ||
func (d *Dispatcher) Register(name string, h Handler) { | ||
d.mtx.Lock() | ||
defer d.mtx.Unlock() | ||
d.handlers[name] = h | ||
} | ||
|
||
// Dispatch dispatches a request to a handler. | ||
func (d *Dispatcher) Dispatch( | ||
ctx context.Context, | ||
req []byte, | ||
stream io.ReadWriter, | ||
) (err error) { | ||
name := string(req) | ||
d.mtx.Lock() | ||
h, ok := d.handlers[name] | ||
d.mtx.Unlock() | ||
if !ok { | ||
return fmt.Errorf("no handler named %q", name) | ||
} | ||
d.logger.Debug("dispatch", zap.String("handler", name)) | ||
if err := h(ctx, stream); err != nil { | ||
return fmt.Errorf("handler %q: %w", name, err) | ||
} | ||
return nil | ||
} |
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,78 @@ | ||
package rangesync_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"testing" | ||
"time" | ||
|
||
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zaptest" | ||
|
||
"github.com/spacemeshos/go-spacemesh/p2p/server" | ||
"github.com/spacemeshos/go-spacemesh/sync2/rangesync" | ||
) | ||
|
||
func makeFakeDispHandler(n int) rangesync.Handler { | ||
return func(ctx context.Context, stream io.ReadWriter) error { | ||
x := rangesync.KeyBytes(bytes.Repeat([]byte{byte(n)}, 32)) | ||
c := rangesync.StartWireConduit(ctx, stream) | ||
defer c.End() | ||
s := rangesync.Sender{c} | ||
s.SendRangeContents(x, x, n) | ||
s.SendEndRound() | ||
return nil | ||
} | ||
} | ||
|
||
func TestDispatcher(t *testing.T) { | ||
mesh, err := mocknet.FullMeshConnected(2) | ||
require.NoError(t, err) | ||
|
||
d := rangesync.NewDispatcher(zaptest.NewLogger(t)) | ||
d.Register("a", makeFakeDispHandler(42)) | ||
d.Register("b", makeFakeDispHandler(43)) | ||
d.Register("c", makeFakeDispHandler(44)) | ||
|
||
proto := "itest" | ||
opts := []server.Opt{ | ||
server.WithTimeout(10 * time.Second), | ||
server.WithLog(zaptest.NewLogger(t)), | ||
} | ||
s := d.SetupServer(mesh.Hosts()[0], proto, opts...) | ||
require.Equal(t, s, d.Server) | ||
runRequester(t, s) | ||
srvPeerID := mesh.Hosts()[0].ID() | ||
|
||
c := server.New(mesh.Hosts()[1], proto, d.Dispatch, opts...) | ||
for _, tt := range []struct { | ||
name string | ||
want int | ||
}{ | ||
{"a", 42}, | ||
{"b", 43}, | ||
{"c", 44}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require.NoError(t, c.StreamRequest( | ||
context.Background(), srvPeerID, []byte(tt.name), | ||
func(ctx context.Context, stream io.ReadWriter) error { | ||
c := rangesync.StartWireConduit(ctx, stream) | ||
defer c.End() | ||
m, err := c.NextMessage() | ||
require.NoError(t, err) | ||
require.Equal(t, rangesync.MessageTypeRangeContents, m.Type()) | ||
exp := rangesync.KeyBytes(bytes.Repeat([]byte{byte(tt.want)}, 32)) | ||
require.Equal(t, exp, m.X()) | ||
require.Equal(t, exp, m.Y()) | ||
require.Equal(t, tt.want, m.Count()) | ||
m, err = c.NextMessage() | ||
require.NoError(t, err) | ||
require.Equal(t, rangesync.MessageTypeEndRound, m.Type()) | ||
return nil | ||
})) | ||
}) | ||
} | ||
} |
Oops, something went wrong.