Esempio n. 1
0
// NewFinder returns a new Finder instance.
func NewFinder(docker *dockerapi.Client, as *aerospike.Client) *Finder {
	return &Finder{
		docker: docker,
		as:     as,
		logger: leverutil.GetLogger(PackageName, "Finder"),
	}
}
Esempio n. 2
0
// NewLeverResource returns a new instance of LeverResource.
func NewLeverResource(
	leverEnv, leverService, instanceID, leverResource, levResResourceID,
	levResSessionID, instanceAddr string,
	conns *scale.GRPCPool, onCloseFun OnCloseFun) (*LeverResource, error) {
	expiryDur := ResourceExpiryTimeFlag.Get()
	logger := leverutil.GetLogger(PackageName, "leverresource").WithFields(
		"leverEnv", leverEnv,
		"leverService", leverService,
		"leverInstanceID", instanceID,
		"leverResource", leverResource,
	)
	resource := &LeverResource{
		leverEnv:      leverEnv,
		leverService:  leverService,
		leverResource: leverResource,
		levResResource: scale.ExistingResource(
			leverutil.ServiceFlag.Get(), levResResourceID, levResSessionID),
		instanceAddr: instanceAddr,
		conns:        conns,
		sessionKAB: scale.NewSessionKeepAliveBuffer(
			levResSessionID, expiryDur),
		expiryDur:  expiryDur,
		onCloseFun: onCloseFun,
		logger:     logger,
	}
	resource.expiryTimer = time.AfterFunc(expiryDur, resource.onExpired)
	go resource.monitorResourceLock(levResResourceID)
	resource.construct()
	return resource, nil
}
Esempio n. 3
0
// NewLeverInstance returns a new instance of LeverInstance.
func NewLeverInstance(
	info *InstanceInfo, instanceAddr string, proxyInAddr string,
	conns *scale.GRPCPool, docker *dockerapi.Client,
	onCloseFun OnCloseFun) *LeverInstance {
	expiryDur := InstanceExpiryTimeFlag.Get()
	logger := leverutil.GetLogger(PackageName, "leverinstance").WithFields(
		"leverEnv", info.Environment,
		"leverService", info.Service,
		"leverInstanceID", info.InstanceID,
	)
	instance := &LeverInstance{
		leverEnv:       info.Environment,
		instanceID:     info.InstanceID,
		leverService:   info.Service,
		containerID:    info.ContainerID,
		servingID:      info.ServingID,
		instanceAddr:   instanceAddr,
		conns:          conns,
		docker:         docker,
		expiryDur:      expiryDur,
		onCloseFun:     onCloseFun,
		logger:         logger,
		leverResources: make(map[string]*LeverResource),
	}
	instance.expiryTimer = time.AfterFunc(expiryDur, instance.onExpired)
	instance.registerAsService(proxyInAddr)
	go instance.monitorContainer()
	if info.LevInstResourceID != "" {
		// This instance has a resource + session associated with it. Manage
		// its lifecycle.
		instance.levInstResource = scale.ExistingResource(
			leverutil.ServiceFlag.Get(), info.LevInstResourceID,
			info.LevInstSessionID)
		instance.sessionKAB = scale.NewSessionKeepAliveBuffer(
			info.LevInstSessionID, expiryDur)
		go instance.monitorInstanceLock(info.LevInstResourceID)
	}
	return instance
}
Esempio n. 4
0
// NewManager returns a new Manager.
func NewManager(
	grpcServer *grpc.Server, grpcPool *scale.GRPCPool,
	docker *dockerapi.Client, grpcAddr string, proxyInAddr string) (
	manager *Manager, err error) {
	manager = &Manager{
		grpcPool:     grpcPool,
		docker:       docker,
		proxyInAddr:  proxyInAddr,
		environments: make(map[string]*envEntry),
		servingIDs:   make(map[string]map[string]struct{}),
		logger:       leverutil.GetLogger(PackageName, "Manager"),
	}
	RegisterManagerServer(grpcServer, manager)
	managerInstanceID := InstanceIDFlag.Get()
	serviceTTL := 30 * time.Second
	err = scale.RegisterServiceLocal(
		ManagerService, managerInstanceID, grpcAddr, serviceTTL)
	if err != nil {
		return nil, err
	}
	manager.serviceSKA = scale.NewServiceSelfKeepAlive(
		managerInstanceID, serviceTTL/2)
	err = manager.registerManagerResource(grpcAddr)
	if err != nil {
		return nil, err
	}
	// TODO: This is a workaround for docker's bug where a network connect
	//       causes active connections to be closed. This prevents the admin
	//       env's network from being created at request time, causing that
	//       request to fail (and client to retry).
	adminEntry, err := manager.getEnvironment(core.AdminEnvFlag.Get())
	if err != nil {
		manager.logger.WithFields("err", err).Warning(
			"Admin env workaround failed")
	}
	adminEntry.envLock.Unlock()
	return manager, nil
}
Esempio n. 5
0
// NewLeverProxy creates a new instance of LeverProxy.
func NewLeverProxy(
	manager *hostman.Manager, finder *Finder, ownIPv4 string,
	grpcPool *scale.GRPCPool) (
	*LeverProxy, error) {
	client, err := http2stream.NewHTTP2Client(
		ConnectionConnectTimeoutFlag.Get(), ConnectionExpiryFlag.Get())
	if err != nil {
		return nil, err
	}
	proxy := &LeverProxy{
		outServer:    http2stream.NewHTTP2Server(),
		inServer:     http2stream.NewHTTP2Server(),
		extServer:    http2stream.NewHTTP2Server(),
		client:       client,
		manager:      manager,
		finder:       finder,
		ownIPv4:      ownIPv4,
		grpcPool:     grpcPool,
		outListeners: make(map[string]net.Listener),
		logger:       leverutil.GetLogger(PackageName, "LeverProxy"),
	}
	proxy.inLogger = proxy.logger.WithFields("listener", "in")
	proxy.outLogger = proxy.logger.WithFields("listener", "out")
	proxy.extLogger = proxy.logger.WithFields("listener", "ext")
	if !DisableProxyExtFlag.Get() {
		err = proxy.serveExt()
		if err != nil {
			return nil, err
		}
	}
	if !DisableProxyInOutFlag.Get() {
		err = proxy.serveIn()
		if err != nil {
			return nil, err
		}
	}
	return proxy, nil
}
Esempio n. 6
0
	"github.com/leveros/leveros/core"
	"github.com/leveros/leveros/host"
	"github.com/leveros/leveros/leverutil"
)

