Skip to content
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

feat: Create config regardless of connection failures #179

Merged
merged 21 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
976444f
fix(connection): will retry connection for each message
aleksander-vedvik Mar 13, 2024
301dbe2
refactor: ran make dev to regenerate all dev files
aleksander-vedvik Mar 13, 2024
d904f3d
refactor: changed connEstablished to atomicflag and added documentation
aleksander-vedvik Mar 19, 2024
55d70fb
fix: ctx will be canceled when node is closed and added documentation
aleksander-vedvik Mar 19, 2024
dcc75e6
doc: mainly added a few doc comments and move if conn == nil
meling Mar 20, 2024
e78895c
refactor(connection): guardclause and reduced lock time
aleksander-vedvik Mar 21, 2024
d487c5f
test(channel): added test for the channel
aleksander-vedvik Mar 21, 2024
d0b19bf
Merge branch 'connection' of github.com:relab/gorums into connection
aleksander-vedvik Mar 21, 2024
720f01a
fix(channel): added stream mutex on initial channel creation
aleksander-vedvik Mar 21, 2024
5297977
doc: revised the comments and added some doc comments
meling Mar 23, 2024
de46337
fix: typos
meling Mar 24, 2024
ac26307
fix: tweaked error output for TestChannelReconnection
meling Mar 24, 2024
2dd48d7
refactor(connection): moved logic to remove confusing naming
aleksander-vedvik Mar 25, 2024
32d9e81
Merge branch 'master' into connection
meling Mar 26, 2024
da290f6
chore: renamed some funcs and added docs
meling Mar 26, 2024
98fd597
chore: call n.newContext() from within newChannel()
meling Mar 26, 2024
3ded667
fix: data race in correctable call type
meling Mar 26, 2024
ebe8380
Merge branch 'master' into connection
meling Mar 26, 2024
7c573fe
chore: unexported testServerSetup
meling Mar 26, 2024
d2815e8
fix: removed encoding.RegisterCodec
meling Mar 26, 2024
b10575b
chore: removed arguments to mockSrv.Test
meling Mar 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 59 additions & 8 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gorums

