Search in sources :

Example 41 with MetadataEntity

use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by caskdata.

the class MetadataStorageTest method testUpdateDropConflictInBatch.

/**
 * See {@link #testUpdateDropConflict()} for a description. The difference in this test is that
 * we issue batches of mutations over a collection of entities. The same assumptions apply,
 * however, for each entity.
 */
@Test
@Category(SlowTests.class)
public void testUpdateDropConflictInBatch() throws IOException {
    int numTests = 10;
    int numThreads = 2;
    int numEntities = 20;
    MetadataStorage mds = getMetadataStorage();
    ExecutorService executor = Executors.newFixedThreadPool(numThreads);
    CompletionService<List<MetadataChange>> completionService = new ExecutorCompletionService<>(executor);
    Map<Integer, MetadataEntity> entities = IntStream.range(0, numEntities).boxed().collect(Collectors.toMap(i -> i, i -> MetadataEntity.ofDataset("myds" + i)));
    List<MetadataMutation> creates = entities.values().stream().map(e -> new Create(e, new Metadata(USER, tags("a")), Collections.emptyMap())).collect(Collectors.toList());
    Random rand = new Random(System.currentTimeMillis());
    IntStream.range(0, numTests).forEach(x -> {
        try {
            mds.batch(creates, MutationOptions.DEFAULT);
            Map<Integer, List<MetadataMutation>> mutations = IntStream.range(0, numThreads).boxed().collect(Collectors.toMap(i -> i, i -> new ArrayList<>()));
            IntStream.range(0, numEntities).forEach(e -> {
                // ensure that at least one thread attempts to drop this entity
                int dropThread = rand.nextInt(numThreads);
                IntStream.range(0, numThreads).forEach(t -> {
                    if (t == dropThread || rand.nextInt(100) < 50) {
                        mutations.get(t).add(new Drop(entities.get(e)));
                    } else {
                        mutations.get(t).add(new Update(entities.get(e), new Metadata(USER, tags("b"))));
                    }
                });
            });
            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);
                }
            });
            IntStream.range(0, numEntities).forEach(e -> {
                try {
                    // each entity is either dropped then updated (and then it has tag "b" only)
                    // or it first update and then dropped (and then it has empty metadata)
                    Assert.assertTrue(ImmutableSet.of(Metadata.EMPTY, new Metadata(USER, tags("b"))).contains(mds.read(new Read(entities.get(e)))));
                } catch (Exception ex) {
                    throw Throwables.propagate(ex);
                }
            });
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    });
    mds.batch(entities.values().stream().map(Drop::new).collect(Collectors.toList()), MutationOptions.DEFAULT);
}
Also used : IntStream(java.util.stream.IntStream) DESCRIPTION_KEY(io.cdap.cdap.spi.metadata.MetadataConstants.DESCRIPTION_KEY) Arrays(java.util.Arrays) Drop(io.cdap.cdap.spi.metadata.MetadataMutation.Drop) HashMap(java.util.HashMap) Random(java.util.Random) USER(io.cdap.cdap.api.metadata.MetadataScope.USER) ENTITY_NAME_KEY(io.cdap.cdap.spi.metadata.MetadataConstants.ENTITY_NAME_KEY) CompletionService(java.util.concurrent.CompletionService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SlowTests(io.cdap.cdap.test.SlowTests) TAG(io.cdap.cdap.spi.metadata.MetadataKind.TAG) ImmutableList(com.google.common.collect.ImmutableList) After(org.junit.After) Map(java.util.Map) MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) PROPERTY(io.cdap.cdap.spi.metadata.MetadataKind.PROPERTY) Tasks(io.cdap.cdap.common.utils.Tasks) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Beta(io.cdap.cdap.api.annotation.Beta) SYSTEM(io.cdap.cdap.api.metadata.MetadataScope.SYSTEM) Update(io.cdap.cdap.spi.metadata.MetadataMutation.Update) Throwables(com.google.common.base.Throwables) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) Category(org.junit.experimental.categories.Category) Maps(com.google.common.collect.Maps) Schema(io.cdap.cdap.api.data.schema.Schema) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Executors(java.util.concurrent.Executors) TTL_KEY(io.cdap.cdap.spi.metadata.MetadataConstants.TTL_KEY) Create(io.cdap.cdap.spi.metadata.MetadataMutation.Create) TimeUnit(java.util.concurrent.TimeUnit) CREATION_TIME_KEY(io.cdap.cdap.spi.metadata.MetadataConstants.CREATION_TIME_KEY) List(java.util.List) MetadataScope(io.cdap.cdap.api.metadata.MetadataScope) Assert(org.junit.Assert) Comparator(java.util.Comparator) Remove(io.cdap.cdap.spi.metadata.MetadataMutation.Remove) Collections(java.util.Collections) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) ArrayList(java.util.ArrayList) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) Update(io.cdap.cdap.spi.metadata.MetadataMutation.Update) IOException(java.io.IOException) Drop(io.cdap.cdap.spi.metadata.MetadataMutation.Drop) Random(java.util.Random) Create(io.cdap.cdap.spi.metadata.MetadataMutation.Create) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 42 with MetadataEntity

