Skip to content

Commit 49dec4b

Browse files
committed
make writer wait configurable
Adds the config property `parfait.writer.wait` that can be used to adjust the amount of time an update metric operation will wait for the writer to start.
1 parent 37b8e82 commit 49dec4b

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

dxm/src/main/java/io/pcp/parfait/dxm/PcpMmvWriter.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ public void putBytes(ByteBuffer buffer, String value) {
167167
private volatile State state = State.STOPPED;
168168
private final Monitor stateMonitor = new Monitor();
169169
private final Monitor.Guard isStarted = stateMonitor.newGuard(() -> state == State.STARTED);
170+
private volatile Duration maxWaitStart = Duration.ofSeconds(10);
170171
private volatile boolean usePerMetricLock = true;
171172
private final Map<PcpValueInfo,ByteBuffer> perMetricByteBuffers = newConcurrentMap();
172173
private final Object globalLock = new Object();
@@ -231,7 +232,6 @@ public PcpMmvWriter(ByteBufferFactory byteBufferFactory, IdentifierSourceSet ide
231232
this.instanceDomainStore = mmvVersion.createInstanceDomainStore(identifierSources, stringStore);
232233
this.mmvVersion = mmvVersion;
233234
this.metricNameValidator = mmvVersion.createMetricNameValidator();
234-
235235
registerType(String.class, MMV_STRING_HANDLER);
236236
}
237237

@@ -324,7 +324,7 @@ public final void updateMetric(MetricName name, Object value) {
324324
if (state == State.STARTED) {
325325
doUpdateMetric(name, value);
326326
} else if (state == State.STARTING) {
327-
if (stateMonitor.enterWhenUninterruptibly(isStarted, Duration.ofSeconds(10))) {
327+
if (stateMonitor.enterWhenUninterruptibly(isStarted, maxWaitStart)) {
328328
// Leave the monitor immediately because we only care about being notified about the state change
329329
stateMonitor.leave();
330330
doUpdateMetric(name, value);
@@ -422,6 +422,15 @@ public void setFlags(Set<MmvFlag> flags) {
422422
this.flags = EnumSet.copyOf(flags);
423423
}
424424

425+
/**
426+
* Sets the maximum amount of time to wait for the writer to start when attempting to update a metric.
427+
*
428+
* @param maxWaitStart the maximum amount of time to wait
429+
*/
430+
public void setMaxWaitStart(Duration maxWaitStart) {
431+
this.maxWaitStart = Preconditions.checkNotNull(maxWaitStart, "maxWaitStart cannot be null");
432+
}
433+
425434
private synchronized void addMetricInfo(MetricName name, Semantics semantics, Unit<?> unit,
426435
Object initialValue, TypeHandler<?> pcpType) {
427436
if (metricData.containsKey(name)) {

parfait-agent/src/main/java/io/pcp/parfait/AgentMonitoringView.java

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.pcp.parfait.ValueSemantics;
3434

3535
import java.io.IOException;
36+
import java.time.Duration;
3637
import java.util.EnumSet;
3738

3839
import javax.management.AttributeNotFoundException;
@@ -73,6 +74,7 @@ public void start() {
7374
writer = new PcpMmvWriter(name, IdentifierSourceSet.DEFAULT_SET);
7475
writer.setClusterIdentifier(MonitoringViewProperties.getCluster());
7576
writer.setFlags(EnumSet.of(PcpMmvWriter.MmvFlag.MMV_FLAG_PROCESS));
77+
writer.setMaxWaitStart(Duration.ofMillis(MonitoringViewProperties.getWriterWait()));
7678

7779
DynamicMonitoringView view;
7880
view = new DynamicMonitoringView(registry,

parfait-agent/src/main/java/io/pcp/parfait/MonitoringViewProperties.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.lang.management.ManagementFactory;
2020
import java.util.Collections;
2121

22-
import io.pcp.parfait.DynamicMonitoringView;
2322
import io.pcp.parfait.dxm.HashingIdentifierSource;
2423
import io.pcp.parfait.dxm.IdentifierSource;
2524

@@ -31,15 +30,18 @@ public class MonitoringViewProperties {
3130
private static final String INTERVAL = "interval";
3231
private static final String STARTUP = "startup";
3332
private static final String CONNECT = "connect";
33+
private static final String WRITER_WAIT = "writer.wait";
3434

3535
public static final String PARFAIT_NAME = PARFAIT + "." + NAME;
3636
public static final String PARFAIT_CLUSTER = PARFAIT + "." + CLUSTER;
3737
public static final String PARFAIT_INTERVAL = PARFAIT + "." + INTERVAL;
3838
public static final String PARFAIT_STARTUP = PARFAIT + "." + STARTUP;
3939
public static final String PARFAIT_CONNECT = PARFAIT + "." + CONNECT;
40+
public static final String PARFAIT_WRITER_WAIT = PARFAIT + "." + WRITER_WAIT;
4041

4142
private static final String DEFAULT_INTERVAL = "1000"; // milliseconds
4243
private static final String DEFAULT_CONNECT = "localhost:9875";
44+
private static final String DEFAULT_WRITER_WAIT_MS = "10000";
4345

4446
public static String getCommandBasename(String command) {
4547
// trim away arguments, produce a generally sanitized basename
@@ -139,6 +141,14 @@ public static String getDefaultConnection() {
139141
return connect;
140142
}
141143

144+
public static String getDefaultWriterWait() {
145+
String writerWait = System.getProperty(PARFAIT_WRITER_WAIT);
146+
if (writerWait == null || writerWait.isEmpty()) {
147+
return DEFAULT_WRITER_WAIT_MS;
148+
}
149+
return writerWait;
150+
}
151+
142152
public static void setupProperties() {
143153
String name = getDefaultName(getParfaitName(), getDefaultCommand(), getRuntimeName());
144154
System.setProperty(PARFAIT_NAME, name);
@@ -154,6 +164,9 @@ public static void setupProperties() {
154164

155165
String connect = getDefaultConnection();
156166
System.setProperty(PARFAIT_CONNECT, connect);
167+
168+
String writerWait = getDefaultWriterWait();
169+
System.setProperty(PARFAIT_WRITER_WAIT, writerWait);
157170
}
158171

159172
//
@@ -174,4 +187,13 @@ public static Long getStartup() {
174187
public static String getConnection() {
175188
return System.getProperty(PARFAIT_CONNECT);
176189
}
190+
191+
/**
192+
* The maximum number of milliseconds to wait for PcpMmvWriter to start when attempting to update a metric.
193+
*
194+
* @return maximum number of milliseconds to wait
195+
*/
196+
public static long getWriterWait() {
197+
return Long.parseLong(System.getProperty(PARFAIT_WRITER_WAIT));
198+
}
177199
}

0 commit comments

Comments
 (0)