You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@storm.apache.org by zhuoliu <gi...@git.apache.org> on 2016/01/20 17:03:45 UTC

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

GitHub user zhuoliu opened a pull request:

    https://github.com/apache/storm/pull/1030

    [STORM-1227] port org.apache.storm.config to java

    1. Port "org.apache.storm.config" to java at "org.apache.storm.config.ConfigUtils".
    2. Update all the callings to the config functions.
    3. Mock the java static functions for test purposes.
    4. Address issues in unit and manual tests.
    5. Comment out most functions in config.clj except a few: cluster-mode (used in multimethod),  ALL-CONFIGS, clojure-config-name.


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/zhuoliu/storm 1227

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/storm/pull/1030.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #1030
    
----
commit 6ce984128fd92e42ae7018d6b27be753e7dfa30d
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-13T19:55:47Z

    [STORM-1227] Init commit for config.clj function translation to java

commit 0b90a63d5ac7075524e9fabc7a49bda47b41b946
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-13T22:45:39Z

    To continue update callers to read-storm-config

commit cb4cdf6910b9bc8bc9077d5ad0750707d14e5cb2
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-14T19:44:12Z

    mock read-storm-config in drpc_test

commit f350f1ae9aaa44ea8f931c5e48eef5051d79770d
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-14T20:34:43Z

    Update all the callings to read-storm-config and add the import for ConfigUtils

commit 3d2796d2fbd7ae182d7414db985cf7de167fcd82
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-14T21:25:47Z

    Update callings to supervisor-isupervisor-dir

commit 635488db5e0670f1e4348fab5e3fd7d3970c5e63
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-15T18:50:00Z

    Updated to callings for supervisor-stormjar-path

commit 99189a6016d24aff485b9206323ad68988d32be1
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-15T19:10:20Z

    Updated to supervisor-storm-resources-path

commit 310128d9469f2151851d4e44710593b6ebcbac59
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-15T19:33:52Z

    updated to nimbus-topo-history-state

commit ba70bf1cfe72aeabec104d8ea664f087b719a08c
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-15T20:28:04Z

    Updated to read-supervisor-storm-conf

commit 0dbdcf39bf411a4ee5abac5e740c07e8813ea025
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-17T22:37:35Z

    Updates workerRoot, worker user, path, state, set. To do a few left.

commit 7c3fec6502ab23855a0b31dbb93debe7e22e73ff
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-19T17:23:26Z

    Updated callings before get-topo-logs-users

commit 13ab1515c503ad3eefe10871b5972d4bce60fde6
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-19T17:38:05Z

    Updated all callings

commit fd37de911ad73cf1a6f0d5500d0c1532a7ece209
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-19T22:54:10Z

    Updated for all the rest that we can do

commit b4847b76f0222344a51987fce1c8180616a4c279
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-19T23:33:51Z

    Manual test

