forked from istio/istio
/
runtime.go
195 lines (151 loc) · 5.44 KB
/
runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2018 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package runtime
import (
"errors"
"sync"
"time"
"istio.io/istio/mixer/pkg/adapter"
"istio.io/istio/mixer/pkg/config/store"
"istio.io/istio/mixer/pkg/pool"
"istio.io/istio/mixer/pkg/runtime/config"
"istio.io/istio/mixer/pkg/runtime/dispatcher"
"istio.io/istio/mixer/pkg/runtime/handler"
"istio.io/istio/mixer/pkg/runtime/routing"
"istio.io/istio/mixer/pkg/template"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/probe"
)
var errNotListening = errors.New("runtime is not listening to the store")
const watchFlushDuration = time.Second
// Runtime is the main entry point to the Mixer runtime environment. It listens to configuration, instantiates handler
// instances, creates the dispatch machinery and handles incoming requests.
type Runtime struct {
defaultConfigNamespace string
ephemeral *config.Ephemeral
snapshot *config.Snapshot
handlers *handler.Table
dispatcher *dispatcher.Impl
store store.Store
handlerPool *pool.GoroutinePool
*probe.Probe
stateLock sync.Mutex
shutdown chan struct{}
waitQuiesceListening sync.WaitGroup
}
// New returns a new instance of Runtime.
func New(
s store.Store,
templates map[string]*template.Info,
adapters map[string]*adapter.Info,
defaultConfigNamespace string,
executorPool *pool.GoroutinePool,
handlerPool *pool.GoroutinePool,
enableTracing bool) *Runtime {
// Ignoring the errors for bad configuration that has already made it to the store.
// during snapshot creation the bad configuration errors are already logged.
e := config.NewEphemeral(templates, adapters)
rt := &Runtime{
defaultConfigNamespace: defaultConfigNamespace,
ephemeral: e,
snapshot: config.Empty(),
handlers: handler.Empty(),
dispatcher: dispatcher.New(executorPool, enableTracing),
handlerPool: handlerPool,
Probe: probe.NewProbe(),
store: s,
}
// Make sure we have a stable state.
rt.processNewConfig()
rt.Probe.SetAvailable(errNotListening)
return rt
}
// Dispatcher returns the dispatcher.Dispatcher that is implemented by this runtime package.
func (c *Runtime) Dispatcher() dispatcher.Dispatcher {
return c.dispatcher
}
// StartListening directs Runtime to start listening to configuration changes. As config changes, runtime processes
// the confguration and creates a dispatcher.
func (c *Runtime) StartListening() error {
c.stateLock.Lock()
defer c.stateLock.Unlock()
if c.shutdown != nil {
return errors.New("already listening")
}
data, watchChan, err := store.StartWatch(c.store)
if err != nil {
return err
}
c.ephemeral.SetState(data)
c.processNewConfig()
c.shutdown = make(chan struct{})
c.waitQuiesceListening.Add(1)
go func() {
store.WatchChanges(watchChan, c.shutdown, watchFlushDuration, c.onConfigChange)
c.waitQuiesceListening.Done()
}()
c.Probe.SetAvailable(nil)
return nil
}
// StopListening directs Runtime to stop listening to configuration changes. It will not unload the current
// configuration, or close the existing adapters.
func (c *Runtime) StopListening() {
c.stateLock.Lock()
defer c.stateLock.Unlock()
if c.shutdown != nil {
c.shutdown <- struct{}{}
c.shutdown = nil
c.waitQuiesceListening.Wait()
c.store.Stop()
c.Probe.SetAvailable(errNotListening)
}
}
func (c *Runtime) onConfigChange(events []*store.Event) {
c.ephemeral.ApplyEvent(events)
c.processNewConfig()
}
func (c *Runtime) processNewConfig() {
newSnapshot, _ := c.ephemeral.BuildSnapshot()
oldHandlers := c.handlers
newHandlers := handler.NewTable(oldHandlers, newSnapshot, c.handlerPool)
newRoutes := routing.BuildTable(
newHandlers, newSnapshot, c.defaultConfigNamespace, log.DebugEnabled())
oldContext := c.dispatcher.ChangeRoute(newRoutes)
c.handlers = newHandlers
c.snapshot = newSnapshot
log.Debugf("New routes in effect:\n%s", newRoutes)
cleanupHandlers(oldContext, oldHandlers, newHandlers, maxCleanupDuration)
}
// maxCleanupDuration is the maximum amount of time cleanup operation will wait
// before resolver ref count goes to 0. It will return after this duration without
// calling Close() on handlers.
const maxCleanupDuration = 10 * time.Second
const cleanupSleepTime = 500 * time.Millisecond
func cleanupHandlers(oldContext *dispatcher.RoutingContext, oldHandlers *handler.Table, currentHandlers *handler.Table, timeout time.Duration) {
start := time.Now()
for {
rc := oldContext.GetRefs()
if rc <= 0 {
log.Infof("Cleaning up handler table, with config ID:%d", oldContext.Routes.ID())
oldHandlers.Cleanup(currentHandlers)
return
}
if time.Since(start) > timeout {
log.Errorf("unable to cleanup handlers(config id:%d) in %v, %d requests remain", oldContext.Routes.ID(), timeout, rc)
return
}
log.Debugf("Waiting for dispatches using routes with config ID '%d' to finish: %d remaining requests", oldContext.Routes.ID(), rc)
time.Sleep(cleanupSleepTime)
}
}