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

Improve hub locking #1694

Merged
merged 28 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ea2bdbe
Upgrade go version and go mod tidy
MariemBaccari Oct 3, 2023
7442560
Add message ids thread safe structure
MariemBaccari Oct 3, 2023
66155dc
fix hub tests
MariemBaccari Oct 3, 2023
0e11844
Add peers struct
MariemBaccari Oct 4, 2023
3205c9e
fix queries data structure
MariemBaccari Oct 4, 2023
5c912bb
fix inbox tests
MariemBaccari Oct 10, 2023
f15fd57
debugging
MariemBaccari Oct 10, 2023
864ec8c
Add hub_state
MariemBaccari Oct 10, 2023
a45c2d9
remove additional config files
MariemBaccari Oct 10, 2023
25977d0
fix typo
MariemBaccari Oct 10, 2023
f853897
Add documentation and fix CI
MariemBaccari Oct 10, 2023
2c6af8e
fix hub log
MariemBaccari Oct 11, 2023
feecb98
Add thread safe map
MariemBaccari Oct 14, 2023
55bf030
Fix threadsafemap usage
MariemBaccari Oct 14, 2023
47a176c
Add usage of sets
MariemBaccari Oct 14, 2023
87e6a88
Address Pierluca's comments
MariemBaccari Oct 18, 2023
70eb68a
Add imports and foreach channels
MariemBaccari Oct 18, 2023
e0ae6a9
Change go version in workflows
MariemBaccari Oct 18, 2023
de9f7e4
change go version on karate workflow
MariemBaccari Oct 18, 2023
1314dbd
experimenting with CI and goimports
MariemBaccari Oct 18, 2023
962663f
experimenting with CI and goimports
MariemBaccari Oct 18, 2023
87934ac
try to add goimports
MariemBaccari Oct 18, 2023
05a8a8b
remove goimports
MariemBaccari Oct 18, 2023
54d4b75
fix documentation
MariemBaccari Oct 24, 2023
dd78bc2
cancel config changes
MariemBaccari Oct 24, 2023
2bc39db
adress pierluca's comments
MariemBaccari Oct 30, 2023
8b7482d
fix small typo
MariemBaccari Oct 30, 2023
94aba08
fix filenames
MariemBaccari Nov 4, 2023
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
9 changes: 7 additions & 2 deletions be1-go/channel/lao/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"golang.org/x/exp/slices"
popstellar "popstellar"
"popstellar/channel"
"popstellar/channel/authentication"
Expand Down Expand Up @@ -705,9 +706,13 @@ func (c *Channel) createAndSendLAOGreet() error {
peers := []messagedata.Peer{}

for _, info := range c.hub.GetPeersInfo() {
peers = append(peers, messagedata.Peer{
peer := messagedata.Peer{
Address: info.ClientAddress,
})
}
if slices.Contains(peers, peer) {
continue
}
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved
peers = append(peers, peer)
}

msgData := messagedata.LaoGreet{
Expand Down
2 changes: 1 addition & 1 deletion be1-go/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module popstellar

go 1.19
go 1.21

require (
github.com/aaronarduino/goqrsvg v0.0.0-20220419053939-17e843f1dd40
Expand Down
4 changes: 4 additions & 0 deletions be1-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc=
Expand Down Expand Up @@ -146,6 +149,7 @@ google.golang.org/protobuf v1.29.1 h1:7QBf+IK2gx70Ap/hDsOmam3GE0v9HicjfEdAxE62Uo
google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI=
gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
Expand Down
44 changes: 44 additions & 0 deletions be1-go/hub/standard_hub/hub_state/channels_by_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package hub_state

import (
"maps"
"popstellar/channel"
"sync"
)

// ChannelsById provides a thread-safe structure that stores channel ids with their corresponding channels
type ChannelsById struct {
sync.RWMutex
table map[string]channel.Channel
}
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved

// NewChannelsById creates a new ChannelsById structure
func NewChannelsById() ChannelsById {
return ChannelsById{
table: make(map[string]channel.Channel),
}
}

// Add adds a channel to the table
func (c *ChannelsById) Add(channelId string, channel channel.Channel) {
c.Lock()
defer c.Unlock()
c.table[channelId] = channel
}

// GetChannel returns a channel from the table if it exists, otherwise it returns false and nil
func (c *ChannelsById) GetChannel(channelId string) (channel.Channel, bool) {
c.RLock()
defer c.RUnlock()
channel, ok := c.table[channelId]
return channel, ok
}

// GetAll returns a copy of the table
func (c *ChannelsById) GetAll() map[string]channel.Channel {
c.RLock()
defer c.RUnlock()
channelsCopy := make(map[string]channel.Channel)
maps.Copy(channelsCopy, c.table)
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved
return channelsCopy
}
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved
59 changes: 59 additions & 0 deletions be1-go/hub/standard_hub/hub_state/ids_by_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package hub_state

import (
"golang.org/x/exp/slices"
"maps"
"sync"
)

// IdsByChannel provides a thread-safe structure that stores a channel id with its corresponding message ids
type IdsByChannel struct {
sync.RWMutex
table map[string][]string
}
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved

// NewIdsByChannel creates a new IdsByChannel structure
func NewIdsByChannel() IdsByChannel {
return IdsByChannel{
table: make(map[string][]string),
}
}

// Add adds a message id to the table
func (i *IdsByChannel) Add(channel string, id string) {
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved
i.Lock()
defer i.Unlock()
messageIds, channelStored := i.table[channel]
if !channelStored {
i.table[channel] = append(i.table[channel], id)
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved
return
}
alreadyStoredId := slices.Contains(messageIds, id)
if !alreadyStoredId {
i.table[channel] = append(i.table[channel], id)
}
}

// AddAll adds a slice of message ids to the table
func (i *IdsByChannel) AddAll(channel string, ids []string) {
i.Lock()
defer i.Unlock()
i.table[channel] = append(i.table[channel], ids...)
}

// GetAll returns a copy of the table
func (i *IdsByChannel) GetAll() map[string][]string {
i.RLock()
defer i.RUnlock()
tableCopy := make(map[string][]string)
maps.Copy(tableCopy, i.table)
return tableCopy
}

// IsEmpty returns true if the table is empty, otherwise it returns false
func (i *IdsByChannel) IsEmpty() bool {
i.RLock()
defer i.RUnlock()

return len(i.table) == 0
}
59 changes: 59 additions & 0 deletions be1-go/hub/standard_hub/hub_state/peers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package hub_state

import (
"golang.org/x/exp/slices"
"popstellar/message/query/method"
"sync"
)

// Peers provides a thread-safe structure that stores the peers' information
type Peers struct {
sync.RWMutex
// peersInfo stores the info of the peers: public key, client and server endpoints associated with the socket ID
peersInfo map[string]method.ServerInfo
// peersGreeted stores the peers that were greeted by the socket ID
peersGreeted []string
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved
}

// NewPeers creates a new Peers structure
func NewPeers() Peers {
return Peers{
peersInfo: make(map[string]method.ServerInfo),
peersGreeted: make([]string, 0),
}
}

// AddPeerInfo adds a peer's info to the table
func (p *Peers) AddPeerInfo(socketId string, info method.ServerInfo) {
p.Lock()
defer p.Unlock()
p.peersInfo[socketId] = info
}

// AddPeerGreeted adds a peer's socket ID to the slice of peers greeted
func (p *Peers) AddPeerGreeted(socketId string) {
p.Lock()
defer p.Unlock()
if slices.Contains(p.peersGreeted, socketId) {
return
}
p.peersGreeted = append(p.peersGreeted, socketId)
}

// GetAllPeersInfo returns a copy of the peers' info slice
func (p *Peers) GetAllPeersInfo() []method.ServerInfo {
p.RLock()
defer p.RUnlock()
peersInfo := make([]method.ServerInfo, 0)
for _, info := range p.peersInfo {
peersInfo = append(peersInfo, info)
}
return peersInfo
}

// IsPeerGreeted returns true if the peer was greeted, otherwise it returns false
func (p *Peers) IsPeerGreeted(socketId string) bool {
p.RLock()
defer p.RUnlock()
return slices.Contains(p.peersGreeted, socketId)
}
60 changes: 60 additions & 0 deletions be1-go/hub/standard_hub/hub_state/queries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package hub_state

import (
"popstellar/message/query/method"
"sync"
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved
)

// Queries let the hub remember all queries that it sent to other servers
type Queries struct {
sync.Mutex
// state stores the ID of the server's queries and their state. False for a
// query not yet answered, else true.
state map[int]*bool
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved
// getMessagesByIdQueries stores the server's getMessagesByIds queries by their ID.
getMessagesByIdQueries map[int]method.GetMessagesById
// nextID store the ID of the next query
nextID int
}

// NewQueries creates a new queries struct
func NewQueries() Queries {
return Queries{
state: make(map[int]*bool),
getMessagesByIdQueries: make(map[int]method.GetMessagesById),
}
}

// GetQueryState returns a given query's state
func (q *Queries) GetQueryState(id int) *bool {
q.Lock()
defer q.Unlock()

return q.state[id]
}

// GetNextID returns the next query ID
func (q *Queries) GetNextID() int {
q.Lock()
defer q.Unlock()

id := q.nextID
q.nextID++
return id
}

// SetQueryState sets the state of the query with the given ID
func (q *Queries) SetQueryState(id int, state bool) {
q.Lock()
defer q.Unlock()

q.state[id] = &state
}

// AddQuery adds the given query to the table
func (q *Queries) AddQuery(id int, query method.GetMessagesById) {
q.Lock()
defer q.Unlock()

q.getMessagesByIdQueries[id] = query
}
Loading
Loading