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

FLUME-3060 Fix race condition in flume-taildir-source #369

Open
wants to merge 1 commit into
base: trunk
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
Expand Up @@ -22,7 +22,6 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Table;
import com.google.gson.stream.JsonReader;
import org.apache.flume.Event;
Expand All @@ -43,6 +42,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

@InterfaceAudience.Private
@InterfaceStability.Evolving
Expand All @@ -53,7 +53,7 @@ public class ReliableTaildirEventReader implements ReliableEventReader {
private final Table<String, String, String> headerTable;

private TailFile currentFile = null;
private Map<Long, TailFile> tailFiles = Maps.newHashMap();
private Map<Long, TailFile> tailFiles = new ConcurrentHashMap<>();
private long updateTime;
private boolean addByteOffset;
private boolean cachePatternMatching;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -81,7 +82,7 @@ public class TaildirSource extends AbstractSource implements
private int writePosInterval;
private boolean cachePatternMatching;

private List<Long> existingInodes = new CopyOnWriteArrayList<Long>();
private List<Long> existingInodes = new ArrayList<>();
private List<Long> idleInodes = new CopyOnWriteArrayList<Long>();
private Long backoffSleepIncrement;
private Long maxBackOffSleepInterval;
Expand Down Expand Up @@ -231,9 +232,12 @@ protected SourceCounter getSourceCounter() {
public Status process() {
Status status = Status.BACKOFF;
try {
existingInodes.clear();
existingInodes.addAll(reader.updateTailFiles());
for (long inode : existingInodes) {
final List<Long> updatedInodes = reader.updateTailFiles();
synchronized (existingInodes) {
existingInodes.clear();
existingInodes.addAll(updatedInodes);
}
for (long inode : updatedInodes) {
TailFile tf = reader.getTailFiles().get(inode);
if (tf.needTail()) {
boolean hasMoreLines = tailFileProcess(tf, true);
Expand Down Expand Up @@ -304,10 +308,10 @@ private void closeTailFiles() throws IOException, InterruptedException {
if (tf.getRaf() != null) { // when file has not closed yet
tailFileProcess(tf, false);
tf.close();
idleInodes.remove(inode);
logger.info("Closed file: " + tf.getPath() + ", inode: " + inode + ", pos: " + tf.getPos());
}
}
idleInodes.clear();
}

/**
Expand Down Expand Up @@ -346,10 +350,8 @@ private void writePosition() {
FileWriter writer = null;
try {
writer = new FileWriter(file);
if (!existingInodes.isEmpty()) {
String json = toPosInfoJson();
writer.write(json);
}
String json = toPosInfoJson();
writer.write(json);
} catch (Throwable t) {
logger.error("Failed writing positionFile", t);
sourceCounter.incrementGenericProcessingFail();
Expand All @@ -366,9 +368,11 @@ private void writePosition() {
private String toPosInfoJson() {
@SuppressWarnings("rawtypes")
List<Map> posInfos = Lists.newArrayList();
for (Long inode : existingInodes) {
TailFile tf = reader.getTailFiles().get(inode);
posInfos.add(ImmutableMap.of("inode", inode, "pos", tf.getPos(), "file", tf.getPath()));
synchronized (existingInodes) {
for (long inode : existingInodes) {
TailFile tf = reader.getTailFiles().get(inode);
posInfos.add(ImmutableMap.of("inode", inode, "pos", tf.getPos(), "file", tf.getPath()));
}
}
return new Gson().toJson(posInfos);
}
Expand Down