// PackageName is the name of this package.
const PackageName = "apiserver"

var (
	// ListenPortFlag is the port the API server should listen on.
	ListenPortFlag = config.DeclareString(PackageName, "listenPort", "3503")
	// DisableAPIServerFlag disables the creation of an HTTP API server.
	DisableAPIServerFlag = config.DeclareBool(PackageName, "disableAPIServer")
)

var logger = leverutil.GetLogger(PackageName, "Server")

const maxNonChanRequestSize = 512 * 1024 // 512 KiB

var bufferedReaderPool = &sync.Pool{
	New: func() interface{} {
		return bufio.NewReaderSize(nil, maxNonChanRequestSize)
	},
}

var bufferPool = &sync.Pool{
	New: func() interface{} {
		return make([]byte, maxNonChanRequestSize)
	},
}
Esempio n. 7
0
	DockerLocalFlag = config.DeclareString(
		PackageName, "dockerLocal", "unix:///var/run/docker.sock")

	// LeverCodeHostDirFlag is the location on the host (outside docker) of the
	// mounted customer code directory, containing all code from all
	// environments and services.
	LeverCodeHostDirFlag = config.DeclareString(
		PackageName, "leverCodeHostDir", "/tmp/leveros/custcodetree")

	// DisableRemoveContainerFlag causes Lever containers to not be
	// removed (on errors, on exit etc).
	DisableRemoveContainerFlag = config.DeclareBool(
		PackageName, "disableRemoveContainer")
)

