Пример #1
0
func (finder *Finder) getResource(
	env, service, servingID, resource string, version int64) (
	levResTarget *LevResTarget, levResResource *scale.Resource,
	isNewInstance bool, err error) {
	levResResourceID := makeLevResResourceID(env, service, resource, version)

	// See if resource already being served somewhere.
	levResResource, err = scale.DereferenceResource(
		leverutil.ServiceFlag.Get(), levResResourceID)
	if err != nil {
		finder.logger.WithFields("err", err).Error(
			"Could not dereference Lever resource")
		return nil, nil, false, err
	}
	if levResResource == nil {
		// Resource not allocated. Register new one.
		return finder.newResource(env, service, servingID, resource, version)
	}

	levResTarget = new(LevResTarget)
	err = json.Unmarshal([]byte(levResResource.GetTarget()), levResTarget)
	if err != nil {
		finder.logger.WithFields("err", err).Panic("Failed to decode json")
	}
	finder.logger.WithFields(
		"leverEnv", env,
		"leverService", service,
		"leverResource", resource,
		"leverInstanceID", levResTarget.InstanceID,
	).Debug("Reusing resource")
	return levResTarget, levResResource, false, nil
}
Пример #2
0
// InitializeInstance initializes the infrastructure necessary for instance to
// start serving.
func InitializeInstance(
	grpcPool *scale.GRPCPool, info *InstanceInfo, node string) error {
	res, err := scale.DereferenceResource(
		ManagerService, "swarm://"+node)
	if err != nil {
		return err
	}
	conn, err := grpcPool.Dial(res.GetTarget())
	if err != nil {
		return err
	}
	client := NewManagerClient(conn)
	_, err = client.InitializeInstance(context.Background(), info)
	return err
}
Пример #3
0
func (finder *Finder) getInstance(
	env, service, servingID string, version int64) (
	levInstTarget *LevInstTarget, targetNode string,
	levInstResource *scale.Resource, isNewInstance bool, err error) {
	// First, try DNS-based as it is faster and provides proper
	// load-balancing.
	hostAddr, targetNode, err := scale.DereferenceService(servingID)
	if err == scale.ErrServiceNotFound {
		// Then try resource-based. This has the disadvantage that it always
		// picks the same instance (the one registered as a resource).
		levInstResourceID := makeLevInstResourceID(env, service, version)
		levInstResource, err = scale.DereferenceResource(
			leverutil.ServiceFlag.Get(), levInstResourceID)
		if err != nil {
			finder.logger.WithFields("err", err).Error(
				"Could not dereference Lever instance")
			return nil, "", nil, false, err
		}
		if levInstResource == nil {
			// No instance found for that service. Bring up a fresh one.
			return finder.newInstance(env, service, version)
		}

		levInstTarget = new(LevInstTarget)
		err = json.Unmarshal([]byte(levInstResource.GetTarget()), levInstTarget)
		if err != nil {
			finder.logger.WithFields("err", err).Panic("Failed to decode json")
		}
		finder.logger.WithFields(
			"leverEnv", env,
			"leverService", service,
			"leverInstanceID", levInstTarget.InstanceID,
		).Debug("Reusing instance")
		return levInstTarget, levInstResource.GetTargetNode(), levInstResource,
			false, nil
	} else if err != nil {
		finder.logger.WithFields("err", err).Error("DNS lookup failed")
		return nil, "", nil, false, err
	}
	return &LevInstTarget{HostAddr: hostAddr}, targetNode, nil, false, nil
}