<commit_before>package goarken

import (
	"errors"
	"github.com/golang/glog"
	"sync"
)

type ServiceCluster struct {
	Name      string
	instances []*Service
	lastIndex int
	lock      sync.RWMutex
}

func (cl *ServiceCluster) Next() (*Service, error) {
	if cl == nil {
		return nil, StatusError{}
	}
	cl.lock.RLock()
	defer cl.lock.RUnlock()
	if len(cl.instances) == 0 {
		return nil, errors.New("no alive instance found")
	}
	var instance *Service
	for tries := 0; tries < len(cl.instances); tries++ {
		index := (cl.lastIndex + 1) % len(cl.instances)
		cl.lastIndex = index

		instance = cl.instances[index]
		glog.V(5).Infof("Checking instance %d Status : %s", index, instance.Status.Compute())
		if instance.Status.Compute() == STARTED_STATUS && instance.Location.IsFullyDefined() {
			return instance, nil
		}

	}

	lastStatus := instance.Status

	if lastStatus == nil && !instance.Location.IsFullyDefined() {
		glog.Infof("No Status and no location for %s", instance.Name)
		return nil, StatusError{ERROR_STATUS, lastStatus}
	}

	glog.V(5).Infof("No instance started for %s", instance.Name)
	glog.V(5).Infof("Last Status :")
	glog.V(5).Infof("   current  : %s", lastStatus.Current)
	glog.V(5).Infof("   expected : %s", lastStatus.Expected)
	glog.V(5).Infof("   alive : %s", lastStatus.Alive)<commit_msg>NXIO-345 prevent npe when na status<commit_after>package goarken

import (
	"errors"
	"github.com/golang/glog"
	"sync"
)

type ServiceCluster struct {
	Name      string
	instances []*Service
	lastIndex int
	lock      sync.RWMutex
}

func (cl *ServiceCluster) Next() (*Service, error) {
	if cl == nil {
		return nil, StatusError{}
	}
	cl.lock.RLock()
	defer cl.lock.RUnlock()
	if len(cl.instances) == 0 {
		return nil, errors.New("no alive instance found")
	}
	var instance *Service
	for tries := 0; tries < len(cl.instances); tries++ {
		index := (cl.lastIndex + 1) % len(cl.instances)
		cl.lastIndex = index

		instance = cl.instances[index]
		glog.V(5).Infof("Checking instance %d Status : %s", index, instance.Status.Compute())
		if instance.Status.Compute() == STARTED_STATUS && instance.Location.IsFullyDefined() {
			return instance, nil
		}

	}

	lastStatus := instance.Status

	if lastStatus == nil && !instance.Location.IsFullyDefined() {
		glog.Infof("No Status and no location for %s", instance.Name)
		return nil, StatusError{ERROR_STATUS, lastStatus}
	}

	glog.V(5).Infof("No instance started for %s", instance.Name)
	if lastStatus != nil {
		glog.V(5).Infof("Last Status :")
		glog.V(5).Infof("   current  : %s", lastStatus.Current)
		glog.V(5).Infof("   expected : %s", lastStatus.Expected)
		glog.V(5).Infof("   alive : %s", lastStatus.Alive)
	} else {
		glog.V(5).Infof("No status available")
	}

{'fraction_non_alphanumeric': 0.15325248070562295, 'fraction_numerical': 0.00808526277104006, 'mean_word_length': 3.994495412844037, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1728567', 'n_tokens_mistral': 1031, 'n_tokens_neox': 986, 'n_words': 231}