Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [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 | */ |
| 16 | |
| 17 | package afrouterd |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | "github.com/golang/protobuf/ptypes" |
Scott Baker | f579f13 | 2019-10-24 14:31:41 -0700 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v2/pkg/kafka" |
| 23 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame^] | 24 | "github.com/opencord/voltha-lib-go/v2/pkg/probe" |
Scott Baker | b6de7a5 | 2019-11-04 09:13:37 -0800 | [diff] [blame] | 25 | pb "github.com/opencord/voltha-protos/v2/go/afrouter" |
| 26 | ic "github.com/opencord/voltha-protos/v2/go/inter_container" |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 27 | "golang.org/x/net/context" |
| 28 | "regexp" |
| 29 | "time" |
| 30 | ) |
| 31 | |
| 32 | func newKafkaClient(clientType string, host string, port int, instanceID string) (kafka.Client, error) { |
| 33 | log.Infow("kafka-client-type", log.Fields{"client": clientType}) |
| 34 | switch clientType { |
| 35 | case "sarama": |
| 36 | return kafka.NewSaramaClient( |
| 37 | kafka.Host(host), |
| 38 | kafka.Port(port), |
| 39 | kafka.ConsumerType(kafka.GroupCustomer), |
| 40 | kafka.ProducerReturnOnErrors(true), |
| 41 | kafka.ProducerReturnOnSuccess(true), |
| 42 | kafka.ProducerMaxRetries(6), |
| 43 | kafka.NumPartitions(3), |
| 44 | kafka.ConsumerGroupName(instanceID), |
| 45 | kafka.ConsumerGroupPrefix(instanceID), |
| 46 | kafka.AutoCreateTopic(false), |
| 47 | kafka.ProducerFlushFrequency(5), |
| 48 | kafka.ProducerRetryBackoff(time.Millisecond*30)), nil |
| 49 | } |
| 50 | return nil, errors.New("unsupported-client-type") |
| 51 | } |
| 52 | |
| 53 | func monitorDiscovery(kc kafka.Client, ctx context.Context, client pb.ConfigurationClient, ch <-chan *ic.InterContainerMessage, doneCh chan<- struct{}) { |
| 54 | defer close(doneCh) |
| 55 | defer kc.Stop() |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame^] | 56 | defer probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusStopped) |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 57 | |
| 58 | monitorLoop: |
| 59 | for { |
| 60 | select { |
| 61 | case <-ctx.Done(): |
| 62 | break monitorLoop |
| 63 | case msg := <-ch: |
| 64 | log.Debug("Received a device discovery notification") |
| 65 | device := &ic.DeviceDiscovered{} |
| 66 | if err := ptypes.UnmarshalAny(msg.Body, device); err != nil { |
| 67 | log.Errorf("Could not unmarshal received notification %v", msg) |
| 68 | } else { |
| 69 | // somewhat hackish solution, backend is known from the first digit found in the publisher name |
| 70 | group := regexp.MustCompile(`\d`).FindString(device.Publisher) |
| 71 | if group != "" { |
| 72 | // set the affinity of the discovered device |
| 73 | setAffinity(ctx, client, device.Id, afrouterRWClusterName+group) |
| 74 | } else { |
| 75 | log.Error("backend is unknown") |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func StartDiscoveryMonitor(ctx context.Context, client pb.ConfigurationClient) (<-chan struct{}, error) { |
| 83 | doneCh := make(chan struct{}) |
| 84 | // Connect to kafka for discovery events |
| 85 | kc, err := newKafkaClient(kafkaClientType, kafkaHost, kafkaPort, kafkaInstanceID) |
| 86 | if err != nil { |
| 87 | panic(err) |
| 88 | } |
| 89 | |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame^] | 90 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPreparing) |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 91 | for { |
| 92 | if err := kc.Start(); err != nil { |
| 93 | log.Error("Could not connect to kafka") |
| 94 | } else { |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame^] | 95 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning) |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 96 | break |
| 97 | } |
| 98 | select { |
| 99 | case <-ctx.Done(): |
| 100 | close(doneCh) |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame^] | 101 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusStopped) |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 102 | return doneCh, errors.New("GRPC context done") |
| 103 | |
| 104 | case <-time.After(5 * time.Second): |
| 105 | } |
| 106 | } |
| 107 | ch, err := kc.Subscribe(&kafka.Topic{Name: kafkaTopic}) |
| 108 | if err != nil { |
| 109 | log.Errorf("Could not subscribe to the '%s' channel, discovery disabled", kafkaTopic) |
| 110 | close(doneCh) |
| 111 | kc.Stop() |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame^] | 112 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusStopped) |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 113 | return doneCh, err |
| 114 | } |
| 115 | |
| 116 | go monitorDiscovery(kc, ctx, client, ch, doneCh) |
| 117 | return doneCh, nil |
| 118 | } |