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

[auto_discovery] process templates larger than the page buffer size #145

Merged
merged 5 commits into from
Jul 11, 2017
Merged
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
41 changes: 33 additions & 8 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.InputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
Expand All @@ -32,6 +33,7 @@

import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import com.google.common.primitives.Bytes;


@SuppressWarnings("unchecked")
Expand All @@ -41,6 +43,8 @@ public class App {
public static final String CANNOT_CONNECT_TO_INSTANCE = "Cannot connect to instance ";
private static final String AD_CONFIG_SEP = "#### AUTO-DISCOVERY ####";
private static final String AD_LEGACY_CONFIG_SEP = "#### SERVICE-DISCOVERY ####";
private static final String AD_CONFIG_TERM = "#### AUTO-DISCOVERY TERM ####";
private static final String AD_LEGACY_CONFIG_TERM = "#### SERVICE-DISCOVERY TERM ####";
private static int loopCounter;
private AtomicBoolean reinit = new AtomicBoolean(false);
private ConcurrentHashMap<String, YamlParser> configs;
Expand Down Expand Up @@ -170,11 +174,12 @@ private String getAutoDiscoveryName(String config){

private FileInputStream newAutoDiscoveryPipe() {
FileInputStream adPipe = null;
String pipeName = appConfig.getAutoDiscoveryPipe();
try {
adPipe = new FileInputStream(appConfig.getAutoDiscoveryPipe()); //Should we use RandomAccessFile?
LOGGER.info("Named pipe for Auto-Discovery opened");
adPipe = new FileInputStream(pipeName);
LOGGER.info("Named pipe for Auto-Discovery opened: " + pipeName);
} catch (FileNotFoundException e) {
LOGGER.info("Unable to open named pipe for Auto-Discovery.");
LOGGER.info("Unable to open named pipe for Auto-Discovery: " + pipeName);
}

return adPipe;
Expand Down Expand Up @@ -244,14 +249,34 @@ void start() {
adPipe = newAutoDiscoveryPipe();
}
try {
int len;
if(adPipe != null && (len = adPipe.available()) > 0) {
byte[] buffer = new byte[len];
adPipe.read(buffer);
if(adPipe != null && adPipe.available() > 0) {
byte[] buffer = new byte[0];
boolean terminated = false;
while (!terminated) {
int len = adPipe.available();
if (len > 0) {
byte[] minibuff = new byte[len];
adPipe.read(minibuff);

// The separator always comes in its own atomic write() from the agent side -
// so it will never be chopped.
if (Bytes.indexOf(minibuff, App.AD_LEGACY_CONFIG_TERM.getBytes()) > -1 ||
Bytes.indexOf(minibuff, App.AD_CONFIG_TERM.getBytes()) > -1 ) {
terminated = true;
}

// make room for read chunk
int oldLen = buffer.length;
buffer = Arrays.copyOf(buffer, buffer.length + len);
System.arraycopy(minibuff, 0, buffer, oldLen, len);
}
}
setReinit(processAutoDiscovery(buffer));
}
} catch(IOException e) {
LOGGER.warn("Unable to read from pipe - Service Discovery configuration may have been skipped.");
} catch(Exception e) {
LOGGER.warn("Unknown problem parsing auto-discovery configuration: " + e);
}

long start = System.currentTimeMillis();
Expand Down Expand Up @@ -298,7 +323,7 @@ public void doIteration() {
LOGGER.debug("it is not time to collect, skipping run for " + instance.getName());
continue;
}

metrics = instance.getMetrics();
numberOfMetrics = metrics.size();

Expand Down