-
Notifications
You must be signed in to change notification settings - Fork 283
/
cdn.go
202 lines (177 loc) · 5.57 KB
/
cdn.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
/*
* Copyright 2020 The Dragonfly 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 cdn
import (
"context"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"d7y.io/dragonfly/v2/cdn/config"
"d7y.io/dragonfly/v2/cdn/gc"
"d7y.io/dragonfly/v2/cdn/metrics"
"d7y.io/dragonfly/v2/cdn/rpcserver"
"d7y.io/dragonfly/v2/cdn/supervisor"
"d7y.io/dragonfly/v2/cdn/supervisor/cdn"
"d7y.io/dragonfly/v2/cdn/supervisor/cdn/storage"
"d7y.io/dragonfly/v2/cdn/supervisor/progress"
"d7y.io/dragonfly/v2/cdn/supervisor/task"
logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/pkg/rpc/manager"
managerClient "d7y.io/dragonfly/v2/pkg/rpc/manager/client"
"d7y.io/dragonfly/v2/pkg/util/hostutils"
)
type Server struct {
// Server configuration
config *config.Config
// GRPC server
grpcServer *rpcserver.Server
// Metrics server
metricsServer *metrics.Server
// Manager client
configServer managerClient.Client
// gc Server
gcServer *gc.Server
}
// New creates a brand-new server instance.
func New(config *config.Config) (*Server, error) {
// Initialize task manager
taskManager, err := task.NewManager(config.Task)
if err != nil {
return nil, errors.Wrapf(err, "create task manager")
}
// Initialize progress manager
progressManager, err := progress.NewManager(taskManager)
if err != nil {
return nil, errors.Wrapf(err, "create progress manager")
}
// Initialize storage manager
storageManager, err := storage.NewManager(config.Storage, taskManager)
if err != nil {
return nil, errors.Wrapf(err, "create storage manager")
}
// Initialize CDN manager
cdnManager, err := cdn.NewManager(config.CDN, storageManager, progressManager, taskManager)
if err != nil {
return nil, errors.Wrapf(err, "create cdn manager")
}
// Initialize CDN service
service, err := supervisor.NewCDNService(taskManager, cdnManager, progressManager)
if err != nil {
return nil, errors.Wrapf(err, "create cdn service")
}
// Initialize storage manager
var opts []grpc.ServerOption
if config.Options.Telemetry.Jaeger != "" {
opts = append(opts, grpc.ChainUnaryInterceptor(otelgrpc.UnaryServerInterceptor()), grpc.ChainStreamInterceptor(otelgrpc.StreamServerInterceptor()))
}
grpcServer, err := rpcserver.New(config.RPCServer, service, opts...)
if err != nil {
return nil, errors.Wrap(err, "create rpcServer")
}
// Initialize gc server
gcServer, err := gc.New()
if err != nil {
return nil, errors.Wrap(err, "create gcServer")
}
var metricsServer *metrics.Server
if config.Metrics.Addr != "" {
// Initialize metrics server
metricsServer, err = metrics.New(config.Metrics, grpcServer.Server)
if err != nil {
return nil, errors.Wrap(err, "create metricsServer")
}
}
// Initialize configServer
var configServer managerClient.Client
if config.Manager.Addr != "" {
configServer, err = managerClient.New(config.Manager.Addr)
if err != nil {
return nil, errors.Wrap(err, "create configServer")
}
}
return &Server{
config: config,
grpcServer: grpcServer,
metricsServer: metricsServer,
configServer: configServer,
gcServer: gcServer,
}, nil
}
func (s *Server) Serve() error {
go func() {
// Start GC
if err := s.gcServer.Serve(); err != nil {
logger.Fatalf("start gc task failed: %v", err)
}
}()
go func() {
if s.metricsServer != nil {
// Start metrics server
if err := s.metricsServer.ListenAndServe(s.metricsServer.Handler()); err != nil {
logger.Fatalf("start metrics server failed: %v", err)
}
}
}()
go func() {
if s.configServer != nil {
var rpcServerConfig = s.grpcServer.GetConfig()
CDNInstance, err := s.configServer.UpdateCDN(&manager.UpdateCDNRequest{
SourceType: manager.SourceType_CDN_SOURCE,
HostName: hostutils.FQDNHostname,
Ip: rpcServerConfig.AdvertiseIP,
Port: int32(rpcServerConfig.ListenPort),
DownloadPort: int32(rpcServerConfig.DownloadPort),
Idc: s.config.Host.IDC,
Location: s.config.Host.Location,
CdnClusterId: uint64(s.config.Manager.CDNClusterID),
})
if err != nil {
logger.Fatalf("update cdn instance failed: %v", err)
}
// Serve Keepalive
logger.Infof("====starting keepalive cdn instance %s to manager %s====", CDNInstance, s.config.Manager.Addr)
s.configServer.KeepAlive(s.config.Manager.KeepAlive.Interval, &manager.KeepAliveRequest{
HostName: hostutils.FQDNHostname,
SourceType: manager.SourceType_CDN_SOURCE,
ClusterId: uint64(s.config.Manager.CDNClusterID),
})
}
}()
// Start grpc server
return s.grpcServer.ListenAndServe()
}
func (s *Server) Stop() error {
g, ctx := errgroup.WithContext(context.Background())
g.Go(func() error {
return s.gcServer.Shutdown()
})
if s.configServer != nil {
// Stop manager client
g.Go(func() error {
return s.configServer.Close()
})
}
g.Go(func() error {
// Stop metrics server
return s.metricsServer.Shutdown(ctx)
})
g.Go(func() error {
// Stop grpc server
return s.grpcServer.Shutdown()
})
return g.Wait()
}