bipinprasad commented on a change in pull request #3366:
URL: https://github.com/apache/storm/pull/3366#discussion_r548497425



##########
File path: 
external/storm-hdfs-oci/src/main/java/org/apache/storm/container/oci/HdfsOciResourcesLocalizer.java
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.storm.container.oci;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.commons.io.FileDeleteStrategy;
+import org.apache.commons.io.FileUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.storm.DaemonConfig;
+import org.apache.storm.utils.ConfigUtils;
+import org.apache.storm.utils.HadoopLoginUtil;
+import org.apache.storm.utils.ObjectReader;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HdfsOciResourcesLocalizer implements 
OciResourcesLocalizerInterface {
+    private static final Logger LOG = 
LoggerFactory.getLogger(HdfsOciResourcesLocalizer.class);
+    private static final int LOCALIZE_MAX_RETRY = 5;
+    private String layersLocalDir;
+    private String configLocalDir;
+    private FileSystem fs;
+
+    /**
+     * Initialization.
+     * @param conf the storm conf.
+     * @throws IOException on I/O exception
+     */
+    public void init(Map<String, Object> conf) throws IOException {
+        //login to hdfs
+        HadoopLoginUtil.loginHadoop(conf);
+
+        String resourcesLocalDir = 
ObjectReader.getString(conf.get(DaemonConfig.STORM_OCI_RESOURCES_LOCAL_DIR),
+            ConfigUtils.supervisorLocalDir(conf) + "/oci-resources");
+        FileUtils.forceMkdir(new File(resourcesLocalDir));
+        this.layersLocalDir = resourcesLocalDir + "/layers/";
+        this.configLocalDir = resourcesLocalDir + "/config/";
+        String topLevelDir = 
ObjectReader.getString(conf.get(DaemonConfig.STORM_OCI_IMAGE_HDFS_TOPLEVEL_DIR));
+        this.fs = new Path(topLevelDir).getFileSystem(new Configuration());
+    }
+
+    /**
+     * Download the resources from HDFS to local dir.
+     * @param ociResource The oci resource to download
+     * @return the destination of the oci resource
+     * @throws IOException on I/O exception
+     */
+    public synchronized String localize(OciResource ociResource) throws 
IOException {
+        if (ociResource == null) {
+            return null;
+        }
+        File dst;
+        switch (ociResource.getType()) {
+            case CONFIG:
+                dst = new File(this.configLocalDir, ociResource.getFileName());
+                break;
+            case LAYER:
+                dst = new File(layersLocalDir, ociResource.getFileName());
+                break;
+            default:
+                throw new IOException("unknown OciResourceType " + 
ociResource.getType());
+        }
+
+        if (dst.exists()) {
+            LOG.info("{} already exists. Skip", dst);
+        } else {
+            // create working dir, copy file here, and set readable, then move 
to final location.
+            // this allows the operation to be atomic in case the supervisor 
dies.
+            File workingDir = new File(dst.getParent() + "/working");
+            workingDir.mkdir();
+            File workingDst = new File(workingDir.getPath() + "/" + 
dst.getName());
+
+            LOG.info("Starting to copy {} from hdfs to {}", 
ociResource.getPath(), workingDst.toString());
+            copyFileLocallyWithRetry(ociResource, workingDst);
+            LOG.info("Successfully finished copying {} from hdfs to {}", 
ociResource.getPath(), workingDst.toString());
+
+            //set to readable by anyone
+            boolean setReadable = workingDst.setReadable(true, false);
+            if (!setReadable) {
+                throw new IOException("Couldn't set " + workingDst + " to be 
world-readable");
+            }
+            workingDst.renameTo(dst);

Review comment:
       nit: return value is not checked for failure




----------------------------------------------------------------
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.

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


Reply via email to