directorobs

package
v0.0.0-...-1dd1f65 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2023 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package directorobs contains a pre-packaged observer set for the test director.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrForwardHandlerNil = errors.New("forward handler is nil")

ErrForwardHandlerNil occurs when NewForwardObserver is given a nil ForwardHandler.

Functions

This section is empty.

Types

type Forward

type Forward struct {
	// Cycle is the cycle message if the forward kind is ForwardCycle;
	// if not, only its cycle is defined, and it determines the cycle from which this forward originates.
	Cycle director.CycleMessage

	// Kind is the kind of message that has been forwarded.
	Kind ForwardKind

	// Instance is, when Kind is ForwardInstance, a forwarded instance message.
	Instance *director.InstanceMessage

	// Analysis is, when Kind is ForwardAnalysis, an analysis message.
	Analysis *analysis.Analysis

	// Compiler is, when Kind is ForwardCompilers, a forwarded compiler message.
	Compiler *compiler.Message

	// Save is, when Kind is ForwardSave, a forwarded save message.
	Save *saver.ArchiveMessage

	// Build is, when Kind is ForwardBuild, a forwarded build message.
	Build *builder.Message

	// Copy is, when Kind is ForwardCopy, a forwarded copy message.
	Copy *copier.Message
}

Forward contains a director observation that has been forwarded from an instance to a 'main' observer, alongside disambiguating information.

This struct, and its sibling structs and interfaces,exist to solve the problem that many things in the director happen in instance threads, but then need to be observed by a single-threaded observer.

func ForwardAnalysisMessage

func ForwardAnalysisMessage(c director.Cycle, m analysis.Analysis) Forward

ForwardAnalysisMessage constructs a forwarding message for m over cycle c.

func ForwardBuildMessage

func ForwardBuildMessage(c director.Cycle, m builder.Message) Forward

ForwardBuildMessage constructs a forwarding message for m over cycle c.

func ForwardCompilerMessage

func ForwardCompilerMessage(c director.Cycle, m compiler.Message) Forward

ForwardCompilerMessage constructs a forwarding message for m over cycle c.

func ForwardCopyMessage

func ForwardCopyMessage(c director.Cycle, m copier.Message) Forward

ForwardCopyMessage constructs a forwarding message for m over cycle c.

func ForwardCycleMessage

func ForwardCycleMessage(m director.CycleMessage) Forward

ForwardCycleMessage constructs a forwarding message for m.

func ForwardInstanceMessage

func ForwardInstanceMessage(c director.Cycle, m director.InstanceMessage) Forward

ForwardInstanceMessage constructs a forwarding message for m over cycle c.

func ForwardSaveMessage

func ForwardSaveMessage(c director.Cycle, m saver.ArchiveMessage) Forward

ForwardSaveMessage constructs a forwarding message for m over cycle c.

type ForwardHandler

type ForwardHandler interface {
	// CycleObserver captures that ForwardHandler can observe cycles in the same way that the forwarding observer can.
	director.CycleObserver

	// Observer captures that ForwardHandler instances can observe machines.
	machine.Observer

	// PrepareObserver captures that ForwardHandler instances can observe preparations.
	director.PrepareObserver

	// OnCycleInstance handles a message for the instance of a particular cycle.
	OnCycleInstance(director.Cycle, director.InstanceMessage)

	// OnCycleAnalysis should handle an analysis for a particular cycle.
	OnCycleAnalysis(director.CycleAnalysis)

	// OnCycleBuild should handle a corpus build for a particular cycle.
	OnCycleBuild(director.Cycle, builder.Message)

	// OnCycleCompiler should handle a compiler message for a particular cycle.
	OnCycleCompiler(director.Cycle, compiler.Message)

	// OnCycleCopy should handle a copy message for a particular cycle.
	OnCycleCopy(director.Cycle, copier.Message)

	// OnCycleSave should handle an archive message for a particular cycle.
	OnCycleSave(director.Cycle, saver.ArchiveMessage)
}

ForwardHandler is the interface of observers that can handle observations forwarded from an instance.

These inject behaviour into a ForwardObserver.

type ForwardKind

type ForwardKind uint8

ForwardKind is the enumeration of possible Forward messages.

