use of io.cdap.cdap.spi.metadata.MetadataMutation.Remove in project cdap by caskdata.
the class MetadataStorageTest method testAsyncMutations.
@Test
public void testAsyncMutations() throws Exception {
MetadataStorage mds = getMetadataStorage();
MetadataEntity entity = ofDataset(DEFAULT_NAMESPACE, "entity");
// get metadata for non-existing entity
verifyMetadataAsync(mds, entity, Metadata.EMPTY);
// drop metadata for non-existing entity succeeds
MetadataChange change = mds.apply(new Drop(entity), ASYNC);
Assert.assertEquals(new MetadataChange(entity, Metadata.EMPTY, Metadata.EMPTY), change);
verifyMetadataAsync(mds, entity, Metadata.EMPTY);
// remove metadata for non-existing entity succeeds
mds.apply(new Remove(entity, ImmutableSet.of(new ScopedNameOfKind(MetadataKind.TAG, SYSTEM, "st1"), new ScopedNameOfKind(MetadataKind.TAG, USER, "ut1"), new ScopedNameOfKind(PROPERTY, SYSTEM, "sp2"), new ScopedNameOfKind(PROPERTY, USER, "up2"))), ASYNC);
Assert.assertEquals(new MetadataChange(entity, Metadata.EMPTY, Metadata.EMPTY), change);
verifyMetadataAsync(mds, entity, Metadata.EMPTY);
// update metadata for non-existing entity creates it
Metadata metadata = new Metadata(ImmutableSet.of(new ScopedName(SYSTEM, "a"), new ScopedName(USER, "b")), ImmutableMap.of(new ScopedName(SYSTEM, "p"), "v", new ScopedName(USER, "k"), "v1"));
change = mds.apply(new Update(entity, metadata), ASYNC);
Assert.assertEquals(new MetadataChange(entity, Metadata.EMPTY, metadata), change);
verifyMetadataAsync(mds, entity, metadata);
// test that update is idempotent
change = mds.apply(new Update(entity, metadata), ASYNC);
Assert.assertEquals(new MetadataChange(entity, metadata, metadata), change);
verifyMetadataAsync(mds, entity, metadata);
// create metadata replaces existing metadata
Metadata previousMetadata = metadata;
metadata = new Metadata(ImmutableSet.of(new ScopedName(SYSTEM, "st1"), new ScopedName(SYSTEM, "st2"), new ScopedName(USER, "ut1")), ImmutableMap.of(new ScopedName(SYSTEM, "sp1"), "sv1", new ScopedName(SYSTEM, "sp2"), "sv2", new ScopedName(USER, "up1"), "uv1", new ScopedName(USER, "up2"), "uv2"));
MetadataMutation create = new Create(entity, metadata, Collections.emptyMap());
change = mds.apply(create, ASYNC);
Assert.assertEquals(new MetadataChange(entity, previousMetadata, metadata), change);
// verify the metadata with variations of scope and kind
verifyMetadataAsync(mds, entity, metadata);
// verify the metadata with a select subset of tags and properties
verifyMetadataSelectionAsync(mds, entity, metadata, ImmutableSet.of(new ScopedNameOfKind(PROPERTY, SYSTEM, "sp1"), new ScopedNameOfKind(PROPERTY, SYSTEM, "nosuch"), new ScopedNameOfKind(PROPERTY, USER, "up2"), new ScopedNameOfKind(PROPERTY, USER, "nosuch"), new ScopedNameOfKind(MetadataKind.TAG, SYSTEM, "st1"), new ScopedNameOfKind(MetadataKind.TAG, SYSTEM, "nosuch"), new ScopedNameOfKind(MetadataKind.TAG, USER, "ut1"), new ScopedNameOfKind(MetadataKind.TAG, USER, "nosuch")));
// verify that a non-matching set tags and properties returns empty metadata
verifyMetadataSelectionAsync(mds, entity, metadata, ImmutableSet.of(new ScopedNameOfKind(PROPERTY, SYSTEM, "nosuch"), new ScopedNameOfKind(MetadataKind.TAG, USER, "nosuch")));
// replace the system metadata with directives, user metadata should remain unchanged
Metadata recreatedMetadata = new Metadata(ImmutableSet.of(new ScopedName(SYSTEM, "nst0")), ImmutableMap.of(new ScopedName(SYSTEM, "sp1"), "nsv1", new ScopedName(SYSTEM, "nsp0"), "sv0"));
MetadataMutation recreate = new Create(entity, recreatedMetadata, ImmutableMap.of(new ScopedNameOfKind(MetadataKind.TAG, SYSTEM, "st1"), MetadataDirective.KEEP, new ScopedNameOfKind(MetadataKind.TAG, SYSTEM, "st2"), MetadataDirective.PRESERVE, new ScopedNameOfKind(PROPERTY, SYSTEM, "sp1"), MetadataDirective.PRESERVE, new ScopedNameOfKind(PROPERTY, SYSTEM, "sp2"), MetadataDirective.KEEP));
previousMetadata = metadata;
// this is the new metadata according to directives
metadata = new Metadata(ImmutableSet.of(new ScopedName(SYSTEM, "st1"), new ScopedName(SYSTEM, "st2"), new ScopedName(SYSTEM, "nst0"), new ScopedName(USER, "ut1")), ImmutableMap.of(new ScopedName(SYSTEM, "sp1"), "sv1", new ScopedName(SYSTEM, "sp2"), "sv2", new ScopedName(SYSTEM, "nsp0"), "sv0", new ScopedName(USER, "up1"), "uv1", new ScopedName(USER, "up2"), "uv2"));
change = mds.apply(recreate, ASYNC);
Assert.assertEquals(new MetadataChange(entity, previousMetadata, metadata), change);
verifyMetadataAsync(mds, entity, metadata);
// replace the metadata with directives
recreatedMetadata = new Metadata(ImmutableSet.of(new ScopedName(SYSTEM, "nst1"), new ScopedName(USER, "nut1")), ImmutableMap.of(new ScopedName(SYSTEM, "sp1"), "nsv1", new ScopedName(SYSTEM, "nsp2"), "sv2", new ScopedName(USER, "up3"), "uv3"));
recreate = new Create(entity, recreatedMetadata, ImmutableMap.of(new ScopedNameOfKind(MetadataKind.TAG, SYSTEM, "st1"), MetadataDirective.KEEP, new ScopedNameOfKind(MetadataKind.TAG, SYSTEM, "st2"), MetadataDirective.PRESERVE, new ScopedNameOfKind(PROPERTY, SYSTEM, "sp1"), MetadataDirective.PRESERVE, new ScopedNameOfKind(PROPERTY, SYSTEM, "sp2"), MetadataDirective.KEEP, new ScopedNameOfKind(PROPERTY, USER, "up2"), MetadataDirective.PRESERVE));
previousMetadata = metadata;
// this is the new metadata according to directives
metadata = new Metadata(ImmutableSet.of(new ScopedName(SYSTEM, "st1"), new ScopedName(SYSTEM, "st2"), new ScopedName(SYSTEM, "nst1"), new ScopedName(USER, "nut1")), ImmutableMap.of(new ScopedName(SYSTEM, "sp1"), "sv1", new ScopedName(SYSTEM, "sp2"), "sv2", new ScopedName(SYSTEM, "nsp2"), "sv2", new ScopedName(USER, "up2"), "uv2", new ScopedName(USER, "up3"), "uv3"));
change = mds.apply(recreate, ASYNC);
Assert.assertEquals(new MetadataChange(entity, previousMetadata, metadata), change);
verifyMetadataAsync(mds, entity, metadata);
// update some tags and properties
MetadataMutation update = new Update(entity, new Metadata(ImmutableSet.of(new ScopedName(SYSTEM, "ast1"), new ScopedName(USER, "aut1")), ImmutableMap.of(new ScopedName(SYSTEM, "sp1"), "nsv1", new ScopedName(SYSTEM, "nsp2"), "nsv2", new ScopedName(USER, "up2"), "nuv2", new ScopedName(USER, "up3"), "uv3")));
// verify new metadata after update
previousMetadata = metadata;
metadata = new Metadata(ImmutableSet.of(new ScopedName(SYSTEM, "ast1"), new ScopedName(SYSTEM, "st1"), new ScopedName(SYSTEM, "st2"), new ScopedName(SYSTEM, "nst1"), new ScopedName(USER, "aut1"), new ScopedName(USER, "nut1")), ImmutableMap.of(new ScopedName(SYSTEM, "sp1"), "nsv1", new ScopedName(SYSTEM, "sp2"), "sv2", new ScopedName(SYSTEM, "nsp2"), "nsv2", new ScopedName(USER, "up2"), "nuv2", new ScopedName(USER, "up3"), "uv3"));
change = mds.apply(update, ASYNC);
Assert.assertEquals(new MetadataChange(entity, previousMetadata, metadata), change);
verifyMetadataAsync(mds, entity, metadata);
// test that update is idempotent
change = mds.apply(update, ASYNC);
Assert.assertEquals(new MetadataChange(entity, metadata, metadata), change);
verifyMetadataAsync(mds, entity, metadata);
// remove some tags and properties
MetadataMutation remove = new Remove(entity, ImmutableSet.of(new ScopedNameOfKind(MetadataKind.TAG, SYSTEM, "st1"), new ScopedNameOfKind(MetadataKind.TAG, SYSTEM, "st2"), new ScopedNameOfKind(MetadataKind.TAG, USER, "nut1"), new ScopedNameOfKind(MetadataKind.TAG, USER, "nosuch"), new ScopedNameOfKind(PROPERTY, SYSTEM, "sp2"), new ScopedNameOfKind(PROPERTY, SYSTEM, "nsp2"), new ScopedNameOfKind(PROPERTY, USER, "up2")));
previousMetadata = metadata;
metadata = new Metadata(ImmutableSet.of(new ScopedName(SYSTEM, "ast1"), new ScopedName(SYSTEM, "nst1"), new ScopedName(USER, "aut1")), ImmutableMap.of(new ScopedName(SYSTEM, "sp1"), "nsv1", new ScopedName(USER, "up3"), "uv3"));
change = mds.apply(remove, ASYNC);
Assert.assertEquals(new MetadataChange(entity, previousMetadata, metadata), change);
verifyMetadataAsync(mds, entity, metadata);
// test that remove is idemtpotent
change = mds.apply(remove, ASYNC);
Assert.assertEquals(new MetadataChange(entity, metadata, metadata), change);
verifyMetadataAsync(mds, entity, metadata);
// drop all metadata for the entity
change = mds.apply(new Drop(entity), ASYNC);
Assert.assertEquals(new MetadataChange(entity, metadata, Metadata.EMPTY), change);
verifyMetadataAsync(mds, entity, Metadata.EMPTY);
// drop is idempotent
change = mds.apply(new Drop(entity), ASYNC);
Assert.assertEquals(new MetadataChange(entity, Metadata.EMPTY, Metadata.EMPTY), change);
verifyMetadataAsync(mds, entity, Metadata.EMPTY);
}
use of io.cdap.cdap.spi.metadata.MetadataMutation.Remove in project cdap by caskdata.
the class MetadataStorageTest method testBatchConcurrency.
@Test
public void testBatchConcurrency() throws IOException {
// T threads
int numThreads = 10;
// N entities
int numEntities = 20;
MetadataStorage mds = getMetadataStorage();
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
CompletionService<List<MetadataChange>> completionService = new ExecutorCompletionService<>(executor);
// Set up N entities with T tags (one to be removed per thread)
Set<String> allRTags = IntStream.range(0, numThreads).mapToObj(t -> "r" + t).collect(Collectors.toSet());
Map<Integer, MetadataEntity> entities = IntStream.range(0, numEntities).boxed().collect(Collectors.toMap(i -> i, i -> MetadataEntity.ofDataset("myds" + i)));
mds.batch(entities.values().stream().map(entity -> new Update(entity, new Metadata(USER, allRTags))).collect(Collectors.toList()), MutationOptions.DEFAULT);
// Generate a random but conflicting set of batches of mutations, one for each thread.
// Construct the expected results for each entity along with the mutations
// Also, because some threads will perform a Create, create a set of directives to preserve all other tags
Map<Integer, Set<String>> expected = IntStream.range(0, numEntities).boxed().collect(Collectors.toMap(i -> i, i -> new HashSet<>(allRTags)));
Map<Integer, List<MetadataMutation>> mutations = IntStream.range(0, numThreads).boxed().collect(Collectors.toMap(i -> i, i -> new ArrayList<>()));
Map<ScopedNameOfKind, MetadataDirective> directives = new HashMap<>();
Random rand = new Random(System.currentTimeMillis());
IntStream.range(0, numThreads).forEach(t -> {
directives.put(new ScopedNameOfKind(TAG, USER, "t" + t), MetadataDirective.KEEP);
directives.put(new ScopedNameOfKind(TAG, USER, "r" + t), MetadataDirective.KEEP);
directives.put(new ScopedNameOfKind(TAG, USER, "c" + t), MetadataDirective.KEEP);
IntStream.range(0, numEntities).forEach(e -> {
int random = rand.nextInt(100);
if (random < 30) {
expected.get(e).add("t" + t);
mutations.get(t).add(new Update(entities.get(e), new Metadata(USER, tags("t" + t))));
} else if (random < 60) {
expected.get(e).add("c" + t);
mutations.get(t).add(new Create(entities.get(e), new Metadata(USER, tags("c" + t)), directives));
} else if (random < 90) {
expected.get(e).remove("r" + t);
mutations.get(t).add(new Remove(entities.get(e), Collections.singleton(new ScopedNameOfKind(TAG, USER, "r" + t))));
}
});
});
// submit all tasks and wait for their completion
IntStream.range(0, numThreads).forEach(t -> completionService.submit(() -> mds.batch(mutations.get(t), MutationOptions.DEFAULT)));
IntStream.range(0, numThreads).forEach(t -> {
try {
completionService.take();
} catch (InterruptedException e) {
throw Throwables.propagate(e);
}
});
// validate that all "r" tags were removed and all "c" and "t" tags were added
IntStream.range(0, numEntities).forEach(e -> {
try {
Assert.assertEquals("For entity " + entities.get(e), expected.get(e), mds.read(new Read(entities.get(e))).getTags(USER));
} catch (Exception ex) {
throw Throwables.propagate(ex);
}
});
// clean up
mds.batch(entities.values().stream().map(Drop::new).collect(Collectors.toList()), MutationOptions.DEFAULT);
}
use of io.cdap.cdap.spi.metadata.MetadataMutation.Remove in project cdap by caskdata.
the class MetadataStorageTest method testUpdateRemove.
@Test
public void testUpdateRemove() throws IOException {
MetadataStorage mds = getMetadataStorage();
MetadataEntity entity = ofDataset(DEFAULT_NAMESPACE, "entity");
// get metadata for non-existing entity
verifyMetadata(mds, entity, Metadata.EMPTY);
// add some metadata for the entity
Metadata metadata = new Metadata(ImmutableSet.of(new ScopedName(SYSTEM, "sysTag"), new ScopedName(USER, "userTag")), ImmutableMap.of(new ScopedName(SYSTEM, "sysProp"), "sysVal", new ScopedName(USER, "userProp"), "userVal"));
MetadataChange change = mds.apply(new Update(entity, metadata), MutationOptions.DEFAULT);
Assert.assertEquals(new MetadataChange(entity, Metadata.EMPTY, metadata), change);
verifyMetadata(mds, entity, metadata);
// remove everything
change = mds.apply(new Remove(entity), MutationOptions.DEFAULT);
Assert.assertEquals(new MetadataChange(entity, metadata, Metadata.EMPTY), change);
verifyMetadata(mds, entity, Metadata.EMPTY);
// add back all metadata, then remove everything in user scope
change = mds.apply(new Update(entity, metadata), MutationOptions.DEFAULT);
Assert.assertEquals(new MetadataChange(entity, Metadata.EMPTY, metadata), change);
verifyMetadata(mds, entity, metadata);
change = mds.apply(new Remove(entity, USER), MutationOptions.DEFAULT);
Metadata newMetadata = filterBy(metadata, SYSTEM, null);
Assert.assertEquals(new MetadataChange(entity, metadata, newMetadata), change);
verifyMetadata(mds, entity, newMetadata);
// add back all metadata, then remove everything in system scope
change = mds.apply(new Update(entity, metadata), MutationOptions.DEFAULT);
Assert.assertEquals(new MetadataChange(entity, newMetadata, metadata), change);
verifyMetadata(mds, entity, metadata);
change = mds.apply(new Remove(entity, SYSTEM), MutationOptions.DEFAULT);
newMetadata = filterBy(metadata, USER, null);
Assert.assertEquals(new MetadataChange(entity, metadata, newMetadata), change);
verifyMetadata(mds, entity, newMetadata);
// add back all metadata, then remove all tags
change = mds.apply(new Update(entity, metadata), MutationOptions.DEFAULT);
Assert.assertEquals(new MetadataChange(entity, newMetadata, metadata), change);
verifyMetadata(mds, entity, metadata);
change = mds.apply(new Remove(entity, TAG), MutationOptions.DEFAULT);
newMetadata = filterBy(metadata, null, PROPERTY);
Assert.assertEquals(new MetadataChange(entity, metadata, newMetadata), change);
verifyMetadata(mds, entity, newMetadata);
// add back all metadata, then remove all properties
change = mds.apply(new Update(entity, metadata), MutationOptions.DEFAULT);
Assert.assertEquals(new MetadataChange(entity, newMetadata, metadata), change);
verifyMetadata(mds, entity, metadata);
change = mds.apply(new Remove(entity, PROPERTY), MutationOptions.DEFAULT);
newMetadata = filterBy(metadata, null, TAG);
Assert.assertEquals(new MetadataChange(entity, metadata, newMetadata), change);
verifyMetadata(mds, entity, newMetadata);
// add back all metadata, then remove all properties in system scope
change = mds.apply(new Update(entity, metadata), MutationOptions.DEFAULT);
Assert.assertEquals(new MetadataChange(entity, newMetadata, metadata), change);
verifyMetadata(mds, entity, metadata);
change = mds.apply(new Remove(entity, SYSTEM, PROPERTY), MutationOptions.DEFAULT);
newMetadata = union(filterBy(metadata, SYSTEM, TAG), filterBy(metadata, USER, null));
Assert.assertEquals(new MetadataChange(entity, metadata, newMetadata), change);
verifyMetadata(mds, entity, newMetadata);
// add back all metadata, then remove all tags in user scope
change = mds.apply(new Update(entity, metadata), MutationOptions.DEFAULT);
Assert.assertEquals(new MetadataChange(entity, newMetadata, metadata), change);
verifyMetadata(mds, entity, metadata);
change = mds.apply(new Remove(entity, USER, TAG), MutationOptions.DEFAULT);
newMetadata = union(filterBy(metadata, USER, PROPERTY), filterBy(metadata, SYSTEM, null));
Assert.assertEquals(new MetadataChange(entity, metadata, newMetadata), change);
verifyMetadata(mds, entity, newMetadata);
// clean up
mds.apply(new Drop(entity), MutationOptions.DEFAULT);
}
use of io.cdap.cdap.spi.metadata.MetadataMutation.Remove in project cdap by caskdata.
the class MetadataStorageTest method testUpdateSearch.
@Test
public void testUpdateSearch() throws IOException {
MetadataStorage mds = getMetadataStorage();
String ns = "ns";
MetadataEntity program = ofWorker(ofApp(ns, "app1"), "wk1");
Metadata meta = new Metadata(USER, tags("tag1", "tag2"), props("key1", "value1", "key2", "value2"));
MetadataRecord programRecord = new MetadataRecord(program, meta);
mds.apply(new Update(program, meta), MutationOptions.DEFAULT);
assertResults(mds, SearchRequest.of("value1").addNamespace(ns).build(), programRecord);
assertResults(mds, SearchRequest.of("value2").addNamespace(ns).build(), programRecord);
assertResults(mds, SearchRequest.of("tag2").addNamespace(ns).build(), programRecord);
mds.apply(new Update(program, new Metadata(USER, props("key1", "value3"))), MutationOptions.DEFAULT);
mds.apply(new Remove(program, ImmutableSet.of(new ScopedNameOfKind(PROPERTY, USER, "key2"), new ScopedNameOfKind(TAG, USER, "tag2"))), MutationOptions.DEFAULT);
programRecord = new MetadataRecord(program, new Metadata(USER, tags("tag1"), props("key1", "value3")));
// Searching for value1 should be empty
assertEmpty(mds, SearchRequest.of("value1").addNamespace(ns).build());
// Instead key1 has value value3 now
assertResults(mds, SearchRequest.of("value3").addNamespace(ns).build(), programRecord);
// key2 and tag2 were deleted
assertEmpty(mds, SearchRequest.of("value2").addNamespace(ns).build());
assertEmpty(mds, SearchRequest.of("tag2").addNamespace(ns).build());
// tag1 is still here
assertResults(mds, SearchRequest.of("tag1").addNamespace(ns).build(), programRecord);
// clean up
mds.apply(new Drop(program), MutationOptions.DEFAULT);
}
use of io.cdap.cdap.spi.metadata.MetadataMutation.Remove in project cdap by caskdata.
the class MetadataStorageTest method testSearchOnKeyValue.
@Test
public void testSearchOnKeyValue() throws Exception {
MetadataStorage mds = getMetadataStorage();
MetadataEntity program = ofWorker(ofApp("ns1", "app1"), "wk1");
MetadataEntity dataset = ofDataset("ns1", "ds2");
// add properties for program and dataset
final String multiWordValue = "aV1 av2 , - , av3 - av4_av5 av6";
MetadataRecord programRecord = new MetadataRecord(program, new Metadata(USER, props("key1", "value1", "key2", "value2", "multiword", multiWordValue)));
MetadataRecord datasetRecord = new MetadataRecord(dataset, new Metadata(ImmutableSet.of(), ImmutableMap.of(new ScopedName(SYSTEM, "sKey1"), "sValue1", new ScopedName(USER, "Key1"), "Value1")));
mds.batch(batch(new Update(program, programRecord.getMetadata()), new Update(dataset, datasetRecord.getMetadata())), MutationOptions.DEFAULT);
// search based on the names of properties
assertResults(mds, SearchRequest.of("properties:key1").build(), programRecord, datasetRecord);
assertResults(mds, SearchRequest.of("properties:skey1").setScope(SYSTEM).build(), datasetRecord);
assertResults(mds, SearchRequest.of("properties:multi*").setScope(USER).build(), programRecord);
// Search for it based on key and value
assertResults(mds, SearchRequest.of("key1:value1").addType(TYPE_PROGRAM).build(), programRecord);
assertResults(mds, SearchRequest.of("key1:value1").build(), programRecord, datasetRecord);
// Search for it based on a word in value
assertResults(mds, SearchRequest.of("multiword:aV5").build(), programRecord);
// Search for it based on a word in value with spaces in search query
assertResults(mds, SearchRequest.of(" multiword:aV1 ").build(), programRecord);
// remove the multiword property
mds.apply(new Remove(program, ImmutableSet.of(new ScopedNameOfKind(PROPERTY, USER, "multiword"))), MutationOptions.DEFAULT);
programRecord = new MetadataRecord(program, new Metadata(USER, props("key1", "value1", "key2", "value2")));
// search results should be empty after removing this key as the indexes are deleted
assertEmpty(mds, SearchRequest.of("multiword:aV5").build());
// Test wrong ns
assertEmpty(mds, SearchRequest.of("key1:value1").addNamespace("ns12").build());
// Test multi word query
assertResults(mds, SearchRequest.of(" value1 av2 ").build(), programRecord, datasetRecord);
assertResults(mds, SearchRequest.of(" value1 sValue1 ").build(), programRecord, datasetRecord);
assertResults(mds, SearchRequest.of(" valu* sVal* ").build(), programRecord, datasetRecord);
// search with explicit set of target types
assertResults(mds, SearchRequest.of(" valu* sVal* ").addType(TYPE_PROGRAM).addType(TYPE_DATASET).build(), programRecord, datasetRecord);
// search all target types
assertResults(mds, SearchRequest.of(" valu* sVal* ").build(), programRecord, datasetRecord);
// clean up
mds.batch(batch(new Drop(program), new Drop(dataset)), MutationOptions.DEFAULT);
}
Aggregations