update
This commit is contained in:
@@ -219,6 +219,7 @@ func (p *CartPool) HandleOwnershipChange(host string, ids []uint64) error {
|
|||||||
p.localMu.Lock()
|
p.localMu.Lock()
|
||||||
defer p.localMu.Unlock()
|
defer p.localMu.Unlock()
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
|
log.Printf("Handling ownership change for cart %d to host %s", id, host)
|
||||||
delete(p.grains, id)
|
delete(p.grains, id)
|
||||||
p.remoteOwners[id] = remoteHost
|
p.remoteOwners[id] = remoteHost
|
||||||
}
|
}
|
||||||
@@ -226,15 +227,11 @@ func (p *CartPool) HandleOwnershipChange(host string, ids []uint64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SnapshotGrains returns a copy of the currently resident grains keyed by id.
|
// SnapshotGrains returns a copy of the currently resident grains keyed by id.
|
||||||
func (p *CartPool) SnapshotGrains() map[CartId]*CartGrain {
|
func (p *CartPool) SnapshotGrains() map[uint64]*CartGrain {
|
||||||
p.localMu.RLock()
|
p.localMu.RLock()
|
||||||
defer p.localMu.RUnlock()
|
defer p.localMu.RUnlock()
|
||||||
out := make(map[CartId]*CartGrain, len(p.grains))
|
out := maps.Clone(p.grains)
|
||||||
for _, g := range p.grains {
|
|
||||||
if g != nil {
|
|
||||||
out[g.GetId()] = g
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,6 +255,11 @@ func (p *CartPool) SnapshotGrains() map[CartId]*CartGrain {
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
func (p *CartPool) TakeOwnership(id uint64) {
|
func (p *CartPool) TakeOwnership(id uint64) {
|
||||||
|
|
||||||
|
if p.grains[id] != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("taking ownership of: %d", id)
|
||||||
p.broadcastOwnership([]uint64{id})
|
p.broadcastOwnership([]uint64{id})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -402,19 +404,19 @@ func (p *CartPool) SendNegotiation() {
|
|||||||
}
|
}
|
||||||
p.remoteMu.RUnlock()
|
p.remoteMu.RUnlock()
|
||||||
|
|
||||||
for _, r := range remotes {
|
p.forAllHosts(func(remote *proxy.RemoteHost) {
|
||||||
knownByRemote, err := r.Negotiate(hosts)
|
knownByRemote, err := remote.Negotiate(hosts)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Negotiate with %s failed: %v", r.Host, err)
|
log.Printf("Negotiate with %s failed: %v", remote.Host, err)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
for _, h := range knownByRemote {
|
for _, h := range knownByRemote {
|
||||||
if !p.IsKnown(h) {
|
if !p.IsKnown(h) {
|
||||||
p.AddRemote(h)
|
go p.AddRemote(h)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *CartPool) forAllHosts(fn func(*proxy.RemoteHost)) {
|
func (p *CartPool) forAllHosts(fn func(*proxy.RemoteHost)) {
|
||||||
@@ -493,8 +495,7 @@ func (p *CartPool) Apply(id uint64, mutation any) (*CartGrain, error) {
|
|||||||
|
|
||||||
if applyErr == nil && result != nil {
|
if applyErr == nil && result != nil {
|
||||||
cartMutationsTotal.Inc()
|
cartMutationsTotal.Inc()
|
||||||
//p.RefreshExpiry(id)
|
|
||||||
//cartActiveGrains.Set(float64(len(p.grains)))
|
|
||||||
} else if applyErr != nil {
|
} else if applyErr != nil {
|
||||||
cartMutationFailuresTotal.Inc()
|
cartMutationFailuresTotal.Inc()
|
||||||
}
|
}
|
||||||
|
|||||||
48
main.go
48
main.go
@@ -67,21 +67,21 @@ type App struct {
|
|||||||
storage *DiskStorage
|
storage *DiskStorage
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) Save() error {
|
// func (a *App) Save() error {
|
||||||
for id, grain := range a.pool.SnapshotGrains() {
|
// for id, grain := range a.pool.SnapshotGrains() {
|
||||||
if grain == nil {
|
// if grain == nil {
|
||||||
continue
|
// continue
|
||||||
}
|
// }
|
||||||
if grain.GetLastChange().After(a.storage.LastSaves[uint64(id)]) {
|
// if grain.GetLastChange().After(a.storage.LastSaves[uint64(id)]) {
|
||||||
|
|
||||||
err := a.storage.Store(id, grain)
|
// err := a.storage.Store(id, grain)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Printf("Error saving grain %s: %v\n", id, err)
|
// log.Printf("Error saving grain %s: %v\n", id, err)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
var podIp = os.Getenv("POD_IP")
|
var podIp = os.Getenv("POD_IP")
|
||||||
var name = os.Getenv("POD_NAME")
|
var name = os.Getenv("POD_NAME")
|
||||||
@@ -140,20 +140,20 @@ func main() {
|
|||||||
storage: storage,
|
storage: storage,
|
||||||
}
|
}
|
||||||
|
|
||||||
grpcSrv, err := actor.NewControlServer(":1337", pool)
|
grpcSrv, err := actor.NewControlServer[*CartGrain](":1337", pool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error starting control plane gRPC server: %v\n", err)
|
log.Fatalf("Error starting control plane gRPC server: %v\n", err)
|
||||||
}
|
}
|
||||||
defer grpcSrv.GracefulStop()
|
defer grpcSrv.GracefulStop()
|
||||||
|
|
||||||
go func() {
|
// go func() {
|
||||||
for range time.Tick(time.Minute * 5) {
|
// for range time.Tick(time.Minute * 5) {
|
||||||
err := app.Save()
|
// err := app.Save()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Printf("Error saving: %v\n", err)
|
// log.Printf("Error saving: %v\n", err)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}()
|
// }()
|
||||||
orderHandler := &AmqpOrderHandler{
|
orderHandler := &AmqpOrderHandler{
|
||||||
Url: amqpUrl,
|
Url: amqpUrl,
|
||||||
}
|
}
|
||||||
@@ -344,7 +344,7 @@ func main() {
|
|||||||
go func() {
|
go func() {
|
||||||
sig := <-sigs
|
sig := <-sigs
|
||||||
fmt.Println("Shutting down due to signal:", sig)
|
fmt.Println("Shutting down due to signal:", sig)
|
||||||
app.Save()
|
//app.Save()
|
||||||
pool.Close()
|
pool.Close()
|
||||||
|
|
||||||
done <- true
|
done <- true
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -153,28 +152,28 @@ func (h *RemoteHost) AnnounceExpiry(uids []uint64) {
|
|||||||
|
|
||||||
func (h *RemoteHost) Proxy(id uint64, w http.ResponseWriter, r *http.Request) (bool, error) {
|
func (h *RemoteHost) Proxy(id uint64, w http.ResponseWriter, r *http.Request) (bool, error) {
|
||||||
target := fmt.Sprintf("%s%s", h.httpBase, r.URL.RequestURI())
|
target := fmt.Sprintf("%s%s", h.httpBase, r.URL.RequestURI())
|
||||||
var bodyCopy []byte
|
// var bodyCopy []byte
|
||||||
if r.Body != nil && r.Body != http.NoBody {
|
// if r.Body != nil && r.Body != http.NoBody {
|
||||||
var err error
|
// var err error
|
||||||
bodyCopy, err = io.ReadAll(r.Body)
|
// bodyCopy, err = io.ReadAll(r.Body)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
http.Error(w, "proxy read error", http.StatusBadGateway)
|
// http.Error(w, "proxy read error", http.StatusBadGateway)
|
||||||
return false, err
|
// return false, err
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
if r.Body != nil {
|
// if r.Body != nil {
|
||||||
r.Body.Close()
|
// r.Body.Close()
|
||||||
}
|
// }
|
||||||
var reqBody io.Reader
|
// var reqBody io.Reader
|
||||||
if len(bodyCopy) > 0 {
|
// if len(bodyCopy) > 0 {
|
||||||
reqBody = bytes.NewReader(bodyCopy)
|
// reqBody = bytes.NewReader(bodyCopy)
|
||||||
}
|
// }
|
||||||
req, err := http.NewRequestWithContext(r.Context(), r.Method, target, reqBody)
|
req, err := http.NewRequestWithContext(r.Context(), r.Method, target, r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "proxy build error", http.StatusBadGateway)
|
http.Error(w, "proxy build error", http.StatusBadGateway)
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
r.Body = io.NopCloser(bytes.NewReader(bodyCopy))
|
//r.Body = io.NopCloser(bytes.NewReader(bodyCopy))
|
||||||
req.Header.Set("X-Forwarded-Host", r.Host)
|
req.Header.Set("X-Forwarded-Host", r.Host)
|
||||||
|
|
||||||
for k, v := range r.Header {
|
for k, v := range r.Header {
|
||||||
|
|||||||
@@ -24,12 +24,8 @@ func NewPoolServer(pool *CartPool, pod_name string) *PoolServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PoolServer) process(id CartId, mutation interface{}) (*CartGrain, error) {
|
func (s *PoolServer) ApplyLocal(id CartId, mutation interface{}) (*CartGrain, error) {
|
||||||
grain, err := s.pool.Apply(uint64(id), mutation)
|
return s.pool.Apply(uint64(id), mutation)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return grain, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PoolServer) HandleGet(w http.ResponseWriter, r *http.Request, id CartId) error {
|
func (s *PoolServer) HandleGet(w http.ResponseWriter, r *http.Request, id CartId) error {
|
||||||
@@ -43,7 +39,7 @@ func (s *PoolServer) HandleGet(w http.ResponseWriter, r *http.Request, id CartId
|
|||||||
|
|
||||||
func (s *PoolServer) HandleAddSku(w http.ResponseWriter, r *http.Request, id CartId) error {
|
func (s *PoolServer) HandleAddSku(w http.ResponseWriter, r *http.Request, id CartId) error {
|
||||||
sku := r.PathValue("sku")
|
sku := r.PathValue("sku")
|
||||||
data, err := s.process(id, &messages.AddRequest{Sku: sku, Quantity: 1})
|
data, err := s.ApplyLocal(id, &messages.AddRequest{Sku: sku, Quantity: 1})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -74,7 +70,7 @@ func (s *PoolServer) HandleDeleteItem(w http.ResponseWriter, r *http.Request, id
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
data, err := s.process(id, &messages.RemoveItem{Id: int64(itemId)})
|
data, err := s.ApplyLocal(id, &messages.RemoveItem{Id: int64(itemId)})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -94,7 +90,7 @@ func (s *PoolServer) HandleSetDelivery(w http.ResponseWriter, r *http.Request, i
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
data, err := s.process(id, &messages.SetDelivery{
|
data, err := s.ApplyLocal(id, &messages.SetDelivery{
|
||||||
Provider: delivery.Provider,
|
Provider: delivery.Provider,
|
||||||
Items: delivery.Items,
|
Items: delivery.Items,
|
||||||
PickupPoint: delivery.PickupPoint,
|
PickupPoint: delivery.PickupPoint,
|
||||||
@@ -117,7 +113,7 @@ func (s *PoolServer) HandleSetPickupPoint(w http.ResponseWriter, r *http.Request
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
reply, err := s.process(id, &messages.SetPickupPoint{
|
reply, err := s.ApplyLocal(id, &messages.SetPickupPoint{
|
||||||
DeliveryId: int64(deliveryId),
|
DeliveryId: int64(deliveryId),
|
||||||
Id: pickupPoint.Id,
|
Id: pickupPoint.Id,
|
||||||
Name: pickupPoint.Name,
|
Name: pickupPoint.Name,
|
||||||
@@ -139,7 +135,7 @@ func (s *PoolServer) HandleRemoveDelivery(w http.ResponseWriter, r *http.Request
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
reply, err := s.process(id, &messages.RemoveDelivery{Id: int64(deliveryId)})
|
reply, err := s.ApplyLocal(id, &messages.RemoveDelivery{Id: int64(deliveryId)})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -152,7 +148,7 @@ func (s *PoolServer) HandleQuantityChange(w http.ResponseWriter, r *http.Request
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
reply, err := s.process(id, &changeQuantity)
|
reply, err := s.ApplyLocal(id, &changeQuantity)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -165,7 +161,7 @@ func (s *PoolServer) HandleSetCartItems(w http.ResponseWriter, r *http.Request,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
reply, err := s.process(id, &setCartItems)
|
reply, err := s.ApplyLocal(id, &setCartItems)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -178,7 +174,7 @@ func (s *PoolServer) HandleAddRequest(w http.ResponseWriter, r *http.Request, id
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
reply, err := s.process(id, &addRequest)
|
reply, err := s.ApplyLocal(id, &addRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -365,14 +361,10 @@ func (s *PoolServer) ProxyHandler(fn func(w http.ResponseWriter, r *http.Request
|
|||||||
return func(cartId CartId, w http.ResponseWriter, r *http.Request) error {
|
return func(cartId CartId, w http.ResponseWriter, r *http.Request) error {
|
||||||
if ownerHost, ok := s.pool.OwnerHost(uint64(cartId)); ok {
|
if ownerHost, ok := s.pool.OwnerHost(uint64(cartId)); ok {
|
||||||
handled, err := ownerHost.Proxy(uint64(cartId), w, r)
|
handled, err := ownerHost.Proxy(uint64(cartId), w, r)
|
||||||
if err != nil {
|
if err == nil && handled {
|
||||||
log.Printf("proxy failed: %v, taking ownership", err)
|
|
||||||
s.pool.TakeOwnership(uint64(cartId))
|
|
||||||
} else if handled {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Local ownership or no owner known, proceed with local handling
|
|
||||||
|
|
||||||
return fn(w, r, cartId)
|
return fn(w, r, cartId)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user