const (
	// ForwardCycle delimits a forwarding message where Cycle is populated.
	ForwardCycle ForwardKind = iota
	// ForwardInstance delimits a forwarding message where Instance is populated (Cycle contains the cycle structure).
	ForwardInstance
	// ForwardAnalysis delimits a forwarding message where Analysis is populated (Cycle contains the cycle structure).
	ForwardAnalysis
	// ForwardCompiler delimits a forwarding message where Compiler is populated (Cycle contains the cycle structure).
	ForwardCompiler
	// ForwardSave delimits a forwarding message where Save is populated (Cycle contains the cycle structure).
	ForwardSave
	// ForwardBuild delimits a forwarding message where Build is populated (Cycle contains the cycle structure).
	ForwardBuild
	// ForwardCopy delimits a forwarding message where Copy is populated (Cycle contains the cycle structure).
	ForwardCopy
)

type ForwardObserver

type ForwardObserver struct {
	// contains filtered or unexported fields
}

ForwardObserver is an observer that uses a ForwardReceiver and a ForwardHandler to handle observations.

func NewForwardObserver

func NewForwardObserver(cap int, hs ...ForwardHandler) (*ForwardObserver, error)

NewForwardObserver constructs a new ForwardObserver with the given handlers hs and message buffer capacity cap.

func (*ForwardObserver) Instance

Instance creates an instance observer that forwards to this logger.

func (*ForwardObserver) OnBuild

func (f *ForwardObserver) OnBuild(builder.Message)

OnBuild does nothing, for now.

func (*ForwardObserver) OnCompilerConfig

func (f *ForwardObserver) OnCompilerConfig(compiler.Message)

OnCompilerConfig does nothing, for now.

func (*ForwardObserver) OnMachines

func (f *ForwardObserver) OnMachines(m machine.Message)

OnMachines delegates to the forward handlers.

func (*ForwardObserver) OnPlan

func (f *ForwardObserver) OnPlan(planner.Message)

OnPlan does nothing, for now.

func (*ForwardObserver) OnPrepare

func (f *ForwardObserver) OnPrepare(p director.PrepareMessage)

OnPrepare forwards prepare messages to the handlers.

func (*ForwardObserver) Run

func (f *ForwardObserver) Run(ctx context.Context) error

Run runs the observer's forwarding loop,and closes any attached instances when it finishes.

type ForwardReceiver

type ForwardReceiver observing.FanIn

ForwardReceiver holds receive channels for Forward messages.

It is effectively a more type-safe form of observing.FanIn, and will usually be used inside a ForwardObserver.

func NewForwardReceiver

func NewForwardReceiver(f func(m Forward) error, cap int) *ForwardReceiver

NewForwardReceiver constructs a new ForwardReceiver.

func (*ForwardReceiver) Add

func (r *ForwardReceiver) Add(c <-chan Forward)

Add adds a channel to the forward receiver.

func (*ForwardReceiver) Run

func (r *ForwardReceiver) Run(ctx context.Context) error

Run runs the forward receiver.

type ForwardingInstanceObserver

type ForwardingInstanceObserver struct {
	// contains filtered or unexported fields
}

ForwardingInstanceObserver is an instance observer that just forwards every observation to a director observer.

func (*ForwardingInstanceObserver) OnAnalysis

func (l *ForwardingInstanceObserver) OnAnalysis(c analysis.Analysis)

OnAnalysis forwards a cycle analysis.

func (*ForwardingInstanceObserver) OnArchive

OnArchive forwards a cycle save message.

func (*ForwardingInstanceObserver) OnBuild

OnBuild forwards a cycle build message.

func (*ForwardingInstanceObserver) OnCompilerConfig

func (l *ForwardingInstanceObserver) OnCompilerConfig(m compiler.Message)

func (*ForwardingInstanceObserver) OnCopy

OnCopy forwards a cycle copy message.

func (*ForwardingInstanceObserver) OnCycle

OnCycle notes that the instance's iteration has changed.

func (*ForwardingInstanceObserver) OnInstance

OnInstance forwards an instance message, and closes the forwarding channel if the instance has closed.

func (*ForwardingInstanceObserver) OnMachineNodeAction

func (l *ForwardingInstanceObserver) OnMachineNodeAction(observer.Message)

OnMachineNodeAction does nothing.

func (*ForwardingInstanceObserver) OnPerturb

OnPerturb does nothing, at the moment.

func (*ForwardingInstanceObserver) OnPlan

OnPlan does nothing, at the moment.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is a ForwardHandler that emits logs to a writer when cycles finish up.

func NewLogger

