├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── NOTICE ├── README.md ├── pom.xml └── src ├── main └── java │ └── com │ └── metamx │ ├── common │ ├── ByteBufferUtils.java │ ├── CompressionUtils.java │ ├── FileUtils.java │ ├── Granularity.java │ ├── IAE.java │ ├── ISE.java │ ├── JodaUtils.java │ ├── MapUtils.java │ ├── MappedByteBufferHandler.java │ ├── Pair.java │ ├── Props.java │ ├── RE.java │ ├── RetryUtils.java │ ├── StreamUtils.java │ ├── StringUtils.java │ ├── Timing.java │ ├── UOE.java │ ├── collect │ │ ├── AggregatingMap.java │ │ ├── CountingMap.java │ │ ├── MoreIterators.java │ │ └── Utils.java │ ├── concurrent │ │ ├── ExecutorServiceConfig.java │ │ ├── ExecutorServices.java │ │ ├── FunctionalThreadFactory.java │ │ ├── ScheduledExecutorFactory.java │ │ ├── ScheduledExecutors.java │ │ └── SimpleExecutorConfig.java │ ├── config │ │ ├── Config.java │ │ └── DurationCoercible.java │ ├── guava │ │ ├── Accumulator.java │ │ ├── Accumulators.java │ │ ├── BaseSequence.java │ │ ├── CloseQuietly.java │ │ ├── Comparators.java │ │ ├── ConcatSequence.java │ │ ├── DefaultingHashMap.java │ │ ├── DelegatingYieldingAccumulator.java │ │ ├── DroppingIterable.java │ │ ├── DroppingIterator.java │ │ ├── ExecuteWhenDoneYielder.java │ │ ├── ExecutorExecutingSequence.java │ │ ├── FilteredSequence.java │ │ ├── FilteringAccumulator.java │ │ ├── FilteringYieldingAccumulator.java │ │ ├── Fns.java │ │ ├── FunctionalIterable.java │ │ ├── FunctionalIterator.java │ │ ├── IteratorWithBaggage.java │ │ ├── LazySequence.java │ │ ├── LimitedSequence.java │ │ ├── LimitedYieldingAccumulator.java │ │ ├── MappedSequence.java │ │ ├── MappingAccumulator.java │ │ ├── MappingYieldingAccumulator.java │ │ ├── MergeIterable.java │ │ ├── MergeIterator.java │ │ ├── MergeSequence.java │ │ ├── ResourceClosingSequence.java │ │ ├── ResourceClosingYielder.java │ │ ├── Sequence.java │ │ ├── Sequences.java │ │ ├── SimpleSequence.java │ │ ├── YieldSign.java │ │ ├── Yielder.java │ │ ├── Yielders.java │ │ ├── YieldingAccumulator.java │ │ ├── YieldingAccumulators.java │ │ ├── YieldingSequenceBase.java │ │ └── nary │ │ │ ├── BinaryFn.java │ │ │ ├── BinaryTransformIterable.java │ │ │ ├── BinaryTransformIterator.java │ │ │ ├── SortedMergeIterable.java │ │ │ ├── SortedMergeIterator.java │ │ │ ├── TrinaryFn.java │ │ │ ├── TrinaryTransformIterable.java │ │ │ └── TrinaryTransformIterator.java │ ├── io │ │ └── smoosh │ │ │ ├── FileSmoosher.java │ │ │ ├── Metadata.java │ │ │ ├── Smoosh.java │ │ │ ├── SmooshedFileMapper.java │ │ │ └── SmooshedWriter.java │ ├── lifecycle │ │ ├── Lifecycle.java │ │ ├── LifecycleStart.java │ │ └── LifecycleStop.java │ ├── logger │ │ └── Logger.java │ └── parsers │ │ ├── CSVParser.java │ │ ├── CloseableIterator.java │ │ ├── DelimitedParser.java │ │ ├── JSONParser.java │ │ ├── JSONPathParser.java │ │ ├── JSONToLowerParser.java │ │ ├── JavaScriptParser.java │ │ ├── ParseException.java │ │ ├── Parser.java │ │ ├── ParserUtils.java │ │ ├── Parsers.java │ │ ├── RegexParser.java │ │ ├── TimestampParser.java │ │ └── ToLowerCaseParser.java │ ├── emitter │ ├── EmittingLogger.java │ ├── core │ │ ├── BaseHttpEmittingConfig.java │ │ ├── Batch.java │ │ ├── BatchingStrategy.java │ │ ├── ComposingEmitter.java │ │ ├── ComposingEmitterConfig.java │ │ ├── ConcurrentTimeCounter.java │ │ ├── ContentEncoding.java │ │ ├── EmittedBatchCounter.java │ │ ├── Emitter.java │ │ ├── Emitters.java │ │ ├── Event.java │ │ ├── FeedUriExtractor.java │ │ ├── HttpEmitterConfig.java │ │ ├── HttpPostEmitter.java │ │ ├── JacksonUtil.java │ │ ├── LoggingEmitter.java │ │ ├── LoggingEmitterConfig.java │ │ ├── NoopEmitter.java │ │ ├── ParametrizedUriEmitter.java │ │ ├── ParametrizedUriEmitterConfig.java │ │ ├── ParametrizedUriExtractor.java │ │ ├── UriExtractor.java │ │ ├── ZeroCopyByteArrayOutputStream.java │ │ ├── factory │ │ │ ├── ComposingEmitterFactory.java │ │ │ ├── EmitterFactory.java │ │ │ ├── HttpEmitterFactory.java │ │ │ ├── LoggingEmitterFactory.java │ │ │ ├── NoopEmiterFactory.java │ │ │ └── ParametrizedUriEmitterFactory.java │ │ ├── filter │ │ │ ├── BWListEventFilter.java │ │ │ ├── BWListEventFilterConfig.java │ │ │ ├── BWListEventFilterFactory.java │ │ │ ├── EmptyBWListEventFilter.java │ │ │ ├── FeedBWListEventFilter.java │ │ │ └── MultiBWListEventFilter.java │ │ └── statsd │ │ │ ├── DimensionConverter.java │ │ │ ├── StatsDEmitter.java │ │ │ ├── StatsDEmitterConfig.java │ │ │ ├── StatsDEmitterFactory.java │ │ │ └── StatsDMetric.java │ └── service │ │ ├── AlertBuilder.java │ │ ├── AlertEvent.java │ │ ├── ServiceEmitter.java │ │ ├── ServiceEvent.java │ │ ├── ServiceEventBuilder.java │ │ └── ServiceMetricEvent.java │ ├── http │ └── client │ │ ├── AbstractHttpClient.java │ │ ├── CredentialedHttpClient.java │ │ ├── EnforceSslHttpClient.java │ │ ├── HttpClient.java │ │ ├── HttpClientConfig.java │ │ ├── HttpClientInit.java │ │ ├── NettyHttpClient.java │ │ ├── Request.java │ │ ├── auth │ │ ├── BasicCredentials.java │ │ └── Credentials.java │ │ ├── io │ │ └── AppendableByteArrayInputStream.java │ │ ├── netty │ │ └── HttpClientPipelineFactory.java │ │ ├── pool │ │ ├── ChannelResourceFactory.java │ │ ├── ResourceContainer.java │ │ ├── ResourceFactory.java │ │ ├── ResourcePool.java │ │ ├── ResourcePoolConfig.java │ │ └── ResourceVerifier.java │ │ └── response │ │ ├── ClientResponse.java │ │ ├── FullResponseHandler.java │ │ ├── FullResponseHolder.java │ │ ├── HttpResponseHandler.java │ │ ├── InputStreamResponseHandler.java │ │ ├── SequenceInputStreamResponseHandler.java │ │ ├── StatusResponseHandler.java │ │ ├── StatusResponseHolder.java │ │ └── ToStringResponseHandler.java │ └── metrics │ ├── AbstractMonitor.java │ ├── CgroupUtil.java │ ├── CompoundMonitor.java │ ├── CpuAcctDeltaMonitor.java │ ├── FeedDefiningMonitor.java │ ├── HttpPostEmitterMonitor.java │ ├── JvmCpuMonitor.java │ ├── JvmMonitor.java │ ├── JvmPidDiscoverer.java │ ├── JvmThreadsMonitor.java │ ├── KeyedDiff.java │ ├── Monitor.java │ ├── MonitorOfTheMonitors.java │ ├── MonitorScheduler.java │ ├── MonitorSchedulerConfig.java │ ├── MonitorUtils.java │ ├── Monitors.java │ ├── ParametrizedUriEmitterMonitor.java │ ├── PidDiscoverer.java │ ├── SigarPidDiscoverer.java │ ├── SigarUtil.java │ ├── SysMonitor.java │ └── cgroups │ ├── CgroupDiscoverer.java │ ├── CpuAcct.java │ ├── ProcCgroupDiscoverer.java │ ├── ProcPidCgroupDiscoverer.java │ └── ProcSelfCgroupDiscoverer.java └── test ├── java └── com │ └── metamx │ ├── common │ ├── BufferUtils.java │ ├── ByteBufferUtilsTest.java │ ├── CompressionUtilsTest.java │ ├── FileUtilsTest.java │ ├── JodaUtilsTest.java │ ├── RetryUtilsTest.java │ ├── StreamUtilsTest.java │ ├── StringUtilsTest.java │ ├── TestGranularity.java │ ├── guava │ │ ├── BaseSequenceTest.java │ │ ├── ComparatorsTest.java │ │ ├── ConcatSequenceTest.java │ │ ├── ExecutorExecutingSequenceTest.java │ │ ├── FilteredSequenceTest.java │ │ ├── FunctionalIterableTest.java │ │ ├── FunctionalIteratorTest.java │ │ ├── LimitedSequenceTest.java │ │ ├── MappedSequenceTest.java │ │ ├── MergeIteratorTest.java │ │ ├── MergeSequenceTest.java │ │ ├── ResourceClosingSequenceTest.java │ │ ├── SequenceTestHelper.java │ │ ├── TestSequence.java │ │ ├── UnsupportedSequence.java │ │ └── nary │ │ │ └── SortedMergeIteratorTest.java │ ├── io │ │ └── smoosh │ │ │ └── SmooshedFileMapperTest.java │ ├── lifecycle │ │ └── LifecycleTest.java │ ├── logger │ │ └── LoggerTest.java │ └── parsers │ │ ├── CSVParserTest.java │ │ ├── DelimitedParserTest.java │ │ ├── JSONParserTest.java │ │ ├── JSONPathParserTest.java │ │ ├── JavaScriptParserTest.java │ │ ├── RegexParserTest.java │ │ └── TimestampParserTest.java │ ├── emitter │ ├── core │ │ ├── BWListEventFilterConfigTest.java │ │ ├── ComposingEmitterTest.java │ │ ├── ComposingEmitterWithParametrizedTest.java │ │ ├── CustomEmitterFactoryTest.java │ │ ├── EmitterTest.java │ │ ├── EmittersTest.java │ │ ├── GoHandler.java │ │ ├── GoHandlers.java │ │ ├── HttpEmitterConfigTest.java │ │ ├── HttpEmitterTest.java │ │ ├── HttpPostEmitterStressTest.java │ │ ├── IntEvent.java │ │ ├── LoggingEmitterConfigTest.java │ │ ├── MapEvent.java │ │ ├── MockHttpClient.java │ │ ├── ParametrizedUriEmitterConfigTest.java │ │ ├── ParametrizedUriEmitterTest.java │ │ ├── filter │ │ │ ├── EmptyBWListEventFilterTest.java │ │ │ ├── FeedBWListEventFilterTest.java │ │ │ └── MultiBWListEventFilterTest.java │ │ └── statsd │ │ │ ├── DimensionConverterTest.java │ │ │ ├── StatsDEmitterConfigTest.java │ │ │ └── StatsDEmitterTest.java │ └── service │ │ ├── AlertEventTest.java │ │ ├── ServiceMetricEventTest.java │ │ └── UnitEvent.java │ ├── http │ └── client │ │ ├── AsyncHttpClientTest.java │ │ ├── FriendlyServersTest.java │ │ ├── JankyServersTest.java │ │ ├── io │ │ └── AppendableByteArrayInputStreamTest.java │ │ ├── pool │ │ └── ResourcePoolTest.java │ │ └── response │ │ └── SequenceInputStreamResponseHandlerTest.java │ └── metrics │ ├── CpuAcctDeltaMonitorTest.java │ ├── GcNameTest.java │ ├── JvmMonitorTest.java │ ├── JvmPidDiscovererTest.java │ ├── MonitorUtilsTest.java │ ├── MonitorsTest.java │ ├── SigarLoadTest.java │ ├── SigarPidDiscovererTest.java │ ├── StubServiceEmitter.java │ └── cgroups │ ├── CpuAcctTest.java │ ├── ProcCgroupDiscovererTest.java │ └── TestUtils.java └── resources ├── cpuacct.usage_all ├── defaultMetricDimensions.json ├── keystore.jks ├── log4j.xml ├── loremipsum.txt ├── proc.cgroups ├── proc.mounts └── proc.pid.cgroup /.gitignore: -------------------------------------------------------------------------------- 1 | *.ipr 2 | *.iws 3 | *.iml 4 | target 5 | dist 6 | *.jar 7 | *.tar 8 | *.zip 9 | *.iml 10 | *.ipr 11 | .idea 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | dist: trusty 3 | sudo: false 4 | 5 | jdk: 6 | - oraclejdk8 7 | 8 | cache: 9 | directories: 10 | - $HOME/.m2 11 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | For code contributions, we do require that a contributor license agreement 2 | (CLA) is agreed to before we can accept the code. Our CLA is fashioned after 3 | the Apache CLA. 4 | 5 | ### Corporate Agreement 6 | 7 | For corporate contributions, please fill out and return the [Metamarkets Corporate 8 | CLA](https://docs.google.com/document/d/1fgAWpzJT5sVwXqSF9r00gMsoJlxZyw24Ifejb4oyyrc/edit?usp=sharing) 9 | to [druid-admin@metamarkets.com](mailto:druid-admin@metamarkets.com) 10 | 11 | ### Individual Agreement 12 | 13 | For individual contributions please fill out and return the [Metamarkets Individual 14 | CLA](https://docs.google.com/document/d/1tHE41iQuQmowLgkBeAe7YMt6EfVjo2hLncvV80bfONs/edit?usp=sharing) 15 | to [druid-admin@metamarkets.com](mailto:druid-admin@metamarkets.com) 16 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | java-util 2 | Copyright 2011-2017 Metamarkets Group Inc. 3 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/ByteBufferUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011-2016 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common; 18 | 19 | import sun.misc.Cleaner; 20 | import sun.nio.ch.DirectBuffer; 21 | 22 | import java.nio.ByteBuffer; 23 | import java.nio.MappedByteBuffer; 24 | 25 | /** 26 | */ 27 | public class ByteBufferUtils 28 | { 29 | /** 30 | * Releases memory held by the given direct ByteBuffer 31 | * 32 | * @param buffer buffer to free 33 | */ 34 | public static void free(ByteBuffer buffer) 35 | { 36 | if (buffer.isDirect()) { 37 | clean((DirectBuffer) buffer); 38 | } 39 | } 40 | 41 | 42 | /** 43 | * Un-maps the given memory mapped file 44 | * 45 | * @param buffer buffer 46 | */ 47 | public static void unmap(MappedByteBuffer buffer) 48 | { 49 | free(buffer); 50 | } 51 | 52 | private static void clean(DirectBuffer buffer) 53 | { 54 | final Cleaner cleaner = buffer.cleaner(); 55 | if (cleaner != null) { 56 | cleaner.clean(); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/IAE.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common; 18 | 19 | /** 20 | */ 21 | public class IAE extends IllegalArgumentException 22 | { 23 | public IAE(String formatText, Object... arguments) 24 | { 25 | super(StringUtils.safeFormat(formatText, arguments)); 26 | } 27 | 28 | public IAE(Throwable cause, String formatText, Object... arguments) 29 | { 30 | super(StringUtils.safeFormat(formatText, arguments), cause); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/ISE.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common; 18 | 19 | /** 20 | */ 21 | public class ISE extends IllegalStateException 22 | { 23 | public ISE(String formatText, Object... arguments) 24 | { 25 | super(StringUtils.safeFormat(formatText, arguments)); 26 | } 27 | 28 | public ISE(Throwable cause, String formatText, Object... arguments) 29 | { 30 | super(StringUtils.safeFormat(formatText, arguments), cause); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/MappedByteBufferHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.metamx.common; 19 | 20 | import java.io.File; 21 | import java.nio.MappedByteBuffer; 22 | 23 | /** 24 | * Facilitates using try-with-resources with {@link MappedByteBuffer}s which don't implement {@link AutoCloseable}. 25 | * 26 | *

