Move accountant to it's own deployment using gRCP

This commit is contained in:
Marc Di Luzio 2020-06-10 23:23:09 +01:00
parent 8f25f55658
commit 99da6c5d67
14 changed files with 868 additions and 64 deletions

View file

@ -0,0 +1,11 @@
FROM golang:latest
LABEL maintainer="Marc Di Luzio <marc.diluzio@gmail.com>"
WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o rove-accountant -ldflags="-X 'github.com/mdiluz/rove/pkg/version.Version=$(git describe --always --long --dirty --tags)'" cmd/rove-accountant/main.go
CMD [ "./rove-accountant" ]

View file

@ -0,0 +1,76 @@
package internal
import (
"fmt"
)
const kAccountsFileName = "rove-accounts.json"
// Account represents a registered user
type Account struct {
// Name simply describes the account and must be unique
Name string `json:"name"`
// Data represents internal account data
Data map[string]string `json:"data"`
}
// Represents the accountant data to store
type accountantData struct {
}
// Accountant manages a set of accounts
type Accountant struct {
Accounts map[string]Account `json:"accounts"`
}
// NewAccountant creates a new accountant
func NewAccountant() *Accountant {
return &Accountant{
Accounts: make(map[string]Account),
}
}
// RegisterAccount adds an account to the set of internal accounts
func (a *Accountant) RegisterAccount(name string) (acc Account, err error) {
// Set the account name
acc.Name = name
acc.Data = make(map[string]string)
// Verify this acount isn't already registered
for _, a := range a.Accounts {
if a.Name == acc.Name {
return Account{}, fmt.Errorf("Account name already registered")
}
}
// Simply add the account to the map
a.Accounts[acc.Name] = acc
return
}
// AssignRover assigns data to an account
func (a *Accountant) AssignData(account string, key string, value string) error {
// Find the account matching the ID
if this, ok := a.Accounts[account]; ok {
this.Data[key] = value
a.Accounts[account] = this
} else {
return fmt.Errorf("no account found for id: %s", account)
}
return nil
}
// GetRover gets the rover rover for the account
func (a *Accountant) GetValue(account string, key string) (string, error) {
// Find the account matching the ID
if this, ok := a.Accounts[account]; !ok {
return "", fmt.Errorf("no account found for id: %s", account)
} else {
return this.Data[key], nil
}
}

View file

@ -0,0 +1,66 @@
package internal
import (
"testing"
"github.com/google/uuid"
)
func TestNewAccountant(t *testing.T) {
// Very basic verify here for now
accountant := NewAccountant()
if accountant == nil {
t.Error("Failed to create accountant")
}
}
func TestAccountant_RegisterAccount(t *testing.T) {
accountant := NewAccountant()
// Start by making two accounts
namea := uuid.New().String()
acca, err := accountant.RegisterAccount(namea)
if err != nil {
t.Error(err)
} else if acca.Name != namea {
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", namea, acca.Name)
}
nameb := uuid.New().String()
accb, err := accountant.RegisterAccount(nameb)
if err != nil {
t.Error(err)
} else if accb.Name != nameb {
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", nameb, acca.Name)
}
// Verify another request gets rejected
_, err = accountant.RegisterAccount(namea)
if err == nil {
t.Error("Duplicate account name did not produce error")
}
}
func TestAccountant_AssignGetData(t *testing.T) {
accountant := NewAccountant()
if len(accountant.Accounts) != 0 {
t.Error("New accountant created with non-zero account number")
}
name := uuid.New().String()
a, err := accountant.RegisterAccount(name)
if err != nil {
t.Error(err)
}
err = accountant.AssignData(a.Name, "key", "value")
if err != nil {
t.Error("Failed to set data for created account")
} else if id, err := accountant.GetValue(a.Name, "key"); err != nil {
t.Error("Failed to get data for account")
} else if id != "value" {
t.Error("Fetched data is incorrect for account")
}
}

