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;
}
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);
}
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);
}
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);
}
}
}
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));
}
Aggregations