func (r *RPCRegistry) Connect() { // We want the connection operation to block and constantly reconnect using grpc backoff log.Info("Starting gRPC connection to fleet-engine...") ep_engines := []string{":fleet-engine:"} r.balancer = newSimpleBalancer(ep_engines) connection, err := grpc.Dial(ep_engines[0], grpc.WithTimeout(12*time.Second), grpc.WithInsecure(), grpc.WithDialer(r.dialer), grpc.WithBlock(), grpc.WithBalancer(r.balancer)) if err != nil { log.Fatalf("Unable to dial to registry: %s", err) } r.registryConn = connection r.registryClient = pb.NewRegistryClient(r.registryConn) log.Info("Connected succesfully to fleet-engine via grpc!") }
func (s *Server) Run() { log.Infof("Establishing etcd connectivity") var err error for sleep := time.Second; ; sleep = pkg.ExpBackoff(sleep, time.Minute) { _, err = s.hrt.Beat(s.mon.TTL) if err == nil { break } time.Sleep(sleep) } log.Infof("Starting server components") s.stop = make(chan bool) go s.Monitor() go s.api.Available(s.stop) go s.mach.PeriodicRefresh(machineStateRefreshInterval, s.stop) go s.agent.Heartbeat(s.stop) go s.aReconciler.Run(s.agent, s.stop) if s.disableEngine { log.Info("Not starting engine; disable-engine is set") } else { go s.engine.Run(s.engineReconcileInterval, s.stop) } beatchan := make(chan *unit.UnitStateHeartbeat) go s.usGen.Run(beatchan, s.stop) go s.usPub.Run(beatchan, s.stop) }
// IsGrpcLeader checks if the current leader has gRPC capabilities enabled or error // if there is not a elected leader yet. func (e *Engine) IsGrpcLeader() (bool, error) { leader, err := e.lManager.GetLease(engineLeaseName) if err != nil { log.Errorf("Unable to determine current lease: %v", err) return false, err } // It can happen that the leader is not yet stored in etcd and nor error (line 122 pkg/lease/etcd.go) if leader == nil { return false, errors.New("Unable to get the current leader") } leaderState, err := e.getMachineState(leader.MachineID()) if err != nil { log.Errorf("Unable to determine current lease: %v", err) return false, err } if leaderState.Capabilities != nil && leaderState.Capabilities.Has(machine.CapGRPC) { return true, nil } log.Info("Engine leader has no gRPC capabilities enabled!") return false, nil }
func (s *Server) Run() { log.Infof("Establishing etcd connectivity") var err error for sleep := time.Second; ; sleep = pkg.ExpBackoff(sleep, time.Minute) { if s.restartServer { _, err = s.hrt.Beat(s.mon.TTL) if err == nil { log.Infof("hrt.Beat() success") break } } else { _, err = s.hrt.Register(s.mon.TTL) if err == nil { log.Infof("hrt.Register() success") break } } log.Warningf("Server register machine failed: %v, retrying in %d sec.", err, sleep) time.Sleep(sleep) } go s.Supervise() log.Infof("Starting server components") s.stopc = make(chan struct{}) s.wg = sync.WaitGroup{} beatc := make(chan *unit.UnitStateHeartbeat) components := []func(){ func() { s.api.Available(s.stopc) }, func() { s.mach.PeriodicRefresh(machineStateRefreshInterval, s.stopc) }, func() { s.agent.Heartbeat(s.stopc) }, func() { s.aReconciler.Run(s.agent, s.stopc) }, func() { s.usGen.Run(beatc, s.stopc) }, func() { s.usPub.Run(beatc, s.stopc) }, } if s.disableEngine { log.Info("Not starting engine; disable-engine is set") } else { components = append(components, func() { s.engine.Run(s.engineReconcileInterval, s.stopc) }) } for _, f := range components { f := f s.wg.Add(1) go func() { f() s.wg.Done() }() } }
func NewRPCServer(reg registry.Registry, addr string) (*rpcserver, error) { s := &rpcserver{ etcdRegistry: reg, mu: new(sync.Mutex), localRegistry: newInmemoryRegistry(), stop: make(chan struct{}), } var err error tcpAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", addr, rpcServerPort)) if err != nil { return nil, err } for it := 0; it < bindAddrMaxRetry; it++ { s.listener, err = net.ListenTCP("tcp", tcpAddr) if err == nil { break } log.Infof("Retrying %d to bind %s address... %v", it, tcpAddr, err) time.Sleep(bindRetryTimeout) } if err != nil { return nil, err } s.grpcserver = grpc.NewServer() s.localRegistry.LoadFrom(s.etcdRegistry) pb.RegisterRegistryServer(s.grpcserver, s) s.SetServingStatus(pb.HealthCheckResponse_NOT_SERVING) machineStates, err := s.etcdRegistry.Machines() if err != nil { return nil, err } s.hasNonGRPCAgents = false for _, state := range machineStates { if !state.Capabilities.Has(machine.CapGRPC) { log.Info("Fleet cluster has non gRPC agents!. Enabled unit state storage into etcd!") s.hasNonGRPCAgents = true break } } return s, nil }