Skip to content

Commit

Permalink
Merge pull request #11124 from howbazaar/allwatcher-model-extras
Browse files Browse the repository at this point in the history
#11124

In order to provide the information to the upcoming model summary watcher (which will be implemented in the cache), we need to get some extra information into the cache.

There is a drive by that renames the ModelUpdate type from the core/multiwatcher package back to ModelInfo. This was renamed previously when the type was moved into the apiserver/params package and we had to deal with a naming collision. Renamed it back for this type for consistency with the rest of the package.

Adds three pieces of information from the state.Model in through the allwatcher: Cloud, Region, and Credential. Each of these is just a string value in the underlying document, so just adding these to the multiwatcher info type is sufficient to pass them along. Also needed to add these to the cache change type, and this makes them available to the internals of the cache.

The cache needed to know about relations, so pass the relation changes from the modelcache worker though to the controller, and add methods to add and remove the relations to the cached model.

## QA steps

Nothing is exposed to the outside world yet, that is coming.

## Documentation changes

Everything is internal, no user facing change.
  • Loading branch information
jujubot committed Jan 20, 2020
2 parents 761c0df + 47c80ad commit 341cb40
Show file tree
Hide file tree
Showing 16 changed files with 438 additions and 100 deletions.
2 changes: 1 addition & 1 deletion apiserver/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (aw *SrvAllWatcher) translate(deltas []multiwatcher.Delta) []params.Delta {
}

