Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
7a6131c4
Commit
7a6131c4
authored
Sep 08, 2013
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make InMemoryMetricRepository.increment() thread safe
parent
eb246d6b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
15 deletions
+64
-15
InMemoryMetricRepository.java
...mework/boot/actuate/metrics/InMemoryMetricRepository.java
+15
-7
InMemoryMetricRepositoryTests.java
...k/boot/actuate/metrics/InMemoryMetricRepositoryTests.java
+49
-8
No files found.
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/InMemoryMetricRepository.java
View file @
7a6131c4
...
@@ -31,18 +31,26 @@ public class InMemoryMetricRepository implements MetricRepository {
...
@@ -31,18 +31,26 @@ public class InMemoryMetricRepository implements MetricRepository {
private
ConcurrentMap
<
String
,
Measurement
>
metrics
=
new
ConcurrentHashMap
<
String
,
Measurement
>();
private
ConcurrentMap
<
String
,
Measurement
>
metrics
=
new
ConcurrentHashMap
<
String
,
Measurement
>();
private
ConcurrentMap
<
String
,
Object
>
locks
=
new
ConcurrentHashMap
<
String
,
Object
>();
@Override
@Override
public
void
increment
(
String
metricName
,
int
amount
,
Date
timestamp
)
{
public
void
increment
(
String
metricName
,
int
amount
,
Date
timestamp
)
{
Measurement
current
=
this
.
metrics
.
get
(
metricName
);
Measurement
current
=
this
.
metrics
.
get
(
metricName
);
if
(
current
!=
null
)
{
if
(
current
!=
null
)
{
Metric
metric
=
current
.
getMetric
();
Object
lock
=
this
.
locks
.
putIfAbsent
(
metricName
,
new
Object
());
this
.
metrics
.
replace
(
metricName
,
current
,
if
(
lock
==
null
)
{
new
Measurement
(
timestamp
,
metric
.
increment
(
amount
)));
lock
=
this
.
locks
.
get
(
metricName
);
}
}
else
{
synchronized
(
lock
)
{
this
.
metrics
.
putIfAbsent
(
metricName
,
new
Measurement
(
timestamp
,
new
Metric
(
current
=
this
.
metrics
.
get
(
metricName
);
metricName
,
amount
)));
Metric
metric
=
current
.
getMetric
();
this
.
metrics
.
replace
(
metricName
,
current
,
new
Measurement
(
timestamp
,
metric
.
increment
(
amount
)));
return
;
}
}
}
this
.
metrics
.
putIfAbsent
(
metricName
,
new
Measurement
(
timestamp
,
new
Metric
(
metricName
,
amount
)));
}
}
@Override
@Override
...
...
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/InMemoryMetricRepositoryTests.java
View file @
7a6131c4
...
@@ -16,24 +16,65 @@
...
@@ -16,24 +16,65 @@
package
org
.
springframework
.
boot
.
actuate
.
metrics
;
package
org
.
springframework
.
boot
.
actuate
.
metrics
;
import
org.junit.Ignore
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
import
java.util.concurrent.TimeUnit
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.actuate.metrics.InMemoryMetricRepository
;
import
static
org
.
junit
.
Assert
.
fail
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
/**
* Tests for {@link InMemoryMetricRepository}.
* Tests for {@link InMemoryMetricRepository}.
*/
*/
@Ignore
public
class
InMemoryMetricRepositoryTests
{
public
class
InMemoryMetricRepositoryTests
{
// FIXME write tests
private
InMemoryMetricRepository
repository
=
new
InMemoryMetricRepository
();
// FIXME possibly also add Metric/Measurement tests
@Test
public
void
increment
()
{
this
.
repository
.
increment
(
"foo"
,
1
,
new
Date
());
assertEquals
(
1.0
,
this
.
repository
.
findOne
(
"foo"
).
getValue
(),
0.01
);
}
@Test
public
void
incrementConcurrent
()
throws
Exception
{
Collection
<
Callable
<
Boolean
>>
tasks
=
new
ArrayList
<
Callable
<
Boolean
>>();
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
tasks
.
add
(
new
Callable
<
Boolean
>()
{
@Override
public
Boolean
call
()
throws
Exception
{
InMemoryMetricRepositoryTests
.
this
.
repository
.
increment
(
"foo"
,
1
,
new
Date
());
return
true
;
}
});
tasks
.
add
(
new
Callable
<
Boolean
>()
{
@Override
public
Boolean
call
()
throws
Exception
{
InMemoryMetricRepositoryTests
.
this
.
repository
.
increment
(
"foo"
,
-
1
,
new
Date
());
return
true
;
}
});
}
List
<
Future
<
Boolean
>>
all
=
Executors
.
newFixedThreadPool
(
10
).
invokeAll
(
tasks
);
for
(
Future
<
Boolean
>
future
:
all
)
{
assertTrue
(
future
.
get
(
1
,
TimeUnit
.
SECONDS
));
}
assertEquals
(
0
,
this
.
repository
.
findOne
(
"foo"
).
getValue
(),
0.01
);
}
@Test
@Test
public
void
test
()
{
public
void
set
()
{
fail
(
"Not yet implemented"
);
this
.
repository
.
set
(
"foo"
,
1
,
new
Date
());
assertEquals
(
1.0
,
this
.
repository
.
findOne
(
"foo"
).
getValue
(),
0.01
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment