<commit_before>package agent

import (
	"fmt"
	"github.com/hashicorp/serf/serf"
	"io"
	"log"
	"net"
	"net/rpc"
	"os"
	"sync"
)

// Agent actually starts and manages a Serf agent.
type Agent struct {
	EventHandler EventHandler
	LogOutput    io.Writer
	RPCAddr      string
	SerfConfig   *serf.Config

	events      []string
	eventChs    map[chan<- string]struct{}
	eventIndex  int
	eventLock   sync.Mutex
	logger      *log.Logger
	once        sync.Once
	rpcListener net.Listener
	serf        *serf.Serf
	shutdownCh  chan<- struct{}
	state       AgentState
	lock        sync.Mutex
}

type AgentState int

const (
	AgentIdle AgentState = iota
	AgentRunning
)

// Join asks the Serf instance to join. See the Serf.Join function.
func (a *Agent) Join(addrs []string) (n int, err error) {
	a.once.Do(a.init)

	a.logger.Printf("[INFO] Agent joining: %v", addrs)
	n, err = a.serf.Join(addrs)
	return
}

// NotifyEvents causes the agent to begin sending log events to the
// given channel. The return value is a buffer of past events up to a
// point.
//
// All NotifyEvent calls should be paired with a StopEvents call.
func (a *Agent) NotifyEvents(ch chan<- string) []string {
	a.once.Do(a.init)

	a.eventLock.Lock()
	defer a.eventLock.Unlock()

	if a.eventChs == nil {
		a.eventChs = make(map[chan<- string]struct{})
	}

	a.eventChs[ch] = struct{}{}

	if a.events == nil {
		return nil
	}

	past := make([]string, 0, len(a.events))
	var endIndex int
	for i := len(a.events) - 1; i >= a.eventIndex; i-- {
		if a.events[i] != "" {
			break
		}

		endIndex = i
	}

	past = append(past, a.events[a.eventIndex:endIndex]...)
	past = append(past, a.events[:a.eventIndex]...)
	return past
}

// StopEvents causes the agent to stop sending events to the given
// channel.
func (a *Agent) StopEvents(ch chan<- string) {
	a.once.Do(a.init)

	a.eventLock.Lock()
	defer a.eventLock.Unlock()

	delete(a.eventChs, ch)
}

// Returns the Serf agent of the running Agent.
func (a *Agent) Serf() *serf.Serf {
	return a.serf
}

// Shutdown does a graceful shutdown of this agent and all of its processes.
func (a *Agent) Shutdown() error {
	a.once.Do(a.init)

	a.lock.Lock()
	defer a.lock.Unlock()

	if a.state == AgentIdle {
		return nil
	}

	// Stop the RPC listener which in turn will stop the RPC server.
	if err := a.rpcListener.Close(); err != nil {
		return err
	}

	// Gracefully leave the serf cluster
	a.logger.Println("[INFO] agent: requesting graceful leave from Serf")
	if err := a.serf.Leave(); err != nil {
		return err
	}

	a.logger.Println("[INFO] agent: requesting serf shutdown")
	if err := a.serf.Shutdown(); err != nil {
		return err
	}

	a.logger.Println("[INFO] agent: shutdown complete")
	a.state = AgentIdle
	close(a.shutdownCh)
	return nil
}

// Start starts the agent, kicking off any goroutines to handle various
// aspects of the agent.
func (a *Agent) Start() error {
	a.once.Do(a.init)

	a.lock.Lock()
	defer a.lock.Unlock()

	a.logger.Println("[INFO] Serf agent starting")

	// Setup logging a bit
	a.SerfConfig.MemberlistConfig.LogOutput = a.LogOutput
	a.SerfConfig.LogOutput = a.LogOutput

	eventCh := make(chan serf.Event, 64)
	a.SerfConfig.EventCh = eventCh

	var err error
	a.serf, err = serf.Create(a.SerfConfig)
	if err != nil {
		return fmt.Errorf("Error creating Serf: %s", err)
	}

	a.rpcListener, err = net.Listen("tcp", a.RPCAddr)
	if err != nil {
		return fmt.Errorf("Error starting RPC listener: %s", err)
	}

	rpcServer := rpc.NewServer()
	err = registerEndpoint(rpcServer, a)
	if err != nil {
		return fmt.Errorf("Error starting RPC server: %s", err)
	}

	go func(l net.Listener) {
		for {
			conn, err := l.Accept()
			if err != nil {
				a.logger.Printf("[ERR] RPC accept error: %s", err)
				return
			}
			go rpcServer.ServeConn(conn)
		}
	}(a.rpcListener)

	shutdownCh := make(chan struct{})

	// Only listen for events if we care about events.
	go a.eventLoop(a.EventHandler, eventCh, shutdownCh)

	a.shutdownCh = shutdownCh
	a.state = AgentRunning
	a.logger.Println("[INFO] Serf agent started")
	return nil
}

// UserEvent sends a UserEvent on Serf, see Serf.UserEvent.
func (a *Agent) UserEvent(name string, payload []byte) error {
	a.logger.Printf("Requesting user event send: %s %#v",
		name, string(payload))
	return a.serf.UserEvent(name, payload)
}

func (a *Agent) event(v string) {
	a.eventLock.Lock()
	defer a.eventLock.Unlock()

	if a.events == nil {
		a.events = make([]string, 512)
	}

	a.events[a.eventIndex] = v
	a.eventIndex++
	if a.eventIndex > len(a.events) {
		a.eventIndex = 0
	}

	for ch, _ := range a.eventChs {
		select {
		case ch <- v:
		default:
		}
	}
}

func (a *Agent) eventLoop(h EventHandler, eventCh <-chan serf.Event, done <-chan struct{}) {
	for {
		select {
		case <-done:
			return
		case e := <-eventCh:
			a.logger.Printf("[DEBUG] agent: Received event: %s", e.String())

			if h == nil {
				continue
			}

			err := h.HandleEvent(a.logger, e)
			if err != nil {
				a.logger.Printf("[ERR] agent: Error invoking event handler: %s", err)
			}
		}
	}
}

func (a *Agent) init() {
	if a.LogOutput == nil {
		a.LogOutput = os.Stderr
	}

	eventWriter := &EventWriter{Agent: a}
	a.LogOutput = io.MultiWriter(a.LogOutput, eventWriter)

	a.logger = log.New(a.LogOutput, "", log.LstdFlags)
}
<commit_msg>command/agent: Make event receive logs info level<commit_after>package agent

import (
	"fmt"
	"github.com/hashicorp/serf/serf"
	"io"
	"log"
	"net"
	"net/rpc"
	"os"
	"sync"
)

// Agent actually starts and manages a Serf agent.
type Agent struct {
	EventHandler EventHandler
	LogOutput    io.Writer
	RPCAddr      string
	SerfConfig   *serf.Config

	events      []string
	eventChs    map[chan<- string]struct{}
	eventIndex  int
	eventLock   sync.Mutex
	logger      *log.Logger
	once        sync.Once
	rpcListener net.Listener
	serf        *serf.Serf
	shutdownCh  chan<- struct{}
	state       AgentState
	lock        sync.Mutex
}

type AgentState int

const (
	AgentIdle AgentState = iota
	AgentRunning
)

// Join asks the Serf instance to join. See the Serf.Join function.
func (a *Agent) Join(addrs []string) (n int, err error) {
	a.once.Do(a.init)

	a.logger.Printf("[INFO] Agent joining: %v", addrs)
	n, err = a.serf.Join(addrs)
	return
}

// NotifyEvents causes the agent to begin sending log events to the
// given channel. The return value is a buffer of past events up to a
// point.
//
// All NotifyEvent calls should be paired with a StopEvents call.
func (a *Agent) NotifyEvents(ch chan<- string) []string {
	a.once.Do(a.init)

	a.eventLock.Lock()
	defer a.eventLock.Unlock()

	if a.eventChs == nil {
		a.eventChs = make(map[chan<- string]struct{})
	}

	a.eventChs[ch] = struct{}{}

	if a.events == nil {
		return nil
	}

	past := make([]string, 0, len(a.events))
	var endIndex int
	for i := len(a.events) - 1; i >= a.eventIndex; i-- {
		if a.events[i] != "" {
			break
		}

		endIndex = i
	}

	past = append(past, a.events[a.eventIndex:endIndex]...)
	past = append(past, a.events[:a.eventIndex]...)
	return past
}

// StopEvents causes the agent to stop sending events to the given
// channel.
func (a *Agent) StopEvents(ch chan<- string) {
	a.once.Do(a.init)

	a.eventLock.Lock()
	defer a.eventLock.Unlock()

	delete(a.eventChs, ch)
}

// Returns the Serf agent of the running Agent.
func (a *Agent) Serf() *serf.Serf {
	return a.serf
}

// Shutdown does a graceful shutdown of this agent and all of its processes.
func (a *Agent) Shutdown() error {
	a.once.Do(a.init)

	a.lock.Lock()
	defer a.lock.Unlock()

	if a.state == AgentIdle {
		return nil
	}

	// Stop the RPC listener which in turn will stop the RPC server.
	if err := a.rpcListener.Close(); err != nil {
		return err
	}

	// Gracefully leave the serf cluster
	a.logger.Println("[INFO] agent: requesting graceful leave from Serf")
	if err := a.serf.Leave(); err != nil {
		return err
	}

	a.logger.Println("[INFO] agent: requesting serf shutdown")
	if err := a.serf.Shutdown(); err != nil {
		return err
	}

	a.logger.Println("[INFO] agent: shutdown complete")
	a.state = AgentIdle
	close(a.shutdownCh)
	return nil
}

// Start starts the agent, kicking off any goroutines to handle various
// aspects of the agent.
func (a *Agent) Start() error {
	a.once.Do(a.init)

	a.lock.Lock()
	defer a.lock.Unlock()

	a.logger.Println("[INFO] Serf agent starting")

	// Setup logging a bit
	a.SerfConfig.MemberlistConfig.LogOutput = a.LogOutput
	a.SerfConfig.LogOutput = a.LogOutput

	eventCh := make(chan serf.Event, 64)
	a.SerfConfig.EventCh = eventCh

	var err error
	a.serf, err = serf.Create(a.SerfConfig)
	if err != nil {
		return fmt.Errorf("Error creating Serf: %s", err)
	}

	a.rpcListener, err = net.Listen("tcp", a.RPCAddr)
	if err != nil {
		return fmt.Errorf("Error starting RPC listener: %s", err)
	}

	rpcServer := rpc.NewServer()
	err = registerEndpoint(rpcServer, a)
	if err != nil {
		return fmt.Errorf("Error starting RPC server: %s", err)
	}

	go func(l net.Listener) {
		for {
			conn, err := l.Accept()
			if err != nil {
				a.logger.Printf("[ERR] RPC accept error: %s", err)
				return
			}
			go rpcServer.ServeConn(conn)
		}
	}(a.rpcListener)

	shutdownCh := make(chan struct{})

	// Only listen for events if we care about events.
	go a.eventLoop(a.EventHandler, eventCh, shutdownCh)

	a.shutdownCh = shutdownCh
	a.state = AgentRunning
	a.logger.Println("[INFO] Serf agent started")
	return nil
}

// UserEvent sends a UserEvent on Serf, see Serf.UserEvent.
func (a *Agent) UserEvent(name string, payload []byte) error {
	a.logger.Printf("Requesting user event send: %s %#v",
		name, string(payload))
	return a.serf.UserEvent(name, payload)
}

func (a *Agent) event(v string) {
	a.eventLock.Lock()
	defer a.eventLock.Unlock()

	if a.events == nil {
		a.events = make([]string, 512)
	}

	a.events[a.eventIndex] = v
	a.eventIndex++
	if a.eventIndex > len(a.events) {
		a.eventIndex = 0
	}

	for ch, _ := range a.eventChs {
		select {
		case ch <- v:
		default:
		}
	}
}

func (a *Agent) eventLoop(h EventHandler, eventCh <-chan serf.Event, done <-chan struct{}) {
	for {
		select {
		case <-done:
			return
		case e := <-eventCh:
			a.logger.Printf("[INFO] agent: Received event: %s", e.String())

			if h == nil {
				continue
			}

			err := h.HandleEvent(a.logger, e)
			if err != nil {
				a.logger.Printf("[ERR] agent: Error invoking event handler: %s", err)
			}
		}
	}
}