130
cmd/rove-accountant/main.go Normal file
View file

@ -0,0 +1,130 @@
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"sync"
"syscall"
"github.com/mdiluz/rove/cmd/rove-accountant/internal"
"github.com/mdiluz/rove/pkg/accounts"
"github.com/mdiluz/rove/pkg/persistence"
"google.golang.org/grpc"
)
var address = flag.String("address", "", "port to server the accountant on")
var data = flag.String("data", os.TempDir(), "path to persistent data")
// accountantServer is the internal object to manage the requests
type accountantServer struct {
accountant *internal.Accountant
sync sync.RWMutex
}
// Register will register an account
func (a *accountantServer) Register(ctx context.Context, in *accounts.RegisterInfo) (*accounts.RegisterResponse, error) {
a.sync.Lock()
defer a.sync.Unlock()
// Try and register the account itself
fmt.Printf("Registering account: %s\n", in.Name)
if _, err := a.accountant.RegisterAccount(in.Name); err != nil {
fmt.Printf("Error: %s\n", err)
return &accounts.RegisterResponse{Success: false, Error: fmt.Sprintf("error registering account: %s", err)}, nil
}
// Save out the accounts
if err := persistence.Save("accounts", a.accountant); err != nil {
fmt.Printf("Error: %s\n", err)
return &accounts.RegisterResponse{Success: false, Error: fmt.Sprintf("failed to save accounts: %s", err)}, nil
}
return &accounts.RegisterResponse{Success: true}, nil
}
// AssignData assigns a key value pair to an account
func (a *accountantServer) AssignValue(_ context.Context, in *accounts.DataKeyValue) (*accounts.Response, error) {
a.sync.RLock()
defer a.sync.RUnlock()
// Try and assign the data
fmt.Printf("Assigning value for account %s: %s->%s\n", in.Account, in.Key, in.Value)
err := a.accountant.AssignData(in.Account, in.Key, in.Value)
if err != nil {
fmt.Printf("Error: %s\n", err)
return &accounts.Response{Success: false, Error: err.Error()}, nil
}
return &accounts.Response{Success: true}, nil
}
// GetData gets the value for a key
func (a *accountantServer) GetValue(_ context.Context, in *accounts.DataKey) (*accounts.DataResponse, error) {
a.sync.RLock()
defer a.sync.RUnlock()
// Try and fetch the rover
fmt.Printf("Getting value for account %s: %s\n", in.Account, in.Key)
data, err := a.accountant.GetValue(in.Account, in.Key)
if err != nil {
fmt.Printf("Error: %s\n", err)
return &accounts.DataResponse{Success: false, Error: err.Error()}, nil
}
return &accounts.DataResponse{Success: true, Value: data}, nil
}
// main
func main() {
flag.Parse()
// Verify the input
if len(*address) == 0 {
log.Fatal("No address set with --address")
}
persistence.SetPath(*data)
// Initialise and load the accountant
accountant := &internal.Accountant{}
if err := persistence.Load("accounts", accountant); err != nil {
log.Fatalf("failed to load account data: %s", err)
}
// Set up the RPC server and register
lis, err := net.Listen("tcp", *address)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
accounts.RegisterAccountantServer(grpcServer, &accountantServer{
accountant: accountant,
})
// Set up the close handler
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("Quit requested, exiting...")
grpcServer.Stop()
}()
// Serve the RPC server
fmt.Printf("Serving accountant on %s\n", *address)
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to server gRPC: %s", err)
}
// Save out the accountant data
if err := persistence.Save("accounts", accountant); err != nil {
log.Fatalf("failed to save accounts: %s", err)
}
}

View file

