You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jackrabbit.apache.org by GitBox <gi...@apache.org> on 2022/10/06 15:38:59 UTC

[GitHub] [jackrabbit-oak] thomasmueller commented on a diff in pull request #728: OAK-9960: (oak-run) introduced datastore-copy command

thomasmueller commented on code in PR #728:
URL: https://github.com/apache/jackrabbit-oak/pull/728#discussion_r989203402


##########
oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCopyCommand.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.jackrabbit.oak.run;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import joptsimple.OptionSpecBuilder;
+import org.apache.jackrabbit.oak.commons.IOUtils;
+import org.apache.jackrabbit.oak.run.commons.Command;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.StandardSystemProperty.FILE_SEPARATOR;
+
+/**
+ * Command to concurrently download blobs from an azure datastore using sas token authentication.
+ * <p>
+ * Blobs are stored in a specific folder following the datastore structure format.
+ */
+public class DataStoreCopyCommand implements Command {
+
+    private static final Logger LOG = LoggerFactory.getLogger(DataStoreCopyCommand.class);
+
+    private String sourceRepo;
+    private String includePath;
+    private File fileIncludePath;
+    private String sasToken;
+    private String outDir;
+    private int concurrency;
+
+    @Override
+    public void execute(String... args) throws Exception {
+        parseCommandLineParams(args);
+
+        List<String> ids;
+        if (fileIncludePath != null) {
+            if (fileIncludePath.isDirectory() || !fileIncludePath.exists()) {
+                throw new IllegalArgumentException("file-include-path must be a valid file");
+            }
+            ids = org.apache.commons.io.IOUtils.readLines(Files.newInputStream(fileIncludePath.toPath()), StandardCharsets.UTF_8);
+        } else {
+            ids = Arrays.stream(includePath.split(";")).collect(Collectors.toList());
+        }
+        if (ids.isEmpty()) {
+            throw new IllegalArgumentException("Blob ids must be specified");
+        }
+
+        try (Downloader downloader = new Downloader(concurrency)) {
+            long start = System.currentTimeMillis();
+            List<Downloader.ItemResponse> responses = downloader.download(ids.stream().map(id -> {
+                Downloader.Item item = new Downloader.Item();
+                item.source = sourceRepo + "/" + id;
+                if (sasToken != null) {
+                    item.source += "?" + sasToken;
+                }
+                // Rename the blob names to match expected datastore cache format (remove the "-" in the name)
+                String blobName = id.replaceAll("-", "");

Review Comment:
   What about having this rename logic in a separate method, with unit tests? And then we we only need something like item.destination = getDestinationFromId(id, outDir, blobName)



##########
oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCopyCommand.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.jackrabbit.oak.run;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import joptsimple.OptionSpecBuilder;
+import org.apache.jackrabbit.oak.commons.IOUtils;
+import org.apache.jackrabbit.oak.run.commons.Command;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.StandardSystemProperty.FILE_SEPARATOR;
+
+/**
+ * Command to concurrently download blobs from an azure datastore using sas token authentication.
+ * <p>
+ * Blobs are stored in a specific folder following the datastore structure format.
+ */
+public class DataStoreCopyCommand implements Command {
+
+    private static final Logger LOG = LoggerFactory.getLogger(DataStoreCopyCommand.class);
+
+    private String sourceRepo;
+    private String includePath;
+    private File fileIncludePath;
+    private String sasToken;
+    private String outDir;
+    private int concurrency;
+
+    @Override
+    public void execute(String... args) throws Exception {
+        parseCommandLineParams(args);
+
+        List<String> ids;
+        if (fileIncludePath != null) {
+            if (fileIncludePath.isDirectory() || !fileIncludePath.exists()) {
+                throw new IllegalArgumentException("file-include-path must be a valid file");
+            }
+            ids = org.apache.commons.io.IOUtils.readLines(Files.newInputStream(fileIncludePath.toPath()), StandardCharsets.UTF_8);
+        } else {
+            ids = Arrays.stream(includePath.split(";")).collect(Collectors.toList());
+        }
+        if (ids.isEmpty()) {
+            throw new IllegalArgumentException("Blob ids must be specified");
+        }
+
+        try (Downloader downloader = new Downloader(concurrency)) {
+            long start = System.currentTimeMillis();
+            List<Downloader.ItemResponse> responses = downloader.download(ids.stream().map(id -> {
+                Downloader.Item item = new Downloader.Item();
+                item.source = sourceRepo + "/" + id;
+                if (sasToken != null) {
+                    item.source += "?" + sasToken;
+                }
+                // Rename the blob names to match expected datastore cache format (remove the "-" in the name)
+                String blobName = id.replaceAll("-", "");
+                if (id.length() < 6) {
+                    LOG.warn("Blob with name {} is less than 6 chars. Cannot create data folder structure. Storing in the root folder", blobName);
+                    item.destination = outDir + FILE_SEPARATOR.value() + blobName;
+                } else {
+                    item.destination = outDir + FILE_SEPARATOR.value()
+                            + blobName.substring(0, 2) + FILE_SEPARATOR.value() + blobName.substring(2, 4) + FILE_SEPARATOR.value()
+                            + blobName.substring(4, 6) + FILE_SEPARATOR.value() + blobName;
+                }
+                return item;
+            }).collect(Collectors.toList()));
+            long totalTime = System.currentTimeMillis() - start;
+
+            Map<Boolean, List<Downloader.ItemResponse>> partitioned =
+                    responses.stream().collect(Collectors.partitioningBy(ir -> ir.failed));
+
+            List<Downloader.ItemResponse> success = partitioned.get(false);
+            if (!success.isEmpty()) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("The following blobs were successfully downloaded:");
+                    success.forEach(s -> LOG.debug("{} [{}] downloaded in {} ms", s.item.source,
+                            IOUtils.humanReadableByteCount(s.size), s.time));
+                }
+
+                long totalBytes = success.stream().mapToLong(s -> s.size).sum();
+                if (totalTime > 60000) {
+                    LOG.info("Elapsed Time (Minutes): {}", TimeUnit.MILLISECONDS.toMinutes(totalTime));
+                } else {
+                    LOG.info("Elapsed Time (Seconds): {}", TimeUnit.MILLISECONDS.toSeconds(totalTime));
+                }
+                LOG.info("Number of File Transfers: {}", success.size());
+                LOG.info("TotalBytesTransferred: {}[{}]", totalBytes, IOUtils.humanReadableByteCount(totalBytes));

Review Comment:
   Total Bytes Transferred=



##########
oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCopyCommand.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.jackrabbit.oak.run;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import joptsimple.OptionSpecBuilder;
+import org.apache.jackrabbit.oak.commons.IOUtils;
+import org.apache.jackrabbit.oak.run.commons.Command;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.StandardSystemProperty.FILE_SEPARATOR;
+
+/**
+ * Command to concurrently download blobs from an azure datastore using sas token authentication.
+ * <p>
+ * Blobs are stored in a specific folder following the datastore structure format.
+ */
+public class DataStoreCopyCommand implements Command {
+
+    private static final Logger LOG = LoggerFactory.getLogger(DataStoreCopyCommand.class);
+
+    private String sourceRepo;
+    private String includePath;
+    private File fileIncludePath;
+    private String sasToken;
+    private String outDir;
+    private int concurrency;
+
+    @Override
+    public void execute(String... args) throws Exception {
+        parseCommandLineParams(args);
+
+        List<String> ids;
+        if (fileIncludePath != null) {
+            if (fileIncludePath.isDirectory() || !fileIncludePath.exists()) {
+                throw new IllegalArgumentException("file-include-path must be a valid file");
+            }
+            ids = org.apache.commons.io.IOUtils.readLines(Files.newInputStream(fileIncludePath.toPath()), StandardCharsets.UTF_8);
+        } else {
+            ids = Arrays.stream(includePath.split(";")).collect(Collectors.toList());
+        }
+        if (ids.isEmpty()) {
+            throw new IllegalArgumentException("Blob ids must be specified");
+        }
+
+        try (Downloader downloader = new Downloader(concurrency)) {
+            long start = System.currentTimeMillis();
+            List<Downloader.ItemResponse> responses = downloader.download(ids.stream().map(id -> {
+                Downloader.Item item = new Downloader.Item();
+                item.source = sourceRepo + "/" + id;
+                if (sasToken != null) {
+                    item.source += "?" + sasToken;
+                }
+                // Rename the blob names to match expected datastore cache format (remove the "-" in the name)
+                String blobName = id.replaceAll("-", "");
+                if (id.length() < 6) {
+                    LOG.warn("Blob with name {} is less than 6 chars. Cannot create data folder structure. Storing in the root folder", blobName);
+                    item.destination = outDir + FILE_SEPARATOR.value() + blobName;
+                } else {
+                    item.destination = outDir + FILE_SEPARATOR.value()
+                            + blobName.substring(0, 2) + FILE_SEPARATOR.value() + blobName.substring(2, 4) + FILE_SEPARATOR.value()
+                            + blobName.substring(4, 6) + FILE_SEPARATOR.value() + blobName;
+                }
+                return item;
+            }).collect(Collectors.toList()));
+            long totalTime = System.currentTimeMillis() - start;
+
+            Map<Boolean, List<Downloader.ItemResponse>> partitioned =
+                    responses.stream().collect(Collectors.partitioningBy(ir -> ir.failed));
+
+            List<Downloader.ItemResponse> success = partitioned.get(false);
+            if (!success.isEmpty()) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("The following blobs were successfully downloaded:");
+                    success.forEach(s -> LOG.debug("{} [{}] downloaded in {} ms", s.item.source,
+                            IOUtils.humanReadableByteCount(s.size), s.time));
+                }
+
+                long totalBytes = success.stream().mapToLong(s -> s.size).sum();
+                if (totalTime > 60000) {
+                    LOG.info("Elapsed Time (Minutes): {}", TimeUnit.MILLISECONDS.toMinutes(totalTime));

Review Comment:
   I would always log seconds, as that's easier to analyze if we need to, and simpler code :-) 



-- 
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@jackrabbit.apache.org

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