func (a *Agent) init() {
	if a.LogOutput == nil {
		a.LogOutput = os.Stderr
	}

	eventWriter := &EventWriter{Agent: a}
	a.LogOutput = io.MultiWriter(a.LogOutput, eventWriter)

	a.logger = log.New(a.LogOutput, "", log.LstdFlags)
}
<|endoftext|>


<commit_before>package command

import (
	"fmt"
	"github.com/hashicorp/nomad/api"
	"sort"
	"strings"
)

type NodeStatusCommand struct {
	Meta
}

func (c *NodeStatusCommand) Help() string {
	helpText := `
Usage: nomad node-status [options] <node>

  Display status information about a given node. The list of nodes
  returned includes only nodes which jobs may be scheduled to, and
  includes status and other high-level information.

  If a node ID is passed, information for that specific node will
  be displayed. If no node ID's are passed, then a short-hand
  list of all nodes will be displayed.

General Options:

  ` + generalOptionsUsage() + `

Node Status Options:

  -short
    Display short output. Used only when a single node is being
    queried, and drops verbose output about node allocations.

  -verbose
    Display full information.
`
	return strings.TrimSpace(helpText)
}

func (c *NodeStatusCommand) Synopsis() string {
	return "Display status information about nodes"
}

func (c *NodeStatusCommand) Run(args []string) int {
	var short, verbose, list_allocs bool

	flags := c.Meta.FlagSet("node-status", FlagSetClient)
	flags.Usage = func() { c.Ui.Output(c.Help()) }
	flags.BoolVar(&short, "short", false, "")
	flags.BoolVar(&verbose, "verbose", false, "")
	flags.BoolVar(&list_allocs, "allocs", false, "")

	if err := flags.Parse(args); err != nil {
		return 1
	}

	// Check that we got either a single node or none
	args = flags.Args()
	if len(args) > 1 {
		c.Ui.Error(c.Help())
		return 1
	}

	// Truncate the id unless full length is requested
	length := shortId
	if verbose {
		length = fullId
	}

	// Get the HTTP client
	client, err := c.Meta.Client()
	if err != nil {
		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
		return 1
	}

	// Use list mode if no node name was provided
	if len(args) == 0 {
		// Query the node info
		nodes, _, err := client.Nodes().List(nil)
		if err != nil {
			c.Ui.Error(fmt.Sprintf("Error querying node status: %s", err))
			return 1
		}

		// Return nothing if no nodes found
		if len(nodes) == 0 {
			return 0
		}

		// Format the nodes list
		out := make([]string, len(nodes)+1)
		if list_allocs {
			out[0] = "ID|DC|Name|Class|Drain|Status|Running Allocs"
		} else {
			out[0] = "ID|DC|Name|Class|Drain|Status"
		}
		for i, node := range nodes {
			if list_allocs {
				numAllocs, err := getRunningAllocs(client, node)
				if err != nil {
					c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err))
					return 1
				}
				out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s|%v",
					limit(node.ID, length),
					node.Datacenter,
					node.Name,
					node.NodeClass,
					node.Drain,
					node.Status,
					numAllocs)
			} else {
				out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s",
					limit(node.ID, length),
					node.Datacenter,
					node.Name,
					node.NodeClass,
					node.Drain,
					node.Status)
			}
		}

		// Dump the output
		c.Ui.Output(formatList(out))
		return 0
	}

	// Query the specific node
	nodeID := args[0]
	node, _, err := client.Nodes().Info(nodeID, nil)
	if err != nil {
		if len(nodeID) == 1 {
			c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
			return 1
		}
		if len(nodeID)%2 == 1 {
			// Identifiers must be of even length, so we strip off the last byte
			// to provide a consistent user experience.
			nodeID = nodeID[:len(nodeID)-1]
		}

		// Exact lookup failed, try with prefix based search
		nodes, _, err := client.Nodes().PrefixList(nodeID)
		if err != nil {
			c.Ui.Error(fmt.Sprintf("Error querying node info: %s", err))
			return 1
		}
		// Return error if no nodes are found
		if len(nodes) == 0 {
			c.Ui.Error(fmt.Sprintf("No node(s) with prefix %q found", nodeID))
			return 1
		}
		if len(nodes) > 1 {
			// Format the nodes list that matches the prefix so that the user
			// can create a more specific request
			out := make([]string, len(nodes)+1)
			out[0] = "ID|DC|Name|Class|Drain|Status"
			for i, node := range nodes {
				out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s",
					limit(node.ID, length),
					node.Datacenter,
					node.Name,
					node.NodeClass,
					node.Drain,
					node.Status)
			}
			// Dump the output
			c.Ui.Output(fmt.Sprintf("Prefix matched multiple nodes\n\n%s", formatList(out)))
			return 0
		}
		// Prefix lookup matched a single node
		node, _, err = client.Nodes().Info(nodes[0].ID, nil)
		if err != nil {
			c.Ui.Error(fmt.Sprintf("Error querying node info: %s", err))
			return 1
		}
	}

	m := node.Attributes
	keys := make([]string, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	var attributes []string
	for _, k := range keys {
		if k != "" {
			attributes = append(attributes, fmt.Sprintf("%s:%s", k, m[k]))
		}
	}

	// Format the output
	basic := []string{
		fmt.Sprintf("ID|%s", limit(node.ID, length)),
		fmt.Sprintf("Name|%s", node.Name),
		fmt.Sprintf("Class|%s", node.NodeClass),
		fmt.Sprintf("DC|%s", node.Datacenter),
		fmt.Sprintf("Drain|%v", node.Drain),
		fmt.Sprintf("Status|%s", node.Status),
		fmt.Sprintf("Attributes|%s", strings.Join(attributes, ", ")),
	}

	var allocs []string
	if !short {
		// Query the node allocations
		nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil)
		if err != nil {
			c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err))
			return 1
		}

		// Format the allocations
		allocs = make([]string, len(nodeAllocs)+1)
		allocs[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status"
		for i, alloc := range nodeAllocs {
			allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s",
				limit(alloc.ID, length),
				limit(alloc.EvalID, length),
				alloc.JobID,
				alloc.TaskGroup,
				alloc.DesiredStatus,
				alloc.ClientStatus)
		}
	}

	// Dump the output
	c.Ui.Output(formatKV(basic))
	if !short {
		c.Ui.Output("\n==> Allocations")
		c.Ui.Output(formatList(allocs))
	}
	return 0
}

func getRunningAllocs(client *api.Client, node *api.NodeListStub) (int, error) {
	// Fetch number of running allocations per node
	numAllocs := 0
	nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil)
	for _, alloc := range nodeAllocs {
		if alloc.ClientStatus == "running" {
			numAllocs += 1
		}
	}
	return numAllocs, err
}
<commit_msg>Add -allocs to help output<commit_after>package command

import (
	"fmt"
	"github.com/hashicorp/nomad/api"
	"sort"
	"strings"
)

type NodeStatusCommand struct {
	Meta
}

func (c *NodeStatusCommand) Help() string {
	helpText := `
Usage: nomad node-status [options] <node>

  Display status information about a given node. The list of nodes
  returned includes only nodes which jobs may be scheduled to, and
  includes status and other high-level information.

  If a node ID is passed, information for that specific node will
  be displayed. If no node ID's are passed, then a short-hand
  list of all nodes will be displayed.

General Options:

  ` + generalOptionsUsage() + `

Node Status Options:

  -short
    Display short output. Used only when a single node is being
    queried, and drops verbose output about node allocations.

  -verbose
    Display full information.

  -allocs
    Display a count of running allocations for each node.
`
	return strings.TrimSpace(helpText)
}

func (c *NodeStatusCommand) Synopsis() string {
	return "Display status information about nodes"
}

func (c *NodeStatusCommand) Run(args []string) int {
	var short, verbose, list_allocs bool

	flags := c.Meta.FlagSet("node-status", FlagSetClient)
	flags.Usage = func() { c.Ui.Output(c.Help()) }
	flags.BoolVar(&short, "short", false, "")
	flags.BoolVar(&verbose, "verbose", false, "")
	flags.BoolVar(&list_allocs, "allocs", false, "")

	if err := flags.Parse(args); err != nil {
		return 1
	}

	// Check that we got either a single node or none
	args = flags.Args()
	if len(args) > 1 {
		c.Ui.Error(c.Help())
		return 1
	}

	// Truncate the id unless full length is requested
	length := shortId
	if verbose {
		length = fullId
	}

	// Get the HTTP client
	client, err := c.Meta.Client()
	if err != nil {
		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
		return 1
	}

	// Use list mode if no node name was provided
	if len(args) == 0 {
		// Query the node info
		nodes, _, err := client.Nodes().List(nil)
		if err != nil {
			c.Ui.Error(fmt.Sprintf("Error querying node status: %s", err))
			return 1
		}

		// Return nothing if no nodes found
		if len(nodes) == 0 {
			return 0
		}

		// Format the nodes list
		out := make([]string, len(nodes)+1)
		if list_allocs {
			out[0] = "ID|DC|Name|Class|Drain|Status|Running Allocs"
		} else {
			out[0] = "ID|DC|Name|Class|Drain|Status"
		}
		for i, node := range nodes {
			if list_allocs {
				numAllocs, err := getRunningAllocs(client, node)
				if err != nil {
					c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err))
					return 1
				}
				out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s|%v",
					limit(node.ID, length),
					node.Datacenter,
					node.Name,
					node.NodeClass,
					node.Drain,
					node.Status,
					numAllocs)
			} else {
				out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s",
					limit(node.ID, length),
					node.Datacenter,
					node.Name,
					node.NodeClass,
					node.Drain,
					node.Status)
			}
		}

		// Dump the output
		c.Ui.Output(formatList(out))
		return 0
	}

	// Query the specific node
	nodeID := args[0]
	node, _, err := client.Nodes().Info(nodeID, nil)
	if err != nil {
		if len(nodeID) == 1 {
			c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
			return 1
		}
		if len(nodeID)%2 == 1 {
			// Identifiers must be of even length, so we strip off the last byte
			// to provide a consistent user experience.
			nodeID = nodeID[:len(nodeID)-1]
		}

		// Exact lookup failed, try with prefix based search
		nodes, _, err := client.Nodes().PrefixList(nodeID)
		if err != nil {
			c.Ui.Error(fmt.Sprintf("Error querying node info: %s", err))
			return 1
		}
		// Return error if no nodes are found
		if len(nodes) == 0 {
			c.Ui.Error(fmt.Sprintf("No node(s) with prefix %q found", nodeID))
			return 1
		}
		if len(nodes) > 1 {
			// Format the nodes list that matches the prefix so that the user
			// can create a more specific request
			out := make([]string, len(nodes)+1)
			out[0] = "ID|DC|Name|Class|Drain|Status"
			for i, node := range nodes {
				out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s",
					limit(node.ID, length),
					node.Datacenter,
					node.Name,
					node.NodeClass,
					node.Drain,
					node.Status)
			}
			// Dump the output
			c.Ui.Output(fmt.Sprintf("Prefix matched multiple nodes\n\n%s", formatList(out)))
			return 0
		}
		// Prefix lookup matched a single node
		node, _, err = client.Nodes().Info(nodes[0].ID, nil)
		if err != nil {
			c.Ui.Error(fmt.Sprintf("Error querying node info: %s", err))
			return 1
		}
	}

	m := node.Attributes
	keys := make([]string, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	var attributes []string
	for _, k := range keys {
		if k != "" {
			attributes = append(attributes, fmt.Sprintf("%s:%s", k, m[k]))
		}
	}

	// Format the output
	basic := []string{
		fmt.Sprintf("ID|%s", limit(node.ID, length)),
		fmt.Sprintf("Name|%s", node.Name),
		fmt.Sprintf("Class|%s", node.NodeClass),
		fmt.Sprintf("DC|%s", node.Datacenter),
		fmt.Sprintf("Drain|%v", node.Drain),
		fmt.Sprintf("Status|%s", node.Status),
		fmt.Sprintf("Attributes|%s", strings.Join(attributes, ", ")),
	}

	var allocs []string
	if !short {
		// Query the node allocations
		nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil)
		if err != nil {
			c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err))
			return 1
		}

		// Format the allocations
		allocs = make([]string, len(nodeAllocs)+1)
		allocs[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status"
		for i, alloc := range nodeAllocs {
			allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s",
				limit(alloc.ID, length),
				limit(alloc.EvalID, length),
				alloc.JobID,
				alloc.TaskGroup,
				alloc.DesiredStatus,
				alloc.ClientStatus)
		}
	}

	// Dump the output
	c.Ui.Output(formatKV(basic))
	if !short {
		c.Ui.Output("\n==> Allocations")
		c.Ui.Output(formatList(allocs))
	}
	return 0
}