use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by caskdata.

the class MetadataStorageTest method testSearchOnValue.

@Test
public void testSearchOnValue() throws Exception {
    MetadataStorage mds = getMetadataStorage();
    MetadataEntity program = ofWorker(ofApp("ns1", "app1"), "wk1");
    MetadataEntity dataset = ofDataset("ns1", "ds2");
    // Add some metadata
    final String multiWordValue = "aV1 av2 ,  -  ,  av3 - av4_av5 av6";
    MetadataRecord programRecord = new MetadataRecord(program, new Metadata(USER, props("key1", "value1", "key2", "value2", "multiword", multiWordValue)));
    mds.apply(new Update(program, programRecord.getMetadata()), MutationOptions.DEFAULT);
    // Search for it based on value
    assertResults(mds, SearchRequest.of("value1").build(), programRecord);
    assertResults(mds, SearchRequest.of("  aV1   ").addType(TYPE_PROGRAM).build(), programRecord);
    assertEmpty(mds, SearchRequest.of("  aV1   ").addType(TYPE_ARTIFACT).build());
    // Search for it based split patterns to make sure nothing is matched
    assertEmpty(mds, SearchRequest.of("-").build());
    assertEmpty(mds, SearchRequest.of(",").build());
    assertEmpty(mds, SearchRequest.of("_").build());
    assertEmpty(mds, SearchRequest.of(", ,").build());
    assertEmpty(mds, SearchRequest.of(", - ,").build());
    // Search for it based on a word in value
    assertResults(mds, SearchRequest.of("av5").addType(TYPE_PROGRAM).build(), programRecord);
    // Case insensitive
    assertResults(mds, SearchRequest.of("ValUe1").addType(TYPE_PROGRAM).build(), programRecord);
    // add a property for the program
    mds.apply(new Update(program, new Metadata(SYSTEM, props("key3", "value1"))), MutationOptions.DEFAULT);
    programRecord = new MetadataRecord(program, new Metadata(ImmutableSet.of(), ImmutableMap.of(new ScopedName(USER, "key1"), "value1", new ScopedName(USER, "key2"), "value2", new ScopedName(USER, "multiword"), multiWordValue, new ScopedName(SYSTEM, "key3"), "value1")));
    // search by value
    assertResults(mds, SearchRequest.of("value1").addType(TYPE_PROGRAM).build(), programRecord);
    // add a property for the dataset
    MetadataRecord datasetRecord = new MetadataRecord(dataset, new Metadata(USER, props("key21", "value21")));
    mds.apply(new Update(dataset, datasetRecord.getMetadata()), MutationOptions.DEFAULT);
    // Search based on value prefix
    assertResults(mds, SearchRequest.of("value2*").build(), programRecord, datasetRecord);
    // Search based on value prefix in the wrong namespace
    assertEmpty(mds, SearchRequest.of("value2*").addNamespace("ns12").build());
    // clean up
    mds.batch(batch(new Drop(program), new Drop(dataset)), MutationOptions.DEFAULT);
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) Update(io.cdap.cdap.spi.metadata.MetadataMutation.Update) Drop(io.cdap.cdap.spi.metadata.MetadataMutation.Drop) Test(org.junit.Test)

Example 43 with MetadataEntity

use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by caskdata.

the class EntityIdTest method testGetSelfOrParentEntityId.

