From fd712258180e64fefb1d5ef422a0f6146f90e49a Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Thu, 14 Mar 2024 11:23:55 -0600 Subject: [PATCH 1/6] Use loggo v2.0.1 to get LogWithTagsf --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 0cb7a8a4778..a60ff8e0884 100644 --- a/go.mod +++ b/go.mod @@ -58,7 +58,7 @@ require ( github.com/juju/idmclient/v2 v2.0.0 github.com/juju/jsonschema v1.0.0 github.com/juju/loggo v1.0.0 - github.com/juju/loggo/v2 v2.0.0 + github.com/juju/loggo/v2 v2.0.1 github.com/juju/lumberjack/v2 v2.0.2 github.com/juju/mgo/v3 v3.0.4 github.com/juju/mutex/v2 v2.0.0 diff --git a/go.sum b/go.sum index a1e02ff8bec..603d23d411e 100644 --- a/go.sum +++ b/go.sum @@ -531,8 +531,8 @@ github.com/juju/loggo v0.0.0-20200526014432-9ce3a2e09b5e/go.mod h1:vgyd7OREkbtVE github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4/go.mod h1:NIXFioti1SmKAlKNuUwbMenNdef59IF52+ZzuOmHYkg= github.com/juju/loggo v1.0.0 h1:Y6ZMQOGR9Aj3BGkiWx7HBbIx6zNwNkxhVNOHU2i1bl0= github.com/juju/loggo v1.0.0/go.mod h1:NIXFioti1SmKAlKNuUwbMenNdef59IF52+ZzuOmHYkg= -github.com/juju/loggo/v2 v2.0.0 h1:PzyVIn+NgoZ22QUtPgKF/lh+6SnaCOEXhcP+sE4FhOk= -github.com/juju/loggo/v2 v2.0.0/go.mod h1:647d6WvXBLj5lvka2qBvccr7vMIvF2KFkEH+0ZuFOUM= +github.com/juju/loggo/v2 v2.0.1 h1:l7EDSQuOpW0SghK5M5cTl6/iAaTLRgjLN5n3y4wzn6A= +github.com/juju/loggo/v2 v2.0.1/go.mod h1:647d6WvXBLj5lvka2qBvccr7vMIvF2KFkEH+0ZuFOUM= github.com/juju/lru v1.0.0 h1:FP8mBNF3jBnKwGO5PtsR+8iIegx8DREfhRhpcGpYcn4= github.com/juju/lru v1.0.0/go.mod h1:YauKGp6PAhOQyGuTOkiFdXVP1jR3vLkp/FI71GcpYcQ= github.com/juju/lumberjack/v2 v2.0.2 h1:FlxrR62Vb7FfN7jwpSBqqereyq5bBQUF0LqOhZ2VGeI= From 4292ebb0667628663e93e2e8092cfeebe565dcd6 Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Thu, 14 Mar 2024 11:28:08 -0600 Subject: [PATCH 2/6] Update params of probablyUpdateStatusHistory and call InfoWithLabelsf --- state/status.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/state/status.go b/state/status.go index 45fd049ef73..1ee9978f35e 100644 --- a/state/status.go +++ b/state/status.go @@ -22,6 +22,8 @@ import ( "github.com/juju/juju/internal/mongo/utils" ) +var status_logger = loggo.GetLogger("juju.status") + type displayStatusFunc func(unitStatus status.StatusInfo, containerStatus status.StatusInfo) status.StatusInfo // ModelStatus holds all the current status values for a given model @@ -264,6 +266,13 @@ type setStatusParams struct { // badge is used to specialize any NotFound error emitted. badge string + // statusKind is the kind of the entity for which the status is being set. + statusKind string + + // statusId is the id of the entity for which the status is being set. It's + // not necessarily the same as the entity's Id(). + statusId string + // globalKey uniquely identifies the entity to which the globalKey string @@ -319,7 +328,8 @@ func setStatus(db Database, params setStatusParams) (err error) { historyDoc = params.historyOverwrite } - newStatus, historyErr := probablyUpdateStatusHistory(db, params.globalKey, *historyDoc) + newStatus, historyErr := probablyUpdateStatusHistory(db, + params.statusKind, params.statusId, params.globalKey, *historyDoc) if params.historyOverwrite == nil && (!newStatus && historyErr == nil) { // If this status is not new (i.e. it is exactly the same as // our last status), there is no need to update the record. @@ -407,7 +417,8 @@ type recordedHistoricalStatusDoc struct { // we update that record to have a new timestamp. // Status messages are considered to be the same if they only differ in their timestamps. // The call returns true if a new status history record has been created. -func probablyUpdateStatusHistory(db Database, globalKey string, doc statusDoc) (bool, error) { +func probablyUpdateStatusHistory(db Database, + statusKind string, statusId string, globalKey string, doc statusDoc) (bool, error) { historyDoc := &historicalStatusDoc{ Status: doc.Status, StatusInfo: doc.StatusInfo, @@ -415,6 +426,14 @@ func probablyUpdateStatusHistory(db Database, globalKey string, doc statusDoc) ( Updated: doc.Updated, GlobalKey: globalKey, } + + status_logger.InfoWithLabelsf("status update", map[string]string{ + "domain": "status", + "kind": statusKind, + "id": statusId, + "value": doc.Status.String(), + }) + history, closer := db.GetCollection(statusesHistoryC) defer closer() From 3cc876bb9a2fa165b17c40eb70bedb093f521044 Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Thu, 14 Mar 2024 11:29:19 -0600 Subject: [PATCH 3/6] Add statusKind/statusId to state entities machines units filesystem remoteapplication models relations volumes --- state/addmachine.go | 6 ++--- state/application.go | 36 +++++++++++++++++--------- state/cloudcontainer.go | 3 +++ state/filesystem.go | 20 +++++++++----- state/initialize.go | 2 +- state/machine.go | 53 ++++++++++++++++++++++++-------------- state/model.go | 19 +++++++++----- state/modelcredential.go | 4 +-- state/modelmigration.go | 2 +- state/relation.go | 17 +++++++----- state/remoteapplication.go | 22 ++++++++++------ state/state.go | 2 +- state/unit.go | 23 ++++++++++++----- state/unitagent.go | 19 +++++++++----- state/volume.go | 20 +++++++++----- 15 files changed, 161 insertions(+), 87 deletions(-) diff --git a/state/addmachine.go b/state/addmachine.go index c909a47923d..7d94d601cfc 100644 --- a/state/addmachine.go +++ b/state/addmachine.go @@ -581,9 +581,9 @@ func (st *State) insertNewMachineOps(mdoc *machineDoc, template MachineTemplate) // history entry. This is risky, and may lead to extra entries, but that's // an intrinsic problem with mixing txn and non-txn ops -- we can't sync // them cleanly. - _, _ = probablyUpdateStatusHistory(st.db(), machineGlobalKey(mdoc.Id), machineStatusDoc) - _, _ = probablyUpdateStatusHistory(st.db(), machineGlobalInstanceKey(mdoc.Id), instanceStatusDoc) - _, _ = probablyUpdateStatusHistory(st.db(), machineGlobalModificationKey(mdoc.Id), modificationStatusDoc) + _, _ = probablyUpdateStatusHistory(st.db(), machineKindPrefix, mdoc.Id, machineGlobalKey(mdoc.Id), machineStatusDoc) + _, _ = probablyUpdateStatusHistory(st.db(), machineInstanceKindPrefix, mdoc.Id, machineGlobalInstanceKey(mdoc.Id), instanceStatusDoc) + _, _ = probablyUpdateStatusHistory(st.db(), machineModificationKindPrefix, mdoc.Id, machineGlobalModificationKey(mdoc.Id), modificationStatusDoc) return prereqOps, machineOp, nil } diff --git a/state/application.go b/state/application.go index 1a390ec7833..7b8b620c4e1 100644 --- a/state/application.go +++ b/state/application.go @@ -158,9 +158,12 @@ func (a *Application) ApplicationTag() names.ApplicationTag { // applicationGlobalKey returns the global database key for the application // with the given name. func applicationGlobalKey(appName string) string { - return "a#" + appName + return applicationKindPrefix + appName } +// applicationKindPrefix is the string we use to denote application kind. +const applicationKindPrefix = "a#" + // globalKey returns the global database key for the application. func (a *Application) globalKey() string { return applicationGlobalKey(a.doc.Name) @@ -2533,9 +2536,9 @@ func (a *Application) addUnitOpsWithCons(args applicationAddUnitOpsArgs) (string // history entries. This is risky, and may lead to extra entries, but that's // an intrinsic problem with mixing txn and non-txn ops -- we can't sync // them cleanly. - _, _ = probablyUpdateStatusHistory(a.st.db(), globalKey, *unitStatusDoc) - _, _ = probablyUpdateStatusHistory(a.st.db(), globalWorkloadVersionKey(name), *workloadVersionDoc) - _, _ = probablyUpdateStatusHistory(a.st.db(), agentGlobalKey, agentStatusDoc) + _, _ = probablyUpdateStatusHistory(a.st.db(), unitKindPrefix, name, globalKey, *unitStatusDoc) + _, _ = probablyUpdateStatusHistory(a.st.db(), unitWorkloadVersionKindPrefix, name, globalWorkloadVersionKey(name), *workloadVersionDoc) + _, _ = probablyUpdateStatusHistory(a.st.db(), unitAgentKindPrefix, name, agentGlobalKey, agentStatusDoc) return name, ops, nil } @@ -3496,6 +3499,8 @@ func (a *Application) SetStatus(statusInfo status.StatusInfo) error { return setStatus(a.st.db(), setStatusParams{ badge: "application", + statusKind: applicationKindPrefix, + statusId: a.Name(), globalKey: a.globalKey(), status: statusInfo.Status, message: statusInfo.Message, @@ -3517,12 +3522,14 @@ func (a *Application) SetOperatorStatus(sInfo status.StatusInfo) error { } err = setStatus(a.st.db(), setStatusParams{ - badge: "operator", - globalKey: applicationGlobalOperatorKey(a.Name()), - status: sInfo.Status, - message: sInfo.Message, - rawData: sInfo.Data, - updated: timeOrNow(sInfo.Since, a.st.clock()), + badge: "operator", + statusKind: applicationKindPrefix, + statusId: a.Name(), + globalKey: applicationGlobalOperatorKey(a.Name()), + status: sInfo.Status, + message: sInfo.Message, + rawData: sInfo.Data, + updated: timeOrNow(sInfo.Since, a.st.clock()), }) if err != nil { return errors.Trace(err) @@ -3537,7 +3544,8 @@ func (a *Application) SetOperatorStatus(sInfo status.StatusInfo) error { } if historyDoc != nil { // rewriting application status history - _, err = probablyUpdateStatusHistory(a.st.db(), a.globalKey(), *historyDoc) + _, err = probablyUpdateStatusHistory(a.st.db(), + applicationKindPrefix, a.Name(), a.globalKey(), *historyDoc) if err != nil { return errors.Trace(err) } @@ -3870,7 +3878,9 @@ func (op *AddUnitOperation) Done(err error) error { StatusData: mgoutils.EscapeKeys(op.props.CloudContainerStatus.Data), Updated: timeOrNow(op.props.CloudContainerStatus.Since, u.st.clock()).UnixNano(), } - _, err := probablyUpdateStatusHistory(op.application.st.db(), globalCloudContainerKey(op.unitName), doc) + _, err := probablyUpdateStatusHistory( + op.application.st.db(), cloudContainerKindPrefix, op.unitName, + globalCloudContainerKey(op.unitName), doc) if err != nil { return errors.Trace(err) } @@ -3887,6 +3897,8 @@ func (op *AddUnitOperation) Done(err error) error { if newHistory != nil { err = setStatus(op.application.st.db(), setStatusParams{ badge: "unit", + statusKind: unitKindPrefix, + statusId: op.unitName, globalKey: unitGlobalKey(op.unitName), status: unitStatus.Status, message: unitStatus.Message, diff --git a/state/cloudcontainer.go b/state/cloudcontainer.go index 30c32b52d6a..7e7d1c15b18 100644 --- a/state/cloudcontainer.go +++ b/state/cloudcontainer.go @@ -80,6 +80,9 @@ func globalCloudContainerKey(name string) string { return unitGlobalKey(name) + "#container" } +// cloudContainerKindPrefix is the string we use to denote cloud container kind. +const cloudContainerKindPrefix = unitKindPrefix + "#container" + func (u *Unit) cloudContainer() (*cloudContainerDoc, error) { coll, closer := u.st.db().GetCollection(cloudContainersC) defer closer() diff --git a/state/filesystem.go b/state/filesystem.go index 5b69a8037b9..509baec66e2 100644 --- a/state/filesystem.go +++ b/state/filesystem.go @@ -283,12 +283,14 @@ func (f *filesystem) SetStatus(fsStatus status.StatusInfo) error { return errors.Errorf("cannot set invalid status %q", fsStatus.Status) } return setStatus(f.mb.db(), setStatusParams{ - badge: "filesystem", - globalKey: filesystemGlobalKey(f.FilesystemTag().Id()), - status: fsStatus.Status, - message: fsStatus.Message, - rawData: fsStatus.Data, - updated: timeOrNow(fsStatus.Since, f.mb.clock()), + badge: "filesystem", + statusKind: filesystemKindPrefix, + statusId: f.FilesystemTag().Id(), + globalKey: filesystemGlobalKey(f.FilesystemTag().Id()), + status: fsStatus.Status, + message: fsStatus.Message, + rawData: fsStatus.Data, + updated: timeOrNow(fsStatus.Since, f.mb.clock()), }) } @@ -1531,6 +1533,10 @@ func filesystemsToInterfaces(sb []*filesystem) []Filesystem { return result } +// filesystemKindPrefix is the kind string we use to denote filesystem kind. +const filesystemKindPrefix = "f#" + +// filesystemGlobalKey returns the global database key for the filesystem. func filesystemGlobalKey(name string) string { - return "f#" + name + return filesystemKindPrefix + name } diff --git a/state/initialize.go b/state/initialize.go index e6f9d12e94b..aefedc0ea31 100644 --- a/state/initialize.go +++ b/state/initialize.go @@ -239,7 +239,7 @@ func Initialize(args InitializeParams, providerConfigSchemaGetter config.ConfigS if err := st.db().RunTransaction(ops); err != nil { return nil, errors.Trace(err) } - _, _ = probablyUpdateStatusHistory(st.db(), modelGlobalKey, modelStatusDoc) + _, _ = probablyUpdateStatusHistory(st.db(), modelKindPrefix, modelGlobalKey, modelGlobalKey, modelStatusDoc) return ctlr, nil } diff --git a/state/machine.go b/state/machine.go index a5f3e7f0a01..c7711fcabf8 100644 --- a/state/machine.go +++ b/state/machine.go @@ -220,15 +220,21 @@ func (m *Machine) forceDestroyedOps() []txn.Op { // machineGlobalKey returns the global database key for the identified machine. func machineGlobalKey(id string) string { - return "m#" + id + return machineKindPrefix + id } +// machineKindPrefix is the kind string we use to denote machine kind. +const machineKindPrefix = "m#" + // machineGlobalInstanceKey returns the global database key for the identified // machine's instance. func machineGlobalInstanceKey(id string) string { return machineGlobalKey(id) + "#instance" } +// machineInstaanceKind is the kind string we use for machine instances. +const machineInstanceKindPrefix = "m#instance" + // globalInstanceKey returns the global database key for the machine's instance. func (m *Machine) globalInstanceKey() string { return machineGlobalInstanceKey(m.doc.Id) @@ -240,6 +246,9 @@ func machineGlobalModificationKey(id string) string { return machineGlobalKey(id) + "#modification" } +// machineModificationKind is the kind string we use for machine modifications. +const machineModificationKindPrefix = "m#modification" + // globalModificationKey returns the global database key for the machine's // modification changes. func (m *Machine) globalModificationKey() string { @@ -1310,12 +1319,14 @@ func (m *Machine) InstanceStatus() (status.StatusInfo, error) { // SetInstanceStatus sets the provider specific instance status for a machine. func (m *Machine) SetInstanceStatus(sInfo status.StatusInfo) (err error) { return setStatus(m.st.db(), setStatusParams{ - badge: "instance", - globalKey: m.globalInstanceKey(), - status: sInfo.Status, - message: sInfo.Message, - rawData: sInfo.Data, - updated: timeOrNow(sInfo.Since, m.st.clock()), + badge: "instance", + statusKind: machineInstanceKindPrefix, + statusId: m.doc.Id, + globalKey: m.globalInstanceKey(), + status: sInfo.Status, + message: sInfo.Message, + rawData: sInfo.Data, + updated: timeOrNow(sInfo.Since, m.st.clock()), }) } @@ -1350,12 +1361,14 @@ func (m *Machine) ModificationStatus() (status.StatusInfo, error) { // operator. func (m *Machine) SetModificationStatus(sInfo status.StatusInfo) (err error) { return setStatus(m.st.db(), setStatusParams{ - badge: "modification", - globalKey: m.globalModificationKey(), - status: sInfo.Status, - message: sInfo.Message, - rawData: sInfo.Data, - updated: timeOrNow(sInfo.Since, m.st.clock()), + badge: "modification", + statusKind: machineModificationKindPrefix, + statusId: m.doc.Id, + globalKey: m.globalModificationKey(), + status: sInfo.Status, + message: sInfo.Message, + rawData: sInfo.Data, + updated: timeOrNow(sInfo.Since, m.st.clock()), }) } @@ -1965,12 +1978,14 @@ func (m *Machine) SetStatus(statusInfo status.StatusInfo) error { return errors.Errorf("cannot set invalid status %q", statusInfo.Status) } return setStatus(m.st.db(), setStatusParams{ - badge: "machine", - globalKey: m.globalKey(), - status: statusInfo.Status, - message: statusInfo.Message, - rawData: statusInfo.Data, - updated: timeOrNow(statusInfo.Since, m.st.clock()), + badge: "machine", + statusKind: machineKindPrefix, + statusId: m.doc.Id, + globalKey: m.globalKey(), + status: statusInfo.Status, + message: statusInfo.Message, + rawData: statusInfo.Data, + updated: timeOrNow(statusInfo.Since, m.st.clock()), }) } diff --git a/state/model.go b/state/model.go index b4dea0c99a6..0c2bb56039c 100644 --- a/state/model.go +++ b/state/model.go @@ -32,6 +32,9 @@ import ( // settings and constraints. const modelGlobalKey = "e" +// modelKindPrefix is the string we use to denote model kind. +const modelKindPrefix = modelGlobalKey + "#" + // modelKey will create the key for a given model using the modelGlobalKey. func modelKey(modelUUID string) string { return fmt.Sprintf("%s#%s", modelGlobalKey, modelUUID) @@ -368,7 +371,7 @@ func (ctlr *Controller) NewModel(configSchemaGetter config.ConfigSchemaSourceGet return nil, nil, errors.Trace(err) } if args.MigrationMode != MigrationModeImporting { - _, _ = probablyUpdateStatusHistory(newSt.db(), modelGlobalKey, modelStatusDoc) + _, _ = probablyUpdateStatusHistory(newSt.db(), modelKindPrefix, modelGlobalKey, modelGlobalKey, modelStatusDoc) } _, err = newSt.SetUserAccess(newModel.Owner(), newModel.ModelTag(), permission.AdminAccess) @@ -579,12 +582,14 @@ func (m *Model) SetStatus(sInfo status.StatusInfo) error { return errors.Errorf("cannot set invalid status %q", sInfo.Status) } return setStatus(m.st.db(), setStatusParams{ - badge: "model", - globalKey: m.globalKey(), - status: sInfo.Status, - message: sInfo.Message, - rawData: sInfo.Data, - updated: timeOrNow(sInfo.Since, m.st.clock()), + badge: "model", + statusKind: modelKindPrefix, + statusId: modelGlobalKey, + globalKey: m.globalKey(), + status: sInfo.Status, + message: sInfo.Message, + rawData: sInfo.Data, + updated: timeOrNow(sInfo.Since, m.st.clock()), }) } diff --git a/state/modelcredential.go b/state/modelcredential.go index 66744169d61..2099786120a 100644 --- a/state/modelcredential.go +++ b/state/modelcredential.go @@ -190,7 +190,7 @@ func (st *State) maybeSetModelStatusHistoryDoc(modelUUID string, doc statusDoc) return } - if _, err = probablyUpdateStatusHistory(one.st.db(), one.globalKey(), doc); err != nil { + if _, err = probablyUpdateStatusHistory(one.st.db(), modelKindPrefix, one.globalKey(), one.globalKey(), doc); err != nil { logger.Warningf("%v", err) } } @@ -212,7 +212,7 @@ func (m *Model) maybeRevertModelStatus() error { Updated: timeOrNow(nil, m.st.clock()).UnixNano(), } - if _, err = probablyUpdateStatusHistory(m.st.db(), m.globalKey(), doc); err != nil { + if _, err = probablyUpdateStatusHistory(m.st.db(), modelKindPrefix, m.globalKey(), m.globalKey(), doc); err != nil { return errors.Trace(err) } } diff --git a/state/modelmigration.go b/state/modelmigration.go index 68f678c317f..24403d69a41 100644 --- a/state/modelmigration.go +++ b/state/modelmigration.go @@ -398,7 +398,7 @@ func migStatusHistoryAndOps(st *State, phase migration.Phase, now int64, msg str if err != nil { return nil, errors.Trace(err) } - _, _ = probablyUpdateStatusHistory(st.db(), globalKey, doc) + _, _ = probablyUpdateStatusHistory(st.db(), modelKindPrefix, globalKey, globalKey, doc) return ops, nil } diff --git a/state/relation.go b/state/relation.go index 2f96ef2afa9..1b79a1f0f91 100644 --- a/state/relation.go +++ b/state/relation.go @@ -158,12 +158,14 @@ func (r *Relation) SetStatus(statusInfo status.StatusInfo) error { } } return setStatus(r.st.db(), setStatusParams{ - badge: "relation", - globalKey: r.globalScope(), - status: statusInfo.Status, - message: statusInfo.Message, - rawData: statusInfo.Data, - updated: timeOrNow(statusInfo.Since, r.st.clock()), + badge: "relation", + statusKind: relationKindPrefix, + statusId: fmt.Sprint(r.Id()), + globalKey: r.globalScope(), + status: statusInfo.Status, + message: statusInfo.Message, + rawData: statusInfo.Data, + updated: timeOrNow(statusInfo.Since, r.st.clock()), }) } @@ -769,6 +771,9 @@ func (r *Relation) unit( }, nil } +// relationKindPrefix is the string we use to denote relation kind. +const relationKindPrefix = "r#" + // globalScope returns the scope prefix for relation scope document keys // in the global scope. func (r *Relation) globalScope() string { diff --git a/state/remoteapplication.go b/state/remoteapplication.go index 7f5c688bf66..2492311fa9a 100644 --- a/state/remoteapplication.go +++ b/state/remoteapplication.go @@ -108,6 +108,9 @@ func newRemoteApplication(st *State, doc *remoteApplicationDoc) *RemoteApplicati return app } +// remoteApplicationkind is the string we use to denote remoteApplication kind. +const remoteApplicationKindPrefix = "c#" + // remoteApplicationGlobalKey returns the global database key for the // remote application with the given name. // @@ -116,7 +119,7 @@ func newRemoteApplication(st *State, doc *remoteApplicationDoc) *RemoteApplicati // and r and a were taken. // TODO(babbageclunk): check whether this is still the case. func remoteApplicationGlobalKey(appName string) string { - return "c#" + appName + return remoteApplicationKindPrefix + appName } // globalKey returns the global database key for the remote application. @@ -541,12 +544,14 @@ func (a *RemoteApplication) SetStatus(info status.StatusInfo) error { } return setStatus(a.st.db(), setStatusParams{ - badge: fmt.Sprintf("saas application %q", a.doc.Name), - globalKey: a.globalKey(), - status: info.Status, - message: info.Message, - rawData: info.Data, - updated: timeOrNow(info.Since, a.st.clock()), + badge: fmt.Sprintf("saas application %q", a.doc.Name), + statusKind: remoteApplicationKindPrefix, + statusId: a.Name(), + globalKey: a.globalKey(), + status: info.Status, + message: info.Message, + rawData: info.Data, + updated: timeOrNow(info.Since, a.st.clock()), }) } @@ -615,7 +620,8 @@ func (op *terminateRemoteApplicationOperation) Done(err error) error { if err != nil { return errors.Annotatef(err, "terminating saas application %q", op.app.Name()) } - _, _ = probablyUpdateStatusHistory(op.app.st.db(), op.app.globalKey(), op.doc) + _, _ = probablyUpdateStatusHistory(op.app.st.db(), + remoteApplicationKindPrefix, op.app.Name(), op.app.globalKey(), op.doc) // Set the life to Dead so that the lifecycle watcher will trigger to inform the // relevant workers that this application is gone. ops := []txn.Op{{ diff --git a/state/state.go b/state/state.go index 11c3dd6253f..a3feb93963b 100644 --- a/state/state.go +++ b/state/state.go @@ -1452,7 +1452,7 @@ func (st *State) AddApplication(prechecker environs.InstancePrechecker, args Add return ops, nil } // At the last moment before inserting the application, prime status history. - _, _ = probablyUpdateStatusHistory(st.db(), app.globalKey(), statusDoc) + _, _ = probablyUpdateStatusHistory(st.db(), applicationKindPrefix, app.Name(), app.globalKey(), statusDoc) if err = st.db().Run(buildTxn); err == nil { // Refresh to pick the txn-revno. diff --git a/state/unit.go b/state/unit.go index c4ae23ff7e4..147418585d1 100644 --- a/state/unit.go +++ b/state/unit.go @@ -190,6 +190,12 @@ func unitGlobalKey(name string) string { return "u#" + name + "#charm" } +// unitKindPrefix is the string we use to denote unit kind. +const unitKindPrefix = "u#charm" + +// unitWorkloadVersionKindPrefix is the string we use to denote unit workload version kind. +const unitWorkloadVersionKindPrefix = unitKindPrefix + "#sat#workload-version" + // globalWorkloadVersionKey returns the global database key for the // workload version status key for this unit. func globalWorkloadVersionKey(name string) string { @@ -244,11 +250,13 @@ func (u *Unit) SetWorkloadVersion(version string) error { // stop a swarm of watchers being notified for irrelevant changes. now := u.st.clock().Now() return setStatus(u.st.db(), setStatusParams{ - badge: "workload", - globalKey: u.globalWorkloadVersionKey(), - status: status.Active, - message: version, - updated: &now, + badge: "workload", + statusKind: unitWorkloadVersionKindPrefix, + statusId: u.Name(), + globalKey: u.globalWorkloadVersionKey(), + status: status.Active, + message: version, + updated: &now, }) } @@ -490,7 +498,8 @@ func (op *UpdateUnitOperation) Done(err error) error { // We can't include in the ops slice the necessary status history updates, // so as with existing practice, do a best effort update of status history. for key, doc := range op.setStatusDocs { - _, _ = probablyUpdateStatusHistory(op.unit.st.db(), key, doc) + _, _ = probablyUpdateStatusHistory(op.unit.st.db(), + unitKindPrefix, op.unit.Name(), key, doc) } return nil } @@ -1481,6 +1490,8 @@ func (u *Unit) SetStatus(unitStatus status.StatusInfo) error { return setStatus(u.st.db(), setStatusParams{ badge: "unit", + statusKind: unitKindPrefix, + statusId: u.Name(), globalKey: u.globalKey(), status: unitStatus.Status, message: unitStatus.Message, diff --git a/state/unitagent.go b/state/unitagent.go index c41e28fe25d..b7504f1837c 100644 --- a/state/unitagent.go +++ b/state/unitagent.go @@ -93,12 +93,14 @@ func (u *UnitAgent) SetStatus(unitAgentStatus status.StatusInfo) (err error) { return errors.Errorf("cannot set invalid status %q", unitAgentStatus.Status) } return setStatus(u.st.db(), setStatusParams{ - badge: "agent", - globalKey: u.globalKey(), - status: unitAgentStatus.Status, - message: unitAgentStatus.Message, - rawData: unitAgentStatus.Data, - updated: timeOrNow(unitAgentStatus.Since, u.st.clock()), + badge: "agent", + statusKind: unitAgentKindPrefix, + statusId: u.name, + globalKey: u.globalKey(), + status: unitAgentStatus.Status, + message: unitAgentStatus.Message, + rawData: unitAgentStatus.Data, + updated: timeOrNow(unitAgentStatus.Since, u.st.clock()), }) } @@ -117,9 +119,12 @@ func (u *UnitAgent) StatusHistory(filter status.StatusHistoryFilter) ([]status.S // unitAgentGlobalKey returns the global database key for the named unit. func unitAgentGlobalKey(name string) string { - return "u#" + name + return unitAgentKindPrefix + name } +// unitAgentKindPrefix is the string we use to denote unit agent kind. +const unitAgentKindPrefix = "u#" + // globalKey returns the global database key for the unit. func (u *UnitAgent) globalKey() string { return unitAgentGlobalKey(u.name) diff --git a/state/volume.go b/state/volume.go index 36d9bf54cf1..71474366bf1 100644 --- a/state/volume.go +++ b/state/volume.go @@ -325,12 +325,14 @@ func (v *volume) SetStatus(volumeStatus status.StatusInfo) error { return errors.Errorf("cannot set invalid status %q", volumeStatus.Status) } return setStatus(v.mb.db(), setStatusParams{ - badge: "volume", - globalKey: volumeGlobalKey(v.VolumeTag().Id()), - status: volumeStatus.Status, - message: volumeStatus.Message, - rawData: volumeStatus.Data, - updated: timeOrNow(volumeStatus.Since, v.mb.clock()), + badge: "volume", + statusKind: volumeKindPrefix, + statusId: v.VolumeTag().Id(), + globalKey: volumeGlobalKey(v.VolumeTag().Id()), + status: volumeStatus.Status, + message: volumeStatus.Message, + rawData: volumeStatus.Data, + updated: timeOrNow(volumeStatus.Since, v.mb.clock()), }) } @@ -1546,6 +1548,10 @@ func (sb *storageBackend) AllVolumes() ([]Volume, error) { return volumesToInterfaces(volumes), nil } +// volumeGlobalKey returns the global database key for the named volume. func volumeGlobalKey(name string) string { - return "v#" + name + return volumeKindPrefix + name } + +// volumeKindPrefix is the string we use to denote a volume kind. +const volumeKindPrefix = "v#" From 73fd0c351aa01ee9e61d27825c9c24ba7737c729 Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Mon, 18 Mar 2024 14:37:45 -0600 Subject: [PATCH 4/6] Use human readable kind names for entities in status logs --- state/addmachine.go | 7 ++++--- state/application.go | 29 ++++++++++++++++++----------- state/cloudcontainer.go | 3 --- state/filesystem.go | 14 ++++++++++---- state/initialize.go | 2 +- state/machine.go | 31 +++++++++++++++++++++---------- state/model.go | 12 +++++++----- state/modelcredential.go | 4 ++-- state/modelmigration.go | 2 +- state/relation.go | 10 ++++++---- state/remoteapplication.go | 16 +++++++++++----- state/state.go | 2 +- state/unit.go | 26 +++++++++++++++++++------- state/unitagent.go | 13 +++++++++---- state/volume.go | 7 ++++++- 15 files changed, 116 insertions(+), 62 deletions(-) diff --git a/state/addmachine.go b/state/addmachine.go index 7d94d601cfc..37928400811 100644 --- a/state/addmachine.go +++ b/state/addmachine.go @@ -577,13 +577,14 @@ func (st *State) insertNewMachineOps(mdoc *machineDoc, template MachineTemplate) } prereqOps = append(prereqOps, storageOps...) + m := newMachine(st, mdoc) // At the last moment we still have statusDoc in scope, set the initial // history entry. This is risky, and may lead to extra entries, but that's // an intrinsic problem with mixing txn and non-txn ops -- we can't sync // them cleanly. - _, _ = probablyUpdateStatusHistory(st.db(), machineKindPrefix, mdoc.Id, machineGlobalKey(mdoc.Id), machineStatusDoc) - _, _ = probablyUpdateStatusHistory(st.db(), machineInstanceKindPrefix, mdoc.Id, machineGlobalInstanceKey(mdoc.Id), instanceStatusDoc) - _, _ = probablyUpdateStatusHistory(st.db(), machineModificationKindPrefix, mdoc.Id, machineGlobalModificationKey(mdoc.Id), modificationStatusDoc) + _, _ = probablyUpdateStatusHistory(st.db(), m.Kind(), mdoc.Id, machineGlobalKey(mdoc.Id), machineStatusDoc) + _, _ = probablyUpdateStatusHistory(st.db(), m.InstanceKind(), mdoc.Id, machineGlobalInstanceKey(mdoc.Id), instanceStatusDoc) + _, _ = probablyUpdateStatusHistory(st.db(), m.ModificationKind(), mdoc.Id, machineGlobalModificationKey(mdoc.Id), modificationStatusDoc) return prereqOps, machineOp, nil } diff --git a/state/application.go b/state/application.go index 7b8b620c4e1..6a3cd8466ac 100644 --- a/state/application.go +++ b/state/application.go @@ -149,6 +149,11 @@ func (a *Application) Tag() names.Tag { return a.ApplicationTag() } +// Kind returns a human readable name identifying the application kind. +func (a *Application) Kind() string { + return a.Tag().Kind() +} + // ApplicationTag returns the more specific ApplicationTag rather than the generic // Tag. func (a *Application) ApplicationTag() names.ApplicationTag { @@ -158,11 +163,11 @@ func (a *Application) ApplicationTag() names.ApplicationTag { // applicationGlobalKey returns the global database key for the application // with the given name. func applicationGlobalKey(appName string) string { - return applicationKindPrefix + appName + return appGlobalKeyPrefix + appName } -// applicationKindPrefix is the string we use to denote application kind. -const applicationKindPrefix = "a#" +// appGlobalKeyPrefix is the string we use to denote application kind. +const appGlobalKeyPrefix = "a#" // globalKey returns the global database key for the application. func (a *Application) globalKey() string { @@ -2532,13 +2537,15 @@ func (a *Application) addUnitOpsWithCons(args applicationAddUnitOpsArgs) (string ops = append(ops, createConstraintsOp(agentGlobalKey, args.cons)) } + u := newUnit(a.st, m.Type(), udoc) + uAgent := newUnitAgent(a.st, unitTag, "") // At the last moment we still have the statusDocs in scope, set the initial // history entries. This is risky, and may lead to extra entries, but that's // an intrinsic problem with mixing txn and non-txn ops -- we can't sync // them cleanly. - _, _ = probablyUpdateStatusHistory(a.st.db(), unitKindPrefix, name, globalKey, *unitStatusDoc) - _, _ = probablyUpdateStatusHistory(a.st.db(), unitWorkloadVersionKindPrefix, name, globalWorkloadVersionKey(name), *workloadVersionDoc) - _, _ = probablyUpdateStatusHistory(a.st.db(), unitAgentKindPrefix, name, agentGlobalKey, agentStatusDoc) + _, _ = probablyUpdateStatusHistory(a.st.db(), u.Kind(), name, globalKey, *unitStatusDoc) + _, _ = probablyUpdateStatusHistory(a.st.db(), u.unitWorkloadVersionKind(), name, globalWorkloadVersionKey(name), *workloadVersionDoc) + _, _ = probablyUpdateStatusHistory(a.st.db(), uAgent.Kind(), name, agentGlobalKey, agentStatusDoc) return name, ops, nil } @@ -3499,7 +3506,7 @@ func (a *Application) SetStatus(statusInfo status.StatusInfo) error { return setStatus(a.st.db(), setStatusParams{ badge: "application", - statusKind: applicationKindPrefix, + statusKind: a.Kind(), statusId: a.Name(), globalKey: a.globalKey(), status: statusInfo.Status, @@ -3523,7 +3530,7 @@ func (a *Application) SetOperatorStatus(sInfo status.StatusInfo) error { err = setStatus(a.st.db(), setStatusParams{ badge: "operator", - statusKind: applicationKindPrefix, + statusKind: a.Kind(), statusId: a.Name(), globalKey: applicationGlobalOperatorKey(a.Name()), status: sInfo.Status, @@ -3545,7 +3552,7 @@ func (a *Application) SetOperatorStatus(sInfo status.StatusInfo) error { if historyDoc != nil { // rewriting application status history _, err = probablyUpdateStatusHistory(a.st.db(), - applicationKindPrefix, a.Name(), a.globalKey(), *historyDoc) + a.Kind(), a.Name(), a.globalKey(), *historyDoc) if err != nil { return errors.Trace(err) } @@ -3879,7 +3886,7 @@ func (op *AddUnitOperation) Done(err error) error { Updated: timeOrNow(op.props.CloudContainerStatus.Since, u.st.clock()).UnixNano(), } _, err := probablyUpdateStatusHistory( - op.application.st.db(), cloudContainerKindPrefix, op.unitName, + op.application.st.db(), u.cloudContainerKind(), op.unitName, globalCloudContainerKey(op.unitName), doc) if err != nil { return errors.Trace(err) @@ -3897,7 +3904,7 @@ func (op *AddUnitOperation) Done(err error) error { if newHistory != nil { err = setStatus(op.application.st.db(), setStatusParams{ badge: "unit", - statusKind: unitKindPrefix, + statusKind: u.Kind(), statusId: op.unitName, globalKey: unitGlobalKey(op.unitName), status: unitStatus.Status, diff --git a/state/cloudcontainer.go b/state/cloudcontainer.go index 7e7d1c15b18..30c32b52d6a 100644 --- a/state/cloudcontainer.go +++ b/state/cloudcontainer.go @@ -80,9 +80,6 @@ func globalCloudContainerKey(name string) string { return unitGlobalKey(name) + "#container" } -// cloudContainerKindPrefix is the string we use to denote cloud container kind. -const cloudContainerKindPrefix = unitKindPrefix + "#container" - func (u *Unit) cloudContainer() (*cloudContainerDoc, error) { coll, closer := u.st.db().GetCollection(cloudContainersC) defer closer() diff --git a/state/filesystem.go b/state/filesystem.go index 509baec66e2..49b512a7584 100644 --- a/state/filesystem.go +++ b/state/filesystem.go @@ -205,6 +205,11 @@ func (f *filesystem) Tag() names.Tag { return f.FilesystemTag() } +// Kind returns a human readable name identifying the filesystem kind. +func (f *filesystem) Kind() string { + return f.Tag().Kind() +} + // FilesystemTag is required to implement Filesystem. func (f *filesystem) FilesystemTag() names.FilesystemTag { return names.NewFilesystemTag(f.doc.FilesystemId) @@ -284,7 +289,7 @@ func (f *filesystem) SetStatus(fsStatus status.StatusInfo) error { } return setStatus(f.mb.db(), setStatusParams{ badge: "filesystem", - statusKind: filesystemKindPrefix, + statusKind: f.Kind(), statusId: f.FilesystemTag().Id(), globalKey: filesystemGlobalKey(f.FilesystemTag().Id()), status: fsStatus.Status, @@ -1533,10 +1538,11 @@ func filesystemsToInterfaces(sb []*filesystem) []Filesystem { return result } -// filesystemKindPrefix is the kind string we use to denote filesystem kind. -const filesystemKindPrefix = "f#" +// filesystemGlobalKeyPrefix is the kind string we use to denote filesystem +// kind. +const filesystemGlobalKeyPrefix = "f#" // filesystemGlobalKey returns the global database key for the filesystem. func filesystemGlobalKey(name string) string { - return filesystemKindPrefix + name + return filesystemGlobalKeyPrefix + name } diff --git a/state/initialize.go b/state/initialize.go index aefedc0ea31..9caf041ec86 100644 --- a/state/initialize.go +++ b/state/initialize.go @@ -239,7 +239,7 @@ func Initialize(args InitializeParams, providerConfigSchemaGetter config.ConfigS if err := st.db().RunTransaction(ops); err != nil { return nil, errors.Trace(err) } - _, _ = probablyUpdateStatusHistory(st.db(), modelKindPrefix, modelGlobalKey, modelGlobalKey, modelStatusDoc) + _, _ = probablyUpdateStatusHistory(st.db(), modelTag.Kind(), modelGlobalKey, modelGlobalKey, modelStatusDoc) return ctlr, nil } diff --git a/state/machine.go b/state/machine.go index c7711fcabf8..1219dfea51c 100644 --- a/state/machine.go +++ b/state/machine.go @@ -220,11 +220,11 @@ func (m *Machine) forceDestroyedOps() []txn.Op { // machineGlobalKey returns the global database key for the identified machine. func machineGlobalKey(id string) string { - return machineKindPrefix + id + return machineGlobalKeyPrefix + id } -// machineKindPrefix is the kind string we use to denote machine kind. -const machineKindPrefix = "m#" +// machineGlobalKeyPrefix is the kind string we use to denote machine kind. +const machineGlobalKeyPrefix = "m#" // machineGlobalInstanceKey returns the global database key for the identified // machine's instance. @@ -232,8 +232,11 @@ func machineGlobalInstanceKey(id string) string { return machineGlobalKey(id) + "#instance" } -// machineInstaanceKind is the kind string we use for machine instances. -const machineInstanceKindPrefix = "m#instance" +// InstanceKind returns a human readable name identifying the machine instance +// kind. +func (m *Machine) InstanceKind() string { + return m.Tag().Kind() + "-instance" +} // globalInstanceKey returns the global database key for the machine's instance. func (m *Machine) globalInstanceKey() string { @@ -246,8 +249,11 @@ func machineGlobalModificationKey(id string) string { return machineGlobalKey(id) + "#modification" } -// machineModificationKind is the kind string we use for machine modifications. -const machineModificationKindPrefix = "m#modification" +// ModificationKind returns the human readable kind string we use for when an +// lxd profile is applied on a machine.. +func (m *Machine) ModificationKind() string { + return m.Tag().Kind() + "-lxd-profile" +} // globalModificationKey returns the global database key for the machine's // modification changes. @@ -399,6 +405,11 @@ func (m *Machine) Tag() names.Tag { return m.MachineTag() } +// Kind returns a human readable name identifying the machine kind. +func (m *Machine) Kind() string { + return m.Tag().Kind() +} + // MachineTag returns the more specific MachineTag type as opposed // to the more generic Tag type. func (m *Machine) MachineTag() names.MachineTag { @@ -1320,7 +1331,7 @@ func (m *Machine) InstanceStatus() (status.StatusInfo, error) { func (m *Machine) SetInstanceStatus(sInfo status.StatusInfo) (err error) { return setStatus(m.st.db(), setStatusParams{ badge: "instance", - statusKind: machineInstanceKindPrefix, + statusKind: m.InstanceKind(), statusId: m.doc.Id, globalKey: m.globalInstanceKey(), status: sInfo.Status, @@ -1362,7 +1373,7 @@ func (m *Machine) ModificationStatus() (status.StatusInfo, error) { func (m *Machine) SetModificationStatus(sInfo status.StatusInfo) (err error) { return setStatus(m.st.db(), setStatusParams{ badge: "modification", - statusKind: machineModificationKindPrefix, + statusKind: m.ModificationKind(), statusId: m.doc.Id, globalKey: m.globalModificationKey(), status: sInfo.Status, @@ -1979,7 +1990,7 @@ func (m *Machine) SetStatus(statusInfo status.StatusInfo) error { } return setStatus(m.st.db(), setStatusParams{ badge: "machine", - statusKind: machineKindPrefix, + statusKind: m.Kind(), statusId: m.doc.Id, globalKey: m.globalKey(), status: statusInfo.Status, diff --git a/state/model.go b/state/model.go index 0c2bb56039c..adeae268c5b 100644 --- a/state/model.go +++ b/state/model.go @@ -32,9 +32,6 @@ import ( // settings and constraints. const modelGlobalKey = "e" -// modelKindPrefix is the string we use to denote model kind. -const modelKindPrefix = modelGlobalKey + "#" - // modelKey will create the key for a given model using the modelGlobalKey. func modelKey(modelUUID string) string { return fmt.Sprintf("%s#%s", modelGlobalKey, modelUUID) @@ -371,7 +368,7 @@ func (ctlr *Controller) NewModel(configSchemaGetter config.ConfigSchemaSourceGet return nil, nil, errors.Trace(err) } if args.MigrationMode != MigrationModeImporting { - _, _ = probablyUpdateStatusHistory(newSt.db(), modelKindPrefix, modelGlobalKey, modelGlobalKey, modelStatusDoc) + _, _ = probablyUpdateStatusHistory(newSt.db(), newModel.Kind(), modelGlobalKey, modelGlobalKey, modelStatusDoc) } _, err = newSt.SetUserAccess(newModel.Owner(), newModel.ModelTag(), permission.AdminAccess) @@ -406,6 +403,11 @@ func (m *Model) Tag() names.Tag { return m.ModelTag() } +// Kind returns a human readable name identifying the model kind. +func (m *Model) Kind() string { + return m.Tag().Kind() +} + // ModelTag is the concrete model tag for this model. func (m *Model) ModelTag() names.ModelTag { return names.NewModelTag(m.doc.UUID) @@ -583,7 +585,7 @@ func (m *Model) SetStatus(sInfo status.StatusInfo) error { } return setStatus(m.st.db(), setStatusParams{ badge: "model", - statusKind: modelKindPrefix, + statusKind: m.Kind(), statusId: modelGlobalKey, globalKey: m.globalKey(), status: sInfo.Status, diff --git a/state/modelcredential.go b/state/modelcredential.go index 2099786120a..2cb5708b031 100644 --- a/state/modelcredential.go +++ b/state/modelcredential.go @@ -190,7 +190,7 @@ func (st *State) maybeSetModelStatusHistoryDoc(modelUUID string, doc statusDoc) return } - if _, err = probablyUpdateStatusHistory(one.st.db(), modelKindPrefix, one.globalKey(), one.globalKey(), doc); err != nil { + if _, err = probablyUpdateStatusHistory(one.st.db(), one.Kind(), one.globalKey(), one.globalKey(), doc); err != nil { logger.Warningf("%v", err) } } @@ -212,7 +212,7 @@ func (m *Model) maybeRevertModelStatus() error { Updated: timeOrNow(nil, m.st.clock()).UnixNano(), } - if _, err = probablyUpdateStatusHistory(m.st.db(), modelKindPrefix, m.globalKey(), m.globalKey(), doc); err != nil { + if _, err = probablyUpdateStatusHistory(m.st.db(), m.Kind(), m.globalKey(), m.globalKey(), doc); err != nil { return errors.Trace(err) } } diff --git a/state/modelmigration.go b/state/modelmigration.go index 24403d69a41..9a11c680cdf 100644 --- a/state/modelmigration.go +++ b/state/modelmigration.go @@ -398,7 +398,7 @@ func migStatusHistoryAndOps(st *State, phase migration.Phase, now int64, msg str if err != nil { return nil, errors.Trace(err) } - _, _ = probablyUpdateStatusHistory(st.db(), modelKindPrefix, globalKey, globalKey, doc) + _, _ = probablyUpdateStatusHistory(st.db(), model.Kind(), globalKey, globalKey, doc) return ops, nil } diff --git a/state/relation.go b/state/relation.go index 1b79a1f0f91..9c8a14f6bc4 100644 --- a/state/relation.go +++ b/state/relation.go @@ -74,6 +74,11 @@ func (r *Relation) Tag() names.Tag { return names.NewRelationTag(r.doc.Key) } +// Kind returns a human readable name identifying the relation kind. +func (r *Relation) Kind() string { + return r.Tag().Kind() +} + // UnitCount is the number of units still in relation scope. func (r *Relation) UnitCount() int { return r.doc.UnitCount @@ -159,7 +164,7 @@ func (r *Relation) SetStatus(statusInfo status.StatusInfo) error { } return setStatus(r.st.db(), setStatusParams{ badge: "relation", - statusKind: relationKindPrefix, + statusKind: r.Kind(), statusId: fmt.Sprint(r.Id()), globalKey: r.globalScope(), status: statusInfo.Status, @@ -771,9 +776,6 @@ func (r *Relation) unit( }, nil } -// relationKindPrefix is the string we use to denote relation kind. -const relationKindPrefix = "r#" - // globalScope returns the scope prefix for relation scope document keys // in the global scope. func (r *Relation) globalScope() string { diff --git a/state/remoteapplication.go b/state/remoteapplication.go index 2492311fa9a..c437b68b7da 100644 --- a/state/remoteapplication.go +++ b/state/remoteapplication.go @@ -108,8 +108,9 @@ func newRemoteApplication(st *State, doc *remoteApplicationDoc) *RemoteApplicati return app } -// remoteApplicationkind is the string we use to denote remoteApplication kind. -const remoteApplicationKindPrefix = "c#" +// remoteAppGlobalKeyPrefix is the string we use to denote +// remoteApplication kind. +const remoteAppGlobalKeyPrefix = "c#" // remoteApplicationGlobalKey returns the global database key for the // remote application with the given name. @@ -119,7 +120,7 @@ const remoteApplicationKindPrefix = "c#" // and r and a were taken. // TODO(babbageclunk): check whether this is still the case. func remoteApplicationGlobalKey(appName string) string { - return remoteApplicationKindPrefix + appName + return remoteAppGlobalKeyPrefix + appName } // globalKey returns the global database key for the remote application. @@ -183,6 +184,11 @@ func (a *RemoteApplication) Tag() names.Tag { return names.NewApplicationTag(a.Name()) } +// Kind returns a human readable name identifying the remote application kind. +func (a *RemoteApplication) Kind() string { + return "remote-application" +} + // Life returns whether the application is Alive, Dying or Dead. func (a *RemoteApplication) Life() Life { return a.doc.Life @@ -545,7 +551,7 @@ func (a *RemoteApplication) SetStatus(info status.StatusInfo) error { return setStatus(a.st.db(), setStatusParams{ badge: fmt.Sprintf("saas application %q", a.doc.Name), - statusKind: remoteApplicationKindPrefix, + statusKind: a.Kind(), statusId: a.Name(), globalKey: a.globalKey(), status: info.Status, @@ -621,7 +627,7 @@ func (op *terminateRemoteApplicationOperation) Done(err error) error { return errors.Annotatef(err, "terminating saas application %q", op.app.Name()) } _, _ = probablyUpdateStatusHistory(op.app.st.db(), - remoteApplicationKindPrefix, op.app.Name(), op.app.globalKey(), op.doc) + op.app.Kind(), op.app.Name(), op.app.globalKey(), op.doc) // Set the life to Dead so that the lifecycle watcher will trigger to inform the // relevant workers that this application is gone. ops := []txn.Op{{ diff --git a/state/state.go b/state/state.go index a3feb93963b..aa596724acf 100644 --- a/state/state.go +++ b/state/state.go @@ -1452,7 +1452,7 @@ func (st *State) AddApplication(prechecker environs.InstancePrechecker, args Add return ops, nil } // At the last moment before inserting the application, prime status history. - _, _ = probablyUpdateStatusHistory(st.db(), applicationKindPrefix, app.Name(), app.globalKey(), statusDoc) + _, _ = probablyUpdateStatusHistory(st.db(), app.Kind(), app.Name(), app.globalKey(), statusDoc) if err = st.db().Run(buildTxn); err == nil { // Refresh to pick the txn-revno. diff --git a/state/unit.go b/state/unit.go index 147418585d1..48b6f00eada 100644 --- a/state/unit.go +++ b/state/unit.go @@ -190,11 +190,18 @@ func unitGlobalKey(name string) string { return "u#" + name + "#charm" } -// unitKindPrefix is the string we use to denote unit kind. -const unitKindPrefix = "u#charm" +// unitGlobalKeyPrefix is the string we use to denote unit kind. +const unitGlobalKeyPrefix = "u#charm" -// unitWorkloadVersionKindPrefix is the string we use to denote unit workload version kind. -const unitWorkloadVersionKindPrefix = unitKindPrefix + "#sat#workload-version" +// unitWorkloadVersionKind returns the unit workload version kind. +func (u *Unit) unitWorkloadVersionKind() string { + return u.Kind() + "-version" +} + +// cloudContainerKind returns the cloud container kind. +func (u *Unit) cloudContainerKind() string { + return u.Kind() + "-container" +} // globalWorkloadVersionKey returns the global database key for the // workload version status key for this unit. @@ -251,7 +258,7 @@ func (u *Unit) SetWorkloadVersion(version string) error { now := u.st.clock().Now() return setStatus(u.st.db(), setStatusParams{ badge: "workload", - statusKind: unitWorkloadVersionKindPrefix, + statusKind: u.unitWorkloadVersionKind(), statusId: u.Name(), globalKey: u.globalWorkloadVersionKey(), status: status.Active, @@ -499,7 +506,7 @@ func (op *UpdateUnitOperation) Done(err error) error { // so as with existing practice, do a best effort update of status history. for key, doc := range op.setStatusDocs { _, _ = probablyUpdateStatusHistory(op.unit.st.db(), - unitKindPrefix, op.unit.Name(), key, doc) + op.unit.Kind(), op.unit.Name(), key, doc) } return nil } @@ -1490,7 +1497,7 @@ func (u *Unit) SetStatus(unitStatus status.StatusInfo) error { return setStatus(u.st.db(), setStatusParams{ badge: "unit", - statusKind: unitKindPrefix, + statusKind: u.Kind(), statusId: u.Name(), globalKey: u.globalKey(), status: unitStatus.Status, @@ -1672,6 +1679,11 @@ func (u *Unit) Tag() names.Tag { return u.UnitTag() } +// Kind returns a human readable name identifying the unit workload kind. +func (u *Unit) Kind() string { + return u.Tag().Kind() + "-workload" +} + // UnitTag returns a names.UnitTag representing this Unit, unless the // unit Name is invalid, in which case it will panic func (u *Unit) UnitTag() names.UnitTag { diff --git a/state/unitagent.go b/state/unitagent.go index b7504f1837c..6a363d63fdb 100644 --- a/state/unitagent.go +++ b/state/unitagent.go @@ -94,7 +94,7 @@ func (u *UnitAgent) SetStatus(unitAgentStatus status.StatusInfo) (err error) { } return setStatus(u.st.db(), setStatusParams{ badge: "agent", - statusKind: unitAgentKindPrefix, + statusKind: u.Kind(), statusId: u.name, globalKey: u.globalKey(), status: unitAgentStatus.Status, @@ -119,11 +119,11 @@ func (u *UnitAgent) StatusHistory(filter status.StatusHistoryFilter) ([]status.S // unitAgentGlobalKey returns the global database key for the named unit. func unitAgentGlobalKey(name string) string { - return unitAgentKindPrefix + name + return unitAgentGlobalKeyPrefix + name } -// unitAgentKindPrefix is the string we use to denote unit agent kind. -const unitAgentKindPrefix = "u#" +// unitAgentGlobalKeyPrefix is the string we use to denote unit agent kind. +const unitAgentGlobalKeyPrefix = "u#" // globalKey returns the global database key for the unit. func (u *UnitAgent) globalKey() string { @@ -134,3 +134,8 @@ func (u *UnitAgent) globalKey() string { func (u *UnitAgent) Tag() names.Tag { return u.tag } + +// Kind returns a human readable name identifying the unit agent kind. +func (u *UnitAgent) Kind() string { + return u.Tag().Kind() + "-agent" +} diff --git a/state/volume.go b/state/volume.go index 71474366bf1..3dace474df4 100644 --- a/state/volume.go +++ b/state/volume.go @@ -255,6 +255,11 @@ func (v *volume) Tag() names.Tag { return v.VolumeTag() } +// Kind returns a human readable name identifying the volume kind. +func (v *volume) Kind() string { + return v.Tag().Kind() +} + // VolumeTag is required to implement Volume. func (v *volume) VolumeTag() names.VolumeTag { return names.NewVolumeTag(v.doc.Name) @@ -326,7 +331,7 @@ func (v *volume) SetStatus(volumeStatus status.StatusInfo) error { } return setStatus(v.mb.db(), setStatusParams{ badge: "volume", - statusKind: volumeKindPrefix, + statusKind: v.Kind(), statusId: v.VolumeTag().Id(), globalKey: volumeGlobalKey(v.VolumeTag().Id()), status: volumeStatus.Status, From 53be0edc9685a7d076f8e86d303b5b338c874fc4 Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Mon, 18 Mar 2024 15:21:44 -0600 Subject: [PATCH 5/6] Avoid logging idle status for machine lxd profile modification --- state/status.go | 22 ++++++++++++++++------ state/unit.go | 3 --- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/state/status.go b/state/status.go index 1ee9978f35e..59d34d7a322 100644 --- a/state/status.go +++ b/state/status.go @@ -410,6 +410,21 @@ type recordedHistoricalStatusDoc struct { StatusData map[string]interface{} `bson:"statusdata"` } +// logStatusUpdate sends the status update to the status logger. +// +// The "idle" status for the machine-lxd-profile is omitted from the status log, +// since only the applied or error statuses are useful in that case. +func logStatusUpdate(statusKind string, statusId string, doc statusDoc) { + if statusKind != "machine-lxd-profile" || doc.Status != status.Idle { + status_logger.InfoWithLabelsf("status update", map[string]string{ + "domain": "status", + "kind": statusKind, + "id": statusId, + "value": doc.Status.String(), + }) + } +} + // probablyUpdateStatusHistory inspects existing status-history // and determines if this status is new or the same as the last recorded. // If this is a new status, a new status history record will be added. @@ -427,12 +442,7 @@ func probablyUpdateStatusHistory(db Database, GlobalKey: globalKey, } - status_logger.InfoWithLabelsf("status update", map[string]string{ - "domain": "status", - "kind": statusKind, - "id": statusId, - "value": doc.Status.String(), - }) + logStatusUpdate(statusKind, statusId, doc) history, closer := db.GetCollection(statusesHistoryC) defer closer() diff --git a/state/unit.go b/state/unit.go index 48b6f00eada..2ee273f120e 100644 --- a/state/unit.go +++ b/state/unit.go @@ -190,9 +190,6 @@ func unitGlobalKey(name string) string { return "u#" + name + "#charm" } -// unitGlobalKeyPrefix is the string we use to denote unit kind. -const unitGlobalKeyPrefix = "u#charm" - // unitWorkloadVersionKind returns the unit workload version kind. func (u *Unit) unitWorkloadVersionKind() string { return u.Kind() + "-version" From 69e46a850c3ef1200fec5d8a26c75b95ca2f3e37 Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Mon, 18 Mar 2024 19:00:37 -0600 Subject: [PATCH 6/6] Use StatusInfo for human readable status update log messages --- state/status.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/state/status.go b/state/status.go index 59d34d7a322..417b6c06152 100644 --- a/state/status.go +++ b/state/status.go @@ -414,9 +414,13 @@ type recordedHistoricalStatusDoc struct { // // The "idle" status for the machine-lxd-profile is omitted from the status log, // since only the applied or error statuses are useful in that case. +// +// TODO (cderici): Once the statusesHistoryC collection goes away we'll lose +// access to the doc parameter, so we'll replace it with a couple more +// parameters for the status info and the status value. func logStatusUpdate(statusKind string, statusId string, doc statusDoc) { if statusKind != "machine-lxd-profile" || doc.Status != status.Idle { - status_logger.InfoWithLabelsf("status update", map[string]string{ + status_logger.InfoWithLabelsf(doc.StatusInfo, map[string]string{ "domain": "status", "kind": statusKind, "id": statusId,