Skip to content

Commit 6aa5f65

Browse files
vjkoskelaBrandonArp
authored andcommitted
Remove commons io usage and dependency. (#88)
* Remove commons io usage and dependency. * Inverted while and try-with-resources surrounding the call to fileLoop.
1 parent c914311 commit 6aa5f65

File tree

4 files changed

+25
-32
lines changed

4 files changed

+25
-32
lines changed

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
<aspectjrt.version>1.8.9</aspectjrt.version>
8080
<cglib.version>3.2.5</cglib.version>
8181
<client.protocol.version>0.10.0</client.protocol.version>
82-
<commons.io.version>2.4</commons.io.version>
8382
<fastutil.version>8.1.1</fastutil.version>
8483
<findbugs.annotations.version>3.0.1</findbugs.annotations.version>
8584
<guava.version>23.4-jre</guava.version>
@@ -597,11 +596,6 @@
597596
<artifactId>fastutil</artifactId>
598597
<version>${fastutil.version}</version>
599598
</dependency>
600-
<dependency>
601-
<groupId>commons-io</groupId>
602-
<artifactId>commons-io</artifactId>
603-
<version>${commons.io.version}</version>
604-
</dependency>
605599
<dependency>
606600
<groupId>org.reflections</groupId>
607601
<artifactId>reflections</artifactId>

src/main/java/com/arpnetworking/metrics/common/sources/FileSource.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.joda.time.Duration;
3535
import org.joda.time.Period;
3636

37+
import java.io.IOException;
3738
import java.nio.file.Path;
3839
import java.util.Optional;
3940
import java.util.concurrent.ExecutorService;
@@ -67,6 +68,11 @@ public void stop() {
6768
.setThrowable(e)
6869
.log();
6970
}
71+
try {
72+
_positionStore.close();
73+
} catch (final IOException e) {
74+
// Ignore close exception.
75+
}
7076
}
7177

