Skip to content

Commit

Permalink
Merge pull request yegor256#187 from carlosmiranda/186
Browse files Browse the repository at this point in the history
yegor256#187: pull request yegor256#186 Cached retrieval of CloudWatch stats
  • Loading branch information
Guard committed Jul 2, 2014
2 parents b8d3a86 + ee865f5 commit 5ce592b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
28 changes: 21 additions & 7 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public AmazonCloudWatchClient get() {
*/
private final transient Htpasswd htpasswd;

/**
* Holder of host stats.
*/
private final transient Stats statistics;

/**
* Amazon Cloudwatch Client.
*/
Expand Down Expand Up @@ -136,6 +141,7 @@ public AmazonCloudWatchClient get() {
this.bucket = bckt;
this.htpasswd = new Htpasswd(this);
this.cloudwatch = cwatch;
this.statistics = new HostStats(this.bucket.bucket());
}

@Override
Expand Down Expand Up @@ -254,7 +260,7 @@ public String syslog() {

@Override
public Stats stats() {
return new HostStats();
return this.statistics;
}

/**
Expand Down Expand Up @@ -348,15 +354,23 @@ public String toString() {

/**
* Stats for this domain.
*
* @todo #173 We should be caching the results of this method for a short
* period somehow, or at least prevent unnecessary repeated requests to
* Amazon CloudWatch API. This is so that we can improve performance and
* reduce access costs.
*/
@Loggable(Loggable.DEBUG)
@EqualsAndHashCode(of = "bucket")
private final class HostStats implements Stats {
/**
* The S3 bucket.
*/
private final transient String bucket;
/**
* Public ctor.
* @param bckt The name of the bucket
*/
public HostStats(final String bckt) {
this.bucket = bckt;
}
@Override
@Cacheable(lifetime = Tv.THIRTY, unit = TimeUnit.MINUTES)
public long bytesTransferred() {
final Date now = new Date();
final List<Datapoint> datapoints =
Expand All @@ -368,7 +382,7 @@ public long bytesTransferred() {
.withDimensions(
new Dimension()
.withName("Bucket")
.withValue(DefaultHost.this.bucket.bucket())
.withValue(this.bucket)
)
.withUnit(StandardUnit.Bytes)
.withPeriod((int) TimeUnit.DAYS.toSeconds(Tv.SEVEN))
Expand Down
17 changes: 14 additions & 3 deletions s3auth-hosts/src/test/java/com/s3auth/hosts/DefaultHostTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ public void showsVersionListingForIndexHtml() throws Exception {
}

/**
* DefaultHost can retrieve Cloudwatch stats.
* DefaultHost can retrieve Cloudwatch stats and cache the results.
*/
@Test
public void retrievesCloudWatchStats() {
public void retrievesAndCachesCloudWatchStats() {
final long sum = 10;
final CloudWatch cloudwatch = this.cloudWatch();
final GetMetricStatisticsResult result = new GetMetricStatisticsResult()
Expand All @@ -304,7 +304,18 @@ public void retrievesCloudWatchStats() {
new BucketMocker().mock(),
cloudwatch
).stats().bytesTransferred(),
Matchers.is(sum)
Matchers.allOf(
Matchers.is(sum),
Matchers.is(
new DefaultHost(
new BucketMocker().mock(),
cloudwatch
).stats().bytesTransferred()
)
)
);
Mockito.verify(cloudwatch.get(), Mockito.times(1)).getMetricStatistics(
Mockito.any(GetMetricStatisticsRequest.class)
);
}

Expand Down

0 comments on commit 5ce592b

Please sign in to comment.