var logger = leverutil.GetLogger(PackageName, "dockerutil")

var (
	ownContainerIDOnce sync.Once
	ownContainerID     = ""
)

// HostCodeDirPath returns the path on the docker host that the code for a given
// service version is found at.
func HostCodeDirPath(environment, service string, codeVersion int64) string {
	return filepath.Join(
		LeverCodeHostDirFlag.Get(), environment, service,
		strconv.Itoa(int(codeVersion)))
}

// CodeDirPath returns the path within the docker container that the code
Esempio n. 8
0
	"github.com/leveros/leveros/scale"
	"github.com/leveros/leveros/store"
	"google.golang.org/grpc"
)

// PackageName is the name of this package.
const PackageName = cmd.PackageName + ".leveroshost"

var (
	// Version is the version of Lever OS. This variable is set at build time.
	Version string
	// GitHash is the git hash of Lever OS. This variable is set at build time.
	GitHash string
)

var logger = leverutil.GetLogger(PackageName, "main")

var (
	// InternalGRPCListenPortFlag is the port to listen to for internal GRPC
	// connections.
	InternalGRPCListenPortFlag = config.DeclareString(
		PackageName, "internalGRPCListenPort", "3501")
	// ExternalIPFlag is the host's external IP address.
	ExternalIPFlag = config.DeclareString(
		PackageName, "externalIP", "127.0.0.1")
)

func main() {
	config.Initialize()
	leverutil.UpdateLoggingSettings()
Esempio n. 9
0
package systemtest

import (
	"github.com/leveros/leveros/leverutil"
)

// PackageName is the name of this package.
const PackageName = "systemtest"

var logger = leverutil.GetLogger(PackageName, "test")
Esempio n. 10
0
package http2stream

import (
	"net"
	"sync"

	"github.com/leveros/leveros/leverutil"
)

var (
	serverLogger = leverutil.GetLogger(PackageName, "server")
)

// StreamHandler is a function that handles incoming streams.
type StreamHandler func(stream *HTTP2Stream)

// HTTP2Server represents a HTTP2 server which listens for connections.
type HTTP2Server struct {
	lock      sync.Mutex
	listeners map[net.Listener]bool
	conns     map[*http2Connection]bool
}

// NewHTTP2Server returns a new instance of HTTP2Server.
func NewHTTP2Server() *HTTP2Server {
	return &HTTP2Server{
		listeners: make(map[net.Listener]bool),
		conns:     make(map[*http2Connection]bool),
	}
}
Esempio n. 11
0
	// http2MaxFrameLen specifies the max length of a HTTP2 frame.
	http2MaxFrameLen = 16384 // 16KiB frame
	// http2IOBufSize specifies the buffer size for sending frames.
	http2IOBufSize = 32 * 1024

	// The default value of flow control window size in HTTP2 spec.
	defaultWindowSize = 65535 // 64KiB
	// The initial window size for flow control.
	initialWindowSize     = defaultWindowSize      // for an RPC
	initialConnWindowSize = defaultWindowSize * 16 // for a connection

	contentType = "application/grpc"
)

var (
	connectionLogger = leverutil.GetLogger(PackageName, "connection")
)

type connectionState uint8

const (
	stateReachable = iota
	stateClosing
)

var (
	clientPreface = []byte(http2.ClientPreface)
)