import (
"context"
"fmt"
"math"
"math/rand"
"sync"
Expand Down Expand Up @@ -35,7 +36,7 @@ type responseRouter struct {

type channel struct {
sendQ chan request
nodeID uint32
node *RawNode
mu sync.Mutex
lastError error
latency time.Duration
Expand All @@ -50,29 +51,39 @@ type channel struct {
cancelStream context.CancelFunc
responseRouters map[uint64]responseRouter
responseMut sync.Mutex
connEstablished bool
meling marked this conversation as resolved.
Show resolved Hide resolved
}

func newChannel(n *RawNode) *channel {
return &channel{
sendQ: make(chan request, n.mgr.opts.sendBuffer),
backoffCfg: n.mgr.opts.backoff,
nodeID: n.ID(),
node: n,
latency: -1 * time.Second,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
responseRouters: make(map[uint64]responseRouter),
connEstablished: false,
meling marked this conversation as resolved.
Show resolved Hide resolved
}
}

func (c *channel) connect(ctx context.Context, conn *grpc.ClientConn) error {
meling marked this conversation as resolved.
Show resolved Hide resolved
var err error
c.parentCtx = ctx
go c.sendMsgs()
meling marked this conversation as resolved.
Show resolved Hide resolved
if conn == nil {
return fmt.Errorf("connection is nil")
}
return c.tryConnect(conn)
}

func (c *channel) tryConnect(conn *grpc.ClientConn) error {
meling marked this conversation as resolved.
Show resolved Hide resolved
var err error
c.streamCtx, c.cancelStream = context.WithCancel(c.parentCtx)
c.gorumsClient = ordering.NewGorumsClient(conn)
c.gorumsStream, err = c.gorumsClient.NodeStream(c.streamCtx)
if err != nil {
return err
}
go c.sendMsgs()
c.connEstablished = true
meling marked this conversation as resolved.
Show resolved Hide resolved
go c.recvMsgs()
return nil
}
Expand Down Expand Up @@ -160,17 +171,23 @@ func (c *channel) sendMsgs() {
return
case req = <-c.sendQ:
}
// try to connect to the node if previous attempts
// have failed or if the node has disconnected
if !c.isConnected() {
// streamBroken will be set if the connection fails
c.tryReconnect()
}
// return error if stream is broken
if c.streamBroken.get() {
err := status.Errorf(codes.Unavailable, "stream is down")
c.routeResponse(req.msg.Metadata.MessageID, response{nid: c.nodeID, msg: nil, err: err})
c.routeResponse(req.msg.Metadata.MessageID, response{nid: c.node.ID(), msg: nil, err: err})
continue
}
// else try to send message
err := c.sendMsg(req)
if err != nil {
// return the error
c.routeResponse(req.msg.Metadata.MessageID, response{nid: c.nodeID, msg: nil, err: err})
c.routeResponse(req.msg.Metadata.MessageID, response{nid: c.node.ID(), msg: nil, err: err})
}
}
}
Expand All @@ -189,7 +206,7 @@ func (c *channel) recvMsgs() {
} else {
c.streamMut.RUnlock()
err := status.FromProto(resp.Metadata.GetStatus()).Err()
c.routeResponse(resp.Metadata.MessageID, response{nid: c.nodeID, msg: resp.Message, err: err})
c.routeResponse(resp.Metadata.MessageID, response{nid: c.node.ID(), msg: resp.Message, err: err})
}

select {
Expand All @@ -200,11 +217,37 @@ func (c *channel) recvMsgs() {
}
}

func (c *channel) reconnect() {
func (c *channel) tryReconnect() {
meling marked this conversation as resolved.
Show resolved Hide resolved
// a connection has never been established
if !c.connEstablished {
meling marked this conversation as resolved.
Show resolved Hide resolved
err := c.node.dial()
if err != nil {
c.streamBroken.set()
return
}
err = c.tryConnect(c.node.conn)
if err != nil {
c.streamBroken.set()
return
}
}
// the node has previously been connected
// but is now disconnected
if c.streamBroken.get() {
// try to reconnect only once
c.reconnect(1)
}
}

func (c *channel) reconnect(maxRetries ...int) {
meling marked this conversation as resolved.
Show resolved Hide resolved
c.streamMut.Lock()
defer c.streamMut.Unlock()
backoffCfg := c.backoffCfg

var maxretries float64 = -1
if len(maxRetries) > 0 {
maxretries = float64(maxRetries[0])
}
var retries float64
for {
var err error
Expand All @@ -217,6 +260,10 @@ func (c *channel) reconnect() {
}
c.cancelStream()
c.setLastErr(err)
if retries >= maxretries && maxretries > 0 {
c.streamBroken.set()
return
}
delay := float64(backoffCfg.BaseDelay)
max := float64(backoffCfg.MaxDelay)
for r := retries; delay < max && r > 0; r-- {
Expand Down Expand Up @@ -257,6 +304,10 @@ type atomicFlag struct {
flag int32
}

func (c *channel) isConnected() bool {
meling marked this conversation as resolved.
Show resolved Hide resolved
return c.connEstablished && !c.streamBroken.get()
}

func (f *atomicFlag) set() { atomic.StoreInt32(&f.flag, 1) }
func (f *atomicFlag) get() bool { return atomic.LoadInt32(&f.flag) == 1 }
func (f *atomicFlag) clear() { atomic.StoreInt32(&f.flag, 0) }
14 changes: 7 additions & 7 deletions cmd/protoc-gen-gorums/dev/zorums.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions cmd/protoc-gen-gorums/dev/zorums_async_gorums.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions cmd/protoc-gen-gorums/dev/zorums_correctable_gorums.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions cmd/protoc-gen-gorums/dev/zorums_multicast_gorums.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading