testing locks
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -22,6 +23,7 @@ func NewPoolServer(pool GrainPool, pod_name string) *PoolServer {
|
||||
|
||||
func (s *PoolServer) HandleGet(w http.ResponseWriter, r *http.Request) error {
|
||||
id := r.PathValue("id")
|
||||
log.Printf("Getting cart %s\n", id)
|
||||
data, err := s.pool.Get(ToCartId(id))
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -319,11 +319,23 @@ func (p *SyncedPool) Negotiate() {
|
||||
|
||||
}
|
||||
|
||||
func (p *SyncedPool) GetHealthyRemotes() []*RemoteHost {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
remotes := make([]*RemoteHost, 0, len(p.remotes))
|
||||
for _, r := range p.remotes {
|
||||
if r.IsHealthy() {
|
||||
remotes = append(remotes, r)
|
||||
}
|
||||
}
|
||||
return remotes
|
||||
}
|
||||
|
||||
func (p *SyncedPool) RequestOwnership(id CartId) error {
|
||||
ok := 0
|
||||
all := 0
|
||||
p.mu.RLock()
|
||||
for _, r := range p.remotes {
|
||||
|
||||
for _, r := range p.GetHealthyRemotes() {
|
||||
if !r.IsHealthy() {
|
||||
continue
|
||||
}
|
||||
@@ -342,7 +354,7 @@ func (p *SyncedPool) RequestOwnership(id CartId) error {
|
||||
}
|
||||
ok++
|
||||
}
|
||||
p.mu.RUnlock()
|
||||
|
||||
if ok == 0 && all > 0 {
|
||||
p.removeLocalGrain(id)
|
||||
return fmt.Errorf("no remotes confirmed change")
|
||||
@@ -361,8 +373,7 @@ func (p *SyncedPool) removeLocalGrain(id CartId) {
|
||||
}
|
||||
|
||||
func (p *SyncedPool) AddRemote(host string) error {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
_, hasHost := p.remotes[host]
|
||||
if host == "" || p.IsKnown(host) || hasHost {
|
||||
return nil
|
||||
@@ -378,7 +389,9 @@ func (p *SyncedPool) AddRemote(host string) error {
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user