func getRunningAllocs(client *api.Client, node *api.NodeListStub) (int, error) {
	// Fetch number of running allocations per node
	numAllocs := 0
	nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil)
	for _, alloc := range nodeAllocs {
		if alloc.ClientStatus == "running" {
			numAllocs += 1
		}
	}
	return numAllocs, err
}
<|endoftext|>


<commit_before>package drain

import (
	"strconv"
	"strings"
	"time"

	bosherr "github.com/cloudfoundry/bosh-utils/errors"
	boshlog "github.com/cloudfoundry/bosh-utils/logger"
	boshsys "github.com/cloudfoundry/bosh-utils/system"
	"github.com/pivotal-golang/clock"
)

type ConcreteScript struct {
	fs     boshsys.FileSystem
	runner boshsys.CmdRunner

	tag    string
	path   string
	params ScriptParams

	timeService clock.Clock
	logTag      string
	logger      boshlog.Logger

	cancelCh chan struct{}
}

func NewConcreteScript(
	fs boshsys.FileSystem,
	runner boshsys.CmdRunner,
	tag string,
	path string,
	params ScriptParams,
	timeService clock.Clock,
	logger boshlog.Logger) ConcreteScript {
	return ConcreteScript{
		fs:     fs,
		runner: runner,

		tag:    tag,
		path:   path,
		params: params,

		timeService: timeService,

		logTag: "DrainScript",
		logger: logger,

		cancelCh: make(chan struct{}, 1),
	}
}

func (s ConcreteScript) Tag() string          { return s.tag }
func (s ConcreteScript) Path() string         { return s.path }
func (s ConcreteScript) Params() ScriptParams { return s.params }
func (s ConcreteScript) Exists() bool         { return s.fs.FileExists(s.path) }

func (s ConcreteScript) Run() error {
	params := s.params

	for {
		value, err := s.runOnce(params)
		if err != nil {
			return err
		} else if value < 0 {
			s.timeService.Sleep(time.Duration(-value) * time.Second)
			params = params.ToStatusParams()
		} else {
			s.timeService.Sleep(time.Duration(value) * time.Second)
			return nil
		}
	}
}

func (s ConcreteScript) Cancel() error {
	select {
	case s.cancelCh <- struct{}{}:
	default:
	}
	return nil
}

func (s ConcreteScript) runOnce(params ScriptParams) (int, error) {
	jobChange := params.JobChange()
	hashChange := params.HashChange()
	updatedPkgs := params.UpdatedPackages()

	command := boshsys.Command{
		Name: s.path,
		Env: map[string]string{
			"PATH": "/usr/sbin:/usr/bin:/sbin:/bin",
		},
	}

	jobState, err := params.JobState()
	if err != nil {
		return 0, bosherr.WrapError(err, "Getting job state")
	}

	if jobState != "" {
		command.Env["BOSH_JOB_STATE"] = jobState
	}

	jobNextState, err := params.JobNextState()
	if err != nil {
		return 0, bosherr.WrapError(err, "Getting job next state")
	}

	if jobNextState != "" {
		command.Env["BOSH_JOB_NEXT_STATE"] = jobNextState
	}

	command.Args = append(command.Args, jobChange, hashChange)
	command.Args = append(command.Args, updatedPkgs...)

	process, err := s.runner.RunComplexCommandAsync(command)
	if err != nil {
		return 0, bosherr.WrapError(err, "Running drain script")
	}

	var result boshsys.Result

	isCanceled := false

	// Can only wait once on a process but cancelling can happen multiple times
	for processExitedCh := process.Wait(); processExitedCh != nil; {
		select {
		case result = <-processExitedCh:
			processExitedCh = nil
		case <-s.cancelCh:
			// Ignore possible TerminateNicely error since we cannot return it
			err := process.TerminateNicely(10 * time.Second)
			if err != nil {
				s.logger.Error(s.logTag, "Failed to terminate %s", err.Error())
			}
			isCanceled = true
		}
	}

	if isCanceled {
		if result.Error != nil {
			return 0, bosherr.WrapError(result.Error, "Script was cancelled by user request")
		} else {
			return 0, bosherr.Error("Script was cancelled by user request")
		}
	}

	if result.Error != nil && result.ExitStatus == -1 {
		return 0, bosherr.WrapError(result.Error, "Running drain script")
	}

	value, err := strconv.Atoi(strings.TrimSpace(result.Stdout))
	if err != nil {
		return 0, bosherr.WrapError(err, "Script did not return a signed integer")
	}

	return value, nil
}
<commit_msg>Fix linter complaint<commit_after>package drain

import (
	"strconv"
	"strings"
	"time"

	bosherr "github.com/cloudfoundry/bosh-utils/errors"
	boshlog "github.com/cloudfoundry/bosh-utils/logger"
	boshsys "github.com/cloudfoundry/bosh-utils/system"
	"github.com/pivotal-golang/clock"
)

type ConcreteScript struct {
	fs     boshsys.FileSystem
	runner boshsys.CmdRunner

	tag    string
	path   string
	params ScriptParams

	timeService clock.Clock
	logTag      string
	logger      boshlog.Logger

	cancelCh chan struct{}
}

func NewConcreteScript(
	fs boshsys.FileSystem,
	runner boshsys.CmdRunner,
	tag string,
	path string,
	params ScriptParams,
	timeService clock.Clock,
	logger boshlog.Logger) ConcreteScript {
	return ConcreteScript{
		fs:     fs,
		runner: runner,

		tag:    tag,
		path:   path,
		params: params,

		timeService: timeService,

		logTag: "DrainScript",
		logger: logger,

		cancelCh: make(chan struct{}, 1),
	}
}

func (s ConcreteScript) Tag() string          { return s.tag }
func (s ConcreteScript) Path() string         { return s.path }
func (s ConcreteScript) Params() ScriptParams { return s.params }
func (s ConcreteScript) Exists() bool         { return s.fs.FileExists(s.path) }

func (s ConcreteScript) Run() error {
	params := s.params

	for {
		value, err := s.runOnce(params)
		if err != nil {
			return err
		} else if value < 0 {
			s.timeService.Sleep(time.Duration(-value) * time.Second)
			params = params.ToStatusParams()
		} else {
			s.timeService.Sleep(time.Duration(value) * time.Second)
			return nil
		}
	}
}

func (s ConcreteScript) Cancel() error {
	select {
	case s.cancelCh <- struct{}{}:
	default:
	}
	return nil
}

func (s ConcreteScript) runOnce(params ScriptParams) (int, error) {
	jobChange := params.JobChange()
	hashChange := params.HashChange()
	updatedPkgs := params.UpdatedPackages()

	command := boshsys.Command{
		Name: s.path,
		Env: map[string]string{
			"PATH": "/usr/sbin:/usr/bin:/sbin:/bin",
		},
	}

	jobState, err := params.JobState()
	if err != nil {
		return 0, bosherr.WrapError(err, "Getting job state")
	}

	if jobState != "" {
		command.Env["BOSH_JOB_STATE"] = jobState
	}

	jobNextState, err := params.JobNextState()
	if err != nil {
		return 0, bosherr.WrapError(err, "Getting job next state")
	}

	if jobNextState != "" {
		command.Env["BOSH_JOB_NEXT_STATE"] = jobNextState
	}

	command.Args = append(command.Args, jobChange, hashChange)
	command.Args = append(command.Args, updatedPkgs...)

	process, err := s.runner.RunComplexCommandAsync(command)
	if err != nil {
		return 0, bosherr.WrapError(err, "Running drain script")
	}

	var result boshsys.Result

	isCanceled := false

	// Can only wait once on a process but cancelling can happen multiple times
	for processExitedCh := process.Wait(); processExitedCh != nil; {
		select {
		case result = <-processExitedCh:
			processExitedCh = nil
		case <-s.cancelCh:
			// Ignore possible TerminateNicely error since we cannot return it
			err := process.TerminateNicely(10 * time.Second)
			if err != nil {
				s.logger.Error(s.logTag, "Failed to terminate %s", err.Error())
			}
			isCanceled = true
		}
	}

	if isCanceled {
		if result.Error != nil {
			return 0, bosherr.WrapError(result.Error, "Script was cancelled by user request")
		}

		return 0, bosherr.Error("Script was cancelled by user request")
	}

	if result.Error != nil && result.ExitStatus == -1 {
		return 0, bosherr.WrapError(result.Error, "Running drain script")
	}

	value, err := strconv.Atoi(strings.TrimSpace(result.Stdout))
	if err != nil {
		return 0, bosherr.WrapError(err, "Script did not return a signed integer")
	}

	return value, nil
}
<|endoftext|>


<commit_before>package workload

import (
	"strings"

	"github.com/rancher/norman/store/transform"
	"github.com/rancher/norman/types"
	"github.com/rancher/norman/types/convert"
	"github.com/rancher/norman/types/definition"
	"github.com/rancher/norman/types/values"
	"github.com/rancher/rancher/pkg/api/store/pod"
	"github.com/sirupsen/logrus"
)

var (
	hideOwnerReferenceKinds = map[string]bool{"CronJob": true, "Deployment": true}
)

func NewTransformStore(store types.Store) types.Store {
	return &transform.Store{
		Store: store,
		Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
			hide := true
			if opt != nil && opt.Options["hidden"] == "true" {
				hide = false
			} else if opt != nil && opt.Options["ByID"] == "true" {
				hide = false
			} else {
				hide = hideByOwner(data)
			}
			typeName := definition.GetType(data)
			name, _ := data["name"].(string)
			if hide && data["ownerReferences"] != nil {
				pod.SaveOwner(apiContext, typeName, name, data)
				return nil, nil
			}
			id, _ := data["id"].(string)
			if typeName != "" && id != "" {
				data["id"] = strings.ToLower(typeName) + ":" + id
			}
			SetPublicEnpointsFields(data)
			nodeName := convert.ToString(values.GetValueN(data, "nodeId"))
			if nodeName != "" {
				state := getState(data)
				nodeID := state[getKey(nodeName)]
				delete(data, "nodeId")
				values.PutValue(data, nodeID, "scheduling", "node", "nodeId")
			}
			setTransitioning(apiContext, data)
			return data, nil
		},
	}
}

func SetPublicEnpointsFields(data map[string]interface{}) {
	if val, ok := data["publicEndpoints"]; ok {
		eps := convert.ToInterfaceSlice(val)
		for _, ep := range eps {
			epMap, err := convert.EncodeToMap(ep)
			if err != nil {
				logrus.Errorf("Failed to convert public endpoint: %v", err)
				continue
			}
			epMap["serviceId"] = epMap["serviceName"]
			epMap["nodeId"] = epMap["nodeName"]
			epMap["ingressId"] = epMap["ingressName"]
		}
	}
}

func setTransitioning(apiContext *types.APIContext, data map[string]interface{}) {
	if data["transitioning"] == "yes" || data["transitioning"] == "error" {
		return
	}
	workloadType := convert.ToString(values.GetValueN(data, "type"))
	switch workloadType {
	case "/v3/project/schemas/deployment":
		update(data, "deploymentStatus", "replicas", "readyReplicas")
	case "/v3/project/schemas/daemonSet":
		update(data, "daemonSetStatus", "desiredNumberScheduled", "numberReady")
	case "/v3/project/schemas/statefulSet":
		update(data, "statefulSetStatus", "replicas", "readyReplicas")
	}
}

func update(data map[string]interface{}, statusField string, desiredField string, currField string) {
	if desiredNum, err := convert.ToNumber(values.GetValueN(data, statusField, desiredField)); err == nil {
		if readyNum, err := convert.ToNumber(values.GetValueN(data, statusField, currField)); err == nil {
			if desiredNum != readyNum {
				data["state"] = "updating"
				data["transitioning"] = "yes"
				data["transitioningMessage"] = "upgrading workload"
			}
		}
	}
}

func hideByOwner(data map[string]interface{}) bool {
	if data["ownerReferences"] != nil {
		owners := convert.ToMapSlice(data["ownerReferences"])
		for _, owner := range owners {
			ownerKind := convert.ToString(owner["kind"])
			if _, ok := hideOwnerReferenceKinds[ownerKind]; ok {
				return true
			}
		}
	}
	return false
}
<commit_msg>SetPublicEndpointsFields<commit_after>package workload

