Пример #1
0
func (s *rpcserver) GetUnitStates(ctx context.Context, filter *pb.UnitStateFilter) (*pb.UnitStates, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_())
	}
	states := make([]*pb.UnitState, 0)
	states = append(states, s.localRegistry.UnitStates()...)

	if s.hasNonGRPCAgents {
		log.Debug("Merging etcd with inmemory unit states in GetUnitStates()")
		etcdUnitStates, err := s.etcdRegistry.UnitStates()
		if err != nil {
			return nil, err
		}

		unitStateNames := make(map[string]string, len(states))
		for _, state := range states {
			unitStateNames[state.Name] = state.MachineID
		}
		for _, state := range etcdUnitStates {
			machId, ok := unitStateNames[state.UnitName]
			if !ok || (ok && machId != state.MachineID) {
				states = append(states, state.ToPB())
			}
		}
	}

	return &pb.UnitStates{states}, nil
}
Пример #2
0
func (s *rpcserver) GetUnits(ctx context.Context, filter *pb.UnitFilter) (*pb.Units, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_())
	}
	units := make([]pb.Unit, 0)
	units = append(units, s.localRegistry.Units()...)

	// Check if there are etcd fleet-based agents in the cluster to share the state
	if s.hasNonGRPCAgents {
		log.Debug("Merging etcd with inmemory units in GetUnits()")
		etcdUnits, err := s.etcdRegistry.Units()
		if err != nil {
			return nil, err
		}

		unitNames := make(map[string]struct{}, len(units))
		for _, unit := range units {
			unitNames[unit.Name] = struct{}{}
		}
		for _, unit := range etcdUnits {
			if _, ok := unitNames[unit.Name]; !ok {
				units = append(units, unit.ToPB())
			}
		}
	}

	return &pb.Units{Units: units}, nil
}
Пример #3
0
func (r *inmemoryRegistry) DestroyUnit(name string) bool {
	if DebugInmemoryRegistry {
		defer debug.Exit_(debug.Enter_(name))
	}
	r.mu.Lock()
	defer r.mu.Unlock()
	r.unitStatesMu.Lock()
	defer r.unitStatesMu.Unlock()
	r.heartbeatsMu.Lock()
	defer r.heartbeatsMu.Unlock()

	deleted := false

	if _, exists := r.unitsCache[name]; exists {
		delete(r.unitsCache, name)
		deleted = true
	}

	if _, exists := r.scheduledUnits[name]; exists {
		delete(r.scheduledUnits, name)
	}

	if _, exists := r.unitHeartbeats[name]; exists {
		delete(r.unitHeartbeats, name)
	}

	if _, exists := r.unitStates[name]; exists {
		delete(r.unitStates, name)
	}

	return deleted
}
Пример #4
0
func (r *RPCRegistry) ClearUnitHeartbeat(unitName string) {
	if DebugRPCRegistry {
		defer debug.Exit_(debug.Enter_(unitName))
	}

	r.getClient().ClearUnitHeartbeat(r.ctx(), &pb.UnitName{unitName})
}
Пример #5
0
func (s *rpcserver) ClearUnitHeartbeat(ctx context.Context, name *pb.UnitName) (*pb.GenericReply, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_(name.Name))
	}

	s.localRegistry.ClearUnitHeartbeat(name.Name)
	return &pb.GenericReply{}, nil
}
Пример #6
0
func (s *rpcserver) UnitHeartbeat(ctx context.Context, heartbeat *pb.Heartbeat) (*pb.GenericReply, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_(heartbeat))
	}

	s.localRegistry.UnitHeartbeat(heartbeat.Name, heartbeat.MachineID, time.Duration(heartbeat.TTL)*time.Second)
	return &pb.GenericReply{}, nil
}
Пример #7
0
func (r *RPCRegistry) DestroyUnit(unitName string) error {
	if DebugRPCRegistry {
		defer debug.Exit_(debug.Enter_(unitName))
	}

	_, err := r.getClient().DestroyUnit(r.ctx(), &pb.UnitName{unitName})
	return err
}
Пример #8
0
func (r *inmemoryRegistry) UnitState(name string) *pb.UnitState {
	if DebugInmemoryRegistry {
		defer debug.Exit_(debug.Enter_())
	}
	r.unitStatesMu.Lock()
	defer r.unitStatesMu.Unlock()

	return r.stateByMUSKey(name)
}
Пример #9
0
func (r *inmemoryRegistry) CreateUnit(u *pb.Unit) {
	if DebugInmemoryRegistry {
		defer debug.Exit_(debug.Enter_(u))
	}
	r.mu.Lock()
	defer r.mu.Unlock()

	r.unitsCache[u.Name] = *u
}
Пример #10
0
func (s *rpcserver) GetScheduledUnits(ctx context.Context, unitFilter *pb.UnitFilter) (*pb.ScheduledUnits, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_())
	}

	units, err := s.localRegistry.Schedule()

	return &pb.ScheduledUnits{Units: units}, err
}
Пример #11
0
func (r *inmemoryRegistry) UnscheduleUnit(unitName, machineid string) {
	if DebugInmemoryRegistry {
		defer debug.Exit_(debug.Enter_(unitName, machineid))
	}
	r.mu.Lock()
	defer r.mu.Unlock()

	delete(r.scheduledUnits, unitName)
}
Пример #12
0
func (s *rpcserver) GetScheduledUnit(ctx context.Context, name *pb.UnitName) (*pb.MaybeScheduledUnit, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_(name.Name))
	}

	su := s.localRegistry.ScheduledUnit(name.Name)

	return &pb.MaybeScheduledUnit{IsScheduled: &pb.MaybeScheduledUnit_Unit{Unit: su}}, nil
}
Пример #13
0
func (s *rpcserver) GetUnitState(ctx context.Context, name *pb.UnitName) (*pb.UnitState, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_(name.Name))
	}

	state := s.localRegistry.UnitState(name.Name)

	return state, nil
}
Пример #14
0
func (r *RPCRegistry) CreateUnit(j *job.Unit) error {
	if DebugRPCRegistry {
		defer debug.Exit_(debug.Enter_(j.Name))
	}

	un := j.ToPB()
	_, err := r.getClient().CreateUnit(r.ctx(), &un)
	return err
}
Пример #15
0
func (r *inmemoryRegistry) Unit(name string) (pb.Unit, bool) {
	if DebugInmemoryRegistry {
		defer debug.Exit_(debug.Enter_(name))
	}
	r.mu.RLock()
	defer r.mu.RUnlock()

	u, exists := r.unitsCache[name]
	return u, exists
}
Пример #16
0
func (s *rpcserver) UnscheduleUnit(ctx context.Context, unit *pb.UnscheduleUnitRequest) (*pb.GenericReply, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_(unit.Name, unit.MachineID))
	}

	err := s.etcdRegistry.UnscheduleUnit(unit.Name, unit.MachineID)
	if err == nil {
		s.localRegistry.UnscheduleUnit(unit.Name, unit.MachineID)
	}
	return &pb.GenericReply{}, err
}
Пример #17
0
func (r *inmemoryRegistry) ClearUnitHeartbeat(name string) {
	if DebugInmemoryRegistry {
		defer debug.Exit_(debug.Enter_(name))
	}
	r.heartbeatsMu.Lock()
	defer r.heartbeatsMu.Unlock()

	if _, exists := r.unitHeartbeats[name]; exists {
		delete(r.unitHeartbeats, name)
	}
}
Пример #18
0
func (s *rpcserver) CreateUnit(ctx context.Context, u *pb.Unit) (*pb.GenericReply, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_(u.Name))
	}

	err := s.etcdRegistry.CreateUnit(rpcUnitToJobUnit(u))
	if err == nil {
		s.localRegistry.CreateUnit(u)
	}
	return &pb.GenericReply{}, err
}
Пример #19
0
func (s *rpcserver) DestroyUnit(ctx context.Context, name *pb.UnitName) (*pb.GenericReply, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_(name.Name))
	}

	err := s.etcdRegistry.DestroyUnit(name.Name)
	if err == nil {
		s.localRegistry.DestroyUnit(name.Name)
	}
	return &pb.GenericReply{}, err
}
Пример #20
0
func (r *RPCRegistry) UnscheduleUnit(unitName, machID string) error {
	if DebugRPCRegistry {
		defer debug.Exit_(debug.Enter_(unitName, machID))
	}

	_, err := r.getClient().UnscheduleUnit(r.ctx(), &pb.UnscheduleUnitRequest{
		Name:      unitName,
		MachineID: machID,
	})
	return err
}
Пример #21
0
func (r *RPCRegistry) SetUnitTargetState(unitName string, state job.JobState) error {
	if DebugRPCRegistry {
		defer debug.Exit_(debug.Enter_(unitName, state))
	}

	_, err := r.getClient().SetUnitTargetState(r.ctx(), &pb.ScheduledUnit{
		Name:         unitName,
		CurrentState: state.ToPB(),
	})
	return err
}
Пример #22
0
func (r *inmemoryRegistry) RemoveUnitState(unitName string) {
	if DebugInmemoryRegistry {
		defer debug.Exit_(debug.Enter_(unitName))
	}
	r.unitStatesMu.Lock()
	defer r.unitStatesMu.Unlock()

	if _, exists := r.unitStates[unitName]; exists {
		delete(r.unitStates, unitName)
	}
}
Пример #23
0
func (s *rpcserver) SetUnitTargetState(ctx context.Context, unit *pb.ScheduledUnit) (*pb.GenericReply, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_(unit.Name, unit.CurrentState))
	}

	err := s.etcdRegistry.SetUnitTargetState(unit.Name, rpcUnitStateToJobState(unit.CurrentState))
	if err == nil {
		if s.localRegistry.SetUnitTargetState(unit.Name, unit.CurrentState) {
		}
	}
	return &pb.GenericReply{}, err
}
Пример #24
0
func (r *RPCRegistry) UnitHeartbeat(unitName, machID string, ttl time.Duration) error {
	if DebugRPCRegistry {
		defer debug.Exit_(debug.Enter_(unitName, machID))
	}

	_, err := r.getClient().UnitHeartbeat(r.ctx(), &pb.Heartbeat{
		Name:      unitName,
		MachineID: machID,
		TTL:       int32(ttl.Seconds()),
	})
	return err
}
Пример #25
0
func (s *rpcserver) GetUnit(ctx context.Context, name *pb.UnitName) (*pb.MaybeUnit, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_(name.Name))
	}

	unit, exists := s.localRegistry.Unit(name.Name)
	if exists {
		return &pb.MaybeUnit{HasUnit: &pb.MaybeUnit_Unit{Unit: &unit}}, nil
	}
	return &pb.MaybeUnit{HasUnit: &pb.MaybeUnit_Notfound{Notfound: &pb.NotFound{}}}, nil

}
Пример #26
0
func (s *rpcserver) RemoveUnitState(ctx context.Context, name *pb.UnitName) (*pb.GenericReply, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_(name.Name))
	}

	// Check if there are etcd fleet-based agents in the cluster to share the state
	if s.hasNonGRPCAgents {
		s.etcdRegistry.RemoveUnitState(name.Name)
	}

	s.localRegistry.RemoveUnitState(name.Name)
	return &pb.GenericReply{}, nil
}
Пример #27
0
func (r *inmemoryRegistry) UnitHeartbeat(unitName, machineid string, ttl time.Duration) {
	if DebugInmemoryRegistry {
		defer debug.Exit_(debug.Enter_(unitName, machineid, ttl))
	}
	r.heartbeatsMu.Lock()
	defer r.heartbeatsMu.Unlock()

	if _, exists := r.unitHeartbeats[unitName]; exists {
		r.unitHeartbeats[unitName][machineid] = time.Now().Add(ttl)
	} else {
		r.unitHeartbeats[unitName] = map[string]time.Time{machineid: time.Now().Add(ttl)}
	}
}
Пример #28
0
func (r *inmemoryRegistry) ScheduleUnit(unitName, machineid string) {
	if DebugInmemoryRegistry {
		defer debug.Exit_(debug.Enter_(unitName, machineid))
	}
	r.mu.Lock()
	defer r.mu.Unlock()

	r.scheduledUnits[unitName] = pb.ScheduledUnit{
		Name:         unitName,
		CurrentState: pb.TargetState_INACTIVE,
		MachineID:    machineid,
	}
}
Пример #29
0
func (s *rpcserver) SaveUnitState(ctx context.Context, req *pb.SaveUnitStateRequest) (*pb.GenericReply, error) {
	if debugRPCServer {
		defer debug.Exit_(debug.Enter_(req))
	}

	// Check if there are etcd fleet-based agents in the cluster to share the state
	if s.hasNonGRPCAgents {
		unitState := rpcUnitStateToExtUnitState(req.State)
		s.etcdRegistry.SaveUnitState(req.Name, unitState, time.Duration(req.TTL)*time.Second)
	}

	s.localRegistry.SaveUnitState(req.Name, req.State, time.Duration(req.TTL)*time.Second)
	return &pb.GenericReply{}, nil
}
Пример #30
0
func (r *inmemoryRegistry) SetUnitTargetState(unitName string, targetState pb.TargetState) bool {
	if DebugInmemoryRegistry {
		defer debug.Exit_(debug.Enter_(unitName, targetState))
	}
	r.mu.Lock()
	defer r.mu.Unlock()

	if u, exists := r.unitsCache[unitName]; exists {
		u.DesiredState = targetState
		r.unitsCache[unitName] = u
		return true
	}
	return false
}