Refactor instance node handling to use a map

This commit is contained in:
2025-10-18 00:33:16 +02:00
parent 7bf0809122
commit 113b51eda2
7 changed files with 83 additions and 31 deletions

View File

@@ -193,8 +193,10 @@ func (i *Instance) GetProxy() (*httputil.ReverseProxy, error) {
// Remote instances should not use local proxy - they are handled by RemoteInstanceProxy
opts := i.GetOptions()
if opts != nil && len(opts.Nodes) > 0 && opts.Nodes[0] != i.localNodeName {
return nil, fmt.Errorf("instance %s is a remote instance and should not use local proxy", i.Name)
if opts != nil && len(opts.Nodes) > 0 {
if _, isLocal := opts.Nodes[i.localNodeName]; !isLocal {
return nil, fmt.Errorf("instance %s is a remote instance and should not use local proxy", i.Name)
}
}
return i.proxy.GetProxy()
@@ -303,8 +305,8 @@ func (i *Instance) IsRemote() bool {
return false
}
// If the first node is the local node, treat it as a local instance
if opts.Nodes[0] == i.localNodeName {
// If the local node is in the nodes map, treat it as a local instance
if _, isLocal := opts.Nodes[i.localNodeName]; isLocal {
return false
}

View File

@@ -209,7 +209,7 @@ func TestSetOptions_PreservesNodes(t *testing.T) {
// Create instance with initial nodes
initialOptions := &instance.Options{
BackendType: backends.BackendTypeLlamaCpp,
Nodes: []string{"worker1"},
Nodes: map[string]struct{}{"worker1": {}},
LlamaServerOptions: &llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf",
Port: 8080,
@@ -222,7 +222,7 @@ func TestSetOptions_PreservesNodes(t *testing.T) {
// Try to update with different nodes
updatedOptions := &instance.Options{
BackendType: backends.BackendTypeLlamaCpp,
Nodes: []string{"worker2"}, // Attempt to change node
Nodes: map[string]struct{}{"worker2": {}}, // Attempt to change node
LlamaServerOptions: &llamacpp.LlamaServerOptions{
Model: "/path/to/new-model.gguf",
Port: 8081,
@@ -233,8 +233,8 @@ func TestSetOptions_PreservesNodes(t *testing.T) {
opts := inst.GetOptions()
// Nodes should remain unchanged
if len(opts.Nodes) != 1 || opts.Nodes[0] != "worker1" {
t.Errorf("Expected nodes to remain ['worker1'], got %v", opts.Nodes)
if _, exists := opts.Nodes["worker1"]; len(opts.Nodes) != 1 || !exists {
t.Errorf("Expected nodes to contain 'worker1', got %v", opts.Nodes)
}
// Other options should be updated

View File

@@ -10,6 +10,7 @@ import (
"llamactl/pkg/config"
"log"
"maps"
"slices"
"sync"
)
@@ -29,7 +30,7 @@ type Options struct {
BackendType backends.BackendType `json:"backend_type"`
BackendOptions map[string]any `json:"backend_options,omitempty"`
Nodes []string `json:"nodes,omitempty"`
Nodes map[string]struct{} `json:"-"`
// Backend-specific options
LlamaServerOptions *llamacpp.LlamaServerOptions `json:"-"`
@@ -87,6 +88,7 @@ func (c *Options) UnmarshalJSON(data []byte) error {
// Use anonymous struct to avoid recursion
type Alias Options
aux := &struct {
Nodes []string `json:"nodes,omitempty"` // Accept JSON array
*Alias
}{
Alias: (*Alias)(c),
@@ -96,6 +98,14 @@ func (c *Options) UnmarshalJSON(data []byte) error {
return err
}
// Convert nodes array to map
if len(aux.Nodes) > 0 {
c.Nodes = make(map[string]struct{}, len(aux.Nodes))
for _, node := range aux.Nodes {
c.Nodes[node] = struct{}{}
}
}
// Parse backend-specific options
switch c.BackendType {
case backends.BackendTypeLlamaCpp:
@@ -147,11 +157,22 @@ func (c *Options) MarshalJSON() ([]byte, error) {
// Use anonymous struct to avoid recursion
type Alias Options
aux := struct {
Nodes []string `json:"nodes,omitempty"` // Output as JSON array
*Alias
}{
Alias: (*Alias)(c),
}
// Convert nodes map to array (sorted for consistency)
if len(c.Nodes) > 0 {
aux.Nodes = make([]string, 0, len(c.Nodes))
for node := range c.Nodes {
aux.Nodes = append(aux.Nodes, node)
}
// Sort for consistent output
slices.Sort(aux.Nodes)
}
// Convert backend-specific options back to BackendOptions map for JSON
switch c.BackendType {
case backends.BackendTypeLlamaCpp: