Java Code Examples for org.apache.mesos.Protos#Attribute

The following examples show how to use org.apache.mesos.Protos#Attribute . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AgentManagementConstraintTest.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private VirtualMachineCurrentState createVirtualMachineCurrentStateMock(String id) {
    VirtualMachineCurrentState currentState = mock(VirtualMachineCurrentState.class);
    VirtualMachineLease lease = mock(VirtualMachineLease.class);
    Map<String, Protos.Attribute> attributes = new HashMap<>();
    attributes.put("id", Protos.Attribute.newBuilder().setName("id").setType(Protos.Value.Type.TEXT).setText(Protos.Value.Text.newBuilder().setValue(id)).build());
    when(lease.getAttributeMap()).thenReturn(attributes);
    when(currentState.getCurrAvailableResources()).thenReturn(lease);
    return currentState;
}
 
Example 2
Source File: CassandraFrameworkProtosUtils.java    From cassandra-mesos-deprecated with Apache License 2.0 5 votes vote down vote up
public static String attributeValue(@NotNull final Protos.Offer offer, @NotNull final String name) {
    for (final Protos.Attribute attribute : offer.getAttributesList()) {
        if (name.equals(attribute.getName())) {
            return attribute.hasText() ? attribute.getText().getValue() : null;
        }
    }

    return null;
}
 
Example 3
Source File: SchedulerTestUtils.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
public static VirtualMachineCurrentState createVirtualMachineCurrentStateMock(String id) {
    VirtualMachineCurrentState currentState = mock(VirtualMachineCurrentState.class);
    VirtualMachineLease lease = mock(VirtualMachineLease.class);
    Map<String, Protos.Attribute> attributes = new HashMap<>();
    attributes.put("id", Protos.Attribute.newBuilder().setName("id").setType(Protos.Value.Type.TEXT).setText(Protos.Value.Text.newBuilder().setValue(id)).build());
    when(lease.getAttributeMap()).thenReturn(attributes);
    when(currentState.getCurrAvailableResources()).thenReturn(lease);
    return currentState;
}
 
Example 4
Source File: LikeConstraintTest.java    From incubator-myriad with Apache License 2.0 5 votes vote down vote up
private Protos.Attribute getTextAttribute(String name, String value) {
  return Protos.Attribute.newBuilder()
    .setName(name)
    .setType(TEXT)
    .setText(Text.newBuilder()
    .setValue(value))
    .build();
}
 
Example 5
Source File: OfferTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Protos.Offer offer(List<Protos.Resource> resources, List<Protos.Attribute> attributes) {
	return Protos.Offer.newBuilder()
		.setId(OFFER_ID)
		.setFrameworkId(FRAMEWORK_ID)
		.setHostname(HOSTNAME)
		.setSlaveId(AGENT_ID)
		.addAllAttributes(attributes)
		.addAllResources(resources)
		.build();
}
 
Example 6
Source File: OfferTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Protos.Attribute attr(String name, double scalar) {
	return Protos.Attribute.newBuilder()
		.setName(name)
		.setType(Protos.Value.Type.SCALAR)
		.setScalar(Protos.Value.Scalar.newBuilder().setValue(scalar))
		.build();
}
 
Example 7
Source File: OfferTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Protos.Attribute attr(String name, double scalar) {
	return Protos.Attribute.newBuilder()
		.setName(name)
		.setType(Protos.Value.Type.SCALAR)
		.setScalar(Protos.Value.Scalar.newBuilder().setValue(scalar))
		.build();
}
 
Example 8
Source File: OfferTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Protos.Offer offer(List<Protos.Resource> resources, List<Protos.Attribute> attributes) {
	return Protos.Offer.newBuilder()
		.setId(OFFER_ID)
		.setFrameworkId(FRAMEWORK_ID)
		.setHostname(HOSTNAME)
		.setSlaveId(AGENT_ID)
		.addAllAttributes(attributes)
		.addAllResources(resources)
		.build();
}
 