func NewLogger(w io.WriteCloser, lflag int) (*Logger, error)

NewLogger constructs a new Logger writing into w, using logger flags lflag when logging things. The logger takes ownership of w.

func (*Logger) Close

func (j *Logger) Close() error

Close closes the log observer.

func (*Logger) OnBuild

func (j *Logger) OnBuild(builder.Message)

OnBuild (currently) does nothing.

func (*Logger) OnCompilerConfig

func (j *Logger) OnCompilerConfig(compiler.Message)

OnCompilerConfig (currently) does nothing.

func (*Logger) OnCycle

func (j *Logger) OnCycle(c director.CycleMessage)

OnCycle logs a cycle event.

Example

ExampleLogger_OnCycle is a runnable example indirectly exercising Logger.OnCycle.

package main

import (
	"context"
	"errors"
	"os"
	"time"

	"github.com/c4-project/c4t/internal/director"

	"github.com/c4-project/c4t/internal/ux/directorobs"

	"github.com/c4-project/c4t/internal/helper/iohelp"
	"github.com/c4-project/c4t/internal/id"
)

func main() {
	l, _ := directorobs.NewLogger(iohelp.NopWriteCloser{Writer: os.Stdout}, 0)
	r, _ := directorobs.NewForwardObserver(0, l)
	i, _ := r.Instance(id.FromString("localhost"))

	c := director.Cycle{
		Instance:  0,
		MachineID: id.FromString("localhost"),
		Iter:      10,
		Start:     time.Time{},
	}

	go func() {
		// These messages will arrive through l.OnCycle.
		director.OnCycle(director.CycleStartMessage(c), i)
		director.OnCycle(director.CycleErrorMessage(c, errors.New("the front fell off")), i)
		// Important, else the logger will keep waiting for the instance to provide observations.
		i.OnInstance(director.InstanceClosedMessage())
	}()
	_ = r.Run(context.Background())

}
Output:

* localhost starts cycle 10 *
* localhost ERROR: the front fell off *
[instance 0 has closed]

func (*Logger) OnCycleAnalysis

func (j *Logger) OnCycleAnalysis(s director.CycleAnalysis)

OnCycleAnalysis logs s to this logger's file.

func (*Logger) OnCycleBuild

func (j *Logger) OnCycleBuild(director.Cycle, builder.Message)

OnCycleBuild does nothing, for now.

func (*Logger) OnCycleCompiler

func (j *Logger) OnCycleCompiler(c director.Cycle, m compiler.Message)

OnCycleCompiler handles a compiler message m coming from the director cycle c.

Example

ExampleLogger_OnCycleCompiler is a runnable example indirectly exercising Logger.OnCycleCompiler.

package main

import (
	"context"
	"os"

	"github.com/c4-project/c4t/internal/director"

	"github.com/c4-project/c4t/internal/model/service/compiler"
	"github.com/c4-project/c4t/internal/model/service/compiler/optlevel"

	"github.com/c4-project/c4t/internal/ux/directorobs"

	"github.com/c4-project/c4t/internal/helper/iohelp"
	"github.com/c4-project/c4t/internal/id"
)

func main() {
	l, _ := directorobs.NewLogger(iohelp.NopWriteCloser{Writer: os.Stdout}, 0)
	r, _ := directorobs.NewForwardObserver(0, l)
	i, _ := r.Instance(id.FromString("localhost"))

	go func() {
		// These messages will arrive through l.OnCycleCompiler.
		compiler.OnCompilerConfigStart(3, i)
		compiler.OnCompilerConfigStep(0,
			compiler.Named{
				ID: id.FromString("gcc.4"),
				Instance: compiler.Instance{
					SelectedMOpt: "arch=native",
					SelectedOpt:  &optlevel.Named{Name: "3", Level: optlevel.Level{}},
					Compiler:     compiler.Compiler{Style: id.CStyleGCC, Arch: id.ArchArm7},
				},
			}, i)
		compiler.OnCompilerConfigStep(1,
			compiler.Named{
				ID: id.FromString("gcc.9"),
				Instance: compiler.Instance{
					SelectedMOpt: "arch=skylake",
					SelectedOpt:  &optlevel.Named{Name: "2", Level: optlevel.Level{}},
					Compiler:     compiler.Compiler{Style: id.CStyleGCC, Arch: id.ArchArm8},
				},
			}, i)
		compiler.OnCompilerConfigStep(2,
			compiler.Named{
				ID: id.FromString("msvc"),
				Instance: compiler.Instance{
					Compiler: compiler.Compiler{Style: id.FromString("msvc"), Arch: id.ArchX8664},
				},
			}, i)
		compiler.OnCompilerConfigEnd(i)
		// Important, else the logger will keep waiting for the instance to provide observations.
		i.OnInstance(director.InstanceClosedMessage())
	}()
	_ = r.Run(context.Background())

}
Output:

