This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
forked from istio/istio
/
server.go
383 lines (315 loc) · 10.7 KB
/
server.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright 2017 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 server
import (
"fmt"
"io"
"net"
"os"
"strings"
"time"
"github.com/gogo/protobuf/proto"
ot "github.com/opentracing/opentracing-go"
oprometheus "github.com/prometheus/client_golang/prometheus"
"go.opencensus.io/exporter/prometheus"
"go.opencensus.io/plugin/ocgrpc"
"go.opencensus.io/stats/view"
"google.golang.org/grpc"
"k8s.io/apimachinery/pkg/runtime/schema"
mixerpb "istio.io/api/mixer/v1"
"istio.io/istio/mixer/pkg/adapter"
"istio.io/istio/mixer/pkg/api"
"istio.io/istio/mixer/pkg/checkcache"
"istio.io/istio/mixer/pkg/config"
"istio.io/istio/mixer/pkg/config/crd"
"istio.io/istio/mixer/pkg/config/store"
"istio.io/istio/mixer/pkg/loadshedding"
"istio.io/istio/mixer/pkg/pool"
"istio.io/istio/mixer/pkg/runtime"
runtimeconfig "istio.io/istio/mixer/pkg/runtime/config"
"istio.io/istio/mixer/pkg/runtime/dispatcher"
"istio.io/istio/mixer/pkg/template"
"istio.io/istio/pkg/ctrlz"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/probe"
"istio.io/istio/pkg/tracing"
)
// Server is an in-memory Mixer service.
type Server struct {
shutdown chan error
server *grpc.Server
gp *pool.GoroutinePool
adapterGP *pool.GoroutinePool
listener net.Listener
monitor *monitor
tracer io.Closer
checkCache *checkcache.Cache
dispatcher dispatcher.Dispatcher
controlZ *ctrlz.Server
// probes
livenessProbe probe.Controller
readinessProbe probe.Controller
*probe.Probe
configStore store.Store
}
type listenFunc func(network string, address string) (net.Listener, error)
// replaceable set of functions for fault injection
type patchTable struct {
newRuntime func(s store.Store, templates map[string]*template.Info, adapters map[string]*adapter.Info,
defaultConfigNamespace string, executorPool *pool.GoroutinePool,
handlerPool *pool.GoroutinePool, enableTracing bool) *runtime.Runtime
configTracing func(serviceName string, options *tracing.Options) (io.Closer, error)
startMonitor func(port uint16, enableProfiling bool, lf listenFunc) (*monitor, error)
listen listenFunc
configLog func(options *log.Options) error
runtimeListen func(runtime *runtime.Runtime) error
remove func(name string) error
// monitoring-related setup
newOpenCensusExporter func() (view.Exporter, error)
registerOpenCensusViews func(...*view.View) error
}
// New instantiates a fully functional Mixer server, ready for traffic.
func New(a *Args) (*Server, error) {
return newServer(a, newPatchTable())
}
func newPatchTable() *patchTable {
return &patchTable{
newRuntime: runtime.New,
configTracing: tracing.Configure,
startMonitor: startMonitor,
listen: net.Listen,
configLog: log.Configure,
runtimeListen: func(rt *runtime.Runtime) error { return rt.StartListening() },
remove: os.Remove,
newOpenCensusExporter: func() (view.Exporter, error) {
return prometheus.NewExporter(prometheus.Options{Registry: oprometheus.DefaultRegisterer.(*oprometheus.Registry)})
},
registerOpenCensusViews: view.Register,
}
}
func newServer(a *Args, p *patchTable) (*Server, error) {
if err := a.validate(); err != nil {
return nil, err
}
if err := p.configLog(a.LoggingOptions); err != nil {
return nil, err
}
s := &Server{}
s.gp = pool.NewGoroutinePool(a.APIWorkerPoolSize, a.SingleThreaded)
s.gp.AddWorkers(a.APIWorkerPoolSize - 1)
s.adapterGP = pool.NewGoroutinePool(a.AdapterWorkerPoolSize, a.SingleThreaded)
s.adapterGP.AddWorkers(a.AdapterWorkerPoolSize - 1)
tmplRepo := template.NewRepository(a.Templates)
adapterMap := config.AdapterInfoMap(a.Adapters, tmplRepo.SupportsTemplate)
s.Probe = probe.NewProbe()
// construct the gRPC options
var grpcOptions []grpc.ServerOption
grpcOptions = append(grpcOptions, grpc.MaxConcurrentStreams(uint32(a.MaxConcurrentStreams)))
grpcOptions = append(grpcOptions, grpc.MaxRecvMsgSize(int(a.MaxMessageSize)))
var err error
if a.TracingOptions.TracingEnabled() {
s.tracer, err = p.configTracing("istio-mixer", a.TracingOptions)
if err != nil {
_ = s.Close()
return nil, fmt.Errorf("unable to setup tracing")
}
grpcOptions = append(grpcOptions, grpc.UnaryInterceptor(TracingServerInterceptor(ot.GlobalTracer())))
}
// get the network stuff setup
network, address := extractNetAddress(a.APIPort, a.APIAddress)
if network == "unix" {
// remove Unix socket before use.
if err = p.remove(address); err != nil && !os.IsNotExist(err) {
_ = s.Close()
// Anything other than "file not found" is an error.
return nil, fmt.Errorf("unable to remove unix://%s: %v", address, err)
}
}
if s.listener, err = p.listen(network, address); err != nil {
_ = s.Close()
return nil, fmt.Errorf("unable to listen: %v", err)
}
st := a.ConfigStore
if st != nil && a.ConfigStoreURL != "" {
_ = s.Close()
return nil, fmt.Errorf("invalid arguments: both ConfigStore and ConfigStoreURL are specified")
}
if st == nil {
configStoreURL := a.ConfigStoreURL
if configStoreURL == "" {
configStoreURL = "k8s://"
}
reg := store.NewRegistry(config.StoreInventory()...)
groupVersion := &schema.GroupVersion{Group: crd.ConfigAPIGroup, Version: crd.ConfigAPIVersion}
if st, err = reg.NewStore(configStoreURL, groupVersion, a.CredentialOptions, runtimeconfig.CriticalKinds()); err != nil {
_ = s.Close()
return nil, fmt.Errorf("unable to connect to the configuration server: %v", err)
}
}
var rt *runtime.Runtime
templateMap := make(map[string]*template.Info, len(a.Templates))
for k, v := range a.Templates {
t := v // Make a local copy, otherwise we end up capturing the location of the last entry
templateMap[k] = &t
}
var kinds map[string]proto.Message
if a.UseAdapterCRDs {
kinds = runtimeconfig.KindMap(adapterMap, templateMap)
} else {
kinds = runtimeconfig.KindMap(map[string]*adapter.Info{}, templateMap)
}
if err := st.Init(kinds); err != nil {
_ = s.Close()
return nil, fmt.Errorf("unable to initialize config store: %v", err)
}
// block wait for the config store to sync
log.Info("Awaiting for config store sync...")
if err := st.WaitForSynced(30 * time.Second); err != nil {
_ = s.Close()
return nil, err
}
s.configStore = st
log.Info("Starting runtime config watch...")
rt = p.newRuntime(st, templateMap, adapterMap, a.ConfigDefaultNamespace,
s.gp, s.adapterGP, a.TracingOptions.TracingEnabled())
if err = p.runtimeListen(rt); err != nil {
_ = s.Close()
return nil, fmt.Errorf("unable to listen: %v", err)
}
s.dispatcher = rt.Dispatcher()
// see issue https://github.com/istio/istio/issues/9596
a.NumCheckCacheEntries = 0
if a.NumCheckCacheEntries > 0 {
s.checkCache = checkcache.New(a.NumCheckCacheEntries)
}
// get the grpc server wired up
grpc.EnableTracing = a.EnableGRPCTracing
exporter, err := p.newOpenCensusExporter()
if err != nil {
_ = s.Close()
return nil, fmt.Errorf("could not build opencensus exporter: %v", err)
}
view.RegisterExporter(exporter)
// Register the views to collect server request count.
if err := p.registerOpenCensusViews(ocgrpc.DefaultServerViews...); err != nil {
_ = s.Close()
return nil, fmt.Errorf("could not register default server views: %v", err)
}
throttler := loadshedding.NewThrottler(a.LoadSheddingOptions)
if eval := throttler.Evaluator(loadshedding.GRPCLatencyEvaluatorName); eval != nil {
grpcOptions = append(grpcOptions, grpc.StatsHandler(newMultiStatsHandler(&ocgrpc.ServerHandler{}, eval.(*loadshedding.GRPCLatencyEvaluator))))
} else {
grpcOptions = append(grpcOptions, grpc.StatsHandler(&ocgrpc.ServerHandler{}))
}
s.server = grpc.NewServer(grpcOptions...)
mixerpb.RegisterMixerServer(s.server, api.NewGRPCServer(s.dispatcher, s.gp, s.checkCache, throttler))
if a.LivenessProbeOptions.IsValid() {
s.livenessProbe = probe.NewFileController(a.LivenessProbeOptions)
s.RegisterProbe(s.livenessProbe, "server")
s.livenessProbe.Start()
}
if a.ReadinessProbeOptions.IsValid() {
s.readinessProbe = probe.NewFileController(a.ReadinessProbeOptions)
rt.RegisterProbe(s.readinessProbe, "dispatcher")
st.RegisterProbe(s.readinessProbe, "store")
s.readinessProbe.Start()
}
log.Info("Starting monitor server...")
if s.monitor, err = p.startMonitor(a.MonitoringPort, a.EnableProfiling, p.listen); err != nil {
_ = s.Close()
return nil, fmt.Errorf("unable to setup monitoring: %v", err)
}
s.controlZ, _ = ctrlz.Run(a.IntrospectionOptions, nil)
return s, nil
}
func extractNetAddress(apiPort uint16, apiAddress string) (string, string) {
if apiAddress != "" {
idx := strings.Index(apiAddress, "://")
if idx < 0 {
return "tcp", apiAddress
}
return apiAddress[:idx], apiAddress[idx+3:]
}
return "tcp", fmt.Sprintf(":%d", apiPort)
}
// Run enables Mixer to start receiving gRPC requests on its main API port.
func (s *Server) Run() {
s.shutdown = make(chan error, 1)
s.SetAvailable(nil)
go func() {
// go to work...
err := s.server.Serve(s.listener)
// notify closer we're done
s.shutdown <- err
}()
}
// Wait waits for the server to exit.
func (s *Server) Wait() error {
if s.shutdown == nil {
return fmt.Errorf("server not running")
}
err := <-s.shutdown
s.shutdown = nil
return err
}
// Close cleans up resources used by the server.
func (s *Server) Close() error {
if s.shutdown != nil {
s.server.GracefulStop()
_ = s.Wait()
}
if s.controlZ != nil {
s.controlZ.Close()
}
if s.configStore != nil {
s.configStore.Stop()
s.configStore = nil
}
if s.checkCache != nil {
_ = s.checkCache.Close()
}
if s.listener != nil {
_ = s.listener.Close()
}
if s.tracer != nil {
_ = s.tracer.Close()
}
if s.monitor != nil {
_ = s.monitor.Close()
}
if s.gp != nil {
_ = s.gp.Close()
}
if s.adapterGP != nil {
_ = s.adapterGP.Close()
}
if s.livenessProbe != nil {
_ = s.livenessProbe.Close()
}
if s.readinessProbe != nil {
_ = s.readinessProbe.Close()
}
// final attempt to purge buffered logs
_ = log.Sync()
return nil
}
// Addr returns the address of the server's API port, where gRPC requests can be sent.
func (s *Server) Addr() net.Addr {
return s.listener.Addr()
}
// Dispatcher returns the dispatcher that was created during server creation. This should only
// be used for testing purposes only.
func (s *Server) Dispatcher() dispatcher.Dispatcher {
return s.dispatcher
}