Search in sources :

Example 31 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class ScoreHostPolicy method choose.

@Override
public DirectCloudHost choose(List<DirectCloudHost> candidates) throws OpsException {
    final String sameGroupId;
    if (Strings.isNullOrEmpty(hostPolicy.groupId)) {
        sameGroupId = DEFAULT_GROUP;
    } else {
        sameGroupId = hostPolicy.groupId;
    }
    final ItemType sameItemType;
    if (hostPolicy.scoreSameItemType != 0) {
        PlatformLayerKey owner = findOwner(newInstance);
        if (owner == null) {
            throw new OpsException();
        }
        sameItemType = owner.getItemType();
    } else {
        sameItemType = null;
    }
    List<HostRecord> records = Lists.newArrayList();
    for (DirectCloudHost candidate : candidates) {
        HostRecord record = new HostRecord();
        record.candidate = candidate;
        record.groups = Maps.newHashMap();
        if (hostPolicy.scoreSameItemType != 0) {
            record.owners = Maps.newHashMap();
        }
        records.add(record);
        for (String assigned : candidate.getModel().getTags().findAll(Tag.ASSIGNED)) {
            PlatformLayerKey instanceKey = PlatformLayerKey.parse(assigned);
            // TODO: Avoid 1+N
            DirectInstance instance = platformLayer.getItem(instanceKey);
            if (instance == null) {
                // TODO: Warn?
                throw new IllegalStateException();
            }
            switch(instance.getState()) {
                case DELETE_REQUESTED:
                case DELETED:
                    continue;
            }
            HostPolicy instanceHostPolicy = instance.hostPolicy;
            String instanceGroupId = instanceHostPolicy.groupId;
            if (Strings.isNullOrEmpty(instanceGroupId)) {
                instanceGroupId = DEFAULT_GROUP;
            }
            record.groups.put(instance, instanceGroupId);
            if (sameItemType != null) {
                PlatformLayerKey owner = findOwner(instance);
                if (owner != null) {
                    record.owners.put(instance, owner);
                }
            }
            record.all.add(instance);
        }
    }
    Function<HostRecord, Float> score = new Function<HostRecord, Float>() {

        @Override
        public Float apply(HostRecord record) {
            float score = 0;
            for (DirectInstance instance : record.all) {
                if (sameGroupId != null) {
                    String instanceGroupId = record.groups.get(instance);
                    if (Objects.equal(instanceGroupId, sameGroupId)) {
                        score += hostPolicy.scoreSameGroup;
                    }
                }
                if (sameItemType != null) {
                    PlatformLayerKey owner = record.owners.get(instance);
                    if (owner != null && owner.getItemType().equals(sameItemType)) {
                        score += hostPolicy.scoreSameItemType;
                    }
                }
            }
            // Break ties using least-loaded
            score -= record.all.size() / 1000.0f;
            return score;
        }
    };
    HostRecord bestRecord = ScoreChooser.chooseMax(score).choose(records);
    return bestRecord.candidate;
}
Also used : OpsException(org.platformlayer.ops.OpsException) ItemType(org.platformlayer.ids.ItemType) DirectInstance(org.platformlayer.service.cloud.direct.model.DirectInstance) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Function(com.google.common.base.Function) HostPolicy(org.platformlayer.core.model.HostPolicy)

Example 32 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class PlatformLayerTestContext method deleteItem.

public <T extends ItemBase> JobData deleteItem(T item) throws IOException, OpsException {
    TypedPlatformLayerClient client = getTypedClient();
    PlatformLayerKey key = item.getKey();
    return client.deleteItem(key);
}
Also used : TypedPlatformLayerClient(org.platformlayer.TypedPlatformLayerClient) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Example 33 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class PlatformLayerTestContext method doAction.

public <T extends ItemBase> JobData doAction(T item, Action action) throws OpsException, IOException {
    TypedPlatformLayerClient client = getTypedClient();
    PlatformLayerKey key = item.getKey();
    return client.doAction(key, action);
}
Also used : TypedPlatformLayerClient(org.platformlayer.TypedPlatformLayerClient) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Example 34 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class PlatformLayerTestContext method waitForJobComplete.

