sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 16 | |
| 17 | package afrouter |
| 18 | |
| 19 | // Backend manager handles redundant connections per backend |
| 20 | |
| 21 | import ( |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 22 | "errors" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 23 | "fmt" |
| 24 | "github.com/opencord/voltha-go/common/log" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 25 | "golang.org/x/net/context" |
| 26 | "google.golang.org/grpc" |
| 27 | "google.golang.org/grpc/codes" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 28 | "google.golang.org/grpc/connectivity" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 29 | "google.golang.org/grpc/metadata" |
| 30 | "io" |
| 31 | "net" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 32 | "strconv" |
| 33 | "strings" |
| 34 | "sync" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 35 | ) |
| 36 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 37 | // backend represents a collection of backends in a HA configuration |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 38 | type backend struct { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 39 | mutex sync.Mutex |
| 40 | name string |
| 41 | beType backendType |
| 42 | activeAssociation association |
| 43 | connFailCallback func(string, *backend) bool |
| 44 | connections map[string]*connection |
| 45 | openConns int |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 48 | type association struct { |
| 49 | strategy associationStrategy |
| 50 | location associationLocation |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 51 | field string // Used only if location is protobuf |
| 52 | key string |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 55 | func (be *backend) openSouthboundStreams(srv interface{}, serverStream grpc.ServerStream, f *nbFrame) (*streams, error) { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 56 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 57 | rtrn := &streams{streams: make(map[string]*stream), activeStream: nil} |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 58 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 59 | log.Debugf("Opening southbound streams for method '%s'", f.methodInfo.method) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 60 | // Get the metadata from the incoming message on the server |
| 61 | md, ok := metadata.FromIncomingContext(serverStream.Context()) |
| 62 | if !ok { |
| 63 | return nil, errors.New("Could not get a server stream metadata") |
| 64 | } |
| 65 | |
| 66 | // TODO: Need to check if this is an active/active backend cluster |
| 67 | // with a serial number in the header. |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 68 | log.Debugf("Serial number for transaction allocated: %d", f.serialNo) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 69 | // If even one stream can be created then proceed. If none can be |
| 70 | // created then report an error becase both the primary and redundant |
| 71 | // connections are non-existant. |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 72 | var atLeastOne = false |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 73 | var errStr strings.Builder |
| 74 | log.Debugf("There are %d connections to open", len(be.connections)) |
Kent Hagerman | 1b9c706 | 2019-05-07 16:46:01 -0400 | [diff] [blame] | 75 | for _, cn := range be.connections { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 76 | // Copy in the metadata |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 77 | if cn.getState() == connectivity.Ready && cn.getConn() != nil { |
sslobodr | 63d160c | 2019-02-08 14:25:13 -0500 | [diff] [blame] | 78 | log.Debugf("Opening southbound stream for connection '%s'", cn.name) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 79 | // Create an outgoing context that includes the incoming metadata |
| 80 | // and that will cancel if the server's context is canceled |
| 81 | clientCtx, clientCancel := context.WithCancel(serverStream.Context()) |
| 82 | clientCtx = metadata.NewOutgoingContext(clientCtx, md.Copy()) |
| 83 | //TODO: Same check here, only add the serial number if necessary |
| 84 | clientCtx = metadata.AppendToOutgoingContext(clientCtx, "voltha_serial_number", |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 85 | strconv.FormatUint(f.serialNo, 10)) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 86 | // Create the client stream |
| 87 | if clientStream, err := grpc.NewClientStream(clientCtx, clientStreamDescForProxying, |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 88 | cn.getConn(), f.methodInfo.all); err != nil { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 89 | log.Debugf("Failed to create a client stream '%s', %v", cn.name, err) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 90 | fmt.Fprintf(&errStr, "{{Failed to create a client stream '%s', %v}} ", cn.name, err) |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 91 | rtrn.streams[cn.name] = nil |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 92 | } else { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 93 | rtrn.streams[cn.name] = &stream{stream: clientStream, ctx: clientCtx, |
| 94 | cancel: clientCancel, s2cReturn: nil, |
| 95 | ok2Close: make(chan struct{}), |
| 96 | c2sReturn: make(chan error, 1)} |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 97 | atLeastOne = true |
| 98 | } |
| 99 | } else if cn.getConn() == nil { |
| 100 | err := errors.New(fmt.Sprintf("Connection '%s' is closed", cn.name)) |
| 101 | fmt.Fprint(&errStr, err.Error()) |
| 102 | log.Debug(err) |
| 103 | } else { |
| 104 | err := errors.New(fmt.Sprintf("Connection '%s' isn't ready", cn.name)) |
| 105 | fmt.Fprint(&errStr, err.Error()) |
| 106 | log.Debug(err) |
| 107 | } |
| 108 | } |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 109 | if atLeastOne { |
sslobodr | 63d160c | 2019-02-08 14:25:13 -0500 | [diff] [blame] | 110 | rtrn.sortStreams() |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 111 | return rtrn, nil |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 112 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 113 | fmt.Fprintf(&errStr, "{{No streams available for backend '%s' unable to send}} ", be.name) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 114 | log.Error(errStr.String()) |
| 115 | return nil, errors.New(errStr.String()) |
| 116 | } |
| 117 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 118 | func (be *backend) handler(srv interface{}, serverStream grpc.ServerStream, nf *nbFrame, sf *sbFrame) error { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 119 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 120 | // Set up and launch each individual southbound stream |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 121 | var beStrms *streams |
sslobodr | 63d160c | 2019-02-08 14:25:13 -0500 | [diff] [blame] | 122 | var rtrn error = nil |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 123 | var s2cOk = false |
| 124 | var c2sOk = false |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 125 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 126 | beStrms, err := be.openSouthboundStreams(srv, serverStream, nf) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 127 | if err != nil { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 128 | log.Errorf("openStreams failed: %v", err) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 129 | return err |
| 130 | } |
| 131 | // If we get here, there has to be AT LEAST ONE open stream |
| 132 | |
| 133 | // *Do not explicitly close* s2cErrChan and c2sErrChan, otherwise the select below will not terminate. |
| 134 | // Channels do not have to be closed, it is just a control flow mechanism, see |
| 135 | // https://groups.google.com/forum/#!msg/golang-nuts/pZwdYRGxCIk/qpbHxRRPJdUJ |
| 136 | |
| 137 | log.Debug("Starting server to client forwarding") |
| 138 | s2cErrChan := beStrms.forwardServerToClient(serverStream, nf) |
| 139 | |
| 140 | log.Debug("Starting client to server forwarding") |
| 141 | c2sErrChan := beStrms.forwardClientToServer(serverStream, sf) |
| 142 | |
| 143 | // We don't know which side is going to stop sending first, so we need a select between the two. |
| 144 | for i := 0; i < 2; i++ { |
| 145 | select { |
| 146 | case s2cErr := <-s2cErrChan: |
sslobodr | 63d160c | 2019-02-08 14:25:13 -0500 | [diff] [blame] | 147 | s2cOk = true |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 148 | log.Debug("Processing s2cErr") |
| 149 | if s2cErr == io.EOF { |
| 150 | log.Debug("s2cErr reporting EOF") |
| 151 | // this is the successful case where the sender has encountered io.EOF, and won't be sending anymore./ |
| 152 | // the clientStream>serverStream may continue sending though. |
sslobodr | 63d160c | 2019-02-08 14:25:13 -0500 | [diff] [blame] | 153 | defer beStrms.closeSend() |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 154 | if c2sOk { |
sslobodr | 63d160c | 2019-02-08 14:25:13 -0500 | [diff] [blame] | 155 | return rtrn |
| 156 | } |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 157 | } else { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 158 | log.Debugf("s2cErr reporting %v", s2cErr) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 159 | // however, we may have gotten a receive error (stream disconnected, a read error etc) in which case we need |
| 160 | // to cancel the clientStream to the backend, let all of its goroutines be freed up by the CancelFunc and |
| 161 | // exit with an error to the stack |
| 162 | beStrms.clientCancel() |
| 163 | return grpc.Errorf(codes.Internal, "failed proxying s2c: %v", s2cErr) |
| 164 | } |
| 165 | case c2sErr := <-c2sErrChan: |
sslobodr | 63d160c | 2019-02-08 14:25:13 -0500 | [diff] [blame] | 166 | c2sOk = true |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 167 | log.Debug("Processing c2sErr") |
| 168 | // This happens when the clientStream has nothing else to offer (io.EOF), returned a gRPC error. In those two |
| 169 | // cases we may have received Trailers as part of the call. In case of other errors (stream closed) the trailers |
| 170 | // will be nil. |
| 171 | serverStream.SetTrailer(beStrms.trailer()) |
| 172 | // c2sErr will contain RPC error from client code. If not io.EOF return the RPC error as server stream error. |
sslobodr | 63d160c | 2019-02-08 14:25:13 -0500 | [diff] [blame] | 173 | // NOTE!!! with redundant backends, it's likely that one of the backends |
| 174 | // returns a response before all the data has been sent southbound and |
| 175 | // the southbound streams are closed. Should this happen one of the |
| 176 | // backends may not get the request. |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 177 | if c2sErr != io.EOF { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 178 | rtrn = c2sErr |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 179 | } |
sslobodr | 63d160c | 2019-02-08 14:25:13 -0500 | [diff] [blame] | 180 | log.Debug("c2sErr reporting EOF") |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 181 | if s2cOk { |
sslobodr | 63d160c | 2019-02-08 14:25:13 -0500 | [diff] [blame] | 182 | return rtrn |
| 183 | } |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | return grpc.Errorf(codes.Internal, "gRPC proxying should never reach this stage.") |
| 187 | } |
| 188 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 189 | func newBackend(conf *BackendConfig, clusterName string) (*backend, error) { |
| 190 | var rtrn_err bool = false |
| 191 | |
| 192 | log.Debugf("Configuring the backend with %v", *conf) |
| 193 | // Validate the conifg and configure the backend |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 194 | be := &backend{name: conf.Name, connections: make(map[string]*connection), openConns: 0} |
| 195 | if conf.Type == BackendUndefined { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 196 | log.Error("Invalid type specified for backend %s in cluster %s", conf.Name, clusterName) |
| 197 | rtrn_err = true |
| 198 | } |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 199 | be.beType = conf.Type |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 200 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 201 | if conf.Association.Strategy == AssociationStrategyUndefined && be.beType == BackendActiveActive { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 202 | log.Errorf("An association strategy must be provided if the backend "+ |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 203 | "type is active/active for backend %s in cluster %s", conf.Name, clusterName) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 204 | rtrn_err = true |
| 205 | } |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 206 | be.activeAssociation.strategy = conf.Association.Strategy |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 207 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 208 | if conf.Association.Location == AssociationLocationUndefined && be.beType == BackendActiveActive { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 209 | log.Errorf("An association location must be provided if the backend "+ |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 210 | "type is active/active for backend %s in cluster %s", conf.Name, clusterName) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 211 | rtrn_err = true |
| 212 | } |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 213 | be.activeAssociation.location = conf.Association.Location |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 214 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 215 | if conf.Association.Field == "" && be.activeAssociation.location == AssociationLocationProtobuf { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 216 | log.Errorf("An association field must be provided if the backend "+ |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 217 | "type is active/active and the location is set to protobuf "+ |
| 218 | "for backend %s in cluster %s", conf.Name, clusterName) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 219 | rtrn_err = true |
| 220 | } |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 221 | be.activeAssociation.field = conf.Association.Field |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 222 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 223 | if conf.Association.Key == "" && be.activeAssociation.location == AssociationLocationHeader { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 224 | log.Errorf("An association key must be provided if the backend "+ |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 225 | "type is active/active and the location is set to header "+ |
| 226 | "for backend %s in cluster %s", conf.Name, clusterName) |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 227 | rtrn_err = true |
| 228 | } |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 229 | be.activeAssociation.key = conf.Association.Key |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 230 | if rtrn_err { |
| 231 | return nil, errors.New("Backend configuration failed") |
| 232 | } |
| 233 | // Configure the connections |
| 234 | // Connections can consist of just a name. This allows for dynamic configuration |
| 235 | // at a later time. |
| 236 | // TODO: validate that there is one connection for all but active/active backends |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 237 | if len(conf.Connections) > 1 && be.beType != BackendActiveActive { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 238 | log.Errorf("Only one connection must be specified if the association " + |
| 239 | "strategy is not set to 'active_active'") |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 240 | rtrn_err = true |
| 241 | } |
| 242 | if len(conf.Connections) == 0 { |
| 243 | log.Errorf("At least one connection must be specified") |
| 244 | rtrn_err = true |
| 245 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 246 | for _, cnConf := range conf.Connections { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 247 | if cnConf.Name == "" { |
| 248 | log.Errorf("A connection must have a name for backend %s in cluster %s", |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 249 | conf.Name, clusterName) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 250 | } else { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 251 | gc := &gConnection{conn: nil, cancel: nil, state: connectivity.Idle} |
| 252 | be.connections[cnConf.Name] = &connection{name: cnConf.Name, addr: cnConf.Addr, port: cnConf.Port, backend: be, gConn: gc} |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 253 | if cnConf.Addr != "" { // This connection will be specified later. |
| 254 | if ip := net.ParseIP(cnConf.Addr); ip == nil { |
| 255 | log.Errorf("The IP address for connection %s in backend %s in cluster %s is invalid", |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 256 | cnConf.Name, conf.Name, clusterName) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 257 | rtrn_err = true |
| 258 | } |
| 259 | // Validate the port number. This just validtes that it's a non 0 integer |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 260 | if n, err := strconv.Atoi(cnConf.Port); err != nil || n <= 0 || n > 65535 { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 261 | log.Errorf("Port %s for connection %s in backend %s in cluster %s is invalid", |
| 262 | cnConf.Port, cnConf.Name, conf.Name, clusterName) |
| 263 | rtrn_err = true |
| 264 | } else { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 265 | if n <= 0 && n > 65535 { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 266 | log.Errorf("Port %s for connection %s in backend %s in cluster %s is invalid", |
| 267 | cnConf.Port, cnConf.Name, conf.Name, clusterName) |
| 268 | rtrn_err = true |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
sslobodr | 63d160c | 2019-02-08 14:25:13 -0500 | [diff] [blame] | 274 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 275 | if rtrn_err { |
| 276 | return nil, errors.New("Connection configuration failed") |
| 277 | } |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 278 | // All is well start the backend cluster connections |
| 279 | be.connectAll() |
| 280 | |
| 281 | return be, nil |
| 282 | } |
| 283 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 284 | func (be *backend) incConn() { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 285 | be.mutex.Lock() |
| 286 | defer be.mutex.Unlock() |
| 287 | be.openConns++ |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | func (be *backend) decConn() { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 291 | be.mutex.Lock() |
| 292 | defer be.mutex.Unlock() |
| 293 | be.openConns-- |
| 294 | if be.openConns < 0 { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 295 | log.Error("Internal error, number of open connections less than 0") |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 296 | be.openConns = 0 |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | // Attempts to establish all the connections for a backend |
| 301 | // any failures result in an abort. This should only be called |
| 302 | // on a first attempt to connect. Individual connections should be |
| 303 | // handled after that. |
| 304 | func (be *backend) connectAll() { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 305 | for _, cn := range be.connections { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 306 | cn.connect() |
| 307 | } |
| 308 | } |
| 309 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 310 | // Set a callback for connection failure notification |
| 311 | // This is currently not used. |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame^] | 312 | func (be *backend) setConnFailCallback(cb func(string, *backend) bool) { |
| 313 | be.connFailCallback = cb |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 314 | } |