-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from carlosmiranda/173
- Loading branch information
Showing
8 changed files
with
470 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) | ||
|
@@ -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. | ||
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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(); | ||
} | ||
|
68 changes: 68 additions & 0 deletions
68
s3auth-hosts/src/main/java/com/s3auth/hosts/DomainStatsData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
Oops, something went wrong.