7278
/**
@@ -98,23 +104,23 @@ private FileSource(final Builder<T> builder) {
98104
super(builder);
99105
_logger = logger;
100106
_parser = builder._parser;
101-
final PositionStore positionStore;
102107
if (builder._stateFile == null) {
103-
positionStore = NO_POSITION_STORE;
108+
_positionStore = NO_POSITION_STORE;
104109
} else {
105-
positionStore = new FilePositionStore.Builder().setFile(builder._stateFile).build();
110+
_positionStore = new FilePositionStore.Builder().setFile(builder._stateFile).build();
106111
}
107112

108113
_tailer = new StatefulTailer.Builder()
109114
.setFile(builder._sourceFile)
110115
.setListener(new LogTailerListener())
111116
.setReadInterval(builder._interval)
112-
.setPositionStore(positionStore)
117+
.setPositionStore(_positionStore)
113118
.setInitialPosition(builder._initialPosition)
114119
.build();
115120
_tailerExecutor = Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "FileSourceTailer"));
116121
}
117122

123+
private final PositionStore _positionStore;
118124
private final Parser<T, byte[]> _parser;
119125
private final Tailer _tailer;
120126
private final ExecutorService _tailerExecutor;

src/main/java/com/arpnetworking/metrics/common/tailer/StatefulTailer.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.arpnetworking.utility.Trigger;
2525
import com.google.common.base.MoreObjects;
2626
import net.sf.oval.constraint.NotNull;
27-
import org.apache.commons.io.IOUtils;
2827
import org.joda.time.Duration;
2928

3029
import java.io.ByteArrayOutputStream;
@@ -68,11 +67,14 @@ public void run() {
6867
.setThrowable(throwable)
6968
.log());
7069

71-
try {
72-
fileLoop();
73-
} finally {
74-
IOUtils.closeQuietly(_positionStore);
75-
IOUtils.closeQuietly(_lineBuffer);
70+
while (_isRunning) {
71+
try (ByteArrayOutputStream lineBuffer = new ByteArrayOutputStream(INITIAL_BUFFER_SIZE)) {
72+
_lineBuffer = lineBuffer;
73+
fileLoop();
74+
} catch (final IOException e) {
75+
// Ignoring exception from closing line buffer because it's not a
76+
// buffered output stream (e.g. nothing to flush).
77+
}
7678
}
7779
}
7880

@@ -107,23 +109,16 @@ protected boolean isRunning() {
107109
}
108110

109111
private void fileLoop() {
110-
SeekableByteChannel reader = null;
111112
InitialPosition nextInitialPosition = _initialPosition;
112113
try {
113114
while (isRunning()) {
114115
// Attempt to open the file
115-
try {
116-
reader = Files.newByteChannel(_file, StandardOpenOption.READ);
116+
try (SeekableByteChannel reader = Files.newByteChannel(_file, StandardOpenOption.READ)) {
117117
LOGGER.trace()
118118
.setMessage("Opened file")
119119
.addData("file", _file)
120120
.log();
121-
} catch (final NoSuchFileException e) {
122-
_listener.fileNotFound();
123-
_trigger.waitOnTrigger();
124-
}
125121

126-
if (reader != null) {
127122
// Position the reader
128123
resume(reader, nextInitialPosition);
129124
_listener.fileOpened();
@@ -135,9 +130,10 @@ private void fileLoop() {
135130
readLoop(reader);
136131

137132
// Reset per file state
138-
IOUtils.closeQuietly(reader);
139-
reader = null;
140133
_hash = Optional.empty();
134+
} catch (final NoSuchFileException e) {
135+
_listener.fileNotFound();
136+
_trigger.waitOnTrigger();
141137
}
142138
}
143139
// Clients may elect to kill the stateful tailer on an exception by calling stop, or they
@@ -153,8 +149,6 @@ private void fileLoop() {
153149
// CHECKSTYLE.ON: IllegalCatch
154150
handleThrowable(e);
155151
} finally {
156-
IOUtils.closeQuietly(reader);
157-
reader = null;
158152
_hash = Optional.empty();
159153
}
160154
}
@@ -515,7 +509,6 @@ private void handleThrowable(final Throwable t) {
515509
_trigger = trigger;
516510

517511
_buffer = ByteBuffer.allocate(INITIAL_BUFFER_SIZE);
518-
_lineBuffer = new ByteArrayOutputStream(INITIAL_BUFFER_SIZE);
519512
try {
520513
_md5 = MessageDigest.getInstance("MD5");
521514
} catch (final NoSuchAlgorithmException e) {
@@ -536,14 +529,14 @@ private StatefulTailer(final Builder builder) {
536529
private final PositionStore _positionStore;
537530
private final TailerListener _listener;
538531
private final ByteBuffer _buffer;
539-
private final ByteArrayOutputStream _lineBuffer;
540532
private final MessageDigest _md5;
541533
private final InitialPosition _initialPosition;
542534
private final Optional<Long> _maximumOffsetOnResume;
543535
private final Trigger _trigger;
544536

545537
private volatile boolean _isRunning = true;
546538
private Optional<String> _hash = Optional.empty();
539+
private ByteArrayOutputStream _lineBuffer;
547540

548541
private static final int REQUIRED_BYTES_FOR_HASH = 512;
549542
private static final int INITIAL_BUFFER_SIZE = 65536;

src/test/java/com/arpnetworking/metrics/mad/performance/CollectdPipelinePT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
2020
import com.carrotsearch.junitbenchmarks.BenchmarkRule;
2121
import com.google.common.collect.ImmutableMap;
22+
import com.google.common.io.ByteStreams;
2223
import com.google.common.io.Resources;
23-
import org.apache.commons.io.IOUtils;
2424
import org.joda.time.Duration;
2525
import org.junit.BeforeClass;
2626
import org.junit.Rule;
@@ -52,7 +52,7 @@ public static void setUp() throws IOException, URISyntaxException {
5252
final Path path = Paths.get("target/tmp/perf/collectd-sample1.log");
5353
final FileOutputStream outputStream = new FileOutputStream(path.toFile());
5454

55-
IOUtils.copy(gzipInputStream, outputStream);
55+
ByteStreams.copy(gzipInputStream, outputStream);
5656

5757
JSON_BENCHMARK_CONSUMER.prepareClass();
5858
}

0 commit comments

Comments
 (0)