[0:  #0 (Jan  1 00:00:00)] compilers 3:
- gcc.4: gcc@arm.7 opt "3" march "arch=native"
- gcc.9: gcc@arm.8 opt "2" march "arch=skylake"
- msvc: msvc@x86.64
[instance 0 has closed]

func (*Logger) OnCycleCopy

func (j *Logger) OnCycleCopy(director.Cycle, copier.Message)

OnCycleCopy does nothing, for now.

func (*Logger) OnCycleInstance

func (j *Logger) OnCycleInstance(c director.Cycle, m director.InstanceMessage)

OnCycleInstance records a message about a cycle's instance.

func (*Logger) OnCycleSave

func (j *Logger) OnCycleSave(c director.Cycle, s saver.ArchiveMessage)

OnCycleSave logs s to this logger's file.

Example

ExampleLogger_OnCycleSave is a runnable example indirectly exercising Logger.OnCycleSave.

package main

import (
	"context"
	"os"

	"github.com/c4-project/c4t/internal/director"

	"github.com/c4-project/c4t/internal/ux/directorobs"

	"github.com/c4-project/c4t/internal/stage/analyser/saver"

	"github.com/c4-project/c4t/internal/helper/iohelp"
	"github.com/c4-project/c4t/internal/id"
)

func main() {
	l, _ := directorobs.NewLogger(iohelp.NopWriteCloser{Writer: os.Stdout}, 0)
	r, _ := directorobs.NewForwardObserver(0, l)
	i, _ := r.Instance(id.FromString("localhost"))

	go func() {
		// These messages will arrive through l.OnCycleSave.
		saver.OnArchiveStart("subj", "subj.tar.gz", 2, i)
		saver.OnArchiveFileAdded("subj", "a.out", 0, i)
		saver.OnArchiveFileMissing("subj", "compile.log", 1, i)
		saver.OnArchiveFinish("subj", i)
		// Important, else the logger will keep waiting for the instance to provide observations.
		i.OnInstance(director.InstanceClosedMessage())
	}()
	_ = r.Run(context.Background())

}
Output:

saving (cycle [0:  #0 (Jan  1 00:00:00)]) subj to subj.tar.gz
when saving (cycle [0:  #0 (Jan  1 00:00:00)]) subj: missing file compile.log
[instance 0 has closed]

func (*Logger) OnMachines

func (j *Logger) OnMachines(m machine.Message)

OnMachines logs a machine block.

func (*Logger) OnPlan

func (j *Logger) OnPlan(planner.Message)

OnPlan (currently) does nothing.

func (*Logger) OnPrepare

func (j *Logger) OnPrepare(m director.PrepareMessage)

OnPrepare logs the preparation attempts of a director.

Example

ExampleLogger_OnPrepare is a runnable example indirectly exercising Logger.OnPrepare.

package main

import (
	"os"

	"github.com/c4-project/c4t/internal/director"

	"github.com/c4-project/c4t/internal/ux/directorobs"

	"github.com/c4-project/c4t/internal/helper/iohelp"
)

func main() {
	l, _ := directorobs.NewLogger(iohelp.NopWriteCloser{Writer: os.Stdout}, 0)
	r, _ := directorobs.NewForwardObserver(0, l)

	director.OnPrepare(director.PrepareInstancesMessage(5), r)

}
Output:

running on 5 instances

type Obs

type Obs struct {
	// contains filtered or unexported fields
}

Obs is the standard top-level director observer.

func NewObs

func NewObs(cfg *config.Config, useDash bool) (*Obs, error)

NewObs creates a director observer using the global configuration cfg. If useDash is true, it will create a dashboard; otherwise, it will bypass this.

func (*Obs) Close

func (o *Obs) Close() error

func (*Obs) Observers

func (o *Obs) Observers() []director.Observer

func (*Obs) Run

func (o *Obs) Run(ctx context.Context, cancel context.CancelFunc) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL