~fwereade/juju-core/uniter-relations-firmer-sketch

« back to all changes in this revision

Viewing changes to worker/uniter/filter.go

  • Committer: William Reade
  • Date: 2012-10-11 08:39:05 UTC
  • Revision ID: fwereade@gmail.com-20121011083905-ce8u2mpvxocrlix6
add relation events to filter

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
        // The out* chans, when set to the corresponding out*On chan (rather than
25
25
        // nil) indicate that an event of the appropriate type is ready to send
26
26
        // to the client.
27
 
        outConfig     chan struct{}
28
 
        outConfigOn   chan struct{}
29
 
        outUpgrade    chan *state.Charm
30
 
        outUpgradeOn  chan *state.Charm
31
 
        outResolved   chan state.ResolvedMode
32
 
        outResolvedOn chan state.ResolvedMode
 
27
        outConfig      chan struct{}
 
28
        outConfigOn    chan struct{}
 
29
        outUpgrade     chan *state.Charm
 
30
        outUpgradeOn   chan *state.Charm
 
31
        outResolved    chan state.ResolvedMode
 
32
        outResolvedOn  chan state.ResolvedMode
 
33
        outRelations   chan map[int]struct{}
 
34
        outRelationsOn chan map[int]struct{}
33
35
 
34
36
        // The want* chans are used to indicate that the filter should send
35
37
        // events if it has them available.
49
51
        upgradeRequested serviceCharm
50
52
        upgradeAvailable serviceCharm
51
53
        upgrade          *state.Charm
 
54
        relations        map[int]struct{}
52
55
}
53
56
 
54
57
// newFilter returns a filter that handles state changes pertaining to the
55
58
// supplied unit.
56
59
func newFilter(st *state.State, unitName string) (*filter, error) {
57
60
        f := &filter{
58
 
                st:            st,
59
 
                outUnitDying:  make(chan struct{}),
60
 
                outConfig:     make(chan struct{}),
61
 
                outConfigOn:   make(chan struct{}),
62
 
                outUpgrade:    make(chan *state.Charm),
63
 
                outUpgradeOn:  make(chan *state.Charm),
64
 
                outResolved:   make(chan state.ResolvedMode),
65
 
                outResolvedOn: make(chan state.ResolvedMode),
66
 
                wantResolved:  make(chan struct{}),
67
 
                wantUpgrade:   make(chan serviceCharm),
68
 
                resetConfig:   make(chan struct{}),
 
61
                st:             st,
 
62
                outUnitDying:   make(chan struct{}),
 
63
                outConfig:      make(chan struct{}),
 
64
                outConfigOn:    make(chan struct{}),
 
65
                outUpgrade:     make(chan *state.Charm),
 
66
                outUpgradeOn:   make(chan *state.Charm),
 
67
                outResolved:    make(chan state.ResolvedMode),
 
68
                outResolvedOn:  make(chan state.ResolvedMode),
 
69
                outRelations:   make(chan map[int]struct{}),
 
70
                outRelationsOn: make(chan map[int]struct{}),
 
71
                wantResolved:   make(chan struct{}),
 
72
                wantUpgrade:    make(chan serviceCharm),
 
73
                resetConfig:    make(chan struct{}),
69
74
        }
70
75
        go func() {
71
76
                defer f.tomb.Done()
115
120
        return f.outConfigOn
116
121
}
117
122
 
 
123
// RelationsEvents returns a channel that will receive the ids of all the service's
 
124
// relations whose Life status has changed.
 
125
func (f *filter) RelationsEvents() <-chan map[int]struct{} {
 
126
        return f.outRelationsOn
 
127
}
 
128
 
118
129
// WantUpgradeEvent sets the baseline from which service charm changes will
119
130
// be considered for upgrade. Any service charm with a URL different from
120
131
// that supplied will be considered; if mustForce is true, unforced service
166
177
        defer watcher.Stop(servicew, &f.tomb)
167
178
        configw := f.service.WatchConfig()
168
179
        defer watcher.Stop(configw, &f.tomb)
 
180
        relationsw := f.service.WatchRelations()
 
181
        defer watcher.Stop(relationsw, &f.tomb)
169
182
 
170
183
        // Config events cannot be meaningfully reset until one is available;
171
184
        // once we receive the initial change, we unblock reset requests by
195
208
                                return err
196
209
                        }
197
210
                case _, ok := <-configw.Changes():
198
 
                        log.Debugf("filter got config change")
 
211
                        log.Debugf("filter: got config change")
199
212
                        if !ok {
200
213
                                return watcher.MustErr(configw)
201
214
                        }
202
215
                        log.Debugf("filter: preparing new config event")
203
216
                        f.outConfig = f.outConfigOn
204
217
                        resetConfig = f.resetConfig
 
218
                case ids, ok := <-relationsw.Changes():
 
219
                        log.Debugf("filter: got relations change")
 
220
                        if !ok {
 
221
                                return watcher.MustErr(relationsw)
 
222
                        }
 
223
                        f.relationsChanged(ids)
205
224
 
206
225
                // Send events on active out chans.
207
226
                case f.outUpgrade <- f.upgrade:
214
233
                case f.outConfig <- nothing:
215
234
                        log.Debugf("filter: sent config event")
216
235
                        f.outConfig = nil
 
236
                case f.outRelations <- f.relations:
 
237
                        log.Debugf("filter: sent relations event")
 
238
                        f.outRelations = nil
 
239
                        f.relations = nil
217
240
 
218
241
                // Handle explicit requests.
219
242
                case req := <-f.wantUpgrade:
297
320
        return nil
298
321
}
299
322
 
 
323
// relationsChanged responds to service relation changes.
 
324
func (f *filter) relationsChanged(ids []int) {
 
325
        if f.relations == nil {
 
326
                f.relations = map[int]struct{}{}
 
327
        }
 
328
        for _, id := range ids {
 
329
                f.relations[id] = nothing
 
330
        }
 
331
        if len(f.relations) != 0 {
 
332
                f.outRelations = f.outRelationsOn
 
333
        }
 
334
}
 
335
 
300
336
// serviceCharm holds information about a charm.
301
337
type serviceCharm struct {
302
338
        url   *charm.URL