Merge remote-tracking branch 'upstream/master'
Project: http://git-wip-us.apache.org/repos/asf/storm/repo Commit: http://git-wip-us.apache.org/repos/asf/storm/commit/6c0f4f01 Tree: http://git-wip-us.apache.org/repos/asf/storm/tree/6c0f4f01 Diff: http://git-wip-us.apache.org/repos/asf/storm/diff/6c0f4f01 Branch: refs/heads/master Commit: 6c0f4f01aaef6c7a728db45c6d1b3726f2da1ab9 Parents: 7ce9a3c 12ceb09 Author: basti.lj <[email protected]> Authored: Sun Feb 14 13:47:48 2016 +0800 Committer: basti.lj <[email protected]> Committed: Sun Feb 14 13:47:48 2016 +0800 ---------------------------------------------------------------------- CHANGELOG.md | 17 + README.markdown | 2 + bin/storm-config.cmd | 6 +- bin/storm.cmd | 47 +- bin/storm.py | 8 +- conf/defaults.yaml | 4 + dev-tools/travis/travis-script.sh | 4 +- .../starter/trident/TridentMapExample.java | 123 +++ external/sql/storm-sql-core/pom.xml | 9 + external/storm-elasticsearch/pom.xml | 2 + .../storm/hbase/security/HBaseSecurityUtil.java | 36 +- external/storm-hdfs/README.md | 15 +- external/storm-hdfs/pom.xml | 22 + .../storm/hdfs/avro/AbstractAvroSerializer.java | 80 ++ .../storm/hdfs/avro/AvroSchemaRegistry.java | 28 + .../org/apache/storm/hdfs/avro/AvroUtils.java | 44 + .../hdfs/avro/ConfluentAvroSerializer.java | 83 ++ .../storm/hdfs/avro/FixedAvroSerializer.java | 67 ++ .../storm/hdfs/avro/GenericAvroSerializer.java | 36 + .../storm/hdfs/bolt/AvroGenericRecordBolt.java | 4 - .../hdfs/avro/TestFixedAvroSerializer.java | 76 ++ .../hdfs/avro/TestGenericAvroSerializer.java | 68 ++ .../test/resources/FixedAvroSerializer.config | 2 + .../jvm/org/apache/storm/kafka/KafkaSpout.java | 8 +- .../jvm/org/apache/storm/kafka/KafkaUtils.java | 44 +- .../apache/storm/kafka/PartitionManager.java | 42 +- .../kafka/trident/TridentKafkaEmitter.java | 23 +- external/storm-mqtt/core/pom.xml | 4 +- log4j2/cluster.xml | 2 +- log4j2/worker.xml | 2 +- pom.xml | 12 +- storm-core/pom.xml | 11 +- .../src/clj/org/apache/storm/LocalCluster.clj | 4 +- storm-core/src/clj/org/apache/storm/clojure.clj | 8 +- storm-core/src/clj/org/apache/storm/cluster.clj | 27 +- .../cluster_state/zookeeper_state_factory.clj | 14 +- .../clj/org/apache/storm/command/blobstore.clj | 11 +- .../org/apache/storm/command/config_value.clj | 25 - .../org/apache/storm/command/dev_zookeeper.clj | 6 +- .../clj/org/apache/storm/command/get_errors.clj | 12 +- .../apache/storm/command/shell_submission.clj | 4 +- storm-core/src/clj/org/apache/storm/config.clj | 18 +- .../src/clj/org/apache/storm/converter.clj | 14 +- .../src/clj/org/apache/storm/daemon/acker.clj | 21 +- .../src/clj/org/apache/storm/daemon/common.clj | 40 +- .../src/clj/org/apache/storm/daemon/drpc.clj | 25 +- .../clj/org/apache/storm/daemon/executor.clj | 532 +++++----- .../clj/org/apache/storm/daemon/logviewer.clj | 70 +- .../src/clj/org/apache/storm/daemon/nimbus.clj | 172 ++-- .../clj/org/apache/storm/daemon/supervisor.clj | 202 ++-- .../src/clj/org/apache/storm/daemon/task.clj | 4 +- .../src/clj/org/apache/storm/daemon/worker.clj | 78 +- .../src/clj/org/apache/storm/disruptor.clj | 10 +- storm-core/src/clj/org/apache/storm/event.clj | 2 +- .../src/clj/org/apache/storm/local_state.clj | 9 +- .../clj/org/apache/storm/messaging/loader.clj | 34 - .../clj/org/apache/storm/messaging/local.clj | 23 - .../org/apache/storm/pacemaker/pacemaker.clj | 7 +- .../storm/pacemaker/pacemaker_state_factory.clj | 24 +- .../clj/org/apache/storm/process_simulator.clj | 4 +- .../apache/storm/scheduler/DefaultScheduler.clj | 7 +- .../apache/storm/scheduler/EvenScheduler.clj | 23 +- .../storm/scheduler/IsolationScheduler.clj | 29 +- storm-core/src/clj/org/apache/storm/stats.clj | 82 +- storm-core/src/clj/org/apache/storm/testing.clj | 89 +- storm-core/src/clj/org/apache/storm/thrift.clj | 6 +- storm-core/src/clj/org/apache/storm/timer.clj | 12 +- .../clj/org/apache/storm/trident/testing.clj | 9 +- storm-core/src/clj/org/apache/storm/ui/core.clj | 99 +- .../src/clj/org/apache/storm/ui/helpers.clj | 14 +- storm-core/src/clj/org/apache/storm/util.clj | 923 +----------------- .../src/clj/org/apache/storm/zookeeper.clj | 1 - storm-core/src/jvm/org/apache/storm/Config.java | 39 + .../org/apache/storm/command/ConfigValue.java | 30 + .../storm/daemon/metrics/MetricsUtils.java | 108 ++ .../reporters/ConsolePreparableReporter.java | 76 ++ .../reporters/CsvPreparableReporter.java | 80 ++ .../reporters/JmxPreparableReporter.java | 70 ++ .../metrics/reporters/PreparableReporter.java | 32 + .../storm/logging/ThriftAccessLogger.java | 13 +- .../serialization/SerializationFactory.java | 17 +- .../staticmocking/MockedConfigUtils.java | 31 - .../jvm/org/apache/storm/trident/Stream.java | 87 +- .../storm/trident/operation/Consumer.java | 35 + .../trident/operation/FlatMapFunction.java | 37 + .../storm/trident/operation/MapFunction.java | 36 + .../operation/impl/ConsumerExecutor.java | 38 + .../operation/impl/FlatMapFunctionExecutor.java | 43 + .../operation/impl/MapFunctionExecutor.java | 41 + .../trident/planner/processor/MapProcessor.java | 87 ++ .../jvm/org/apache/storm/utils/ConfigUtils.java | 20 +- .../jvm/org/apache/storm/utils/Container.java | 11 +- .../jvm/org/apache/storm/utils/IPredicate.java | 27 + .../org/apache/storm/utils/NimbusClient.java | 2 +- .../utils/StormConnectionStateConverter.java | 44 + .../jvm/org/apache/storm/utils/TestUtils.java | 34 - .../src/jvm/org/apache/storm/utils/Time.java | 26 +- .../src/jvm/org/apache/storm/utils/Utils.java | 973 ++++++++++++++++++- .../storm/validation/ConfigValidation.java | 2 +- .../org/apache/storm/zookeeper/Zookeeper.java | 7 + .../org/apache/storm/integration_test.clj | 100 +- .../org/apache/storm/testing4j_test.clj | 37 +- .../apache/storm/trident/integration_test.clj | 15 +- .../test/clj/org/apache/storm/cluster_test.clj | 20 +- .../test/clj/org/apache/storm/drpc_test.clj | 23 +- .../clj/org/apache/storm/logviewer_test.clj | 267 ++--- .../storm/messaging/netty_integration_test.clj | 2 +- .../test/clj/org/apache/storm/nimbus_test.clj | 131 ++- .../scheduler/resource_aware_scheduler_test.clj | 21 +- .../apache/storm/security/auth/auth_test.clj | 11 +- .../authorizer/DRPCSimpleACLAuthorizer_test.clj | 2 +- .../BlowfishTupleSerializer_test.clj | 1 - .../clj/org/apache/storm/serialization_test.clj | 23 +- .../clj/org/apache/storm/supervisor_test.clj | 645 ++++++------ .../clj/org/apache/storm/transactional_test.clj | 18 + .../clj/org/apache/storm/trident/state_test.clj | 5 +- .../clj/org/apache/storm/trident/tuple_test.clj | 15 +- .../test/clj/org/apache/storm/utils_test.clj | 16 +- .../test/clj/org/apache/storm/worker_test.clj | 1 - .../staticmocking/ConfigUtilsInstaller.java | 38 + .../utils/staticmocking/UtilsInstaller.java | 38 + .../storm/utils/staticmocking/package-info.java | 95 ++ 122 files changed, 4723 insertions(+), 2472 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/storm/blob/6c0f4f01/storm-core/src/clj/org/apache/storm/daemon/acker.clj ---------------------------------------------------------------------- diff --cc storm-core/src/clj/org/apache/storm/daemon/acker.clj index 9bd4f44,dc05dfc..7e17d40 --- a/storm-core/src/clj/org/apache/storm/daemon/acker.clj +++ b/storm-core/src/clj/org/apache/storm/daemon/acker.clj @@@ -14,13 -14,13 +14,14 @@@ ;; See the License for the specific language governing permissions and ;; limitations under the License. (ns org.apache.storm.daemon.acker - (:import [org.apache.storm.task OutputCollector TopologyContext IBolt]) + (:import [org.apache.storm.task OutputCollector TopologyContext IBolt] + [org.apache.storm.utils Utils]) (:import [org.apache.storm.tuple Tuple Fields]) - (:import [org.apache.storm.utils RotatingMap MutableObject]) + (:import [org.apache.storm.utils Container RotatingMap MutableObject]) (:import [java.util List Map]) - (:import [org.apache.storm Constants]) + (:import [org.apache.storm Constants] + (org.apache.storm.daemon AckerBolt)) - (:use [org.apache.storm config util]) + (:use [org.apache.storm config log]) (:gen-class :init init :implements [org.apache.storm.task.IBolt] @@@ -35,18 -44,61 +36,18 @@@ (defn mk-acker-bolt [] (let [output-collector (MutableObject.) pending (MutableObject.)] - (reify IBolt - (^void prepare [this ^Map storm-conf ^TopologyContext context ^OutputCollector collector] - (.setObject output-collector collector) - (.setObject pending (RotatingMap. 2)) - ) - (^void execute [this ^Tuple tuple] - (let [^RotatingMap pending (.getObject pending) - stream-id (.getSourceStreamId tuple)] - (if (= stream-id Constants/SYSTEM_TICK_STREAM_ID) - (.rotate pending) - (let [id (.getValue tuple 0) - ^OutputCollector output-collector (.getObject output-collector) - curr (.get pending id) - curr (condp = stream-id - ACKER-INIT-STREAM-ID (-> curr - (update-ack (.getValue tuple 1)) - (assoc :spout-task (.getValue tuple 2))) - ACKER-ACK-STREAM-ID (update-ack curr (.getValue tuple 1)) - ACKER-FAIL-STREAM-ID (assoc curr :failed true))] - (.put pending id curr) - (when (and curr (:spout-task curr)) - (cond (= 0 (:val curr)) - (do - (.remove pending id) - (acker-emit-direct output-collector - (:spout-task curr) - ACKER-ACK-STREAM-ID - [id] - )) - (:failed curr) - (do - (.remove pending id) - (acker-emit-direct output-collector - (:spout-task curr) - ACKER-FAIL-STREAM-ID - [id] - )) - )) - (.ack output-collector tuple) - )))) - (^void cleanup [this] - ) - ))) + (AckerBolt.))) (defn -init [] - [[] (container)]) + [[] (Container.)]) - (defn -prepare [this conf context collector] + (defn -prepare [^org.apache.storm.daemon.acker this conf context collector] (let [^IBolt ret (mk-acker-bolt)] - (container-set! (.state ^org.apache.storm.daemon.acker this) ret) + (.. this state (set ret)) - (.prepare ret conf context collector) - )) + (.prepare ret conf context collector))) - (defn -execute [this tuple] - (let [^IBolt delegate (container-get (.state ^org.apache.storm.daemon.acker this))] + (defn -execute [^org.apache.storm.daemon.acker this tuple] + (let [^IBolt delegate (.. this state (get))] (.execute delegate tuple) )) http://git-wip-us.apache.org/repos/asf/storm/blob/6c0f4f01/storm-core/src/clj/org/apache/storm/daemon/common.clj ---------------------------------------------------------------------- diff --cc storm-core/src/clj/org/apache/storm/daemon/common.clj index 6ecc918,eb1ec1e..42fa1fa --- a/storm-core/src/clj/org/apache/storm/daemon/common.clj +++ b/storm-core/src/clj/org/apache/storm/daemon/common.clj @@@ -24,14 -26,23 +26,23 @@@ (:import [org.apache.storm.metric SystemBolt]) (:import [org.apache.storm.metric EventLoggerBolt]) (:import [org.apache.storm.security.auth IAuthorizer]) - (:import [java.io InterruptedIOException]) + (:import [java.io InterruptedIOException] + [org.json.simple JSONValue]) - (:require [clojure.set :as set]) + (:require [clojure.set :as set]) (:require [org.apache.storm.daemon.acker :as acker]) (:require [org.apache.storm.thrift :as thrift]) - (:require [metrics.reporters.jmx :as jmx])) + (:require [metrics.core :refer [default-registry]])) + + (defn start-metrics-reporter [reporter conf] + (doto reporter + (.prepare default-registry conf) + (.start)) + (log-message "Started statistics report plugin...")) + + (defn start-metrics-reporters [conf] + (doseq [reporter (MetricsUtils/getPreparableReporters conf)] + (start-metrics-reporter reporter conf))) - (defn start-metrics-reporters [] - (jmx/start (jmx/reporter {}))) (def ACKER-COMPONENT-ID acker/ACKER-COMPONENT-ID) (def ACKER-INIT-STREAM-ID acker/ACKER-INIT-STREAM-ID) @@@ -134,9 -144,10 +144,10 @@@ (defn component-conf [component] (->> component - .get_common - .get_json_conf + .get_common + .get_json_conf - from-json)) + (#(if % (JSONValue/parse %))) + clojurify-structure)) (defn validate-basic! [^StormTopology topology] (validate-ids! topology) http://git-wip-us.apache.org/repos/asf/storm/blob/6c0f4f01/storm-core/src/jvm/org/apache/storm/utils/Utils.java ---------------------------------------------------------------------- diff --cc storm-core/src/jvm/org/apache/storm/utils/Utils.java index 2361987,a0c0b1a..44fb1a1 --- a/storm-core/src/jvm/org/apache/storm/utils/Utils.java +++ b/storm-core/src/jvm/org/apache/storm/utils/Utils.java @@@ -441,7 -509,11 +509,11 @@@ public class Utils HashMap nconf = new HashMap(conf); // only enable cleanup of blobstore on nimbus nconf.put(Config.BLOBSTORE_CLEANUP_ENABLE, Boolean.TRUE); + + if(store != null) { + // store can be null during testing when mocking utils. - store.prepare(nconf, baseDir, nimbusInfo); + store.prepare(nconf, baseDir, nimbusInfo); + } return store; } @@@ -878,34 -945,18 +945,18 @@@ } else { inputStream = new BufferedInputStream(new FileInputStream(inFile)); } - tis = new TarArchiveInputStream(inputStream); + try (TarArchiveInputStream tis = new TarArchiveInputStream(inputStream)) { - for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null; ) { - unpackEntries(tis, entry, untarDir); - entry = tis.getNextTarEntry(); - } + for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null; ) { + unpackEntries(tis, entry, untarDir); + entry = tis.getNextTarEntry(); + } + } } finally { - cleanup(tis, inputStream); + if(inputStream != null) { + inputStream.close(); - } } } - - /** - * Close the Closeable objects and <b>ignore</b> any {@link IOException} or - * null pointers. Must only be used for cleanup in exception handlers. - * - * @param closeables the objects to close - */ - private static void cleanup(java.io.Closeable... closeables) { - for (java.io.Closeable c : closeables) { - if (c != null) { - try { - c.close(); - } catch (IOException e) { - LOG.debug("Exception in closing " + c, e); - + } - } - } - } private static void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir) throws IOException { @@@ -1186,7 -1258,12 +1258,12 @@@ return dump.toString(); } - // Assumes caller is synchronizing - /** ++ /* + * Creates an instance of the pluggable SerializationDelegate or falls back to + * DefaultSerializationDelegate if something goes wrong. + * @param stormConf The config from which to pull the name of the pluggable class. + * @return an instance of the class specified by storm.meta.serialization.delegate + */ private static SerializationDelegate getSerializationDelegate(Map stormConf) { String delegateClassName = (String)stormConf.get(Config.STORM_META_SERIALIZATION_DELEGATE); SerializationDelegate delegate; @@@ -1375,8 -1446,806 +1446,810 @@@ } } + /** + * Determines if a zip archive contains a particular directory. + * + * @param zipfile path to the zipped file + * @param target directory being looked for in the zip. + * @return boolean whether or not the directory exists in the zip. + */ + public static boolean zipDoesContainDir(String zipfile, String target) throws IOException { + List<ZipEntry> entries = (List<ZipEntry>)Collections.list(new ZipFile(zipfile).entries()); + + String targetDir = target + "/"; + for(ZipEntry entry : entries) { + String name = entry.getName(); + if(name.startsWith(targetDir)) { + return true; + } + } + + return false; + } + + /** + * Joins any number of maps together into a single map, combining their values into + * a list, maintaining values in the order the maps were passed in. Nulls are inserted + * for given keys when the map does not contain that key. + * + * i.e. joinMaps({'a' => 1, 'b' => 2}, {'b' => 3}, {'a' => 4, 'c' => 5}) -> + * {'a' => [1, null, 4], 'b' => [2, 3, null], 'c' => [null, null, 5]} + * + * @param maps variable number of maps to join - order affects order of values in output. + * @return combined map + */ + public static <K, V> Map<K, List<V>> joinMaps(Map<K, V>... maps) { + Map<K, List<V>> ret = new HashMap<>(); + + Set<K> keys = new HashSet<>(); + + for(Map<K, V> map : maps) { + keys.addAll(map.keySet()); + } + + for(Map<K, V> m : maps) { + for(K key : keys) { + V value = m.get(key); + + if(!ret.containsKey(key)) { + ret.put(key, new ArrayList<V>()); + } + + List<V> targetList = ret.get(key); + targetList.add(value); + } + } + return ret; + } + + /** + * Fills up chunks out of a collection (given a maximum amount of chunks) + * + * i.e. partitionFixed(5, [1,2,3]) -> [[1,2,3]] + * partitionFixed(5, [1..9]) -> [[1,2], [3,4], [5,6], [7,8], [9]] + * partitionFixed(3, [1..10]) -> [[1,2,3,4], [5,6,7], [8,9,10]] + * @param maxNumChunks the maximum number of chunks to return + * @param coll the collection to be chunked up + * @return a list of the chunks, which are themselves lists. + */ + public static <T> List<List<T>> partitionFixed(int maxNumChunks, Collection<T> coll) { + List<List<T>> ret = new ArrayList<>(); + + if(maxNumChunks == 0 || coll == null) { + return ret; + } + + Map<Integer, Integer> parts = integerDivided(coll.size(), maxNumChunks); + + // Keys sorted in descending order + List<Integer> sortedKeys = new ArrayList<Integer>(parts.keySet()); + Collections.sort(sortedKeys, Collections.reverseOrder()); + + + Iterator<T> it = coll.iterator(); + for(Integer chunkSize : sortedKeys) { + if(!it.hasNext()) { break; } + Integer times = parts.get(chunkSize); + for(int i = 0; i < times; i++) { + if(!it.hasNext()) { break; } + List<T> chunkList = new ArrayList<>(); + for(int j = 0; j < chunkSize; j++) { + if(!it.hasNext()) { break; } + chunkList.add(it.next()); + } + ret.add(chunkList); + } + } + + return ret; + } + + /** + * Return a new instance of a pluggable specified in the conf. + * @param conf The conf to read from. + * @param configKey The key pointing to the pluggable class + * @return an instance of the class or null if it is not specified. + */ + public static Object getConfiguredClass(Map conf, Object configKey) { + if (conf.containsKey(configKey)) { + return newInstance((String)conf.get(configKey)); + } + return null; + } + + public static String logsFilename(String stormId, String port) { + return stormId + FILE_PATH_SEPARATOR + port + FILE_PATH_SEPARATOR + "worker.log"; + } + + public static String eventLogsFilename(String stormId, String port) { + return stormId + FILE_PATH_SEPARATOR + port + FILE_PATH_SEPARATOR + "events.log"; + } + + public static Object readYamlFile(String yamlFile) { + try (FileReader reader = new FileReader(yamlFile)) { + return new Yaml(new SafeConstructor()).load(reader); + } catch(Exception ex) { + LOG.error("Failed to read yaml file.", ex); + } + return null; + } + + public static void setupDefaultUncaughtExceptionHandler() { + Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { + public void uncaughtException(Thread thread, Throwable thrown) { + try { + handleUncaughtException(thrown); + } catch (Error err) { + LOG.error("Received error in main thread.. terminating server...", err); + Runtime.getRuntime().exit(-2); + } + } + }); + } + + /** + * Creates a new map with a string value in the map replaced with an + * equivalently-lengthed string of '#'. + * @param m The map that a value will be redacted from + * @param key The key pointing to the value to be redacted + * @return a new map with the value redacted. The original map will not be modified. + */ + public static Map<Object, String> redactValue(Map<Object, String> m, Object key) { + if(m.containsKey(key)) { + HashMap<Object, String> newMap = new HashMap<>(m); + String value = newMap.get(key); + String redacted = new String(new char[value.length()]).replace("\0", "#"); + newMap.put(key, redacted); + return newMap; + } + return m; + } + + /** + * Make sure a given key name is valid for the storm config. + * Throw RuntimeException if the key isn't valid. + * @param name The name of the config key to check. + */ + private static final Set<String> disallowedKeys = new HashSet<>(Arrays.asList(new String[] {"/", ".", ":", "\\"})); + public static void validateKeyName(String name) { + + for(String key : disallowedKeys) { + if( name.contains(key) ) { + throw new RuntimeException("Key name cannot contain any of the following: " + disallowedKeys.toString()); + } + } + if(name.trim().isEmpty()) { + throw new RuntimeException("Key name cannot be blank"); + } + } + + /** + * Find the first item of coll for which pred.test(...) returns true. + * @param pred The IPredicate to test for + * @param coll The Collection of items to search through. + * @return The first matching value in coll, or null if nothing matches. + */ + public static <T> T findOne (IPredicate<T> pred, Collection<T> coll) { + if(coll == null) { + return null; + } + for(T elem : coll) { + if (pred.test(elem)) { + return elem; + } + } + return null; + } + + public static <T, U> T findOne (IPredicate<T> pred, Map<U, T> map) { + if(map == null) { + return null; + } + return findOne(pred, (Set<T>)map.entrySet()); + } + + public static String localHostname () throws UnknownHostException { + return _instance.localHostnameImpl(); + } + + // Non-static impl methods exist for mocking purposes. + protected String localHostnameImpl () throws UnknownHostException { + return InetAddress.getLocalHost().getCanonicalHostName(); + } + + private static String memoizedLocalHostnameString = null; + + public static String memoizedLocalHostname () throws UnknownHostException { + if (memoizedLocalHostnameString == null) { + memoizedLocalHostnameString = localHostname(); + } + return memoizedLocalHostnameString; + } + + /** + * Gets the storm.local.hostname value, or tries to figure out the local hostname + * if it is not set in the config. + * @param conf The storm config to read from + * @return a string representation of the hostname. + */ + public static String hostname (Map<String, Object> conf) throws UnknownHostException { + if (conf == null) { + return memoizedLocalHostname(); + } + Object hostnameString = conf.get(Config.STORM_LOCAL_HOSTNAME); + if (hostnameString == null || hostnameString.equals("")) { + return memoizedLocalHostname(); + } + return (String)hostnameString; + } + + public static String uuid() { + return UUID.randomUUID().toString(); + } + + public static void exitProcess (int val, Object... msg) { + StringBuilder errorMessage = new StringBuilder(); + errorMessage.append("Halting process: "); + for (Object oneMessage: msg) { + errorMessage.append(oneMessage); + } + String combinedErrorMessage = errorMessage.toString(); + LOG.error(combinedErrorMessage, new RuntimeException(combinedErrorMessage)); + Runtime.getRuntime().exit(val); + } + + /** + * "{:a 1 :b 1 :c 2} -> {1 [:a :b] 2 :c}" + * + * Example usage in java: + * Map<Integer, String> tasks; + * Map<String, List<Integer>> componentTasks = Utils.reverse_map(tasks); + * + * The order of he resulting list values depends on the ordering properties + * of the Map passed in. The caller is responsible for passing an ordered + * map if they expect the result to be consistently ordered as well. + * + * @param map to reverse + * @return a reversed map + */ + public static <K, V> HashMap<V, List<K>> reverseMap(Map<K, V> map) { + HashMap<V, List<K>> rtn = new HashMap<V, List<K>>(); + if (map == null) { + return rtn; + } + for (Entry<K, V> entry : map.entrySet()) { + K key = entry.getKey(); + V val = entry.getValue(); + List<K> list = rtn.get(val); + if (list == null) { + list = new ArrayList<K>(); + rtn.put(entry.getValue(), list); + } + list.add(key); + } + return rtn; + } + + /** + * "[[:a 1] [:b 1] [:c 2]} -> {1 [:a :b] 2 :c}" + * Reverses an assoc-list style Map like reverseMap(Map...) + * + * @param listSeq to reverse + * @return a reversed map + */ + public static HashMap reverseMap(List listSeq) { + HashMap<Object, List<Object>> rtn = new HashMap(); + if (listSeq == null) { + return rtn; + } + for (Object entry : listSeq) { + List listEntry = (List) entry; + Object key = listEntry.get(0); + Object val = listEntry.get(1); + List list = rtn.get(val); + if (list == null) { + list = new ArrayList<Object>(); + rtn.put(val, list); + } + list.add(key); + } + return rtn; + } + + + /** + * @return the pid of this JVM, because Java doesn't provide a real way to do this. + */ + public static String processPid() { + String name = ManagementFactory.getRuntimeMXBean().getName(); + String[] split = name.split("@"); + if (split.length != 2) { + throw new RuntimeException("Got unexpected process name: " + name); + } + return split[0]; + } + + public static int execCommand(String... command) throws ExecuteException, IOException { + CommandLine cmd = new CommandLine(command[0]); + for (int i = 1; i < command.length; i++) { + cmd.addArgument(command[i]); + } + + DefaultExecutor exec = new DefaultExecutor(); + return exec.execute(cmd); + } + + /** + * Extract dir from the jar to destdir + * + * @param jarpath Path to the jar file + * @param dir Directory in the jar to pull out + * @param destdir Path to the directory where the extracted directory will be put + * + */ + public static void extractDirFromJar(String jarpath, String dir, String destdir) { + try (JarFile jarFile = new JarFile(jarpath)) { + Enumeration<JarEntry> jarEnums = jarFile.entries(); + while (jarEnums.hasMoreElements()) { + JarEntry entry = jarEnums.nextElement(); + if (!entry.isDirectory() && entry.getName().startsWith(dir)) { + File aFile = new File(destdir, entry.getName()); + aFile.getParentFile().mkdirs(); + try (FileOutputStream out = new FileOutputStream(aFile); + InputStream in = jarFile.getInputStream(entry)) { + IOUtils.copy(in, out); + } + } + } + } catch (IOException e) { + LOG.info("Could not extract {} from {}", dir, jarpath); + } + } + + public static void sendSignalToProcess(long lpid, int signum) throws IOException { + String pid = Long.toString(lpid); + try { + if (isOnWindows()) { + if (signum == SIGKILL) { + execCommand("taskkill", "/f", "/pid", pid); + } else { + execCommand("taskkill", "/pid", pid); + } + } else { + execCommand("kill", "-" + signum, pid); + } + } catch (ExecuteException e) { + LOG.info("Error when trying to kill {}. Process is probably already dead.", pid); + } catch (IOException e) { + LOG.info("IOException Error when trying to kill {}.", pid); + throw e; + } + } + + public static void forceKillProcess (String pid) throws IOException { + sendSignalToProcess(Long.parseLong(pid), SIGKILL); + } + + public static void killProcessWithSigTerm (String pid) throws IOException { + sendSignalToProcess(Long.parseLong(pid), SIGTERM); + } + + /** + * Adds the user supplied function as a shutdown hook for cleanup. + * Also adds a function that sleeps for a second and then halts the + * runtime to avoid any zombie process in case cleanup function hangs. + */ + public static void addShutdownHookWithForceKillIn1Sec (Runnable func) { + Runnable sleepKill = new Runnable() { + @Override + public void run() { + try { + Time.sleepSecs(1); + Runtime.getRuntime().halt(20); + } catch (Exception e) { + LOG.warn("Exception in the ShutDownHook", e); + } + } + }; + Runtime.getRuntime().addShutdownHook(new Thread(func)); + Runtime.getRuntime().addShutdownHook(new Thread(sleepKill)); + } + + /** + * Returns the combined string, escaped for posix shell. + * @param command the list of strings to be combined + * @return the resulting command string + */ + public static String shellCmd (List<String> command) { + List<String> changedCommands = new ArrayList<>(command.size()); + for (String str: command) { + if (str == null) { + continue; + } + changedCommands.add("'" + str.replaceAll("'", "'\"'\"'") + "'"); + } + return StringUtils.join(changedCommands, " "); + } + + public static String scriptFilePath (String dir) { + return dir + FILE_PATH_SEPARATOR + "storm-worker-script.sh"; + } + + public static String containerFilePath (String dir) { + return dir + FILE_PATH_SEPARATOR + "launch_container.sh"; + } + + public static Object nullToZero (Object v) { + return (v != null ? v : 0); + } + + /** + * Deletes a file or directory and its contents if it exists. Does not + * complain if the input is null or does not exist. + * @param path the path to the file or directory + */ + public static void forceDelete(String path) throws IOException { + _instance.forceDeleteImpl(path); + } + + // Non-static impl methods exist for mocking purposes. + protected void forceDeleteImpl(String path) throws IOException { + LOG.debug("Deleting path {}", path); + if (checkFileExists(path)) { + try { + FileUtils.forceDelete(new File(path)); + } catch (FileNotFoundException ignored) {} + } + } + + /** + * Creates a symbolic link to the target + * @param dir the parent directory of the link + * @param targetDir the parent directory of the link's target + * @param targetFilename the file name of the links target + * @param filename the file name of the link + * @throws IOException + */ + public static void createSymlink(String dir, String targetDir, + String targetFilename, String filename) throws IOException { + Path path = Paths.get(dir, filename).toAbsolutePath(); + Path target = Paths.get(targetDir, targetFilename).toAbsolutePath(); + LOG.debug("Creating symlink [{}] to [{}]", path, target); + if (!path.toFile().exists()) { + Files.createSymbolicLink(path, target); + } + } + + /** + * Convenience method for the case when the link's file name should be the + * same as the file name of the target + */ + public static void createSymlink(String dir, String targetDir, + String targetFilename) throws IOException { + Utils.createSymlink(dir, targetDir, targetFilename, + targetFilename); + } + + /** + * Returns a Collection of file names found under the given directory. + * @param dir a directory + * @return the Collection of file names + */ + public static Collection<String> readDirContents(String dir) { + Collection<String> ret = new HashSet<>(); + File[] files = new File(dir).listFiles(); + if (files != null) { + for (File f: files) { + ret.add(f.getName()); + } + } + return ret; + } + + /** + * Returns the value of java.class.path System property. Kept separate for + * testing. + * @return the classpath + */ + public static String currentClasspath() { + return _instance.currentClasspathImpl(); + } + + // Non-static impl methods exist for mocking purposes. + public String currentClasspathImpl() { + return System.getProperty("java.class.path"); + } + + /** + * Returns a collection of jar file names found under the given directory. + * @param dir the directory to search + * @return the jar file names + */ + private static List<String> getFullJars(String dir) { + File[] files = new File(dir).listFiles(jarFilter); + + if(files == null) { + return new ArrayList<>(); + } + + List<String> ret = new ArrayList<>(files.length); + for (File f : files) { + ret.add(Paths.get(dir, f.getName()).toString()); + } + return ret; + } + private static final FilenameFilter jarFilter = new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.endsWith(".jar"); + } + }; + + + public static String workerClasspath() { + String stormDir = System.getProperty("storm.home"); + + if (stormDir == null) { + return Utils.currentClasspath(); + } + + String stormLibDir = Paths.get(stormDir, "lib").toString(); + String stormConfDir = + System.getenv("STORM_CONF_DIR") != null ? + System.getenv("STORM_CONF_DIR") : + Paths.get(stormDir, "conf").toString(); + String stormExtlibDir = Paths.get(stormDir, "extlib").toString(); + String extcp = System.getenv("STORM_EXT_CLASSPATH"); + List<String> pathElements = new LinkedList<>(); + pathElements.addAll(Utils.getFullJars(stormLibDir)); + pathElements.addAll(Utils.getFullJars(stormExtlibDir)); + pathElements.add(extcp); + pathElements.add(stormConfDir); + + return StringUtils.join(pathElements, + CLASS_PATH_SEPARATOR); + } + + public static String addToClasspath(String classpath, + Collection<String> paths) { + return _instance.addToClasspathImpl(classpath, paths); + } + + // Non-static impl methods exist for mocking purposes. + public String addToClasspathImpl(String classpath, + Collection<String> paths) { + if (paths == null || paths.isEmpty()) { + return classpath; + } + List<String> l = new LinkedList<>(); + l.add(classpath); + l.addAll(paths); + return StringUtils.join(l, CLASS_PATH_SEPARATOR); + } + + public static class UptimeComputer { + int startTime = 0; + + public UptimeComputer() { + startTime = Time.currentTimeSecs(); + } + + public int upTime() { + return Time.deltaSecs(startTime); + } + } + + public static UptimeComputer makeUptimeComputer() { + return _instance.makeUptimeComputerImpl(); + } + + // Non-static impl methods exist for mocking purposes. + public UptimeComputer makeUptimeComputerImpl() { + return new UptimeComputer(); + } + + /** + * Writes a posix shell script file to be executed in its own process. + * @param dir the directory under which the script is to be written + * @param command the command the script is to execute + * @param environment optional environment variables to set before running the script's command. May be null. + * @return the path to the script that has been written + */ + public static String writeScript(String dir, List<String> command, + Map<String,String> environment) throws IOException { + String path = Utils.scriptFilePath(dir); + try(BufferedWriter out = new BufferedWriter(new FileWriter(path))) { + out.write("#!/bin/bash"); + out.newLine(); + if (environment != null) { + for (String k : environment.keySet()) { + String v = environment.get(k); + if (v == null) { + v = ""; + } + out.write(Utils.shellCmd( + Arrays.asList( + "export",k+"="+v))); + out.write(";"); + out.newLine(); + } + } + out.newLine(); + out.write("exec "+Utils.shellCmd(command)+";"); + } + return path; + } + + /** + * A thread that can answer if it is sleeping in the case of simulated time. + * This class is not useful when simulated time is not being used. + */ + public static class SmartThread extends Thread { + public boolean isSleeping() { + return Time.isThreadWaiting(this); + } + public SmartThread(Runnable r) { + super(r); + } + } + + /** + * Creates a thread that calls the given code repeatedly, sleeping for an + * interval of seconds equal to the return value of the previous call. + * + * The given afn may be a callable that returns the number of seconds to + * sleep, or it may be a Callable that returns another Callable that in turn + * returns the number of seconds to sleep. In the latter case isFactory. + * + * @param afn the code to call on each iteration + * @param isDaemon whether the new thread should be a daemon thread + * @param eh code to call when afn throws an exception + * @param priority the new thread's priority + * @param isFactory whether afn returns a callable instead of sleep seconds + * @param startImmediately whether to start the thread before returning + * @param threadName a suffix to be appended to the thread name + * @return the newly created thread + * @see java.lang.Thread + */ + public static SmartThread asyncLoop(final Callable afn, + boolean isDaemon, final Thread.UncaughtExceptionHandler eh, + int priority, final boolean isFactory, boolean startImmediately, + String threadName) { + SmartThread thread = new SmartThread(new Runnable() { + public void run() { + Object s; + try { + Callable fn = isFactory ? (Callable) afn.call() : afn; + while ((s = fn.call()) instanceof Long) { + Time.sleepSecs((Long) s); + } + } catch (Throwable t) { + if (Utils.exceptionCauseIsInstanceOf( + InterruptedException.class, t)) { + LOG.info("Async loop interrupted!"); + return; + } + LOG.error("Async loop died!", t); + throw new RuntimeException(t); + } + } + }); + if (eh != null) { + thread.setUncaughtExceptionHandler(eh); + } else { + thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { + public void uncaughtException(Thread t, Throwable e) { + LOG.error("Async loop died!", e); + Utils.exitProcess(1, "Async loop died!"); + } + }); + } + thread.setDaemon(isDaemon); + thread.setPriority(priority); + if (threadName != null && !threadName.isEmpty()) { + thread.setName(thread.getName() +"-"+ threadName); + } + if (startImmediately) { + thread.start(); + } + return thread; + } + + /** + * Convenience method used when only the function and name suffix are given. + * @param afn the code to call on each iteration + * @param threadName a suffix to be appended to the thread name + * @return the newly created thread + * @see java.lang.Thread + */ + public static SmartThread asyncLoop(final Callable afn, String threadName, final Thread.UncaughtExceptionHandler eh) { + return asyncLoop(afn, false, eh, Thread.NORM_PRIORITY, false, true, + threadName); + } + + /** + * Convenience method used when only the function is given. + * @param afn the code to call on each iteration + * @return the newly created thread + */ + public static SmartThread asyncLoop(final Callable afn) { + return asyncLoop(afn, false, null, Thread.NORM_PRIORITY, false, true, + null); + } + + /** + * A callback that can accept an integer. + * @param <V> the result type of method <code>call</code> + */ + public interface ExitCodeCallable<V> extends Callable<V> { + V call(int exitCode); + } + + /** + * Launch a new process as per {@link java.lang.ProcessBuilder} with a given + * callback. + * @param command the command to be executed in the new process + * @param environment the environment to be applied to the process. Can be + * null. + * @param logPrefix a prefix for log entries from the output of the process. + * Can be null. + * @param exitCodeCallback code to be called passing the exit code value + * when the process completes + * @param dir the working directory of the new process + * @return the new process + * @throws IOException + * @see java.lang.ProcessBuilder + */ + public static Process launchProcess(List<String> command, + Map<String,String> environment, + final String logPrefix, + final ExitCodeCallable exitCodeCallback, + File dir) + throws IOException { + return _instance.launchProcessImpl(command, environment, logPrefix, + exitCodeCallback, dir); + } + + public Process launchProcessImpl( + List<String> command, + Map<String,String> cmdEnv, + final String logPrefix, + final ExitCodeCallable exitCodeCallback, + File dir) + throws IOException { + ProcessBuilder builder = new ProcessBuilder(command); + Map<String,String> procEnv = builder.environment(); + if (dir != null) { + builder.directory(dir); + } + builder.redirectErrorStream(true); + if (cmdEnv != null) { + procEnv.putAll(cmdEnv); + } + final Process process = builder.start(); + if (logPrefix != null || exitCodeCallback != null) { + Utils.asyncLoop(new Callable() { + public Object call() { + if (logPrefix != null ) { + Utils.readAndLogStream(logPrefix, + process.getInputStream()); + } + if (exitCodeCallback != null) { + try { + process.waitFor(); + } catch (InterruptedException ie) { + LOG.info("{} interrupted", logPrefix); + exitCodeCallback.call(process.exitValue()); + } + } + return null; // Run only once. + } + }); + } + return process; + } ++ + public static long bitXor(Object a, Object b) { - return ((Long) a) ^ ((Long) b); ++ return ((Long) a) ^ ((Long) b); + } } -
