Skip to content

Commit

Permalink
feat(agent): register target
Browse files Browse the repository at this point in the history
  • Loading branch information
mcharytoniuk committed May 13, 2024
1 parent f798766 commit 622c7a7
Show file tree
Hide file tree
Showing 16 changed files with 319 additions and 115 deletions.
11 changes: 11 additions & 0 deletions agent/AgentConfiguration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package agent

import "time"

type AgentConfiguration struct {
ReportingIntervalMiliseconds uint
}

func (self *AgentConfiguration) GetReportingIntervalDuration() time.Duration {
return time.Duration(self.ReportingIntervalMiliseconds) * time.Millisecond
}
42 changes: 42 additions & 0 deletions agent/LlamaCppObserver.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
package agent

import (
"time"

"github.com/distantmagic/paddler/goroutine"
"github.com/distantmagic/paddler/llamacpp"
"github.com/distantmagic/paddler/management"
"github.com/hashicorp/go-hclog"
)

type LlamaCppObserver struct {
AgentConfiguration *AgentConfiguration
LlamaCppClient *llamacpp.LlamaCppClient
Logger hclog.Logger
ManagementClient *management.Client
}

func (self *LlamaCppObserver) ObserveAndReport(
serverEventsChannel chan goroutine.ResultMessage,
) {
llamaCppHealthStatusChannel := make(chan llamacpp.LlamaCppHealthStatus)

defer close(llamaCppHealthStatusChannel)

ticker := time.NewTicker(self.AgentConfiguration.GetReportingIntervalDuration())

go self.RunTickerInterval(llamaCppHealthStatusChannel, ticker)

for llamaCppHealthStatus := range llamaCppHealthStatusChannel {
go self.ManagementClient.ReportLlamaCppHealthStatus(
serverEventsChannel,
self.LlamaCppClient.LlamaCppConfiguration,
&llamaCppHealthStatus,
)
}
}

func (self *LlamaCppObserver) RunTickerInterval(
llamaCppHealthStatusChannel chan llamacpp.LlamaCppHealthStatus,
ticker *time.Ticker,
) {
for range ticker.C {
go self.LlamaCppClient.GetHealth(llamaCppHealthStatusChannel)
}
}
37 changes: 34 additions & 3 deletions cmd/Agent.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
package cmd

import (
"net/http"

"github.com/distantmagic/paddler/agent"
"github.com/distantmagic/paddler/goroutine"
"github.com/distantmagic/paddler/llamacpp"
"github.com/distantmagic/paddler/management"
"github.com/distantmagic/paddler/reverseproxy"
"github.com/hashicorp/go-hclog"
"github.com/urfave/cli/v2"
)

type Agent struct {
Logger hclog.Logger
LlamaCppConfiguration *llamacpp.LlamaCppConfiguration
ReverseProxyConfiguration *reverseproxy.ReverseProxyConfiguration
AgentConfiguration *agent.AgentConfiguration
Logger hclog.Logger
LlamaCppConfiguration *llamacpp.LlamaCppConfiguration
ManagementServerConfiguration *management.ManagementServerConfiguration
ReverseProxyConfiguration *reverseproxy.ReverseProxyConfiguration
}

func (self *Agent) Action(cliContext *cli.Context) error {
serverEventsChannel := make(chan goroutine.ResultMessage)

llamaCppObserver := &agent.LlamaCppObserver{
AgentConfiguration: self.AgentConfiguration,
LlamaCppClient: &llamacpp.LlamaCppClient{
HttpClient: http.DefaultClient,
LlamaCppConfiguration: self.LlamaCppConfiguration,
},
Logger: self.Logger.Named("LlamaCppObserver"),
ManagementClient: &management.Client{
HttpClient: http.DefaultClient,
ManagementServerConfiguration: self.ManagementServerConfiguration,
},
}

go llamaCppObserver.ObserveAndReport(serverEventsChannel)

for serverEvent := range serverEventsChannel {
self.Logger.Info(
"server event",
"event", serverEvent,
)
}

return nil
}
7 changes: 4 additions & 3 deletions cmd/Balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (self *Balancer) Action(cliContext *cli.Context) error {
},
}

