-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial node host services impl for key/value
- Loading branch information
Showing
8 changed files
with
645 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package nexnode | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
"strings" | ||
|
||
"github.com/nats-io/nats.go" | ||
"github.com/synadia-io/nex/internal/node/services" | ||
hostservices "github.com/synadia-io/nex/internal/node/services/lib" | ||
) | ||
|
||
const hostServiceHTTP = "http" | ||
const hostServiceKeyValue = "kv" | ||
const hostServiceMessaging = "messaging" | ||
const hostServiceObjectStore = "objectstore" | ||
|
||
// Host services server implements select functionality which is | ||
// exposed to workloads by way of the agent which makes RPC calls | ||
// via the internal NATS connection | ||
type HostServices struct { | ||
log *slog.Logger | ||
mgr *MachineManager | ||
nc *nats.Conn | ||
ncint *nats.Conn | ||
|
||
http services.HostService | ||
kv services.HostService | ||
messaging services.HostService | ||
object services.HostService | ||
} | ||
|
||
func NewHostServices(mgr *MachineManager, nc, ncint *nats.Conn, log *slog.Logger) *HostServices { | ||
return &HostServices{ | ||
log: log, | ||
mgr: mgr, | ||
nc: nc, | ||
ncint: ncint, | ||
} | ||
} | ||
|
||
func (h *HostServices) init() error { | ||
var err error | ||
|
||
h.http, err = hostservices.NewHTTPService(h.nc, h.log) | ||
if err != nil { | ||
h.log.Error(fmt.Sprintf("failed to initialize http host service: %s", err.Error())) | ||
return err | ||
} else { | ||
h.log.Debug("initialized http host service") | ||
} | ||
|
||
h.kv, err = hostservices.NewKeyValueService(h.nc, h.log) | ||
if err != nil { | ||
h.log.Error(fmt.Sprintf("failed to initialize key/value host service: %s", err.Error())) | ||
return err | ||
} else { | ||
h.log.Debug("initialized key/value host service") | ||
} | ||
|
||
h.messaging, err = hostservices.NewMessagingService(h.nc, h.log) | ||
if err != nil { | ||
h.log.Error(fmt.Sprintf("failed to initialize messaging host service: %s", err.Error())) | ||
return err | ||
} else { | ||
h.log.Debug("initialized messaging host service") | ||
} | ||
|
||
h.object, err = hostservices.NewObjectStoreService(h.nc, h.log) | ||
if err != nil { | ||
h.log.Error(fmt.Sprintf("failed to initialize object store host service: %s", err.Error())) | ||
return err | ||
} else { | ||
h.log.Debug("initialized object store host service") | ||
} | ||
|
||
// agentint.{vmID}.rpc.{namespace}.{service}.{method} | ||
_, err = h.ncint.Subscribe("agentint.*.rpc.*.*.*", h.handleRPC) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (h *HostServices) handleRPC(msg *nats.Msg) { | ||
// agentint.{vmID}.rpc.{namespace}.{service}.{method} | ||
tokens := strings.Split(msg.Subject, ".") | ||
vmID := tokens[1] | ||
namespace := tokens[3] | ||
service := tokens[4] | ||
method := tokens[5] | ||
|
||
_, ok := h.mgr.allVMs[vmID] | ||
if !ok { | ||
h.log.Warn("Received a host services RPC request from an unknown VM.") | ||
resp, _ := json.Marshal(map[string]interface{}{ | ||
"error": "unknown vm", | ||
}) | ||
|
||
err := msg.Respond(resp) | ||
if err != nil { | ||
h.log.Error(fmt.Sprintf("failed to respond to host services RPC request: %s", err.Error())) | ||
} | ||
return | ||
} | ||
|
||
h.log.Debug("Received host services RPC request", | ||
slog.String("vmid", vmID), | ||
slog.String("namespace", namespace), | ||
slog.String("service", service), | ||
slog.String("method", method), | ||
slog.Int("payload_size", len(msg.Data)), | ||
) | ||
|
||
switch service { | ||
case hostServiceHTTP: | ||
h.http.HandleRPC(msg) | ||
case hostServiceKeyValue: | ||
h.kv.HandleRPC(msg) | ||
case hostServiceMessaging: | ||
h.messaging.HandleRPC(msg) | ||
case hostServiceObjectStore: | ||
h.object.HandleRPC(msg) | ||
default: | ||
h.log.Warn("Received invalid host services RPC request", | ||
slog.String("service", service), | ||
slog.String("method", method), | ||
) | ||
|
||
resp, _ := json.Marshal(map[string]interface{}{ | ||
"error": "invalid rpc request", | ||
}) | ||
|
||
err := msg.Respond(resp) | ||
if err != nil { | ||
h.log.Error(fmt.Sprintf("failed to respond to host services RPC request: %s", err.Error())) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package services | ||
|
||
import "github.com/nats-io/nats.go" | ||
|
||
type HostService interface { | ||
HandleRPC(msg *nats.Msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package lib | ||
|
||
import ( | ||
"log/slog" | ||
"strings" | ||
|
||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
// HTTP client operations available: | ||
// Request (payload contains method, headers, etc) | ||
|
||
type HTTPService struct { | ||
log *slog.Logger | ||
nc *nats.Conn | ||
} | ||
|
||
func NewHTTPService(nc *nats.Conn, log *slog.Logger) (*HTTPService, error) { | ||
http := &HTTPService{ | ||
log: log, | ||
nc: nc, | ||
} | ||
|
||
err := http.init() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return http, nil | ||
} | ||
|
||
func (h *HTTPService) init() error { | ||
return nil | ||
} | ||
|
||
func (h *HTTPService) HandleRPC(msg *nats.Msg) { | ||
// agentint.{vmID}.rpc.{namespace}.{service}.{method} | ||
tokens := strings.Split(msg.Subject, ".") | ||
service := tokens[4] | ||
method := tokens[5] | ||
|
||
switch method { | ||
default: | ||
h.log.Warn("Received invalid host services RPC request", | ||
slog.String("service", service), | ||
slog.String("method", method), | ||
) | ||
|
||
// msg.Respond() | ||
} | ||
} |
Oops, something went wrong.