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 all commits
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,63 @@
/*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.suro.sink.localfile;

import org.joda.time.DateTime;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Created by [email protected] on 10/20/14.
*/
public class TestGranularity {

@Test
public void testNextHour(){
DateTime dateTime = new DateTime("2014-10-18T15:48:55.000+08:00");
DateTime expected = new DateTime("2014-10-18T16:00:00.000+08:00");
assertEquals(Granularity.HOUR.next(dateTime),expected);

dateTime = new DateTime("2014-10-18T23:48:55.000+08:00");
expected = new DateTime("2014-10-19T00:00:00.000+08:00");
assertEquals(Granularity.HOUR.next(dateTime),expected);
}

@Test
public void testNextDay(){
DateTime dateTime = new DateTime("2014-10-18T15:48:55.000+08:00");
DateTime expected = new DateTime("2014-10-19T00:00:00.000+08:00");
assertEquals(Granularity.DAY.next(dateTime),expected);

dateTime = new DateTime("2014-10-18T00:00:00.000+08:00");
expected = new DateTime("2014-10-19T00:00:00.000+08:00");
assertEquals(Granularity.DAY.next(dateTime),expected);
}

}