You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@gobblin.apache.org by GitBox <gi...@apache.org> on 2022/11/16 01:23:50 UTC

[GitHub] [gobblin] umustafi commented on a diff in pull request #3598: [GOBBLIN-1741] Create manifest based dataset finder

umustafi commented on code in PR #3598:
URL: https://github.com/apache/gobblin/pull/3598#discussion_r1023405736


##########
gobblin-data-management/src/main/java/org/apache/gobblin/data/management/copy/ManifestBasedDataset.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.gobblin.data.management.copy;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Iterators;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.gson.Gson;
+import com.google.gson.JsonIOException;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonSyntaxException;
+import com.google.gson.stream.JsonReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.gobblin.commit.CommitStep;
+import org.apache.gobblin.data.management.copy.entities.PrePublishStep;
+import org.apache.gobblin.data.management.partition.FileSet;
+import org.apache.gobblin.util.commit.DeleteFileCommitStep;
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+
+
+/**
+ * A dataset that based on Manifest. We expect the Manifest contains the list of all the files for this dataset.
+ * At first phase, we only support copy across different clusters to the same location. (We can add more feature to support rename in the future)
+ */
+@Slf4j
+public class ManifestBasedDataset implements IterableCopyableDataset {
+
+  public static final String CONFIG_PREFIX = CopyConfiguration.COPY_PREFIX + ".manifestBased";
+  private static final String DELETE_FILE_NOT_EXIST_ON_SOURCE = CONFIG_PREFIX + ".deleteFileNotExistOnSource";
+  /** If true, will delete newly empty directories up to the config set in DELETE_EMPTY_DIRECTORIES_UPTO. */
+  public static final String DELETE_EMPTY_DIRECTORIES_KEY = CONFIG_PREFIX + ".deleteEmptyDirectories";
+  public static final String DELETE_EMPTY_DIRECTORIES_UPTO = CONFIG_PREFIX + ".deleteEmptyDirectoriesUpTo";
+  private final FileSystem fs;
+  private final Path manifestPath;
+  private final Properties properties;
+  private final boolean deleteEmptyDirectories;
+  private Gson GSON = new Gson();
+
+  public ManifestBasedDataset(final FileSystem fs, Path manifestPath, Properties properties) {
+    this.fs = fs;
+    this.manifestPath = manifestPath;
+    this.properties = properties;
+    this.deleteEmptyDirectories = Boolean.parseBoolean(properties.getProperty(DELETE_EMPTY_DIRECTORIES_KEY, "false"))
+        && properties.containsKey(DELETE_EMPTY_DIRECTORIES_UPTO);
+  }
+
+  @Override
+  public String datasetURN() {
+    return this.manifestPath.toString();
+  }
+
+  @Override
+  public Iterator<FileSet<CopyEntity>> getFileSetIterator(FileSystem targetFs, CopyConfiguration configuration)
+      throws IOException {
+    if (!fs.exists(manifestPath)) {
+      log.warn(String.format("Manifest path %s does not exist on filesystem %s, will not copy data in this manifest"
+          + ", probably due to wrong configuration of %s", manifestPath.toString(), fs.getUri().toString(), ManifestBasedDatasetFinder.MANIFEST_LOCATION));
+      return Iterators.emptyIterator();
+    } else if (fs.getFileStatus(manifestPath).isDirectory()) {
+      log.warn(String.format("Manifest path %s on filesystem %s is a directory, which is not supported. Please set the manifest file locations in"
+          + "%s, you can specify multi locations split by '',", manifestPath.toString(), fs.getUri().toString(), ManifestBasedDatasetFinder.MANIFEST_LOCATION));
+      return Iterators.emptyIterator();
+    }
+    JsonReader reader = null;
+    List<CopyEntity> copyEntities = Lists.newArrayList();
+    List<FileStatus> toDelete = Lists.newArrayList();
+    //todo: put permission preserve logic here?
+    try {
+      reader = new JsonReader(new InputStreamReader(fs.open(manifestPath), "UTF-8"));
+      reader.beginArray();
+      while (reader.hasNext()) {
+        //todo: We can use fileSet to partition the data in case of some softbound issue
+        //todo: After partition, change this to directly return iterator so that we can save time if we meet resources limitation
+        JsonObject file = GSON.fromJson(reader, JsonObject.class);
+        Path fileToCopy = new Path(file.get("fileName").getAsString());
+        if (this.fs.exists(fileToCopy)) {
+          if (!targetFs.exists(fileToCopy) || needCopy(this.fs.getFileStatus(fileToCopy), targetFs.getFileStatus(fileToCopy))) {
+            CopyableFile copyableFile =
+                CopyableFile.fromOriginAndDestination(this.fs, this.fs.getFileStatus(fileToCopy), fileToCopy, configuration)
+                    .fileSet(datasetURN())
+                    .datasetOutputPath(fileToCopy.toString())
+                    .ancestorsOwnerAndPermission(CopyableFile
+                        .resolveReplicatedOwnerAndPermissionsRecursively(this.fs, fileToCopy.getParent(),
+                            new Path("/"), configuration))
+                    .build();
+            copyableFile.setFsDatasets(this.fs, targetFs);
+            copyEntities.add(copyableFile);
+          }
+        } else if (targetFs.exists(fileToCopy)){
+          toDelete.add(targetFs.getFileStatus(fileToCopy));
+        }
+      }
+      if (Boolean.parseBoolean(this.properties.getProperty(DELETE_FILE_NOT_EXIST_ON_SOURCE, "false"))) {
+        CommitStep step = new DeleteFileCommitStep(targetFs, toDelete, this.properties,
+            this.deleteEmptyDirectories ? Optional.of(new Path(this.properties.getProperty(DELETE_EMPTY_DIRECTORIES_UPTO))) : Optional.<Path>absent());
+        copyEntities.add(new PrePublishStep(datasetURN(), Maps.newHashMap(), step, 1));
+      }
+    } catch (JsonIOException| JsonSyntaxException e) {
+      //todo: update error message to point to a sample json file instead of schema which is hard to understand
+      log.warn(String.format("Failed to read Manifest path %s on filesystem %s, please make sure it's in correct json format with schema"
+          + " {type:array, items:{type: object, properties:{id:{type:String}, fileName:{type:String}, fileGroup:{type:String}, fileSizeInBytes: {type:Long}}}}",
+          manifestPath.toString(), fs.getUri().toString()), e);

Review Comment:
   missing a `%s` for the exception e



##########
gobblin-data-management/src/main/java/org/apache/gobblin/data/management/copy/ManifestBasedDatasetFinder.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * 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.gobblin.data.management.copy;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.util.stream.Collectors;
+import org.apache.gobblin.dataset.IterableDatasetFinder;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+
+
+public class ManifestBasedDatasetFinder implements IterableDatasetFinder<ManifestBasedDataset> {
+
+  public static final String MANIFEST_LOCATION = ManifestBasedDataset.CONFIG_PREFIX + ".manifest.location";
+  private final FileSystem fs;
+  private final List<Path> manifestLocations;
+  private final  Properties properties;
+  public ManifestBasedDatasetFinder(final FileSystem fs, Properties properties) {
+    Preconditions.checkArgument(properties.containsKey(MANIFEST_LOCATION), "Please config " + MANIFEST_LOCATION);

Review Comment:
   improve config "manifest location key required in config. Please set"...



##########
gobblin-data-management/src/main/java/org/apache/gobblin/data/management/copy/ManifestBasedDataset.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.gobblin.data.management.copy;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Iterators;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.gson.Gson;
+import com.google.gson.JsonIOException;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonSyntaxException;
+import com.google.gson.stream.JsonReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.gobblin.commit.CommitStep;
+import org.apache.gobblin.data.management.copy.entities.PrePublishStep;
+import org.apache.gobblin.data.management.partition.FileSet;
+import org.apache.gobblin.util.commit.DeleteFileCommitStep;
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+
+
+/**
+ * A dataset that based on Manifest. We expect the Manifest contains the list of all the files for this dataset.
+ * At first phase, we only support copy across different clusters to the same location. (We can add more feature to support rename in the future)
+ */
+@Slf4j
+public class ManifestBasedDataset implements IterableCopyableDataset {
+
+  public static final String CONFIG_PREFIX = CopyConfiguration.COPY_PREFIX + ".manifestBased";
+  private static final String DELETE_FILE_NOT_EXIST_ON_SOURCE = CONFIG_PREFIX + ".deleteFileNotExistOnSource";
+  /** If true, will delete newly empty directories up to the config set in DELETE_EMPTY_DIRECTORIES_UPTO. */
+  public static final String DELETE_EMPTY_DIRECTORIES_KEY = CONFIG_PREFIX + ".deleteEmptyDirectories";
+  public static final String DELETE_EMPTY_DIRECTORIES_UPTO = CONFIG_PREFIX + ".deleteEmptyDirectoriesUpTo";
+  private final FileSystem fs;
+  private final Path manifestPath;
+  private final Properties properties;
+  private final boolean deleteEmptyDirectories;
+  private Gson GSON = new Gson();
+
+  public ManifestBasedDataset(final FileSystem fs, Path manifestPath, Properties properties) {
+    this.fs = fs;
+    this.manifestPath = manifestPath;
+    this.properties = properties;
+    this.deleteEmptyDirectories = Boolean.parseBoolean(properties.getProperty(DELETE_EMPTY_DIRECTORIES_KEY, "false"))
+        && properties.containsKey(DELETE_EMPTY_DIRECTORIES_UPTO);
+  }
+
+  @Override
+  public String datasetURN() {
+    return this.manifestPath.toString();
+  }
+
+  @Override
+  public Iterator<FileSet<CopyEntity>> getFileSetIterator(FileSystem targetFs, CopyConfiguration configuration)
+      throws IOException {
+    if (!fs.exists(manifestPath)) {
+      log.warn(String.format("Manifest path %s does not exist on filesystem %s, will not copy data in this manifest"
+          + ", probably due to wrong configuration of %s", manifestPath.toString(), fs.getUri().toString(), ManifestBasedDatasetFinder.MANIFEST_LOCATION));
+      return Iterators.emptyIterator();
+    } else if (fs.getFileStatus(manifestPath).isDirectory()) {
+      log.warn(String.format("Manifest path %s on filesystem %s is a directory, which is not supported. Please set the manifest file locations in"
+          + "%s, you can specify multi locations split by '',", manifestPath.toString(), fs.getUri().toString(), ManifestBasedDatasetFinder.MANIFEST_LOCATION));
+      return Iterators.emptyIterator();
+    }
+    JsonReader reader = null;
+    List<CopyEntity> copyEntities = Lists.newArrayList();
+    List<FileStatus> toDelete = Lists.newArrayList();
+    //todo: put permission preserve logic here?
+    try {
+      reader = new JsonReader(new InputStreamReader(fs.open(manifestPath), "UTF-8"));
+      reader.beginArray();
+      while (reader.hasNext()) {
+        //todo: We can use fileSet to partition the data in case of some softbound issue
+        //todo: After partition, change this to directly return iterator so that we can save time if we meet resources limitation
+        JsonObject file = GSON.fromJson(reader, JsonObject.class);
+        Path fileToCopy = new Path(file.get("fileName").getAsString());
+        if (this.fs.exists(fileToCopy)) {
+          if (!targetFs.exists(fileToCopy) || needCopy(this.fs.getFileStatus(fileToCopy), targetFs.getFileStatus(fileToCopy))) {
+            CopyableFile copyableFile =
+                CopyableFile.fromOriginAndDestination(this.fs, this.fs.getFileStatus(fileToCopy), fileToCopy, configuration)
+                    .fileSet(datasetURN())
+                    .datasetOutputPath(fileToCopy.toString())
+                    .ancestorsOwnerAndPermission(CopyableFile
+                        .resolveReplicatedOwnerAndPermissionsRecursively(this.fs, fileToCopy.getParent(),
+                            new Path("/"), configuration))
+                    .build();
+            copyableFile.setFsDatasets(this.fs, targetFs);
+            copyEntities.add(copyableFile);
+          }
+        } else if (targetFs.exists(fileToCopy)){
+          toDelete.add(targetFs.getFileStatus(fileToCopy));
+        }
+      }
+      if (Boolean.parseBoolean(this.properties.getProperty(DELETE_FILE_NOT_EXIST_ON_SOURCE, "false"))) {
+        CommitStep step = new DeleteFileCommitStep(targetFs, toDelete, this.properties,
+            this.deleteEmptyDirectories ? Optional.of(new Path(this.properties.getProperty(DELETE_EMPTY_DIRECTORIES_UPTO))) : Optional.<Path>absent());

Review Comment:
   is this to delete directories that are emptied after we remove the files that do not exist on source?



-- 
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: dev-unsubscribe@gobblin.apache.org

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