Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support truncate suro file hourly/daily #146

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.netflix.suro.sink.localfile;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need a copy right.


import org.joda.time.*;

/**
* Created by [email protected] on 9/30/14.
*/
public enum Granularity {

HOUR{
@Override
public DateTime truncate(DateTime time) {
final MutableDateTime mutableDateTime = time.toMutableDateTime();
mutableDateTime.setMillisOfSecond(0);
mutableDateTime.setSecondOfMinute(0);
mutableDateTime.setMinuteOfHour(0);
return mutableDateTime.toDateTime();
}

@Override
public ReadablePeriod getUnits(int n) {
return Hours.hours(n);
}
} ,
DAY{
@Override
public DateTime truncate(DateTime time) {
final MutableDateTime mutableDateTime = time.toMutableDateTime();
mutableDateTime.setMillisOfDay(0);
return mutableDateTime.toDateTime();
}

@Override
public ReadablePeriod getUnits(int n) {
return Days.days(n);
}
};

public abstract DateTime truncate(DateTime time);
public abstract ReadablePeriod getUnits(int n);

public final DateTime next(DateTime time){
return truncate(time.plus(getUnits(1)));
}


}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a unit test case for this class?

Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public class LocalFileSink extends QueuedSink implements Sink {

private boolean messageWrittenInRotation = false;

private Granularity granularity;

@JsonCreator
public LocalFileSink(
@JsonProperty("outputDir") String outputDir,
Expand All @@ -97,6 +99,7 @@ public LocalFileSink(
@JsonProperty("queue4Sink") MessageQueue4Sink queue4Sink,
@JsonProperty("batchSize") int batchSize,
@JsonProperty("batchTimeout") int batchTimeout,
@JsonProperty("granularity") String gran,
@JacksonInject TrafficController trafficController,
@JacksonInject SpaceChecker spaceChecker) {
if (!outputDir.endsWith("/")) {
Expand All @@ -113,6 +116,10 @@ public LocalFileSink(
this.trafficController = trafficController;
this.spaceChecker = spaceChecker;

if(gran!=null){
granularity = Granularity.valueOf(gran.toUpperCase());
}

Monitors.registerObject(outputDir.replace('/', '_'), this);
initialize("localfile_" + outputDir.replace('/', '_'),
queue4Sink == null ? new MemoryQueue4Sink(10000) : queue4Sink, batchSize, batchTimeout);
Expand Down Expand Up @@ -189,7 +196,14 @@ private void rotate() throws IOException {

filePath = newName;

nextRotation = new DateTime().plus(rotationPeriod).getMillis();
DateTime currentDateTime = new DateTime();
nextRotation = currentDateTime.plus(rotationPeriod).getMillis();
if(granularity!=null){
long expectedBreak = granularity.next(currentDateTime).getMillis();
if(nextRotation >expectedBreak){
nextRotation = expectedBreak;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add this test case for this particular fix in TestLocalFileSink?


if (!spaceChecker.hasEnoughSpace()) {
trafficController.stopTakingTraffic();
Expand Down