commit b7374d2bb4cf49111936ec1cbd2a4f4f22661100
Author: zhuol <zh...@yahoo-inc.com>
Date:   2016-01-20T15:54:30Z

    Cleanup the comments

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50329212
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    --- End diff --
    
    removed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50302203
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (LOG_DIR + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (LOG_DIR + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id, Integer port) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf, id) + FILE_SEPARATOR + port);
    +    }
    +
    +    public static String workerArtifactsPidPath(Map conf, String id, Integer port) {
    +        return (workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR +  "worker.pid");
    +    }
    +
    +    public static File getLogMetaDataFile(String fname) {
    +        String[] subStrings = fname.split(FILE_SEPARATOR); // TODO: does this work well on windows?
    +        String id = subStrings[0];
    +        Integer port = Integer.parseInt(subStrings[1]);
    +        return getLogMetaDataFile(Utils.readStormConfig(), id, port);
    +    }
    +
    +    public static File getLogMetaDataFile(Map conf, String id, Integer port) {
    +        String fname = workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR + "worker.yaml";
    +        return new File(fname);
    +    }
    +
    +    public static File getWorkerDirFromRoot(String logRoot, String id, Integer port) {
    +        return new File((logRoot + FILE_SEPARATOR + id + FILE_SEPARATOR + port));
    +    }
    +
    +    public static String workerRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "workers");
    +    }
    +
    +    public static String workerRoot(Map conf, String id) {
    +        return (workerRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerPidsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "pids");
    +    }
    +
    +    public static String workerPidPath(Map conf, String id, String pid) {
    +        return (workerPidsRoot(conf, id) + FILE_SEPARATOR + pid);
    +    }
    +
    +    public static String workerHeartbeatsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "heartbeats");
    +    }
    +
    +    public static LocalState workerState(Map conf, String id) throws IOException {
    +        return new LocalState(workerHeartbeatsRoot(conf, id));
    +    }
    +
    +    public static Map overrideLoginConfigWithSystemProperty(Map conf) { // note that we delete the return value
    +        String loginConfFile = System.getProperty("java.security.auth.login.config");
    +        if (loginConfFile != null) {
    +             conf.put("java.security.auth.login.config", loginConfFile);
    +        }
    +        return conf;
    +    }
    +
    +    /* TODO: make sure test these two functions in manual tests */
    +    public static List<String> getTopoLogsUsers(Map topologyConf) {
    +        List<String> logsUsers = (List<String>)topologyConf.get(Config.LOGS_USERS);
    +        List<String> topologyUsers = (List<String>)topologyConf.get(Config.TOPOLOGY_USERS);
    +        Set<String> mergedUsers = new HashSet<String>();
    +        if (logsUsers != null) {
    +            for (String user : logsUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    +        if (topologyUsers != null) {
    +            for (String user : topologyUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    +        List<String> ret = new ArrayList<String>(mergedUsers);
    +        Collections.sort(ret);
    +        return ret;
    +    }
    +
    +    public static List<String> getTopoLogsGroups(Map topologyConf) {
    +        List<String> logsGroups = (List<String>)topologyConf.get(Config.LOGS_GROUPS);
    +        List<String> topologyGroups = (List<String>)topologyConf.get(Config.TOPOLOGY_GROUPS);
    +        Set<String> mergedGroups = new HashSet<String>();
    +        if (logsGroups != null) {
    +            for (String group : logsGroups) {
    +                if (group != null) {
    +                    mergedGroups.add(group);
    +                }
    +            }
    +        }
    +        if (topologyGroups != null) {
    +            for (String group : topologyGroups) {
    +                if (group != null) {
    +                    mergedGroups.add(group);
    +                }
    +            }
    +        }
    --- End diff --
    
    See the above comment for users.  This should apply to groups as well.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50284634
  
    --- Diff: storm-core/src/clj/org/apache/storm/config.clj ---
    @@ -41,11 +41,30 @@
       (dofor [f (seq (.getFields Config))]
              (.get f nil)))
     
    -
    +;; TODO this function and its callings will be replace when nimbus and supervisor move to Java
     (defn cluster-mode
       [conf & args]
       (keyword (conf STORM-CLUSTER-MODE)))
     
    +(defn sampling-rate
    +  [conf]
    +  (->> (conf TOPOLOGY-STATS-SAMPLE-RATE)
    +    (/ 1)
    +    int))
    +
    +;; TODO this function together with sampling-rate are to be replaced with Java version when util.clj is in
    +(defn mk-stats-sampler
    +  [conf]
    +  (even-sampler (sampling-rate conf)))
    +
    +;; TODO this function is to be replaced with Java version when util.clj is in
    +(defn read-default-config
    +  []
    +  (clojurify-structure (Utils/readDefaultConfig)))
    +
    +
    +
    +(comment
    --- End diff --
    
    Why is all of this code commented out and not deleted?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50287342
  
    --- Diff: storm-core/src/clj/org/apache/storm/config.clj ---
    @@ -41,11 +41,30 @@
       (dofor [f (seq (.getFields Config))]
              (.get f nil)))
     
    -
    +;; TODO this function and its callings will be replace when nimbus and supervisor move to Java
     (defn cluster-mode
       [conf & args]
       (keyword (conf STORM-CLUSTER-MODE)))
     
    +(defn sampling-rate
    +  [conf]
    +  (->> (conf TOPOLOGY-STATS-SAMPLE-RATE)
    +    (/ 1)
    +    int))
    +
    +;; TODO this function together with sampling-rate are to be replaced with Java version when util.clj is in
    +(defn mk-stats-sampler
    +  [conf]
    +  (even-sampler (sampling-rate conf)))
    +
    +;; TODO this function is to be replaced with Java version when util.clj is in
    +(defn read-default-config
    +  []
    +  (clojurify-structure (Utils/readDefaultConfig)))
    --- End diff --
    
    These 2 functions are actually depending on util.clj. I may just translate those two util functions within this pull request and delete these two.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50408413
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (LOG_DIR + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (LOG_DIR + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id, Integer port) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf, id) + FILE_SEPARATOR + port);
    +    }
    +
    +    public static String workerArtifactsPidPath(Map conf, String id, Integer port) {
    +        return (workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR +  "worker.pid");
    +    }
    +
    +    public static File getLogMetaDataFile(String fname) {
    +        String[] subStrings = fname.split(FILE_SEPARATOR); // TODO: does this work well on windows?
    +        String id = subStrings[0];
    +        Integer port = Integer.parseInt(subStrings[1]);
    +        return getLogMetaDataFile(Utils.readStormConfig(), id, port);
    +    }
    +
    +    public static File getLogMetaDataFile(Map conf, String id, Integer port) {
    +        String fname = workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR + "worker.yaml";
    +        return new File(fname);
    +    }
    +
    +    public static File getWorkerDirFromRoot(String logRoot, String id, Integer port) {
    +        return new File((logRoot + FILE_SEPARATOR + id + FILE_SEPARATOR + port));
    +    }
    +
    +    public static String workerRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "workers");
    +    }
    +
    +    public static String workerRoot(Map conf, String id) {
    +        return (workerRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerPidsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "pids");
    +    }
    +
    +    public static String workerPidPath(Map conf, String id, String pid) {
    +        return (workerPidsRoot(conf, id) + FILE_SEPARATOR + pid);
    +    }
    +
    +    public static String workerHeartbeatsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "heartbeats");
    +    }
    +
    +    public static LocalState workerState(Map conf, String id) throws IOException {
    +        return new LocalState(workerHeartbeatsRoot(conf, id));
    +    }
    +
    +    public static Map overrideLoginConfigWithSystemProperty(Map conf) { // note that we delete the return value
    +        String loginConfFile = System.getProperty("java.security.auth.login.config");
    +        if (loginConfFile != null) {
    +             conf.put("java.security.auth.login.config", loginConfFile);
    +        }
    +        return conf;
    +    }
    +
    +    /* TODO: make sure test these two functions in manual tests */
    +    public static List<String> getTopoLogsUsers(Map topologyConf) {
    +        List<String> logsUsers = (List<String>)topologyConf.get(Config.LOGS_USERS);
    +        List<String> topologyUsers = (List<String>)topologyConf.get(Config.TOPOLOGY_USERS);
    +        Set<String> mergedUsers = new HashSet<String>();
    +        if (logsUsers != null) {
    +            for (String user : logsUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    +        if (topologyUsers != null) {
    +            for (String user : topologyUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    --- End diff --
    
    Good point I missed that, this is fine.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50288346
  
    --- Diff: storm-core/src/clj/org/apache/storm/config.clj ---
    @@ -41,11 +41,30 @@
       (dofor [f (seq (.getFields Config))]
              (.get f nil)))
     
    -
    +;; TODO this function and its callings will be replace when nimbus and supervisor move to Java
     (defn cluster-mode
       [conf & args]
       (keyword (conf STORM-CLUSTER-MODE)))
     
    +(defn sampling-rate
    +  [conf]
    +  (->> (conf TOPOLOGY-STATS-SAMPLE-RATE)
    +    (/ 1)
    +    int))
    +
    +;; TODO this function together with sampling-rate are to be replaced with Java version when util.clj is in
    +(defn mk-stats-sampler
    +  [conf]
    +  (even-sampler (sampling-rate conf)))
    +
    +;; TODO this function is to be replaced with Java version when util.clj is in
    +(defn read-default-config
    +  []
    +  (clojurify-structure (Utils/readDefaultConfig)))
    --- End diff --
    
    Either way is fine with me so long as they are not dropped.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50298176
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    --- End diff --
    
    Yes lets delete it.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50300449
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    --- End diff --
    
    This works, it is not the RE used in the clojure, but it should be fine.  My only comment would be to have static final strings for "-stormjar.jar", etc. and use the .length() of the static final string instead of the hard coded length in the substring.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50408252
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    --- End diff --
    
    Then lets take the model and expand things to be a more complete mock, and we can do this everywhere.  I really would like consistency in how this works for all of the static mocking we do, and the real issue is we need to support returning a null value.
    
    ```
        public static class SetMockedSupervisorLocalDir implements Closeable {
            public SetMockedSupervisorLocalDir(String dir) {
                mockedSupervisorLocalDir = dir;
                isSupervisorLocalDirMocked = true;
            }
            @Override
            public void close() {
                isSupervisorLocalDirMocked = false;
                mockedSupervisorLocalDir = null;
            }
        }
        private static String mockedSupervisorLocalDir = null;
        private static boolean isSupervisorLocalDirMocked = false;
    
        public static String supervisorLocalDir(Map conf) throws IOException {
            if (isSupervisorLocalDirMocked) {
                return mockedSupervisorLocalDir;
        ...
    ```
    
    
    



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50329739
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    --- End diff --
    
    Then it will need to depend on util.clj. Or I will translate the url-encode within this pull request.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/storm/pull/1030


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50597247
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,678 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +import java.net.URLEncoder;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +
    +    public static String getLogDir() {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            return new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = (String)conf.get(Config.STORM_HEALTH_CHECK_DIR);
    +        if (healthCheckDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf));
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + URLEncoder.encode(stormId, "UTF-8");
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for {}.", workerId);
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        final String STORM_JAR_SUFFIX = "-stormjar.jar";
    +        final String STORM_CODE_SUFFIX = "-stormcode.ser";
    +        final String STORM_CONF_SUFFIX = "-stormconf.ser";
    +
    +        String ret = null;
    +        if (key.endsWith(STORM_JAR_SUFFIX)) {
    +            ret = key.substring(0, key.length() - STORM_JAR_SUFFIX.length());
    +        } else if (key.endsWith(STORM_CODE_SUFFIX)) {
    +            ret = key.substring(0, key.length() - STORM_CODE_SUFFIX.length());
    +        } else if (key.endsWith(STORM_CONF_SUFFIX)) {
    +            ret = key.substring(0, key.length() - STORM_CONF_SUFFIX.length());
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (getLogDir() + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (getLogDir() + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            // if the mockedWorkerArtifactsRoot is set, return its value no matter what
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id, Integer port) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            // if the mockedWorkerArtifactsRoot is set, return its value no matter what
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf, id) + FILE_SEPARATOR + port);
    +    }
    +
    +    public static String workerArtifactsPidPath(Map conf, String id, Integer port) {
    +        return (workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR +  "worker.pid");
    +    }
    +
    +    public static File getLogMetaDataFile(String fname) {
    +        String[] subStrings = fname.split(FILE_SEPARATOR); // TODO: does this work well on windows?
    +        String id = subStrings[0];
    +        Integer port = Integer.parseInt(subStrings[1]);
    +        return getLogMetaDataFile(Utils.readStormConfig(), id, port);
    +    }
    +
    +    public static File getLogMetaDataFile(Map conf, String id, Integer port) {
    +        String fname = workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR + "worker.yaml";
    +        return new File(fname);
    +    }
    +
    +    public static File getWorkerDirFromRoot(String logRoot, String id, Integer port) {
    +        return new File((logRoot + FILE_SEPARATOR + id + FILE_SEPARATOR + port));
    +    }
    +
    +    public static String workerRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "workers");
    +    }
    +
    +    public static String workerRoot(Map conf, String id) {
    +        return (workerRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerPidsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "pids");
    +    }
    +
    +    public static String workerPidPath(Map conf, String id, String pid) {
    +        return (workerPidsRoot(conf, id) + FILE_SEPARATOR + pid);
    +    }
    +
    +    public static String workerHeartbeatsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "heartbeats");
    +    }
    +
    +    public static LocalState workerState(Map conf, String id) throws IOException {
    +        return new LocalState(workerHeartbeatsRoot(conf, id));
    +    }
    +
    +    public static Map overrideLoginConfigWithSystemProperty(Map conf) { // note that we delete the return value
    +        String loginConfFile = System.getProperty("java.security.auth.login.config");
    +        if (loginConfFile != null) {
    +             conf.put("java.security.auth.login.config", loginConfFile);
    +        }
    +        return conf;
    +    }
    +
    +    /* TODO: make sure test these two functions in manual tests */
    +    public static List<String> getTopoLogsUsers(Map topologyConf) {
    +        List<String> logsUsers = (List<String>)topologyConf.get(Config.LOGS_USERS);
    +        List<String> topologyUsers = (List<String>)topologyConf.get(Config.TOPOLOGY_USERS);
    +        Set<String> mergedUsers = new HashSet<String>();
    +        if (logsUsers != null) {
    +            for (String user : logsUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    +        if (topologyUsers != null) {
    +            for (String user : topologyUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    +        List<String> ret = new ArrayList<String>(mergedUsers);
    +        Collections.sort(ret);
    +        return ret;
    +    }
    +
    +    public static List<String> getTopoLogsGroups(Map topologyConf) {
    +        List<String> logsGroups = (List<String>)topologyConf.get(Config.LOGS_GROUPS);
    +        List<String> topologyGroups = (List<String>)topologyConf.get(Config.TOPOLOGY_GROUPS);
    +        Set<String> mergedGroups = new HashSet<String>();
    +        if (logsGroups != null) {
    +            for (String group : logsGroups) {
    +                if (group != null) {
    +                    mergedGroups.add(group);
    +                }
    +            }
    +        }
    +        if (topologyGroups != null) {
    +            for (String group : topologyGroups) {
    +                if (group != null) {
    +                    mergedGroups.add(group);
    +                }
    +            }
    +        }
    +        List<String> ret = new ArrayList<String>(mergedGroups);
    +        Collections.sort(ret);
    +        return ret;
    +    }
    +}
    --- End diff --
    
    Addressed.



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50301841
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    --- End diff --
    
    Yes, this is a direct translation master-local-dir does have a forceMkdir with it. 
    
    So where should we put those forceMkdir, or we require the calling code to check and create by themselves? I can file a JIRA for that if needed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50298796
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    --- End diff --
    
    If clj does not, lets not do it either, just remove the TODO.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50284210
  
    --- Diff: storm-core/src/clj/org/apache/storm/config.clj ---
    @@ -18,14 +18,14 @@
       (:import [java.io FileReader File IOException]
                [org.apache.storm.generated StormTopology])
       (:import [org.apache.storm Config])
    -  (:import [org.apache.storm.utils Utils LocalState])
    +  (:import [org.apache.storm.utils Utils LocalState ConfigUtils])
       (:import [org.apache.storm.validation ConfigValidation])
       (:import [org.apache.commons.io FileUtils])
       (:require [clojure [string :as str]])
       (:use [org.apache.storm log util]))
     
    -(def RESOURCES-SUBDIR "resources")
    -(def NIMBUS-DO-NOT-REASSIGN "NIMBUS-DO-NOT-REASSIGN")
    +;; (def RESOURCES-SUBDIR "resources")
    +;; (def NIMBUS-DO-NOT-REASSIGN "NIMBUS-DO-NOT-REASSIGN")
    --- End diff --
    
    If these are not used we should delete them, not comment them out.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50299666
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    --- End diff --
    
    This matches the code, but I don't think we want the extra "/"  we should file a JIRA to check on that. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by knusbaum <gi...@git.apache.org>.
Github user knusbaum commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50459185
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,678 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +import java.net.URLEncoder;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +
    +    public static String getLogDir() {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            return new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    --- End diff --
    
    Should we maybe call this MockedStormConfigGuard or something with 'guard' in it? It would be nice to directly indicate that this object is an RAII-style guard.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50301038
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    --- End diff --
    
    Actually here we should return null. I know it is not straightforward: by default, mockedSupervisorState is null. Only when the test set this variable to be not null then we return "null". 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by knusbaum <gi...@git.apache.org>.
Github user knusbaum commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50462413
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,678 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +import java.net.URLEncoder;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +
    +    public static String getLogDir() {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            return new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = (String)conf.get(Config.STORM_HEALTH_CHECK_DIR);
    +        if (healthCheckDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf));
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + URLEncoder.encode(stormId, "UTF-8");
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    --- End diff --
    
    This pattern is repeated many, many times in this file. Can we move this to its own method and call that from here?
    ```
    public static String supervisorStormBasePath(String stormRoot) {
            String ret = "";
            if (stormRoot != null) {
                ret = stormRoot;
            }
            return ret + FILE_SEPARATOR;
        }
    
    public static String supervisorStormJarPath(String stormRoot) {
        return supervisorStormBasePath(stormRoot) + "stormjar.jar";
    }
    ...
    ```



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50300594
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    --- End diff --
    
    This should be renamed to something like IgnoreMockedWorkerUserWSE


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50298950
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    --- End diff --
    
    Yes we need to do the url-encode


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50301231
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    --- End diff --
    
    Or we can do the opposite, by default this "mockedSupervisorState" is not null, only when test set this not be null, we return null in the static function


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by knusbaum <gi...@git.apache.org>.
Github user knusbaum commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50453900
  
    --- Diff: storm-core/src/clj/org/apache/storm/config.clj ---
    @@ -41,291 +38,18 @@
       (dofor [f (seq (.getFields Config))]
              (.get f nil)))
     
    -
    +;; TODO this function and its callings will be replace when nimbus and supervisor move to Java
     (defn cluster-mode
       [conf & args]
       (keyword (conf STORM-CLUSTER-MODE)))
     
    -(defn local-mode?
    -  [conf]
    -  (let [mode (conf STORM-CLUSTER-MODE)]
    -    (condp = mode
    -      "local" true
    -      "distributed" false
    -      (throw (IllegalArgumentException.
    -               (str "Illegal cluster mode in conf: " mode))))))
    -
     (defn sampling-rate
       [conf]
       (->> (conf TOPOLOGY-STATS-SAMPLE-RATE)
    -       (/ 1)
    -       int))
    +    (/ 1)
    +    int))
     
    +;; TODO this function together with sampling-rate are to be replaced with Java version when util.clj is in
     (defn mk-stats-sampler
       [conf]
    -  (even-sampler (sampling-rate conf)))
    -
    -(defn read-default-config
    -  []
    -  (clojurify-structure (Utils/readDefaultConfig)))
    -
    -(defn validate-configs-with-schemas
    -  [conf]
    -  (ConfigValidation/validateFields conf))
    -
    -(defn read-storm-config
    -  []
    -  (let [conf (clojurify-structure (Utils/readStormConfig))]
    -    (validate-configs-with-schemas conf)
    -    conf))
    -
    -(defn read-yaml-config
    -  ([name must-exist]
    -     (let [conf (clojurify-structure (Utils/findAndReadConfigFile name must-exist))]
    -       (validate-configs-with-schemas conf)
    -       conf))
    -  ([name]
    -     (read-yaml-config true)))
    -
    -(defn absolute-storm-local-dir [conf]
    -  (let [storm-home (System/getProperty "storm.home")
    -        path (conf STORM-LOCAL-DIR)]
    -    (if path
    -      (if (is-absolute-path? path) path (str storm-home file-path-separator path))
    -      (str storm-home file-path-separator "storm-local"))))
    -
    -(def LOG-DIR
    -  (.getCanonicalPath
    -    (clojure.java.io/file (or (System/getProperty "storm.log.dir")
    -                              (get (read-storm-config) "storm.log.dir")
    -                              (str (System/getProperty "storm.home") file-path-separator "logs")))))
    -
    -(defn absolute-healthcheck-dir [conf]
    -  (let [storm-home (System/getProperty "storm.home")
    -        path (conf STORM-HEALTH-CHECK-DIR)]
    -    (if path
    -      (if (is-absolute-path? path) path (str storm-home file-path-separator path))
    -      (str storm-home file-path-separator "healthchecks"))))
    -
    -(defn master-local-dir
    -  [conf]
    -  (let [ret (str (absolute-storm-local-dir conf) file-path-separator "nimbus")]
    -    (FileUtils/forceMkdir (File. ret))
    -    ret))
    -
    -(defn master-stormjar-key
    -  [topology-id]
    -  (str topology-id "-stormjar.jar"))
    -
    -(defn master-stormcode-key
    -  [topology-id]
    -  (str topology-id "-stormcode.ser"))
    -
    -(defn master-stormconf-key
    -  [topology-id]
    -  (str topology-id "-stormconf.ser"))
    -
    -(defn master-stormdist-root
    -  ([conf]
    -   (str (master-local-dir conf) file-path-separator "stormdist"))
    -  ([conf storm-id]
    -   (str (master-stormdist-root conf) file-path-separator storm-id)))
    -
    -(defn master-tmp-dir
    -  [conf]
    -  (let [ret (str (master-local-dir conf) file-path-separator "tmp")]
    -    (FileUtils/forceMkdir (File. ret))
    -    ret ))
    -
    -(defn read-supervisor-storm-conf-given-path
    -  [conf stormconf-path]
    -  (merge conf (clojurify-structure (Utils/fromCompressedJsonConf (FileUtils/readFileToByteArray (File. stormconf-path))))))
    -
    -(defn master-storm-metafile-path [stormroot ]
    -  (str stormroot file-path-separator "storm-code-distributor.meta"))
    -
    -(defn master-stormjar-path
    -  [stormroot]
    -  (str stormroot file-path-separator "stormjar.jar"))
    -
    -(defn master-stormcode-path
    -  [stormroot]
    -  (str stormroot file-path-separator "stormcode.ser"))
    -
    -(defn master-stormconf-path
    -  [stormroot]
    -  (str stormroot file-path-separator "stormconf.ser"))
    -
    -(defn master-inbox
    -  [conf]
    -  (let [ret (str (master-local-dir conf) file-path-separator "inbox")]
    -    (FileUtils/forceMkdir (File. ret))
    -    ret ))
    -
    -(defn master-inimbus-dir
    -  [conf]
    -  (str (master-local-dir conf) file-path-separator "inimbus"))
    -
    -(defn supervisor-local-dir
    -  [conf]
    -  (let [ret (str (absolute-storm-local-dir conf) file-path-separator "supervisor")]
    -    (FileUtils/forceMkdir (File. ret))
    -    ret))
    -
    -(defn supervisor-isupervisor-dir
    -  [conf]
    -  (str (supervisor-local-dir conf) file-path-separator "isupervisor"))
    -
    -(defn supervisor-stormdist-root
    -  ([conf]
    -   (str (supervisor-local-dir conf) file-path-separator "stormdist"))
    -  ([conf storm-id]
    -   (str (supervisor-stormdist-root conf) file-path-separator (url-encode storm-id))))
    -
    -(defn supervisor-stormjar-path [stormroot]
    -  (str stormroot file-path-separator "stormjar.jar"))
    -
    -(defn supervisor-storm-metafile-path [stormroot]
    -  (str stormroot file-path-separator "storm-code-distributor.meta"))
    -
    -(defn supervisor-stormcode-path
    -  [stormroot]
    -  (str stormroot file-path-separator "stormcode.ser"))
    -
    -(defn supervisor-stormconf-path
    -  [stormroot]
    -  (str stormroot file-path-separator "stormconf.ser"))
    -
    -(defn supervisor-tmp-dir
    -  [conf]
    -  (let [ret (str (supervisor-local-dir conf) file-path-separator "tmp")]
    -    (FileUtils/forceMkdir (File. ret))
    -    ret ))
    -
    -(defn supervisor-storm-resources-path
    -  [stormroot]
    -  (str stormroot file-path-separator RESOURCES-SUBDIR))
    -
    -(defn ^LocalState supervisor-state
    -  [conf]
    -  (LocalState. (str (supervisor-local-dir conf) file-path-separator "localstate")))
    -
    -(defn ^LocalState nimbus-topo-history-state
    -  [conf]
    -  (LocalState. (str (master-local-dir conf) file-path-separator "history")))
    -
    -(defn read-supervisor-storm-conf
    -  [conf storm-id]
    -  (let [stormroot (supervisor-stormdist-root conf storm-id)
    -        conf-path (supervisor-stormconf-path stormroot)]
    -    (read-supervisor-storm-conf-given-path conf conf-path)))
    -
    -(defn read-supervisor-topology
    -  [conf storm-id]
    -  (let [stormroot (supervisor-stormdist-root conf storm-id)
    -        topology-path (supervisor-stormcode-path stormroot)]
    -    (Utils/deserialize (FileUtils/readFileToByteArray (File. topology-path)) StormTopology)
    -    ))
    -
    -(defn worker-user-root [conf]
    -  (str (absolute-storm-local-dir conf) "/workers-users"))
    -
    -(defn worker-user-file [conf worker-id]
    -  (str (worker-user-root conf) "/" worker-id))
    -
    -(defn get-worker-user [conf worker-id]
    -  (log-message "GET worker-user " worker-id)
    -  (try
    -    (str/trim (slurp (worker-user-file conf worker-id)))
    -  (catch IOException e
    -    (log-warn-error e "Failed to get worker user for " worker-id ".")
    -    nil
    -    )))
    -
    -(defn get-id-from-blob-key
    -  [key]
    -  (if-let [groups (re-find #"^(.*)((-stormjar\.jar)|(-stormcode\.ser)|(-stormconf\.ser))$" key)]
    -    (nth groups 1)))
    -
    -(defn set-worker-user! [conf worker-id user]
    -  (log-message "SET worker-user " worker-id " " user)
    -  (let [file (worker-user-file conf worker-id)]
    -    (.mkdirs (.getParentFile (File. file)))
    -    (spit (worker-user-file conf worker-id) user)))
    -
    -(defn remove-worker-user! [conf worker-id]
    -  (log-message "REMOVE worker-user " worker-id)
    -  (.delete (File. (worker-user-file conf worker-id))))
    -
    -(defn worker-artifacts-root
    -  ([conf]
    -   (let [workers-artifacts-dir (conf STORM-WORKERS-ARTIFACTS-DIR)]
    -     (if workers-artifacts-dir
    -       (if (is-absolute-path? workers-artifacts-dir)
    -         workers-artifacts-dir
    -         (str LOG-DIR file-path-separator workers-artifacts-dir))
    -       (str LOG-DIR file-path-separator "workers-artifacts"))))
    -  ([conf id]
    -   (str (worker-artifacts-root conf) file-path-separator id))
    -  ([conf id port]
    -   (str (worker-artifacts-root conf id) file-path-separator port)))
    -
    -(defn worker-artifacts-pid-path
    -  [conf id port]
    -  (str (worker-artifacts-root conf id port) file-path-separator "worker.pid"))
    -
    -(defn get-log-metadata-file
    -  ([fname]
    -    (let [[id port & _] (str/split fname (re-pattern file-path-separator))]
    -      (get-log-metadata-file (read-storm-config) id port)))
    -  ([conf id port]
    -    (clojure.java.io/file (str (worker-artifacts-root conf id) file-path-separator port file-path-separator) "worker.yaml")))
    -
    -(defn get-worker-dir-from-root
    -  [log-root id port]
    -  (clojure.java.io/file (str log-root file-path-separator id file-path-separator port)))
    -
    -(defn worker-root
    -  ([conf]
    -   (str (absolute-storm-local-dir conf) file-path-separator "workers"))
    -  ([conf id]
    -   (str (worker-root conf) file-path-separator id)))
    -
    -(defn worker-pids-root
    -  [conf id]
    -  (str (worker-root conf id) file-path-separator "pids"))
    -
    -(defn worker-pid-path
    -  [conf id pid]
    -  (str (worker-pids-root conf id) file-path-separator pid))
    -
    -(defn worker-heartbeats-root
    -  [conf id]
    -  (str (worker-root conf id) file-path-separator "heartbeats"))
    -
    -;; workers heartbeat here with pid and timestamp
    -;; if supervisor stops receiving heartbeat, it kills and restarts the process
    -;; in local mode, keep a global map of ids to threads for simulating process management
    -(defn ^LocalState worker-state
    -  [conf id]
    -  (LocalState. (worker-heartbeats-root conf id)))
    -
    -(defn override-login-config-with-system-property [conf]
    -  (if-let [login_conf_file (System/getProperty "java.security.auth.login.config")]
    -    (assoc conf "java.security.auth.login.config" login_conf_file)
    -    conf))
    -
    -(defn get-topo-logs-users
    -  [topology-conf]
    -  (sort (distinct (remove nil?
    -                    (concat
    -                      (topology-conf LOGS-USERS)
    -                      (topology-conf TOPOLOGY-USERS))))))
    -
    -(defn get-topo-logs-groups
    -  [topology-conf]
    -  (sort (distinct (remove nil?
    -                    (concat
    -                      (topology-conf LOGS-GROUPS)
    -                      (topology-conf TOPOLOGY-GROUPS))))))
    -
    +  (even-sampler (sampling-rate conf)))
    --- End diff --
    
    Please don't remove the newline from this file.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50332475
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    --- End diff --
    
    Mark here: may need to file a JIRA.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by d2r <gi...@git.apache.org>.
Github user d2r commented on the pull request:

    https://github.com/apache/storm/pull/1030#issuecomment-173704553
  
    > I will post a link when I have an example.
    
    new [staticmocking](https://github.com/d2r/storm/tree/56d48faac918123af46f1e021e38549fa6a4cf6c/storm-core/src/jvm/org/apache/storm/testing/staticmocking) package
    
    See example methods under test: [Utils](https://github.com/d2r/storm/blob/56d48faac918123af46f1e021e38549fa6a4cf6c/external/storm-hbase/src/main/java/org/apache/storm/hbase/common/Utils.java)
    
    Here is a (clojure) example of [usage](https://github.com/d2r/storm/blob/56d48faac918123af46f1e021e38549fa6a4cf6c/storm-core/test/clj/org/apache/storm/logviewer_test.clj#L372-L387). This will all eventually be Java, and it would use a try-with-resources syntax in place of the `with-open`.
    
    If this seems like a good pattern to follow, we can add it to the [migration guidelines](https://cwiki.apache.org/confluence/display/STORM/Java+Migration+Guidelines).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50602239
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (LOG_DIR + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (LOG_DIR + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id, Integer port) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf, id) + FILE_SEPARATOR + port);
    +    }
    +
    +    public static String workerArtifactsPidPath(Map conf, String id, Integer port) {
    +        return (workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR +  "worker.pid");
    +    }
    +
    +    public static File getLogMetaDataFile(String fname) {
    +        String[] subStrings = fname.split(FILE_SEPARATOR); // TODO: does this work well on windows?
    +        String id = subStrings[0];
    +        Integer port = Integer.parseInt(subStrings[1]);
    +        return getLogMetaDataFile(Utils.readStormConfig(), id, port);
    +    }
    +
    +    public static File getLogMetaDataFile(Map conf, String id, Integer port) {
    +        String fname = workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR + "worker.yaml";
    +        return new File(fname);
    +    }
    +
    +    public static File getWorkerDirFromRoot(String logRoot, String id, Integer port) {
    +        return new File((logRoot + FILE_SEPARATOR + id + FILE_SEPARATOR + port));
    +    }
    +
    +    public static String workerRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "workers");
    +    }
    +
    +    public static String workerRoot(Map conf, String id) {
    +        return (workerRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerPidsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "pids");
    +    }
    +
    +    public static String workerPidPath(Map conf, String id, String pid) {
    +        return (workerPidsRoot(conf, id) + FILE_SEPARATOR + pid);
    +    }
    +
    +    public static String workerHeartbeatsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "heartbeats");
    +    }
    +
    +    public static LocalState workerState(Map conf, String id) throws IOException {
    +        return new LocalState(workerHeartbeatsRoot(conf, id));
    +    }
    +
    +    public static Map overrideLoginConfigWithSystemProperty(Map conf) { // note that we delete the return value
    +        String loginConfFile = System.getProperty("java.security.auth.login.config");
    +        if (loginConfFile != null) {
    +             conf.put("java.security.auth.login.config", loginConfFile);
    +        }
    +        return conf;
    +    }
    +
    +    /* TODO: make sure test these two functions in manual tests */
    --- End diff --
    
    Manually Tested, passed. I will remove this TODO.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50287458
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    --- End diff --
    
    Will delete.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50333592
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    --- End diff --
    
    Fixed


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by knusbaum <gi...@git.apache.org>.
Github user knusbaum commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50461090
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,678 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +import java.net.URLEncoder;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +
    +    public static String getLogDir() {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            return new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = (String)conf.get(Config.STORM_HEALTH_CHECK_DIR);
    +        if (healthCheckDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    --- End diff --
    
    Double parens?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50303592
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (LOG_DIR + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (LOG_DIR + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id, Integer port) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf, id) + FILE_SEPARATOR + port);
    +    }
    +
    +    public static String workerArtifactsPidPath(Map conf, String id, Integer port) {
    +        return (workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR +  "worker.pid");
    +    }
    +
    +    public static File getLogMetaDataFile(String fname) {
    +        String[] subStrings = fname.split(FILE_SEPARATOR); // TODO: does this work well on windows?
    +        String id = subStrings[0];
    +        Integer port = Integer.parseInt(subStrings[1]);
    +        return getLogMetaDataFile(Utils.readStormConfig(), id, port);
    +    }
    +
    +    public static File getLogMetaDataFile(Map conf, String id, Integer port) {
    +        String fname = workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR + "worker.yaml";
    +        return new File(fname);
    +    }
    +
    +    public static File getWorkerDirFromRoot(String logRoot, String id, Integer port) {
    +        return new File((logRoot + FILE_SEPARATOR + id + FILE_SEPARATOR + port));
    +    }
    +
    +    public static String workerRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "workers");
    +    }
    +
    +    public static String workerRoot(Map conf, String id) {
    +        return (workerRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerPidsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "pids");
    +    }
    +
    +    public static String workerPidPath(Map conf, String id, String pid) {
    +        return (workerPidsRoot(conf, id) + FILE_SEPARATOR + pid);
    +    }
    +
    +    public static String workerHeartbeatsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "heartbeats");
    +    }
    +
    +    public static LocalState workerState(Map conf, String id) throws IOException {
    +        return new LocalState(workerHeartbeatsRoot(conf, id));
    +    }
    +
    +    public static Map overrideLoginConfigWithSystemProperty(Map conf) { // note that we delete the return value
    +        String loginConfFile = System.getProperty("java.security.auth.login.config");
    +        if (loginConfFile != null) {
    +             conf.put("java.security.auth.login.config", loginConfFile);
    +        }
    +        return conf;
    +    }
    +
    +    /* TODO: make sure test these two functions in manual tests */
    +    public static List<String> getTopoLogsUsers(Map topologyConf) {
    +        List<String> logsUsers = (List<String>)topologyConf.get(Config.LOGS_USERS);
    +        List<String> topologyUsers = (List<String>)topologyConf.get(Config.TOPOLOGY_USERS);
    +        Set<String> mergedUsers = new HashSet<String>();
    +        if (logsUsers != null) {
    +            for (String user : logsUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    +        if (topologyUsers != null) {
    +            for (String user : topologyUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    --- End diff --
    
    A reason for not doing so is that some user might be null or "null" in the configuration?
    I see there is "remove nil?" in original clojure code.
    ``
      (sort (distinct (remove nil?
                        (concat
                          (topology-conf LOGS-GROUPS)
                          (topology-conf TOPOLOGY-GROUPS))))))
    ``



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50298206
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    --- End diff --
    
    And the others that are not used.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50600899
  
    --- Diff: storm-core/src/jvm/org/apache/storm/testing/staticmocking/MockedConfigUtils.java ---
    @@ -0,0 +1,31 @@
    +/**
    + * 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.testing.staticmocking;
    +
    +import org.apache.storm.utils.ConfigUtils;
    +
    +public class MockedConfigUtils extends ConfigUtils implements AutoCloseable {
    --- End diff --
    
    Yep, the above documentation file will go in with util translation (STORM-1226)[https://issues.apache.org/jira/browse/STORM-1226].


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50301246
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (LOG_DIR + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (LOG_DIR + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id, Integer port) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf, id) + FILE_SEPARATOR + port);
    +    }
    +
    +    public static String workerArtifactsPidPath(Map conf, String id, Integer port) {
    +        return (workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR +  "worker.pid");
    +    }
    +
    +    public static File getLogMetaDataFile(String fname) {
    +        String[] subStrings = fname.split(FILE_SEPARATOR); // TODO: does this work well on windows?
    --- End diff --
    
    This does not even work all that well off windows.  We should file a JIRA to look at using File for this instead.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50288899
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    --- End diff --
    
    Extra line


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50287433
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    --- End diff --
    
    OK. Will turn it into a static function instead.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50298493
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    --- End diff --
    
    I think this should be `return mockedSupervisorLocalDir`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50284391
  
    --- Diff: storm-core/src/clj/org/apache/storm/config.clj ---
    @@ -41,11 +41,30 @@
       (dofor [f (seq (.getFields Config))]
              (.get f nil)))
     
    -
    +;; TODO this function and its callings will be replace when nimbus and supervisor move to Java
    --- End diff --
    
    Don't need the TODO.  Please file a separate JIRA to remove the code when it is no longer needed.  You can leave the comment, but preferably with the JIRA number instead.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by knusbaum <gi...@git.apache.org>.
Github user knusbaum commented on the pull request:

    https://github.com/apache/storm/pull/1030#issuecomment-176276904
  
    +1


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50302004
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (LOG_DIR + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (LOG_DIR + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id, Integer port) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf, id) + FILE_SEPARATOR + port);
    +    }
    +
    +    public static String workerArtifactsPidPath(Map conf, String id, Integer port) {
    +        return (workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR +  "worker.pid");
    +    }
    +
    +    public static File getLogMetaDataFile(String fname) {
    +        String[] subStrings = fname.split(FILE_SEPARATOR); // TODO: does this work well on windows?
    +        String id = subStrings[0];
    +        Integer port = Integer.parseInt(subStrings[1]);
    +        return getLogMetaDataFile(Utils.readStormConfig(), id, port);
    +    }
    +
    +    public static File getLogMetaDataFile(Map conf, String id, Integer port) {
    +        String fname = workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR + "worker.yaml";
    +        return new File(fname);
    +    }
    +
    +    public static File getWorkerDirFromRoot(String logRoot, String id, Integer port) {
    +        return new File((logRoot + FILE_SEPARATOR + id + FILE_SEPARATOR + port));
    +    }
    +
    +    public static String workerRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "workers");
    +    }
    +
    +    public static String workerRoot(Map conf, String id) {
    +        return (workerRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerPidsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "pids");
    +    }
    +
    +    public static String workerPidPath(Map conf, String id, String pid) {
    +        return (workerPidsRoot(conf, id) + FILE_SEPARATOR + pid);
    +    }
    +
    +    public static String workerHeartbeatsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "heartbeats");
    +    }
    +
    +    public static LocalState workerState(Map conf, String id) throws IOException {
    +        return new LocalState(workerHeartbeatsRoot(conf, id));
    +    }
    +
    +    public static Map overrideLoginConfigWithSystemProperty(Map conf) { // note that we delete the return value
    +        String loginConfFile = System.getProperty("java.security.auth.login.config");
    +        if (loginConfFile != null) {
    +             conf.put("java.security.auth.login.config", loginConfFile);
    +        }
    +        return conf;
    +    }
    +
    +    /* TODO: make sure test these two functions in manual tests */
    +    public static List<String> getTopoLogsUsers(Map topologyConf) {
    +        List<String> logsUsers = (List<String>)topologyConf.get(Config.LOGS_USERS);
    +        List<String> topologyUsers = (List<String>)topologyConf.get(Config.TOPOLOGY_USERS);
    +        Set<String> mergedUsers = new HashSet<String>();
    +        if (logsUsers != null) {
    +            for (String user : logsUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    --- End diff --
    
    This can be reduced to
    ```
    if (logsUsers != null) {
      mergedUsers.addAll(logsUsers);
    }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50287206
  
    --- Diff: storm-core/src/clj/org/apache/storm/config.clj ---
    @@ -41,11 +41,30 @@
       (dofor [f (seq (.getFields Config))]
              (.get f nil)))
     
    -
    +;; TODO this function and its callings will be replace when nimbus and supervisor move to Java
     (defn cluster-mode
       [conf & args]
       (keyword (conf STORM-CLUSTER-MODE)))
     
    +(defn sampling-rate
    +  [conf]
    +  (->> (conf TOPOLOGY-STATS-SAMPLE-RATE)
    +    (/ 1)
    +    int))
    +
    +;; TODO this function together with sampling-rate are to be replaced with Java version when util.clj is in
    +(defn mk-stats-sampler
    +  [conf]
    +  (even-sampler (sampling-rate conf)))
    +
    +;; TODO this function is to be replaced with Java version when util.clj is in
    +(defn read-default-config
    +  []
    +  (clojurify-structure (Utils/readDefaultConfig)))
    +
    +
    +
    +(comment
    --- End diff --
    
    Will delete.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50301097
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (LOG_DIR + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (LOG_DIR + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id, Integer port) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    --- End diff --
    
    Same comment here about why we are not including the port/etc in this.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50299218
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    --- End diff --
    
    `return mockedSupervisorState`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50300517
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    --- End diff --
    
    This should be a boolean.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by knusbaum <gi...@git.apache.org>.
Github user knusbaum commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50455607
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,678 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +import java.net.URLEncoder;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +
    +    public static String getLogDir() {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    --- End diff --
    
    Can we avoid calling System.getProperty twice by assigning at declaration of dir?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50287040
  
    --- Diff: storm-core/src/clj/org/apache/storm/config.clj ---
    @@ -41,11 +41,30 @@
       (dofor [f (seq (.getFields Config))]
              (.get f nil)))
     
    -
    +;; TODO this function and its callings will be replace when nimbus and supervisor move to Java
    --- End diff --
    
    Will address.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50284660
  
    --- Diff: storm-core/src/clj/org/apache/storm/config.clj ---
    @@ -41,11 +41,30 @@
       (dofor [f (seq (.getFields Config))]
              (.get f nil)))
     
    -
    +;; TODO this function and its callings will be replace when nimbus and supervisor move to Java
     (defn cluster-mode
       [conf & args]
       (keyword (conf STORM-CLUSTER-MODE)))
     
    +(defn sampling-rate
    +  [conf]
    +  (->> (conf TOPOLOGY-STATS-SAMPLE-RATE)
    +    (/ 1)
    +    int))
    +
    +;; TODO this function together with sampling-rate are to be replaced with Java version when util.clj is in
    +(defn mk-stats-sampler
    +  [conf]
    +  (even-sampler (sampling-rate conf)))
    +
    +;; TODO this function is to be replaced with Java version when util.clj is in
    +(defn read-default-config
    +  []
    +  (clojurify-structure (Utils/readDefaultConfig)))
    --- End diff --
    
    Are these functions still needed?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on the pull request:

    https://github.com/apache/storm/pull/1030#issuecomment-174074220
  
    Just one very very minor comment.  I am +1 on this either way.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by knusbaum <gi...@git.apache.org>.
Github user knusbaum commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50468248
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,678 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +import java.net.URLEncoder;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +
    +    public static String getLogDir() {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            return new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = (String)conf.get(Config.STORM_HEALTH_CHECK_DIR);
    +        if (healthCheckDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf));
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + URLEncoder.encode(stormId, "UTF-8");
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for {}.", workerId);
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        final String STORM_JAR_SUFFIX = "-stormjar.jar";
    +        final String STORM_CODE_SUFFIX = "-stormcode.ser";
    +        final String STORM_CONF_SUFFIX = "-stormconf.ser";
    +
    +        String ret = null;
    +        if (key.endsWith(STORM_JAR_SUFFIX)) {
    +            ret = key.substring(0, key.length() - STORM_JAR_SUFFIX.length());
    +        } else if (key.endsWith(STORM_CODE_SUFFIX)) {
    +            ret = key.substring(0, key.length() - STORM_CODE_SUFFIX.length());
    +        } else if (key.endsWith(STORM_CONF_SUFFIX)) {
    +            ret = key.substring(0, key.length() - STORM_CONF_SUFFIX.length());
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (getLogDir() + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (getLogDir() + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            // if the mockedWorkerArtifactsRoot is set, return its value no matter what
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id, Integer port) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            // if the mockedWorkerArtifactsRoot is set, return its value no matter what
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf, id) + FILE_SEPARATOR + port);
    +    }
    +
    +    public static String workerArtifactsPidPath(Map conf, String id, Integer port) {
    +        return (workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR +  "worker.pid");
    +    }
    +
    +    public static File getLogMetaDataFile(String fname) {
    +        String[] subStrings = fname.split(FILE_SEPARATOR); // TODO: does this work well on windows?
    +        String id = subStrings[0];
    +        Integer port = Integer.parseInt(subStrings[1]);
    +        return getLogMetaDataFile(Utils.readStormConfig(), id, port);
    +    }
    +
    +    public static File getLogMetaDataFile(Map conf, String id, Integer port) {
    +        String fname = workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR + "worker.yaml";
    +        return new File(fname);
    +    }
    +
    +    public static File getWorkerDirFromRoot(String logRoot, String id, Integer port) {
    +        return new File((logRoot + FILE_SEPARATOR + id + FILE_SEPARATOR + port));
    +    }
    +
    +    public static String workerRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "workers");
    +    }
    +
    +    public static String workerRoot(Map conf, String id) {
    +        return (workerRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerPidsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "pids");
    +    }
    +
    +    public static String workerPidPath(Map conf, String id, String pid) {
    +        return (workerPidsRoot(conf, id) + FILE_SEPARATOR + pid);
    +    }
    +
    +    public static String workerHeartbeatsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "heartbeats");
    +    }
    +
    +    public static LocalState workerState(Map conf, String id) throws IOException {
    +        return new LocalState(workerHeartbeatsRoot(conf, id));
    +    }
    +
    +    public static Map overrideLoginConfigWithSystemProperty(Map conf) { // note that we delete the return value
    +        String loginConfFile = System.getProperty("java.security.auth.login.config");
    +        if (loginConfFile != null) {
    +             conf.put("java.security.auth.login.config", loginConfFile);
    +        }
    +        return conf;
    +    }
    +
    +    /* TODO: make sure test these two functions in manual tests */
    +    public static List<String> getTopoLogsUsers(Map topologyConf) {
    +        List<String> logsUsers = (List<String>)topologyConf.get(Config.LOGS_USERS);
    +        List<String> topologyUsers = (List<String>)topologyConf.get(Config.TOPOLOGY_USERS);
    +        Set<String> mergedUsers = new HashSet<String>();
    +        if (logsUsers != null) {
    +            for (String user : logsUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    +        if (topologyUsers != null) {
    +            for (String user : topologyUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    +        List<String> ret = new ArrayList<String>(mergedUsers);
    +        Collections.sort(ret);
    +        return ret;
    +    }
    +
    +    public static List<String> getTopoLogsGroups(Map topologyConf) {
    +        List<String> logsGroups = (List<String>)topologyConf.get(Config.LOGS_GROUPS);
    +        List<String> topologyGroups = (List<String>)topologyConf.get(Config.TOPOLOGY_GROUPS);
    +        Set<String> mergedGroups = new HashSet<String>();
    +        if (logsGroups != null) {
    +            for (String group : logsGroups) {
    +                if (group != null) {
    +                    mergedGroups.add(group);
    +                }
    +            }
    +        }
    +        if (topologyGroups != null) {
    +            for (String group : topologyGroups) {
    +                if (group != null) {
    +                    mergedGroups.add(group);
    +                }
    +            }
    +        }
    +        List<String> ret = new ArrayList<String>(mergedGroups);
    +        Collections.sort(ret);
    +        return ret;
    +    }
    +}
    --- End diff --
    
    Newline!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50299266
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    --- End diff --
    
    `return mockedNimbusTopoHistoryState;`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50298726
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    --- End diff --
    
    And here too `return  mockedSupervisorStormDistRoot`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by d2r <gi...@git.apache.org>.
Github user d2r commented on the pull request:

    https://github.com/apache/storm/pull/1030#issuecomment-173699059
  
    We have a separate implementation for mocking static methods that we think will also allow things like Mockito to track calls, arguments, and things of that sort. I will post a link when I have an example.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50285565
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    --- End diff --
    
    Exceptions in static code blocks are really confusing to debug.  I personally would prefer to see a static function to get the log dir, that caches the saved result. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on the pull request:

    https://github.com/apache/storm/pull/1030#issuecomment-174069422
  
    Thanks @revans2 @d2r @knusbaum for all your thorough comments, I addressed them. And I've taken Derek's proposed static mocking to replace the previous one.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50600024
  
    --- Diff: storm-core/src/jvm/org/apache/storm/testing/staticmocking/MockedConfigUtils.java ---
    @@ -0,0 +1,31 @@
    +/**
    + * 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.testing.staticmocking;
    +
    +import org.apache.storm.utils.ConfigUtils;
    +
    +public class MockedConfigUtils extends ConfigUtils implements AutoCloseable {
    --- End diff --
    
    Can we add some documentation about how to use this, like we had in the previous mocking code?  This is really minor though.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by d2r <gi...@git.apache.org>.
Github user d2r commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50703615
  
    --- Diff: storm-core/src/jvm/org/apache/storm/testing/staticmocking/MockedConfigUtils.java ---
    @@ -0,0 +1,31 @@
    +/**
    + * 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.testing.staticmocking;
    +
    +import org.apache.storm.utils.ConfigUtils;
    +
    +public class MockedConfigUtils extends ConfigUtils implements AutoCloseable {
    --- End diff --
    
    > Derek's repository has a file package-info.java, which shows how to use it. Do I need to include that within this pull request or wait util JIRA to do that? @d2r @revans2
    
    For what it's worth, it is fine with me if you want to include that file in this pull request.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on the pull request:

    https://github.com/apache/storm/pull/1030#issuecomment-175694846
  
    Hi @knusbaum , I addressed your comments. Could you check back on this one?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50299901
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    --- End diff --
    
    Lest use "{}" replacement here.  Very minor.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50602689
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (LOG_DIR + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (LOG_DIR + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id, Integer port) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf, id) + FILE_SEPARATOR + port);
    +    }
    +
    +    public static String workerArtifactsPidPath(Map conf, String id, Integer port) {
    +        return (workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR +  "worker.pid");
    +    }
    +
    +    public static File getLogMetaDataFile(String fname) {
    +        String[] subStrings = fname.split(FILE_SEPARATOR); // TODO: does this work well on windows?
    --- End diff --
    
    A jira filed for that in [STORM-1498](https://issues.apache.org/jira/browse/STORM-1498).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50301027
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (LOG_DIR + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (LOG_DIR + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    --- End diff --
    
    I'm not sure how the mocking worked in the clj code.  This looks OK, but is confusing because we are not including the ID.  It might be worth a comment here on why that is.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50302126
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (LOG_DIR + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (LOG_DIR + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id, Integer port) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf, id) + FILE_SEPARATOR + port);
    +    }
    +
    +    public static String workerArtifactsPidPath(Map conf, String id, Integer port) {
    +        return (workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR +  "worker.pid");
    +    }
    +
    +    public static File getLogMetaDataFile(String fname) {
    +        String[] subStrings = fname.split(FILE_SEPARATOR); // TODO: does this work well on windows?
    +        String id = subStrings[0];
    +        Integer port = Integer.parseInt(subStrings[1]);
    +        return getLogMetaDataFile(Utils.readStormConfig(), id, port);
    +    }
    +
    +    public static File getLogMetaDataFile(Map conf, String id, Integer port) {
    +        String fname = workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR + "worker.yaml";
    +        return new File(fname);
    +    }
    +
    +    public static File getWorkerDirFromRoot(String logRoot, String id, Integer port) {
    +        return new File((logRoot + FILE_SEPARATOR + id + FILE_SEPARATOR + port));
    +    }
    +
    +    public static String workerRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "workers");
    +    }
    +
    +    public static String workerRoot(Map conf, String id) {
    +        return (workerRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerPidsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "pids");
    +    }
    +
    +    public static String workerPidPath(Map conf, String id, String pid) {
    +        return (workerPidsRoot(conf, id) + FILE_SEPARATOR + pid);
    +    }
    +
    +    public static String workerHeartbeatsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "heartbeats");
    +    }
    +
    +    public static LocalState workerState(Map conf, String id) throws IOException {
    +        return new LocalState(workerHeartbeatsRoot(conf, id));
    +    }
    +
    +    public static Map overrideLoginConfigWithSystemProperty(Map conf) { // note that we delete the return value
    +        String loginConfFile = System.getProperty("java.security.auth.login.config");
    +        if (loginConfFile != null) {
    +             conf.put("java.security.auth.login.config", loginConfFile);
    +        }
    +        return conf;
    +    }
    +
    +    /* TODO: make sure test these two functions in manual tests */
    +    public static List<String> getTopoLogsUsers(Map topologyConf) {
    +        List<String> logsUsers = (List<String>)topologyConf.get(Config.LOGS_USERS);
    +        List<String> topologyUsers = (List<String>)topologyConf.get(Config.TOPOLOGY_USERS);
    +        Set<String> mergedUsers = new HashSet<String>();
    +        if (logsUsers != null) {
    +            for (String user : logsUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    +        if (topologyUsers != null) {
    +            for (String user : topologyUsers) {
    +                if (user != null) {
    +                    mergedUsers.add(user);
    +                }
    +            }
    +        }
    --- End diff --
    
    And this can similarly be
    ```
    if (topologyUsers != null) {
        mergedUsers.addAll(topologyUsers);
    }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50286960
  
    --- Diff: storm-core/src/clj/org/apache/storm/config.clj ---
    @@ -18,14 +18,14 @@
       (:import [java.io FileReader File IOException]
                [org.apache.storm.generated StormTopology])
       (:import [org.apache.storm Config])
    -  (:import [org.apache.storm.utils Utils LocalState])
    +  (:import [org.apache.storm.utils Utils LocalState ConfigUtils])
       (:import [org.apache.storm.validation ConfigValidation])
       (:import [org.apache.commons.io FileUtils])
       (:require [clojure [string :as str]])
       (:use [org.apache.storm log util]))
     
    -(def RESOURCES-SUBDIR "resources")
    -(def NIMBUS-DO-NOT-REASSIGN "NIMBUS-DO-NOT-REASSIGN")
    +;; (def RESOURCES-SUBDIR "resources")
    +;; (def NIMBUS-DO-NOT-REASSIGN "NIMBUS-DO-NOT-REASSIGN")
    --- End diff --
    
    Will delete.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50299712
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    --- End diff --
    
    yes delete this.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50301324
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    --- End diff --
    
    This is also why the type of "mockedSupervisorState" does not really matter.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50288819
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    --- End diff --
    
    Why the string "null" here and an actual null in absoluteStormLocalDir?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50301784
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    +    public static LocalState nimbusTopoHistoryState(Map conf) throws IOException {
    +        if (mockedNimbusTopoHistoryState != null) {
    +            return null;
    +        }
    +        return new LocalState((masterLocalDir(conf) + FILE_SEPARATOR + "history"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormConf mocked = new SetMockedSupervisorStormConf(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormConf. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormConf implements Closeable {
    +        public SetMockedSupervisorStormConf(Map conf) {
    +            mockedSupervisorStormConf = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormConf = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormConf = null;
    +    public static Map readSupervisorStormConf(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormConf != null) {
    +            return mockedSupervisorStormConf;
    +        }
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String confPath = supervisorStormConfPath(stormRoot);
    +        return readSupervisorStormConfGivenPath(conf, confPath);
    +    }
    +
    +    public static StormTopology readSupervisorTopology(Map conf, String stormId) throws IOException {
    +        String stormRoot = supervisorStormDistRoot(conf, stormId);
    +        String topologyPath = supervisorStormCodePath(stormRoot);
    +        return Utils.deserialize(FileUtils.readFileToByteArray(new File(topologyPath)), StormTopology.class);
    +    }
    +
    +    public static String workerUserRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "/workers-users");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String workerUserFile(Map conf, String workerId) {
    +        return (workerUserRoot(conf) + FILE_SEPARATOR + workerId);
    +    }
    +
    +    public static String getWorkerUser(Map conf, String workerId) {
    +        LOG.info("GET worker-user for {}", workerId);
    +        File file = new File(workerUserFile(conf, workerId));
    +
    +        try (InputStream in = new FileInputStream(file);
    +             Reader reader = new InputStreamReader(in);
    +             BufferedReader br = new BufferedReader(reader);) {
    +            StringBuilder sb = new StringBuilder();
    +            int r;
    +            while ((r = br.read()) != -1) {
    +                char ch = (char)r;
    +                sb.append(ch);
    +            }
    +            String ret = sb.toString().trim();
    +            return ret;
    +        } catch (IOException e) {
    +            LOG.error("Failed to get worker user for " + workerId + ".");
    +            return null;
    +        }
    +    }
    +
    +    public static String getIdFromBlobKey(String key) {
    +        if (key == null) return null;
    +        String ret = null;
    +        if (key.endsWith("-stormjar.jar")) {
    +            ret = key.substring(0, key.length() - 13);
    +        } else if (key.endsWith("-stormcode.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        } else if (key.endsWith("-stormconf.ser")) {
    +            ret = key.substring(0, key.length() - 14);
    +        }
    +        return ret;
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerUserWSE mocked = new SetMockedWorkerUserWSE(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerUserWSE. conf)]
    +    //     run test ...)
    +    public static class SetMockedWorkerUserWSE implements Closeable {
    +        public SetMockedWorkerUserWSE(Map conf) {
    +            mockedWorkerUserWSE = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerUserWSE = null;
    +        }
    +    }
    +    private static Map mockedWorkerUserWSE = null;
    +    public static void setWorkerUserWSE(Map conf, String workerId, String user) throws IOException {
    +        if (mockedWorkerUserWSE != null) {
    +            return;
    +        }
    +        LOG.info("SET worker-user {} {}", workerId, user);
    +        File file = new File(workerUserFile(conf, workerId));
    +        file.getParentFile().mkdirs();
    +
    +        try (FileWriter fw = new FileWriter(file);
    +             BufferedWriter writer = new BufferedWriter(fw);) {
    +            writer.write(user);
    +        }
    +    }
    +
    +    public static void removeWorkerUserWSE(Map conf, String workerId) {
    +        LOG.info("REMOVE worker-user {}", workerId);
    +        new File(workerUserFile(conf, workerId)).delete();
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedWorkerArtifactsRoot mocked = new SetMockedWorkerArtifactsRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedWorkerArtifactsRoot. root)]
    +    //     run test ...)
    +    public static class SetMockedWorkerArtifactsRoot implements Closeable {
    +        public SetMockedWorkerArtifactsRoot(String root) {
    +            mockedWorkerArtifactsRoot = root;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedWorkerArtifactsRoot = null;
    +        }
    +    }
    +    private static String mockedWorkerArtifactsRoot = null;
    +    public static String workerArtifactsRoot(Map conf) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        String artifactsDir = (String)conf.get(Config.STORM_WORKERS_ARTIFACTS_DIR);
    +        if (artifactsDir == null) {
    +            return (LOG_DIR + FILE_SEPARATOR + "workers-artifacts");
    +        } else {
    +            if (new File(artifactsDir).isAbsolute()) {
    +                return artifactsDir;
    +            } else {
    +                return (LOG_DIR + FILE_SEPARATOR + artifactsDir);
    +            }
    +        }
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerArtifactsRoot(Map conf, String id, Integer port) {
    +        if (mockedWorkerArtifactsRoot != null) {
    +            return mockedWorkerArtifactsRoot;
    +        }
    +        return (workerArtifactsRoot(conf, id) + FILE_SEPARATOR + port);
    +    }
    +
    +    public static String workerArtifactsPidPath(Map conf, String id, Integer port) {
    +        return (workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR +  "worker.pid");
    +    }
    +
    +    public static File getLogMetaDataFile(String fname) {
    +        String[] subStrings = fname.split(FILE_SEPARATOR); // TODO: does this work well on windows?
    +        String id = subStrings[0];
    +        Integer port = Integer.parseInt(subStrings[1]);
    +        return getLogMetaDataFile(Utils.readStormConfig(), id, port);
    +    }
    +
    +    public static File getLogMetaDataFile(Map conf, String id, Integer port) {
    +        String fname = workerArtifactsRoot(conf, id, port) + FILE_SEPARATOR + "worker.yaml";
    +        return new File(fname);
    +    }
    +
    +    public static File getWorkerDirFromRoot(String logRoot, String id, Integer port) {
    +        return new File((logRoot + FILE_SEPARATOR + id + FILE_SEPARATOR + port));
    +    }
    +
    +    public static String workerRoot(Map conf) {
    +        return (absoluteStormLocalDir(conf) + FILE_SEPARATOR + "workers");
    +    }
    +
    +    public static String workerRoot(Map conf, String id) {
    +        return (workerRoot(conf) + FILE_SEPARATOR + id);
    +    }
    +
    +    public static String workerPidsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "pids");
    +    }
    +
    +    public static String workerPidPath(Map conf, String id, String pid) {
    +        return (workerPidsRoot(conf, id) + FILE_SEPARATOR + pid);
    +    }
    +
    +    public static String workerHeartbeatsRoot(Map conf, String id) {
    +        return (workerRoot(conf, id) + FILE_SEPARATOR + "heartbeats");
    +    }
    +
    +    public static LocalState workerState(Map conf, String id) throws IOException {
    +        return new LocalState(workerHeartbeatsRoot(conf, id));
    +    }
    +
    +    public static Map overrideLoginConfigWithSystemProperty(Map conf) { // note that we delete the return value
    +        String loginConfFile = System.getProperty("java.security.auth.login.config");
    +        if (loginConfFile != null) {
    +             conf.put("java.security.auth.login.config", loginConfFile);
    +        }
    +        return conf;
    +    }
    +
    +    /* TODO: make sure test these two functions in manual tests */
    --- End diff --
    
    What is the result of this TODO?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50328528
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    --- End diff --
    
    The second has a valueOf. Will unify as "null", which is clearer.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50289425
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    --- End diff --
    
    Catching an exception to log it an rethrow it is not typically good practice.  Could we remove the catch/rethrow?  I also am confused why we do a mkdir here, but that is what the original code did so it is a good translation.  Could we file a JIRA to clean this up later?  Creating directories does not feel like something ConfigUtils should be doing.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50299404
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf)); // TODO: no need to forceMake here?, clj does not.
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + stormId; // TODO: need to (url-encode storm-id)? Not.
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String supervisorStormMetaFilePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String supervisorStormCodePath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    public static String supervisorStormConfPath(String stormRoot) {
    +        String ret = "";
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String supervisorTmpDir(Map conf) throws IOException {
    +        String ret = supervisorLocalDir(conf) + FILE_SEPARATOR + "tmp";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorStormResourcesPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return (ret + FILE_SEPARATOR + RESOURCES_SUBDIR);
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorState mocked = new SetMockedSupervisorState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorState. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorState implements Closeable {
    +        public SetMockedSupervisorState(Map conf) {
    +            mockedSupervisorState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorState = null;
    +        }
    +    }
    +    private static Map mockedSupervisorState = null;
    +    public static LocalState supervisorState(Map conf) throws IOException {
    +        if (mockedSupervisorState != null) {
    +            return null;
    +        }
    +        return new LocalState((supervisorLocalDir(conf) + FILE_SEPARATOR + "localstate"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedNimbusTopoHistoryState mocked = new SetMockedNimbusTopoHistoryState(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedNimbusTopoHistoryState. conf)]
    +    //     run test ...)
    +    public static class SetMockedNimbusTopoHistoryState implements Closeable {
    +        public SetMockedNimbusTopoHistoryState(Map conf) {
    +            mockedNimbusTopoHistoryState = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedNimbusTopoHistoryState = null;
    +        }
    +    }
    +    private static Map mockedNimbusTopoHistoryState = null;
    --- End diff --
    
    This is not a Map.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on the pull request:

    https://github.com/apache/storm/pull/1030#issuecomment-173327601
  
    overall things look fairly good.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50288645
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    --- End diff --
    
    Extra line


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50289560
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    --- End diff --
    
    Actually can you just make it generic about all of the forceMkdir calls in ConfigUtils?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50600384
  
    --- Diff: storm-core/src/jvm/org/apache/storm/testing/staticmocking/MockedConfigUtils.java ---
    @@ -0,0 +1,31 @@
    +/**
    + * 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.testing.staticmocking;
    +
    +import org.apache.storm.utils.ConfigUtils;
    +
    +public class MockedConfigUtils extends ConfigUtils implements AutoCloseable {
    --- End diff --
    
    Derek's repository has a file [package-info.java](https://github.com/d2r/storm/blob/56d48faac918123af46f1e021e38549fa6a4cf6c/storm-core/src/jvm/org/apache/storm/testing/staticmocking/package-info.java), which shows how to use it. Do I need to include that within this pull request or wait util JIRA to do that? @d2r @revans2 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by knusbaum <gi...@git.apache.org>.
Github user knusbaum commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50460728
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,678 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +import java.net.URLEncoder;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +
    +    public static String getLogDir() {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            return new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = (String)conf.get(Config.STORM_HEALTH_CHECK_DIR);
    +        if (healthCheckDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    --- End diff --
    
    Same comments as I made above apply here, and everywhere you use this trick.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50298399
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    --- End diff --
    
    Again lets remove the try/catch/log/rethrow


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50597310
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,678 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +import java.net.URLEncoder;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +
    +    public static String getLogDir() {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            return new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = (String)conf.get(Config.STORM_HEALTH_CHECK_DIR);
    +        if (healthCheckDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    --- End diff --
    
    Now go with Derek's proposal for static mocking.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by knusbaum <gi...@git.apache.org>.
Github user knusbaum commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50457931
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,678 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +import java.net.URLEncoder;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +
    +    public static String getLogDir() {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            return new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    --- End diff --
    
    Can we move this declaration just above SetMockedStormConfig and give it proper spacing?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50597332
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,678 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +import java.net.URLEncoder;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +
    +    public static String getLogDir() {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    --- End diff --
    
    Fixed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by revans2 <gi...@git.apache.org>.
Github user revans2 commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50285640
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    --- End diff --
    
    Extra line


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50329442
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,711 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +    public final static String LOG_DIR;
    +
    +    static {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            LOG_DIR = new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String) conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = String.valueOf(conf.get(Config.STORM_HEALTH_CHECK_DIR));
    +        if (healthCheckDir.equals("null")) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static String stormTmpPath(String stormRoot) {
    +        return stormRoot + FILE_SEPARATOR + "tmp";
    +    }
    +
    +    /* Never get used TODO : delete it*/
    +    public static String masterTmpDir(Map conf) throws IOException {
    +        String ret = stormTmpPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormMetaFilePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "storm-code-distributor.meta");
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormCodePath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormcode.ser");
    +    }
    +
    +    /* Never get used TODO : may delete it*/
    +    public static String masterStormConfPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormconf.ser");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        try {
    +            FileUtils.forceMkdir(new File(ret));
    +        } catch (IOException e) {
    +            LOG.error("Failed to create dir " + ret, e);
    +            throw e;
    +        }
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    --- End diff --
    
    Actually we want to return null if the test is setting mockedSupervisorLocalDir as "sth".


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by knusbaum <gi...@git.apache.org>.
Github user knusbaum commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50455998
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,678 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +import java.net.URLEncoder;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +
    +    public static String getLogDir() {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    --- End diff --
    
    No need for nested if here. Make an else-if and an else.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: [STORM-1227] port org.apache.storm.config to j...

Posted by zhuoliu <gi...@git.apache.org>.
Github user zhuoliu commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1030#discussion_r50597227
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/ConfigUtils.java ---
    @@ -0,0 +1,678 @@
    +/**
    + * 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.utils;
    +
    +import org.apache.storm.Config;
    +import org.apache.storm.validation.ConfigValidation;
    +import org.apache.storm.generated.StormTopology;
    +import org.apache.commons.io.FileUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.*;
    +import java.lang.reflect.Field;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.HashSet;
    +import java.util.Collections;
    +import java.net.URLEncoder;
    +
    +public class ConfigUtils {
    +    private final static Logger LOG = LoggerFactory.getLogger(ConfigUtils.class);
    +    public final static String RESOURCES_SUBDIR = "resources";
    +    public final static String NIMBUS_DO_NOT_REASSIGN = "NIMBUS-DO-NOT-REASSIGN";
    +    public static final String FILE_SEPARATOR = File.separator;
    +
    +    public static String getLogDir() {
    +        String dir;
    +        Map conf;
    +        if (System.getProperty("storm.log.dir") != null) {
    +            dir = System.getProperty("storm.log.dir");
    +        } else if ((conf = readStormConfig()).get("storm.log.dir") != null) {
    +            dir = String.valueOf(conf.get("storm.log.dir"));
    +        } else {
    +            if (System.getProperty("storm.home") != null) {
    +                dir = System.getProperty("storm.home") + FILE_SEPARATOR + "logs";
    +            } else {
    +                dir = FILE_SEPARATOR + "logs";
    +            }
    +        }
    +        try {
    +            return new File(dir).getCanonicalPath();
    +        } catch (IOException ex) {
    +            throw new IllegalArgumentException("Illegal storm.log.dir in conf: " + dir);
    +        }
    +    }
    +
    +    public static String clojureConfigName(String name) {
    +        return name.toUpperCase().replace("_", "-");
    +    }
    +
    +    // ALL-CONFIGS is only used by executor.clj once, do we want to do it here? TODO
    +    public static List<Object> All_CONFIGS() {
    +        List<Object> ret = new ArrayList<Object>();
    +        Config config = new Config();
    +        Class<?> ConfigClass = config.getClass();
    +        Field[] fields = ConfigClass.getFields();
    +        for (int i = 0; i < fields.length; i++) {
    +            try {
    +                Object obj = fields[i].get(null);
    +                ret.add(obj);
    +            } catch (IllegalArgumentException e) {
    +                LOG.error(e.getMessage(), e);
    +            } catch (IllegalAccessException e) {
    +                LOG.error(e.getMessage(), e);
    +            }
    +        }
    +        return ret;
    +    }
    +
    +    public static String clusterMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        return mode;
    +    }
    +
    +    public static boolean isLocalMode(Map conf) {
    +        String mode = (String)conf.get(Config.STORM_CLUSTER_MODE);
    +        if (mode != null) {
    +            if ("local".equals(mode)) {
    +                return true;
    +            }
    +            if ("distributed".equals(mode)) {
    +                return false;
    +            }
    +        }
    +        throw new IllegalArgumentException("Illegal cluster mode in conf: " + mode);
    +    }
    +
    +    public static int samplingRate(Map conf) {
    +        double rate = Utils.getDouble(conf.get(Config.TOPOLOGY_STATS_SAMPLE_RATE));
    +        if (rate != 0) {
    +            return (int) (1 / rate);
    +        }
    +        throw new IllegalArgumentException("Illegal topology.stats.sample.rate in conf: " + rate);
    +    }
    +
    +    // public static mkStatsSampler // depends on Utils.evenSampler() TODO, this is sth we need to do after util
    +    // public static readDefaultConfig // depends on Utils.clojurifyStructure and Utils.readDefaultConfig // TODO
    +    // validate-configs-with-schemas is just a wrapper of ConfigValidation.validateFields(conf)
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedStormConfig mocked = new SetMockedStormConfig(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedStormConfig. conf)]
    +    //     run test ...)
    +    public static class SetMockedStormConfig implements Closeable {
    +        public SetMockedStormConfig(Map conf) {
    +            mockedStormConfig = conf;
    +        }
    +
    +        @Override
    +        public void close() {
    +            mockedStormConfig = null;
    +        }
    +    }
    +    private static Map mockedStormConfig = null;
    +    public static Map readStormConfig() {
    +        if (mockedStormConfig != null) return mockedStormConfig;
    +        Map conf = Utils.readStormConfig();
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name, boolean mustExist) {
    +        Map conf = Utils.findAndReadConfigFile(name, mustExist);
    +        ConfigValidation.validateFields(conf);
    +        return conf;
    +    }
    +
    +    public static Map readYamlConfig(String name) {
    +        return  readYamlConfig(name, true);
    +    }
    +
    +    public static String absoluteStormLocalDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String localDir = (String) conf.get(Config.STORM_LOCAL_DIR);
    +        if (localDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "storm-local");
    +        } else {
    +            if (new File(localDir).isAbsolute()) {
    +                return localDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + localDir);
    +            }
    +        }
    +    }
    +
    +    public static String absoluteHealthCheckDir(Map conf) {
    +        String stormHome = System.getProperty("storm.home");
    +        String healthCheckDir = (String)conf.get(Config.STORM_HEALTH_CHECK_DIR);
    +        if (healthCheckDir == null) {
    +            return (stormHome + FILE_SEPARATOR + "healthchecks");
    +        } else {
    +            if (new File(healthCheckDir).isAbsolute()) {
    +                return healthCheckDir;
    +            } else {
    +                return (stormHome + FILE_SEPARATOR + healthCheckDir);
    +            }
    +        }
    +    }
    +
    +    public static String masterLocalDir(Map conf) throws IOException {
    +        String ret = String.valueOf(conf.get(Config.STORM_LOCAL_DIR)) + FILE_SEPARATOR + "nimbus";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarKey(String topologyId) {
    +        return (topologyId + "-stormjar.jar");
    +    }
    +
    +    public static String masterStormCodeKey(String topologyId) {
    +        return (topologyId + "-stormcode.ser");
    +    }
    +
    +    public static String masterStormConfKey(String topologyId) {
    +        return (topologyId + "-stormconf.ser");
    +    }
    +
    +    public static String masterStormDistRoot(Map conf) throws IOException {
    +        String ret = stormDistPath(masterLocalDir(conf));
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterStormDistRoot(Map conf, String stormId) throws IOException {
    +        return (masterStormDistRoot(conf) + FILE_SEPARATOR + stormId);
    +    }
    +
    +    public static String stormDistPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    +        if (stormRoot != null) {
    +            ret = stormRoot;
    +        }
    +        return ret + FILE_SEPARATOR + "stormdist";
    +    }
    +
    +    public static Map readSupervisorStormConfGivenPath(Map conf, String stormConfPath) throws  IOException {
    +        Map ret = new HashMap(conf);
    +        ret.putAll(Utils.fromCompressedJsonConf(FileUtils.readFileToByteArray(new File(stormConfPath))));
    +        return ret;
    +    }
    +
    +    public static String masterStormJarPath(String stormRoot) {
    +        return (stormRoot + FILE_SEPARATOR + "stormjar.jar");
    +    }
    +
    +    public static String masterInbox(Map conf) throws IOException {
    +        String ret = masterLocalDir(conf) + FILE_SEPARATOR + "inbox";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String masterInimbusDir(Map conf) throws IOException {
    +        return (masterLocalDir(conf) + FILE_SEPARATOR + "inimbus");
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorLocalDir mocked = new SetMockedSupervisorLocalDir(dir)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorLocalDir. dir)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorLocalDir implements Closeable {
    +        public SetMockedSupervisorLocalDir(String dir) {
    +            mockedSupervisorLocalDir = dir;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorLocalDir = null;
    +        }
    +    }
    +    private static String mockedSupervisorLocalDir = null;
    +    public static String supervisorLocalDir(Map conf) throws IOException {
    +        if (mockedSupervisorLocalDir != null) {
    +            return null;
    +        }
    +        String ret = absoluteStormLocalDir(conf) + FILE_SEPARATOR + "supervisor";
    +        FileUtils.forceMkdir(new File(ret));
    +        return ret;
    +    }
    +
    +    public static String supervisorIsupervisorDir(Map conf) throws IOException {
    +        return ((supervisorLocalDir(conf) + FILE_SEPARATOR + "isupervisor"));
    +    }
    +
    +    //For testing only
    +    // for java
    +    // try (SetMockedSupervisorStormDistRoot mocked = new SetMockedSupervisorStormDistRoot(conf)) {
    +    //    run test ...
    +    // }
    +    //
    +    // for clojure
    +    // (with-open [mock (SetMockedSupervisorStormDistRoot. conf)]
    +    //     run test ...)
    +    public static class SetMockedSupervisorStormDistRoot implements Closeable {
    +        public SetMockedSupervisorStormDistRoot(Map conf) {
    +            mockedSupervisorStormDistRoot = conf;
    +        }
    +        @Override
    +        public void close() {
    +            mockedSupervisorStormDistRoot = null;
    +        }
    +    }
    +    private static Map mockedSupervisorStormDistRoot = null;
    +    public static String supervisorStormDistRoot(Map conf) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return stormDistPath(supervisorLocalDir(conf));
    +    }
    +
    +    public static String supervisorStormDistRoot(Map conf, String stormId) throws IOException {
    +        if (mockedSupervisorStormDistRoot != null) {
    +            return null;
    +        }
    +        return supervisorStormDistRoot(conf) + FILE_SEPARATOR + URLEncoder.encode(stormId, "UTF-8");
    +    }
    +
    +    public static String supervisorStormJarPath(String stormRoot) {
    +        String ret = "";
    +        // we do this since to concat a null String will actually concat a "null", which is not the expected: ""
    --- End diff --
    
    Make sense, addressed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---