Java Code Examples for org.apache.mesos.Protos#Value
The following examples show how to use
org.apache.mesos.Protos#Value .
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: SchedulerConfigTestUtils.java From dcos-commons with Apache License 2.0 | 6 votes |
@SuppressWarnings("PMD.AvoidUsingHardCodedIP") public static SchedulerConfig getTestSchedulerConfig() { SchedulerConfig schedulerConfig = mock(SchedulerConfig.class); when(schedulerConfig.isDeadlockExitEnabled()).thenReturn(true); when(schedulerConfig.getApiServerPort()).thenReturn(TestConstants.PORT_API_VALUE); when(schedulerConfig.getJavaURI()).thenReturn("test-java-uri"); when(schedulerConfig.getBootstrapURI()).thenReturn("test-bootstrap-uri"); when(schedulerConfig.getLibmesosURI()).thenReturn("test-libmesos-uri"); when(schedulerConfig.getDcosSpace()).thenReturn("/"); when(schedulerConfig.getSecretsNamespace(TestConstants.SERVICE_NAME)).thenReturn(TestConstants.SERVICE_NAME); when(schedulerConfig.getApiServerInitTimeout()).thenReturn(Duration.ofSeconds(10)); when(schedulerConfig.getAutoipTLD()).thenReturn("autoip.tld"); when(schedulerConfig.getVipTLD()).thenReturn("vip.tld"); when(schedulerConfig.getMarathonName()).thenReturn("test-marathon"); when(schedulerConfig.getSchedulerRegion()).thenReturn(Optional.of("test-region")); when(schedulerConfig.getMultiServiceRemovalTimeout()).thenReturn(Duration.ofSeconds(60)); when(schedulerConfig.getSchedulerIP()).thenReturn("127.0.0.1"); when(schedulerConfig.getBuildInfo()).thenReturn(new JSONObject()); Map<String, Protos.Value> map = new TreeMap<>(); map.put(Constants.CPUS_RESOURCE_TYPE, scalar(0.1)); map.put(Constants.DISK_RESOURCE_TYPE, scalar(256)); map.put(Constants.MEMORY_RESOURCE_TYPE, scalar(32)); when(schedulerConfig.getExecutorResources()).thenReturn(map); return schedulerConfig; }
Example 2
Source File: ResourceTestUtils.java From dcos-commons with Apache License 2.0 | 6 votes |
@SuppressWarnings("deprecation") // for Resource.setRole() private static Protos.Resource getUnreservedResource(String name, Protos.Value value, String role) { Protos.Resource.Builder resBuilder = Protos.Resource.newBuilder() .setName(name) .setType(value.getType()) .setRole(role); if (!role.equals(Constants.ANY_ROLE)) { // Fill in the prereserved role info: resBuilder.addReservationsBuilder() .setRole(role) .setPrincipal(TestConstants.PRINCIPAL); } switch (value.getType()) { case SCALAR: return resBuilder.setScalar(value.getScalar()).build(); case RANGES: return resBuilder.setRanges(value.getRanges()).build(); case SET: return resBuilder.setSet(value.getSet()).build(); default: return null; } }
Example 3
Source File: ResourceBuilder.java From dcos-commons with Apache License 2.0 | 6 votes |
private static Protos.Resource.Builder setValue( Protos.Resource.Builder builder, Protos.Value value) { builder.setType(value.getType()); switch (value.getType()) { case SCALAR: builder.setScalar(value.getScalar()); return builder; case RANGES: builder.setRanges(value.getRanges()); return builder; case SET: builder.setSet(value.getSet()); return builder; default: throw new IllegalArgumentException( String.format("Unsupported spec value type: %s", value.getType()) ); } }
Example 4
Source File: MesosResourcePoolTest.java From dcos-commons with Apache License 2.0 | 5 votes |
@Test public void testConsumeUnreservedMergedResource() { Resource resource = ResourceTestUtils.getUnreservedCpus(1.0); Protos.Value resourceValue = ValueUtils.getValue(resource); Offer offer = OfferTestUtils.getOffer(resource); MesosResourcePool pool = new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE)); Assert.assertEquals(1, pool.getUnreservedMergedPool().size()); Assert.assertEquals(resource.getScalar().getValue(), pool.getUnreservedMergedPool().get("cpus").getScalar().getValue(), 0.0); MesosResource resourceToConsume = pool.consumeReservableMerged(resource.getName(), resourceValue, Constants.ANY_ROLE).get(); Assert.assertEquals(resource, resourceToConsume.getResource()); Assert.assertEquals(ValueUtils.getZero(Protos.Value.Type.SCALAR), pool.getUnreservedMergedPool().get("cpus")); }
Example 5
Source File: OfferEvaluationUtilsTest.java From dcos-commons with Apache License 2.0 | 5 votes |
private void testResourceSufficient(Optional<String> resourceId, Optional<String> namespace, Optional<String> frameworkId) { Protos.Value desired = getValue(5); ResourceSpec resourceSpec = getResourceSpec(desired); if (resourceId.isPresent()) { when(mockPool.consumeReserved(RESOURCE_NAME, desired, resourceId.get())) .thenReturn(Optional.of(getMesosResource(resourceSpec, resourceId.get(), namespace, frameworkId))); } else { when(mockPool.consumeReservableMerged(RESOURCE_NAME, desired, Constants.ANY_ROLE)) .thenReturn(Optional.of(getMesosResource(desired))); } ReserveEvaluationOutcome outcome = OfferEvaluationUtils.evaluateSimpleResource( LOGGER, mockStage, resourceSpec, resourceId, namespace, mockPool, frameworkId); Assert.assertTrue(outcome.getEvaluationOutcome().isPassing()); if (resourceId.isPresent()) { Assert.assertTrue(outcome.getEvaluationOutcome().getOfferRecommendations().isEmpty()); Assert.assertEquals(resourceId.get(), outcome.getResourceId().get()); } else { OfferRecommendation recommendation = outcome.getEvaluationOutcome().getOfferRecommendations().get(0); Assert.assertTrue(recommendation instanceof ReserveOfferRecommendation); Assert.assertTrue(outcome.getResourceId().isPresent()); Protos.Resource resource = recommendation.getOperation().get().getReserve().getResources(0); Assert.assertEquals(desired.getScalar(), resource.getScalar()); if (namespace.isPresent()) { Assert.assertEquals(namespace.get(), ResourceUtils.getNamespace(resource).get()); } else { Assert.assertFalse(ResourceUtils.getNamespace(resource).isPresent()); } if (frameworkId.isPresent()) { Assert.assertEquals(frameworkId.get(), ResourceUtils.getFrameworkId(resource).get()); } else { Assert.assertFalse(ResourceUtils.getFrameworkId(resource).isPresent()); } } }
Example 6
Source File: MesosResourcePoolTest.java From dcos-commons with Apache License 2.0 | 5 votes |
@Test public void testConsumeInsufficientUnreservedMergedResource() { Resource desiredUnreservedResource = ResourceTestUtils.getUnreservedCpus(2.0); Protos.Value resourceValue = ValueUtils.getValue(desiredUnreservedResource); Resource offeredUnreservedResource = ResourceTestUtils.getUnreservedCpus(1.0); Offer offer = OfferTestUtils.getOffer(offeredUnreservedResource); MesosResourcePool pool = new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE)); Assert.assertFalse( pool.consumeReservableMerged(desiredUnreservedResource.getName(), resourceValue, Constants.ANY_ROLE) .isPresent()); }
Example 7
Source File: AttributeStringUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
/** * Converts the provided attribute's value into a string which follows the format defined by * Mesos: * <code> * attributes : attribute ( ";" attribute )* * attribute : text ":" ( scalar | range | text ) * text : [a-zA-Z0-9_/.-] * scalar : floatValue * floatValue : ( intValue ( "." intValue )? ) | ... * intValue : [0-9]+ * range : "[" rangeValue ( "," rangeValue )* "]" * rangeValue : scalar "-" scalar * set : "{" text ( "," text )* "}" * </code> * <p> * NOTE that it is difficult if not impossible to consistently perform the inverse of this * operation. For example, how can you tell if something is supposed to be a SCALAR value or a * TEXT value? [0-9.]+ is valid in both cases! Your best hope is to consistently convert to * string, and then compare strings... * * @throws IllegalArgumentException if some part of the provided attributes couldn't be * serialized */ public static String toString(Protos.Value value) throws IllegalArgumentException { StringBuilder buf = new StringBuilder(); switch (value.getType()) { case RANGES: // "ports:[21000-24000,30000-34000]" buf.append('['); buf.append(value .getRanges() .getRangeList() .stream() .map(range -> String.format("%d-%d", range.getBegin(), range.getEnd())) .collect(Collectors.joining(",")) ); buf.append(']'); break; case SCALAR: // according to mesos.proto: "Mesos keeps three decimal digits of precision ..." // let's just ensure that we're producing consistent strings. buf.append(String.format(Locale.ROOT, "%.3f", value.getScalar().getValue())); break; case SET: // "bugs(debug_role):{a,b,c}" buf.append('{'); buf.append(String.join(",", value.getSet().getItemList())); buf.append('}'); break; case TEXT: // "key:value" buf.append(value.getText().getValue()); break; default: throw new IllegalArgumentException( "Unsupported value type: " + TextFormat.shortDebugString(value) ); } return buf.toString(); }
Example 8
Source File: SchedulerConfig.java From dcos-commons with Apache License 2.0 | 5 votes |
/** * Returns the default resources to be require for a pod's executor, in addition to any resources for the tasks * composing the pod. This is effectively the configured executor overhead. * * @return a mapping of resource name to value for the resources to be reserved for a pod's executor */ public Map<String, Protos.Value> getExecutorResources() { Map<String, Protos.Value> map = new TreeMap<>(); map.put(Constants.CPUS_RESOURCE_TYPE, scalar(envStore.getOptionalDouble(EXECUTOR_CPUS_ENV, DEFAULT_EXECUTOR_CPUS))); map.put(Constants.MEMORY_RESOURCE_TYPE, scalar(envStore.getOptionalDouble(EXECUTOR_MEM_MB_ENV, DEFAULT_EXECUTOR_MEM_MB))); map.put(Constants.DISK_RESOURCE_TYPE, scalar(envStore.getOptionalDouble(EXECUTOR_DISK_MB_ENV, DEFAULT_EXECUTOR_DISK_MB))); return map; }
Example 9
Source File: PortEvaluationStageTest.java From dcos-commons with Apache License 2.0 | 5 votes |
private Protos.Value getPort(int port) { return Protos.Value.newBuilder() .setType(Protos.Value.Type.RANGES) .setRanges(Protos.Value.Ranges.newBuilder() .addRange(Protos.Value.Range.newBuilder() .setBegin(port) .setEnd(port))) .build(); }
Example 10
Source File: OfferEvaluationUtilsTest.java From dcos-commons with Apache License 2.0 | 5 votes |
private void testResourceDecrease(Optional<String> namespace, Optional<String> frameworkId) { String resourceId = UUID.randomUUID().toString(); Protos.Value current = getValue(5); Protos.Value desired = getValue(4); Protos.Value toSubtract = ValueUtils.subtract(current, desired); ResourceSpec resourceSpec = getResourceSpec(desired); when(mockPool.consumeReserved(RESOURCE_NAME, desired, resourceId)) .thenReturn(Optional.of(getMesosResource(getResourceSpec(current), resourceId, namespace, frameworkId))); when(mockPool.consumeReservableMerged(RESOURCE_NAME, desired, Constants.ANY_ROLE)) .thenReturn(Optional.of(getMesosResource(toSubtract))); ReserveEvaluationOutcome outcome = OfferEvaluationUtils.evaluateSimpleResource( LOGGER, mockStage, resourceSpec, Optional.of(resourceId), namespace, mockPool, frameworkId); Assert.assertTrue(outcome.getEvaluationOutcome().isPassing()); OfferRecommendation recommendation = outcome.getEvaluationOutcome().getOfferRecommendations().get(0); Assert.assertTrue(recommendation instanceof UnreserveOfferRecommendation); Assert.assertTrue(outcome.getResourceId().isPresent()); Protos.Resource resource = recommendation.getOperation().get().getUnreserve().getResources(0); Assert.assertEquals(toSubtract.getScalar(), resource.getScalar()); if (namespace.isPresent()) { Assert.assertEquals(namespace.get(), ResourceUtils.getNamespace(resource).get()); } else { Assert.assertFalse(ResourceUtils.getNamespace(resource).isPresent()); } if (frameworkId.isPresent()) { Assert.assertEquals(frameworkId.get(), ResourceUtils.getFrameworkId(resource).get()); } else { Assert.assertFalse(ResourceUtils.getFrameworkId(resource).isPresent()); } }
Example 11
Source File: PortSpec.java From dcos-commons with Apache License 2.0 | 5 votes |
/** * Returns a copy of the provided {@link PortSpec} which has been updated to have the provided {@code value}. */ @JsonIgnore public static PortSpec withValue(PortSpec portSpec, Protos.Value value) { return new PortSpec( value, portSpec.getRole(), portSpec.getPreReservedRole(), portSpec.getPrincipal(), portSpec.getEnvKey(), portSpec.getPortName(), portSpec.getVisibility(), portSpec.getNetworkNames(), portSpec.getRanges()); }
Example 12
Source File: OfferEvaluationUtilsTest.java From dcos-commons with Apache License 2.0 | 5 votes |
private void testResourceIncreaseSufficient(Optional<String> namespace, Optional<String> frameworkId) { String resourceId = UUID.randomUUID().toString(); Protos.Value current = getValue(4); Protos.Value desired = getValue(5); Protos.Value toAdd = ValueUtils.subtract(desired, current); ResourceSpec resourceSpec = getResourceSpec(desired); when(mockPool.consumeReserved(RESOURCE_NAME, desired, resourceId)) .thenReturn(Optional.of(getMesosResource(getResourceSpec(current), resourceId, namespace, frameworkId))); when(mockPool.consumeReservableMerged(RESOURCE_NAME, toAdd, Constants.ANY_ROLE)) .thenReturn(Optional.of(getMesosResource(toAdd))); ReserveEvaluationOutcome outcome = OfferEvaluationUtils.evaluateSimpleResource( LOGGER, mockStage, resourceSpec, Optional.of(resourceId), namespace, mockPool, frameworkId); Assert.assertTrue(outcome.getEvaluationOutcome().isPassing()); OfferRecommendation recommendation = outcome.getEvaluationOutcome().getOfferRecommendations().get(0); Assert.assertTrue(recommendation instanceof ReserveOfferRecommendation); Assert.assertTrue(outcome.getResourceId().isPresent()); Protos.Resource resource = recommendation.getOperation().get().getReserve().getResources(0); Assert.assertEquals(toAdd.getScalar(), resource.getScalar()); if (namespace.isPresent()) { Assert.assertEquals(namespace.get(), ResourceUtils.getNamespace(resource).get()); } else { Assert.assertFalse(ResourceUtils.getNamespace(resource).isPresent()); } if (frameworkId.isPresent()) { Assert.assertEquals(frameworkId.get(), ResourceUtils.getFrameworkId(resource).get()); } else { Assert.assertFalse(ResourceUtils.getFrameworkId(resource).isPresent()); } }
Example 13
Source File: SendOffer.java From dcos-commons with Apache License 2.0 | 5 votes |
@SuppressWarnings("deprecation") // for Resource.setRole() private static Protos.Resource toUnreservedResource(String resourceName, Protos.Value value, boolean isMountDisk) { Protos.Resource.Builder resourceBuilder = Protos.Resource.newBuilder() .setRole("*") .setName(resourceName) .setType(value.getType()); switch (value.getType()) { case SCALAR: resourceBuilder.setScalar(value.getScalar()); break; case RANGES: resourceBuilder.setRanges(value.getRanges()); break; case SET: resourceBuilder.setSet(value.getSet()); break; default: throw new IllegalArgumentException("Unsupported value type: " + value); } if (isMountDisk) { resourceBuilder.getDiskBuilder().getSourceBuilder() .setType(Protos.Resource.DiskInfo.Source.Type.MOUNT) .getMountBuilder().setRoot("test-mount-root"); } return resourceBuilder.build(); }
Example 14
Source File: OfferEvaluationUtilsTest.java From dcos-commons with Apache License 2.0 | 4 votes |
private static MesosResource getMesosResource(Protos.Value value) { return new MesosResource(ResourceBuilder.fromUnreservedValue(RESOURCE_NAME, value).build()); }
Example 15
Source File: ResourceBuilder.java From dcos-commons with Apache License 2.0 | 4 votes |
public static ResourceBuilder fromUnreservedValue(String resourceName, Protos.Value value) { return new ResourceBuilder(resourceName, value, Constants.ANY_ROLE); }
Example 16
Source File: ResourceBuilder.java From dcos-commons with Apache License 2.0 | 4 votes |
/** * Sets the value for this resource. Supported types are {@code SCALAR}, {@code RANGES}, and {@code SET}. */ public ResourceBuilder setValue(Protos.Value value) { this.value = value; return this; }
Example 17
Source File: OfferEvaluationUtilsTest.java From dcos-commons with Apache License 2.0 | 4 votes |
private static Protos.Value getValue(double value) { Protos.Value.Builder valueBuilder = Protos.Value.newBuilder().setType(Protos.Value.Type.SCALAR); valueBuilder.getScalarBuilder().setValue(value); return valueBuilder.build(); }
Example 18
Source File: DefaultVolumeSpec.java From dcos-commons with Apache License 2.0 | 4 votes |
private static Protos.Value scalarValue(double value) { Protos.Value.Builder builder = Protos.Value.newBuilder().setType(Protos.Value.Type.SCALAR); builder.getScalarBuilder().setValue(value); return builder.build(); }
Example 19
Source File: SchedulerConfigTestUtils.java From dcos-commons with Apache License 2.0 | 4 votes |
private static Protos.Value scalar(double val) { Protos.Value.Builder builder = Protos.Value.newBuilder() .setType(Protos.Value.Type.SCALAR); builder.getScalarBuilder().setValue(val); return builder.build(); }
Example 20
Source File: SchedulerConfig.java From dcos-commons with Apache License 2.0 | 4 votes |
private static Protos.Value scalar(double val) { Protos.Value.Builder builder = Protos.Value.newBuilder() .setType(Protos.Value.Type.SCALAR); builder.getScalarBuilder().setValue(val); return builder.build(); }