Feat v0.6.0 cluster registry + HA
All checks were successful
CI/CD / detect-changes (pull_request) Successful in 21s
CI/CD / test-frontend (pull_request) Successful in 5s
CI/CD / test-go-pg (pull_request) Successful in 2m58s
CI/CD / test-sqlite (pull_request) Successful in 3m2s
CI/CD / build-and-deploy (pull_request) Successful in 1m15s

PG-backed cluster registry for horizontal scaling — zero new
infrastructure (no etcd/Consul/Redis). MVP convergence point.

- node_registry UNLOGGED table (migration 013)
- Self-registration, heartbeat tick (10s), stale sweep (30s),
  self-eviction (os.Exit on 0 rows → K8s restarts)
- GET /api/v1/admin/cluster returns nodes with runtime stats
- Health endpoints include node_id + cluster size/peers
- cluster-dashboard admin surface with auto-refresh
- 3-replica E2E test (docker-compose-e2e.yml + ci/e2e-cluster-test.sh)
- chat + cluster-dashboard added to curated default bundle
- 5 new tests (3 unit + 2 handler), all passing

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-30 22:20:56 +00:00
parent 768f15b3cd
commit 35d2309450
21 changed files with 1101 additions and 19 deletions

View File

@@ -0,0 +1,32 @@
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"switchboard-core/store"
)
// ClusterHandler serves the admin cluster API.
type ClusterHandler struct {
stores store.Stores
}
func NewClusterHandler(stores store.Stores) *ClusterHandler {
return &ClusterHandler{stores: stores}
}
// ListNodes returns all registered cluster nodes.
// GET /api/v1/admin/cluster
func (h *ClusterHandler) ListNodes(c *gin.Context) {
nodes, err := h.stores.Cluster.ListNodes(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list cluster nodes"})
return
}
if nodes == nil {
nodes = []store.ClusterNode{}
}
c.JSON(http.StatusOK, gin.H{"data": nodes})
}

View File

@@ -0,0 +1,114 @@
package handlers
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
"switchboard-core/store"
)
// mockClusterStore implements store.ClusterStore for handler tests.
type mockClusterStore struct {
nodes []store.ClusterNode
}
func (m *mockClusterStore) Register(_ context.Context, _, _ string) error { return nil }
func (m *mockClusterStore) Heartbeat(_ context.Context, _ string, _ json.RawMessage) (int64, error) {
return 1, nil
}
func (m *mockClusterStore) SweepStale(_ context.Context, _ time.Duration) (int64, error) { return 0, nil }
func (m *mockClusterStore) ListNodes(_ context.Context) ([]store.ClusterNode, error) {
return m.nodes, nil
}
func (m *mockClusterStore) Deregister(_ context.Context, _ string) error { return nil }
func TestClusterListNodes(t *testing.T) {
gin.SetMode(gin.TestMode)
mock := &mockClusterStore{
nodes: []store.ClusterNode{
{
NodeID: "node-1",
Endpoint: "http://node-1:8080",
Seq: 1,
RegisteredAt: time.Now(),
Heartbeat: time.Now(),
Stats: json.RawMessage(`{"ws_clients":3}`),
},
{
NodeID: "node-2",
Endpoint: "http://node-2:8080",
Seq: 2,
RegisteredAt: time.Now(),
Heartbeat: time.Now(),
Stats: json.RawMessage(`{"ws_clients":7}`),
},
},
}
stores := store.Stores{Cluster: mock}
h := NewClusterHandler(stores)
r := gin.New()
r.GET("/api/v1/admin/cluster", h.ListNodes)
req := httptest.NewRequest(http.MethodGet, "/api/v1/admin/cluster", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", w.Code, http.StatusOK)
}
var resp struct {
Data []store.ClusterNode `json:"data"`
}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("unmarshal response: %v", err)
}
if len(resp.Data) != 2 {
t.Fatalf("data length = %d, want 2", len(resp.Data))
}
if resp.Data[0].NodeID != "node-1" {
t.Errorf("data[0].node_id = %q, want %q", resp.Data[0].NodeID, "node-1")
}
if resp.Data[1].NodeID != "node-2" {
t.Errorf("data[1].node_id = %q, want %q", resp.Data[1].NodeID, "node-2")
}
}
func TestClusterListNodesEmpty(t *testing.T) {
gin.SetMode(gin.TestMode)
mock := &mockClusterStore{nodes: nil}
stores := store.Stores{Cluster: mock}
h := NewClusterHandler(stores)
r := gin.New()
r.GET("/api/v1/admin/cluster", h.ListNodes)
req := httptest.NewRequest(http.MethodGet, "/api/v1/admin/cluster", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", w.Code, http.StatusOK)
}
var resp struct {
Data []json.RawMessage `json:"data"`
}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("unmarshal: %v", err)
}
// Should be empty array, not null
if resp.Data == nil {
t.Error("data should be [], not null")
}
}

View File

@@ -23,13 +23,19 @@ import (
)
// defaultBundledPackages is the curated set of packages installed by default.
// This is the dev/test bundle — includes demos and workflow examples.
// Other packages still ship in the Docker image but require BUNDLED_PACKAGES
// to be set explicitly (or "*" for all).
//
// Recommended production override (lean):
// BUNDLED_PACKAGES=notes,chat,chat-core,cluster-dashboard
var defaultBundledPackages = map[string]bool{
"notes": true,
"chat": true,
"chat-core": true,
"workflow-chat": true,
"dashboard": true,
"cluster-dashboard": true,
"workflow-demo": true,
"bug-report-triage": true,
"content-approval": true,