@Override protected InstanceDomain newInstance(String name, Set<Integer> usedIds) { return new InstanceDomain(name, identifierSource.calculateId(name, usedIds), instanceStoreFactory); }
@Override public void writeToMmv(ByteBuffer byteBuffer) { byteBuffer.position(offset); writeInstanceDomainSection(byteBuffer); for (Instance instance : getInstances()) { instance.writeToMmv(byteBuffer); } }
@Override public void writeToMmv(ByteBuffer byteBuffer) { byteBuffer.position(offset); byteBuffer.putLong(instanceDomain.getOffset()); byteBuffer.putInt(0); byteBuffer.putInt(id); byteBuffer.put(name.getBytes(PCP_CHARSET)); }
private void writeInstanceDomainSection(ByteBuffer dataFileBuffer) { dataFileBuffer.putInt(id); dataFileBuffer.putInt(getInstanceCount()); dataFileBuffer.putLong(getFirstInstanceOffset()); dataFileBuffer.putLong(getStringOffset(shortHelpText)); dataFileBuffer.putLong(getStringOffset(longHelpText)); }
@Test public void shouldReturnInstancesAssociatedWithTheStore() { InstanceStoreFactory instanceStoreFactory = mock(InstanceStoreFactory.class); Store<Instance> instanceStore = mock(Store.class); Collection<Instance> instances = singletonList(mock(Instance.class)); when(instanceStoreFactory.createNewInstanceStore(eq(DOMAIN), any(InstanceDomain.class))).thenReturn(instanceStore); when(instanceStore.all()).thenReturn(instances); InstanceDomain instanceDomain = new InstanceDomain(DOMAIN, 111, instanceStoreFactory); assertThat(instanceDomain.getInstances(), is(instances)); } }
@Test @SuppressWarnings("unchecked") public void shouldBuildAByteBufferOfTheCorrectLength() throws IOException { InstanceDomain instanceDomain = mock(InstanceDomain.class); Instance instance = mock(Instance.class); PcpMetricInfo pcpMetricInfo = mock(PcpMetricInfo.class); when(pcpMetricInfo.getTypeHandler()).thenReturn(mock(TypeHandler.class)); when(pcpMetricInfo.byteSize()).thenReturn(MOCK_PCP_METRIC_INFO_BYTE_SIZE); when(instanceDomain.getInstances()).thenReturn(singletonList(instance)); when(instanceDomain.byteSize()).thenReturn(MOCK_INSTANCE_DOMAIN_BYTE_SIZE); when(instance.byteSize()).thenReturn(MOCK_INSTANCE_BYTE_SIZE); when(metricInfoStore.byName(SOME_METRIC_NAME)).thenReturn(pcpMetricInfo); when(metricInfoStore.all()).thenReturn(singletonList(pcpMetricInfo)); when(instanceDomainStore.all()).thenReturn(singletonList(instanceDomain)); when(byteBuffer.slice()).thenReturn(mock(ByteBuffer.class)); pcpMmvWriter.addMetric(MetricName.parse(SOME_METRIC_NAME), Semantics.COUNTER, null, 1); pcpMmvWriter.setMetricHelpText(SOME_METRIC_NAME, "Short help", "Long help"); pcpMmvWriter.start(); verify(byteBufferFactory).build(EXPECTED_LENGTH); }
private Collection<Instance> getInstances() { Collection<Instance> instances = new ArrayList<Instance>(); for (InstanceDomain domain : instanceDomainStore.all()) { instances.addAll(domain.getInstances()); } return instances; }
@Override public void writeToMmv(ByteBuffer byteBuffer) { byteBuffer.position(offset); int originalPosition = byteBuffer.position(); byteBuffer.put(metricName.getBytes(PCP_CHARSET)); byteBuffer.put((byte) 0); byteBuffer.position(originalPosition + METRIC_NAME_LIMIT + 1); byteBuffer.putInt(getId()); byteBuffer.putInt(typeHandler.getMetricType().getIdentifier()); byteBuffer.putInt(getSemantics().getPcpValue()); byteBuffer.putInt(UnitMapping.getDimensions(getUnit(), metricName)); if (domain != null) { byteBuffer.putInt(domain.getId()); } else { byteBuffer.putInt(DEFAULT_INSTANCE_DOMAIN_ID); } // Just padding byteBuffer.putInt(0); byteBuffer.putLong(getStringOffset(shortHelpText)); byteBuffer.putLong(getStringOffset(longHelpText)); }
@Override public final void setInstanceDomainHelpText(String instanceDomain, String shortHelpText, String longHelpText) { InstanceDomain domain = getInstanceDomain(instanceDomain); domain.setHelpText(stringStore.createPcpString(shortHelpText), stringStore.createPcpString(longHelpText)); }
private synchronized void addMetricInfo(MetricName name, Semantics semantics, Unit<?> unit, Object initialValue, TypeHandler<?> pcpType) { if (metricData.containsKey(name)) { throw new IllegalArgumentException("Metric " + name + " has already been added to writer"); } metricNameValidator.validateNameConstraints(name); PcpMetricInfo metricInfo = getMetricInfo(name.getMetric()); InstanceDomain domain = null; Instance instance = null; if (name.hasInstance()) { domain = getInstanceDomain(name.getInstanceDomainTag()); instance = domain.getInstance(name.getInstance()); metricInfo.setInstanceDomain(domain); } metricInfo.setTypeHandler(pcpType); metricInfo.setUnit(unit); metricInfo.setSemantics(semantics); PcpValueInfo info = new PcpValueInfo(name, metricInfo, instance, initialValue, stringStore); metricData.put(name, info); }
@Override public void writeToMmv(ByteBuffer byteBuffer) { byteBuffer.position(offset); byteBuffer.putLong(getStringOffset(nameAsPcpString)); byteBuffer.putInt(getId()); byteBuffer.putInt(typeHandler.getMetricType().getIdentifier()); byteBuffer.putInt(getSemantics().getPcpValue()); byteBuffer.putInt(UnitMapping.getDimensions(getUnit(), metricName)); if (domain != null) { byteBuffer.putInt(domain.getId()); } else { byteBuffer.putInt(DEFAULT_INSTANCE_DOMAIN_ID); } // Just padding byteBuffer.putInt(0); byteBuffer.putLong(getStringOffset(shortHelpText)); byteBuffer.putLong(getStringOffset(longHelpText)); }
@Override public void writeToMmv(ByteBuffer byteBuffer) { byteBuffer.position(offset); byteBuffer.putLong(instanceDomain.getOffset()); byteBuffer.putInt(0); byteBuffer.putInt(id); byteBuffer.putLong(getStringOffset(nameAsString)); }
@Test public void writeToMmvShouldWriteTheCorrectContentToTheByteBuffer() { InstanceDomain instanceDomain = mock(InstanceDomain.class); PcpString pcpString = mock(PcpString.class); ByteBuffer byteBuffer = ByteBuffer.allocate(EXPECTED_BYTE_SIZE); InstanceV2 instanceV2 = new InstanceV2(instanceDomain, INSTANCE_NAME, INSTANCE_DOMAIN_ID, pcpString); when(instanceDomain.getOffset()).thenReturn(INSTANCE_DOMAIN_OFFSET); when(pcpString.getOffset()).thenReturn(STRING_OFFSET); instanceV2.writeToMmv(byteBuffer); /* * As defined in mmv(5), this is the format of the v2 Instance data structure. * * |-------------------------------------| * | 4 bytes | 4 bytes | * |-------------------------------------| * | indom section offset | * |-------------------------------------| * | padding | external inst id | * |-------------------------------------| * | external inst name string offset | * |-------------------------------------| */ byte[] expected = { 0, 0, 0, 0, 0, 0, 0, 40, /* indom section offset */ 0, 0, 0, 0, 0, 0, 0, 123, /* padding, external inst id */ 0, 0, 0, 0, 0, 0, 0, 30, /* external inst name string offset */ }; assertArrayEquals(expected, byteBuffer.array()); }