@ -1,12 +1,14 @@
package internal
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/google/uuid"
"github.com/mdiluz/rove/pkg/accounts"
"github.com/mdiluz/rove/pkg/rove"
"github.com/mdiluz/rove/pkg/version"
)
@ -84,14 +86,19 @@ func HandleRegister(s *Server, vars map[string]string, b io.ReadCloser, w io.Wri
} else if len(data.Name) == 0 {
response.Error = "Cannot register empty name"
} else if acc, err := s.accountant.RegisterAccount(data.Name); err != nil {
}
reg := accounts.RegisterInfo{Name: data.Name}
if acc, err := s.accountant.Register(context.Background(), &reg); err != nil {
response.Error = err.Error()
} else if _, _, err := s.SpawnRoverForAccount(acc.Name); err != nil {
} else if !acc.Success {
response.Error = acc.Error
} else if _, _, err := s.SpawnRoverForAccount(data.Name); err != nil {
response.Error = err.Error()
} else if err := s.SaveAll(); err != nil {
response.Error = fmt.Sprintf("Internal server error when saving: %s", err)
} else if err := s.SaveWorld(); err != nil {
response.Error = fmt.Sprintf("Internal server error when saving world: %s", err)
} else {
// Save out the new accounts
@ -116,13 +123,19 @@ func HandleCommand(s *Server, vars map[string]string, b io.ReadCloser, w io.Writ
fmt.Printf("Failed to decode json: %s\n", err)
response.Error = err.Error()
} else if len(id) == 0 {
}
key := accounts.DataKey{Account: id, Key: "rover"}
if len(id) == 0 {
response.Error = "No account ID provided"
} else if inst, err := s.accountant.GetData(id, "rover"); err != nil {
} else if resp, err := s.accountant.GetValue(context.Background(), &key); err != nil {
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
} else if id, err := uuid.Parse(inst); err != nil {
} else if !resp.Success {
response.Error = resp.Error
} else if id, err := uuid.Parse(resp.Value); err != nil {
response.Error = fmt.Sprintf("Account had invalid rover id: %s", err)
} else if err := s.world.Enqueue(id, data.Commands...); err != nil {
@ -143,13 +156,17 @@ func HandleRadar(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
}
id := vars["account"]
key := accounts.DataKey{Account: id, Key: "rover"}
if len(id) == 0 {
response.Error = "No account ID provided"
} else if inst, err := s.accountant.GetData(id, "rover"); err != nil {
} else if resp, err := s.accountant.GetValue(context.Background(), &key); err != nil {
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
} else if id, err := uuid.Parse(inst); err != nil {
} else if !resp.Success {
response.Error = resp.Error
} else if id, err := uuid.Parse(resp.Value); err != nil {
response.Error = fmt.Sprintf("Account had invalid rover id: %s", err)
} else if attrib, err := s.world.RoverAttributes(id); err != nil {
@ -175,13 +192,17 @@ func HandleRover(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
}
id := vars["account"]
key := accounts.DataKey{Account: id, Key: "rover"}
if len(id) == 0 {
response.Error = "No account ID provided"
} else if inst, err := s.accountant.GetData(id, "rover"); err != nil {
} else if resp, err := s.accountant.GetValue(context.Background(), &key); err != nil {
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
} else if id, err := uuid.Parse(inst); err != nil {
} else if !resp.Success {
response.Error = resp.Error
} else if id, err := uuid.Parse(resp.Value); err != nil {
response.Error = fmt.Sprintf("Account had invalid rover id: %s", err)
} else if attribs, err := s.world.RoverAttributes(id); err != nil {

View file

@ -2,12 +2,14 @@ package internal
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path"
"testing"
"github.com/mdiluz/rove/pkg/accounts"
"github.com/mdiluz/rove/pkg/game"
"github.com/mdiluz/rove/pkg/rove"
"github.com/mdiluz/rove/pkg/vector"
@ -62,11 +64,15 @@ func TestHandleRegister(t *testing.T) {
func TestHandleCommand(t *testing.T) {
s := NewServer()
s.Initialise(false) // Leave the world empty with no obstacles
a, err := s.accountant.RegisterAccount("test")
reg := accounts.RegisterInfo{Name: "test"}
acc, err := s.accountant.Register(context.Background(), &reg)
assert.NoError(t, err)
assert.True(t, acc.Success)
assert.NoError(t, err, "Error registering account")
// Spawn the rover rover for the account
_, inst, err := s.SpawnRoverForAccount(a.Name)
_, inst, err := s.SpawnRoverForAccount("test")
assert.NoError(t, s.world.WarpRover(inst, vector.Vector{}))
attribs, err := s.world.RoverAttributes(inst)
@ -85,7 +91,7 @@ func TestHandleCommand(t *testing.T) {
b, err := json.Marshal(data)
assert.NoError(t, err, "Error marshalling data")
request, _ := http.NewRequest(http.MethodPost, path.Join("/", a.Name, "/command"), bytes.NewReader(b))
request, _ := http.NewRequest(http.MethodPost, path.Join("/", "test", "/command"), bytes.NewReader(b))
response := httptest.NewRecorder()
s.router.ServeHTTP(response, request)
@ -114,11 +120,14 @@ func TestHandleCommand(t *testing.T) {
func TestHandleRadar(t *testing.T) {
s := NewServer()
s.Initialise(false) // Spawn a clean world
a, err := s.accountant.RegisterAccount("test")
reg := accounts.RegisterInfo{Name: "test"}
acc, err := s.accountant.Register(context.Background(), &reg)
assert.NoError(t, err)
assert.True(t, acc.Success)
assert.NoError(t, err, "Error registering account")
// Spawn the rover rover for the account
attrib, id, err := s.SpawnRoverForAccount(a.Name)
attrib, id, err := s.SpawnRoverForAccount("test")
assert.NoError(t, err)
// Warp this rover to 0,0
@ -134,7 +143,7 @@ func TestHandleRadar(t *testing.T) {
assert.NoError(t, s.world.Atlas.SetTile(rockPos, game.TileRock))
assert.NoError(t, s.world.Atlas.SetTile(emptyPos, game.TileEmpty))
request, _ := http.NewRequest(http.MethodGet, path.Join("/", a.Name, "/radar"), nil)
request, _ := http.NewRequest(http.MethodGet, path.Join("/", "test", "/radar"), nil)
response := httptest.NewRecorder()
s.router.ServeHTTP(response, request)
@ -168,14 +177,16 @@ func TestHandleRadar(t *testing.T) {
func TestHandleRover(t *testing.T) {
s := NewServer()
s.Initialise(true)
a, err := s.accountant.RegisterAccount("test")
assert.NoError(t, err, "Error registering account")
reg := accounts.RegisterInfo{Name: "test"}
acc, err := s.accountant.Register(context.Background(), &reg)
assert.NoError(t, err)
assert.True(t, acc.Success)
// Spawn one rover for the account
attribs, _, err := s.SpawnRoverForAccount(a.Name)
attribs, _, err := s.SpawnRoverForAccount("test")
assert.NoError(t, err)
request, _ := http.NewRequest(http.MethodGet, path.Join("/", a.Name, "/rover"), nil)
request, _ := http.NewRequest(http.MethodGet, path.Join("/", "test", "/rover"), nil)
response := httptest.NewRecorder()
s.router.ServeHTTP(response, request)

View file

@ -3,6 +3,7 @@ package internal
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net"
@ -16,8 +17,11 @@ import (
"github.com/mdiluz/rove/pkg/game"
"github.com/mdiluz/rove/pkg/persistence"
"github.com/robfig/cron"
"google.golang.org/grpc"
)
var accountantAddress = flag.String("accountant", "", "address of the accountant to connect to")
const (
// PersistentData will allow the server to load and save it's state
PersistentData = iota
@ -30,8 +34,11 @@ const (
type Server struct {
// Internal state
accountant *accounts.Accountant
world *game.World
world *game.World
// Accountant server
accountant accounts.AccountantClient
clientConn *grpc.ClientConn
// HTTP server
listener net.Listener
@ -96,9 +103,6 @@ func NewServer(opts ...ServerOption) *Server {
// Set up the server object
s.server = &http.Server{Addr: s.address, Handler: s.router}
// Create the accountant
s.accountant = accounts.NewAccountant()
// Start small, we can grow the world later
s.world = game.NewWorld(4, 8)
@ -111,13 +115,21 @@ func (s *Server) Initialise(fillWorld bool) (err error) {
// Add to our sync
s.sync.Add(1)
// Connect to the accountant
fmt.Printf("Dialing accountant on %s\n", *accountantAddress)
clientConn, err := grpc.Dial(*accountantAddress, grpc.WithInsecure())
if err != nil {
return err
}
s.accountant = accounts.NewAccountantClient(clientConn)
// Spawn a border on the default world
if err := s.world.SpawnWorld(fillWorld); err != nil {
return err
}
// Load the accounts if requested
if err := s.LoadAll(); err != nil {
// Load the world file
if err := s.LoadWorld(); err != nil {
return err
}
@ -183,6 +195,11 @@ func (s *Server) Stop() error {
return err
}
// Close the accountant connection
if err := s.clientConn.Close(); err != nil {
return err
}
return nil
}
@ -192,7 +209,7 @@ func (s *Server) Close() error {
s.sync.Wait()
// Save and return
return s.SaveAll()
return s.SaveWorld()
}
// Close waits until the server is finished and closes up shop
@ -218,36 +235,12 @@ func (s *Server) SaveWorld() error {
return nil
}
// SaveAccounts will save out the accounts file
func (s *Server) SaveAccounts() error {
if s.persistence == PersistentData {
if err := persistence.SaveAll("accounts", s.accountant); err != nil {
return fmt.Errorf("failed to save out persistent data: %s", err)
}
}
return nil
}
// SaveAll will save out all server files
func (s *Server) SaveAll() error {
// Save the accounts if requested
if s.persistence == PersistentData {
s.world.RLock()
defer s.world.RUnlock()
if err := persistence.SaveAll("accounts", s.accountant, "world", s.world); err != nil {
return err
}
}
return nil
}
// LoadAll will load all persistent data
func (s *Server) LoadAll() error {
// LoadWorld will load all persistent data
func (s *Server) LoadWorld() error {
if s.persistence == PersistentData {
s.world.Lock()
defer s.world.Unlock()
if err := persistence.LoadAll("accounts", &s.accountant, "world", &s.world); err != nil {
if err := persistence.LoadAll("world", &s.world); err != nil {
return err
}
}
@ -289,7 +282,11 @@ func (s *Server) SpawnRoverForAccount(account string) (game.RoverAttributes, uui
return game.RoverAttributes{}, uuid.UUID{}, fmt.Errorf("No attributes found for created rover: %s", err)
} else {
if err := s.accountant.AssignData(account, "rover", inst.String()); err != nil {
keyval := accounts.DataKeyValue{Account: account, Key: "rover", Value: inst.String()}
resp, err := s.accountant.AssignValue(context.Background(), &keyval)
if err != nil || !resp.Success {
fmt.Printf("Failed to assign rover to account, %s, %s", err, resp.Error)
// Try and clear up the rover
if err := s.world.DestroyRover(inst); err != nil {
fmt.Printf("Failed to destroy rover after failed rover assign: %s", err)

View file

@ -113,6 +113,9 @@ func InnerMain(command string) error {
}
case "register":
if len(*name) == 0 {
return fmt.Errorf("must set name with -name")
}
d := rove.RegisterData{
Name: *name,
}
@ -125,8 +128,8 @@ func InnerMain(command string) error {
return fmt.Errorf("Server returned failure: %s", response.Error)
default:
fmt.Printf("Registered account with id: %s\n", d.Name)
config.Accounts[config.Host] = d.Name
fmt.Printf("Registered account with id: %s\n", *name)
config.Accounts[config.Host] = *name
}
case "move":