Skip to content

Commit f5119f2

Browse files
authored
Merge pull request #104 from Stephen-Bao/master
Thread-safety enhancements, unit tests, and benchmarking
2 parents bbd2097 + b6c2551 commit f5119f2

File tree

13 files changed

+1121
-28
lines changed

13 files changed

+1121
-28
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,24 @@ config.setAgentEndpoint("udp://127.0.0.1:1000");
316316
AWS_EMF_AGENT_ENDPOINT="udp://127.0.0.1:1000"
317317
```
318318

319+
## Thread-safety
320+
321+
### Internal Synchronization
322+
323+
The MetricsLogger class is thread-safe. Specifically, the generalized multi-threading use cases for this library are:
324+
325+
1. Collect some metrics or metadata on a single MetricsLogger; Pass the logger into one or more async contexts where new metrics or metadata can be added concurrently; Join the async contexts (e.g. Future.get()) and flush the metrics.
326+
2. Collect some metrics or metadata on a single MetricsLogger; Pass the logger into an async context; Flush from the async context concurrently.
327+
328+
Thread-safety for the first use case is achieved by introducing concurrent internal data structures and atomic operations associated with these models, to ensure the access to shared mutable resources are always synchronized.
329+
330+
Thread-safety for the second use case is achieved by using a ReentrantReadWriteLock. This lock is used to create an internal sync context for flush() method in multi-threading situations. `flush()` acquires write lock, while other methods (which have access to mutable shared data with `flush()`) acquires read lock. This makes sure `flush()` is always executed exclusively, while other methods can be executed concurrently.
331+
332+
### Use Cases that are Not Covered
333+
334+
With all the internal synchronization measures, however, there're still certain multi-threading use cases that are not covered by this library, which might require external synchronizations or other protection measures.
335+
This is due to the fact that the execution order of APIs are not determined in async contexts. For example, if user needs to associate a given set of properties with a metric in each thread, the results are not guaranteed since the execution order of `putProperty()` is not determined across threads. In such cases, we recommend using a different MetricsLogger instance for different threads, so that no resources are shared and no thread-safety problem would ever happen. Note that this can often be simplified by using a ThreadLocal variable.
336+
319337
## Examples
320338

321339
Check out the [examples](https://github.com/awslabs/aws-embedded-metrics-java/tree/master/examples) directory to get started.
@@ -362,6 +380,14 @@ To auto fix code style, run
362380
./gradlew :spotlessApply
363381
```
364382

383+
### Benchmark
384+
385+
We use [JMH](https://github.com/openjdk/jmh) as our framework for concurrency performance benchmarking. Benchmarks can be run by:
386+
```
387+
./gradlew jmh
388+
```
389+
To run a single benchmark, consider using JMH plugins. For example, [JMH plugin for IntelliJ IDEA](https://github.com/artyushov/idea-jmh-plugin)
390+
365391
## License
366392

367393
This project is licensed under the Apache-2.0 License.

build.gradle

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ plugins {
1818
id 'com.diffplug.spotless' version '5.8.2'
1919
id 'maven-publish'
2020
id 'signing'
21+
id "me.champeau.jmh" version "0.6.6"
2122
}
2223

2324
group "software.amazon.cloudwatchlogs"
@@ -78,6 +79,10 @@ dependencies {
7879
testImplementation 'software.amazon.awssdk:cloudwatch:2.13.54'
7980
testCompileOnly 'org.projectlombok:lombok:1.18.12'
8081
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
82+
83+
implementation 'org.openjdk.jmh:jmh-core:1.29'
84+
implementation 'org.openjdk.jmh:jmh-generator-annprocess:1.29'
85+
jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.29'
8186
}
8287

8388
spotless {
@@ -124,7 +129,7 @@ tasks.withType(JavaCompile) {
124129
}
125130

126131
tasks.named('wrapper') {
127-
gradleVersion = '6.5.1'
132+
gradleVersion = '7.4.2'
128133
distributionType = Wrapper.DistributionType.ALL
129134
}
130135

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)