This interface is a specialization of {@code io.druid.collections.ResourceHandler}. 27 | * @see FileUtils#map(File) 28 | */ 29 | public final class MappedByteBufferHandler implements AutoCloseable 30 | { 31 | private final MappedByteBuffer mappedByteBuffer; 32 | 33 | MappedByteBufferHandler(MappedByteBuffer mappedByteBuffer) 34 | { 35 | this.mappedByteBuffer = mappedByteBuffer; 36 | } 37 | 38 | /** 39 | * Returns the wrapped buffer. 40 | */ 41 | public MappedByteBuffer get() 42 | { 43 | return mappedByteBuffer; 44 | } 45 | 46 | /** 47 | * Unmaps the wrapped buffer. 48 | */ 49 | @Override 50 | public void close() 51 | { 52 | ByteBufferUtils.unmap(mappedByteBuffer); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/Props.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 - 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common; 18 | 19 | import java.io.FileInputStream; 20 | import java.io.IOException; 21 | import java.util.Map; 22 | import java.util.Properties; 23 | 24 | public class Props { 25 | 26 | public static Properties fromFilename(String filename) throws IOException { 27 | final Properties props = new Properties(); 28 | props.load(new FileInputStream(filename)); 29 | return props; 30 | } 31 | 32 | public static Properties fromEnvs(String prefix) { 33 | final Properties props = new Properties(); 34 | for (Map.Entry env: System.getenv().entrySet()) { 35 | if (env.getKey().startsWith(prefix)) { 36 | props.put(env.getKey().replaceAll("_", "."), env.getValue()); 37 | } 38 | } 39 | return props; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/RE.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common; 18 | 19 | /** 20 | */ 21 | public class RE extends RuntimeException 22 | { 23 | public RE(String formatText, Object... arguments) 24 | { 25 | super(StringUtils.safeFormat(formatText, arguments)); 26 | } 27 | 28 | public RE(Throwable cause, String formatText, Object... arguments) 29 | { 30 | super(StringUtils.safeFormat(formatText, arguments), cause); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/Timing.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common; 18 | 19 | import com.metamx.common.logger.Logger; 20 | 21 | import java.util.concurrent.Callable; 22 | 23 | /** 24 | */ 25 | public class Timing { 26 | public static RetType timeBenchmarkWrapException(String prefix, Callable callable, final Logger log) { 27 | try { 28 | return timeBenchmark(prefix, callable, log); 29 | } catch (Exception e) { 30 | throw new RuntimeException(e); 31 | } 32 | } 33 | 34 | 35 | public static RetType timeBenchmark(String prefix, Callable callable, Logger log) throws Exception { 36 | RetType retVal; 37 | 38 | long startTime = System.currentTimeMillis(); 39 | retVal = callable.call(); 40 | long endTime = System.currentTimeMillis(); 41 | 42 | log.info(String.format("%s completed %,d millis.", prefix, endTime - startTime)); 43 | 44 | return retVal; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/UOE.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common; 18 | 19 | /** 20 | */ 21 | public class UOE extends UnsupportedOperationException 22 | { 23 | public UOE(String formatText, Object... arguments) 24 | { 25 | super(StringUtils.safeFormat(formatText, arguments)); 26 | } 27 | 28 | public UOE(Throwable cause, String formatText, Object... arguments) 29 | { 30 | super(StringUtils.safeFormat(formatText, arguments), cause); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/collect/AggregatingMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.collect; 18 | 19 | import java.util.HashMap; 20 | 21 | // Can't find a good way to abstract over which aggregator representation is used, 22 | // so I just pick Double/MutableDouble. 23 | public class AggregatingMap extends HashMap 24 | { 25 | public void add(K k, double n) 26 | { 27 | final Double value = get(k); 28 | 29 | if (value == null) { 30 | put(k, n); 31 | return; 32 | } 33 | 34 | put(k, value + n); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/collect/CountingMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.collect; 18 | 19 | import com.google.common.base.Function; 20 | import com.google.common.collect.Maps; 21 | 22 | import java.util.AbstractMap; 23 | import java.util.HashMap; 24 | import java.util.Set; 25 | import java.util.concurrent.atomic.AtomicLong; 26 | 27 | // Can't find a good way to abstract over which counter representation is used, 28 | // so I just pick Long/MutableLong. 29 | public class CountingMap extends AbstractMap 30 | { 31 | private final HashMap counts = new HashMap<>(); 32 | 33 | public void add(K k, Long n) 34 | { 35 | if (!counts.containsKey(k)) { 36 | counts.put(k, new AtomicLong(0)); 37 | } 38 | counts.get(k).addAndGet(n); 39 | } 40 | 41 | public Set> entrySet() 42 | { 43 | return Maps.transformValues( 44 | counts, 45 | new Function() 46 | { 47 | @Override 48 | public Long apply(AtomicLong n) 49 | { 50 | return n.get(); 51 | } 52 | } 53 | ).entrySet(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/concurrent/ExecutorServiceConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.concurrent; 18 | 19 | import org.skife.config.Config; 20 | import org.skife.config.Default; 21 | 22 | /** 23 | */ 24 | public abstract class ExecutorServiceConfig 25 | { 26 | @Config(value = "${base_path}.formatString") 27 | @Default("processing-%s") 28 | public abstract String getFormatString(); 29 | 30 | @Config(value = "${base_path}.numThreads") 31 | public int getNumThreads() 32 | { 33 | return Runtime.getRuntime().availableProcessors() - 1; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/concurrent/ExecutorServices.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.concurrent; 18 | 19 | import com.google.common.base.Throwables; 20 | import com.google.common.util.concurrent.ThreadFactoryBuilder; 21 | import com.metamx.common.lifecycle.Lifecycle; 22 | 23 | import java.util.concurrent.ExecutorService; 24 | import java.util.concurrent.Executors; 25 | 26 | public class ExecutorServices 27 | { 28 | public static ExecutorService create(Lifecycle lifecycle, ExecutorServiceConfig config) 29 | { 30 | return manageLifecycle( 31 | lifecycle, 32 | Executors.newFixedThreadPool( 33 | config.getNumThreads(), 34 | new ThreadFactoryBuilder().setDaemon(true).setNameFormat(config.getFormatString()).build() 35 | ) 36 | ); 37 | } 38 | 39 | public static T manageLifecycle(Lifecycle lifecycle, final T service) 40 | { 41 | try { 42 | lifecycle.addMaybeStartHandler( 43 | new Lifecycle.Handler() 44 | { 45 | @Override 46 | public void start() throws Exception 47 | { 48 | } 49 | 50 | @Override 51 | public void stop() 52 | { 53 | service.shutdownNow(); 54 | } 55 | } 56 | ); 57 | } 58 | catch (Exception e) { 59 | Throwables.propagate(e); 60 | } 61 | return service; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/concurrent/ScheduledExecutorFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.concurrent; 18 | 19 | import java.util.concurrent.ScheduledExecutorService; 20 | 21 | public interface ScheduledExecutorFactory 22 | { 23 | public ScheduledExecutorService create(int corePoolSize, String nameFormat); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/concurrent/SimpleExecutorConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.concurrent; 18 | 19 | /** 20 | */ 21 | public class SimpleExecutorConfig extends ExecutorServiceConfig 22 | { 23 | private final String formatString; 24 | private final int numThreads; 25 | 26 | public SimpleExecutorConfig( 27 | String formatString, 28 | int numThreads 29 | ) 30 | { 31 | this.formatString = formatString; 32 | this.numThreads = numThreads; 33 | } 34 | 35 | @Override 36 | public String getFormatString() 37 | { 38 | return formatString; 39 | } 40 | 41 | @Override 42 | public int getNumThreads() 43 | { 44 | return numThreads; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/config/Config.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.config; 18 | 19 | import org.skife.config.ConfigurationObjectFactory; 20 | 21 | import java.util.Properties; 22 | 23 | /** 24 | */ 25 | public class Config 26 | { 27 | public static ConfigurationObjectFactory createFactory(Properties props) 28 | { 29 | ConfigurationObjectFactory configFactory = new ConfigurationObjectFactory(props); 30 | configFactory.addCoercible(new DurationCoercible()); 31 | return configFactory; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/config/DurationCoercible.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.config; 18 | 19 | import org.joda.time.Duration; 20 | import org.joda.time.Period; 21 | import org.skife.config.Coercer; 22 | import org.skife.config.Coercible; 23 | 24 | /** 25 | */ 26 | public class DurationCoercible implements Coercible 27 | { 28 | @Override 29 | public Coercer accept(Class clazz) 30 | { 31 | if (Duration.class != clazz) { 32 | return null; 33 | } 34 | 35 | return new Coercer() 36 | { 37 | @Override 38 | public Duration coerce(String value) 39 | { 40 | return new Period(value).toStandardDuration(); 41 | } 42 | }; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/Accumulator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | /** 20 | */ 21 | public interface Accumulator 22 | { 23 | public AccumulatedType accumulate(AccumulatedType accumulated, InType in); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/Accumulators.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import java.util.List; 20 | 21 | /** 22 | */ 23 | public class Accumulators 24 | { 25 | public static , T> Accumulator list() 26 | { 27 | return new Accumulator() 28 | { 29 | @Override 30 | public ListType accumulate(ListType accumulated, T in) 31 | { 32 | accumulated.add(in); 33 | return accumulated; 34 | } 35 | }; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/CloseQuietly.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.metamx.common.logger.Logger; 20 | 21 | import java.io.Closeable; 22 | import java.io.IOException; 23 | 24 | /** 25 | */ 26 | public class CloseQuietly 27 | { 28 | private static final Logger log = new Logger(CloseQuietly.class); 29 | 30 | public static void close(Closeable closeable) 31 | { 32 | if (closeable == null) { 33 | return; 34 | } 35 | try { 36 | closeable.close(); 37 | } 38 | catch (IOException e) { 39 | log.error(e, "IOException thrown while closing Closeable."); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/DefaultingHashMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.base.Supplier; 20 | 21 | import java.util.HashMap; 22 | 23 | /** 24 | */ 25 | public class DefaultingHashMap extends HashMap 26 | { 27 | private final Supplier supplier; 28 | 29 | public DefaultingHashMap( 30 | Supplier supplier 31 | ) 32 | { 33 | this.supplier = supplier; 34 | } 35 | 36 | @Override 37 | public V get(Object o) 38 | { 39 | V retVal = super.get(o); 40 | 41 | if (retVal == null) { 42 | retVal = supplier.get(); 43 | super.put((K) o, retVal); 44 | } 45 | 46 | return retVal; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/DelegatingYieldingAccumulator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | /** 20 | */ 21 | public class DelegatingYieldingAccumulator extends YieldingAccumulator 22 | { 23 | private final YieldingAccumulator delegate; 24 | 25 | public DelegatingYieldingAccumulator( 26 | YieldingAccumulator delegate 27 | ) 28 | { 29 | this.delegate = delegate; 30 | } 31 | 32 | @Override 33 | public void yield() 34 | { 35 | delegate.yield(); 36 | } 37 | 38 | @Override 39 | public boolean yielded() 40 | { 41 | return delegate.yielded(); 42 | } 43 | 44 | @Override 45 | public void reset() 46 | { 47 | delegate.reset(); 48 | } 49 | 50 | @Override 51 | public OutType accumulate(OutType accumulated, T in) 52 | { 53 | return delegate.accumulate(accumulated, in); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/DroppingIterable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import java.util.Iterator; 20 | 21 | /** 22 | */ 23 | public class DroppingIterable implements Iterable 24 | { 25 | private final Iterable delegate; 26 | private final int numToDrop; 27 | 28 | public DroppingIterable( 29 | Iterable delegate, 30 | int numToDrop 31 | ) 32 | { 33 | this.delegate = delegate; 34 | this.numToDrop = numToDrop; 35 | } 36 | 37 | public Iterator iterator() 38 | { 39 | return new DroppingIterator<>(delegate.iterator(), numToDrop); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/DroppingIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import java.util.Iterator; 20 | 21 | /** 22 | */ 23 | public class DroppingIterator implements Iterator 24 | { 25 | private final Iterator delegate; 26 | private final int numToDrop; 27 | private boolean dropped = false; 28 | 29 | public DroppingIterator( 30 | Iterator delegate, 31 | int numToDrop 32 | ) 33 | { 34 | this.delegate = delegate; 35 | this.numToDrop = numToDrop; 36 | } 37 | 38 | public boolean hasNext() 39 | { 40 | if (! dropped) { 41 | for (int i = 0; i < numToDrop; ++i) { 42 | delegate.next(); 43 | } 44 | dropped = true; 45 | } 46 | 47 | return delegate.hasNext(); 48 | } 49 | 50 | public T next() 51 | { 52 | if (! dropped) { 53 | for (int i = 0; i < numToDrop; ++i) { 54 | delegate.next(); 55 | } 56 | dropped = true; 57 | } 58 | return delegate.next(); 59 | } 60 | 61 | public void remove() 62 | { 63 | throw new UnsupportedOperationException(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/ExecuteWhenDoneYielder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.metamx.common.guava; 19 | 20 | import java.io.IOException; 21 | import java.util.concurrent.Executor; 22 | 23 | public class ExecuteWhenDoneYielder implements Yielder 24 | { 25 | private final Yielder baseYielder; 26 | private final Runnable runnable; 27 | private final Executor executor; 28 | 29 | public ExecuteWhenDoneYielder(Yielder baseYielder, Runnable runnable, Executor executor) 30 | { 31 | this.baseYielder = baseYielder; 32 | this.runnable = runnable; 33 | this.executor = executor; 34 | } 35 | 36 | @Override 37 | public T get() 38 | { 39 | return baseYielder.get(); 40 | } 41 | 42 | @Override 43 | public Yielder next(T initValue) 44 | { 45 | return new ExecuteWhenDoneYielder<>(baseYielder.next(initValue), runnable, executor); 46 | } 47 | 48 | @Override 49 | public boolean isDone() 50 | { 51 | return baseYielder.isDone(); 52 | } 53 | 54 | @Override 55 | public void close() throws IOException 56 | { 57 | if (isDone()) { 58 | executor.execute(runnable); 59 | } 60 | baseYielder.close(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/FilteringAccumulator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.base.Predicate; 20 | 21 | /** 22 | */ 23 | public class FilteringAccumulator implements Accumulator 24 | { 25 | private final Predicate pred; 26 | private final Accumulator accumulator; 27 | 28 | public FilteringAccumulator( 29 | Predicate pred, 30 | Accumulator accumulator 31 | ) { 32 | this.pred = pred; 33 | this.accumulator = accumulator; 34 | } 35 | 36 | @Override 37 | public OutType accumulate(OutType accumulated, T in) 38 | { 39 | if (pred.apply(in)) { 40 | return accumulator.accumulate(accumulated, in); 41 | } 42 | return accumulated; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/FilteringYieldingAccumulator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.base.Predicate; 20 | 21 | /** 22 | */ 23 | public class FilteringYieldingAccumulator extends YieldingAccumulator 24 | { 25 | private final Predicate pred; 26 | private final YieldingAccumulator accumulator; 27 | 28 | private volatile boolean didSomething = false; 29 | 30 | public FilteringYieldingAccumulator( 31 | Predicate pred, 32 | YieldingAccumulator accumulator 33 | ) { 34 | this.pred = pred; 35 | this.accumulator = accumulator; 36 | } 37 | 38 | @Override 39 | public void yield() 40 | { 41 | accumulator.yield(); 42 | } 43 | 44 | @Override 45 | public boolean yielded() 46 | { 47 | return accumulator.yielded(); 48 | } 49 | 50 | @Override 51 | public void reset() 52 | { 53 | didSomething = false; 54 | accumulator.reset(); 55 | } 56 | 57 | public boolean didSomething() 58 | { 59 | return didSomething; 60 | } 61 | 62 | @Override 63 | public OutType accumulate(OutType accumulated, T in) 64 | { 65 | if (pred.apply(in)) { 66 | if (!didSomething) { 67 | didSomething = true; 68 | } 69 | return accumulator.accumulate(accumulated, in); 70 | } 71 | return accumulated; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/Fns.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.base.Function; 20 | 21 | import java.util.Map; 22 | 23 | /** 24 | */ 25 | public class Fns 26 | { 27 | public static Function splitFn(final String splitChar, final int numCols) 28 | { 29 | return new Function() 30 | { 31 | public String[] apply(String input) 32 | { 33 | return input.split(splitChar, numCols); 34 | } 35 | }; 36 | } 37 | 38 | public static Function, OutType> getFromMap(final KeyType key) 39 | { 40 | return new Function, OutType>() 41 | { 42 | @Override 43 | public OutType apply(Map in) 44 | { 45 | return in.get(key); 46 | } 47 | }; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/IteratorWithBaggage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.metamx.common.parsers.CloseableIterator; 20 | 21 | import java.io.Closeable; 22 | import java.io.IOException; 23 | import java.util.Iterator; 24 | 25 | /** 26 | */ 27 | public class IteratorWithBaggage implements CloseableIterator 28 | { 29 | private final Iterator baseIter; 30 | private final Closeable baggage; 31 | 32 | public IteratorWithBaggage( 33 | Iterator baseIter, 34 | Closeable baggage 35 | ) 36 | { 37 | this.baseIter = baseIter; 38 | this.baggage = baggage; 39 | } 40 | 41 | @Override 42 | public boolean hasNext() 43 | { 44 | return baseIter.hasNext(); 45 | } 46 | 47 | @Override 48 | public T next() 49 | { 50 | return baseIter.next(); 51 | } 52 | 53 | @Override 54 | public void remove() 55 | { 56 | baseIter.remove(); 57 | } 58 | 59 | @Override 60 | public void close() throws IOException 61 | { 62 | baggage.close(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/LazySequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.base.Supplier; 20 | 21 | /** 22 | */ 23 | public class LazySequence implements Sequence 24 | { 25 | private final Supplier> provider; 26 | 27 | public LazySequence( 28 | Supplier> provider 29 | ) 30 | { 31 | this.provider = provider; 32 | } 33 | 34 | @Override 35 | public OutType accumulate(OutType initValue, Accumulator accumulator) 36 | { 37 | return provider.get().accumulate(initValue, accumulator); 38 | } 39 | 40 | @Override 41 | public Yielder toYielder(OutType initValue, YieldingAccumulator accumulator) 42 | { 43 | return provider.get().toYielder(initValue, accumulator); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/LimitedYieldingAccumulator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 - 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | /** 20 | * @deprecated this class uses expensive volatile counter inside, but it is not thread-safe. It is going to be removed 21 | * in the future. 22 | */ 23 | @Deprecated 24 | public class LimitedYieldingAccumulator extends YieldingAccumulator 25 | { 26 | private final int limit; 27 | private final YieldingAccumulator delegate; 28 | 29 | private volatile int count = 0; 30 | 31 | public LimitedYieldingAccumulator( 32 | YieldingAccumulator delegate, int limit 33 | ) 34 | { 35 | this.limit = limit; 36 | this.delegate = delegate; 37 | } 38 | 39 | @Override 40 | public void yield() 41 | { 42 | delegate.yield(); 43 | } 44 | 45 | @Override 46 | public boolean yielded() 47 | { 48 | return delegate.yielded(); 49 | } 50 | 51 | @Override 52 | public void reset() 53 | { 54 | delegate.reset(); 55 | } 56 | 57 | @Override 58 | public OutType accumulate(OutType accumulated, T in) 59 | { 60 | if (count < limit) { 61 | count++; 62 | return delegate.accumulate(accumulated, in); 63 | } 64 | return accumulated; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/MappedSequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.base.Function; 20 | 21 | /** 22 | */ 23 | public class MappedSequence implements Sequence 24 | { 25 | private final Sequence baseSequence; 26 | private final Function fn; 27 | 28 | public MappedSequence( 29 | Sequence baseSequence, 30 | Function fn 31 | ) 32 | { 33 | this.baseSequence = baseSequence; 34 | this.fn = fn; 35 | } 36 | 37 | @Override 38 | public OutType accumulate(OutType initValue, Accumulator accumulator) 39 | { 40 | return baseSequence.accumulate(initValue, new MappingAccumulator<>(fn, accumulator)); 41 | } 42 | 43 | @Override 44 | public Yielder toYielder(OutType initValue, YieldingAccumulator accumulator) 45 | { 46 | return baseSequence.toYielder(initValue, new MappingYieldingAccumulator<>(fn, accumulator)); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/MappingAccumulator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.base.Function; 20 | 21 | /** 22 | */ 23 | public class MappingAccumulator implements Accumulator 24 | { 25 | private final Function fn; 26 | private final Accumulator accumulator; 27 | 28 | public MappingAccumulator( 29 | Function fn, 30 | Accumulator accumulator 31 | ) { 32 | this.fn = fn; 33 | this.accumulator = accumulator; 34 | } 35 | 36 | @Override 37 | public OutType accumulate(OutType accumulated, InType in) 38 | { 39 | return accumulator.accumulate(accumulated, fn.apply(in)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/MappingYieldingAccumulator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.base.Function; 20 | 21 | /** 22 | */ 23 | public class MappingYieldingAccumulator extends YieldingAccumulator 24 | { 25 | private final Function fn; 26 | private final YieldingAccumulator baseAccumulator; 27 | 28 | public MappingYieldingAccumulator( 29 | Function fn, 30 | YieldingAccumulator baseAccumulator 31 | ) { 32 | this.fn = fn; 33 | this.baseAccumulator = baseAccumulator; 34 | } 35 | 36 | @Override 37 | public void yield() 38 | { 39 | baseAccumulator.yield(); 40 | } 41 | 42 | @Override 43 | public boolean yielded() 44 | { 45 | return baseAccumulator.yielded(); 46 | } 47 | 48 | @Override 49 | public void reset() 50 | { 51 | baseAccumulator.reset(); 52 | } 53 | 54 | @Override 55 | public OutType accumulate(OutType accumulated, InType in) 56 | { 57 | return baseAccumulator.accumulate(accumulated, fn.apply(in)); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/MergeIterable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.collect.Lists; 20 | 21 | import java.util.Comparator; 22 | import java.util.Iterator; 23 | import java.util.List; 24 | 25 | /** 26 | */ 27 | public class MergeIterable implements Iterable 28 | { 29 | private final Comparator comparator; 30 | private final Iterable> baseIterables; 31 | 32 | public MergeIterable( 33 | Comparator comparator, 34 | Iterable> baseIterables 35 | ) 36 | { 37 | this.comparator = comparator; 38 | this.baseIterables = baseIterables; 39 | } 40 | 41 | @Override 42 | public Iterator iterator() 43 | { 44 | List> iterators = Lists.newArrayList(); 45 | for (Iterable baseIterable : baseIterables) { 46 | iterators.add(baseIterable.iterator()); 47 | } 48 | 49 | return new MergeIterator<>(comparator, iterators); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/MergeIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.collect.Iterators; 20 | import com.google.common.collect.PeekingIterator; 21 | 22 | import java.util.Comparator; 23 | import java.util.Iterator; 24 | import java.util.List; 25 | import java.util.NoSuchElementException; 26 | import java.util.PriorityQueue; 27 | 28 | /** 29 | */ 30 | public class MergeIterator implements Iterator 31 | { 32 | private final PriorityQueue> pQueue; 33 | 34 | public MergeIterator( 35 | final Comparator comparator, 36 | List> iterators 37 | ) 38 | { 39 | pQueue = new PriorityQueue<>( 40 | 16, 41 | new Comparator>() 42 | { 43 | @Override 44 | public int compare(PeekingIterator lhs, PeekingIterator rhs) 45 | { 46 | return comparator.compare(lhs.peek(), rhs.peek()); 47 | } 48 | } 49 | ); 50 | 51 | for (Iterator iterator : iterators) { 52 | final PeekingIterator iter = Iterators.peekingIterator(iterator); 53 | 54 | if (iter != null && iter.hasNext()) { 55 | pQueue.add(iter); 56 | } 57 | } 58 | 59 | } 60 | 61 | @Override 62 | public boolean hasNext() 63 | { 64 | return ! pQueue.isEmpty(); 65 | } 66 | 67 | @Override 68 | public T next() 69 | { 70 | if (! hasNext()) { 71 | throw new NoSuchElementException(); 72 | } 73 | 74 | PeekingIterator retIt = pQueue.remove(); 75 | T retVal = retIt.next(); 76 | 77 | if (retIt.hasNext()) { 78 | pQueue.add(retIt); 79 | } 80 | 81 | return retVal; 82 | } 83 | 84 | @Override 85 | public void remove() 86 | { 87 | throw new UnsupportedOperationException(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/ResourceClosingSequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 - 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import java.io.Closeable; 20 | 21 | /** 22 | */ 23 | public class ResourceClosingSequence implements Sequence 24 | { 25 | private final Sequence baseSequence; 26 | private final Closeable closeable; 27 | 28 | public ResourceClosingSequence(Sequence baseSequence, Closeable closeable) 29 | { 30 | this.baseSequence = baseSequence; 31 | this.closeable = closeable; 32 | } 33 | 34 | @Override 35 | public OutType accumulate(OutType initValue, Accumulator accumulator) 36 | { 37 | try { 38 | return baseSequence.accumulate(initValue, accumulator); 39 | } 40 | finally { 41 | CloseQuietly.close(closeable); 42 | } 43 | } 44 | 45 | @Override 46 | public Yielder toYielder( 47 | OutType initValue, YieldingAccumulator accumulator 48 | ) 49 | { 50 | final Yielder baseYielder; 51 | try { 52 | baseYielder = baseSequence.toYielder(initValue, accumulator); 53 | } 54 | catch (RuntimeException e) { 55 | CloseQuietly.close(closeable); 56 | throw e; 57 | } 58 | 59 | return new ResourceClosingYielder<>(baseYielder, closeable); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/ResourceClosingYielder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 - 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import java.io.Closeable; 20 | import java.io.IOException; 21 | 22 | /** 23 | */ 24 | public class ResourceClosingYielder implements Yielder 25 | { 26 | private final Yielder baseYielder; 27 | private final Closeable closeable; 28 | 29 | public ResourceClosingYielder(Yielder baseYielder, Closeable closeable) 30 | { 31 | this.baseYielder = baseYielder; 32 | this.closeable = closeable; 33 | } 34 | 35 | @Override 36 | public OutType get() 37 | { 38 | return baseYielder.get(); 39 | } 40 | 41 | @Override 42 | public Yielder next(OutType initValue) 43 | { 44 | return new ResourceClosingYielder<>(baseYielder.next(initValue), closeable); 45 | } 46 | 47 | @Override 48 | public boolean isDone() 49 | { 50 | return baseYielder.isDone(); 51 | } 52 | 53 | @Override 54 | public void close() throws IOException 55 | { 56 | if (closeable != null) { 57 | closeable.close(); 58 | } 59 | baseYielder.close(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/Sequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | /** 20 | * A Sequence represents an iterable sequence of elements. Unlike normal Iterators however, it doesn't expose 21 | * a way for you to extract values from it, instead you provide it with a worker (an Accumulator) and that defines 22 | * what happens with the data. 23 | * 24 | * This inversion of control is in place to allow the Sequence to do resource management. It can enforce that close() 25 | * methods get called and other resources get cleaned up whenever processing is complete. Without this inversion 26 | * it is very easy to unintentionally leak resources when iterating over something that is backed by a resource. 27 | * 28 | * Sequences also expose {#see com.metamx.common.guava.Yielder} Yielder objects which allow you to implement a 29 | * continuation over the Sequence. Yielder do not offer the same guarantees of automagic resource management 30 | * as the accumulate method, but they are Closeable and will do the proper cleanup when close() is called on them. 31 | */ 32 | public interface Sequence 33 | { 34 | public OutType accumulate(OutType initValue, Accumulator accumulator); 35 | public Yielder toYielder(OutType initValue, YieldingAccumulator accumulator); 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/SimpleSequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import java.util.Iterator; 20 | 21 | /** 22 | */ 23 | public class SimpleSequence extends BaseSequence> 24 | { 25 | public static Sequence create(Iterable iterable) 26 | { 27 | return new SimpleSequence<>(iterable); 28 | } 29 | 30 | public SimpleSequence( 31 | final Iterable iterable 32 | ) 33 | { 34 | super( 35 | new IteratorMaker>() 36 | { 37 | @Override 38 | public Iterator make() 39 | { 40 | return iterable.iterator(); 41 | } 42 | 43 | @Override 44 | public void cleanup(Iterator iterFromMake) 45 | { 46 | 47 | } 48 | } 49 | ); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/YieldSign.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | /** 20 | */ 21 | public interface YieldSign 22 | { 23 | public T yield(T toYield); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/Yielders.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.io.Closeables; 20 | 21 | import java.io.Closeable; 22 | import java.io.IOException; 23 | 24 | /** 25 | */ 26 | public class Yielders 27 | { 28 | public static Yielder done(final T finalVal, final Closeable closeable) 29 | { 30 | return new Yielder() 31 | { 32 | @Override 33 | public T get() 34 | { 35 | return finalVal; 36 | } 37 | 38 | @Override 39 | public Yielder next(T initValue) 40 | { 41 | return null; 42 | } 43 | 44 | @Override 45 | public boolean isDone() 46 | { 47 | return true; 48 | } 49 | 50 | @Override 51 | public void close() throws IOException 52 | { 53 | Closeables.close(closeable, false); 54 | } 55 | }; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/YieldingAccumulator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | /** 20 | * A YieldingAccumulator is used along with a Yielder in order to replicate continuations in Java. I'm still not sure 21 | * this is such a great idea, but it's there. We shall see. 22 | * 23 | * The accumulated has its accumulate() method called and has the option of "yielding" its response by calling 24 | * yield() before returning as response. If it chooses not to yield its response, then it expects to get called 25 | * again with the next value and the value it just returned. 26 | */ 27 | public abstract class YieldingAccumulator 28 | { 29 | private boolean yielded = false; 30 | 31 | public void yield() 32 | { 33 | yielded = true; 34 | } 35 | 36 | public boolean yielded() 37 | { 38 | return yielded; 39 | } 40 | 41 | public void reset() 42 | { 43 | yielded = false; 44 | } 45 | 46 | public abstract AccumulatedType accumulate(AccumulatedType accumulated, InType in); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/YieldingAccumulators.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | /** 20 | */ 21 | public class YieldingAccumulators 22 | { 23 | public static YieldingAccumulator fromAccumulator( 24 | final Accumulator accumulator 25 | ) 26 | { 27 | return new YieldingAccumulator() 28 | { 29 | @Override 30 | public AccumulatedType accumulate(AccumulatedType accumulated, InType in) 31 | { 32 | return accumulator.accumulate(accumulated, in); 33 | } 34 | }; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/YieldingSequenceBase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 - 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.base.Throwables; 20 | import com.google.common.io.Closeables; 21 | 22 | import java.io.IOException; 23 | 24 | /** 25 | * A Sequence that is based entirely on the Yielder implementation. 26 | *

