use of co.cask.cdap.common.utils.ImmutablePair in project cdap by caskdata.
the class ServiceLifeCycleTestRun method getStates.
/**
* Returns the handler state change as a Multimap. The key is the hashcode of the handler instance,
* the value is a list of state changes for that handler instance.
*/
private Multimap<Integer, String> getStates(ServiceManager serviceManager) throws Exception {
URL url = serviceManager.getServiceURL(10, TimeUnit.SECONDS).toURI().resolve("states").toURL();
Multimap<Integer, String> result = LinkedListMultimap.create();
try (InputStream is = url.openConnection().getInputStream()) {
List<ImmutablePair<Integer, String>> states = GSON.fromJson(new InputStreamReader(is, Charsets.UTF_8), STATES_TYPE);
for (ImmutablePair<Integer, String> pair : states) {
result.put(pair.getFirst(), pair.getSecond());
}
}
return result;
}
use of co.cask.cdap.common.utils.ImmutablePair in project cdap by caskdata.
the class ArtifactRepository method validatePluginSet.
/**
* Validates the set of plugins for an artifact. Checks that the pair of plugin type and name are unique among
* all plugins in an artifact.
*
* @param plugins the set of plugins to validate
* @throws InvalidArtifactException if there is more than one class with the same type and name
*/
@VisibleForTesting
static void validatePluginSet(Set<PluginClass> plugins) throws InvalidArtifactException {
boolean isInvalid = false;
StringBuilder errMsg = new StringBuilder("Invalid plugins field.");
Set<ImmutablePair<String, String>> existingPlugins = new HashSet<>();
Set<ImmutablePair<String, String>> dupes = new HashSet<>();
for (PluginClass plugin : plugins) {
ImmutablePair<String, String> typeAndName = ImmutablePair.of(plugin.getType(), plugin.getName());
if (!existingPlugins.add(typeAndName) && !dupes.contains(typeAndName)) {
errMsg.append(" Only one plugin with type '");
errMsg.append(typeAndName.getFirst());
errMsg.append("' and name '");
errMsg.append(typeAndName.getSecond());
errMsg.append("' can be present.");
dupes.add(typeAndName);
isInvalid = true;
}
}
// "Invalid plugins. Only one plugin with type 'source' and name 'table' can be present."
if (isInvalid) {
throw new InvalidArtifactException(errMsg.toString());
}
}
use of co.cask.cdap.common.utils.ImmutablePair in project cdap by caskdata.
the class MetadataDataset method getMetadata.
/**
* Returns metadata for a given set of entities
*
* @param targetIds entities for which metadata is required
* @return map of entitiyId to set of metadata for that entity
*/
public Set<Metadata> getMetadata(Set<? extends NamespacedEntityId> targetIds) {
if (targetIds.isEmpty()) {
return Collections.emptySet();
}
List<ImmutablePair<byte[], byte[]>> fuzzyKeys = new ArrayList<>(targetIds.size());
for (NamespacedEntityId targetId : targetIds) {
fuzzyKeys.add(getFuzzyKeyFor(targetId));
}
// Sort fuzzy keys
Collections.sort(fuzzyKeys, FUZZY_KEY_COMPARATOR);
// Scan using fuzzy filter. Scan returns one row per property.
// Group the rows on namespacedId
Multimap<NamespacedEntityId, MetadataEntry> metadataMap = HashMultimap.create();
byte[] start = fuzzyKeys.get(0).getFirst();
byte[] end = Bytes.stopKeyForPrefix(fuzzyKeys.get(fuzzyKeys.size() - 1).getFirst());
try (Scanner scan = indexedTable.scan(new Scan(start, end, new FuzzyRowFilter(fuzzyKeys)))) {
Row next;
while ((next = scan.next()) != null) {
MetadataEntry metadataEntry = convertRow(next);
if (metadataEntry != null) {
metadataMap.put(metadataEntry.getTargetId(), metadataEntry);
}
}
}
// Create metadata objects for each entity from grouped rows
Set<Metadata> metadataSet = new HashSet<>();
for (Map.Entry<NamespacedEntityId, Collection<MetadataEntry>> entry : metadataMap.asMap().entrySet()) {
Map<String, String> properties = new HashMap<>();
Set<String> tags = Collections.emptySet();
for (MetadataEntry metadataEntry : entry.getValue()) {
if (TAGS_KEY.equals(metadataEntry.getKey())) {
tags = splitTags(metadataEntry.getValue());
} else {
properties.put(metadataEntry.getKey(), metadataEntry.getValue());
}
}
metadataSet.add(new Metadata(entry.getKey(), properties, tags));
}
return metadataSet;
}
use of co.cask.cdap.common.utils.ImmutablePair in project cdap by caskdata.
the class MetadataDataset method getFuzzyKeyFor.
private ImmutablePair<byte[], byte[]> getFuzzyKeyFor(NamespacedEntityId targetId) {
// We need to create fuzzy pairs to match the first part of the key containing targetId
MDSKey mdsKey = MdsKey.getMDSValueKey(targetId, null);
byte[] keyBytes = mdsKey.getKey();
// byte array is automatically initialized to 0, which implies fixed match in fuzzy info
// the row key after targetId doesn't need to be a match.
// Workaround for HBASE-15676, need to have at least one 1 in the fuzzy filter
byte[] infoBytes = new byte[keyBytes.length + 1];
infoBytes[infoBytes.length - 1] = 1;
// the key array size and mask array size has to be equal so increase the size by 1
return new ImmutablePair<>(Bytes.concat(keyBytes, new byte[1]), infoBytes);
}
use of co.cask.cdap.common.utils.ImmutablePair in project cdap by caskdata.
the class FactCodecTest method test.
@Test
public void test() {
InMemoryTableService.create("FactCodecTest");
MetricsTable table = new InMemoryMetricsTable("FactCodecTest");
int resolution = 10;
int rollTimebaseInterval = 2;
FactCodec codec = new FactCodec(new EntityTable(table), resolution, rollTimebaseInterval);
// testing encoding with multiple dimensions
List<DimensionValue> dimensionValues = ImmutableList.of(new DimensionValue("dimension1", "value1"), new DimensionValue("dimension2", "value2"), new DimensionValue("dimension3", "value3"));
// note: we use seconds everywhere and rely on this
long ts = 1422312915;
byte[] rowKey = codec.createRowKey(dimensionValues, "myMetric", ts);
byte[] column = codec.createColumn(ts);
Assert.assertEquals((ts / resolution) * resolution, codec.getTimestamp(rowKey, column));
Assert.assertEquals(dimensionValues, codec.getDimensionValues(rowKey));
Assert.assertEquals("myMetric", codec.getMeasureName(rowKey));
// testing encoding without one dimension
dimensionValues = ImmutableList.of(new DimensionValue("myTag", "myValue"));
rowKey = codec.createRowKey(dimensionValues, "mySingleTagMetric", ts);
Assert.assertEquals((ts / resolution) * resolution, codec.getTimestamp(rowKey, column));
Assert.assertEquals(dimensionValues, codec.getDimensionValues(rowKey));
Assert.assertEquals("mySingleTagMetric", codec.getMeasureName(rowKey));
// testing encoding without empty dimensions
rowKey = codec.createRowKey(new ArrayList<DimensionValue>(), "myNoTagsMetric", ts);
Assert.assertEquals((ts / resolution) * resolution, codec.getTimestamp(rowKey, column));
Assert.assertEquals(new ArrayList<DimensionValue>(), codec.getDimensionValues(rowKey));
Assert.assertEquals("myNoTagsMetric", codec.getMeasureName(rowKey));
// testing null metric
dimensionValues = ImmutableList.of(new DimensionValue("myTag", "myValue"));
rowKey = codec.createRowKey(dimensionValues, "mySingleTagMetric", ts);
Assert.assertEquals((ts / resolution) * resolution, codec.getTimestamp(rowKey, column));
Assert.assertEquals(dimensionValues, codec.getDimensionValues(rowKey));
Assert.assertEquals("mySingleTagMetric", codec.getMeasureName(rowKey));
// testing null value
dimensionValues = ImmutableList.of(new DimensionValue("dimension1", "value1"), new DimensionValue("dimension2", null), new DimensionValue("dimension3", "value3"));
rowKey = codec.createRowKey(dimensionValues, "myNullTagMetric", ts);
Assert.assertEquals((ts / resolution) * resolution, codec.getTimestamp(rowKey, column));
Assert.assertEquals(dimensionValues, codec.getDimensionValues(rowKey));
Assert.assertEquals("myNullTagMetric", codec.getMeasureName(rowKey));
// testing fuzzy mask for fuzzy stuff in row key
dimensionValues = ImmutableList.of(new DimensionValue("dimension1", "value1"), // any value is accepted
new DimensionValue("dimension2", null), new DimensionValue("dimension3", "value3"));
byte[] mask = codec.createFuzzyRowMask(dimensionValues, "myMetric");
rowKey = codec.createRowKey(dimensionValues, "myMetric", ts);
FuzzyRowFilter filter = new FuzzyRowFilter(ImmutableList.of(new ImmutablePair<>(rowKey, mask)));
dimensionValues = ImmutableList.of(new DimensionValue("dimension1", "value1"), new DimensionValue("dimension2", "annnnnnnnnny"), new DimensionValue("dimension3", "value3"));
rowKey = codec.createRowKey(dimensionValues, "myMetric", ts);
Assert.assertEquals(FuzzyRowFilter.ReturnCode.INCLUDE, filter.filterRow(rowKey));
dimensionValues = ImmutableList.of(new DimensionValue("dimension1", "value12"), new DimensionValue("dimension2", "value2"), new DimensionValue("dimension3", "value3"));
rowKey = codec.createRowKey(dimensionValues, "myMetric", ts);
Assert.assertTrue(FuzzyRowFilter.ReturnCode.INCLUDE != filter.filterRow(rowKey));
dimensionValues = ImmutableList.of(new DimensionValue("dimension1", "value1"), new DimensionValue("dimension2", "value2"), new DimensionValue("dimension3", "value13"));
rowKey = codec.createRowKey(dimensionValues, "myMetric", ts);
Assert.assertTrue(FuzzyRowFilter.ReturnCode.INCLUDE != filter.filterRow(rowKey));
dimensionValues = ImmutableList.of(new DimensionValue("dimension1", "value1"), new DimensionValue("dimension3", "value3"));
rowKey = codec.createRowKey(dimensionValues, "myMetric", ts);
Assert.assertTrue(FuzzyRowFilter.ReturnCode.INCLUDE != filter.filterRow(rowKey));
// fuzzy in value should match the "null" value
dimensionValues = ImmutableList.of(new DimensionValue("dimension1", "value1"), new DimensionValue("dimension2", null), new DimensionValue("dimension3", "value3"));
rowKey = codec.createRowKey(dimensionValues, "myMetric", ts);
Assert.assertEquals(FuzzyRowFilter.ReturnCode.INCLUDE, filter.filterRow(rowKey));
dimensionValues = ImmutableList.of(new DimensionValue("dimension1", "value1"), new DimensionValue("dimension2", "value2"), new DimensionValue("dimension3", "value3"));
rowKey = codec.createRowKey(dimensionValues, "myMetric2", ts);
Assert.assertTrue(FuzzyRowFilter.ReturnCode.INCLUDE != filter.filterRow(rowKey));
rowKey = codec.createRowKey(dimensionValues, null, ts);
Assert.assertTrue(FuzzyRowFilter.ReturnCode.INCLUDE != filter.filterRow(rowKey));
rowKey = codec.createRowKey(new ArrayList<DimensionValue>(), "myMetric", ts);
Assert.assertTrue(FuzzyRowFilter.ReturnCode.INCLUDE != filter.filterRow(rowKey));
// testing fuzzy mask for fuzzy metric
dimensionValues = ImmutableList.of(new DimensionValue("myTag", "myValue"));
rowKey = codec.createRowKey(dimensionValues, null, ts);
mask = codec.createFuzzyRowMask(dimensionValues, null);
filter = new FuzzyRowFilter(ImmutableList.of(new ImmutablePair<>(rowKey, mask)));
rowKey = codec.createRowKey(dimensionValues, "annyyy", ts);
Assert.assertEquals(FuzzyRowFilter.ReturnCode.INCLUDE, filter.filterRow(rowKey));
rowKey = codec.createRowKey(dimensionValues, "zzzzzzzzzzzz", ts);
Assert.assertEquals(FuzzyRowFilter.ReturnCode.INCLUDE, filter.filterRow(rowKey));
dimensionValues = ImmutableList.of(new DimensionValue("myTag", "myValue2"));
rowKey = codec.createRowKey(dimensionValues, "metric", ts);
Assert.assertTrue(FuzzyRowFilter.ReturnCode.INCLUDE != filter.filterRow(rowKey));
// todo: test prefix of multi dimension valued row key is not same one dimension valued row key
// todo: test that rollTimebaseInterval applies well
}
Aggregations