reverseProxyServer := &reverseproxy.Server{
reverseProxyServer := &loadbalancer.ReverseProxyServer{
LoadBalancer: loadBalancer,
Logger: self.Logger.Named("reverseproxy"),
ReverseProxyConfiguration: self.ReverseProxyConfiguration,
Expand All @@ -47,8 +47,9 @@ func (self *Balancer) Action(cliContext *cli.Context) error {
go reverseProxyServer.Serve(serverEventsChannel)

for serverEvent := range serverEventsChannel {
self.Logger.Info(
"server",
self.Logger.Log(
hclog.Info,
"server event",
"event", serverEvent,
)
}
Expand Down
2 changes: 0 additions & 2 deletions llamacpp/LlamaCppClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ type LlamaCppClient struct {
func (self *LlamaCppClient) GetHealth(
responseChannel chan LlamaCppHealthStatus,
) {
defer close(responseChannel)

request, err := http.NewRequest(
"GET",
self.LlamaCppConfiguration.HttpAddress.BuildUrlWithPath("health").String(),
Expand Down
2 changes: 2 additions & 0 deletions llamacpp/LlamaCppClient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var llamaCppClient *LlamaCppClient = &LlamaCppClient{
func TestHealthIsObtained(t *testing.T) {
responseChannel := make(chan LlamaCppHealthStatus)

defer close(responseChannel)

go llamaCppClient.GetHealth(responseChannel)

healthStatus := <-responseChannel
Expand Down
2 changes: 1 addition & 1 deletion loadbalancer/LlamaCppTargetConfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package loadbalancer
import "github.com/distantmagic/paddler/llamacpp"

type LlamaCppTargetConfiguration struct {
LlamaCppConfiguration *llamacpp.LlamaCppConfiguration
LlamaCppConfiguration *llamacpp.LlamaCppConfiguration `json:"llama_cpp_configuration"`
}
2 changes: 1 addition & 1 deletion loadbalancer/LoadBalancerStatus.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package loadbalancer

type LoadBalancerStatus struct {
RegisteredTargets int
RegisteredTargets int `json:"registred_targets"`
}
56 changes: 56 additions & 0 deletions loadbalancer/ReverseProxyServer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package loadbalancer

import (
"net/http"
"net/http/httputil"

"github.com/distantmagic/paddler/goroutine"
"github.com/distantmagic/paddler/reverseproxy"
"github.com/hashicorp/go-hclog"
)

type ReverseProxyServer struct {
LoadBalancer *LoadBalancer
Logger hclog.Logger
ReverseProxyConfiguration *reverseproxy.ReverseProxyConfiguration
}

func (self *ReverseProxyServer) Serve(serverEventsChannel chan goroutine.ResultMessage) {
self.Logger.Debug(
"listen",
"host", self.ReverseProxyConfiguration.HttpAddress.GetHostWithPort(),
)

reverseProxy := &httputil.ReverseProxy{
ErrorLog: self.Logger.Named("ReverseProxy").StandardLogger(&hclog.StandardLoggerOptions{
InferLevels: true,
}),
Rewrite: func(request *httputil.ProxyRequest) {
targetUrl, err := self.LoadBalancer.Balance(request.In)

if err != nil {
serverEventsChannel <- goroutine.ResultMessage{
Comment: "failed to balance request",
Error: err,
}

return
}

request.SetURL(targetUrl)
request.SetXForwarded()
},
}

err := http.ListenAndServe(
self.ReverseProxyConfiguration.HttpAddress.GetHostWithPort(),
reverseProxy,
)

if err != nil {
serverEventsChannel <- goroutine.ResultMessage{
Comment: "failed to listen",
Error: err,
}
}
}
62 changes: 47 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"os"

"github.com/distantmagic/paddler/agent"
"github.com/distantmagic/paddler/cmd"
"github.com/distantmagic/paddler/llamacpp"
"github.com/distantmagic/paddler/management"
Expand All @@ -12,17 +13,28 @@ import (
"github.com/urfave/cli/v2"
)

const (
DefaultManagementHost = "127.0.0.1"
DefaultManagementPort = 8085
DefaultManagementScheme = "http"
)

func main() {
logger := hclog.New(&hclog.LoggerOptions{
Name: "paddler",
Name: "paddler",
// JSONFormat: true,
Level: hclog.Debug,
})

agent := &cmd.Agent{
Logger: logger.Named("Agent"),
AgentConfiguration: &agent.AgentConfiguration{},
LlamaCppConfiguration: &llamacpp.LlamaCppConfiguration{
HttpAddress: &netcfg.HttpAddressConfiguration{},
},
Logger: logger.Named("Agent"),
ManagementServerConfiguration: &management.ManagementServerConfiguration{
HttpAddress: &netcfg.HttpAddressConfiguration{},
},
ReverseProxyConfiguration: &reverseproxy.ReverseProxyConfiguration{
HttpAddress: &netcfg.HttpAddressConfiguration{},
},
Expand All @@ -47,20 +59,25 @@ func main() {
Usage: "start llama.cpp observer agent",
Action: agent.Action,
Flags: []cli.Flag{
&cli.UintFlag{
Name: "agent-reporting-interval-miliseconds",
Value: 1000,
Destination: &agent.AgentConfiguration.ReportingIntervalMiliseconds,
},
&cli.StringFlag{
Name: "balancer-host",
Value: "127.0.0.1",
Destination: &agent.ReverseProxyConfiguration.HttpAddress.Host,
Name: "management-host",
Value: DefaultManagementHost,
Destination: &agent.ManagementServerConfiguration.HttpAddress.Host,
},
&cli.UintFlag{
Name: "balancer-port",
Value: 8085,
Destination: &agent.ReverseProxyConfiguration.HttpAddress.Port,
Name: "management-port",
Value: DefaultManagementPort,
Destination: &agent.ManagementServerConfiguration.HttpAddress.Port,
},
&cli.StringFlag{
Name: "balancer-scheme",
Value: "http",
Destination: &agent.ReverseProxyConfiguration.HttpAddress.Scheme,
Name: "management-scheme",
Value: DefaultManagementScheme,
Destination: &agent.ManagementServerConfiguration.HttpAddress.Scheme,
},
&cli.StringFlag{
Name: "llamacpp-host",
Expand All @@ -69,14 +86,29 @@ func main() {
},
&cli.UintFlag{
Name: "llamacpp-port",
Value: 8080,
Value: 8088,
Destination: &agent.LlamaCppConfiguration.HttpAddress.Port,
},
&cli.StringFlag{
Name: "llamacpp-scheme",
Value: "http",
Destination: &agent.LlamaCppConfiguration.HttpAddress.Scheme,
},
&cli.StringFlag{
Name: "reverseproxy-host",
Value: "127.0.0.1",
Destination: &balancer.ReverseProxyConfiguration.HttpAddress.Host,
},
&cli.UintFlag{
Name: "reverseproxy-port",
Value: 8086,
Destination: &balancer.ReverseProxyConfiguration.HttpAddress.Port,
},
&cli.StringFlag{
Name: "reverseproxy-scheme",
Value: "http",
Destination: &balancer.ReverseProxyConfiguration.HttpAddress.Scheme,
},
},
},
{
Expand All @@ -86,17 +118,17 @@ func main() {
Flags: []cli.Flag{
&cli.StringFlag{
Name: "management-host",
Value: "127.0.0.1",
Value: DefaultManagementHost,
Destination: &balancer.ManagementServerConfiguration.HttpAddress.Host,
},
&cli.UintFlag{
Name: "management-port",
Value: 8085,
Value: DefaultManagementPort,
Destination: &balancer.ManagementServerConfiguration.HttpAddress.Port,
},
&cli.StringFlag{
Name: "management-scheme",
Value: "http",
Value: DefaultManagementScheme,
Destination: &balancer.ManagementServerConfiguration.HttpAddress.Scheme,
},
&cli.StringFlag{
Expand Down
Loading

0 comments on commit 622c7a7

Please sign in to comment.