Example 9
Source File: KubeApiServerIntegratorTest.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Test
public void testHasRequiredNodeAttributes() {
    List<Protos.Attribute> attributes = new ArrayList<>();
    assertThat(KubeApiServerIntegrator.hasRequiredNodeAttributes(attributes)).isFalse();

    attributes.add(Protos.Attribute.newBuilder().setName(KubeApiServerIntegrator.NODE_ATTRIBUTE_ID).setType(Protos.Value.Type.TEXT).build());
    attributes.add(Protos.Attribute.newBuilder().setName(KubeApiServerIntegrator.NODE_ATTRIBUTE_HOST_IP).setType(Protos.Value.Type.TEXT).build());
    attributes.add(Protos.Attribute.newBuilder().setName(KubeApiServerIntegrator.NODE_ATTRIBUTE_RES).setType(Protos.Value.Type.TEXT).build());
    attributes.add(Protos.Attribute.newBuilder().setName(KubeApiServerIntegrator.NODE_ATTRIBUTE_REGION).setType(Protos.Value.Type.TEXT).build());
    attributes.add(Protos.Attribute.newBuilder().setName(KubeApiServerIntegrator.NODE_ATTRIBUTE_ZONE).setType(Protos.Value.Type.TEXT).build());
    attributes.add(Protos.Attribute.newBuilder().setName(KubeApiServerIntegrator.NODE_ATTRIBUTE_I_TYPE).setType(Protos.Value.Type.TEXT).build());
    assertThat(KubeApiServerIntegrator.hasRequiredNodeAttributes(attributes)).isTrue();
}
 
Example 10
Source File: SchedulerUtils.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
public static String getAttributeValueOrDefault(Map<String, Protos.Attribute> attributeMap, String attributeName, String defaultValue) {
    if (attributeMap == null) {
        return defaultValue;
    }
    Protos.Attribute attr = attributeMap.get(attributeName);
    if (attr == null || attr.getText() == null) {
        return defaultValue;
    }
    String attrValue = StringExt.safeTrim(attr.getText().getValue());
    return attrValue.isEmpty() ? defaultValue : attrValue;
}
 
Example 11
Source File: VMOperationsImpl.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public List<AgentInfo> getAgentInfos() {
    List<VirtualMachineCurrentState> vmStates = vmStatesMap.get("0");
    List<AgentInfo> agentInfos = new ArrayList<>();
    if (vmStates != null && !vmStates.isEmpty()) {
        for (VirtualMachineCurrentState s : vmStates) {
            List<VirtualMachineLease.Range> ranges = s.getCurrAvailableResources().portRanges();
            int ports = 0;
            if (ranges != null && !ranges.isEmpty()) {
                for (VirtualMachineLease.Range r : ranges) {
                    ports += r.getEnd() - r.getBeg();
                }
            }
            Map<String, Protos.Attribute> attributeMap = s.getCurrAvailableResources().getAttributeMap();
            Map<String, String> attributes = new HashMap<>();
            if (attributeMap != null && !attributeMap.isEmpty()) {
                for (Map.Entry<String, Protos.Attribute> entry : attributeMap.entrySet()) {
                    attributes.put(entry.getKey(), entry.getValue().getText().getValue());
                }
            }

            List<String> offerIds = s.getAllCurrentOffers().stream()
                    .map(offer -> offer.getId().getValue()).collect(Collectors.toList());
            agentInfos.add(new AgentInfo(
                    s.getHostname(), s.getCurrAvailableResources().cpuCores(),
                    s.getCurrAvailableResources().memoryMB(), s.getCurrAvailableResources().diskMB(),
                    ports, s.getCurrAvailableResources().getScalarValues(), attributes, s.getResourceSets().keySet(),
                    getTimeString(s.getDisabledUntil()), offerIds
            ));
        }
    }
    return agentInfos;
}
 
Example 12
Source File: KubeApiServerIntegrator.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private Protos.Attribute createAttribute(String name, String value) {
    if (name.startsWith(ATTRIBUTE_PREFIX)) {
        name = name.replace(ATTRIBUTE_PREFIX, "");
    }
    return Protos.Attribute.newBuilder()
            .setType(Protos.Value.Type.TEXT)
            .setName(name)
            .setText(Protos.Value.Text.newBuilder().setValue(value).build())
            .build();
}
 
Example 13
Source File: OfferTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private static List<Protos.Attribute> attrs(Protos.Attribute... attributes) {
	return Arrays.asList(attributes);
}
 
