From b26e3742ac6ed09134e7d6515412a57be5e525cc Mon Sep 17 00:00:00 2001 From: Carlos Miranda Date: Wed, 11 Jun 2014 17:27:47 +0800 Subject: [PATCH] #173 Refactored DefaultResource to use DomainStatsData --- .../java/com/s3auth/hosts/DefaultHost.java | 5 +- .../com/s3auth/hosts/DefaultResource.java | 31 +++--------- .../com/s3auth/hosts/H2DomainStatsData.java | 30 ++---------- .../src/main/java/com/s3auth/hosts/Stats.java | 25 ++++++++++ .../com/s3auth/hosts/DefaultResourceTest.java | 47 ++++++------------- 5 files changed, 53 insertions(+), 85 deletions(-) diff --git a/s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultHost.java b/s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultHost.java index 2bd61f6d..6519281d 100644 --- a/s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultHost.java +++ b/s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultHost.java @@ -159,6 +159,7 @@ public Resource fetch(@NotNull final URI uri, throws IOException { Resource resource = null; final Collection errors = new LinkedList(); + final DomainStatsData data = new H2DomainStatsData(); for (final DefaultHost.ObjectName name : this.names(uri)) { try { if (version.list()) { @@ -168,7 +169,7 @@ public Resource fetch(@NotNull final URI uri, } else { resource = new DefaultResource( this.bucket.client(), this.bucket.bucket(), - name.get(), range, version, this.cloudwatch.get() + name.get(), range, version, data ); } break; @@ -202,7 +203,7 @@ public Resource fetch(@NotNull final URI uri, resource = new DefaultResource( this.bucket.client(), this.bucket.bucket(), config.getErrorDocument(), Range.ENTIRE, - Version.LATEST, this.cloudwatch.get() + Version.LATEST, data ); } } catch (final AmazonClientException exc) { diff --git a/s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultResource.java b/s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultResource.java index 51abc8ed..4e47f7be 100644 --- a/s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultResource.java +++ b/s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultResource.java @@ -29,11 +29,6 @@ */ package com.s3auth.hosts; -import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient; -import com.amazonaws.services.cloudwatch.model.Dimension; -import com.amazonaws.services.cloudwatch.model.MetricDatum; -import com.amazonaws.services.cloudwatch.model.PutMetricDataRequest; -import com.amazonaws.services.cloudwatch.model.StandardUnit; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.ObjectMetadata; @@ -95,9 +90,9 @@ final class DefaultResource implements Resource { private final transient S3Object object; /** - * Amazon Cloudwatch Client. + * Domain Stats. */ - private final transient AmazonCloudWatchClient cloudwatch; + private final transient DomainStatsData stats; /** * Public ctor. @@ -106,13 +101,13 @@ final class DefaultResource implements Resource { * @param name Key name * @param rng Range to deliver * @param ver Version of object to retrieve - * @param cwatch Amazon Cloudwatch Client + * @param dstats Domain stats data * @checkstyle ParameterNumber (5 lines) */ DefaultResource(@NotNull final AmazonS3 clnt, @NotNull final String bckt, @NotNull final String name, @NotNull final Range rng, @NotNull final Version ver, - @NotNull final AmazonCloudWatchClient cwatch) { + @NotNull final DomainStatsData dstats) { this.client = clnt; this.bucket = bckt; this.key = name; @@ -121,7 +116,7 @@ final class DefaultResource implements Resource { this.object = this.client.getObject( this.request(this.range, this.version) ); - this.cloudwatch = cwatch; + this.stats = dstats; } @Override @@ -189,21 +184,7 @@ public long writeTo(@NotNull final OutputStream output) throws IOException { } total += count; } - this.cloudwatch.putMetricData( - new PutMetricDataRequest() - .withNamespace("S3Auth") - .withMetricData( - new MetricDatum() - .withMetricName("BytesTransferred") - .withDimensions( - new Dimension() - .withName("Bucket") - .withValue(this.bucket) - ) - .withUnit(StandardUnit.Bytes) - .withValue((double) total) - ) - ); + this.stats.put(this.bucket, new Stats.Simple(total)); } finally { input.close(); } diff --git a/s3auth-hosts/src/main/java/com/s3auth/hosts/H2DomainStatsData.java b/s3auth-hosts/src/main/java/com/s3auth/hosts/H2DomainStatsData.java index 7df70e5d..d7f19ff1 100644 --- a/s3auth-hosts/src/main/java/com/s3auth/hosts/H2DomainStatsData.java +++ b/s3auth-hosts/src/main/java/com/s3auth/hosts/H2DomainStatsData.java @@ -82,7 +82,7 @@ final class H2DomainStatsData implements DomainStatsData { public Stats handle(final ResultSet rset, final Statement stmt) throws SQLException { rset.next(); - return new Simple(rset.getLong(1)); + return new Stats.Simple(rset.getLong(1)); } }; @@ -97,7 +97,9 @@ public Map handle(final ResultSet rset, final Statement stmt) throws SQLException { final Map stats = new HashMap(); while (rset.next()) { - stats.put(rset.getString(1), new Simple(rset.getLong(2))); + stats.put( + rset.getString(1), new Stats.Simple(rset.getLong(2)) + ); } return stats; } @@ -175,28 +177,4 @@ public Map all() throws IOException { private Connection connection() throws SQLException { return new Driver().connect(this.jdbc, new Properties()); } - - /** - * Simple stats. - */ - @Immutable - private static final class Simple implements Stats { - /** - * Bytes transferred. - */ - private final transient long bytes; - - /** - * Ctor. - * @param transferred Number of bytes transferred. - */ - public Simple(final long transferred) { - this.bytes = transferred; - } - - @Override - public long bytesTransferred() { - return this.bytes; - } - } } diff --git a/s3auth-hosts/src/main/java/com/s3auth/hosts/Stats.java b/s3auth-hosts/src/main/java/com/s3auth/hosts/Stats.java index 1c24d785..e7945218 100644 --- a/s3auth-hosts/src/main/java/com/s3auth/hosts/Stats.java +++ b/s3auth-hosts/src/main/java/com/s3auth/hosts/Stats.java @@ -30,6 +30,7 @@ package com.s3auth.hosts; import com.jcabi.aspects.Immutable; +import lombok.EqualsAndHashCode; /** * Statistics for a given domain. @@ -46,4 +47,28 @@ public interface Stats { */ long bytesTransferred(); + /** + * Simple stats. + */ + @Immutable + @EqualsAndHashCode(of = "bytes") + final class Simple implements Stats { + /** + * Bytes transferred. + */ + private final transient long bytes; + + /** + * Ctor. + * @param transferred Number of bytes transferred. + */ + public Simple(final long transferred) { + this.bytes = transferred; + } + + @Override + public long bytesTransferred() { + return this.bytes; + } + } } diff --git a/s3auth-hosts/src/test/java/com/s3auth/hosts/DefaultResourceTest.java b/s3auth-hosts/src/test/java/com/s3auth/hosts/DefaultResourceTest.java index dface97e..9de98e53 100644 --- a/s3auth-hosts/src/test/java/com/s3auth/hosts/DefaultResourceTest.java +++ b/s3auth-hosts/src/test/java/com/s3auth/hosts/DefaultResourceTest.java @@ -29,11 +29,6 @@ */ package com.s3auth.hosts; -import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient; -import com.amazonaws.services.cloudwatch.model.Dimension; -import com.amazonaws.services.cloudwatch.model.MetricDatum; -import com.amazonaws.services.cloudwatch.model.PutMetricDataRequest; -import com.amazonaws.services.cloudwatch.model.StandardUnit; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.ObjectMetadata; @@ -74,7 +69,7 @@ public void getsHeadersFromAmazonObject() throws Exception { Mockito.doReturn(1L).when(meta).getContentLength(); final Resource res = new DefaultResource( client, "a", "", Range.ENTIRE, Version.LATEST, - Mockito.mock(AmazonCloudWatchClient.class) + Mockito.mock(DomainStatsData.class) ); MatcherAssert.assertThat( res.headers(), @@ -100,7 +95,7 @@ public void writesFromAmazonObjectToOutputStream() throws Exception { ResourceMocker.toString( new DefaultResource( client, "b", "", Range.ENTIRE, Version.LATEST, - Mockito.mock(AmazonCloudWatchClient.class) + Mockito.mock(DomainStatsData.class) ) ), Matchers.equalTo("") @@ -132,7 +127,7 @@ public void writesInputToOutputStream() throws Exception { ResourceMocker.toByteArray( new DefaultResource( client, "c", "", Range.ENTIRE, Version.LATEST, - Mockito.mock(AmazonCloudWatchClient.class) + Mockito.mock(DomainStatsData.class) ) ), Matchers.equalTo(data) @@ -158,7 +153,7 @@ public void throwsWhenFailedToRead() throws Exception { ResourceMocker.toString( new DefaultResource( client, "d", "", Range.ENTIRE, Version.LATEST, - Mockito.mock(AmazonCloudWatchClient.class) + Mockito.mock(DomainStatsData.class) ) ), Matchers.equalTo("") @@ -181,7 +176,7 @@ public void getsLastModifiedDate() throws Exception { Mockito.doReturn(date).when(meta).getLastModified(); final Resource res = new DefaultResource( client, "x", "", Range.ENTIRE, Version.LATEST, - Mockito.mock(AmazonCloudWatchClient.class) + Mockito.mock(DomainStatsData.class) ); MatcherAssert.assertThat( res.lastModified(), @@ -204,7 +199,7 @@ public void getsCacheControlHeaderFromAmazonObject() throws Exception { Mockito.doReturn("max-age: 600, public").when(meta).getCacheControl(); final Resource res = new DefaultResource( client, "e", "", Range.ENTIRE, Version.LATEST, - Mockito.mock(AmazonCloudWatchClient.class) + Mockito.mock(DomainStatsData.class) ); MatcherAssert.assertThat( res.headers(), @@ -228,7 +223,7 @@ public void getsDefaultCacheControlHeader() throws Exception { Mockito.doReturn(null).when(meta).getCacheControl(); final Resource res = new DefaultResource( client, "f", "", Range.ENTIRE, Version.LATEST, - Mockito.mock(AmazonCloudWatchClient.class) + Mockito.mock(DomainStatsData.class) ); MatcherAssert.assertThat( res.headers(), @@ -237,11 +232,11 @@ public void getsDefaultCacheControlHeader() throws Exception { } /** - * DefaultResource can post metrics to Amazon CloudWatch. + * DefaultResource can post metrics. * @throws Exception If there is some problem inside */ @Test - public void postsCloudWatchMetricData() throws Exception { + public void postsMetricData() throws Exception { final int size = 100; final byte[] data = new byte[size]; final Random random = new Random(); @@ -257,30 +252,18 @@ public void postsCloudWatchMetricData() throws Exception { Mockito.doReturn(object).when(client) .getObject(Mockito.any(GetObjectRequest.class)); Mockito.doReturn(stream).when(object).getObjectContent(); - final AmazonCloudWatchClient cloudwatch = - Mockito.mock(AmazonCloudWatchClient.class); - final String bucket = "CloudWatchTest"; + final DomainStatsData stats = Mockito.mock(DomainStatsData.class); + final String bucket = "MetricsTest"; MatcherAssert.assertThat( ResourceMocker.toByteArray( new DefaultResource( - client, bucket, "", Range.ENTIRE, Version.LATEST, cloudwatch + client, bucket, "", Range.ENTIRE, Version.LATEST, stats ) ), Matchers.equalTo(data) ); - Mockito.verify(cloudwatch, Mockito.only()).putMetricData( - new PutMetricDataRequest() - .withNamespace("S3Auth") - .withMetricData( - new MetricDatum() - .withMetricName("BytesTransferred") - .withDimensions( - new Dimension() - .withName("Bucket") - .withValue(bucket) - ).withUnit(StandardUnit.Bytes) - .withValue(Double.valueOf(size)) - ) + Mockito.verify(stats, Mockito.only()).put( + bucket, new Stats.Simple(data.length) ); } @@ -307,7 +290,7 @@ public S3Object answer(final InvocationOnMock invocation) { ).when(client).getObject(Mockito.any(GetObjectRequest.class)); new DefaultResource( client, "h", "", Range.ENTIRE, new Version.Simple(version), - Mockito.mock(AmazonCloudWatchClient.class) + Mockito.mock(DomainStatsData.class) ); }