@Test
public void testGetSelfOrParentEntityId() {
    // entity is a known entityId
    MetadataEntity metadataEntity = MetadataEntity.ofDataset("testNs", "testDs");
    Assert.assertEquals(new DatasetId("testNs", "testDs"), EntityId.getSelfOrParentEntityId(metadataEntity));
    // entity's parent is not a known entity
    metadataEntity = MetadataEntity.builder().append("ab", "cd").appendAsType("unkonwType", "value").append("ef", "gh").build();
    try {
        EntityId.getSelfOrParentEntityId(metadataEntity);
        Assert.fail("Should have failed to get a parent entity id");
    } catch (IllegalArgumentException e) {
    // expected
    }
    metadataEntity = MetadataEntity.builder().append(MetadataEntity.NAMESPACE, "testNs").append(MetadataEntity.DATASET, "testDs").appendAsType("field", "testField").build();
    Assert.assertEquals(new DatasetId("testNs", "testDs"), EntityId.getSelfOrParentEntityId(metadataEntity));
    metadataEntity = MetadataEntity.builder().append(MetadataEntity.NAMESPACE, "testNs").append(MetadataEntity.APPLICATION, "testApp").appendAsType("custom", "custValue").build();
    Assert.assertEquals(new ApplicationId("testNs", "testApp"), EntityId.getSelfOrParentEntityId(metadataEntity));
    metadataEntity = MetadataEntity.builder().append(MetadataEntity.NAMESPACE, "testNs").append(MetadataEntity.APPLICATION, "testApp").append(MetadataEntity.TYPE, ProgramType.WORKER.getPrettyName()).append(MetadataEntity.PROGRAM, "testProg").appendAsType("subType", "realtime").build();
    Assert.assertEquals(new ProgramId("testNs", "testApp", ProgramType.WORKER, "testProg"), EntityId.getSelfOrParentEntityId(metadataEntity));
    ArtifactId artifactId = new ArtifactId("testNs", "testArtifact-1.0.0.jar");
    metadataEntity = MetadataEntity.builder(artifactId.toMetadataEntity()).appendAsType("time", "t2").build();
    Assert.assertEquals(artifactId, EntityId.getSelfOrParentEntityId(metadataEntity));
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) Test(org.junit.Test)

Example 44 with MetadataEntity

use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by caskdata.

the class EntityIdTest method testFromMetadataEntity.

@Test
public void testFromMetadataEntity() {
    ApplicationId applicationId = new ApplicationId("testNs", "app1");
    Assert.assertEquals(applicationId, EntityId.fromMetadataEntity(applicationId.toMetadataEntity()));
    MetadataEntity metadataEntity = MetadataEntity.builder().append(MetadataEntity.NAMESPACE, "testNs").appendAsType(MetadataEntity.APPLICATION, "app1").build();
    Assert.assertEquals(applicationId, EntityId.fromMetadataEntity(metadataEntity));
    metadataEntity = MetadataEntity.builder(MetadataEntity.ofDataset("testNs", "testDs")).appendAsType("field", "testField").build();
    try {
        EntityId.fromMetadataEntity(metadataEntity);
        Assert.fail("Should have failed to get create an EntityId from MetadataEntity");
    } catch (IllegalArgumentException e) {
    // expected
    }
    ProgramId programId = new ProgramId(applicationId, ProgramType.WORKER, "testWorker");
    Assert.assertEquals(programId, EntityId.fromMetadataEntity(programId.toMetadataEntity()));
    // artifact
    ArtifactId artifactId = new ArtifactId("testNs", "testArtifact-1.0.0.jar");
    Assert.assertEquals(artifactId, EntityId.fromMetadataEntity(artifactId.toMetadataEntity()));
    // schedule
    ScheduleId scheduleId = applicationId.schedule("sch");
    metadataEntity = MetadataEntity.builder().append(MetadataEntity.NAMESPACE, "testNs").append(MetadataEntity.APPLICATION, "app1").appendAsType(MetadataEntity.SCHEDULE, "sch").build();
    Assert.assertEquals(scheduleId, EntityId.fromMetadataEntity(metadataEntity));
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) Test(org.junit.Test)

Example 45 with MetadataEntity

use of io.cdap.cdap.api.metadata.MetadataEntity 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);
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) Remove(io.cdap.cdap.spi.metadata.MetadataMutation.Remove) Update(io.cdap.cdap.spi.metadata.MetadataMutation.Update) Drop(io.cdap.cdap.spi.metadata.MetadataMutation.Drop) Test(org.junit.Test)

Aggregations

MetadataEntity (io.cdap.cdap.api.metadata.MetadataEntity)192 Test (org.junit.Test)114 Drop (io.cdap.cdap.spi.metadata.MetadataMutation.Drop)58 Update (io.cdap.cdap.spi.metadata.MetadataMutation.Update)56 MetadataScope (io.cdap.cdap.api.metadata.MetadataScope)34 HashMap (java.util.HashMap)30 HashSet (java.util.HashSet)30 Map (java.util.Map)28 ImmutableMap (com.google.common.collect.ImmutableMap)26 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)26 Remove (io.cdap.cdap.spi.metadata.MetadataMutation.Remove)24 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)22 List (java.util.List)22 Metadata (io.cdap.cdap.spi.metadata.Metadata)20 Create (io.cdap.cdap.spi.metadata.MetadataMutation.Create)20 Set (java.util.Set)20 Collectors (java.util.stream.Collectors)20 Nullable (javax.annotation.Nullable)20 Path (javax.ws.rs.Path)20