27 | * This is a base class to simplify the creation of Sequences. 28 | */ 29 | public abstract class YieldingSequenceBase implements Sequence 30 | { 31 | @Override 32 | public OutType accumulate(OutType initValue, Accumulator accumulator) 33 | { 34 | Yielder yielder = toYielder(initValue, YieldingAccumulators.fromAccumulator(accumulator)); 35 | 36 | try { 37 | return yielder.get(); 38 | } 39 | finally { 40 | CloseQuietly.close(yielder); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/nary/BinaryFn.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava.nary; 18 | 19 | /** 20 | */ 21 | public interface BinaryFn 22 | { 23 | public OutType apply(Type1 arg1, Type2 arg2); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/nary/BinaryTransformIterable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava.nary; 18 | 19 | import java.util.Iterator; 20 | 21 | /** 22 | */ 23 | public class BinaryTransformIterable implements Iterable 24 | { 25 | public static BinaryTransformIterable create( 26 | Iterable lhs, 27 | Iterable rhs, 28 | BinaryFn fn 29 | ) 30 | { 31 | return new BinaryTransformIterable<>(lhs, rhs, fn); 32 | } 33 | 34 | private final Iterable lhs; 35 | private final Iterable rhs; 36 | private final BinaryFn binaryFn; 37 | 38 | public BinaryTransformIterable( 39 | Iterable lhs, 40 | Iterable rhs, 41 | BinaryFn binaryFn 42 | ) 43 | { 44 | this.lhs = lhs; 45 | this.rhs = rhs; 46 | this.binaryFn = binaryFn; 47 | } 48 | 49 | @Override 50 | public Iterator iterator() 51 | { 52 | return BinaryTransformIterator.create(lhs.iterator(), rhs.iterator(), binaryFn); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/nary/BinaryTransformIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava.nary; 18 | 19 | import java.util.Iterator; 20 | import java.util.NoSuchElementException; 21 | 22 | /** 23 | */ 24 | public class BinaryTransformIterator implements Iterator 25 | { 26 | public static BinaryTransformIterator create( 27 | Iterator lhs, 28 | Iterator rhs, 29 | BinaryFn fn 30 | ) 31 | { 32 | return new BinaryTransformIterator<>(lhs, rhs, fn); 33 | } 34 | 35 | private final Iterator lhsIter; 36 | private final Iterator rhsIter; 37 | private final BinaryFn binaryFn; 38 | 39 | public BinaryTransformIterator(Iterator lhsIter, Iterator rhsIter, BinaryFn binaryFn) 40 | { 41 | this.lhsIter = lhsIter; 42 | this.rhsIter = rhsIter; 43 | this.binaryFn = binaryFn; 44 | } 45 | 46 | @Override 47 | public boolean hasNext() 48 | { 49 | return lhsIter.hasNext() || rhsIter.hasNext(); 50 | } 51 | 52 | @Override 53 | public RetType next() 54 | { 55 | if (!hasNext()) { 56 | throw new NoSuchElementException(); 57 | } 58 | 59 | return binaryFn.apply( 60 | lhsIter.hasNext() ? lhsIter.next() : null, 61 | rhsIter.hasNext() ? rhsIter.next() : null 62 | ); 63 | } 64 | 65 | @Override 66 | public void remove() 67 | { 68 | throw new UnsupportedOperationException(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/nary/SortedMergeIterable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava.nary; 18 | 19 | import java.util.Comparator; 20 | import java.util.Iterator; 21 | 22 | /** 23 | */ 24 | public class SortedMergeIterable implements Iterable 25 | { 26 | public static SortedMergeIterable create( 27 | Iterable lhs, 28 | Iterable rhs, 29 | Comparator comparator, 30 | BinaryFn fn 31 | ) 32 | { 33 | return new SortedMergeIterable<>(lhs, rhs, comparator, fn); 34 | } 35 | 36 | private final Iterable lhs; 37 | private final Iterable rhs; 38 | private final Comparator comparator; 39 | private final BinaryFn fn; 40 | 41 | public SortedMergeIterable( 42 | Iterable lhs, 43 | Iterable rhs, 44 | Comparator comparator, 45 | BinaryFn fn 46 | ) 47 | { 48 | this.lhs = lhs; 49 | this.rhs = rhs; 50 | this.comparator = comparator; 51 | this.fn = fn; 52 | } 53 | 54 | @Override 55 | public Iterator iterator() 56 | { 57 | return SortedMergeIterator.create(lhs.iterator(), rhs.iterator(), comparator, fn); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/nary/TrinaryFn.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava.nary; 18 | 19 | /** 20 | */ 21 | public interface TrinaryFn 22 | { 23 | public OutType apply(Type1 arg1, Type2 arg2, Type3 arg3); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/guava/nary/TrinaryTransformIterable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava.nary; 18 | 19 | import java.util.Iterator; 20 | 21 | /** 22 | */ 23 | public class TrinaryTransformIterable implements Iterable 24 | { 25 | public static TrinaryTransformIterable create( 26 | Iterable iterable1, 27 | Iterable iterable2, 28 | Iterable iterable3, 29 | TrinaryFn fn 30 | ) 31 | { 32 | return new TrinaryTransformIterable<>(iterable1, iterable2, iterable3, fn); 33 | } 34 | 35 | private final Iterable iterable1; 36 | private final Iterable iterable2; 37 | private final Iterable iterable3; 38 | private final TrinaryFn trinaryFn; 39 | 40 | public TrinaryTransformIterable( 41 | Iterable iterable1, 42 | Iterable iterable2, 43 | Iterable iterable3, 44 | TrinaryFn trinaryFn 45 | ) 46 | { 47 | this.iterable1 = iterable1; 48 | this.iterable2 = iterable2; 49 | this.iterable3 = iterable3; 50 | this.trinaryFn = trinaryFn; 51 | } 52 | 53 | @Override 54 | public Iterator iterator() 55 | { 56 | return TrinaryTransformIterator.create(iterable1.iterator(), iterable2.iterator(), iterable3.iterator(), trinaryFn); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/io/smoosh/Metadata.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.io.smoosh; 18 | 19 | /** 20 | */ 21 | class Metadata 22 | { 23 | private final int fileNum; 24 | private final int startOffset; 25 | private final int endOffset; 26 | 27 | Metadata( 28 | int fileNum, 29 | int startOffset, 30 | int endOffset 31 | ) 32 | { 33 | this.fileNum = fileNum; 34 | this.startOffset = startOffset; 35 | this.endOffset = endOffset; 36 | } 37 | 38 | public int getFileNum() 39 | { 40 | return fileNum; 41 | } 42 | 43 | public int getStartOffset() 44 | { 45 | return startOffset; 46 | } 47 | 48 | public int getEndOffset() 49 | { 50 | return endOffset; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/io/smoosh/SmooshedWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 - 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.io.smoosh; 18 | 19 | import java.io.Closeable; 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.nio.channels.GatheringByteChannel; 23 | 24 | /** 25 | */ 26 | public interface SmooshedWriter extends Closeable, GatheringByteChannel 27 | { 28 | public int write(InputStream in) throws IOException; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/lifecycle/LifecycleStart.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.lifecycle; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Target(ElementType.METHOD) 26 | public @interface LifecycleStart 27 | { 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/lifecycle/LifecycleStop.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.lifecycle; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Target(ElementType.METHOD) 26 | public @interface LifecycleStop 27 | { 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/parsers/CloseableIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.parsers; 18 | 19 | import java.io.Closeable; 20 | import java.util.Iterator; 21 | 22 | /** 23 | */ 24 | public interface CloseableIterator extends Iterator, Closeable 25 | { 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/parsers/ParseException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.parsers; 18 | 19 | /** 20 | */ 21 | public class ParseException extends RuntimeException 22 | { 23 | public ParseException(String formatText, Object... arguments) 24 | { 25 | super(String.format(formatText, arguments)); 26 | } 27 | 28 | public ParseException(Throwable cause, String formatText, Object... arguments) 29 | { 30 | super(String.format(formatText, arguments), cause); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/parsers/Parser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.parsers; 18 | 19 | import java.util.List; 20 | import java.util.Map; 21 | import java.util.Set; 22 | 23 | /** 24 | * Class that can parse Strings into Maps. 25 | */ 26 | public interface Parser 27 | { 28 | /** 29 | * Parse a String into a Map. 30 | * 31 | * @throws ParseException if the String cannot be parsed 32 | */ 33 | public Map parse(String input); 34 | 35 | /** 36 | * Set the fieldNames that you expect to see in parsed Maps. Deprecated; Parsers should not, in general, be 37 | * expected to know what fields they will return. Some individual types of parsers do need to know (like a TSV 38 | * parser) and those parsers have their own way of setting field names. 39 | */ 40 | @Deprecated 41 | public void setFieldNames(Iterable fieldNames); 42 | 43 | /** 44 | * Returns the fieldNames that we expect to see in parsed Maps, if known, or null otherwise. Deprecated; Parsers 45 | * should not, in general, be expected to know what fields they will return. 46 | */ 47 | @Deprecated 48 | public List getFieldNames(); 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/parsers/Parsers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.parsers; 18 | 19 | 20 | import com.google.common.base.Function; 21 | 22 | import java.util.Map; 23 | 24 | public class Parsers 25 | { 26 | public static final String DEFAULT_LIST_DELIMITER = "\u0001"; 27 | 28 | public static Function> toFunction(final Parser p) 29 | { 30 | /** 31 | * Creates a Function object wrapping the given parser. 32 | * Parser inputs that throw an FormattedException are mapped to null. 33 | */ 34 | return new Function>() 35 | { 36 | @Override 37 | public Map apply(String input) 38 | { 39 | try { 40 | return p.parse(input); 41 | } 42 | catch (Exception e) { 43 | return null; 44 | } 45 | } 46 | }; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/common/parsers/ToLowerCaseParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.parsers; 18 | 19 | import com.google.common.collect.Maps; 20 | import com.metamx.common.IAE; 21 | 22 | import java.util.List; 23 | import java.util.Map; 24 | 25 | /** 26 | */ 27 | public class ToLowerCaseParser implements Parser 28 | { 29 | private final Parser baseParser; 30 | 31 | public ToLowerCaseParser(Parser baseParser) 32 | { 33 | this.baseParser = baseParser; 34 | } 35 | 36 | @Override 37 | public Map parse(String input) 38 | { 39 | Map line = baseParser.parse(input); 40 | Map retVal = Maps.newLinkedHashMap(); 41 | for (Map.Entry entry : line.entrySet()) { 42 | String k = entry.getKey().toLowerCase(); 43 | 44 | if(retVal.containsKey(k)) { 45 | // Duplicate key, case-insensitively 46 | throw new ParseException("Unparseable row. Duplicate key found : [%s]", k); 47 | } 48 | 49 | retVal.put(k, entry.getValue()); 50 | } 51 | return retVal; 52 | } 53 | 54 | @Override 55 | public void setFieldNames(Iterable fieldNames) 56 | { 57 | baseParser.setFieldNames(fieldNames); 58 | } 59 | 60 | @Override 61 | public List getFieldNames() 62 | { 63 | return baseParser.getFieldNames(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/ComposingEmitterConfig.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | import com.google.common.collect.ImmutableMap; 5 | 6 | import java.util.Map; 7 | 8 | public class ComposingEmitterConfig 9 | { 10 | @JsonProperty("composing") 11 | private Map> emitterConfigs = ImmutableMap.of(); 12 | 13 | public Map> getEmitterConfigs() 14 | { 15 | return emitterConfigs; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/ContentEncoding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.emitter.core; 18 | 19 | public enum ContentEncoding 20 | { 21 | GZIP 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/EmittedBatchCounter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.emitter.core; 18 | 19 | import java.util.concurrent.Phaser; 20 | import java.util.concurrent.TimeUnit; 21 | import java.util.concurrent.TimeoutException; 22 | 23 | 24 | final class EmittedBatchCounter 25 | { 26 | /** 27 | * Max {@link Phaser}'s phase, specified in it's javadoc. Then it wraps to zero. 28 | */ 29 | private static final int MAX_PHASE = Integer.MAX_VALUE; 30 | 31 | static int nextBatchNumber(int prevBatchNumber) 32 | { 33 | return (prevBatchNumber + 1) & MAX_PHASE; 34 | } 35 | 36 | private final Phaser phaser = new Phaser(1); 37 | 38 | void batchEmitted() 39 | { 40 | phaser.arrive(); 41 | } 42 | 43 | void awaitBatchEmitted(int batchNumberToAwait, long timeout, TimeUnit unit) 44 | throws InterruptedException, TimeoutException 45 | { 46 | batchNumberToAwait &= MAX_PHASE; 47 | int currentBatch = phaser.getPhase(); 48 | checkNotTerminated(currentBatch); 49 | while (comparePhases(batchNumberToAwait, currentBatch) >= 0) { 50 | currentBatch = phaser.awaitAdvanceInterruptibly(currentBatch, timeout, unit); 51 | checkNotTerminated(currentBatch); 52 | } 53 | } 54 | 55 | private static int comparePhases(int phase1, int phase2) 56 | { 57 | int diff = (phase1 - phase2) & MAX_PHASE; 58 | if (diff == 0) { 59 | return 0; 60 | } 61 | return diff < MAX_PHASE / 2 ? 1 : -1; 62 | } 63 | 64 | private void checkNotTerminated(int phase) 65 | { 66 | if (phase < 0) { 67 | throw new IllegalStateException("Phaser[" + phaser + "] unexpectedly terminated."); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/Emitter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.emitter.core; 18 | 19 | import java.io.Closeable; 20 | import java.io.Flushable; 21 | import java.io.IOException; 22 | 23 | /** 24 | */ 25 | public interface Emitter extends Closeable, Flushable 26 | { 27 | void start(); 28 | void emit(Event event); 29 | void flush() throws IOException; 30 | void close() throws IOException; 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/Event.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.emitter.core; 18 | 19 | import org.joda.time.DateTime; 20 | 21 | import java.util.Map; 22 | 23 | /** 24 | */ 25 | public interface Event 26 | { 27 | public Map toMap(); 28 | public String getFeed(); 29 | public DateTime getCreatedTime(); 30 | public boolean isSafeToBuffer(); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/FeedUriExtractor.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core; 2 | 3 | import java.net.URI; 4 | import java.net.URISyntaxException; 5 | 6 | public class FeedUriExtractor implements UriExtractor 7 | { 8 | private String uriPattern; 9 | 10 | public FeedUriExtractor(String uriPattern) 11 | { 12 | this.uriPattern = uriPattern; 13 | } 14 | 15 | @Override 16 | public URI apply(Event event) throws URISyntaxException 17 | { 18 | return new URI(String.format(uriPattern, event.getFeed())); 19 | } 20 | 21 | @Override 22 | public String toString() 23 | { 24 | return "FeedUriExtractor{" + 25 | "uriPattern='" + uriPattern + '\'' + 26 | '}'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/JacksonUtil.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core; 2 | 3 | import com.fasterxml.jackson.core.JsonParser; 4 | import com.fasterxml.jackson.databind.DeserializationContext; 5 | import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer; 6 | 7 | import java.io.IOException; 8 | import java.util.Arrays; 9 | import java.util.List; 10 | import java.util.stream.Collectors; 11 | 12 | public class JacksonUtil 13 | { 14 | public static class CommaDelimitedListDeserializer extends StdScalarDeserializer> 15 | { 16 | protected CommaDelimitedListDeserializer() 17 | { 18 | super(List.class); 19 | } 20 | 21 | @Override 22 | public List deserialize( 23 | JsonParser jsonParser, 24 | DeserializationContext deserializationContext 25 | ) throws IOException 26 | { 27 | final String text = jsonParser.getText().trim(); 28 | return Arrays.stream(text.split(",")).map(String::trim).collect(Collectors.toList()); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/LoggingEmitterConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.emitter.core; 18 | 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | 21 | import javax.validation.constraints.NotNull; 22 | 23 | /** 24 | */ 25 | public class LoggingEmitterConfig 26 | { 27 | @NotNull 28 | @JsonProperty 29 | private String loggerClass = LoggingEmitter.class.getName(); 30 | 31 | @NotNull 32 | @JsonProperty 33 | private String logLevel = "info"; 34 | 35 | public String getLoggerClass() 36 | { 37 | return loggerClass; 38 | } 39 | 40 | public void setLoggerClass(String loggerClass) 41 | { 42 | this.loggerClass = loggerClass; 43 | } 44 | 45 | public String getLogLevel() 46 | { 47 | return logLevel; 48 | } 49 | 50 | public void setLogLevel(String logLevel) 51 | { 52 | this.logLevel = logLevel; 53 | } 54 | 55 | @Override 56 | public String toString() 57 | { 58 | return "LoggingEmitterConfig{" + 59 | "loggerClass='" + loggerClass + '\'' + 60 | ", logLevel='" + logLevel + '\'' + 61 | '}'; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/NoopEmitter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.emitter.core; 18 | 19 | import java.io.IOException; 20 | 21 | /** 22 | */ 23 | public class NoopEmitter implements Emitter 24 | { 25 | @Override 26 | public void start() 27 | { 28 | // Do nothing 29 | } 30 | 31 | @Override 32 | public void emit(Event event) 33 | { 34 | // Do nothing 35 | } 36 | 37 | @Override 38 | public void flush() throws IOException 39 | { 40 | // Do nothing 41 | } 42 | 43 | @Override 44 | public void close() throws IOException 45 | { 46 | // Do nothing 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/ParametrizedUriEmitterConfig.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | import com.metamx.emitter.core.filter.BWListEventFilterConfig; 5 | 6 | import javax.validation.constraints.NotNull; 7 | 8 | public class ParametrizedUriEmitterConfig 9 | { 10 | private static final BaseHttpEmittingConfig DEFAULT_HTTP_EMITTING_CONFIG = new BaseHttpEmittingConfig(); 11 | 12 | private static final BWListEventFilterConfig DEFAULT_BW_LIST_EVENT_FILTER_CONFIG = new BWListEventFilterConfig(); 13 | 14 | @NotNull 15 | @JsonProperty 16 | private String recipientBaseUrlPattern; 17 | 18 | @JsonProperty("httpEmitting") 19 | private BaseHttpEmittingConfig httpEmittingConfig = DEFAULT_HTTP_EMITTING_CONFIG; 20 | 21 | @JsonProperty("bwFilter") 22 | private BWListEventFilterConfig bwListEventFilterConfig = DEFAULT_BW_LIST_EVENT_FILTER_CONFIG; 23 | 24 | public String getRecipientBaseUrlPattern() 25 | { 26 | return recipientBaseUrlPattern; 27 | } 28 | 29 | public BWListEventFilterConfig getBwListEventFilterConfig() 30 | { 31 | return bwListEventFilterConfig; 32 | } 33 | 34 | public HttpEmitterConfig buildHttpEmitterConfig(String baseUri) 35 | { 36 | return new HttpEmitterConfig(httpEmittingConfig, baseUri); 37 | } 38 | 39 | @Override 40 | public String toString() 41 | { 42 | return "ParametrizedUriEmitterConfig{" + 43 | "recipientBaseUrlPattern='" + recipientBaseUrlPattern + '\'' + 44 | ", httpEmittingConfig=" + httpEmittingConfig + '\'' + 45 | ", bwListEventFilterConfig='" + bwListEventFilterConfig + 46 | '}'; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/ParametrizedUriExtractor.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core; 2 | 3 | import java.net.URI; 4 | import java.net.URISyntaxException; 5 | import java.util.HashSet; 6 | import java.util.Map; 7 | import java.util.Set; 8 | import java.util.regex.Matcher; 9 | import java.util.regex.Pattern; 10 | 11 | public class ParametrizedUriExtractor implements UriExtractor 12 | { 13 | private String uriPattern; 14 | private Set params; 15 | 16 | public ParametrizedUriExtractor(String uriPattern) 17 | { 18 | this.uriPattern = uriPattern; 19 | Matcher keyMatcher = Pattern.compile("\\{([^\\}]+)\\}").matcher(uriPattern); 20 | params = new HashSet<>(); 21 | while (keyMatcher.find()) { 22 | params.add(keyMatcher.group(1)); 23 | } 24 | } 25 | 26 | public Set getParams() 27 | { 28 | return params; 29 | } 30 | 31 | @Override 32 | public URI apply(Event event) throws URISyntaxException 33 | { 34 | Map eventMap = event.toMap(); 35 | String processedUri = uriPattern; 36 | for (String key : params) { 37 | Object paramValue = eventMap.get(key); 38 | if (paramValue == null) { 39 | throw new IllegalArgumentException(String.format( 40 | "ParametrizedUriExtractor with pattern %s requires %s to be set in event, but found %s", 41 | uriPattern, 42 | key, 43 | eventMap 44 | )); 45 | } 46 | processedUri = processedUri.replace(String.format("{%s}", key), paramValue.toString()); 47 | } 48 | return new URI(processedUri); 49 | } 50 | 51 | @Override 52 | public String toString() 53 | { 54 | return "ParametrizedUriExtractor{" + 55 | "uriPattern='" + uriPattern + '\'' + 56 | ", params=" + params + 57 | '}'; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/UriExtractor.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core; 2 | 3 | import java.net.URI; 4 | import java.net.URISyntaxException; 5 | 6 | public interface UriExtractor 7 | { 8 | URI apply(Event event) throws URISyntaxException; 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/ZeroCopyByteArrayOutputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.emitter.core; 18 | 19 | import java.io.ByteArrayOutputStream; 20 | 21 | class ZeroCopyByteArrayOutputStream extends ByteArrayOutputStream 22 | { 23 | 24 | ZeroCopyByteArrayOutputStream(int capacity) 25 | { 26 | super(capacity); 27 | } 28 | 29 | byte[] getBuffer() 30 | { 31 | return buf; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/factory/ComposingEmitterFactory.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.factory; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.metamx.common.lifecycle.Lifecycle; 5 | import com.metamx.emitter.core.ComposingEmitter; 6 | import com.metamx.emitter.core.ComposingEmitterConfig; 7 | import com.metamx.emitter.core.Emitter; 8 | import org.asynchttpclient.AsyncHttpClient; 9 | 10 | import java.util.Map; 11 | import java.util.stream.Collectors; 12 | 13 | public class ComposingEmitterFactory extends ComposingEmitterConfig implements EmitterFactory 14 | { 15 | @Override 16 | public Emitter makeEmitter( 17 | ObjectMapper objectMapper, 18 | AsyncHttpClient httpClient, 19 | Lifecycle lifecycle 20 | ) 21 | { 22 | final Map emitters = getEmitterConfigs() 23 | .entrySet() 24 | .stream() 25 | .collect(Collectors.toMap( 26 | Map.Entry::getKey, 27 | e -> objectMapper.convertValue(e.getValue(), EmitterFactory.class) 28 | .makeEmitter(objectMapper, httpClient, lifecycle) 29 | )); 30 | return new ComposingEmitter(emitters); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/factory/EmitterFactory.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.factory; 2 | 3 | import com.fasterxml.jackson.annotation.JsonSubTypes; 4 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import com.metamx.common.lifecycle.Lifecycle; 7 | import com.metamx.emitter.core.Emitter; 8 | import com.metamx.emitter.core.statsd.StatsDEmitterFactory; 9 | import org.asynchttpclient.AsyncHttpClient; 10 | 11 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") 12 | @JsonSubTypes(value = { 13 | @JsonSubTypes.Type(name = "http", value = HttpEmitterFactory.class), 14 | @JsonSubTypes.Type(name = "statsd", value = StatsDEmitterFactory.class), 15 | @JsonSubTypes.Type(name = "logging", value = LoggingEmitterFactory.class), 16 | @JsonSubTypes.Type(name = "parametrized", value = ParametrizedUriEmitterFactory.class), 17 | @JsonSubTypes.Type(name = "composing", value = ComposingEmitterFactory.class), 18 | @JsonSubTypes.Type(name = "noop", value = NoopEmiterFactory.class) 19 | }) 20 | public interface EmitterFactory 21 | { 22 | Emitter makeEmitter(ObjectMapper objectMapper, AsyncHttpClient httpClient, Lifecycle lifecycle); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/factory/HttpEmitterFactory.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.factory; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.metamx.common.lifecycle.Lifecycle; 5 | import com.metamx.emitter.core.Emitter; 6 | import com.metamx.emitter.core.HttpEmitterConfig; 7 | import com.metamx.emitter.core.HttpPostEmitter; 8 | import org.asynchttpclient.AsyncHttpClient; 9 | 10 | public class HttpEmitterFactory extends HttpEmitterConfig implements EmitterFactory 11 | { 12 | 13 | @Override 14 | public Emitter makeEmitter(ObjectMapper objectMapper, AsyncHttpClient httpClient, Lifecycle lifecycle) 15 | { 16 | Emitter retVal = new HttpPostEmitter(this, httpClient, objectMapper); 17 | lifecycle.addManagedInstance(retVal); 18 | return retVal; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/factory/LoggingEmitterFactory.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.factory; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.metamx.common.lifecycle.Lifecycle; 5 | import com.metamx.emitter.core.Emitter; 6 | import com.metamx.emitter.core.LoggingEmitter; 7 | import com.metamx.emitter.core.LoggingEmitterConfig; 8 | import org.asynchttpclient.AsyncHttpClient; 9 | 10 | public class LoggingEmitterFactory extends LoggingEmitterConfig implements EmitterFactory 11 | { 12 | public LoggingEmitterFactory() {} 13 | 14 | @Override 15 | public Emitter makeEmitter(ObjectMapper objectMapper, AsyncHttpClient httpClient, Lifecycle lifecycle) 16 | { 17 | return makeEmitter(objectMapper, lifecycle); 18 | } 19 | 20 | public Emitter makeEmitter(ObjectMapper objectMapper, Lifecycle lifecycle) 21 | { 22 | Emitter retVal = new LoggingEmitter(this, objectMapper); 23 | lifecycle.addManagedInstance(retVal); 24 | return retVal; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/factory/NoopEmiterFactory.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.factory; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.metamx.common.lifecycle.Lifecycle; 5 | import com.metamx.emitter.core.Emitter; 6 | import com.metamx.emitter.core.NoopEmitter; 7 | import org.asynchttpclient.AsyncHttpClient; 8 | 9 | public class NoopEmiterFactory implements EmitterFactory 10 | { 11 | @Override 12 | public Emitter makeEmitter(ObjectMapper objectMapper, AsyncHttpClient httpClient, Lifecycle lifecycle) 13 | { 14 | return makeEmitter(lifecycle); 15 | } 16 | 17 | public Emitter makeEmitter(Lifecycle lifecycle) 18 | { 19 | Emitter retVal = new NoopEmitter(); 20 | lifecycle.addManagedInstance(retVal); 21 | return retVal; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/factory/ParametrizedUriEmitterFactory.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.factory; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.metamx.common.lifecycle.Lifecycle; 5 | import com.metamx.emitter.core.Emitter; 6 | import com.metamx.emitter.core.ParametrizedUriEmitter; 7 | import com.metamx.emitter.core.ParametrizedUriEmitterConfig; 8 | import org.asynchttpclient.AsyncHttpClient; 9 | 10 | public class ParametrizedUriEmitterFactory extends ParametrizedUriEmitterConfig implements EmitterFactory 11 | { 12 | 13 | @Override 14 | public Emitter makeEmitter(ObjectMapper objectMapper, AsyncHttpClient httpClient, Lifecycle lifecycle) 15 | { 16 | final Emitter retVal = new ParametrizedUriEmitter(this, httpClient, objectMapper); 17 | lifecycle.addManagedInstance(retVal); 18 | return retVal; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/filter/BWListEventFilter.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.filter; 2 | 3 | import com.metamx.emitter.core.Event; 4 | 5 | /** 6 | * Filters out events based on white and black lists of event field values. 7 | */ 8 | public interface BWListEventFilter 9 | { 10 | /** 11 | * Checks if an event is not whitelisted. 12 | * There are three possible cases: whitelisted, not whitelisted, the white list is not defined. 13 | * "Is not whitelisted" is opposite to the other two options so it's used as a predicate to decide 14 | * if the event should be filtered out. 15 | */ 16 | boolean isNotWhiteListed(Event event); 17 | 18 | /** 19 | * Checks if an event is blacklisted. 20 | * There are three possible cases: blacklisted, not blacklisted, the black list is not defined. 21 | * "Is blacklisted" is opposite to the other two options so it's used as a predicate to decide 22 | * if the event should be filtered out. 23 | */ 24 | boolean isBlackListed(Event event); 25 | 26 | /** 27 | * Checks if an event is not whitelisted or is blacklisted. 28 | */ 29 | default boolean isNotListed(Event event) 30 | { 31 | return isNotWhiteListed(event) || isBlackListed(event); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/filter/BWListEventFilterConfig.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.filter; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 5 | import com.google.common.collect.ImmutableMap; 6 | import com.metamx.emitter.core.JacksonUtil; 7 | 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | /** 12 | * A config for {@link BWListEventFilter}. 13 | */ 14 | public class BWListEventFilterConfig 15 | { 16 | @JsonProperty 17 | @JsonDeserialize(contentUsing = JacksonUtil.CommaDelimitedListDeserializer.class) 18 | private Map> whiteList = ImmutableMap.of(); 19 | 20 | @JsonProperty 21 | @JsonDeserialize(contentUsing = JacksonUtil.CommaDelimitedListDeserializer.class) 22 | private Map> blackList = ImmutableMap.of(); 23 | 24 | public Map> getWhiteList() 25 | { 26 | return whiteList; 27 | } 28 | 29 | public Map> getBlackList() 30 | { 31 | return blackList; 32 | } 33 | 34 | @Override 35 | public String toString() 36 | { 37 | return "BWListEventFilterConfig{" + 38 | "whiteList='" + whiteList + '\'' + 39 | ", blackList='" + blackList + '\'' + 40 | '}'; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/filter/BWListEventFilterFactory.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.filter; 2 | 3 | import java.util.HashSet; 4 | 5 | /** 6 | * The factory creates {@link BWListEventFilter} out of the {@link BWListEventFilterConfig}. 7 | */ 8 | public class BWListEventFilterFactory 9 | { 10 | private static final String FEED_FIELD_NAME = "feed"; 11 | 12 | public static BWListEventFilter makeEventFilter(BWListEventFilterConfig config) 13 | { 14 | final HashSet commonKeys = new HashSet<>(config.getWhiteList().keySet()); 15 | commonKeys.addAll(config.getBlackList().keySet()); 16 | if (commonKeys.size() == 0) { 17 | return new EmptyBWListEventFilter(); 18 | } 19 | if (commonKeys.size() == 1 && commonKeys.contains(FEED_FIELD_NAME)) { 20 | return new FeedBWListEventFilter( 21 | config.getWhiteList().get(FEED_FIELD_NAME), 22 | config.getBlackList().get(FEED_FIELD_NAME) 23 | ); 24 | } else { 25 | return new MultiBWListEventFilter(config.getWhiteList(), config.getBlackList()); 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/filter/EmptyBWListEventFilter.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.filter; 2 | 3 | import com.metamx.emitter.core.Event; 4 | 5 | /** 6 | * The {@link BWListEventFilter} with no white and black lists. 7 | */ 8 | public class EmptyBWListEventFilter implements BWListEventFilter 9 | { 10 | @Override 11 | public boolean isNotWhiteListed(Event event) 12 | { 13 | return false; 14 | } 15 | 16 | @Override 17 | public boolean isBlackListed(Event event) 18 | { 19 | return false; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/filter/FeedBWListEventFilter.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.filter; 2 | 3 | import com.metamx.emitter.core.Event; 4 | 5 | import javax.annotation.Nullable; 6 | import java.util.HashSet; 7 | import java.util.List; 8 | import java.util.Set; 9 | 10 | /** 11 | * The {@link BWListEventFilter} that has only BW lists for a {@link Event}'s feed field. 12 | */ 13 | public class FeedBWListEventFilter implements BWListEventFilter 14 | { 15 | private final Set whiteSet; 16 | private final Set blackSet; 17 | 18 | public FeedBWListEventFilter(@Nullable List whiteList, @Nullable List blackList) 19 | { 20 | this.whiteSet = whiteList == null ? null : new HashSet<>(whiteList); 21 | this.blackSet = blackList == null ? null : new HashSet<>(blackList); 22 | } 23 | 24 | @Override 25 | public boolean isNotWhiteListed(Event event) 26 | { 27 | return !(whiteSet == null || whiteSet.contains(event.getFeed())); 28 | } 29 | 30 | @Override 31 | public boolean isBlackListed(Event event) 32 | { 33 | return blackSet != null && blackSet.contains(event.getFeed()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/filter/MultiBWListEventFilter.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.filter; 2 | 3 | import com.metamx.emitter.core.Event; 4 | 5 | import java.util.HashSet; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Set; 9 | import java.util.stream.Collectors; 10 | 11 | /** 12 | * The {@link BWListEventFilter} that supports BW lists for any field. 13 | * Before checking an event the filter converts the event to a map and iterates over the map keys. 14 | */ 15 | public class MultiBWListEventFilter implements BWListEventFilter 16 | { 17 | private final Map> fieldToValueWhiteSet; 18 | private final Map> fieldToValueBlackSet; 19 | 20 | public MultiBWListEventFilter( 21 | Map> fieldToValueWhiteList, 22 | Map> fieldToValueBlackList 23 | ) 24 | { 25 | this.fieldToValueWhiteSet = fieldToValueWhiteList.entrySet().stream().collect(Collectors.toMap( 26 | Map.Entry::getKey, 27 | e -> new HashSet<>(e.getValue()) 28 | )); 29 | this.fieldToValueBlackSet = fieldToValueBlackList.entrySet().stream().collect(Collectors.toMap( 30 | Map.Entry::getKey, 31 | e -> new HashSet<>(e.getValue()) 32 | )); 33 | } 34 | 35 | @Override 36 | public boolean isNotWhiteListed(Event event) 37 | { 38 | final Map eventMap = event.toMap(); 39 | return fieldToValueWhiteSet.entrySet().stream().anyMatch( 40 | e -> eventMap.containsKey(e.getKey()) && !e.getValue().contains(eventMap.get(e.getKey())) 41 | ); 42 | } 43 | 44 | @Override 45 | public boolean isBlackListed(Event event) 46 | { 47 | final Map eventMap = event.toMap(); 48 | return fieldToValueBlackSet.entrySet().stream().anyMatch( 49 | e -> eventMap.containsKey(e.getKey()) && e.getValue().contains(eventMap.get(e.getKey())) 50 | ); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/statsd/StatsDEmitterFactory.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.statsd; 2 | 3 | import com.fasterxml.jackson.annotation.JsonCreator; 4 | import com.fasterxml.jackson.annotation.JsonProperty; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 7 | import com.metamx.common.lifecycle.Lifecycle; 8 | import com.metamx.emitter.core.Emitter; 9 | import com.metamx.emitter.core.JacksonUtil; 10 | import com.metamx.emitter.core.factory.EmitterFactory; 11 | import org.asynchttpclient.AsyncHttpClient; 12 | 13 | import javax.annotation.Nullable; 14 | import java.util.List; 15 | 16 | public class StatsDEmitterFactory extends StatsDEmitterConfig implements EmitterFactory 17 | { 18 | @JsonCreator 19 | public StatsDEmitterFactory( 20 | @JsonProperty("hostname") String hostname, 21 | @JsonProperty("port") Integer port, 22 | @JsonProperty("prefix") @Nullable String prefix, 23 | @JsonProperty("separator") @Nullable String separator, 24 | @JsonProperty("includeHost") @Nullable Boolean includeHost, 25 | @JsonProperty("dimensionMapPath") @Nullable String dimensionMapPath, 26 | @JsonProperty("blankHolder") @Nullable String blankHolder, 27 | @JsonProperty("dogstatsd") @Nullable Boolean dogstatsd, 28 | @JsonProperty("dogstatsdConstantTags") 29 | @JsonDeserialize(using = JacksonUtil.CommaDelimitedListDeserializer.class) 30 | @Nullable List dogstatsdConstantTags, 31 | @JsonProperty("dogstatsdServiceAsTag") @Nullable Boolean dogstatsdServiceAsTag, 32 | @JsonProperty("dogstatsdEvents") @Nullable Boolean dogstatsdEvents 33 | ) 34 | { 35 | super( 36 | hostname, 37 | port, 38 | prefix, 39 | separator, 40 | includeHost, 41 | dimensionMapPath, 42 | blankHolder, 43 | dogstatsd, 44 | dogstatsdConstantTags, 45 | dogstatsdServiceAsTag, 46 | dogstatsdEvents 47 | ); 48 | } 49 | 50 | @Override 51 | public Emitter makeEmitter(ObjectMapper objectMapper, AsyncHttpClient httpClient, Lifecycle lifecycle) 52 | { 53 | Emitter retVal = StatsDEmitter.of(this, objectMapper); 54 | lifecycle.addManagedInstance(retVal); 55 | return retVal; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/core/statsd/StatsDMetric.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | package com.metamx.emitter.core.statsd; 20 | 21 | import com.fasterxml.jackson.annotation.JsonCreator; 22 | import com.fasterxml.jackson.annotation.JsonProperty; 23 | 24 | import java.util.SortedSet; 25 | 26 | /** 27 | */ 28 | public class StatsDMetric 29 | { 30 | public final SortedSet dimensions; 31 | public final Type type; 32 | public final boolean convertRange; 33 | 34 | @JsonCreator 35 | public StatsDMetric( 36 | @JsonProperty("dimensions") SortedSet dimensions, 37 | @JsonProperty("type") Type type, 38 | @JsonProperty("convertRange") boolean convertRange 39 | ) 40 | { 41 | this.dimensions = dimensions; 42 | this.type = type; 43 | this.convertRange = convertRange; 44 | } 45 | 46 | public enum Type 47 | { 48 | count, gauge, timer 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/service/ServiceEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 - 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.emitter.service; 18 | 19 | import com.metamx.emitter.core.Event; 20 | 21 | public interface ServiceEvent extends Event 22 | { 23 | public String getService(); 24 | public String getHost(); 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/emitter/service/ServiceEventBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.emitter.service; 18 | 19 | import com.google.common.collect.ImmutableMap; 20 | import com.metamx.emitter.core.Event; 21 | 22 | public abstract class ServiceEventBuilder 23 | { 24 | public abstract X build(ImmutableMap serviceDimensions); 25 | 26 | public X build(String service, String host) 27 | { 28 | return build(ImmutableMap.of("service", service, "host", host)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/AbstractHttpClient.java: -------------------------------------------------------------------------------- 1 | package com.metamx.http.client; 2 | 3 | import com.google.common.util.concurrent.ListenableFuture; 4 | import com.metamx.http.client.response.HttpResponseHandler; 5 | 6 | public abstract class AbstractHttpClient implements HttpClient 7 | { 8 | @Override 9 | public ListenableFuture go( 10 | final Request request, 11 | final HttpResponseHandler handler 12 | ) 13 | { 14 | return go(request, handler, null); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/CredentialedHttpClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to Metamarkets Group Inc. (Metamarkets) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. Metamarkets licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package com.metamx.http.client; 21 | 22 | import com.google.common.util.concurrent.ListenableFuture; 23 | import com.metamx.http.client.auth.Credentials; 24 | import com.metamx.http.client.response.HttpResponseHandler; 25 | import org.joda.time.Duration; 26 | 27 | /** 28 | */ 29 | public class CredentialedHttpClient extends AbstractHttpClient 30 | { 31 | 32 | private final Credentials creds; 33 | private final HttpClient delegate; 34 | 35 | public CredentialedHttpClient(Credentials creds, HttpClient delegate) 36 | { 37 | this.creds = creds; 38 | this.delegate = delegate; 39 | } 40 | 41 | @Override 42 | public ListenableFuture go( 43 | Request request, 44 | HttpResponseHandler handler, 45 | Duration requestReadTimeout 46 | ) 47 | { 48 | return delegate.go(creds.addCredentials(request), handler, requestReadTimeout); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/EnforceSslHttpClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to Metamarkets Group Inc. (Metamarkets) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. Metamarkets licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package com.metamx.http.client; 21 | 22 | import com.google.common.util.concurrent.ListenableFuture; 23 | import com.metamx.http.client.response.HttpResponseHandler; 24 | import org.joda.time.Duration; 25 | 26 | import java.net.URL; 27 | 28 | /** 29 | */ 30 | public class EnforceSslHttpClient extends AbstractHttpClient 31 | { 32 | 33 | private final HttpClient delegate; 34 | 35 | public EnforceSslHttpClient( 36 | HttpClient delegate 37 | ) 38 | { 39 | this.delegate = delegate; 40 | } 41 | 42 | @Override 43 | public ListenableFuture go( 44 | Request request, 45 | HttpResponseHandler handler, 46 | Duration requestReadTimeout 47 | ) 48 | { 49 | URL url = request.getUrl(); 50 | 51 | if (!"https".equals(url.getProtocol())) { 52 | throw new IllegalArgumentException(String.format("Requests must be over https, got[%s].", url)); 53 | } 54 | 55 | return delegate.go(request, handler, requestReadTimeout); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/auth/BasicCredentials.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 - 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.auth; 18 | 19 | import com.metamx.http.client.Request; 20 | 21 | /** 22 | */ 23 | public class BasicCredentials implements Credentials 24 | { 25 | private final String username; 26 | private final String password; 27 | 28 | public BasicCredentials( 29 | String username, 30 | String password 31 | ) 32 | { 33 | this.username = username; 34 | this.password = password; 35 | } 36 | 37 | @Override 38 | public Request addCredentials(Request builder) 39 | { 40 | return builder.setBasicAuthentication(username, password); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/auth/Credentials.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 - 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.auth; 18 | 19 | import com.metamx.http.client.Request; 20 | 21 | /** 22 | */ 23 | public interface Credentials 24 | { 25 | public Request addCredentials(Request builder); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/netty/HttpClientPipelineFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.netty; 18 | 19 | import org.jboss.netty.channel.ChannelPipeline; 20 | import org.jboss.netty.channel.ChannelPipelineFactory; 21 | import org.jboss.netty.channel.DefaultChannelPipeline; 22 | import org.jboss.netty.handler.codec.http.HttpClientCodec; 23 | import org.jboss.netty.handler.codec.http.HttpContentDecompressor; 24 | 25 | /** 26 | */ 27 | public class HttpClientPipelineFactory implements ChannelPipelineFactory 28 | { 29 | @Override 30 | public ChannelPipeline getPipeline() throws Exception 31 | { 32 | ChannelPipeline pipeline = new DefaultChannelPipeline(); 33 | 34 | pipeline.addLast("codec", new HttpClientCodec()); 35 | pipeline.addLast("inflater", new HttpContentDecompressor()); 36 | 37 | return pipeline; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/pool/ResourceContainer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 - 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.pool; 18 | 19 | /** 20 | */ 21 | public interface ResourceContainer 22 | { 23 | public ResourceType get(); 24 | public void returnResource(); 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/pool/ResourceFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.pool; 18 | 19 | /** 20 | */ 21 | public interface ResourceFactory 22 | { 23 | public V generate(K key); 24 | public boolean isGood(V resource); 25 | public void close(V resource); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/pool/ResourcePoolConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.pool; 18 | 19 | /** 20 | */ 21 | public class ResourcePoolConfig 22 | { 23 | private final int maxPerKey; 24 | private final long unusedConnectionTimeoutMillis; 25 | 26 | public ResourcePoolConfig( 27 | int maxPerKey, 28 | long unusedConnectionTimeoutMillis 29 | ) 30 | { 31 | this.maxPerKey = maxPerKey; 32 | this.unusedConnectionTimeoutMillis = unusedConnectionTimeoutMillis; 33 | } 34 | 35 | @Deprecated 36 | public ResourcePoolConfig( 37 | int maxPerKey, 38 | boolean cleanIdle, 39 | long unusedConnectionTimeoutMillis 40 | ) 41 | { 42 | this(maxPerKey, unusedConnectionTimeoutMillis); 43 | 44 | if (cleanIdle) { 45 | throw new IllegalStateException( 46 | "Cleaning up idle connections is a bad idea. " 47 | + "If your services can't handle the max number then lower the max number." 48 | ); 49 | } 50 | } 51 | 52 | public int getMaxPerKey() 53 | { 54 | return maxPerKey; 55 | } 56 | 57 | public boolean isCleanIdle() 58 | { 59 | return false; 60 | } 61 | 62 | public long getUnusedConnectionTimeoutMillis() 63 | { 64 | return unusedConnectionTimeoutMillis; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/pool/ResourceVerifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.pool; 18 | 19 | /** 20 | */ 21 | public interface ResourceVerifier 22 | { 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/response/ClientResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.response; 18 | 19 | /** 20 | */ 21 | public class ClientResponse 22 | { 23 | private final boolean finished; 24 | private final T obj; 25 | 26 | public static ClientResponse finished(T obj) 27 | { 28 | return new ClientResponse(true, obj); 29 | } 30 | 31 | public static ClientResponse unfinished(T obj) 32 | { 33 | return new ClientResponse(false, obj); 34 | } 35 | 36 | protected ClientResponse( 37 | boolean finished, 38 | T obj 39 | ) 40 | { 41 | this.finished = finished; 42 | this.obj = obj; 43 | } 44 | 45 | public boolean isFinished() 46 | { 47 | return finished; 48 | } 49 | 50 | public T getObj() 51 | { 52 | return obj; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/response/FullResponseHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.response; 18 | 19 | import org.jboss.netty.handler.codec.http.HttpChunk; 20 | import org.jboss.netty.handler.codec.http.HttpResponse; 21 | 22 | import java.nio.charset.Charset; 23 | 24 | /** 25 | */ 26 | public class FullResponseHandler implements HttpResponseHandler 27 | { 28 | private final Charset charset; 29 | 30 | public FullResponseHandler(Charset charset) 31 | { 32 | this.charset = charset; 33 | } 34 | 35 | @Override 36 | public ClientResponse handleResponse(HttpResponse response) 37 | { 38 | return ClientResponse.unfinished( 39 | new FullResponseHolder( 40 | response.getStatus(), 41 | response, 42 | new StringBuilder(response.getContent().toString(charset)) 43 | ) 44 | ); 45 | } 46 | 47 | @Override 48 | public ClientResponse handleChunk( 49 | ClientResponse response, 50 | HttpChunk chunk 51 | ) 52 | { 53 | final StringBuilder builder = response.getObj().getBuilder(); 54 | 55 | if (builder == null) { 56 | return ClientResponse.finished(null); 57 | } 58 | 59 | builder.append(chunk.getContent().toString(charset)); 60 | return response; 61 | } 62 | 63 | @Override 64 | public ClientResponse done(ClientResponse response) 65 | { 66 | return ClientResponse.finished(response.getObj()); 67 | } 68 | 69 | @Override 70 | public void exceptionCaught( 71 | ClientResponse clientResponse, Throwable e 72 | ) 73 | { 74 | // Its safe to Ignore as the ClientResponse returned in handleChunk were unfinished 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/response/FullResponseHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.response; 18 | 19 | import org.jboss.netty.handler.codec.http.HttpResponse; 20 | import org.jboss.netty.handler.codec.http.HttpResponseStatus; 21 | 22 | /** 23 | */ 24 | public class FullResponseHolder 25 | { 26 | private final HttpResponseStatus status; 27 | private final HttpResponse response; 28 | private final StringBuilder builder; 29 | 30 | public FullResponseHolder( 31 | HttpResponseStatus status, 32 | HttpResponse response, 33 | StringBuilder builder 34 | ) 35 | { 36 | this.status = status; 37 | this.response = response; 38 | this.builder = builder; 39 | } 40 | 41 | public HttpResponseStatus getStatus() 42 | { 43 | return status; 44 | } 45 | 46 | public HttpResponse getResponse() 47 | { 48 | return response; 49 | } 50 | 51 | public StringBuilder getBuilder() 52 | { 53 | return builder; 54 | } 55 | 56 | public String getContent() 57 | { 58 | return builder.toString(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/response/StatusResponseHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.response; 18 | 19 | import org.jboss.netty.handler.codec.http.HttpChunk; 20 | import org.jboss.netty.handler.codec.http.HttpResponse; 21 | 22 | import java.nio.charset.Charset; 23 | 24 | /** 25 | */ 26 | public class StatusResponseHandler implements HttpResponseHandler 27 | { 28 | private final Charset charset; 29 | 30 | public StatusResponseHandler(Charset charset) 31 | { 32 | this.charset = charset; 33 | } 34 | 35 | @Override 36 | public ClientResponse handleResponse(HttpResponse response) 37 | { 38 | return ClientResponse.unfinished( 39 | new StatusResponseHolder( 40 | response.getStatus(), 41 | new StringBuilder(response.getContent().toString(charset)) 42 | ) 43 | ); 44 | } 45 | 46 | @Override 47 | public ClientResponse handleChunk( 48 | ClientResponse response, 49 | HttpChunk chunk 50 | ) 51 | { 52 | final StringBuilder builder = response.getObj().getBuilder(); 53 | 54 | if (builder == null) { 55 | return ClientResponse.finished(null); 56 | } 57 | 58 | builder.append(chunk.getContent().toString(charset)); 59 | return response; 60 | } 61 | 62 | @Override 63 | public ClientResponse done(ClientResponse response) 64 | { 65 | return ClientResponse.finished(response.getObj()); 66 | } 67 | 68 | @Override 69 | public void exceptionCaught( 70 | ClientResponse clientResponse, Throwable e 71 | ) 72 | { 73 | // Its safe to Ignore as the ClientResponse returned in handleChunk were unfinished 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/response/StatusResponseHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.response; 18 | 19 | import org.jboss.netty.handler.codec.http.HttpResponseStatus; 20 | 21 | /** 22 | */ 23 | public class StatusResponseHolder 24 | { 25 | private final HttpResponseStatus status; 26 | private final StringBuilder builder; 27 | 28 | public StatusResponseHolder( 29 | HttpResponseStatus status, 30 | StringBuilder builder 31 | ) 32 | { 33 | this.status = status; 34 | this.builder = builder; 35 | } 36 | 37 | public HttpResponseStatus getStatus() 38 | { 39 | return status; 40 | } 41 | 42 | public StringBuilder getBuilder() 43 | { 44 | return builder; 45 | } 46 | 47 | public String getContent() 48 | { 49 | return builder.toString(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/http/client/response/ToStringResponseHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.http.client.response; 18 | 19 | import org.jboss.netty.handler.codec.http.HttpChunk; 20 | import org.jboss.netty.handler.codec.http.HttpResponse; 21 | 22 | import java.nio.charset.Charset; 23 | 24 | /** 25 | */ 26 | public class ToStringResponseHandler implements HttpResponseHandler 27 | { 28 | private final Charset charset; 29 | 30 | public ToStringResponseHandler(Charset charset) 31 | { 32 | this.charset = charset; 33 | } 34 | 35 | @Override 36 | public ClientResponse handleResponse(HttpResponse response) 37 | { 38 | return ClientResponse.unfinished(new StringBuilder(response.getContent().toString(charset))); 39 | } 40 | 41 | @Override 42 | public ClientResponse handleChunk( 43 | ClientResponse response, 44 | HttpChunk chunk 45 | ) 46 | { 47 | final StringBuilder builder = response.getObj(); 48 | if (builder == null) { 49 | return ClientResponse.finished(null); 50 | } 51 | 52 | builder.append(chunk.getContent().toString(charset)); 53 | return response; 54 | } 55 | 56 | @Override 57 | public ClientResponse done(ClientResponse response) 58 | { 59 | final StringBuilder builder = response.getObj(); 60 | if (builder == null) { 61 | return ClientResponse.finished(null); 62 | } 63 | 64 | return ClientResponse.finished(builder.toString()); 65 | } 66 | 67 | @Override 68 | public void exceptionCaught( 69 | ClientResponse clientResponse, Throwable e 70 | ) 71 | { 72 | // Its safe to Ignore as the ClientResponse returned in handleChunk were unfinished 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/AbstractMonitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import com.metamx.emitter.service.ServiceEmitter; 20 | 21 | /** 22 | */ 23 | public abstract class AbstractMonitor implements Monitor 24 | { 25 | private volatile boolean started = false; 26 | 27 | public void start() 28 | { 29 | started = true; 30 | } 31 | 32 | public void stop() 33 | { 34 | started = false; 35 | } 36 | 37 | @Override 38 | public boolean monitor(ServiceEmitter emitter) 39 | { 40 | if (started) { 41 | return doMonitor(emitter); 42 | } 43 | 44 | return false; 45 | } 46 | 47 | public abstract boolean doMonitor(ServiceEmitter emitter); 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/CgroupUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import com.metamx.common.StringUtils; 20 | import java.lang.management.ManagementFactory; 21 | import java.util.regex.Pattern; 22 | 23 | public class CgroupUtil 24 | { 25 | public static final String SPACE_MATCH = Pattern.quote(" "); 26 | public static final String COMMA_MATCH = Pattern.quote(","); 27 | public static final String COLON_MATCH = Pattern.quote(":"); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/CompoundMonitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import com.google.common.base.Function; 20 | import com.google.common.collect.Lists; 21 | import com.metamx.emitter.service.ServiceEmitter; 22 | 23 | import java.util.Arrays; 24 | import java.util.List; 25 | 26 | public abstract class CompoundMonitor implements Monitor 27 | { 28 | private final List monitors; 29 | 30 | public CompoundMonitor(List monitors) 31 | { 32 | this.monitors = monitors; 33 | } 34 | 35 | public CompoundMonitor(Monitor... monitors) 36 | { 37 | this(Arrays.asList(monitors)); 38 | } 39 | 40 | @Override 41 | public void start() 42 | { 43 | for (Monitor monitor : monitors) { 44 | monitor.start(); 45 | } 46 | } 47 | 48 | @Override 49 | public void stop() 50 | { 51 | for (Monitor monitor : monitors) { 52 | monitor.stop(); 53 | } 54 | } 55 | 56 | @Override 57 | public boolean monitor(final ServiceEmitter emitter) 58 | { 59 | return shouldReschedule(Lists.transform(monitors, 60 | new Function() 61 | { 62 | @Override 63 | public Boolean apply(Monitor monitor) 64 | { 65 | return monitor.monitor(emitter); 66 | } 67 | } 68 | )); 69 | } 70 | 71 | public abstract boolean shouldReschedule(List reschedules); 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/FeedDefiningMonitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import com.google.common.base.Preconditions; 20 | import com.metamx.emitter.service.ServiceMetricEvent; 21 | 22 | public abstract class FeedDefiningMonitor extends AbstractMonitor 23 | { 24 | public static final String DEFAULT_METRICS_FEED = "metrics"; 25 | protected final String feed; 26 | 27 | public FeedDefiningMonitor(String feed) 28 | { 29 | Preconditions.checkNotNull(feed); 30 | this.feed = feed; 31 | } 32 | 33 | protected ServiceMetricEvent.Builder builder() 34 | { 35 | return ServiceMetricEvent.builder().setFeed(feed); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/KeyedDiff.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import com.metamx.common.logger.Logger; 20 | 21 | import java.util.HashMap; 22 | import java.util.Map; 23 | 24 | public class KeyedDiff 25 | { 26 | private static final Logger log = new Logger(KeyedDiff.class); 27 | 28 | private final Map> prevs = new HashMap>(); 29 | 30 | public Map to(String key, Map curr) 31 | { 32 | final Map prev = prevs.put(key, curr); 33 | if (prev != null) { 34 | return subtract(curr, prev); 35 | } 36 | else { 37 | log.debug("No previous data for key[%s]", key); 38 | return null; 39 | } 40 | } 41 | 42 | public static Map subtract(Map xs, Map ys) 43 | { 44 | assert xs.keySet().equals(ys.keySet()); 45 | final Map zs = new HashMap(); 46 | for (String k : xs.keySet()) { 47 | zs.put(k, xs.get(k) - ys.get(k)); 48 | } 49 | return zs; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/Monitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import com.metamx.emitter.service.ServiceEmitter; 20 | 21 | /** 22 | */ 23 | public interface Monitor 24 | { 25 | public void start(); 26 | public void stop(); 27 | public boolean monitor(ServiceEmitter emitter); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/MonitorOfTheMonitors.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import com.metamx.common.logger.Logger; 20 | import com.metamx.emitter.service.ServiceEmitter; 21 | 22 | public class MonitorOfTheMonitors extends AbstractMonitor 23 | { 24 | private static final Logger log = new Logger(MonitorOfTheMonitors.class); 25 | 26 | @Override 27 | public boolean doMonitor(ServiceEmitter emitter) 28 | { 29 | log.info("I am watching..."); 30 | return true; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/MonitorSchedulerConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import org.joda.time.Duration; 20 | import org.skife.config.Config; 21 | import org.skife.config.Default; 22 | 23 | /** 24 | */ 25 | public abstract class MonitorSchedulerConfig 26 | { 27 | @Config({"com.metamx.metrics.emitter.period", "com.metamx.druid.emitter.period"}) 28 | @Default("PT60s") 29 | public abstract Duration getEmitterPeriod(); 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/MonitorUtils.java: -------------------------------------------------------------------------------- 1 | package com.metamx.metrics; 2 | 3 | import com.metamx.emitter.service.ServiceMetricEvent; 4 | import java.util.Map; 5 | 6 | public class MonitorUtils 7 | { 8 | public static void addDimensionsToBuilder(ServiceMetricEvent.Builder builder, Map dimensions) 9 | { 10 | for (Map.Entry keyValue : dimensions.entrySet()) { 11 | builder.setDimension(keyValue.getKey(), keyValue.getValue()); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/ParametrizedUriEmitterMonitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.metamx.metrics; 19 | 20 | import com.google.common.collect.ImmutableMap; 21 | import com.metamx.emitter.core.ParametrizedUriEmitter; 22 | import com.metamx.emitter.service.ServiceEmitter; 23 | 24 | import java.net.URI; 25 | import java.util.HashMap; 26 | import java.util.Map; 27 | 28 | public class ParametrizedUriEmitterMonitor extends FeedDefiningMonitor 29 | { 30 | private final ParametrizedUriEmitter parametrizedUriEmitter; 31 | private final Map monitors = new HashMap<>(); 32 | 33 | public ParametrizedUriEmitterMonitor(String feed, ParametrizedUriEmitter parametrizedUriEmitter) 34 | { 35 | super(feed); 36 | this.parametrizedUriEmitter = parametrizedUriEmitter; 37 | } 38 | 39 | private void updateMonitors() 40 | { 41 | parametrizedUriEmitter.forEachEmitter( 42 | (uri, emitter) -> { 43 | monitors.computeIfAbsent( 44 | uri, 45 | u -> { 46 | HttpPostEmitterMonitor monitor = new HttpPostEmitterMonitor( 47 | feed, 48 | emitter, 49 | ImmutableMap.of("uri", uri.toString()) 50 | ); 51 | monitor.start(); 52 | return monitor; 53 | } 54 | ); 55 | } 56 | ); 57 | } 58 | 59 | @Override 60 | public void stop() 61 | { 62 | monitors.values().forEach(AbstractMonitor::stop); 63 | super.stop(); 64 | } 65 | 66 | @Override 67 | public boolean doMonitor(ServiceEmitter emitter) 68 | { 69 | updateMonitors(); 70 | monitors.values().forEach(m -> m.doMonitor(emitter)); 71 | return true; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/PidDiscoverer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | public interface PidDiscoverer 20 | { 21 | long getPid(); 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/SigarPidDiscoverer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | public class SigarPidDiscoverer implements PidDiscoverer 20 | { 21 | private static final SigarPidDiscoverer INSTANCE = new SigarPidDiscoverer(); 22 | 23 | public static SigarPidDiscoverer instance() 24 | { 25 | return INSTANCE; 26 | } 27 | 28 | /** 29 | * use {SigarPidDiscoverer.instance()} 30 | */ 31 | private SigarPidDiscoverer() 32 | { 33 | 34 | } 35 | 36 | @Override 37 | public long getPid() 38 | { 39 | return SigarUtil.getCurrentProcessId(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/cgroups/CgroupDiscoverer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics.cgroups; 18 | 19 | import java.nio.file.Path; 20 | 21 | public interface CgroupDiscoverer 22 | { 23 | /** 24 | * Returns a path for a specific cgroup. This path should contain the interesting cgroup files without further traversing needed. 25 | * @param cgroup The cgroup 26 | * @return The path that contains that cgroup's interesting bits. 27 | */ 28 | Path discover(String cgroup); 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/cgroups/ProcPidCgroupDiscoverer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.metamx.metrics.cgroups; 19 | 20 | import com.metamx.metrics.PidDiscoverer; 21 | 22 | import java.nio.file.Path; 23 | import java.nio.file.Paths; 24 | 25 | public class ProcPidCgroupDiscoverer implements CgroupDiscoverer 26 | { 27 | private final ProcCgroupDiscoverer delegate; 28 | 29 | public ProcPidCgroupDiscoverer(PidDiscoverer pidDiscoverer) 30 | { 31 | delegate = new ProcCgroupDiscoverer(Paths.get("/proc", Long.toString(pidDiscoverer.getPid()))); 32 | } 33 | 34 | @Override 35 | public Path discover(String cgroup) 36 | { 37 | return delegate.discover(cgroup); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/metamx/metrics/cgroups/ProcSelfCgroupDiscoverer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics.cgroups; 18 | 19 | import java.nio.file.Path; 20 | import java.nio.file.Paths; 21 | 22 | public class ProcSelfCgroupDiscoverer implements CgroupDiscoverer 23 | { 24 | private final ProcCgroupDiscoverer delegate; 25 | 26 | public ProcSelfCgroupDiscoverer() 27 | { 28 | delegate = new ProcCgroupDiscoverer(Paths.get("/proc/self")); 29 | } 30 | 31 | @Override 32 | public Path discover(String cgroup) 33 | { 34 | return delegate.discover(cgroup); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/common/BufferUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.metamx.common; 19 | 20 | import java.lang.management.BufferPoolMXBean; 21 | import java.lang.management.ManagementFactory; 22 | import java.util.List; 23 | 24 | public final class BufferUtils 25 | { 26 | 27 | public static long totalMemoryUsedByDirectAndMappedBuffers() 28 | { 29 | long totalMemoryUsed = 0L; 30 | List pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class); 31 | for (BufferPoolMXBean pool : pools) { 32 | totalMemoryUsed += pool.getMemoryUsed(); 33 | } 34 | return totalMemoryUsed; 35 | } 36 | 37 | private BufferUtils() {} 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/common/ByteBufferUtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common; 18 | 19 | import com.google.common.io.Files; 20 | import junit.framework.Assert; 21 | import org.junit.Rule; 22 | import org.junit.Test; 23 | import org.junit.rules.TemporaryFolder; 24 | 25 | import java.io.BufferedOutputStream; 26 | import java.io.File; 27 | import java.io.FileOutputStream; 28 | import java.io.OutputStream; 29 | import java.nio.ByteBuffer; 30 | import java.nio.MappedByteBuffer; 31 | import java.util.Arrays; 32 | 33 | public class ByteBufferUtilsTest 34 | { 35 | @Rule 36 | public TemporaryFolder temporaryFolder = new TemporaryFolder(); 37 | 38 | @Test 39 | public void testUnmapDoesntCrashJVM() throws Exception 40 | { 41 | final File file = temporaryFolder.newFile("some_mmap_file"); 42 | try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { 43 | final byte[] data = new byte[4096]; 44 | Arrays.fill(data, (byte) 0x5A); 45 | os.write(data); 46 | } 47 | final MappedByteBuffer mappedByteBuffer = Files.map(file); 48 | Assert.assertEquals((byte) 0x5A, mappedByteBuffer.get(0)); 49 | ByteBufferUtils.unmap(mappedByteBuffer); 50 | ByteBufferUtils.unmap(mappedByteBuffer); 51 | } 52 | 53 | @Test 54 | public void testFreeDoesntCrashJVM() throws Exception 55 | { 56 | final ByteBuffer directBuffer = ByteBuffer.allocateDirect(4096); 57 | ByteBufferUtils.free(directBuffer); 58 | ByteBufferUtils.free(directBuffer); 59 | 60 | final ByteBuffer heapBuffer = ByteBuffer.allocate(4096); 61 | ByteBufferUtils.free(heapBuffer); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/common/FileUtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.metamx.common; 19 | 20 | import org.junit.Assert; 21 | import org.junit.Rule; 22 | import org.junit.Test; 23 | import org.junit.rules.TemporaryFolder; 24 | 25 | import java.io.File; 26 | import java.io.IOException; 27 | import java.io.RandomAccessFile; 28 | 29 | public class FileUtilsTest 30 | { 31 | @Rule 32 | public TemporaryFolder folder = new TemporaryFolder(); 33 | 34 | @Test 35 | public void testMap() throws IOException 36 | { 37 | File dataFile = folder.newFile("data"); 38 | long buffersMemoryBefore = BufferUtils.totalMemoryUsedByDirectAndMappedBuffers(); 39 | try (RandomAccessFile raf = new RandomAccessFile(dataFile, "rw")) { 40 | raf.write(42); 41 | raf.setLength(1 << 20); // 1 MB 42 | } 43 | try (MappedByteBufferHandler mappedByteBufferHandler = FileUtils.map(dataFile)) { 44 | Assert.assertEquals(42, mappedByteBufferHandler.get().get(0)); 45 | } 46 | long buffersMemoryAfter = BufferUtils.totalMemoryUsedByDirectAndMappedBuffers(); 47 | Assert.assertEquals(buffersMemoryBefore, buffersMemoryAfter); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/common/guava/FilteredSequenceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.base.Predicate; 20 | import com.google.common.collect.Iterables; 21 | import com.google.common.collect.Lists; 22 | import org.junit.Test; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | */ 28 | public class FilteredSequenceTest 29 | { 30 | @Test 31 | public void testSanity() throws Exception 32 | { 33 | Predicate pred = new Predicate() 34 | { 35 | @Override 36 | public boolean apply(Integer input) 37 | { 38 | return input % 3 == 0; 39 | } 40 | }; 41 | 42 | for (int i = 0; i < 25; ++i) { 43 | List vals = Lists.newArrayList(); 44 | for (int j = 0; j < i; ++j) { 45 | vals.add(j); 46 | } 47 | 48 | SequenceTestHelper.testAll( 49 | String.format("Run %,d: ", i), 50 | new FilteredSequence<>(Sequences.simple(vals), pred), 51 | Lists.newArrayList(Iterables.filter(vals, pred)) 52 | ); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/common/guava/MappedSequenceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.base.Function; 20 | import com.google.common.collect.Lists; 21 | import org.junit.Test; 22 | 23 | import java.util.List; 24 | 25 | /** 26 | */ 27 | public class MappedSequenceTest 28 | { 29 | @Test 30 | public void testSanity() throws Exception 31 | { 32 | Function fn = new Function() 33 | { 34 | @Override 35 | public Integer apply(Integer input) 36 | { 37 | return input + 2; 38 | } 39 | }; 40 | 41 | for (int i = 4; i < 5; ++i) { 42 | List vals = Lists.newArrayList(); 43 | for (int j = 0; j < i; ++j) { 44 | vals.add(j); 45 | } 46 | 47 | SequenceTestHelper.testAll( 48 | String.format("Run %,d: ", i), 49 | new MappedSequence<>(Sequences.simple(vals), fn), 50 | Lists.transform(vals, fn) 51 | ); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/common/guava/MergeIteratorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import com.google.common.collect.Lists; 20 | import com.google.common.collect.Ordering; 21 | import junit.framework.Assert; 22 | import org.junit.Test; 23 | 24 | import java.util.Arrays; 25 | 26 | /** 27 | */ 28 | public class MergeIteratorTest 29 | { 30 | @Test 31 | public void testSanity() throws Exception 32 | { 33 | MergeIterator iter = new MergeIterator<>( 34 | Ordering.natural(), 35 | Lists.newArrayList( 36 | Arrays.asList(1, 3, 5, 7, 9).iterator(), 37 | Arrays.asList(2, 8).iterator(), 38 | Arrays.asList(4, 6, 8).iterator() 39 | ) 40 | ); 41 | 42 | Assert.assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 8, 9), Lists.newArrayList(iter)); 43 | } 44 | 45 | @Test 46 | public void testScrewsUpOnOutOfOrder() throws Exception 47 | { 48 | MergeIterator iter = new MergeIterator<>( 49 | Ordering.natural(), 50 | Lists.newArrayList( 51 | Arrays.asList(1, 3, 5, 4, 7, 9).iterator(), 52 | Arrays.asList(2, 8).iterator(), 53 | Arrays.asList(4, 6).iterator() 54 | ) 55 | ); 56 | 57 | Assert.assertEquals(Arrays.asList(1, 2, 3, 4, 5, 4, 6, 7, 8, 9), Lists.newArrayList(iter)); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/common/guava/ResourceClosingSequenceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 - 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | 22 | import java.io.Closeable; 23 | import java.io.IOException; 24 | import java.util.Arrays; 25 | import java.util.List; 26 | import java.util.concurrent.atomic.AtomicInteger; 27 | 28 | /** 29 | */ 30 | public class ResourceClosingSequenceTest 31 | { 32 | @Test 33 | public void testSanity() throws Exception 34 | { 35 | final AtomicInteger closedCounter = new AtomicInteger(0); 36 | Closeable closeable = new Closeable() 37 | { 38 | @Override 39 | public void close() throws IOException 40 | { 41 | closedCounter.incrementAndGet(); 42 | } 43 | }; 44 | 45 | final List nums = Arrays.asList(1, 2, 3, 4, 5); 46 | 47 | SequenceTestHelper.testAll(Sequences.withBaggage(Sequences.simple(nums), closeable), nums); 48 | 49 | Assert.assertEquals(3, closedCounter.get()); 50 | 51 | closedCounter.set(0); 52 | SequenceTestHelper.testClosed(closedCounter, Sequences.withBaggage(new UnsupportedSequence(), closeable)); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/common/guava/TestSequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | import java.util.Arrays; 20 | import java.util.Iterator; 21 | import java.util.concurrent.atomic.AtomicBoolean; 22 | 23 | /** 24 | */ 25 | public class TestSequence implements Sequence 26 | { 27 | public static TestSequence create(Iterable iterable) 28 | { 29 | return new TestSequence<>(iterable); 30 | } 31 | 32 | public static TestSequence create(T... vals) 33 | { 34 | return create(Arrays.asList(vals)); 35 | } 36 | 37 | private final AtomicBoolean closed = new AtomicBoolean(false); 38 | private final Sequence base; 39 | 40 | public TestSequence(final Iterable iterable) 41 | { 42 | base = new BaseSequence<>( 43 | new BaseSequence.IteratorMaker>() 44 | { 45 | @Override 46 | public Iterator make() 47 | { 48 | return iterable.iterator(); 49 | } 50 | 51 | @Override 52 | public void cleanup(Iterator iterFromMake) 53 | { 54 | closed.set(true); 55 | } 56 | }); 57 | } 58 | 59 | @Override 60 | public OutType accumulate(OutType initValue, Accumulator accumulator) 61 | { 62 | return base.accumulate(initValue, accumulator); 63 | } 64 | 65 | @Override 66 | public Yielder toYielder(OutType initValue, YieldingAccumulator accumulator) 67 | { 68 | return base.toYielder(initValue, accumulator); 69 | } 70 | 71 | public boolean isClosed() 72 | { 73 | return closed.get(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/common/guava/UnsupportedSequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 - 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava; 18 | 19 | /** 20 | */ 21 | public class UnsupportedSequence implements Sequence 22 | { 23 | @Override 24 | public OutType accumulate( 25 | OutType initValue, Accumulator accumulator 26 | ) 27 | { 28 | throw new UnsupportedOperationException(); 29 | } 30 | 31 | @Override 32 | public Yielder toYielder( 33 | OutType initValue, YieldingAccumulator accumulator 34 | ) 35 | { 36 | throw new UnsupportedOperationException(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/common/guava/nary/SortedMergeIteratorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011,2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.common.guava.nary; 18 | 19 | import com.google.common.collect.Lists; 20 | import com.metamx.common.guava.Comparators; 21 | import org.junit.Assert; 22 | import org.junit.Test; 23 | 24 | import java.util.Arrays; 25 | 26 | /** 27 | */ 28 | public class SortedMergeIteratorTest 29 | { 30 | @Test 31 | public void testSanity() throws Exception 32 | { 33 | SortedMergeIterator iter = SortedMergeIterator.create( 34 | Arrays.asList(1, 4, 5, 7, 9).iterator(), 35 | Arrays.asList(1, 2, 3, 6, 7, 8, 9, 10, 11).iterator(), 36 | Comparators.comparable(), 37 | new BinaryFn() 38 | { 39 | @Override 40 | public Integer apply(Integer arg1, Integer arg2) 41 | { 42 | return arg1 == null ? arg2 : arg2 == null ? arg1 : arg1 + arg2; 43 | } 44 | } 45 | ); 46 | 47 | Assert.assertEquals( 48 | Arrays.asList(2, 2, 3, 4, 5, 6, 14, 8, 18, 10, 11), 49 | Lists.newArrayList(iter) 50 | ); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/common/logger/LoggerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.metamx.common.logger; 17 | 18 | import org.junit.Test; 19 | 20 | public class LoggerTest 21 | { 22 | @Test 23 | public void testLogWithCrazyMessages() 24 | { 25 | final String message = "this % might %d kill %*.s the %s parser"; 26 | final Logger log = new Logger(LoggerTest.class); 27 | log.warn(message); 28 | } 29 | 30 | @Test 31 | public void testLegacyLogging() 32 | { 33 | final Logger log = new Logger(LoggerTest.class); 34 | final Throwable throwable = new Throwable(); 35 | // These should show up in an IDE as deprecated, but shouldn't actually fail. 36 | log.error("foo", throwable); 37 | log.warn("foo", throwable); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/emitter/core/BWListEventFilterConfigTest.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | import java.util.Properties; 8 | 9 | public class BWListEventFilterConfigTest 10 | { 11 | @Test 12 | public void testDefaults() 13 | { 14 | final Properties props = new Properties(); 15 | 16 | final ObjectMapper objectMapper = new ObjectMapper(); 17 | final ParametrizedUriEmitterConfig paramConfig = objectMapper.convertValue(Emitters.makeCustomFactoryMap(props), ParametrizedUriEmitterConfig.class); 18 | 19 | Assert.assertTrue(paramConfig.getBwListEventFilterConfig().getWhiteList().isEmpty()); 20 | Assert.assertTrue(paramConfig.getBwListEventFilterConfig().getBlackList().isEmpty()); 21 | } 22 | 23 | @Test 24 | public void testSettingEverything() 25 | { 26 | final Properties props = new Properties(); 27 | props.setProperty("com.metamx.emitter.bwFilter.whiteList.A", "AW1, AW2"); 28 | props.setProperty("com.metamx.emitter.bwFilter.blackList.A", "AB1, AB2"); 29 | props.setProperty("com.metamx.emitter.bwFilter.blackList.B", "BB1, BB2"); 30 | 31 | final ObjectMapper objectMapper = new ObjectMapper(); 32 | final ParametrizedUriEmitterConfig paramConfig = objectMapper.convertValue(Emitters.makeCustomFactoryMap(props), ParametrizedUriEmitterConfig.class); 33 | 34 | Assert.assertTrue(paramConfig.getBwListEventFilterConfig().getWhiteList().containsKey("A")); 35 | Assert.assertTrue(paramConfig.getBwListEventFilterConfig().getWhiteList().get("A").contains("AW1")); 36 | Assert.assertTrue(paramConfig.getBwListEventFilterConfig().getWhiteList().get("A").contains("AW2")); 37 | Assert.assertTrue(paramConfig.getBwListEventFilterConfig().getBlackList().containsKey("A")); 38 | Assert.assertTrue(paramConfig.getBwListEventFilterConfig().getBlackList().get("A").contains("AB1")); 39 | Assert.assertTrue(paramConfig.getBwListEventFilterConfig().getBlackList().get("A").contains("AB2")); 40 | Assert.assertTrue(paramConfig.getBwListEventFilterConfig().getBlackList().containsKey("B")); 41 | Assert.assertTrue(paramConfig.getBwListEventFilterConfig().getBlackList().get("B").contains("BB1")); 42 | Assert.assertTrue(paramConfig.getBwListEventFilterConfig().getBlackList().get("B").contains("BB2")); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/emitter/core/EmittersTest.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.util.Map; 7 | import java.util.Properties; 8 | 9 | public class EmittersTest 10 | { 11 | @Test 12 | public void makeCustomFactoryMapTest() 13 | { 14 | final Properties properties = new Properties(); 15 | properties.setProperty("com.metamx.emitter.property.A", "valueA"); 16 | properties.setProperty("com.metamx.emitter.property.B", "valueB"); 17 | final Map map = Emitters.makeCustomFactoryMap(properties); 18 | Assert.assertEquals("valueA", ((Map) map.get("property")).get("A")); 19 | Assert.assertEquals("valueB", ((Map) map.get("property")).get("B")); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/emitter/core/GoHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.metamx.emitter.core; 19 | 20 | import com.google.common.base.Throwables; 21 | import com.metamx.common.ISE; 22 | import org.asynchttpclient.ListenableFuture; 23 | import org.asynchttpclient.Request; 24 | import org.asynchttpclient.Response; 25 | 26 | import java.util.concurrent.atomic.AtomicInteger; 27 | 28 | /** 29 | */ 30 | public abstract class GoHandler 31 | { 32 | /******* Abstract Methods *********/ 33 | protected abstract ListenableFuture go(Request request) throws X; 34 | 35 | /******* Non Abstract Methods ********/ 36 | private volatile boolean succeeded = false; 37 | 38 | public boolean succeeded() 39 | { 40 | return succeeded; 41 | } 42 | 43 | public ListenableFuture run(Request request) 44 | { 45 | try { 46 | final ListenableFuture retVal = go(request); 47 | succeeded = true; 48 | return retVal; 49 | } 50 | catch (Throwable e) { 51 | succeeded = false; 52 | throw Throwables.propagate(e); 53 | } 54 | } 55 | 56 | public GoHandler times(final int n) 57 | { 58 | final GoHandler myself = this; 59 | 60 | return new GoHandler() 61 | { 62 | AtomicInteger counter = new AtomicInteger(0); 63 | 64 | @Override 65 | public ListenableFuture go(final Request request) 66 | { 67 | if (counter.getAndIncrement() < n) { 68 | return myself.go(request); 69 | } 70 | succeeded = false; 71 | throw new ISE("Called more than %d times", n); 72 | } 73 | }; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/emitter/core/IntEvent.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core; 2 | 3 | import org.joda.time.DateTime; 4 | 5 | import java.util.Map; 6 | 7 | class IntEvent implements Event 8 | { 9 | int index; 10 | 11 | IntEvent() 12 | { 13 | } 14 | 15 | @Override 16 | public Map toMap() 17 | { 18 | return null; 19 | } 20 | 21 | @Override 22 | public String getFeed() 23 | { 24 | return null; 25 | } 26 | 27 | @Override 28 | public DateTime getCreatedTime() 29 | { 30 | return null; 31 | } 32 | 33 | @Override 34 | public boolean isSafeToBuffer() 35 | { 36 | return false; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/emitter/core/MapEvent.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core; 2 | 3 | import com.fasterxml.jackson.databind.annotation.JsonSerialize; 4 | import com.fasterxml.jackson.databind.util.StdConverter; 5 | import org.joda.time.DateTime; 6 | 7 | import java.util.Map; 8 | 9 | @JsonSerialize(converter = MapEvent.MapEventConverter.class) 10 | public final class MapEvent implements Event 11 | { 12 | private final Map map; 13 | 14 | public MapEvent(Map map) 15 | { 16 | this.map = map; 17 | } 18 | 19 | @Override 20 | public Map toMap() 21 | { 22 | return map; 23 | } 24 | 25 | @Override 26 | public String getFeed() 27 | { 28 | return (String) map.get("feed"); 29 | } 30 | 31 | @Override 32 | public DateTime getCreatedTime() 33 | { 34 | return (DateTime) map.get("createdTime"); 35 | } 36 | 37 | @Override 38 | public boolean isSafeToBuffer() 39 | { 40 | return map.containsKey("isSafeToBuffer"); 41 | } 42 | 43 | public static class MapEventConverter extends StdConverter> 44 | { 45 | @Override 46 | public Map convert(MapEvent value) 47 | { 48 | return value.toMap(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/emitter/core/MockHttpClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.metamx.emitter.core; 19 | 20 | import org.asynchttpclient.DefaultAsyncHttpClient; 21 | import org.asynchttpclient.ListenableFuture; 22 | import org.asynchttpclient.Request; 23 | import org.asynchttpclient.Response; 24 | 25 | /** 26 | */ 27 | public class MockHttpClient extends DefaultAsyncHttpClient 28 | { 29 | private volatile GoHandler goHandler; 30 | 31 | public MockHttpClient() 32 | { 33 | } 34 | 35 | public GoHandler getGoHandler() 36 | { 37 | return goHandler; 38 | } 39 | 40 | public void setGoHandler(GoHandler goHandler) 41 | { 42 | this.goHandler = goHandler; 43 | } 44 | 45 | public boolean succeeded() 46 | { 47 | return goHandler.succeeded(); 48 | } 49 | 50 | @Override 51 | public ListenableFuture executeRequest(Request request) 52 | { 53 | return goHandler.run(request); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/emitter/core/filter/EmptyBWListEventFilterTest.java: -------------------------------------------------------------------------------- 1 | package com.metamx.emitter.core.filter; 2 | 3 | import com.google.common.collect.ImmutableMap; 4 | import com.metamx.emitter.core.MapEvent; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | public class EmptyBWListEventFilterTest 9 | { 10 | @Test 11 | public void isListed() 12 | { 13 | final EmptyBWListEventFilter eventFilter = new EmptyBWListEventFilter(); 14 | Assert.assertFalse(eventFilter.isNotListed(new MapEvent(ImmutableMap.of()))); 15 | Assert.assertFalse(eventFilter.isNotListed(new MapEvent(ImmutableMap.of("A", "A1")))); 16 | } 17 | } -------------------------------------------------------------------------------- /src/test/java/com/metamx/emitter/service/UnitEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.emitter.service; 18 | 19 | import com.fasterxml.jackson.annotation.JsonValue; 20 | import com.google.common.collect.ImmutableMap; 21 | import com.metamx.emitter.core.Event; 22 | import java.util.Collections; 23 | import java.util.HashMap; 24 | import org.joda.time.DateTime; 25 | 26 | import java.util.Map; 27 | 28 | /** 29 | */ 30 | public class UnitEvent implements Event 31 | { 32 | private final String feed; 33 | private final Number value; 34 | private final Map dimensions; 35 | private final DateTime createdTime; 36 | 37 | public UnitEvent(String feed, Number value) 38 | { 39 | this(feed, value, Collections.emptyMap()); 40 | } 41 | 42 | public UnitEvent(String feed, Number value, Map dimensions) 43 | { 44 | this.feed = feed; 45 | this.value = value; 46 | this.dimensions = dimensions; 47 | 48 | createdTime = new DateTime(); 49 | } 50 | 51 | @Override 52 | @JsonValue 53 | public Map toMap() 54 | { 55 | Map result = new HashMap<>(); 56 | result.putAll(dimensions); 57 | result.put("feed", feed); 58 | result.put("metrics", ImmutableMap.of("value", value)); 59 | return ImmutableMap.copyOf(result); 60 | } 61 | 62 | public DateTime getCreatedTime() 63 | { 64 | return createdTime; 65 | } 66 | 67 | public String getFeed() 68 | { 69 | return feed; 70 | } 71 | 72 | public boolean isSafeToBuffer() 73 | { 74 | return true; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/metrics/GcNameTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import org.gridkit.lab.jvm.perfdata.JStatData; 20 | 21 | public class GcNameTest 22 | { 23 | 24 | public static void main(String[] args) 25 | { 26 | JStatData jStatData = JStatData.connect(SigarUtil.getCurrentProcessId()); 27 | System.out.println(jStatData.getAllCounters().get("sun.gc.collector.0.name").getValue()); 28 | System.out.println(jStatData.getAllCounters().get("sun.gc.collector.1.name").getValue()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/metrics/JvmPidDiscovererTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | 22 | public class JvmPidDiscovererTest 23 | { 24 | @Test 25 | public void getPid() throws Exception 26 | { 27 | Assert.assertNotNull(JvmPidDiscoverer.instance().getPid()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/metrics/MonitorUtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import com.google.common.collect.ImmutableList; 20 | import com.google.common.collect.ImmutableMap; 21 | import com.metamx.emitter.service.ServiceMetricEvent; 22 | import java.util.Map; 23 | import org.junit.Assert; 24 | import org.junit.Test; 25 | 26 | public class MonitorUtilsTest 27 | { 28 | @Test 29 | public void testAddDimensionsToBuilder() 30 | { 31 | ServiceMetricEvent.Builder builder = new ServiceMetricEvent.Builder(); 32 | Map dimensions = ImmutableMap.of( 33 | "dim1", new String[]{"value1"}, 34 | "dim2", new String[]{"value2.1", "value2.2"} 35 | ); 36 | 37 | MonitorUtils.addDimensionsToBuilder(builder, dimensions); 38 | 39 | Assert.assertEquals(builder.getDimension("dim1"), ImmutableList.of("value1")); 40 | Assert.assertEquals(builder.getDimension("dim2"), ImmutableList.of("value2.1", "value2.2")); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/metrics/MonitorsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import com.google.common.collect.ImmutableMap; 20 | import com.metamx.emitter.core.Event; 21 | import java.util.List; 22 | import org.junit.Assert; 23 | import org.junit.Test; 24 | 25 | public class MonitorsTest 26 | { 27 | 28 | @Test 29 | public void testSetFeed() 30 | { 31 | String feed = "testFeed"; 32 | StubServiceEmitter emitter = new StubServiceEmitter("dev/monitor-test", "localhost:0000"); 33 | Monitor m = Monitors.createCompoundJvmMonitor(ImmutableMap.of(), feed); 34 | m.start(); 35 | m.monitor(emitter); 36 | m.stop(); 37 | checkEvents(emitter.getEvents(), feed); 38 | } 39 | 40 | @Test 41 | public void testDefaultFeed() 42 | { 43 | StubServiceEmitter emitter = new StubServiceEmitter("dev/monitor-test", "localhost:0000"); 44 | Monitor m = Monitors.createCompoundJvmMonitor(ImmutableMap.of()); 45 | m.start(); 46 | m.monitor(emitter); 47 | m.stop(); 48 | checkEvents(emitter.getEvents(), "metrics"); 49 | } 50 | 51 | private void checkEvents(List events, String expectedFeed) 52 | { 53 | Assert.assertFalse("no events emitted", events.isEmpty()); 54 | for (Event e : events) { 55 | if (!expectedFeed.equals(e.getFeed())) { 56 | String message = String.format("\"feed\" in event: %s", e.toMap().toString()); 57 | Assert.assertEquals(message, expectedFeed, e.getFeed()); 58 | } 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/metrics/SigarLoadTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import junit.framework.Assert; 20 | import org.hyperic.sigar.Sigar; 21 | import org.hyperic.sigar.SigarException; 22 | import org.junit.Test; 23 | 24 | public class SigarLoadTest 25 | { 26 | @Test 27 | public void testSigarLoad() throws SigarException 28 | { 29 | Sigar sigar = SigarUtil.getSigar(); 30 | Assert.assertTrue(sigar.getPid() > 0); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/metrics/SigarPidDiscovererTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import org.junit.Test; 20 | 21 | public class SigarPidDiscovererTest 22 | { 23 | @Test 24 | public void simpleTest() 25 | { 26 | // Just make sure we don't crash 27 | SigarPidDiscoverer.instance().getPid(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/metrics/StubServiceEmitter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics; 18 | 19 | import com.metamx.emitter.core.Event; 20 | import com.metamx.emitter.service.ServiceEmitter; 21 | import java.io.IOException; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | class StubServiceEmitter extends ServiceEmitter 26 | { 27 | private List events = new ArrayList<>(); 28 | 29 | public StubServiceEmitter(String service, String host) 30 | { 31 | super(service, host, null); 32 | } 33 | 34 | @Override 35 | public void emit(Event event) 36 | { 37 | events.add(event); 38 | } 39 | 40 | public List getEvents() 41 | { 42 | return events; 43 | } 44 | 45 | @Override 46 | public void start() 47 | { 48 | } 49 | 50 | @Override 51 | public void flush() throws IOException 52 | { 53 | } 54 | 55 | @Override 56 | public void close() throws IOException 57 | { 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/com/metamx/metrics/cgroups/TestUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Metamarkets Group Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.metamx.metrics.cgroups; 18 | 19 | import com.metamx.common.StringUtils; 20 | import java.io.File; 21 | import java.io.IOException; 22 | import java.nio.file.Files; 23 | import java.util.regex.Pattern; 24 | import org.junit.Assert; 25 | 26 | public class TestUtils 27 | { 28 | public static void setUpCgroups( 29 | File procDir, 30 | File cgroupDir 31 | ) throws IOException 32 | { 33 | final File procMountsTemplate = new File(procDir, "mounts.template"); 34 | final File procMounts = new File(procDir, "mounts"); 35 | copyResource("/proc.mounts", procMountsTemplate); 36 | 37 | final String procMountsString = StringUtils.fromUtf8(Files.readAllBytes(procMountsTemplate.toPath())); 38 | Files.write( 39 | procMounts.toPath(), 40 | StringUtils.toUtf8(procMountsString.replaceAll( 41 | Pattern.quote("/sys/fs/cgroup"), 42 | cgroupDir.getAbsolutePath() 43 | )) 44 | ); 45 | 46 | Assert.assertTrue(new File( 47 | cgroupDir, 48 | "cpu,cpuacct/system.slice/mesos-agent-druid.service/f12ba7e0-fa16-462e-bb9d-652ccc27f0ee" 49 | ).mkdirs()); 50 | copyResource("/proc.pid.cgroup", new File(procDir, "cgroup")); 51 | } 52 | 53 | public static void copyResource(String resource, File out) throws IOException 54 | { 55 | Files.copy(TestUtils.class.getResourceAsStream(resource), out.toPath()); 56 | Assert.assertTrue(out.exists()); 57 | Assert.assertNotEquals(0, out.length()); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/resources/defaultMetricDimensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "query/time" : { "dimensions" : ["dataSource", "type"], "type" : "timer"}, 3 | "query/cache/total/hitRate" : { "dimensions" : [], "type" : "gauge", "convertRange" : true }, 4 | "jvm/gc/count" : { "dimensions" : ["gcName", "gcGen"], "type" : "count" } 5 | } 6 | -------------------------------------------------------------------------------- /src/test/resources/keystore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/metamx/java-util/02dccb0bab36c3acaa3a8911ced34c9becf2ff50/src/test/resources/keystore.jks -------------------------------------------------------------------------------- /src/test/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/test/resources/proc.cgroups: -------------------------------------------------------------------------------- 1 | #subsys_name hierarchy num_cgroups enabled 2 | cpuset 4 1 1 3 | cpu 9 108 1 4 | cpuacct 9 108 1 5 | blkio 8 60 1 6 | memory 3 160 1 7 | devices 7 60 1 8 | freezer 6 52 1 9 | net_cls 2 1 1 10 | perf_event 10 1 1 11 | net_prio 2 1 1 12 | hugetlb 5 1 1 13 | pids 11 63 1 14 | -------------------------------------------------------------------------------- /src/test/resources/proc.pid.cgroup: -------------------------------------------------------------------------------- 1 | 11:pids:/system.slice/mesos-agent-druid.service 2 | 10:perf_event:/ 3 | 9:cpu,cpuacct:/system.slice/mesos-agent-druid.service/f12ba7e0-fa16-462e-bb9d-652ccc27f0ee 4 | 8:blkio:/system.slice/mesos-agent-druid.service 5 | 7:devices:/system.slice/mesos-agent-druid.service 6 | 6:freezer:/system.slice/mesos-agent-druid.service/f12ba7e0-fa16-462e-bb9d-652ccc27f0ee 7 | 5:hugetlb:/ 8 | 4:cpuset:/ 9 | 3:memory:/system.slice/mesos-agent-druid.service 10 | 2:net_cls,net_prio:/ 11 | 1:name=systemd:/mesos_executors.slice 12 | --------------------------------------------------------------------------------