Skip to content

Commit

Permalink
yegor256#173 Refactored DefaultResource to use DomainStatsData
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmiranda committed Jun 11, 2014
1 parent 9bbd786 commit b26e374
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 85 deletions.
5 changes: 3 additions & 2 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public Resource fetch(@NotNull final URI uri,
throws IOException {
Resource resource = null;
final Collection<String> errors = new LinkedList<String>();
final DomainStatsData data = new H2DomainStatsData();
for (final DefaultHost.ObjectName name : this.names(uri)) {
try {
if (version.list()) {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
31 changes: 6 additions & 25 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
30 changes: 4 additions & 26 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/H2DomainStatsData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};

Expand All @@ -97,7 +97,9 @@ public Map<String, Stats> handle(final ResultSet rset,
final Statement stmt) throws SQLException {
final Map<String, Stats> stats = new HashMap<String, Stats>();
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;
}
Expand Down Expand Up @@ -175,28 +177,4 @@ public Map<String, Stats> 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;
}
}
}
25 changes: 25 additions & 0 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/Stats.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package com.s3auth.hosts;

import com.jcabi.aspects.Immutable;
import lombok.EqualsAndHashCode;

/**
* Statistics for a given domain.
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -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("")
Expand Down Expand Up @@ -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)
Expand All @@ -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("")
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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();
Expand All @@ -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)
);
}

Expand All @@ -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)
);
}

Expand Down

0 comments on commit b26e374

Please sign in to comment.