Example 14
Source File: StubbedVirtualMachineMasterService.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
VirtualMachineLease buildLease(String taskId) {
    return new VirtualMachineLease() {
        @Override
        public String getId() {
            return "leaseOf#" + taskId;
        }

        @Override
        public long getOfferedTime() {
            return TimeUnit.DAYS.toMillis(1);
        }

        @Override
        public String hostname() {
            return "i-12345";
        }

        @Override
        public String getVMID() {
            return "vm#0";
        }

        @Override
        public double cpuCores() {
            return 16;
        }

        @Override
        public double memoryMB() {
            return 16384;
        }

        @Override
        public double networkMbps() {
            return 2000;
        }

        @Override
        public double diskMB() {
            return 32768;
        }

        @Override
        public List<Range> portRanges() {
            return Collections.emptyList();
        }

        @Override
        public Protos.Offer getOffer() {
            throw new IllegalStateException("not supported");
        }

        @Override
        public Map<String, Protos.Attribute> getAttributeMap() {
            return Collections.emptyMap();
        }

        @Override
        public Double getScalarValue(String name) {
            return null;
        }

        @Override
        public Map<String, Double> getScalarValues() {
            return Collections.emptyMap();
        }
    };
}
 
Example 15
Source File: SchedulerUtils.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
public static String getAttributeValueOrEmptyString(Map<String, Protos.Attribute> attributeMap, String attributeName) {
    return getAttributeValueOrDefault(attributeMap, attributeName, "");
}
 
Example 16
Source File: KubeApiServerIntegrator.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
static boolean hasRequiredNodeAttributes(List<Protos.Attribute> nodeAttributes) {
    Set<String> names = nodeAttributes.stream().map(Protos.Attribute::getName).collect(Collectors.toSet());
    return names.containsAll(REQUIRED_NODE_ATTRIBUTES);
}
 
Example 17
Source File: Offer.java    From flink with Apache License 2.0 4 votes vote down vote up
public Offer(Protos.Offer offer) {
	this.offer = checkNotNull(offer);
	this.hostname = offer.getHostname();
	this.vmID = offer.getSlaveId().getValue();
	this.offeredTime = System.currentTimeMillis();

	List<Protos.Resource> resources = new ArrayList<>(offer.getResourcesList().size());
	Map<String, Double> aggregatedScalarResourceMap = new HashMap<String, Double>() {
		@Override
		public Double remove(Object key) {
			if (super.containsKey(key)) {
				return super.remove(key);
			} else {
				return 0.0;
			}
		}
	};
	Map<String, List<Protos.Resource>> rangesResourceMap = new HashMap<>();
	for (Protos.Resource resource : offer.getResourcesList()) {
		switch (resource.getType()) {
			case SCALAR:
				resources.add(resource);
				aggregatedScalarResourceMap.merge(resource.getName(), resource.getScalar().getValue(), Double::sum);
				break;
			case RANGES:
				resources.add(resource);
				rangesResourceMap.computeIfAbsent(resource.getName(), k -> new ArrayList<>(2)).add(resource);
				break;
			default:
				logger.debug("Unknown resource type " + resource.getType() + " for resource " + resource.getName() +
					" in offer, hostname=" + hostname + ", offerId=" + offer.getId());
		}
	}
	this.resources = Collections.unmodifiableList(resources);

	this.cpuCores = aggregatedScalarResourceMap.remove("cpus");
	this.memoryMB = aggregatedScalarResourceMap.remove("mem");
	this.networkMbps = aggregatedScalarResourceMap.remove("network");
	this.diskMB = aggregatedScalarResourceMap.remove("disk");
	this.aggregatedScalarResourceMap = Collections.unmodifiableMap(aggregatedScalarResourceMap);
	this.portRanges = Collections.unmodifiableList(aggregateRangesResource(rangesResourceMap, "ports"));

	if (offer.getAttributesCount() > 0) {
		Map<String, Protos.Attribute> attributeMap = new HashMap<>();
		for (Protos.Attribute attribute: offer.getAttributesList()) {
			attributeMap.put(attribute.getName(), attribute);
		}
		this.attributeMap = Collections.unmodifiableMap(attributeMap);
	} else {
		this.attributeMap = Collections.emptyMap();
	}
}
 
Example 18
Source File: OfferTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private static List<Protos.Attribute> attrs(Protos.Attribute... attributes) {
	return Arrays.asList(attributes);
}
 
Example 19
Source File: Offer.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Protos.Attribute> getAttributeMap() {
	return attributeMap;
}
 
Example 20
Source File: AttributeStringUtils.java    From dcos-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Converts the provided attribute's name + value into a string which follows the format defined
 * by Mesos.
 *
 * @see #valueString(Attribute)
 */
public static String toString(Protos.Attribute attribute) throws IllegalArgumentException {
  return String.format("%s%c%s",
      attribute.getName(), ATTRIBUTE_KEYVAL_SEPARATOR, toString(toValue(attribute)));
}