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

[service_discovery] Fix race condition preventing SD initialization #135

Merged
merged 2 commits into from
Apr 18, 2017
Merged
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
37 changes: 24 additions & 13 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileFilter;
import java.io.FileNotFoundException;
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 Down Expand Up @@ -169,6 +167,17 @@ private String getSDName(String config){
return SERVICE_DISCOVERY_PREFIX + splitted[0].substring(2, splitted[0].length());
}

private FileInputStream newSdPipe() {
FileInputStream sdPipe = null;
try {
sdPipe = new FileInputStream(appConfig.getServiceDiscoveryPipe()); //Should we use RandomAccessFile?
LOGGER.info("Named pipe for Service Discovery opened");
} catch (FileNotFoundException e) {
LOGGER.info("Unable to open named pipe for Service Discovery.");
}
return sdPipe;
}

public boolean processServiceDiscovery(byte[] buffer) {
boolean reinit = false;
String[] discovered;
Expand Down Expand Up @@ -210,11 +219,9 @@ void start() {
long delta_s = 0;
FileInputStream sdPipe = null;

try {
sdPipe = new FileInputStream(appConfig.getServiceDiscoveryPipe()); //Should we use RandomAccessFile?
} catch (FileNotFoundException e) {
LOGGER.warn("Unable to open named pipe - Service Discovery disabled.");
sdPipe = null;
if(appConfig.getSDEnabled()) {
LOGGER.info("Service Discovery enabled");
sdPipe = newSdPipe();
}

while (true) {
Expand All @@ -225,13 +232,17 @@ void start() {
}

// any SD configs waiting in pipe?
if(sdPipe == null && appConfig.getSDEnabled()) {
// If SD is enabled and the pipe is not open, retry opening pipe
sdPipe = newSdPipe();
}
try {

Choose a reason for hiding this comment

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

Little nit, but could do
int len = 0
if(sdPipe != null && ((len = sdPipe.available()) > 0)) {
byte[] buffer....

No need to call sdPipe.available twice in succession.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 , addressed in 1c5bede

Copy link
Member

@truthbk truthbk Apr 18, 2017

Choose a reason for hiding this comment

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

Nice one. My bad.

if(sdPipe != null && sdPipe.available() > 0) {
int len = sdPipe.available();
byte[] buffer = new byte[len];
sdPipe.read(buffer);
setReinit(processServiceDiscovery(buffer));
}
if(sdPipe != null && sdPipe.available() > 0) {
int len = sdPipe.available();
byte[] buffer = new byte[len];
sdPipe.read(buffer);
setReinit(processServiceDiscovery(buffer));
}
} catch(IOException e) {
LOGGER.warn("Unable to read from pipe - Service Discovery configuration may have been skipped.");
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/datadog/jmxfetch/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ class AppConfig {
required = false)
private int checkPeriod = 15000;

@Parameter(names = {"--sd_standby", "-w"},
description = "Service Discovery standby.",
@Parameter(names = {"--sd_enabled", "-w"},
description = "Enable Service Discovery.",
required = false)
private boolean sdStandby = false;
private boolean sdEnabled = false;

@Parameter(names = {"--sd_pipe", "-S"},
description = "Service Discovery pipe name.",
Expand Down Expand Up @@ -128,8 +128,8 @@ public int getCheckPeriod() {
return checkPeriod;
}

public boolean getSDStandby() {
return sdStandby;
public boolean getSDEnabled() {
return sdEnabled;
}

public Reporter getReporter() {
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/datadog/jmxfetch/TestCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ protected void initApplication(String yamlFileName, String serviceDiscoveryPipeF
if (sdEnabled) {
params.add(4, "--tmp_directory");
params.add(5, "/foo"); //could be anything we're stubbing it out
params.add(6, "--sd_enabled");
}
new JCommander(appConfig, params.toArray(new String[params.size()]));

Expand Down