/
main.go
248 lines (205 loc) · 7.65 KB
/
main.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
// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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 (
"context"
"net"
"net/http"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/golang/glog"
"github.com/google/trillian"
"github.com/google/trillian/extension"
"github.com/google/trillian/monitoring"
"github.com/google/trillian/server/admin"
"github.com/google/trillian/server/interceptor"
"github.com/google/trillian/util"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/naming"
"google.golang.org/grpc/reflection"
etcdnaming "github.com/coreos/etcd/clientv3/naming"
)
const (
// DefaultTreeDeleteThreshold is the suggested threshold for tree deletion.
// It represents the minimum time a tree has to remain Deleted before being hard-deleted.
DefaultTreeDeleteThreshold = 7 * 24 * time.Hour
// DefaultTreeDeleteMinInterval is the suggested min interval between tree GC sweeps.
// A tree GC sweep consists of listing deleted trees older than the deletion threshold and
// hard-deleting them.
// Actual runs happen randomly between [minInterval,2*minInterval).
DefaultTreeDeleteMinInterval = 4 * time.Hour
)
// Main encapsulates the data and logic to start a Trillian server (Log or Map).
type Main struct {
// Endpoints for RPC and HTTP/REST servers.
// HTTP/REST is optional, if empty it'll not be bound.
RPCEndpoint, HTTPEndpoint string
// TLS Certificate and Key files for the server.
TLSCertFile, TLSKeyFile string
DBClose func() error
Registry extension.Registry
StatsPrefix string
QuotaDryRun bool
// RegisterHandlerFn is called to register REST-proxy handlers.
RegisterHandlerFn func(context.Context, *runtime.ServeMux, string, []grpc.DialOption) error
// RegisterServerFn is called to register RPC servers.
RegisterServerFn func(*grpc.Server, extension.Registry) error
// IsHealthy will be called whenever "/healthz" is called on the mux.
// A nil return value from this function will result in a 200-OK response
// on the /healthz endpoint.
IsHealthy func(context.Context) error
// HealthyDeadline is the maximum duration to wait wait for a successful
// IsHealthy() call.
HealthyDeadline time.Duration
// AllowedTreeTypes determines which types of trees may be created through the Admin Server
// bound by Main. nil means unrestricted.
AllowedTreeTypes []trillian.TreeType
TreeGCEnabled bool
TreeDeleteThreshold time.Duration
TreeDeleteMinInterval time.Duration
// These will be added to the GRPC server options.
ExtraOptions []grpc.ServerOption
}
func (m *Main) healthz(rw http.ResponseWriter, req *http.Request) {
if m.IsHealthy != nil {
ctx, cancel := context.WithTimeout(req.Context(), m.HealthyDeadline)
defer cancel()
if err := m.IsHealthy(ctx); err != nil {
rw.WriteHeader(http.StatusServiceUnavailable)
rw.Write([]byte(err.Error()))
return
}
}
rw.Write([]byte("ok"))
}
// Run starts the configured server. Blocks until the server exits.
func (m *Main) Run(ctx context.Context) error {
glog.CopyStandardLogTo("WARNING")
if m.HealthyDeadline == 0 {
m.HealthyDeadline = 5 * time.Second
}
srv, err := m.newGRPCServer()
if err != nil {
glog.Exitf("Error creating gRPC server: %v", err)
}
defer srv.GracefulStop()
defer m.DBClose()
if err := m.RegisterServerFn(srv, m.Registry); err != nil {
return err
}
trillian.RegisterTrillianAdminServer(srv, admin.New(m.Registry, m.AllowedTreeTypes))
reflection.Register(srv)
if endpoint := m.HTTPEndpoint; endpoint != "" {
gatewayMux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
if err := m.RegisterHandlerFn(ctx, gatewayMux, m.RPCEndpoint, opts); err != nil {
return err
}
if err := trillian.RegisterTrillianAdminHandlerFromEndpoint(ctx, gatewayMux, m.RPCEndpoint, opts); err != nil {
return err
}
http.Handle("/", gatewayMux)
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/healthz", m.healthz)
go func() {
glog.Infof("HTTP server starting on %v", endpoint)
var err error
// Let http.ListenAndServeTLS handle the error case when only one of the flags is set.
if m.TLSCertFile != "" || m.TLSKeyFile != "" {
err = http.ListenAndServeTLS(endpoint, m.TLSCertFile, m.TLSKeyFile, nil)
} else {
err = http.ListenAndServe(endpoint, nil)
}
if err != nil {
glog.Errorf("HTTP server stopped: %v", err)
}
}()
}
glog.Infof("RPC server starting on %v", m.RPCEndpoint)
lis, err := net.Listen("tcp", m.RPCEndpoint)
if err != nil {
return err
}
go util.AwaitSignal(srv.Stop)
if m.TreeGCEnabled {
go func() {
glog.Info("Deleted tree GC started")
gc := admin.NewDeletedTreeGC(
m.Registry.AdminStorage,
m.TreeDeleteThreshold,
m.TreeDeleteMinInterval,
m.Registry.MetricFactory)
gc.Run(ctx)
}()
}
if err := srv.Serve(lis); err != nil {
glog.Errorf("RPC server terminated: %v", err)
}
glog.Infof("Stopping server, about to exit")
glog.Flush()
// Give things a few seconds to tidy up
time.Sleep(time.Second * 5)
return nil
}
// newGRPCServer starts a new Trillian gRPC server.
func (m *Main) newGRPCServer() (*grpc.Server, error) {
ts := util.SystemTimeSource{}
stats := monitoring.NewRPCStatsInterceptor(ts, m.StatsPrefix, m.Registry.MetricFactory)
ti := interceptor.New(
m.Registry.AdminStorage, m.Registry.QuotaManager, m.QuotaDryRun, m.Registry.MetricFactory)
netInterceptor := interceptor.Combine(stats.Interceptor(), interceptor.ErrorWrapper, ti.UnaryInterceptor)
serverOpts := []grpc.ServerOption{
grpc.UnaryInterceptor(netInterceptor),
}
serverOpts = append(serverOpts, m.ExtraOptions...)
// Let credentials.NewServerTLSFromFile handle the error case when only one of the flags is set.
if m.TLSCertFile != "" || m.TLSKeyFile != "" {
serverCreds, err := credentials.NewServerTLSFromFile(m.TLSCertFile, m.TLSKeyFile)
if err != nil {
return nil, err
}
serverOpts = append(serverOpts, grpc.Creds(serverCreds))
}
s := grpc.NewServer(serverOpts...)
return s, nil
}
// AnnounceSelf announces this binary's presence to etcd. Returns a function that
// should be called on process exit.
// AnnounceSelf does nothing if client is nil.
func AnnounceSelf(ctx context.Context, client *clientv3.Client, etcdService, endpoint string) func() {
if client == nil {
return func() {}
}
res := etcdnaming.GRPCResolver{Client: client}
// Get a lease so our entry self-destructs.
leaseRsp, err := client.Grant(ctx, 30)
if err != nil {
glog.Exitf("Failed to get lease from etcd: %v", err)
}
client.KeepAlive(ctx, leaseRsp.ID)
update := naming.Update{Op: naming.Add, Addr: endpoint} // nolint: megacheck
res.Update(ctx, etcdService, update, clientv3.WithLease(leaseRsp.ID))
glog.Infof("Announcing our presence in %v with %+v", etcdService, update)
bye := naming.Update{Op: naming.Delete, Addr: endpoint} // nolint: megacheck
return func() {
// Use a background context because the original context may have been cancelled.
glog.Infof("Removing our presence in %v with %+v", etcdService, bye)
ctx := context.Background()
res.Update(ctx, etcdService, bye)
client.Revoke(ctx, leaseRsp.ID)
}
}