feat(presence): report workers and broker on status
This commit is contained in:
1 parent
7680dc5079
commit
ff9b6c7196
9 files changed
+194
-79
No files matched your search
@@ -3,16 +3,23 @@ package agentworker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
"github.com/samcharles93/archie-core/internal/domain/presence"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/agentboot"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/agentgit"
|
||||
agentnats "github.com/samcharles93/archie-core/internal/infrastructure/agenttransport/nats"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/readiness"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps"
|
||||
"github.com/samcharles93/archie-core/internal/installtype"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
@@ -44,14 +51,12 @@ type Settings struct {
|
||||
}
|
||||
|
||||
type workerTransport interface {
|
||||
taskServiceTransport
|
||||
Close()
|
||||
LogPublisher() agentexec.LogPublisher
|
||||
EventPublisher() agentexec.EventPublisher
|
||||
SubscribeTasks(context.Context, int64, agentnats.TaskHandler, *slog.Logger) (agentnats.Subscription, error)
|
||||
Forger(identity, credential string, timeout time.Duration) workflow.Forger
|
||||
Store(time.Duration) workflow.Store
|
||||
Calls(time.Duration) task.Caller
|
||||
Trees(string, string, time.Duration) agentnats.RemoteTrees
|
||||
PutPresence(context.Context, storecontract.Presence) error
|
||||
BrokerReady() bool
|
||||
}
|
||||
|
||||
type workerDependencies struct {
|
||||
@@ -148,6 +153,13 @@ func run(ctx context.Context, settings Settings, log *slog.Logger, dependencies
|
||||
}
|
||||
}()
|
||||
|
||||
registry := health.NewRegistry(readiness.NewContractProbe("nats", 0, func(context.Context) error {
|
||||
if !transport.BrokerReady() {
|
||||
return errors.New("broker disconnected")
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
go presence.Run(ctx, presence.Agent, presence.Build{InstanceID: strconv.FormatInt(taskID, 10), Version: Version(), InstallType: installtype.Type()}, transport, registry, log)
|
||||
log.Info("archie-agent ready", "nats", settings.NATSURL)
|
||||
dependencies.wait(ctx, log)
|
||||
return nil
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
@@ -29,6 +30,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/container"
|
||||
"github.com/samcharles93/archie-core/internal/daemon"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
"github.com/samcharles93/archie-core/internal/domain/presence"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
@@ -36,6 +38,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/forge"
|
||||
"github.com/samcharles93/archie-core/internal/forgerpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/readiness"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
@@ -187,6 +190,7 @@ func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence
|
||||
}
|
||||
if ps, ok := b.stateStore.(storecontract.PresenceStore); ok {
|
||||
go presence.Run(ctx, b.processName, servicekit.Build(), ps, b.healthRegistry, b.log)
|
||||
b.reportBrokerPresence(ctx, ps)
|
||||
}
|
||||
// The daemon publishes the configuration view, which carries the catalog's
|
||||
// model limits.
|
||||
@@ -526,3 +530,15 @@ func resolveRegistryAuth(ref secret.SecretRef, secrets *secret.Registry, log *sl
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (b *boot) reportBrokerPresence(ctx context.Context, ps storecontract.PresenceStore) {
|
||||
if conn, err := b.natsClient.CoreConn(); err == nil {
|
||||
registry := health.NewRegistry(readiness.NewContractProbe("connection", 0, func(context.Context) error {
|
||||
if !conn.IsConnected() {
|
||||
return errors.New("broker disconnected")
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
go presence.Run(ctx, presence.Broker, presence.Build{Version: conn.ConnectedServerVersion(), InstallType: b.cfg.NATS.Mode}, ps, registry, b.log)
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"log/slog"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -23,16 +24,19 @@ const (
|
||||
Messaging = applystatus.Messaging
|
||||
UI = "archie-ui"
|
||||
StateStore = "archie-state-store"
|
||||
Agent = "archie-agent"
|
||||
Broker = "nats"
|
||||
)
|
||||
|
||||
// Services is every service expected to report.
|
||||
func Services() []string { return []string{Daemon, Gateway, Messaging, UI, StateStore} }
|
||||
func Services() []string { return []string{Daemon, Gateway, Messaging, UI, StateStore, Broker} }
|
||||
|
||||
// Stale reports whether a record's instance has stopped re-stamping it.
|
||||
func Stale(reportedAt, now time.Time) bool { return applystatus.Stale(reportedAt, now) }
|
||||
|
||||
// Build is what the binary was compiled as.
|
||||
type Build struct {
|
||||
InstanceID string
|
||||
Version string
|
||||
InstallType string
|
||||
}
|
||||
@@ -40,7 +44,10 @@ type Build struct {
|
||||
// Run publishes this instance's record immediately and then on every restamp
|
||||
// interval until ctx ends. A nil registry reports ready with no probes.
|
||||
// Write failures are logged: the next tick retries.
|
||||
func Run(ctx context.Context, service string, build Build, store storecontract.PresenceStore, registry *health.Registry, log *slog.Logger) {
|
||||
func Run(ctx context.Context, service string, build Build, store interface {
|
||||
PutPresence(context.Context, storecontract.Presence) error
|
||||
}, registry *health.Registry, log *slog.Logger,
|
||||
) {
|
||||
if store == nil {
|
||||
return
|
||||
}
|
||||
@@ -48,6 +55,9 @@ func Run(ctx context.Context, service string, build Build, store storecontract.P
|
||||
Service: service, InstanceID: instanceID(), Version: build.Version,
|
||||
InstallType: build.InstallType, StartedAt: time.Now().UTC(),
|
||||
}
|
||||
if build.InstanceID != "" {
|
||||
record.InstanceID = build.InstanceID
|
||||
}
|
||||
ticker := time.NewTicker(applystatus.RestampInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
@@ -124,7 +134,20 @@ func Mesh(records []storecontract.Presence, now time.Time) []Service {
|
||||
}
|
||||
services = append(services, service)
|
||||
}
|
||||
return services
|
||||
var workers []Service
|
||||
for _, record := range records {
|
||||
if record.Service != Agent {
|
||||
continue
|
||||
}
|
||||
worker := Service{Presence: record, State: state(record, true, now)}
|
||||
if worker.State == StateDown {
|
||||
worker.Detail = ""
|
||||
worker.Checks = nil
|
||||
}
|
||||
workers = append(workers, worker)
|
||||
}
|
||||
sort.Slice(workers, func(i, j int) bool { return workers[i].InstanceID < workers[j].InstanceID })
|
||||
return append(services, workers...)
|
||||
}
|
||||
|
||||
// Unreachable is the mesh view when the State Store cannot be read: the store
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
@@ -345,3 +346,12 @@ func (*Transport) respondTask(msg *natsio.Msg, response *taskrun.Response, runEr
|
||||
log.Error("taskrun respond failed", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// PutPresence publishes the worker's task-scoped record.
|
||||
func (t *Transport) PutPresence(ctx context.Context, record storecontract.Presence) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
return t.state.PutPresence(ctx, record)
|
||||
}
|
||||
|
||||
func (t *Transport) BrokerReady() bool { return t.conn.IsConnected() }
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
@@ -596,7 +597,15 @@ func (s *server) PutPresence(ctx context.Context, r *pb.PutPresenceRequest) (*pb
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ps.PutPresence(ctx, presenceValue(r.Presence)); err != nil {
|
||||
record := presenceValue(r.Presence)
|
||||
if strings.HasPrefix(access.ActorFromContext(ctx), "task/") {
|
||||
// A worker's clock cannot expire other services' retained records.
|
||||
record.ReportedAt = time.Now().UTC()
|
||||
if record.StartedAt.IsZero() || record.StartedAt.After(record.ReportedAt) {
|
||||
record.StartedAt = record.ReportedAt
|
||||
}
|
||||
}
|
||||
if err := ps.PutPresence(ctx, record); err != nil {
|
||||
return nil, s.logErr("PutPresence", err)
|
||||
}
|
||||
return &pb.PutPresenceResponse{}, nil
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
@@ -18,6 +19,7 @@ import (
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/presence"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
@@ -87,6 +89,17 @@ func authorizesTaskScopedCall(fullMethod string, req any, taskID int64) bool {
|
||||
// taskScopedTargets maps each RPC a task grant may call to the task ID its
|
||||
// request targets.
|
||||
var taskScopedTargets = map[string]func(any) int64{
|
||||
pb.StateStoreService_PutPresence_FullMethodName: func(req any) int64 {
|
||||
r, ok := req.(*pb.PutPresenceRequest)
|
||||
if !ok || r.Presence == nil || r.Presence.Service != presence.Agent {
|
||||
return 0
|
||||
}
|
||||
id, err := strconv.ParseInt(r.Presence.InstanceId, 10, 64)
|
||||
if err != nil || id <= 0 || strconv.FormatInt(id, 10) != r.Presence.InstanceId {
|
||||
return 0
|
||||
}
|
||||
return id
|
||||
},
|
||||
pb.StateStoreService_Update_FullMethodName: func(req any) int64 {
|
||||
r, ok := req.(*pb.UpdateRequest)
|
||||
if !ok || r.Task == nil {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
)
|
||||
|
||||
func TestWorkerPresenceAuthority(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, service, instance string
|
||||
allowed bool
|
||||
}{
|
||||
{"own task", "archie-agent", "7", true},
|
||||
{"another task", "archie-agent", "8", false},
|
||||
{"daemon", "archied", "7", false},
|
||||
{"broker", "nats", "7", false},
|
||||
{"noncanonical task", "archie-agent", "007", false},
|
||||
{"no task", "archie-agent", "", false},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := &pb.PutPresenceRequest{Presence: &pb.Presence{Service: tt.service, InstanceId: tt.instance}}
|
||||
if got := authorizesTaskScopedCall(pb.StateStoreService_PutPresence_FullMethodName, req, 7); got != tt.allowed {
|
||||
t.Fatalf("authorized = %v, want %v", got, tt.allowed)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user