Skip to content

Commit

Permalink
Merge pull request #177 from carlosmiranda/173
Browse files Browse the repository at this point in the history
#177: pull request #173 Post metrics to local database to minimize CloudWatch API accesses
  • Loading branch information
Guard committed Jun 13, 2014
2 parents f3765e4 + b548f86 commit 3a6c405
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 62 deletions.
10 changes: 10 additions & 0 deletions s3auth-hosts/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@
<groupId>com.jcabi</groupId>
<artifactId>jcabi-xml</artifactId>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-jdbc</artifactId>
<version>0.12.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.176</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
16 changes: 11 additions & 5 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,17 +169,17 @@ 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;
} catch (final AmazonServiceException ex) {
if (StringUtils.endsWith(name.get(), SUFFIX)
if (StringUtils.endsWith(name.get(), DefaultHost.SUFFIX)
&& "NoSuchKey".equals(ex.getErrorCode())
) {
resource = new DirectoryListing(
this.bucket.client(), this.bucket.bucket(),
StringUtils.removeEnd(name.get(), SUFFIX)
StringUtils.removeEnd(name.get(), DefaultHost.SUFFIX)
);
break;
} else if ("NoSuchBucket".equals(ex.getErrorCode())) {
Expand All @@ -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 Expand Up @@ -308,7 +309,7 @@ public String get() {
suffix = "";
}
if (suffix == null || suffix.isEmpty()) {
suffix = SUFFIX;
suffix = DefaultHost.SUFFIX;
}
final StringBuilder text = new StringBuilder(this.origin);
if (text.length() > 0) {
Expand Down Expand Up @@ -351,6 +352,11 @@ 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)
private final class HostStats implements Stats {
Expand Down
39 changes: 14 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 All @@ -59,6 +54,14 @@
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.0.1
* @todo #173 Due to the large cost of having very many small CloudWatch
* requests, we changed the implementation of DefaultResource so that it now
* retains information to a local H2 database instead of directly making
* CloudWatch API requests. However, we still don't have a way to post it to
* CloudWatch. Let's create a way to do this, such as a cron job that runs
* every hour. It should post aggregated traffic metrics to Amazon CloudWatch
* per domain, and also perform cleanup of old data after it manages to post
* the information.
*/
@EqualsAndHashCode(of = { "bucket", "key", "range" })
@Loggable(Loggable.DEBUG)
Expand Down Expand Up @@ -95,9 +98,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 +109,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 +124,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 +192,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
68 changes: 68 additions & 0 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/DomainStatsData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) 2012, s3auth.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the s3auth.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.s3auth.hosts;

import com.jcabi.aspects.Immutable;
import java.io.IOException;
import java.util.Map;

/**
* Store of {@link Stats} per domain.
*
* @author Carlos Miranda ([email protected])
* @version $Id$
*/
@Immutable
interface DomainStatsData {

/**
* Post the statistics of the given domain, for this particular time.
* @param domain The domain of this stats.
* @param stats The stats to keep.
* @throws IOException If something goes wrong.
*/
void put(String domain, Stats stats) throws IOException;

/**
* Get the stats for the given domain.
* @param domain The domain whose stats we're interested in
* @return The stats for this domain
* @throws IOException If something goes wrong.
*/
Stats get(String domain) throws IOException;

/**
* Get the stats for all domains.
* @return Map of each domain and their corresponding stats.
* @throws IOException If something goes wrong.
*/
Map<String, Stats> all() throws IOException;

}
Loading

0 comments on commit 3a6c405

Please sign in to comment.