func (aw *SrvAllWatcher) translateModel(info multiwatcher.EntityInfo) params.EntityInfo {
orig, ok := info.(*multiwatcher.ModelUpdate)
orig, ok := info.(*multiwatcher.ModelInfo)
if !ok {
logger.Criticalf("consistency error: %s", pretty.Sprint(info))
return nil
Expand Down
24 changes: 18 additions & 6 deletions core/cache/cachetest/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package cachetest

import (
"strings"

"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
Expand All @@ -12,6 +14,7 @@ import (
"github.com/juju/juju/core/life"
"github.com/juju/juju/core/lxdprofile"
"github.com/juju/juju/core/model"
"github.com/juju/juju/core/permission"
"github.com/juju/juju/network"
"github.com/juju/juju/state"
)
Expand All @@ -32,13 +35,22 @@ func ModelChange(c *gc.C, model *state.Model) cache.ModelChange {
status, err := model.Status()
c.Assert(err, jc.ErrorIsNil)

users, err := model.Users()
permissions := make(map[string]permission.Access)
for _, user := range users {
// Cache permission map is always lower case.
permissions[strings.ToLower(user.UserName)] = user.Access
}

return cache.ModelChange{
ModelUUID: model.UUID(),
Name: model.Name(),
Life: life.Value(model.Life().String()),
Owner: model.Owner().Name(),
Config: cfg.AllAttrs(),
Status: status,
ModelUUID: model.UUID(),
Name: model.Name(),
Life: life.Value(model.Life().String()),
Owner: model.Owner().Name(),
IsController: model.IsControllerModel(),
Config: cfg.AllAttrs(),
Status: status,
UserPermissions: permissions,
}
}

Expand Down
57 changes: 51 additions & 6 deletions core/cache/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@ import (
"github.com/juju/juju/core/life"
"github.com/juju/juju/core/lxdprofile"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/permission"
"github.com/juju/juju/core/settings"
"github.com/juju/juju/core/status"
)

// ModelChange represents either a new model, or a change
// to an existing model.
type ModelChange struct {
ModelUUID string
Name string
Life life.Value
Owner string // tag maybe?
Config map[string]interface{}
Status status.StatusInfo
ModelUUID string
Name string
Life life.Value
Owner string // tag maybe?
IsController bool
Cloud string
CloudRegion string
CloudCredential string
Config map[string]interface{}
Status status.StatusInfo

UserPermissions map[string]permission.Access
}

// RemoveModel represents the situation when a model is removed
Expand Down Expand Up @@ -171,6 +178,44 @@ type RemoveUnit struct {
Name string
}

// RelationChange represents either a new relation, or a change
// to an existing relation in a model.
type RelationChange struct {
ModelUUID string
Key string
Endpoints []Endpoint
}

// Endpoint holds all relevant information about a relation endpoint.
type Endpoint struct {
Application string
Name string
Role string
Interface string
Optional bool
Limit int
Scope string
}

// copy returns a deep copy of the RelationChange.
func (c RelationChange) copy() RelationChange {
if existing := c.Endpoints; existing != nil {
endpoints := make([]Endpoint, len(existing))
for i, ep := range existing {
endpoints[i] = ep
}
c.Endpoints = existing
}
return c
}

// RemoveRelation represents the situation when a relation
// is removed from a model in the database.
type RemoveRelation struct {
ModelUUID string
Key string
}

// MachineChange represents either a new machine, or a change
// to an existing machine in a model.
type MachineChange struct {
Expand Down
14 changes: 14 additions & 0 deletions core/cache/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ func (c *Controller) loop() error {
c.updateUnit(ch)
case RemoveUnit:
err = c.removeUnit(ch)
case RelationChange:
c.updateRelation(ch)
case RemoveRelation:
err = c.removeRelation(ch)
case BranchChange:
c.updateBranch(ch)
case RemoveBranch:
Expand Down Expand Up @@ -303,6 +307,16 @@ func (c *Controller) removeUnit(ch RemoveUnit) error {
return errors.Trace(c.removeResident(ch.ModelUUID, func(m *Model) error { return m.removeUnit(ch) }))
}

// updateRelation adds or updates the relation in the specified model.
func (c *Controller) updateRelation(ch RelationChange) {
c.ensureModel(ch.ModelUUID).updateRelation(ch, c.manager)
}

// removeRelation removes the relation from the cached model.
func (c *Controller) removeRelation(ch RemoveRelation) error {
return errors.Trace(c.removeResident(ch.ModelUUID, func(m *Model) error { return m.removeRelation(ch) }))
}

// updateMachine adds or updates the machine in the specified model.
func (c *Controller) updateMachine(ch MachineChange) {
c.ensureModel(ch.ModelUUID).updateMachine(ch, c.manager)
Expand Down
37 changes: 37 additions & 0 deletions core/cache/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (s *ControllerSuite) TestAddModel(c *gc.C) {
"charm-count": 0,
"machine-count": 0,
"unit-count": 0,
"relation-count": 0,
"branch-count": 0,
}})

Expand Down Expand Up @@ -280,6 +281,38 @@ func (s *ControllerSuite) TestRemoveUnit(c *gc.C) {
s.AssertResident(c, unit.CacheId(), false)
}

func (s *ControllerSuite) TestAddRelation(c *gc.C) {
controller, events := s.new(c)
s.processChange(c, relationChange, events)

mod, err := controller.Model(relationChange.ModelUUID)
c.Assert(err, jc.ErrorIsNil)
c.Check(mod.Report()["relation-count"], gc.Equals, 1)

relation, err := mod.Relation(relationChange.Key)
c.Assert(err, jc.ErrorIsNil)
s.AssertResident(c, relation.CacheId(), true)
}

func (s *ControllerSuite) TestRemoveRelation(c *gc.C) {
controller, events := s.new(c)
s.processChange(c, relationChange, events)

mod, err := controller.Model(relationChange.ModelUUID)
c.Assert(err, jc.ErrorIsNil)
relation, err := mod.Relation(relationChange.Key)
c.Assert(err, jc.ErrorIsNil)

remove := cache.RemoveRelation{
ModelUUID: modelChange.ModelUUID,
Key: relationChange.Key,
}
s.processChange(c, remove, events)

c.Check(mod.Report()["relation-count"], gc.Equals, 0)
s.AssertResident(c, relation.CacheId(), false)
}

func (s *ControllerSuite) TestAddBranch(c *gc.C) {
controller, events := s.new(c)
s.processChange(c, branchChange, events)
Expand Down Expand Up @@ -380,6 +413,10 @@ func (s *ControllerSuite) captureEvents(c *gc.C) <-chan interface{} {
send = true
case cache.RemoveUnit:
send = true
case cache.RelationChange:
send = true
case cache.RemoveRelation:
send = true
case cache.BranchChange:
send = true
case cache.RemoveBranch:
Expand Down
58 changes: 57 additions & 1 deletion core/cache/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func newModel(metrics *ControllerGauges, hub *pubsub.SimpleHub, res *Resident) *
charms: make(map[string]*Charm),
machines: make(map[string]*Machine),
units: make(map[string]*Unit),
relations: make(map[string]*Relation),
branches: make(map[string]*Branch),
}
return m
Expand All @@ -59,6 +60,7 @@ type Model struct {
charms map[string]*Charm
machines map[string]*Machine
units map[string]*Unit
relations map[string]*Relation
branches map[string]*Branch
}

Expand Down Expand Up @@ -105,6 +107,7 @@ func (m *Model) Report() map[string]interface{} {
"charm-count": len(m.charms),
"machine-count": len(m.machines),
"unit-count": len(m.units),
"relation-count": len(m.relations),
"branch-count": len(m.branches),
}
}
Expand All @@ -117,7 +120,7 @@ func (m *Model) Branches() []Branch {
i := 0
for _, b := range m.branches {
branches[i] = b.copy()
i += 1
i++
}

m.mu.Unlock()
Expand Down Expand Up @@ -341,6 +344,59 @@ func (m *Model) removeUnit(ch RemoveUnit) error {
return nil
}

// Relation returns the relation with the specified key.
// If the relation is not found, a NotFoundError is returned.
func (m *Model) Relation(key string) (Relation, error) {
defer m.doLocked()()

relation, found := m.relations[key]
if !found {
return Relation{}, errors.NotFoundf("relation %q", key)
}
return relation.copy(), nil
}

// Relations returns all relations in the model.
func (m *Model) Relations() map[string]Relation {
m.mu.Lock()

relations := make(map[string]Relation, len(m.relations))
for key, r := range m.relations {
relations[key] = r.copy()
}

m.mu.Unlock()
return relations
}

// updateRelation adds or updates the relation in the model.
func (m *Model) updateRelation(ch RelationChange, rm *residentManager) {
m.mu.Lock()

relation, found := m.relations[ch.Key]
if !found {
relation = newRelation(m, rm.new())
m.relations[ch.Key] = relation
}
relation.setDetails(ch)

m.mu.Unlock()
}

// removeRelation removes the relation from the model.
func (m *Model) removeRelation(ch RemoveRelation) error {
defer m.doLocked()()

relation, ok := m.relations[ch.Key]
if ok {
if err := relation.evict(); err != nil {
return errors.Trace(err)
}
delete(m.relations, ch.Key)
}
return nil
}

// updateMachine adds or updates the machine in the model.
func (m *Model) updateMachine(ch MachineChange, rm *residentManager) {
m.mu.Lock()
Expand Down
15 changes: 11 additions & 4 deletions core/cache/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/juju/juju/core/cache"
"github.com/juju/juju/core/life"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/permission"
"github.com/juju/juju/core/status"
"github.com/juju/juju/testing"
)
Expand All @@ -34,6 +35,7 @@ func (s *ModelSuite) TestReport(c *gc.C) {
"charm-count": 0,
"machine-count": 0,
"unit-count": 0,
"relation-count": 0,
"branch-count": 0,
})
}
Expand Down Expand Up @@ -360,10 +362,11 @@ func (s *ModelSuite) TestWaitForUnitCancelClosesChannel(c *gc.C) {
}

var modelChange = cache.ModelChange{
ModelUUID: "model-uuid",
Name: "test-model",
Life: life.Alive,
Owner: "model-owner",
ModelUUID: "model-uuid",
Name: "test-model",
Life: life.Alive,
Owner: "model-owner",
IsController: false,
Config: map[string]interface{}{
"key": "value",
"another": "foo",
Expand All @@ -372,4 +375,8 @@ var modelChange = cache.ModelChange{
Status: status.StatusInfo{
Status: status.Active,
},
UserPermissions: map[string]permission.Access{
"model-owner": permission.AdminAccess,
"read-user": permission.ReadAccess,
},
}
1 change: 1 addition & 0 deletions core/cache/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (*ImportSuite) TestImports(c *gc.C) {
"core/life",
"core/lxdprofile",
"core/network",
"core/permission",
"core/settings",
"core/status",
})
Expand Down

0 comments on commit 341cb40

Please sign in to comment.