-
Notifications
You must be signed in to change notification settings - Fork 499
/
serverpool.go
185 lines (157 loc) · 5.25 KB
/
serverpool.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
/*
* Copyright (c) 2017, MegaEase
* 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 proxies provides the common interface and implementation of proxies.
package proxies
import (
"fmt"
"sync"
"sync/atomic"
"github.com/megaease/easegress/v2/pkg/logger"
"github.com/megaease/easegress/v2/pkg/object/serviceregistry"
"github.com/megaease/easegress/v2/pkg/supervisor"
"github.com/megaease/easegress/v2/pkg/util/stringtool"
)
// ServerPoolImpl is the interface for server pool.
type ServerPoolImpl interface {
CreateLoadBalancer(spec *LoadBalanceSpec, servers []*Server) LoadBalancer
}
// ServerPoolBase defines a base server pool.
type ServerPoolBase struct {
spImpl ServerPoolImpl
Name string
done chan struct{}
wg sync.WaitGroup
loadBalancer atomic.Value
}
// ServerPoolBaseSpec is the spec for a base server pool.
type ServerPoolBaseSpec struct {
ServerTags []string `json:"serverTags" jsonschema:"omitempty,uniqueItems=true"`
Servers []*Server `json:"servers" jsonschema:"omitempty"`
ServiceRegistry string `json:"serviceRegistry" jsonschema:"omitempty"`
ServiceName string `json:"serviceName" jsonschema:"omitempty"`
LoadBalance *LoadBalanceSpec `json:"loadBalance" jsonschema:"omitempty"`
}
// Validate validates ServerPoolSpec.
func (sps *ServerPoolBaseSpec) Validate() error {
if sps.ServiceName == "" && len(sps.Servers) == 0 {
return fmt.Errorf("both serviceName and servers are empty")
}
serversGotWeight := 0
for _, server := range sps.Servers {
if server.Weight > 0 {
serversGotWeight++
}
}
if serversGotWeight > 0 && serversGotWeight < len(sps.Servers) {
msgFmt := "not all servers have weight(%d/%d)"
return fmt.Errorf(msgFmt, serversGotWeight, len(sps.Servers))
}
if sps.ServiceName != "" && sps.LoadBalance != nil && sps.LoadBalance.HealthCheck != nil {
return fmt.Errorf("can not open health check for service discovery")
}
return nil
}
// Init initialize the base server pool according to the spec.
func (spb *ServerPoolBase) Init(spImpl ServerPoolImpl, super *supervisor.Supervisor, name string, spec *ServerPoolBaseSpec) {
spb.spImpl = spImpl
spb.Name = name
spb.done = make(chan struct{})
if spec.ServiceRegistry == "" || spec.ServiceName == "" {
spb.createLoadBalancer(spec.LoadBalance, spec.Servers)
return
}
// watch service registry
entity := super.MustGetSystemController(serviceregistry.Kind)
registry := entity.Instance().(*serviceregistry.ServiceRegistry)
instances, err := registry.ListServiceInstances(spec.ServiceRegistry, spec.ServiceName)
if err != nil {
msgFmt := "first try to use service %s/%s failed(will try again): %v"
logger.Warnf(msgFmt, spec.ServiceRegistry, spec.ServiceName, err)
spb.createLoadBalancer(spec.LoadBalance, spec.Servers)
}
spb.useService(spec, instances)
watcher := registry.NewServiceWatcher(spec.ServiceRegistry, spec.ServiceName)
spb.wg.Add(1)
go func() {
for {
select {
case <-spb.done:
watcher.Stop()
spb.wg.Done()
return
case event := <-watcher.Watch():
spb.useService(spec, event.Instances)
}
}
}()
}
// LoadBalancer returns the load balancer of the server pool.
func (spb *ServerPoolBase) LoadBalancer() LoadBalancer {
if v := spb.loadBalancer.Load(); v != nil {
return v.(LoadBalancer)
}
return nil
}
func (spb *ServerPoolBase) createLoadBalancer(spec *LoadBalanceSpec, servers []*Server) {
for _, server := range servers {
server.CheckAddrPattern()
}
if spec == nil {
spec = &LoadBalanceSpec{}
}
lb := spb.spImpl.CreateLoadBalancer(spec, servers)
if old := spb.loadBalancer.Swap(lb); old != nil {
old.(LoadBalancer).Close()
}
}
func (spb *ServerPoolBase) useService(spec *ServerPoolBaseSpec, instances map[string]*serviceregistry.ServiceInstanceSpec) {
servers := make([]*Server, 0)
for _, instance := range instances {
// default to true in case of sp.spec.ServerTags is empty
match := true
for _, tag := range spec.ServerTags {
if match = stringtool.StrInSlice(tag, instance.Tags); match {
break
}
}
if match {
servers = append(servers, &Server{
URL: instance.URL(),
Tags: instance.Tags,
Weight: instance.Weight,
})
}
}
if len(servers) == 0 {
msgFmt := "%s/%s: no service instance satisfy tags: %v"
logger.Warnf(msgFmt, spec.ServiceRegistry, spec.ServiceName, spec.ServerTags)
servers = spec.Servers
}
spb.createLoadBalancer(spec.LoadBalance, servers)
}
// Done returns the done channel, which indicates the closing of the server pool.
func (spb *ServerPoolBase) Done() <-chan struct{} {
return spb.done
}
// Close closes the server pool.
func (spb *ServerPoolBase) Close() {
close(spb.done)
spb.wg.Wait()
if lb := spb.LoadBalancer(); lb != nil {
lb.Close()
}
}