type http2Connection struct {
	isServer bool
Esempio n. 12
0
package store

import (
	"github.com/leveros/leveros/leverutil"
)

// PackageName is the name of this package.
const PackageName = "store"

var logger = leverutil.GetLogger(PackageName, "store")
Esempio n. 13
0
package http2stream

import (
	"net"
	"time"

	"github.com/leveros/leveros/leverutil"
)

var clientLogger = leverutil.GetLogger(PackageName, "client")

// HTTP2Client represents a collection of client HTTP2 connections.
// The structure manages a connection pool internally.
type HTTP2Client struct {
	conns *leverutil.Cache
}

// NewHTTP2Client creates a new HTTP2Client.
func NewHTTP2Client(
	connectionConnectTimeout time.Duration,
	connectionExpiry time.Duration) (*HTTP2Client, error) {
	client := new(HTTP2Client)
	client.conns = leverutil.NewCache(
		connectionExpiry,
		func(addr string) (interface{}, error) {
			clientLogger.WithFields("addr", addr).Debug("New connection")
			netConn, err := net.DialTimeout(
				"tcp", addr, connectionConnectTimeout)
			if err != nil {
				return nil, err
			}
Esempio n. 14
0
package fleettracker

import (
	"math"
	"sync"
	"sync/atomic"
	"time"

	consulapi "github.com/hashicorp/consul/api"
	"github.com/leveros/leveros/config"
	"github.com/leveros/leveros/leverutil"
	"github.com/leveros/leveros/scale"
)

var trackerLogger = leverutil.GetLogger(PackageName, "loadtracker")

var (
	// TickNumEventsFlag is the number of events that the load tracker aims to
	// put in a tick length. Yet, due to {Min,Max}TickPeriodFlag, the number of
	// events may vary.
	TickNumEventsFlag = config.DeclareInt(PackageName, "tickNumEvents", 16)
	// MinTickPeriodFlag is the minimum time between load tracker ticks.
	MinTickPeriodFlag = config.DeclareDuration(
		PackageName, "minTickPeriod", 250*time.Millisecond)
	// MaxTickPeriodFlag is the maximum time between load tracker
	// ticks. Note: The period may still be longer than this if no events at all
	// come up for a while.
	MaxTickPeriodFlag = config.DeclareDuration(
		PackageName, "maxTickPeriod", 15*time.Second)
	// StatsExpPeriodFlag is the amount of time stats are taken into
	// consideration. This value is used for the computation of alpha in EWMA.
Esempio n. 15
0
	dockerapi "github.com/fsouza/go-dockerclient"
	consulapi "github.com/hashicorp/consul/api"
	"github.com/leveros/leveros/config"
	"github.com/leveros/leveros/core"
	"github.com/leveros/leveros/dockerutil"
	"github.com/leveros/leveros/hostman"
	"github.com/leveros/leveros/leverutil"
	"github.com/leveros/leveros/scale"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
)

// PackageName is the name of this package.
const PackageName = "fleettracker"

var logger = leverutil.GetLogger(PackageName, "fleettracker")

// FleetTrackerService is the name of the fleettracker internal service.
const FleetTrackerService = "fleettracker"

var (
	// InstanceIDFlag is the instance ID of the fleettracker. Note: This is a
	// different instance ID than leverutil.InstanceIDFlag because they serve
	// different things.
	InstanceIDFlag = config.DeclareString(
		PackageName, "instanceID", leverutil.RandomID())
	// TTLFlag is the time after a service expires in fleettracker. If no RPC
	// event comes up during this span of time for a Lever service, fleettracker
	// stops keeping track of its stats (it is assumed all its instances
	// have expired anyway).
	TTLFlag = config.DeclareDuration(PackageName, "ttl", 20*time.Minute)
Esempio n. 16
0
import (
	"encoding/json"
	"fmt"
	"net/url"
	"strconv"
	"strings"
	"time"

	consulapi "github.com/hashicorp/consul/api"
	"github.com/leveros/leveros/config"
	"github.com/leveros/leveros/leverutil"
	"github.com/miekg/dns"
)

var (
	discoveryLogger = leverutil.GetLogger(PackageName, "discovery")
)

var (
	// DNSServerFlag is the DNS server address in format <ip>:<port>. Used for
	// service discovery.
	DNSServerFlag = config.DeclareString(
		PackageName, "dnsServer", "leverosconsul:8600")
)

// ErrServiceNotFound is an error that is returned when a service with provided
// name is not found.
var ErrServiceNotFound = fmt.Errorf("Service not found")

const (
	consulResourcePrefix       = "res/"