public JobData waitForJobComplete(JobData job, TimeSpan timeout) throws OpsException, IOException {
    TypedPlatformLayerClient client = getTypedClient();
    PlatformLayerKey jobKey = job.key;
    long startedAt = System.currentTimeMillis();
    while (true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new IllegalStateException("Interrupted", e);
        }
        if (timeout != null && timeout.hasTimedOut(startedAt)) {
            throw new OpsException("Timeout waiting for job completion");
        }
        // TODO: We really need a "get job status" function
        JobData found = null;
        for (JobData candidate : client.listJobs().getJobs()) {
            if (jobKey.equals(candidate.getJobKey())) {
                found = candidate;
            }
        }
        if (found == null) {
            // Assume completed?
            throw new IllegalStateException("Job not found in job list");
        }
        JobExecutionList executions = client.listJobExecutions(job.getJobKey().getItemIdString());
        JobExecutionData foundExecution = null;
        for (JobExecutionData candidate : executions) {
            if (jobKey.equals(candidate.getJobKey())) {
                foundExecution = candidate;
            }
        }
        if (foundExecution == null) {
            throw new IllegalStateException("Execution not found in execution list");
        }
        JobState state = foundExecution.getState();
        switch(state) {
            case FAILED:
            case SUCCESS:
                System.out.println("Job completed; state=" + state);
                return found;
            case RUNNING:
                System.out.println("Continuing to wait for " + job.key + "; state=" + state);
                break;
            default:
                throw new IllegalStateException("Unexpected state: " + state + " for " + job.key);
        }
    }
}
Also used : TypedPlatformLayerClient(org.platformlayer.TypedPlatformLayerClient) OpsException(org.platformlayer.ops.OpsException) JobExecutionData(org.platformlayer.jobs.model.JobExecutionData) JobState(org.platformlayer.jobs.model.JobState) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) JobData(org.platformlayer.jobs.model.JobData) JobExecutionList(org.platformlayer.jobs.model.JobExecutionList)

Example 35 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class PlatformLayerCliContext method pathToItem.

public PlatformLayerKey pathToItem(ProjectId project, String path) throws PlatformLayerClientException {
    String serviceType = null;
    String itemType = null;
    String itemId = null;
    if (path.contains("://")) {
        return PlatformLayerKey.parse(path);
    }
    List<String> components = Lists.newArrayList(path.split("/"));
    if (components.size() <= 1) {
        throw new IllegalArgumentException("Cannot resolve path: " + path);
    }
    String head = components.get(0);
    {
        Map<String, ServiceInfo> services = listServices();
        if (services.containsKey(head)) {
            serviceType = services.get(head).serviceType;
            components.remove(0);
            if (components.size() <= 1) {
                throw new IllegalArgumentException("Cannot resolve path: " + path);
            }
            head = components.get(0);
        }
    }
    Multimap<String, ServiceInfo> items = listItemTypes();
    if (items.containsKey(head)) {
        Collection<ServiceInfo> services = items.get(head);
        if (services.size() > 1) {
            throw new IllegalArgumentException("Cannot resolve path (ambiguous item type): " + path);
        }
        ServiceInfo serviceInfo = Iterables.getOnlyElement(services);
        itemType = head;
        if (serviceType != null) {
            if (!Objects.equal(serviceType, serviceInfo.serviceType)) {
                throw new IllegalArgumentException("Cannot resolve path (service/item type mismatch): " + path);
            }
        } else {
            serviceType = serviceInfo.serviceType;
        }
        components.remove(0);
    } else {
        throw new IllegalArgumentException("Cannot resolve path (unknown item type): " + path);
    }
    itemId = Joiner.on('/').join(components);
    if (serviceType == null) {
        throw new IllegalArgumentException("Cannot resolve path (service type not resolved): " + path);
    }
    if (itemType == null) {
        throw new IllegalArgumentException("Cannot resolve path (item type not resolved): " + path);
    }
    if (Strings.isNullOrEmpty(itemId)) {
        throw new IllegalArgumentException("Cannot resolve path (item id not resolved): " + path);
    }
    FederationKey host = null;
    return new PlatformLayerKey(host, project, new ServiceType(serviceType), new ItemType(itemType), new ManagedItemId(itemId));
}
Also used : ServiceInfo(org.platformlayer.core.model.ServiceInfo) ServiceType(org.platformlayer.ids.ServiceType) ItemType(org.platformlayer.ids.ItemType) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Map(java.util.Map) ManagedItemId(org.platformlayer.ids.ManagedItemId) FederationKey(org.platformlayer.ids.FederationKey)

Aggregations

PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)86 PlatformLayerClient (org.platformlayer.PlatformLayerClient)21 OpsException (org.platformlayer.ops.OpsException)16 ItemBase (org.platformlayer.core.model.ItemBase)14 ManagedItemId (org.platformlayer.ids.ManagedItemId)13 UntypedItem (org.platformlayer.common.UntypedItem)10 ProjectId (org.platformlayer.ids.ProjectId)10 Tag (org.platformlayer.core.model.Tag)8 RepositoryException (org.platformlayer.RepositoryException)7 ServiceType (org.platformlayer.ids.ServiceType)7 JobData (org.platformlayer.jobs.model.JobData)7 InstanceBase (org.platformlayer.core.model.InstanceBase)6 ItemType (org.platformlayer.ids.ItemType)6 OpsTarget (org.platformlayer.ops.OpsTarget)6 JaxbHelper (org.platformlayer.xml.JaxbHelper)6 Handler (org.platformlayer.ops.Handler)5 TypedPlatformLayerClient (org.platformlayer.TypedPlatformLayerClient)4 UntypedItemXml (org.platformlayer.UntypedItemXml)4 FederationKey (org.platformlayer.ids.FederationKey)4 Machine (org.platformlayer.ops.Machine)4