import (
	"strings"

	"github.com/rancher/norman/store/transform"
	"github.com/rancher/norman/types"
	"github.com/rancher/norman/types/convert"
	"github.com/rancher/norman/types/definition"
	"github.com/rancher/norman/types/values"
	"github.com/rancher/rancher/pkg/api/store/pod"
	"github.com/sirupsen/logrus"
)

var (
	hideOwnerReferenceKinds = map[string]bool{"CronJob": true, "Deployment": true}
)

func NewTransformStore(store types.Store) types.Store {
	return &transform.Store{
		Store: store,
		Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
			hide := true
			if opt != nil && opt.Options["hidden"] == "true" {
				hide = false
			} else if opt != nil && opt.Options["ByID"] == "true" {
				hide = false
			} else {
				hide = hideByOwner(data)
			}
			typeName := definition.GetType(data)
			name, _ := data["name"].(string)
			if hide && data["ownerReferences"] != nil {
				pod.SaveOwner(apiContext, typeName, name, data)
				return nil, nil
			}
			id, _ := data["id"].(string)
			if typeName != "" && id != "" {
				data["id"] = strings.ToLower(typeName) + ":" + id
			}
			SetPublicEndpointsFields(data)
			nodeName := convert.ToString(values.GetValueN(data, "nodeId"))
			if nodeName != "" {
				state := getState(data)
				nodeID := state[getKey(nodeName)]
				delete(data, "nodeId")
				values.PutValue(data, nodeID, "scheduling", "node", "nodeId")
			}
			setTransitioning(apiContext, data)
			return data, nil
		},
	}
}

func SetPublicEndpointsFields(data map[string]interface{}) {
	if val, ok := data["publicEndpoints"]; ok {
		eps := convert.ToInterfaceSlice(val)
		for _, ep := range eps {
			epMap, err := convert.EncodeToMap(ep)
			if err != nil {
				logrus.Errorf("Failed to convert public endpoint: %v", err)
				continue
			}
			epMap["serviceId"] = epMap["serviceName"]
			epMap["nodeId"] = epMap["nodeName"]
			epMap["ingressId"] = epMap["ingressName"]
		}
	}
}

func setTransitioning(apiContext *types.APIContext, data map[string]interface{}) {
	if data["transitioning"] == "yes" || data["transitioning"] == "error" {
		return
	}
	workloadType := convert.ToString(values.GetValueN(data, "type"))
	switch workloadType {
	case "/v3/project/schemas/deployment":
		update(data, "deploymentStatus", "replicas", "readyReplicas")
	case "/v3/project/schemas/daemonSet":
		update(data, "daemonSetStatus", "desiredNumberScheduled", "numberReady")
	case "/v3/project/schemas/statefulSet":
		update(data, "statefulSetStatus", "replicas", "readyReplicas")
	}
}

func update(data map[string]interface{}, statusField string, desiredField string, currField string) {
	if desiredNum, err := convert.ToNumber(values.GetValueN(data, statusField, desiredField)); err == nil {
		if readyNum, err := convert.ToNumber(values.GetValueN(data, statusField, currField)); err == nil {
			if desiredNum != readyNum {
				data["state"] = "updating"
				data["transitioning"] = "yes"
				data["transitioningMessage"] = "upgrading workload"
			}
		}
	}
}

func hideByOwner(data map[string]interface{}) bool {
	if data["ownerReferences"] != nil {
		owners := convert.ToMapSlice(data["ownerReferences"])
		for _, owner := range owners {
			ownerKind := convert.ToString(owner["kind"])
			if _, ok := hideOwnerReferenceKinds[ownerKind]; ok {
				return true
			}
		}
	}
	return false
}
<|endoftext|>


<commit_before>// Copyright 2018 The Fractal Team Authors
// This file is part of the fractal project.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package consensus

import (
	"math/big"

	"github.com/fractalplatform/fractal/common"
	"github.com/fractalplatform/fractal/params"
	"github.com/fractalplatform/fractal/processor/vm"
	"github.com/fractalplatform/fractal/rpc"
	"github.com/fractalplatform/fractal/state"
	"github.com/fractalplatform/fractal/types"
)

// IAPI returns the RPC APIs this consensus engine provides.
type IAPI interface {
	APIs(chain IChainReader) []rpc.API
}

// IChainReader defines a small collection of methods needed to access the local blockchain.
type IChainReader interface {
	// Config retrieves the blockchain's chain configuration.
	Config() *params.ChainConfig

	// CurrentHeader retrieves the current header from the local chain.
	CurrentHeader() *types.Header

	// GetHeaderByNumber retrieves a block header from the database by number.
	GetHeaderByNumber(number uint64) *types.Header

	// GetHeaderByHash retrieves a block header from the database by its hash.
	GetHeaderByHash(hash common.Hash) *types.Header

	// GetHeader retrieves a block header from the database by hash and number.
	GetHeader(hash common.Hash, number uint64) *types.Header

	// GetBlock retrieves a block from the database by hash and number.
	GetBlock(hash common.Hash, number uint64) *types.Block

	// StateAt retrieves a block state from the database by hash.
	StateAt(hash common.Hash) (*state.StateDB, error)

	// WriteBlockWithState writes the block and all associated state to the database.
	WriteBlockWithState(block *types.Block, receipts []*types.Receipt, state *state.StateDB) (bool, error)

	// HasBlockAndState checks if a block and associated state trie is fully present
	// in the database or not, caching it if present.
	HasBlockAndState(hash common.Hash, number uint64) bool

	// HasBlock checks if a block is fully present in the database or not.
	HasBlock(hash common.Hash, number uint64) bool

	// FillForkID fills the current and next forkID
	FillForkID(header *types.Header, statedb *state.StateDB) error

	// ForkUpdate checks and records the fork information
	ForkUpdate(block *types.Block, statedb *state.StateDB) error
}

// IValidator implements consensus validator.
type IValidator interface {
	// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
	// that a new block should have.
	CalcDifficulty(chain IChainReader, time uint64, parent *types.Header) *big.Int

	// VerifySeal checks whether the crypto seal on a header is valid according to the consensus rules of the given engine.
	VerifySeal(chain IChainReader, header *types.Header) error
}

// IEngine is an algorithm agnostic consensus engine.
type IEngine interface {
	// Author retrieves the name of the account that minted the given block
	Author(header *types.Header) (common.Name, error)

	// Prepare initializes the consensus fields of a block header according to the rules of a particular engine. The changes are executed inline.
	Prepare(chain IChainReader, header *types.Header, txs []*types.Transaction, receipts []*types.Receipt, state *state.StateDB) error

	// Finalize assembles the final block.
	Finalize(chain IChainReader, header *types.Header, txs []*types.Transaction, receipts []*types.Receipt, state *state.StateDB) (*types.Block, error)

	// Seal generates a new block for the given input block with the local miner's seal place on top.
	Seal(chain IChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error)

	Engine() IEngine

	ProcessAction(fid uint64, number uint64, chainCfg *params.ChainConfig, state *state.StateDB, action *types.Action) ([]*types.InternalAction, error)

	GetDelegatedByTime(state *state.StateDB, candidate string, timestamp uint64) (stake *big.Int, err error)

	//GetLatestEpoch(state *state.StateDB) (epoch uint64, err error)

	//GetPrevEpoch(state *state.StateDB, epoch uint64) (pecho uint64, err error)

	//GetNextEpoch(state *state.StateDB, epoch uint64) (necho uint64, err error)

	GetEpoch(state *state.StateDB, t uint64, curEpoch uint64) (epoch uint64, time uint64, err error)

	GetActivedCandidateSize(state *state.StateDB, epoch uint64) (size uint64, err error)

	GetActivedCandidate(state *state.StateDB, epoch uint64, index uint64) (name string, stake *big.Int, totalVote *big.Int, counter uint64, actualCounter uint64, replace uint64, isbad bool, err error)

	GetCandidateStake(state *state.StateDB, epoch uint64, candidate string) (stake *big.Int, err error)

	GetVoterStake(state *state.StateDB, epoch uint64, voter string, candidate string) (stake *big.Int, err error)

	CalcBFTIrreversible() uint64

	IAPI

	IValidator
}

// ITxProcessor is an Processor.
type ITxProcessor interface {
	// ApplyTransaction attempts to apply a transaction.
	ApplyTransaction(coinbase *common.Name, gp *common.GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, uint64, error)
}

// ITxPool contains all currently known transactions.
type ITxPool interface {
	Pending() (map[common.Name][]*types.Transaction, error)
}

