major refactor
This commit is contained in:
118
synced-pool.go
118
synced-pool.go
@@ -22,7 +22,7 @@ type HealthHandler interface {
|
||||
}
|
||||
|
||||
type SyncedPool struct {
|
||||
*Server
|
||||
Server *GenericListener
|
||||
mu sync.RWMutex
|
||||
Hostname string
|
||||
local *GrainLocalPool
|
||||
@@ -61,11 +61,16 @@ var (
|
||||
})
|
||||
)
|
||||
|
||||
func (p *SyncedPool) PongHandler(data []byte) (PoolMessage, []byte, error) {
|
||||
return Pong, data, nil
|
||||
var (
|
||||
PongResponse = MakeFrameWithPayload(Pong, 200, nil)
|
||||
)
|
||||
|
||||
func (p *SyncedPool) PongHandler(data *FrameWithPayload, resultChan chan<- FrameWithPayload) error {
|
||||
resultChan <- PongResponse
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *SyncedPool) GetCartIdHandler(data []byte) (PoolMessage, []byte, error) {
|
||||
func (p *SyncedPool) GetCartIdHandler(data *FrameWithPayload, resultChan chan<- FrameWithPayload) error {
|
||||
ids := make([]string, 0, len(p.local.grains))
|
||||
for id := range p.local.grains {
|
||||
if p.local.grains[id] == nil {
|
||||
@@ -78,45 +83,45 @@ func (p *SyncedPool) GetCartIdHandler(data []byte) (PoolMessage, []byte, error)
|
||||
ids = append(ids, s)
|
||||
}
|
||||
log.Printf("Returning %d cart ids\n", len(ids))
|
||||
return CartIdsResponse, []byte(strings.Join(ids, ";")), nil
|
||||
resultChan <- MakeFrameWithPayload(CartIdsResponse, 200, []byte(strings.Join(ids, ";")))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *SyncedPool) NegotiateHandler(data []byte) (PoolMessage, []byte, error) {
|
||||
func (p *SyncedPool) NegotiateHandler(data *FrameWithPayload, resultChan chan<- FrameWithPayload) error {
|
||||
negotiationCount.Inc()
|
||||
log.Printf("Handling negotiation\n")
|
||||
for _, host := range p.ExcludeKnown(strings.Split(string(data), ";")) {
|
||||
for _, host := range p.ExcludeKnown(strings.Split(string(data.Payload), ";")) {
|
||||
if host == "" {
|
||||
continue
|
||||
}
|
||||
go p.AddRemote(host)
|
||||
|
||||
}
|
||||
|
||||
return RemoteNegotiateResponse, []byte("ok"), nil
|
||||
resultChan <- MakeFrameWithPayload(RemoteNegotiateResponse, 200, []byte("ok"))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *SyncedPool) GrainOwnerChangeHandler(data []byte) (PoolMessage, []byte, error) {
|
||||
func (p *SyncedPool) GrainOwnerChangeHandler(data *FrameWithPayload, resultChan chan<- FrameWithPayload) error {
|
||||
grainSyncCount.Inc()
|
||||
|
||||
idAndHostParts := strings.Split(string(data), ";")
|
||||
idAndHostParts := strings.Split(string(data.Payload), ";")
|
||||
if len(idAndHostParts) != 2 {
|
||||
log.Printf("Invalid remote grain change message\n")
|
||||
return AckChange, []byte("incorrect"), fmt.Errorf("invalid remote grain change message")
|
||||
resultChan <- MakeFrameWithPayload(AckError, 400, []byte("invalid"))
|
||||
return nil
|
||||
}
|
||||
id := ToCartId(idAndHostParts[0])
|
||||
host := idAndHostParts[1]
|
||||
log.Printf("Handling remote grain owner change to %s for id %s\n", host, id)
|
||||
for _, r := range p.remotes {
|
||||
if r.Host == host && r.IsHealthy() {
|
||||
// log.Printf("Remote grain %s changed to %s\n", id, host)
|
||||
|
||||
go p.SpawnRemoteGrain(id, host)
|
||||
|
||||
return AckChange, []byte("ok"), nil
|
||||
break
|
||||
}
|
||||
}
|
||||
go p.AddRemote(host)
|
||||
return AckChange, []byte("ok"), nil
|
||||
resultChan <- MakeFrameWithPayload(AckChange, 200, []byte("ok"))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *SyncedPool) RemoveRemoteGrain(id CartId) {
|
||||
@@ -142,12 +147,12 @@ func (p *SyncedPool) SpawnRemoteGrain(id CartId, host string) {
|
||||
log.Printf("Error creating remote grain %v\n", err)
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
<-remote.PersistentConnection.Died
|
||||
p.RemoveRemoteGrain(id)
|
||||
p.HandleHostError(host)
|
||||
log.Printf("Remote grain %s died, host: %s\n", id.String(), host)
|
||||
}()
|
||||
// go func() {
|
||||
// <-remote.Died
|
||||
// p.RemoveRemoteGrain(id)
|
||||
// p.HandleHostError(host)
|
||||
// log.Printf("Remote grain %s died, host: %s\n", id.String(), host)
|
||||
// }()
|
||||
|
||||
p.mu.Lock()
|
||||
p.remoteIndex[id] = remote
|
||||
@@ -159,8 +164,6 @@ func (p *SyncedPool) HandleHostError(host string) {
|
||||
if r.Host == host {
|
||||
if !r.IsHealthy() {
|
||||
p.RemoveHost(r)
|
||||
} else {
|
||||
r.ErrorCount++
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -169,8 +172,8 @@ func (p *SyncedPool) HandleHostError(host string) {
|
||||
|
||||
func NewSyncedPool(local *GrainLocalPool, hostname string, discovery Discovery) (*SyncedPool, error) {
|
||||
listen := fmt.Sprintf("%s:1338", hostname)
|
||||
|
||||
server, err := Listen(listen)
|
||||
conn := NewConnection(listen)
|
||||
server, err := conn.Listen()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -186,10 +189,10 @@ func NewSyncedPool(local *GrainLocalPool, hostname string, discovery Discovery)
|
||||
remoteIndex: make(map[CartId]*RemoteGrain),
|
||||
}
|
||||
|
||||
server.HandleCall(Ping, pool.PongHandler)
|
||||
server.HandleCall(GetCartIds, pool.GetCartIdHandler)
|
||||
server.HandleCall(RemoteNegotiate, pool.NegotiateHandler)
|
||||
server.HandleCall(RemoteGrainChanged, pool.GrainOwnerChangeHandler)
|
||||
server.AddHandler(Ping, pool.PongHandler)
|
||||
server.AddHandler(GetCartIds, pool.GetCartIdHandler)
|
||||
server.AddHandler(RemoteNegotiate, pool.NegotiateHandler)
|
||||
server.AddHandler(RemoteGrainChanged, pool.GrainOwnerChangeHandler)
|
||||
|
||||
if discovery != nil {
|
||||
go func() {
|
||||
@@ -259,18 +262,11 @@ func (p *SyncedPool) ExcludeKnown(hosts []string) []string {
|
||||
}
|
||||
|
||||
func (p *SyncedPool) RemoveHost(host *RemoteHost) {
|
||||
if p.remotes[host.Host] == nil {
|
||||
return
|
||||
}
|
||||
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
h := p.remotes[host.Host]
|
||||
if h != nil {
|
||||
h.Close()
|
||||
}
|
||||
delete(p.remotes, host.Host)
|
||||
|
||||
p.mu.Unlock()
|
||||
p.RemoveHostMappedCarts(host)
|
||||
connectedRemotes.Set(float64(len(p.remotes)))
|
||||
}
|
||||
|
||||
@@ -279,24 +275,21 @@ func (p *SyncedPool) RemoveHostMappedCarts(host *RemoteHost) {
|
||||
defer p.mu.Unlock()
|
||||
for id, r := range p.remoteIndex {
|
||||
if r.Host == host.Host {
|
||||
p.remoteIndex[id].Close()
|
||||
delete(p.remoteIndex, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type PoolMessage uint32
|
||||
|
||||
const (
|
||||
RemoteNegotiate = PoolMessage(3)
|
||||
RemoteGrainChanged = PoolMessage(4)
|
||||
AckChange = PoolMessage(5)
|
||||
//AckError = PoolMessage(6)
|
||||
Ping = PoolMessage(7)
|
||||
Pong = PoolMessage(8)
|
||||
GetCartIds = PoolMessage(9)
|
||||
CartIdsResponse = PoolMessage(10)
|
||||
RemoteNegotiateResponse = PoolMessage(11)
|
||||
RemoteNegotiate = FrameType(3)
|
||||
RemoteGrainChanged = FrameType(4)
|
||||
AckChange = FrameType(5)
|
||||
AckError = FrameType(6)
|
||||
Ping = FrameType(7)
|
||||
Pong = FrameType(8)
|
||||
GetCartIds = FrameType(9)
|
||||
CartIdsResponse = FrameType(10)
|
||||
RemoteNegotiateResponse = FrameType(11)
|
||||
)
|
||||
|
||||
func (p *SyncedPool) Negotiate() {
|
||||
@@ -377,25 +370,22 @@ func (p *SyncedPool) AddRemote(host string) error {
|
||||
if host == "" || p.IsKnown(host) || hasHost {
|
||||
return nil
|
||||
}
|
||||
client, err := Dial(fmt.Sprintf("%s:1338", host))
|
||||
if err != nil {
|
||||
client := NewConnection(fmt.Sprintf("%s:1338", host))
|
||||
response, err := client.Call(Ping, nil)
|
||||
if err != nil || response.StatusCode != 200 || response.Type != Pong {
|
||||
log.Printf("Error connecting to remote %s: %v\n", host, err)
|
||||
return err
|
||||
}
|
||||
|
||||
remote := RemoteHost{
|
||||
Client: client,
|
||||
Connection: client,
|
||||
MissedPings: 0,
|
||||
Host: host,
|
||||
}
|
||||
p.mu.Lock()
|
||||
p.remotes[host] = &remote
|
||||
p.mu.Unlock()
|
||||
go func() {
|
||||
<-remote.PersistentConnection.Died
|
||||
log.Printf("Removing host, remote died %s", host)
|
||||
p.RemoveHost(&remote)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for range time.Tick(time.Second * 3) {
|
||||
|
||||
@@ -450,9 +440,9 @@ func (p *SyncedPool) getGrain(id CartId) (Grain, error) {
|
||||
return localGrain, nil
|
||||
}
|
||||
|
||||
func (p *SyncedPool) Process(id CartId, messages ...Message) (*CallResult, error) {
|
||||
func (p *SyncedPool) Process(id CartId, messages ...Message) (*FrameWithPayload, error) {
|
||||
pool, err := p.getGrain(id)
|
||||
var res *CallResult
|
||||
var res *FrameWithPayload
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -465,7 +455,7 @@ func (p *SyncedPool) Process(id CartId, messages ...Message) (*CallResult, error
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (p *SyncedPool) Get(id CartId) (*CallResult, error) {
|
||||
func (p *SyncedPool) Get(id CartId) (*FrameWithPayload, error) {
|
||||
grain, err := p.getGrain(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user