예제 #1
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
}
예제 #2
0
// NewLoadTracker creates a new LoadTracker.
func NewLoadTracker(
	servingID string, maxInstanceLoad float64, minInstances int,
	sessionID string) *LoadTracker {
	sessionKAB := scale.NewSessionKeepAliveBuffer(sessionID, TTLFlag.Get()/2)
	tracker := &LoadTracker{
		servingID:       servingID,
		rateT:           newRateTracker(),
		rpcNanosT:       newValueTracker(),
		maxInstanceLoad: maxInstanceLoad,
		minInstances:    minInstances,
		sessionKAB:      sessionKAB,
		numInstances:    -1,
	}
	return tracker
}
예제 #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
}