// IConsensus defines a small collection of methods needed for miner.
type IConsensus interface {
	IChainReader
	IEngine
	ITxProcessor
	ITxPool
}
<commit_msg>add doc of intefaces in consensus (#446)<commit_after>// Copyright 2018 The Fractal Team Authors
// This file is part of the fractal project.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package consensus

import (
	"math/big"

	"github.com/fractalplatform/fractal/common"
	"github.com/fractalplatform/fractal/params"
	"github.com/fractalplatform/fractal/processor/vm"
	"github.com/fractalplatform/fractal/rpc"
	"github.com/fractalplatform/fractal/state"
	"github.com/fractalplatform/fractal/types"
)

// IAPI defines interface to provide the RPC APIs.
type IAPI interface {
	APIs(chain IChainReader) []rpc.API
}

// IChainReader defines interface to access the blockchain.
type IChainReader interface {
	// Config retrieves the blockchain's configuration.
	Config() *params.ChainConfig

	// CurrentHeader retrieves the current header from the database.
	CurrentHeader() *types.Header

	// GetHeaderByNumber retrieves a block header from the database by number.
	GetHeaderByNumber(number uint64) *types.Header

	// GetHeaderByHash retrieves a block header from the database by its hash.
	GetHeaderByHash(hash common.Hash) *types.Header

	// GetHeader retrieves a block header from the database by hash and number.
	GetHeader(hash common.Hash, number uint64) *types.Header

	// GetBlock retrieves a block from the database by hash and number.
	GetBlock(hash common.Hash, number uint64) *types.Block

	// StateAt retrieves a block state from the database by hash.
	StateAt(hash common.Hash) (*state.StateDB, error)

	// WriteBlockWithState writes the block and all associated state to the database.
	WriteBlockWithState(block *types.Block, receipts []*types.Receipt, state *state.StateDB) (bool, error)

	// HasBlockAndState checks if a block and associated state trie is fully present
	// in the database or not, caching it if present.
	HasBlockAndState(hash common.Hash, number uint64) bool

	// HasBlock checks if a block is fully present in the database or not.
	HasBlock(hash common.Hash, number uint64) bool

	// FillForkID fills the current and next forkID
	FillForkID(header *types.Header, statedb *state.StateDB) error

	// ForkUpdate checks and records the fork information
	ForkUpdate(block *types.Block, statedb *state.StateDB) error
}

// IValidator defines interface to validate block.
type IValidator interface {
	// CalcDifficulty is the difficulty adjustment algorithm.
	// It returns the difficulty that a new block should have.
	CalcDifficulty(chain IChainReader, time uint64, parent *types.Header) *big.Int

	// VerifySeal checks whether the crypto seal on a header is valid according to the consensus rules of the given engine.
	VerifySeal(chain IChainReader, header *types.Header) error
}

// IEngine is an algorithm agnostic consensus engine.
type IEngine interface {
	// Author retrieves the name of the account that minted the given block
	Author(header *types.Header) (common.Name, error)

	// Prepare initializes the consensus fields of a block header according to the rules of a particular engine. The changes are executed inline.
	Prepare(chain IChainReader, header *types.Header, txs []*types.Transaction, receipts []*types.Receipt, state *state.StateDB) error

	// Finalize assembles the final block.
	Finalize(chain IChainReader, header *types.Header, txs []*types.Transaction, receipts []*types.Receipt, state *state.StateDB) (*types.Block, error)

	// Seal generates a new block for the given input block with the local miner's seal place on top.
	Seal(chain IChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error)

	// Engine retrieves engine interface
	Engine() IEngine

	// Engine process actions of dpos
	ProcessAction(fid uint64, number uint64, chainCfg *params.ChainConfig, state *state.StateDB, action *types.Action) ([]*types.InternalAction, error)
	// GetDelegatedByTime get delegate stake of candidate in snapshot database
	GetDelegatedByTime(state *state.StateDB, candidate string, timestamp uint64) (stake *big.Int, err error)

	// GetEpoch get number and timestamp of prev/next epoch
	GetEpoch(state *state.StateDB, t uint64, curEpoch uint64) (epoch uint64, time uint64, err error)

	// GetActivedCandidateSize get actived candidates size in epoch database
	GetActivedCandidateSize(state *state.StateDB, epoch uint64) (size uint64, err error)

	// GetActivedCandidate get actived info in epoch database
	GetActivedCandidate(state *state.StateDB, epoch uint64, index uint64) (name string, stake *big.Int, totalVote *big.Int, counter uint64, actualCounter uint64, replace uint64, isbad bool, err error)

	// GetCandidateStake get voted stake in epoch database
	GetCandidateStake(state *state.StateDB, epoch uint64, candidate string) (stake *big.Int, err error)

	// GetVoterStake get voted stake in epoch database
	GetVoterStake(state *state.StateDB, epoch uint64, voter string, candidate string) (stake *big.Int, err error)

	// CalcBFTIrreversible get chain rreversible number
	CalcBFTIrreversible() uint64

	IAPI

	IValidator
}

// ITxProcessor defines interface to process tx.
type ITxProcessor interface {
	// ApplyTransaction attempts to apply a transaction.
	ApplyTransaction(coinbase *common.Name, gp *common.GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, uint64, error)
}

// ITxPool defines interface to get pending transactions.
type ITxPool interface {
	// Pending attempts to get all pending transaction.
	Pending() (map[common.Name][]*types.Transaction, error)
}

// IConsensus defines interface to invoke for miner.
type IConsensus interface {
	IChainReader
	IEngine
	ITxProcessor
	ITxPool
}
<|endoftext|>


<commit_before>package tsar

import (
	"log"
	"errors"
	"fmt"

	"gopkg.in/libgit2/git2go.v22"
	"github.com/SearchSpring/RepoTsar/gitutils"
	"github.com/SearchSpring/RepoTsar/fileutils"
	"github.com/SearchSpring/RepoTsar/config"
)

type semaphore chan error

type RepoTsar struct{
	Config config.Config
	Branch string
	ReposList []string
	Signature *git.Signature
}

func (r *RepoTsar) Run() error {
	reposlist := r.ReposList
	// if the reposlist is empty append all repos from config to reposlist
	c := r.Config.Repos
	if reposlist[0] == "" {
		// delete item from array
		reposlist = append(reposlist[:0], reposlist[0+1:]...)
		for k := range c {
			reposlist = append(reposlist,k)
		}
	}

	// Semaphore for concurrency
	thrnum := len(reposlist)
	sem := make(semaphore, thrnum)
	for _, key := range reposlist {
		go func(k string) {

			_,ok := c[k]
			if ! ok {
				err := errors.New(fmt.Sprintf("Repo %#v is not defined in config\n", k))
				sem <- err
				return
			}
			log.Printf("[%s, url: %s, path: %s, branch: %s]", k, c[k].URL, c[k].Path, c[k].Branch)

			// Createpath
			path,err := fileutils.CreatePath(c[k].Path)
			if err != nil {
				sem <-err
				return
			}

			// Clone Repo
			cloneinfo := &gitutils.CloneInfo{
				Reponame: k,
				Path: path,
				URL: c[k].URL,
				Branch: c[k].Branch,
			}
			repo, err := cloneinfo.CloneRepo()
			if err != nil {
				sem <-err
				return
			}

			// Git Pull
			pullinfo := gitutils.PullInfo{
				Reponame: k,
				Repo: repo,
				Branch: c[k].Branch,
			}
			err = pullinfo.GitPull()
			if err != nil {
				sem <-err
				return
			}

			// If branch option, branch and checkout selected repos
			if r.Branch != "" {
				branchinfo := &gitutils.BranchInfo{
					Reponame: k,
					Branchname: r.Branch,
					Msg: "RepoTsar Branching",
					Repo: *repo,
					Signature: r.Signature,
				}
				err = branchinfo.GitBranch()
				if err != nil {
					sem <-err
					return
				}
			}
			sem <-nil
			return
		}(key)
	}

	// Wait for threads to finish
	for i := 0; i < thrnum; i++ {
		err := <-sem
		if err != nil {
			return err
		}
	}
	return nil
}<commit_msg>Fixing number of bugs<commit_after>package tsar

import (
	"log"
	"errors"
	"fmt"

	"gopkg.in/libgit2/git2go.v22"
	"github.com/SearchSpring/RepoTsar/gitutils"
	"github.com/SearchSpring/RepoTsar/fileutils"
	"github.com/SearchSpring/RepoTsar/config"
)

type semaphore chan error

type RepoTsar struct{
	Config config.Config
	Branch string
	ReposList []string
	Signature *git.Signature
}

func (r *RepoTsar) Run() error {
	reposlist := r.ReposList
	// if the reposlist is empty append all repos from config to reposlist
	c := r.Config.Repos
	if reposlist[0] == "" {
		// delete item from array
		reposlist = append(reposlist[:0], reposlist[0+1:]...)
		for k := range c {
			reposlist = append(reposlist,k)
		}
	}

	// Semaphore for concurrency
	thrnum := len(reposlist)
	sem := make(semaphore, thrnum)
	for _, k := range reposlist {
	//	go func(k string) error {

			_,ok := c[k]
			if ! ok {
				err := errors.New(fmt.Sprintf("Repo %#v is not defined in config\n", k))
				sem <- err
				return err
			}
			log.Printf("[%s, url: %s, path: %s, branch: %s]", k, c[k].URL, c[k].Path, c[k].Branch)

			// Createpath
			path,err := fileutils.CreatePath(c[k].Path)
			if err != nil {
				sem <-err
				return fmt.Errorf("Cannot create path %#v : %s", c[k].Path,err)
			}

			// Clone Repo
			cloneinfo := &gitutils.CloneInfo{
				Reponame: k,
				Path: path,
				URL: c[k].URL,
				Branch: c[k].Branch,
			}
			repo, err := cloneinfo.CloneRepo()
			if err != nil {
				sem <-err
				return fmt.Errorf("Cannot clone repo %#v : %s", k,err)
			}

			// Git Pull
			pullinfo := gitutils.PullInfo{
				Reponame: k,
				Repo: repo,
				Branch: c[k].Branch,
			}
			err = pullinfo.GitPull()
			if err != nil {
				sem <-err
				return fmt.Errorf("Cannot pull repo %#v : %s ", k,err)
			}

			// If branch option, branch and checkout selected repos
			if r.Branch != "" {
				branchinfo := &gitutils.BranchInfo{
					Reponame: k,
					Branchname: r.Branch,
					Msg: "RepoTsar Branching",
					Repo: *repo,
					Signature: r.Signature,
				}
				err = branchinfo.GitBranch()
				if err != nil {
					sem <-err
					return fmt.Errorf("Cannot branch repo %#v : %s", k,err)
				}
			}
			sem <-nil
			// return nil
	//	}(key)
	}

	// // Wait for threads to finish
	// for i := 0; i < thrnum; i++ {
	// 	err := <-sem
	// 	if err != nil {
	// 		return err
	// 	}
	// }
	return nil
}<|endoftext|>


<commit_before>/*
Copyright 2014 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scheduler

import (
	"fmt"
	"math/rand"
	"strings"
	"sync"

	"github.com/golang/glog"
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/util/sets"
	"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
	"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates"
)

type FailedPredicateMap map[string]sets.String

type FitError struct {
	Pod              *api.Pod
	FailedPredicates FailedPredicateMap
}

var ErrNoNodesAvailable = fmt.Errorf("no nodes available to schedule pods")

// implementation of the error interface
func (f *FitError) Error() string {
	var reason string
	// We iterate over all nodes for logging purposes, even though we only return one reason from one node
	for node, predicateList := range f.FailedPredicates {
		glog.Infof("Failed to find fit for pod %v on node %s: %s", f.Pod.Name, node, strings.Join(predicateList.List(), ","))
		if len(reason) == 0 {
			reason, _ = predicateList.PopAny()
		}
	}
	return fmt.Sprintf("Failed for reason %s and possibly others", reason)
}

type genericScheduler struct {
	predicates   map[string]algorithm.FitPredicate
	prioritizers []algorithm.PriorityConfig
	pods         algorithm.PodLister
	random       *rand.Rand
	randomLock   sync.Mutex
}

func (g *genericScheduler) Schedule(pod *api.Pod, nodeLister algorithm.NodeLister) (string, error) {
	nodes, err := nodeLister.List()
	if err != nil {
		return "", err
	}
	if len(nodes.Items) == 0 {
		return "", ErrNoNodesAvailable
	}

	filteredNodes, failedPredicateMap, err := findNodesThatFit(pod, g.pods, g.predicates, nodes)
	if err != nil {
		return "", err
	}

	priorityList, err := PrioritizeNodes(pod, g.pods, g.prioritizers, algorithm.FakeNodeLister(filteredNodes))
	if err != nil {
		return "", err
	}
	if len(priorityList) == 0 {
		return "", &FitError{
			Pod:              pod,
			FailedPredicates: failedPredicateMap,
		}
	}

	return g.selectHost(priorityList)
}

// This method takes a prioritized list of nodes, shuffles those with the highest scores to the
// front of the list, and then picks one randomly from the nodes that had the highest score.
func (g *genericScheduler) selectHost(priorityList algorithm.HostPriorityList) (string, error) {
	if len(priorityList) == 0 {
		return "", fmt.Errorf("empty priorityList")
	}
	max := priorityList[0].Score
	n := 0
	for i, hostEntry := range priorityList {
		if hostEntry.Score < max {
			continue
		}
		if hostEntry.Score > max {
			max = hostEntry.Score
			n = 0
		}
		priorityList[i], priorityList[n] = priorityList[n], priorityList[i]
		n++
	}
	if n == 1 {
		return priorityList[0].Host, nil
	}

	g.randomLock.Lock()
	defer g.randomLock.Unlock()
	return priorityList[g.random.Intn(n)].Host, nil
}

// Filters the nodes to find the ones that fit based on the given predicate functions
// Each node is passed through the predicate functions to determine if it is a fit
func findNodesThatFit(pod *api.Pod, podLister algorithm.PodLister, predicateFuncs map[string]algorithm.FitPredicate, nodes api.NodeList) (api.NodeList, FailedPredicateMap, error) {
	filtered := []api.Node{}
	machineToPods, err := predicates.MapPodsToMachines(podLister)
	failedPredicateMap := FailedPredicateMap{}
	if err != nil {
		return api.NodeList{}, FailedPredicateMap{}, err
	}
	for _, node := range nodes.Items {
		fits := true
		for name, predicate := range predicateFuncs {
			predicates.FailedResourceType = ""
			fit, err := predicate(pod, machineToPods[node.Name], node.Name)
			if err != nil {
				return api.NodeList{}, FailedPredicateMap{}, err
			}
			if !fit {
				fits = false
				if _, found := failedPredicateMap[node.Name]; !found {
					failedPredicateMap[node.Name] = sets.String{}
				}
				if predicates.FailedResourceType != "" {
					failedPredicateMap[node.Name].Insert(predicates.FailedResourceType)
					break
				}
				failedPredicateMap[node.Name].Insert(name)
				break
			}
		}
		if fits {
			filtered = append(filtered, node)
		}
	}
	return api.NodeList{Items: filtered}, failedPredicateMap, nil
}

// Prioritizes the nodes by running the individual priority functions sequentially.
// Each priority function is expected to set a score of 0-10
// 0 is the lowest priority score (least preferred node) and 10 is the highest
// Each priority function can also have its own weight
// The node scores returned by the priority function are multiplied by the weights to get weighted scores
// All scores are finally combined (added) to get the total weighted scores of all nodes
func PrioritizeNodes(pod *api.Pod, podLister algorithm.PodLister, priorityConfigs []algorithm.PriorityConfig, nodeLister algorithm.NodeLister) (algorithm.HostPriorityList, error) {
	result := algorithm.HostPriorityList{}

	// If no priority configs are provided, then the EqualPriority function is applied
	// This is required to generate the priority list in the required format
	if len(priorityConfigs) == 0 {
		return EqualPriority(pod, podLister, nodeLister)
	}

	combinedScores := map[string]int{}
	for _, priorityConfig := range priorityConfigs {
		weight := priorityConfig.Weight
		// skip the priority function if the weight is specified as 0
		if weight == 0 {
			continue
		}
		priorityFunc := priorityConfig.Function
		prioritizedList, err := priorityFunc(pod, podLister, nodeLister)
		if err != nil {
			return algorithm.HostPriorityList{}, err
		}
		for _, hostEntry := range prioritizedList {
			combinedScores[hostEntry.Host] += hostEntry.Score * weight
		}
	}
	for host, score := range combinedScores {
		glog.V(10).Infof("Host %s Score %d", host, score)
		result = append(result, algorithm.HostPriority{Host: host, Score: score})
	}
	return result, nil
}

// EqualPriority is a prioritizer function that gives an equal weight of one to all nodes
func EqualPriority(_ *api.Pod, podLister algorithm.PodLister, nodeLister algorithm.NodeLister) (algorithm.HostPriorityList, error) {
	nodes, err := nodeLister.List()
	if err != nil {
		glog.Errorf("Failed to list nodes: %v", err)
		return []algorithm.HostPriority{}, err
	}

	result := []algorithm.HostPriority{}
	for _, node := range nodes.Items {
		result = append(result, algorithm.HostPriority{
			Host:  node.Name,
			Score: 1,
		})
	}
	return result, nil
}

func NewGenericScheduler(predicates map[string]algorithm.FitPredicate, prioritizers []algorithm.PriorityConfig, pods algorithm.PodLister, random *rand.Rand) algorithm.ScheduleAlgorithm {
	return &genericScheduler{
		predicates:   predicates,
		prioritizers: prioritizers,
		pods:         pods,
		random:       random,
	}
}
<commit_msg>Revert "Avoid full sort when selecting host with highest priority."<commit_after>/*
Copyright 2014 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scheduler

import (
	"fmt"
	"math/rand"
	"sort"
	"strings"
	"sync"

	"github.com/golang/glog"
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/util/sets"
	"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
	"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates"
)

type FailedPredicateMap map[string]sets.String

type FitError struct {
	Pod              *api.Pod
	FailedPredicates FailedPredicateMap
}

var ErrNoNodesAvailable = fmt.Errorf("no nodes available to schedule pods")

// implementation of the error interface
func (f *FitError) Error() string {
	var reason string
	// We iterate over all nodes for logging purposes, even though we only return one reason from one node
	for node, predicateList := range f.FailedPredicates {
		glog.Infof("Failed to find fit for pod %v on node %s: %s", f.Pod.Name, node, strings.Join(predicateList.List(), ","))
		if len(reason) == 0 {
			reason, _ = predicateList.PopAny()
		}
	}
	return fmt.Sprintf("Failed for reason %s and possibly others", reason)
}

type genericScheduler struct {
	predicates   map[string]algorithm.FitPredicate
	prioritizers []algorithm.PriorityConfig
	pods         algorithm.PodLister
	random       *rand.Rand
	randomLock   sync.Mutex
}

func (g *genericScheduler) Schedule(pod *api.Pod, nodeLister algorithm.NodeLister) (string, error) {
	nodes, err := nodeLister.List()
	if err != nil {
		return "", err
	}
	if len(nodes.Items) == 0 {
		return "", ErrNoNodesAvailable
	}

	filteredNodes, failedPredicateMap, err := findNodesThatFit(pod, g.pods, g.predicates, nodes)
	if err != nil {
		return "", err
	}

	priorityList, err := PrioritizeNodes(pod, g.pods, g.prioritizers, algorithm.FakeNodeLister(filteredNodes))
	if err != nil {
		return "", err
	}
	if len(priorityList) == 0 {
		return "", &FitError{
			Pod:              pod,
			FailedPredicates: failedPredicateMap,
		}
	}

	return g.selectHost(priorityList)
}

// This method takes a prioritized list of nodes and sorts them in reverse order based on scores
// and then picks one randomly from the nodes that had the highest score
func (g *genericScheduler) selectHost(priorityList algorithm.HostPriorityList) (string, error) {
	if len(priorityList) == 0 {
		return "", fmt.Errorf("empty priorityList")
	}
	sort.Sort(sort.Reverse(priorityList))

	hosts := getBestHosts(priorityList)
	g.randomLock.Lock()
	defer g.randomLock.Unlock()

	ix := g.random.Int() % len(hosts)
	return hosts[ix], nil
}

// Filters the nodes to find the ones that fit based on the given predicate functions
// Each node is passed through the predicate functions to determine if it is a fit
func findNodesThatFit(pod *api.Pod, podLister algorithm.PodLister, predicateFuncs map[string]algorithm.FitPredicate, nodes api.NodeList) (api.NodeList, FailedPredicateMap, error) {
	filtered := []api.Node{}
	machineToPods, err := predicates.MapPodsToMachines(podLister)
	failedPredicateMap := FailedPredicateMap{}
	if err != nil {
		return api.NodeList{}, FailedPredicateMap{}, err
	}
	for _, node := range nodes.Items {
		fits := true
		for name, predicate := range predicateFuncs {
			predicates.FailedResourceType = ""
			fit, err := predicate(pod, machineToPods[node.Name], node.Name)
			if err != nil {
				return api.NodeList{}, FailedPredicateMap{}, err
			}
			if !fit {
				fits = false
				if _, found := failedPredicateMap[node.Name]; !found {
					failedPredicateMap[node.Name] = sets.String{}
				}
				if predicates.FailedResourceType != "" {
					failedPredicateMap[node.Name].Insert(predicates.FailedResourceType)
					break
				}
				failedPredicateMap[node.Name].Insert(name)
				break
			}
		}
		if fits {
			filtered = append(filtered, node)
		}
	}
	return api.NodeList{Items: filtered}, failedPredicateMap, nil
}

// Prioritizes the nodes by running the individual priority functions sequentially.
// Each priority function is expected to set a score of 0-10
// 0 is the lowest priority score (least preferred node) and 10 is the highest
// Each priority function can also have its own weight
// The node scores returned by the priority function are multiplied by the weights to get weighted scores
// All scores are finally combined (added) to get the total weighted scores of all nodes
func PrioritizeNodes(pod *api.Pod, podLister algorithm.PodLister, priorityConfigs []algorithm.PriorityConfig, nodeLister algorithm.NodeLister) (algorithm.HostPriorityList, error) {
	result := algorithm.HostPriorityList{}

	// If no priority configs are provided, then the EqualPriority function is applied
	// This is required to generate the priority list in the required format
	if len(priorityConfigs) == 0 {
		return EqualPriority(pod, podLister, nodeLister)
	}

	combinedScores := map[string]int{}
	for _, priorityConfig := range priorityConfigs {
		weight := priorityConfig.Weight
		// skip the priority function if the weight is specified as 0
		if weight == 0 {
			continue
		}
		priorityFunc := priorityConfig.Function
		prioritizedList, err := priorityFunc(pod, podLister, nodeLister)
		if err != nil {
			return algorithm.HostPriorityList{}, err
		}
		for _, hostEntry := range prioritizedList {
			combinedScores[hostEntry.Host] += hostEntry.Score * weight
		}
	}
	for host, score := range combinedScores {
		glog.V(10).Infof("Host %s Score %d", host, score)
		result = append(result, algorithm.HostPriority{Host: host, Score: score})
	}
	return result, nil
}

func getBestHosts(list algorithm.HostPriorityList) []string {
	result := []string{}
	for _, hostEntry := range list {
		if hostEntry.Score == list[0].Score {
			result = append(result, hostEntry.Host)
		} else {
			break
		}
	}
	return result
}

// EqualPriority is a prioritizer function that gives an equal weight of one to all nodes
func EqualPriority(_ *api.Pod, podLister algorithm.PodLister, nodeLister algorithm.NodeLister) (algorithm.HostPriorityList, error) {
	nodes, err := nodeLister.List()
	if err != nil {
		glog.Errorf("Failed to list nodes: %v", err)
		return []algorithm.HostPriority{}, err
	}

	result := []algorithm.HostPriority{}
	for _, node := range nodes.Items {
		result = append(result, algorithm.HostPriority{
			Host:  node.Name,
			Score: 1,
		})
	}
	return result, nil
}

func NewGenericScheduler(predicates map[string]algorithm.FitPredicate, prioritizers []algorithm.PriorityConfig, pods algorithm.PodLister, random *rand.Rand) algorithm.ScheduleAlgorithm {
	return &genericScheduler{
		predicates:   predicates,
		prioritizers: prioritizers,
		pods:         pods,
		random:       random,
	}
}
<|endoftext|>


<commit_before>package mysql

import (
	"github.com/funkygao/dbus/engine"
	"github.com/funkygao/dbus/pkg/model"
	conf "github.com/funkygao/jsconf"
	log "github.com/funkygao/log4go"
)

/*

        +-------+
        | mysql |
        +-------+
            | rows event
   +-------------------+
   |       |           |
  db1     db2         dbN
   |       |           |
   +-------------------+
           |
     MysqlbinlogFilter
           | dispatch
   +-------------------+
   |       |           |
 out1     out2       outM
*/
type MysqlbinlogFilter struct {
}

func (this *MysqlbinlogFilter) Init(config *conf.Conf) {}

func (this *MysqlbinlogFilter) Run(r engine.FilterRunner, h engine.PluginHelper) error {
	for {
		select {
		case pack, ok := <-r.InChan():
			if !ok {
				return nil
			}

			row, ok := pack.Payload.(*model.RowsEvent)
			if !ok {
				log.Warn("illegal payload: %+v", pack.Payload)
				continue
			}

			p := h.ClonePacket(pack)
			p.Ident = row.Schema
			r.Inject(p)

			pack.Recycle()
		}
	}
}
<commit_msg>BUG FIX: always handle the pack recycling<commit_after>package mysql

import (
	"github.com/funkygao/dbus/engine"
	"github.com/funkygao/dbus/pkg/model"
	conf "github.com/funkygao/jsconf"
	log "github.com/funkygao/log4go"
)

/*

        +-------+
        | mysql |
        +-------+
            | rows event
   +-------------------+
   |       |           |
  db1     db2         dbN
   |       |           |
   +-------------------+
           |
     MysqlbinlogFilter
           | dispatch
   +-------------------+
   |       |           |
 out1     out2       outM
*/
type MysqlbinlogFilter struct {
}

func (this *MysqlbinlogFilter) Init(config *conf.Conf) {}

func (this *MysqlbinlogFilter) Run(r engine.FilterRunner, h engine.PluginHelper) error {
	for {
		select {
		case pack, ok := <-r.InChan():
			if !ok {
				return nil
			}

			row, ok := pack.Payload.(*model.RowsEvent)
			if !ok {
				pack.Recycle()

				log.Warn("illegal payload: %+v", pack.Payload)
				continue
			}

			p := h.ClonePacket(pack)
			p.Ident = row.Schema
			r.Inject(p)

			pack.Recycle()
		}
	}
}
<|endoftext|>


<commit_before>// Package inbound implements dns server for inbound connection.
package inbound

import (
	"encoding/json"
	"io"
	"net"
	"net/http"
	"os"
	"strconv"
	"strings"
	"sync"

	log "github.com/Sirupsen/logrus"
	"github.com/miekg/dns"
	"github.com/shawn1m/overture/core/outbound"
)

type Server struct {
	bindAddress      string
	debugHttpAddress string
	dispatcher       outbound.Dispatcher
	rejectQType      []uint16
}

func NewServer(bindAddress string, debugHTTPAddress string, dispatcher outbound.Dispatcher, rejectQType []uint16) *Server {
	return &Server{
		bindAddress:      bindAddress,
		debugHttpAddress: debugHTTPAddress,
		dispatcher:       dispatcher,
		rejectQType:      rejectQType,
	}
}

func (s *Server) DumpCache(w http.ResponseWriter, req *http.Request) {
	if s.dispatcher.Cache == nil {
		io.WriteString(w, "error: cache not enabled")
		return
	}

	type answer struct {
		Name  string `json:"name"`
		TTL   int    `json:"ttl"`
		Type  string `json:"type"`
		Rdata string `json:"rdata"`
	}

	type response struct {
		Length   int                  `json:"length"`
		Capacity int                  `json:"capacity"`
		Body     map[string][]*answer `json:"body"`
	}

	query := req.URL.Query()
	nobody := true
	if t := query.Get("nobody"); strings.ToLower(t) == "false" {
		nobody = false
	}

	rs, l := s.dispatcher.Cache.Dump(nobody)
	body := make(map[string][]*answer)

	for k, es := range rs {
		answers := []*answer{}
		for _, e := range es {
			ts := strings.Split(e, "\t")
			ttl, _ := strconv.Atoi(ts[1])
			r := &answer{
				Name:  ts[0],
				TTL:   ttl,
				Type:  ts[3],
				Rdata: ts[4],
			}
			answers = append(answers, r)
		}
		body[strings.TrimSpace(k)] = answers
	}

	res := response{
		Body:     body,
		Length:   l,
		Capacity: s.dispatcher.Cache.Capacity(),
	}

	responseBytes, err := json.Marshal(&res)
	if err != nil {
		io.WriteString(w, err.Error())
		return
	}

	io.WriteString(w, string(responseBytes))
}

func (s *Server) Run() {

	mux := dns.NewServeMux()
	mux.Handle(".", s)

	wg := new(sync.WaitGroup)
	wg.Add(2)

	log.Info("Start overture on " + s.bindAddress)

	for _, p := range [2]string{"tcp", "udp"} {
		go func(p string) {
			err := dns.ListenAndServe(s.bindAddress, p, mux)
			if err != nil {
				log.Fatal("Listen "+p+" failed: ", err)
				os.Exit(1)
			}
		}(p)
	}

	if s.debugHttpAddress != "" {
		http.HandleFunc("/cache", s.DumpCache)
		wg.Add(1)
		go http.ListenAndServe(s.debugHttpAddress, nil)
	}

	wg.Wait()
}

func (s *Server) ServeDNS(w dns.ResponseWriter, q *dns.Msg) {
	inboundIP, _, _ := net.SplitHostPort(w.RemoteAddr().String())

	log.Debug("Question from " + inboundIP + ": " + q.Question[0].String())

	for _, qt := range s.rejectQType {
		if isQuestionType(q, qt) {
			return
		}
	}

	responseMessage := s.dispatcher.Exchange(q, inboundIP)

	if responseMessage == nil {
		return
	}

	err := w.WriteMsg(responseMessage)
	if err != nil {
		log.Warn("Write message fail:", responseMessage)
		return
	}
}

func isQuestionType(q *dns.Msg, qt uint16) bool { return q.Question[0].Qtype == qt }
<commit_msg>return error rather than do nothing when have no response (#138)<commit_after>// Package inbound implements dns server for inbound connection.
package inbound

import (
	"encoding/json"
	"io"
	"net"
	"net/http"
	"os"
	"strconv"
	"strings"
	"sync"

	log "github.com/Sirupsen/logrus"
	"github.com/miekg/dns"
	"github.com/shawn1m/overture/core/outbound"
)

type Server struct {
	bindAddress      string
	debugHttpAddress string
	dispatcher       outbound.Dispatcher
	rejectQType      []uint16
}

func NewServer(bindAddress string, debugHTTPAddress string, dispatcher outbound.Dispatcher, rejectQType []uint16) *Server {
	return &Server{
		bindAddress:      bindAddress,
		debugHttpAddress: debugHTTPAddress,
		dispatcher:       dispatcher,
		rejectQType:      rejectQType,
	}
}

func (s *Server) DumpCache(w http.ResponseWriter, req *http.Request) {
	if s.dispatcher.Cache == nil {
		io.WriteString(w, "error: cache not enabled")
		return
	}

	type answer struct {
		Name  string `json:"name"`
		TTL   int    `json:"ttl"`
		Type  string `json:"type"`
		Rdata string `json:"rdata"`
	}

	type response struct {
		Length   int                  `json:"length"`
		Capacity int                  `json:"capacity"`
		Body     map[string][]*answer `json:"body"`
	}

	query := req.URL.Query()
	nobody := true
	if t := query.Get("nobody"); strings.ToLower(t) == "false" {
		nobody = false
	}

	rs, l := s.dispatcher.Cache.Dump(nobody)
	body := make(map[string][]*answer)

	for k, es := range rs {
		answers := []*answer{}
		for _, e := range es {
			ts := strings.Split(e, "\t")
			ttl, _ := strconv.Atoi(ts[1])
			r := &answer{
				Name:  ts[0],
				TTL:   ttl,
				Type:  ts[3],
				Rdata: ts[4],
			}
			answers = append(answers, r)
		}
		body[strings.TrimSpace(k)] = answers
	}

	res := response{
		Body:     body,
		Length:   l,
		Capacity: s.dispatcher.Cache.Capacity(),
	}

	responseBytes, err := json.Marshal(&res)
	if err != nil {
		io.WriteString(w, err.Error())
		return
	}

	io.WriteString(w, string(responseBytes))
}

func (s *Server) Run() {

	mux := dns.NewServeMux()
	mux.Handle(".", s)

	wg := new(sync.WaitGroup)
	wg.Add(2)

	log.Info("Start overture on " + s.bindAddress)

	for _, p := range [2]string{"tcp", "udp"} {
		go func(p string) {
			err := dns.ListenAndServe(s.bindAddress, p, mux)
			if err != nil {
				log.Fatal("Listen "+p+" failed: ", err)
				os.Exit(1)
			}
		}(p)
	}

	if s.debugHttpAddress != "" {
		http.HandleFunc("/cache", s.DumpCache)
		wg.Add(1)
		go http.ListenAndServe(s.debugHttpAddress, nil)
	}

	wg.Wait()
}

func (s *Server) ServeDNS(w dns.ResponseWriter, q *dns.Msg) {
	inboundIP, _, _ := net.SplitHostPort(w.RemoteAddr().String())

	log.Debug("Question from " + inboundIP + ": " + q.Question[0].String())

	for _, qt := range s.rejectQType {
		if isQuestionType(q, qt) {
			return
		}
	}

	responseMessage := s.dispatcher.Exchange(q, inboundIP)

	if responseMessage == nil {
		dns.HandleFailed(w, q)
		return
	}

	err := w.WriteMsg(responseMessage)
	if err != nil {
		log.Warn("Write message fail:", responseMessage)
		return
	}
}

func isQuestionType(q *dns.Msg, qt uint16) bool { return q.Question[0].Qtype == qt }
<|endoftext|>


<commit_before>/*
Copyright 2014 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package priorities

import (
	"math"

	"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
	"github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/scheduler/algorithm"
	"github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/scheduler/algorithm/predicates"
	"github.com/golang/glog"
)

// the unused capacity is calculated on a scale of 0-10
// 0 being the lowest priority and 10 being the highest
func calculateScore(requested int64, capacity int64, node string) int {
	if capacity == 0 {
		return 0
	}
	if requested > capacity {
		glog.Infof("Combined requested resources %d from existing pods exceeds capacity %d on node %s",
			requested, capacity, node)
		return 0
	}
	return int(((capacity - requested) * 10) / capacity)
}

// For each of these resources, a pod that doesn't request the resource explicitly
// will be treated as having requested the amount indicated below, for the purpose
// of computing priority only. This ensures that when scheduling zero-limit pods, such
// pods will not all be scheduled to the machine with the smallest in-use limit,
// and that when scheduling regular pods, such pods will not see zero-limit pods as
// consuming no resources whatsoever. We chose these values to be similar to the
// resources that we give to cluster addon pods (#10653). But they are pretty arbitrary.
const defaultMilliCpuLimit int64 = 100             // 0.1 core
const defaultMemoryLimit int64 = 200 * 1024 * 1024 // 200 MB

// TODO: Consider setting default as a fixed fraction of machine capacity (take "capacity api.ResourceList"
// as an additional argument here) rather than using constants
func getNonzeroLimits(limits *api.ResourceList) (int64, int64) {
	var out_millicpu, out_memory int64
	// Override if un-set, but not if explicitly set to zero
	if (*limits.Cpu() == resource.Quantity{}) {
		out_millicpu = defaultMilliCpuLimit
	} else {
		out_millicpu = limits.Cpu().MilliValue()
	}
	// Override if un-set, but not if explicitly set to zero
	if (*limits.Memory() == resource.Quantity{}) {
		out_memory = defaultMemoryLimit
	} else {
		out_memory = limits.Memory().Value()
	}
	return out_millicpu, out_memory
}

// Calculate the resource occupancy on a node.  'node' has information about the resources on the node.
// 'pods' is a list of pods currently scheduled on the node.
func calculateResourceOccupancy(pod *api.Pod, node api.Node, pods []*api.Pod) algorithm.HostPriority {
	totalMilliCPU := int64(0)
	totalMemory := int64(0)
	capacityMilliCPU := node.Status.Capacity.Cpu().MilliValue()
	capacityMemory := node.Status.Capacity.Memory().Value()

	for _, existingPod := range pods {
		for _, container := range existingPod.Spec.Containers {
			cpu, memory := getNonzeroLimits(&container.Resources.Limits)
			totalMilliCPU += cpu
			totalMemory += memory
		}
	}
	// Add the resources requested by the current pod being scheduled.
	// This also helps differentiate between differently sized, but empty, minions.
	for _, container := range pod.Spec.Containers {
		cpu, memory := getNonzeroLimits(&container.Resources.Limits)
		totalMilliCPU += cpu
		totalMemory += memory
	}

	cpuScore := calculateScore(totalMilliCPU, capacityMilliCPU, node.Name)
	memoryScore := calculateScore(totalMemory, capacityMemory, node.Name)
	glog.V(10).Infof(
		"%v -> %v: Least Requested Priority, Absolute/Requested: (%d, %d) / (%d, %d) Score: (%d, %d)",
		pod.Name, node.Name,
		totalMilliCPU, totalMemory,
		capacityMilliCPU, capacityMemory,
		cpuScore, memoryScore,
	)

	return algorithm.HostPriority{
		Host:  node.Name,
		Score: int((cpuScore + memoryScore) / 2),
	}
}

// LeastRequestedPriority is a priority function that favors nodes with fewer requested resources.
// It calculates the percentage of memory and CPU requested by pods scheduled on the node, and prioritizes
// based on the minimum of the average of the fraction of requested to capacity.
// Details: (Sum(requested cpu) / Capacity + Sum(requested memory) / Capacity) * 50
func LeastRequestedPriority(pod *api.Pod, podLister algorithm.PodLister, minionLister algorithm.MinionLister) (algorithm.HostPriorityList, error) {
	nodes, err := minionLister.List()
	if err != nil {
		return algorithm.HostPriorityList{}, err
	}
	podsToMachines, err := predicates.MapPodsToMachines(podLister)

	list := algorithm.HostPriorityList{}
	for _, node := range nodes.Items {
		list = append(list, calculateResourceOccupancy(pod, node, podsToMachines[node.Name]))
	}
	return list, nil
}

type NodeLabelPrioritizer struct {
	label    string
	presence bool
}

func NewNodeLabelPriority(label string, presence bool) algorithm.PriorityFunction {
	labelPrioritizer := &NodeLabelPrioritizer{
		label:    label,
		presence: presence,
	}
	return labelPrioritizer.CalculateNodeLabelPriority
}

// CalculateNodeLabelPriority checks whether a particular label exists on a minion or not, regardless of its value.
// If presence is true, prioritizes minions that have the specified label, regardless of value.
// If presence is false, prioritizes minions that do not have the specified label.
func (n *NodeLabelPrioritizer) CalculateNodeLabelPriority(pod *api.Pod, podLister algorithm.PodLister, minionLister algorithm.MinionLister) (algorithm.HostPriorityList, error) {
	var score int
	minions, err := minionLister.List()
	if err != nil {
		return nil, err
	}

	labeledMinions := map[string]bool{}
	for _, minion := range minions.Items {
		exists := labels.Set(minion.Labels).Has(n.label)
		labeledMinions[minion.Name] = (exists && n.presence) || (!exists && !n.presence)
	}

	result := []algorithm.HostPriority{}
	//score int - scale of 0-10
	// 0 being the lowest priority and 10 being the highest
	for minionName, success := range labeledMinions {
		if success {
			score = 10
		} else {
			score = 0
		}
		result = append(result, algorithm.HostPriority{Host: minionName, Score: score})
	}
	return result, nil
}

// BalancedResourceAllocation favors nodes with balanced resource usage rate.
// BalancedResourceAllocation should **NOT** be used alone, and **MUST** be used together with LeastRequestedPriority.
// It calculates the difference between the cpu and memory fracion of capacity, and prioritizes the host based on how
// close the two metrics are to each other.
// Detail: score = 10 - abs(cpuFraction-memoryFraction)*10. The algorithm is partly inspired by:
// "Wei Huang et al. An Energy Efficient Virtual Machine Placement Algorithm with Balanced Resource Utilization"
func BalancedResourceAllocation(pod *api.Pod, podLister algorithm.PodLister, minionLister algorithm.MinionLister) (algorithm.HostPriorityList, error) {
	nodes, err := minionLister.List()
	if err != nil {
		return algorithm.HostPriorityList{}, err
	}
	podsToMachines, err := predicates.MapPodsToMachines(podLister)

	list := algorithm.HostPriorityList{}
	for _, node := range nodes.Items {
		list = append(list, calculateBalancedResourceAllocation(pod, node, podsToMachines[node.Name]))
	}
	return list, nil
}

func calculateBalancedResourceAllocation(pod *api.Pod, node api.Node, pods []*api.Pod) algorithm.HostPriority {
	totalMilliCPU := int64(0)
	totalMemory := int64(0)
	score := int(0)
	for _, existingPod := range pods {
		for _, container := range existingPod.Spec.Containers {
			cpu, memory := getNonzeroLimits(&container.Resources.Limits)
			totalMilliCPU += cpu
			totalMemory += memory
		}
	}
	// Add the resources requested by the current pod being scheduled.
	// This also helps differentiate between differently sized, but empty, minions.
	for _, container := range pod.Spec.Containers {
		cpu, memory := getNonzeroLimits(&container.Resources.Limits)
		totalMilliCPU += cpu
		totalMemory += memory
	}

	capacityMilliCPU := node.Status.Capacity.Cpu().MilliValue()
	capacityMemory := node.Status.Capacity.Memory().Value()

	cpuFraction := fractionOfCapacity(totalMilliCPU, capacityMilliCPU, node.Name)
	memoryFraction := fractionOfCapacity(totalMemory, capacityMemory, node.Name)
	if cpuFraction >= 1 || memoryFraction >= 1 {
		// if requested >= capacity, the corresponding host should never be preferrred.
		score = 0
	} else {
		// Upper and lower boundary of difference between cpuFraction and memoryFraction are -1 and 1
		// respectively. Multilying the absolute value of the difference by 10 scales the value to
		// 0-10 with 0 representing well balanced allocation and 10 poorly balanced. Subtracting it from
		// 10 leads to the score which also scales from 0 to 10 while 10 representing well balanced.
		diff := math.Abs(cpuFraction - memoryFraction)
		score = int(10 - diff*10)
	}
	glog.V(10).Infof(
		"%v -> %v: Balanced Resource Allocation, Absolute/Requested: (%d, %d) / (%d, %d) Score: (%d)",
		pod.Name, node.Name,
		totalMilliCPU, totalMemory,
		capacityMilliCPU, capacityMemory,
		score,
	)

	return algorithm.HostPriority{
		Host:  node.Name,
		Score: score,
	}
}

func fractionOfCapacity(requested, capacity int64, node string) float64 {
	if capacity == 0 {
		return 1
	}
	return float64(requested) / float64(capacity)
}
<commit_msg>Remove unused argument from fractionOfCapacity()<commit_after>/*
Copyright 2014 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package priorities

import (
	"math"

	"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
	"github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/scheduler/algorithm"
	"github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/scheduler/algorithm/predicates"
	"github.com/golang/glog"
)

// the unused capacity is calculated on a scale of 0-10
// 0 being the lowest priority and 10 being the highest
func calculateScore(requested int64, capacity int64, node string) int {
	if capacity == 0 {
		return 0
	}
	if requested > capacity {
		glog.Infof("Combined requested resources %d from existing pods exceeds capacity %d on node %s",
			requested, capacity, node)
		return 0
	}
	return int(((capacity - requested) * 10) / capacity)
}

// For each of these resources, a pod that doesn't request the resource explicitly
// will be treated as having requested the amount indicated below, for the purpose
// of computing priority only. This ensures that when scheduling zero-limit pods, such
// pods will not all be scheduled to the machine with the smallest in-use limit,
// and that when scheduling regular pods, such pods will not see zero-limit pods as
// consuming no resources whatsoever. We chose these values to be similar to the
// resources that we give to cluster addon pods (#10653). But they are pretty arbitrary.
const defaultMilliCpuLimit int64 = 100             // 0.1 core
const defaultMemoryLimit int64 = 200 * 1024 * 1024 // 200 MB

// TODO: Consider setting default as a fixed fraction of machine capacity (take "capacity api.ResourceList"
// as an additional argument here) rather than using constants
func getNonzeroLimits(limits *api.ResourceList) (int64, int64) {
	var out_millicpu, out_memory int64
	// Override if un-set, but not if explicitly set to zero
	if (*limits.Cpu() == resource.Quantity{}) {
		out_millicpu = defaultMilliCpuLimit
	} else {
		out_millicpu = limits.Cpu().MilliValue()
	}
	// Override if un-set, but not if explicitly set to zero
	if (*limits.Memory() == resource.Quantity{}) {
		out_memory = defaultMemoryLimit
	} else {
		out_memory = limits.Memory().Value()
	}
	return out_millicpu, out_memory
}

// Calculate the resource occupancy on a node.  'node' has information about the resources on the node.
// 'pods' is a list of pods currently scheduled on the node.
func calculateResourceOccupancy(pod *api.Pod, node api.Node, pods []*api.Pod) algorithm.HostPriority {
	totalMilliCPU := int64(0)
	totalMemory := int64(0)
	capacityMilliCPU := node.Status.Capacity.Cpu().MilliValue()
	capacityMemory := node.Status.Capacity.Memory().Value()

	for _, existingPod := range pods {
		for _, container := range existingPod.Spec.Containers {
			cpu, memory := getNonzeroLimits(&container.Resources.Limits)
			totalMilliCPU += cpu
			totalMemory += memory
		}
	}
	// Add the resources requested by the current pod being scheduled.
	// This also helps differentiate between differently sized, but empty, minions.
	for _, container := range pod.Spec.Containers {
		cpu, memory := getNonzeroLimits(&container.Resources.Limits)
		totalMilliCPU += cpu
		totalMemory += memory
	}

	cpuScore := calculateScore(totalMilliCPU, capacityMilliCPU, node.Name)
	memoryScore := calculateScore(totalMemory, capacityMemory, node.Name)
	glog.V(10).Infof(
		"%v -> %v: Least Requested Priority, Absolute/Requested: (%d, %d) / (%d, %d) Score: (%d, %d)",
		pod.Name, node.Name,
		totalMilliCPU, totalMemory,
		capacityMilliCPU, capacityMemory,
		cpuScore, memoryScore,
	)

	return algorithm.HostPriority{
		Host:  node.Name,
		Score: int((cpuScore + memoryScore) / 2),
	}
}

// LeastRequestedPriority is a priority function that favors nodes with fewer requested resources.
// It calculates the percentage of memory and CPU requested by pods scheduled on the node, and prioritizes
// based on the minimum of the average of the fraction of requested to capacity.
// Details: (Sum(requested cpu) / Capacity + Sum(requested memory) / Capacity) * 50
func LeastRequestedPriority(pod *api.Pod, podLister algorithm.PodLister, minionLister algorithm.MinionLister) (algorithm.HostPriorityList, error) {
	nodes, err := minionLister.List()
	if err != nil {
		return algorithm.HostPriorityList{}, err
	}
	podsToMachines, err := predicates.MapPodsToMachines(podLister)

	list := algorithm.HostPriorityList{}
	for _, node := range nodes.Items {
		list = append(list, calculateResourceOccupancy(pod, node, podsToMachines[node.Name]))
	}
	return list, nil
}

type NodeLabelPrioritizer struct {
	label    string
	presence bool
}

func NewNodeLabelPriority(label string, presence bool) algorithm.PriorityFunction {
	labelPrioritizer := &NodeLabelPrioritizer{
		label:    label,
		presence: presence,
	}
	return labelPrioritizer.CalculateNodeLabelPriority
}

// CalculateNodeLabelPriority checks whether a particular label exists on a minion or not, regardless of its value.
// If presence is true, prioritizes minions that have the specified label, regardless of value.
// If presence is false, prioritizes minions that do not have the specified label.
func (n *NodeLabelPrioritizer) CalculateNodeLabelPriority(pod *api.Pod, podLister algorithm.PodLister, minionLister algorithm.MinionLister) (algorithm.HostPriorityList, error) {
	var score int
	minions, err := minionLister.List()
	if err != nil {
		return nil, err
	}

	labeledMinions := map[string]bool{}
	for _, minion := range minions.Items {
		exists := labels.Set(minion.Labels).Has(n.label)
		labeledMinions[minion.Name] = (exists && n.presence) || (!exists && !n.presence)
	}

	result := []algorithm.HostPriority{}
	//score int - scale of 0-10
	// 0 being the lowest priority and 10 being the highest
	for minionName, success := range labeledMinions {
		if success {
			score = 10
		} else {
			score = 0
		}
		result = append(result, algorithm.HostPriority{Host: minionName, Score: score})
	}
	return result, nil
}

// BalancedResourceAllocation favors nodes with balanced resource usage rate.
// BalancedResourceAllocation should **NOT** be used alone, and **MUST** be used together with LeastRequestedPriority.
// It calculates the difference between the cpu and memory fracion of capacity, and prioritizes the host based on how
// close the two metrics are to each other.
// Detail: score = 10 - abs(cpuFraction-memoryFraction)*10. The algorithm is partly inspired by:
// "Wei Huang et al. An Energy Efficient Virtual Machine Placement Algorithm with Balanced Resource Utilization"
func BalancedResourceAllocation(pod *api.Pod, podLister algorithm.PodLister, minionLister algorithm.MinionLister) (algorithm.HostPriorityList, error) {
	nodes, err := minionLister.List()
	if err != nil {
		return algorithm.HostPriorityList{}, err
	}
	podsToMachines, err := predicates.MapPodsToMachines(podLister)

	list := algorithm.HostPriorityList{}
	for _, node := range nodes.Items {
		list = append(list, calculateBalancedResourceAllocation(pod, node, podsToMachines[node.Name]))
	}
	return list, nil
}

func calculateBalancedResourceAllocation(pod *api.Pod, node api.Node, pods []*api.Pod) algorithm.HostPriority {
	totalMilliCPU := int64(0)
	totalMemory := int64(0)
	score := int(0)
	for _, existingPod := range pods {
		for _, container := range existingPod.Spec.Containers {
			cpu, memory := getNonzeroLimits(&container.Resources.Limits)
			totalMilliCPU += cpu
			totalMemory += memory
		}
	}
	// Add the resources requested by the current pod being scheduled.
	// This also helps differentiate between differently sized, but empty, minions.
	for _, container := range pod.Spec.Containers {
		cpu, memory := getNonzeroLimits(&container.Resources.Limits)
		totalMilliCPU += cpu
		totalMemory += memory
	}

	capacityMilliCPU := node.Status.Capacity.Cpu().MilliValue()
	capacityMemory := node.Status.Capacity.Memory().Value()

	cpuFraction := fractionOfCapacity(totalMilliCPU, capacityMilliCPU)
	memoryFraction := fractionOfCapacity(totalMemory, capacityMemory)
	if cpuFraction >= 1 || memoryFraction >= 1 {
		// if requested >= capacity, the corresponding host should never be preferrred.
		score = 0
	} else {
		// Upper and lower boundary of difference between cpuFraction and memoryFraction are -1 and 1
		// respectively. Multilying the absolute value of the difference by 10 scales the value to
		// 0-10 with 0 representing well balanced allocation and 10 poorly balanced. Subtracting it from
		// 10 leads to the score which also scales from 0 to 10 while 10 representing well balanced.
		diff := math.Abs(cpuFraction - memoryFraction)
		score = int(10 - diff*10)
	}
	glog.V(10).Infof(
		"%v -> %v: Balanced Resource Allocation, Absolute/Requested: (%d, %d) / (%d, %d) Score: (%d)",
		pod.Name, node.Name,
		totalMilliCPU, totalMemory,
		capacityMilliCPU, capacityMemory,
		score,
	)

	return algorithm.HostPriority{
		Host:  node.Name,
		Score: score,
	}
}

func fractionOfCapacity(requested, capacity int64) float64 {
	if capacity == 0 {
		return 1
	}
	return float64(requested) / float64(capacity)
}
<|endoftext|>