You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/05/04 15:56:52 UTC

[GitHub] [nifi] nandorsoma commented on a diff in pull request #5926: NIFI-9865: Add Atlas lineage for GCS processors (PutHDFS)

nandorsoma commented on code in PR #5926:
URL: https://github.com/apache/nifi/pull/5926#discussion_r864978834


##########
nifi-nar-bundles/nifi-atlas-bundle/nifi-atlas-reporting-task/src/main/java/org/apache/nifi/atlas/provenance/analyzer/GCSDirectory.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.atlas.provenance.analyzer;
+
+import org.apache.atlas.model.instance.AtlasEntity;
+import org.apache.atlas.model.instance.AtlasObjectId;
+import org.apache.atlas.utils.AtlasPathExtractorUtil;
+import org.apache.atlas.utils.PathExtractorContext;
+import org.apache.atlas.v1.model.instance.Referenceable;
+import org.apache.hadoop.fs.Path;
+import org.apache.nifi.atlas.provenance.AbstractNiFiProvenanceEventAnalyzer;
+import org.apache.nifi.atlas.provenance.AnalysisContext;
+import org.apache.nifi.atlas.provenance.DataSetRefs;
+import org.apache.nifi.provenance.ProvenanceEventRecord;
+
+import java.util.Map;
+
+import static org.apache.nifi.atlas.NiFiTypes.ATTR_QUALIFIED_NAME;
+
+/**
+ * Analyzes a transit URI as a GCS bucket or directory (skipping the file name).
+ * <p>
+ * Atlas entity hierarchy v1: gcs_virtual_dir -> gcs_bucket
+ * <p>gcs_virtual_dir
+ * <ul>
+ *   <li>qualifiedName=gs://bucket/path@namespace (example: gs://mybucket/mydir1/mydir2@ns1)
+ *   <li>name=/path (example: /mydir1/mydir2)
+ * </ul>
+ * <p>gcs_bucket
+ * <ul>
+ *   <li>qualifiedName=gs://bucket@namespace (example: gs://mybucket@ns1)
+ *   <li>name=bucket (example: mybucket)
+ * </ul>
+ */
+public class GCSDirectory extends AbstractNiFiProvenanceEventAnalyzer {
+
+    static final String GCP_STORAGE_VIRTUAL_DIRECTORY = "gcp_storage_virtual_directory";
+    static final String REL_PARENT = "parent";
+
+    @Override
+    public DataSetRefs analyze(AnalysisContext context, ProvenanceEventRecord event) {

Review Comment:
   This whole method is duplicated in AwsS3Directory. Wouldn't it make sense to extract it to a util or to an AbstractDirectoryEventAnalyzer class? What do you think?



##########
nifi-nar-bundles/nifi-atlas-bundle/nifi-atlas-reporting-task/src/main/java/org/apache/nifi/atlas/provenance/analyzer/GCSDirectory.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.atlas.provenance.analyzer;
+
+import org.apache.atlas.model.instance.AtlasEntity;
+import org.apache.atlas.model.instance.AtlasObjectId;
+import org.apache.atlas.utils.AtlasPathExtractorUtil;
+import org.apache.atlas.utils.PathExtractorContext;
+import org.apache.atlas.v1.model.instance.Referenceable;
+import org.apache.hadoop.fs.Path;
+import org.apache.nifi.atlas.provenance.AbstractNiFiProvenanceEventAnalyzer;
+import org.apache.nifi.atlas.provenance.AnalysisContext;
+import org.apache.nifi.atlas.provenance.DataSetRefs;
+import org.apache.nifi.provenance.ProvenanceEventRecord;
+
+import java.util.Map;
+
+import static org.apache.nifi.atlas.NiFiTypes.ATTR_QUALIFIED_NAME;
+
+/**
+ * Analyzes a transit URI as a GCS bucket or directory (skipping the file name).
+ * <p>
+ * Atlas entity hierarchy v1: gcs_virtual_dir -> gcs_bucket
+ * <p>gcs_virtual_dir
+ * <ul>
+ *   <li>qualifiedName=gs://bucket/path@namespace (example: gs://mybucket/mydir1/mydir2@ns1)
+ *   <li>name=/path (example: /mydir1/mydir2)
+ * </ul>
+ * <p>gcs_bucket
+ * <ul>
+ *   <li>qualifiedName=gs://bucket@namespace (example: gs://mybucket@ns1)
+ *   <li>name=bucket (example: mybucket)
+ * </ul>
+ */
+public class GCSDirectory extends AbstractNiFiProvenanceEventAnalyzer {
+
+    static final String GCP_STORAGE_VIRTUAL_DIRECTORY = "gcp_storage_virtual_directory";
+    static final String REL_PARENT = "parent";
+
+    @Override
+    public DataSetRefs analyze(AnalysisContext context, ProvenanceEventRecord event) {
+        String transitUri = event.getTransitUri();
+        if (transitUri == null) {
+            return null;
+        }
+
+        String directoryUri = transitUri.substring(0, transitUri.lastIndexOf('/') + 1);

Review Comment:
   finals are missing in multiple lines. I don't know on what lvl do we want to stick to them, just mentioning.



##########
nifi-nar-bundles/nifi-atlas-bundle/nifi-atlas-reporting-task/src/test/java/org/apache/nifi/atlas/provenance/analyzer/TestGCSDirectory.java:
##########
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.atlas.provenance.analyzer;
+
+import org.apache.atlas.utils.AtlasPathExtractorUtil;
+import org.apache.atlas.v1.model.instance.Referenceable;
+import org.apache.nifi.atlas.provenance.AnalysisContext;
+import org.apache.nifi.atlas.provenance.DataSetRefs;
+import org.apache.nifi.atlas.provenance.NiFiProvenanceEventAnalyzer;
+import org.apache.nifi.atlas.provenance.NiFiProvenanceEventAnalyzerFactory;
+import org.apache.nifi.atlas.resolver.NamespaceResolver;
+import org.apache.nifi.provenance.ProvenanceEventRecord;
+import org.apache.nifi.provenance.ProvenanceEventType;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.apache.nifi.atlas.NiFiTypes.ATTR_NAME;
+import static org.apache.nifi.atlas.NiFiTypes.ATTR_QUALIFIED_NAME;
+import static org.apache.nifi.atlas.provenance.analyzer.GCSDirectory.GCP_STORAGE_VIRTUAL_DIRECTORY;
+import static org.apache.nifi.atlas.provenance.analyzer.GCSDirectory.REL_PARENT;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+public class TestGCSDirectory {
+
+    protected static final ProvenanceEventType PROVENANCE_EVENT_TYPE = ProvenanceEventType.SEND;
+    protected static final String ATLAS_NAMESPACE = "namespace1";
+    protected static final String GCS_BUCKET = "bucket1";
+    protected static final String GCS_FILENAME = "file1";
+
+
+    @Test
+    public void testSimpleDirectory() {
+        String processorName = "PutHDFS";
+        String dirPath = "/dir1";
+
+        String expectedDirectoryQualifiedName = String.format("gs://%s%s/@%s", GCS_BUCKET, dirPath, ATLAS_NAMESPACE);

Review Comment:
   Since the pattern is reused, I think you can extract it to a static variable. There is another comment about it, see my comment at `createTransitUri()`.



##########
nifi-nar-bundles/nifi-atlas-bundle/nifi-atlas-reporting-task/src/test/java/org/apache/nifi/atlas/provenance/analyzer/TestGCSDirectory.java:
##########
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.atlas.provenance.analyzer;
+
+import org.apache.atlas.utils.AtlasPathExtractorUtil;
+import org.apache.atlas.v1.model.instance.Referenceable;
+import org.apache.nifi.atlas.provenance.AnalysisContext;
+import org.apache.nifi.atlas.provenance.DataSetRefs;
+import org.apache.nifi.atlas.provenance.NiFiProvenanceEventAnalyzer;
+import org.apache.nifi.atlas.provenance.NiFiProvenanceEventAnalyzerFactory;
+import org.apache.nifi.atlas.resolver.NamespaceResolver;
+import org.apache.nifi.provenance.ProvenanceEventRecord;
+import org.apache.nifi.provenance.ProvenanceEventType;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.apache.nifi.atlas.NiFiTypes.ATTR_NAME;
+import static org.apache.nifi.atlas.NiFiTypes.ATTR_QUALIFIED_NAME;
+import static org.apache.nifi.atlas.provenance.analyzer.GCSDirectory.GCP_STORAGE_VIRTUAL_DIRECTORY;
+import static org.apache.nifi.atlas.provenance.analyzer.GCSDirectory.REL_PARENT;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+public class TestGCSDirectory {
+
+    protected static final ProvenanceEventType PROVENANCE_EVENT_TYPE = ProvenanceEventType.SEND;
+    protected static final String ATLAS_NAMESPACE = "namespace1";
+    protected static final String GCS_BUCKET = "bucket1";
+    protected static final String GCS_FILENAME = "file1";
+
+
+    @Test
+    public void testSimpleDirectory() {
+        String processorName = "PutHDFS";
+        String dirPath = "/dir1";
+
+        String expectedDirectoryQualifiedName = String.format("gs://%s%s/@%s", GCS_BUCKET, dirPath, ATLAS_NAMESPACE);
+        executeTest(processorName, dirPath, "dir1", "/", GCS_BUCKET,
+                GCP_STORAGE_VIRTUAL_DIRECTORY, expectedDirectoryQualifiedName, AtlasPathExtractorUtil.GCS_BUCKET);
+    }
+
+    @Test
+    public void testCompoundDirectory() {
+        String processorName = "PutHDFS";
+        String dirPath = "/dir1/dir2/dir3/dir4/dir5";
+        String expectedDirectoryQualifiedName = String.format("gs://%s%s/@%s", GCS_BUCKET, dirPath, ATLAS_NAMESPACE);
+
+        executeTest(processorName, dirPath, "dir5", "/dir1/dir2/dir3/dir4/", "dir4",
+                GCP_STORAGE_VIRTUAL_DIRECTORY, expectedDirectoryQualifiedName, AtlasPathExtractorUtil.GCS_VIRTUAL_DIR);
+    }
+
+    @Test
+    public void testRootDirectory() {
+        String processorName = "PutHDFS";
+        String dirPath = "/";
+        String expectedDirectoryQualifiedName = String.format("gs://%s@%s", GCS_BUCKET, ATLAS_NAMESPACE);
+
+        executeTest(processorName, dirPath, GCS_BUCKET, null, "/",
+                "gcp_storage_bucket", expectedDirectoryQualifiedName, AtlasPathExtractorUtil.GCS_BUCKET);
+    }
+
+    protected void executeTest(String processorName, String directory, String lastDirName, String parentPath, String parentName,
+                               String directoryType, String expectedDirectoryQualifiedName, String parentType) {
+        String transitUri = createTransitUri(directory);
+
+        ProvenanceEventRecord provenanceEvent = mockProvenanceEvent(processorName, transitUri);
+        AnalysisContext analysisContext = mockAnalysisContext();
+
+        NiFiProvenanceEventAnalyzer analyzer = NiFiProvenanceEventAnalyzerFactory.getAnalyzer(processorName, transitUri, PROVENANCE_EVENT_TYPE);
+        assertAnalyzer(analyzer);
+
+        DataSetRefs refs = analyzer.analyze(analysisContext, provenanceEvent);
+        assertAnalysisResult(refs, lastDirName, parentPath, parentName, directoryType, expectedDirectoryQualifiedName, parentType);
+    }
+
+    protected void assertAnalysisResult(DataSetRefs refs, String lastDirName, String parentPath, String parentName,
+                                        String directoryType, String expectedDirectoryQualifiedName, String parentType) {
+
+
+        Assertions.assertEquals(0, refs.getInputs().size());
+        Assertions.assertEquals(1, refs.getOutputs().size());
+
+        Referenceable directoryRef = refs.getOutputs().iterator().next();
+
+        Assertions.assertEquals(directoryType, directoryRef.getTypeName());
+        Assertions.assertEquals(expectedDirectoryQualifiedName, directoryRef.get(ATTR_QUALIFIED_NAME));
+        Assertions.assertEquals(lastDirName, directoryRef.get(ATTR_NAME));
+        Assertions.assertEquals(parentPath, directoryRef.get(AtlasPathExtractorUtil.ATTRIBUTE_OBJECT_PREFIX));
+
+        Referenceable bucketRef = (Referenceable) directoryRef.get(REL_PARENT);
+        if (parentPath != null) {
+            Assertions.assertNotNull(bucketRef);
+            Assertions.assertEquals(parentType, bucketRef.getTypeName());
+            Assertions.assertEquals(parentName, bucketRef.get(ATTR_NAME));
+        }
+    }
+
+    private String createTransitUri(String directory) {

Review Comment:
   I think you can directly use the expectedDirectoryQualifiedName with a different name instead of using this in the executeTest method. Currently expectedDirectoryQualifiedName checks against something that is created in the test and not in the production code which makes the assertion useless and makes the test more difficult to understand.



##########
nifi-nar-bundles/nifi-atlas-bundle/nifi-atlas-reporting-task/src/test/java/org/apache/nifi/atlas/provenance/analyzer/TestGCSDirectory.java:
##########
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.atlas.provenance.analyzer;
+
+import org.apache.atlas.utils.AtlasPathExtractorUtil;
+import org.apache.atlas.v1.model.instance.Referenceable;
+import org.apache.nifi.atlas.provenance.AnalysisContext;
+import org.apache.nifi.atlas.provenance.DataSetRefs;
+import org.apache.nifi.atlas.provenance.NiFiProvenanceEventAnalyzer;
+import org.apache.nifi.atlas.provenance.NiFiProvenanceEventAnalyzerFactory;
+import org.apache.nifi.atlas.resolver.NamespaceResolver;
+import org.apache.nifi.provenance.ProvenanceEventRecord;
+import org.apache.nifi.provenance.ProvenanceEventType;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.apache.nifi.atlas.NiFiTypes.ATTR_NAME;
+import static org.apache.nifi.atlas.NiFiTypes.ATTR_QUALIFIED_NAME;
+import static org.apache.nifi.atlas.provenance.analyzer.GCSDirectory.GCP_STORAGE_VIRTUAL_DIRECTORY;
+import static org.apache.nifi.atlas.provenance.analyzer.GCSDirectory.REL_PARENT;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+public class TestGCSDirectory {
+
+    protected static final ProvenanceEventType PROVENANCE_EVENT_TYPE = ProvenanceEventType.SEND;
+    protected static final String ATLAS_NAMESPACE = "namespace1";
+    protected static final String GCS_BUCKET = "bucket1";
+    protected static final String GCS_FILENAME = "file1";
+
+
+    @Test
+    public void testSimpleDirectory() {
+        String processorName = "PutHDFS";
+        String dirPath = "/dir1";
+
+        String expectedDirectoryQualifiedName = String.format("gs://%s%s/@%s", GCS_BUCKET, dirPath, ATLAS_NAMESPACE);
+        executeTest(processorName, dirPath, "dir1", "/", GCS_BUCKET,
+                GCP_STORAGE_VIRTUAL_DIRECTORY, expectedDirectoryQualifiedName, AtlasPathExtractorUtil.GCS_BUCKET);
+    }
+
+    @Test
+    public void testCompoundDirectory() {
+        String processorName = "PutHDFS";
+        String dirPath = "/dir1/dir2/dir3/dir4/dir5";
+        String expectedDirectoryQualifiedName = String.format("gs://%s%s/@%s", GCS_BUCKET, dirPath, ATLAS_NAMESPACE);
+
+        executeTest(processorName, dirPath, "dir5", "/dir1/dir2/dir3/dir4/", "dir4",
+                GCP_STORAGE_VIRTUAL_DIRECTORY, expectedDirectoryQualifiedName, AtlasPathExtractorUtil.GCS_VIRTUAL_DIR);
+    }
+
+    @Test
+    public void testRootDirectory() {
+        String processorName = "PutHDFS";
+        String dirPath = "/";
+        String expectedDirectoryQualifiedName = String.format("gs://%s@%s", GCS_BUCKET, ATLAS_NAMESPACE);
+
+        executeTest(processorName, dirPath, GCS_BUCKET, null, "/",
+                "gcp_storage_bucket", expectedDirectoryQualifiedName, AtlasPathExtractorUtil.GCS_BUCKET);
+    }
+
+    protected void executeTest(String processorName, String directory, String lastDirName, String parentPath, String parentName,
+                               String directoryType, String expectedDirectoryQualifiedName, String parentType) {
+        String transitUri = createTransitUri(directory);
+
+        ProvenanceEventRecord provenanceEvent = mockProvenanceEvent(processorName, transitUri);
+        AnalysisContext analysisContext = mockAnalysisContext();
+
+        NiFiProvenanceEventAnalyzer analyzer = NiFiProvenanceEventAnalyzerFactory.getAnalyzer(processorName, transitUri, PROVENANCE_EVENT_TYPE);
+        assertAnalyzer(analyzer);
+
+        DataSetRefs refs = analyzer.analyze(analysisContext, provenanceEvent);
+        assertAnalysisResult(refs, lastDirName, parentPath, parentName, directoryType, expectedDirectoryQualifiedName, parentType);
+    }
+
+    protected void assertAnalysisResult(DataSetRefs refs, String lastDirName, String parentPath, String parentName,
+                                        String directoryType, String expectedDirectoryQualifiedName, String parentType) {
+
+
+        Assertions.assertEquals(0, refs.getInputs().size());
+        Assertions.assertEquals(1, refs.getOutputs().size());
+
+        Referenceable directoryRef = refs.getOutputs().iterator().next();
+
+        Assertions.assertEquals(directoryType, directoryRef.getTypeName());
+        Assertions.assertEquals(expectedDirectoryQualifiedName, directoryRef.get(ATTR_QUALIFIED_NAME));
+        Assertions.assertEquals(lastDirName, directoryRef.get(ATTR_NAME));
+        Assertions.assertEquals(parentPath, directoryRef.get(AtlasPathExtractorUtil.ATTRIBUTE_OBJECT_PREFIX));
+
+        Referenceable bucketRef = (Referenceable) directoryRef.get(REL_PARENT);

Review Comment:
   Minor, but you can move this line into the if block.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org