public class PcpMmvWriter extends java.lang.Object implements PcpWriter
Creates and updates a memory-mapped file suitable for reading by the PCP MMV PMDA. The method
used to 'render' the Java values into the file (converting them to a corresponding PCP Type and
byte array) are determined by TypeHandlers
; some default TypeHandlers are
supplied (see DefaultTypeHandlers.getDefaultMappings()
) but additional ones can be
supplied for a particular Java class (registerType(Class, TypeHandler)
, or on a
metric-by-metric basis.
addMetric(MetricName, Semantics, Unit, Object)
for every metric which will be monitored by the file
start()
to write out the file ready for the MMV agent to read
updateMetric(MetricName, Object)
metrics as new values come to hand
Adding metrics after the file is started will result in an IllegalStateException
, as will
updating metric values before it's started.
This class currently has a few important limitations:
Modifier and Type | Class and Description |
---|---|
static class |
PcpMmvWriter.MmvFlag |
(package private) static class |
PcpMmvWriter.Store<T extends PcpId> |
private static class |
PcpMmvWriter.TocType |
Modifier and Type | Field and Description |
---|---|
private ByteBufferFactory |
byteBufferFactory |
private int |
clusterIdentifier |
(package private) static int |
DATA_VALUE_LENGTH |
private java.nio.ByteBuffer |
dataFileBuffer |
private static java.util.Set<PcpMmvWriter.MmvFlag> |
DEFAULT_FLAGS |
private java.io.File |
file |
private java.util.Set<PcpMmvWriter.MmvFlag> |
flags |
private java.lang.Object |
globalLock |
private static int |
HEADER_LENGTH |
private PcpMmvWriter.Store<InstanceDomain> |
instanceDomainStore |
private java.util.Map<MetricName,PcpValueInfo> |
metricData |
private PcpMmvWriter.Store<PcpMetricInfo> |
metricInfoStore |
private MetricNameValidator |
metricNameValidator |
private static TypeHandler<java.lang.String> |
MMV_STRING_HANDLER |
private MmvVersion |
mmvVersion |
(package private) static java.nio.charset.Charset |
PCP_CHARSET
The charset used for PCP metrics names and String values.
|
private java.util.Map<PcpValueInfo,java.nio.ByteBuffer> |
perMetricByteBuffers |
private int |
processIdentifier |
private boolean |
started |
private PcpString.PcpStringStore |
stringStore |
private static byte[] |
TAG |
private static int |
TOC_LENGTH |
private java.util.Map<java.lang.Class<?>,TypeHandler<?>> |
typeHandlers |
private boolean |
usePerMetricLock |
Constructor and Description |
---|
PcpMmvWriter(ByteBufferFactory byteBufferFactory,
IdentifierSourceSet identifierSources) |
PcpMmvWriter(ByteBufferFactory byteBufferFactory,
IdentifierSourceSet identifierSources,
MmvVersion mmvVersion) |
PcpMmvWriter(java.io.File file)
Deprecated.
should pass in an explicit IdentifierSourceSet
|
PcpMmvWriter(java.io.File file,
IdentifierSourceSet identifierSources)
Creates a new PcpMmvWriter writing to the underlying file, which will be created + opened as a
memory-mapped file.
|
PcpMmvWriter(java.io.File file,
IdentifierSourceSet identifierSources,
MmvVersion mmvVersion)
Creates a new PcpMmvWriter writing to the underlying file, which will be created + opened as a
memory-mapped file.
|
PcpMmvWriter(java.lang.String name,
IdentifierSourceSet identifierSources)
Creates a new PcpMmvWriter writing to the underlying file, which will be created + opened as a
memory-mapped file.
|
PcpMmvWriter(java.lang.String name,
IdentifierSourceSet identifierSources,
MmvVersion mmvVersion)
Creates a new PcpMmvWriter writing to the underlying file, which will be created + opened as a
memory-mapped file.
|
Modifier and Type | Method and Description |
---|---|
void |
addMetric(MetricName name,
Semantics semantics,
javax.measure.Unit<?> unit,
java.lang.Object initialValue)
Adds a new metric to the writer, with an initial default value.
|
<T> void |
addMetric(MetricName name,
Semantics semantics,
javax.measure.Unit<?> unit,
T initialValue,
TypeHandler<T> pcpType)
Adds a new metric to the writer, with an initial default value.
|
private void |
addMetricInfo(MetricName name,
Semantics semantics,
javax.measure.Unit<?> unit,
java.lang.Object initialValue,
TypeHandler<?> pcpType) |
private int |
getBufferLength() |
private int |
getByteSizeTotalFor(java.util.Collection<? extends PcpOffset> offsettables) |
private int |
getFlagMask() |
private InstanceDomain |
getInstanceDomain(java.lang.String name) |
private java.util.Collection<InstanceDomain> |
getInstanceDomains() |
private java.util.Collection<Instance> |
getInstances() |
private PcpMetricInfo |
getMetricInfo(java.lang.String name) |
private java.util.Collection<PcpMetricInfo> |
getMetricInfos() |
private int |
getProcessIdentifier() |
private java.util.Collection<PcpString> |
getStrings() |
private int |
getTocOffset(int tocIndex)
Calculates the file offset of a given PCP MMV TOC block
|
private java.util.Collection<PcpValueInfo> |
getValueInfos() |
private void |
initialiseOffsets() |
private int |
initializeOffsets(java.util.Collection<? extends PcpOffset> offsettables,
int nextOffset) |
static void |
main(java.lang.String[] args) |
private static java.io.File |
mmvFileFromName(java.lang.String name) |
private void |
populateDataBuffer(java.nio.ByteBuffer dataFileBuffer,
java.util.Collection<PcpValueInfo> valueInfos) |
private void |
preparePerMetricBufferSlices() |
<T> void |
registerType(java.lang.Class<T> runtimeClass,
TypeHandler<T> handler)
Registers a new
TypeHandler to be used to convert all subsequent values of type
runtimeClass |
void |
reset()
Prepares this object such that it can be restarted by invoking the
PcpWriter.start() method
again. |
void |
setClusterIdentifier(int clusterIdentifier) |
void |
setFlags(java.util.Set<PcpMmvWriter.MmvFlag> flags) |
void |
setInstanceDomainHelpText(java.lang.String instanceDomain,
java.lang.String shortHelpText,
java.lang.String longHelpText)
Sets the help text associated with an instance domain.
|
void |
setMetricHelpText(java.lang.String metricName,
java.lang.String shortHelpText,
java.lang.String longHelpText)
Sets the help text associated with a particular metric
|
void |
setPerMetricLock(boolean usePerMetricLock) |
void |
setProcessIdentifier(int pid) |
void |
start()
Starts the Writer, freezing the file format and writing out the metadata and initial values.
|
private int |
tocCount() |
java.lang.String |
toString() |
void |
updateMetric(MetricName name,
java.lang.Object value)
Updates the metric value of the given metric, once the writer has been started
|
private void |
updateValue(PcpValueInfo info,
java.lang.Object value) |
private void |
writeToc(java.nio.ByteBuffer dataFileBuffer,
PcpMmvWriter.TocType tocType,
int entryCount,
int firstEntryOffset)
Writes out a PCP MMV table-of-contents block.
|
private void |
writeValueWithGlobalLock(PcpValueInfo info,
java.lang.Object value,
TypeHandler rawHandler) |
private void |
writeValueWithLockPerMetric(PcpValueInfo info,
java.lang.Object value,
TypeHandler rawHandler) |
private static final java.util.Set<PcpMmvWriter.MmvFlag> DEFAULT_FLAGS
private static final int HEADER_LENGTH
private static final int TOC_LENGTH
static final java.nio.charset.Charset PCP_CHARSET
private static final byte[] TAG
static final int DATA_VALUE_LENGTH
private static final TypeHandler<java.lang.String> MMV_STRING_HANDLER
private final ByteBufferFactory byteBufferFactory
private final PcpMmvWriter.Store<PcpMetricInfo> metricInfoStore
private final PcpMmvWriter.Store<InstanceDomain> instanceDomainStore
private final MmvVersion mmvVersion
private final MetricNameValidator metricNameValidator
private final java.util.Map<MetricName,PcpValueInfo> metricData
private final java.util.Map<java.lang.Class<?>,TypeHandler<?>> typeHandlers
private final PcpString.PcpStringStore stringStore
private volatile boolean started
private volatile boolean usePerMetricLock
private final java.util.Map<PcpValueInfo,java.nio.ByteBuffer> perMetricByteBuffers
private final java.lang.Object globalLock
private volatile java.nio.ByteBuffer dataFileBuffer
private java.io.File file
private volatile int processIdentifier
private volatile int clusterIdentifier
private volatile java.util.Set<PcpMmvWriter.MmvFlag> flags
@Deprecated public PcpMmvWriter(java.io.File file)
IdentifierSourceSet
.PcpMmvWriter(File, IdentifierSourceSet)
public PcpMmvWriter(java.io.File file, IdentifierSourceSet identifierSources)
MmvVersion.MMV_VERSION1
formatfile
- the file to mapidentifierSources
- the sources to use for coming up with identifiers for new metrics etc.public PcpMmvWriter(java.io.File file, IdentifierSourceSet identifierSources, MmvVersion mmvVersion)
file
- the file to mapidentifierSources
- the sources to use for coming up with identifiers for new metrics etc.mmvVersion
- the MMV version format to usepublic PcpMmvWriter(ByteBufferFactory byteBufferFactory, IdentifierSourceSet identifierSources)
public PcpMmvWriter(ByteBufferFactory byteBufferFactory, IdentifierSourceSet identifierSources, MmvVersion mmvVersion)
public PcpMmvWriter(java.lang.String name, IdentifierSourceSet identifierSources)
MmvVersion.MMV_VERSION1
formatname
- logical name of instrumented subsystem (e.g. "hadoop")identifierSources
- the sources to use for coming up with identifiers for new metrics etc.public PcpMmvWriter(java.lang.String name, IdentifierSourceSet identifierSources, MmvVersion mmvVersion)
name
- logical name of instrumented subsystem (e.g. "hadoop")identifierSources
- the sources to use for coming up with identifiers for new metrics etc.mmvVersion
- the MMV version format to useprivate static java.io.File mmvFileFromName(java.lang.String name)
public final void addMetric(MetricName name, Semantics semantics, javax.measure.Unit<?> unit, java.lang.Object initialValue)
PcpWriter
TypeHandler
based on the runtime type of the initialValue parameter.addMetric
in interface PcpWriter
name
- the name of the metric to export. Must not exceed any byte-length limits specified
by the implementationsemantics
- the PCP semantics of the metricunit
- the unit used to measure the metric.initialValue
- the 'default' value to write into the file at initialisation timepublic final <T> void addMetric(MetricName name, Semantics semantics, javax.measure.Unit<?> unit, T initialValue, TypeHandler<T> pcpType)
PcpWriter
TypeHandler
based on the runtime type of the initialValue parameter.addMetric
in interface PcpWriter
name
- the name of the metric to export. Must not exceed any byte-length limits specified
by the implementationsemantics
- the PCP semantics of the metricunit
- the unit used to measure the metric.initialValue
- the 'default' value to write into the file at initialisation timepcpType
- the type converter to use to render the initial value (and all subsequent values)
to the PCP streampublic final <T> void registerType(java.lang.Class<T> runtimeClass, TypeHandler<T> handler)
PcpWriter
TypeHandler
to be used to convert all subsequent values of type
runtimeClassregisterType
in interface PcpWriter
runtimeClass
- the class to be converted by the new handlerhandler
- the handler to usepublic final void updateMetric(MetricName name, java.lang.Object value)
PcpWriter
updateMetric
in interface PcpWriter
name
- the metric to updatevalue
- the new value (must be convertible by the TypeHandler
used when adding the
metric)public final void start() throws java.io.IOException
PcpWriter
public void reset()
PcpWriter
PcpWriter.start()
method
again.public final void setInstanceDomainHelpText(java.lang.String instanceDomain, java.lang.String shortHelpText, java.lang.String longHelpText)
PcpWriter
setInstanceDomainHelpText
in interface PcpWriter
instanceDomain
- Java pseudo-instance domain identifier (i.e. metric prefix; for
animals.dog[xxx].size this is the 'animals.dog' part)shortHelpText
- the short help text; must not exceed any length limits specified by the
implementationlongHelpText
- the long explanatory text; must not exceed any length limits specified by the
implementationpublic final void setMetricHelpText(java.lang.String metricName, java.lang.String shortHelpText, java.lang.String longHelpText)
PcpWriter
setMetricHelpText
in interface PcpWriter
metricName
- String version of the metric name, ignoring any possible instance domains. (e.g. for
animals.dog[xxx].size this is 'animals.dog.size')shortHelpText
- the short help text; must not exceed any length limits specified by the
implementationlongHelpText
- the long explanatory text; must not exceed any length limits specified by the
implementationprivate void preparePerMetricBufferSlices()
public void setClusterIdentifier(int clusterIdentifier)
public void setProcessIdentifier(int pid)
public void setPerMetricLock(boolean usePerMetricLock)
public void setFlags(java.util.Set<PcpMmvWriter.MmvFlag> flags)
private void addMetricInfo(MetricName name, Semantics semantics, javax.measure.Unit<?> unit, java.lang.Object initialValue, TypeHandler<?> pcpType)
private PcpMetricInfo getMetricInfo(java.lang.String name)
private InstanceDomain getInstanceDomain(java.lang.String name)
private java.util.Collection<PcpMetricInfo> getMetricInfos()
private java.util.Collection<InstanceDomain> getInstanceDomains()
private java.util.Collection<Instance> getInstances()
private java.util.Collection<PcpValueInfo> getValueInfos()
private java.util.Collection<PcpString> getStrings()
private void updateValue(PcpValueInfo info, java.lang.Object value)
private void writeValueWithLockPerMetric(PcpValueInfo info, java.lang.Object value, TypeHandler rawHandler)
private void writeValueWithGlobalLock(PcpValueInfo info, java.lang.Object value, TypeHandler rawHandler)
private void populateDataBuffer(java.nio.ByteBuffer dataFileBuffer, java.util.Collection<PcpValueInfo> valueInfos) throws java.io.IOException
java.io.IOException
private int getFlagMask()
private int getBufferLength()
private int getByteSizeTotalFor(java.util.Collection<? extends PcpOffset> offsettables)
private void writeToc(java.nio.ByteBuffer dataFileBuffer, PcpMmvWriter.TocType tocType, int entryCount, int firstEntryOffset)
dataFileBuffer
- ByteBuffer positioned at the correct offset in the file for the blocktocType
- the type of TOC block to writeentryCount
- the number of blocks of type tocType to be found in the filefirstEntryOffset
- the offset of the first tocType block, relative to start of the fileprivate int getTocOffset(int tocIndex)
tocIndex
- the 0-based index of the TOC block to be writtenprivate void initialiseOffsets()
private int initializeOffsets(java.util.Collection<? extends PcpOffset> offsettables, int nextOffset)
private int tocCount()
private int getProcessIdentifier()
public static void main(java.lang.String[] args) throws java.io.IOException
java.io.IOException
public java.lang.String toString()
toString
in class java.lang.Object