Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Java: Cleanup operation samplers if absent from strategy response #655

Merged
merged 1 commit into from
Oct 2, 2019
Merged
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 @@ -18,13 +18,15 @@
import io.jaegertracing.internal.samplers.http.PerOperationSamplingParameters;
import io.jaegertracing.spi.Sampler;
import java.util.HashMap;
import java.util.Map;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;


/**
* Computes {@link #sample(String, long)} using the name of the operation, and maintains a specific
* {@link GuaranteedThroughputSampler} instance for each operation.
Expand All @@ -36,7 +38,7 @@
@Getter(AccessLevel.PACKAGE) //Visible for testing
public class PerOperationSampler implements Sampler {
private final int maxOperations;
private final HashMap<String, GuaranteedThroughputSampler> operationNameToSampler;
private Map<String, GuaranteedThroughputSampler> operationNameToSampler;
private ProbabilisticSampler defaultSampler;
private double lowerBound;

Expand All @@ -51,9 +53,9 @@ public PerOperationSampler(int maxOperations, OperationSamplingParameters strate
/**
* Updates the GuaranteedThroughputSampler for each operation
* @param strategies The parameters for operation sampling
* @return true iff any samplers were updated
* @return true if any samplers were updated
*/
public synchronized boolean update(OperationSamplingParameters strategies) {
public synchronized boolean update(final OperationSamplingParameters strategies) {
boolean isUpdated = false;

lowerBound = strategies.getDefaultLowerBoundTracesPerSecond();
Expand All @@ -64,23 +66,28 @@ public synchronized boolean update(OperationSamplingParameters strategies) {
isUpdated = true;
}

Map<String, GuaranteedThroughputSampler> newOpsSamplers = new HashMap<String, GuaranteedThroughputSampler>();
//add or update operation samples using given strategies
for (PerOperationSamplingParameters strategy : strategies.getPerOperationStrategies()) {
String operation = strategy.getOperation();
double samplingRate = strategy.getProbabilisticSampling().getSamplingRate();
GuaranteedThroughputSampler sampler = operationNameToSampler.get(operation);
if (sampler != null) {
isUpdated = sampler.update(samplingRate, lowerBound) || isUpdated;
newOpsSamplers.put(operation, sampler);
} else {
if (operationNameToSampler.size() < maxOperations) {
if (newOpsSamplers.size() < maxOperations) {
sampler = new GuaranteedThroughputSampler(samplingRate, lowerBound);
operationNameToSampler.put(operation, sampler);
newOpsSamplers.put(operation, sampler);
isUpdated = true;
} else {
log.info("Exceeded the maximum number of operations({}) for per operations sampling",
maxOperations);
}
}
}

operationNameToSampler = newOpsSamplers;
return isUpdated;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public void testUpdateIgnoreGreaterThanMax() {
undertest.update(new OperationSamplingParameters(DEFAULT_SAMPLING_PROBABILITY,
DEFAULT_LOWER_BOUND_TRACES_PER_SECOND, parametersList));

assertEquals(1, operationToSamplers.size());
assertNotNull(operationToSamplers.get(OPERATION));
assertEquals(1, undertest.getOperationNameToSampler().size());
assertNotNull(undertest.getOperationNameToSampler().get(OPERATION));
}

@Test
Expand All @@ -169,10 +169,29 @@ public void testUpdateAddOperation() {
DEFAULT_LOWER_BOUND_TRACES_PER_SECOND,
parametersList));

assertEquals(1, operationToSamplers.size());
assertEquals(1, undertest.getOperationNameToSampler().size());
assertEquals(new GuaranteedThroughputSampler(SAMPLING_RATE,
DEFAULT_LOWER_BOUND_TRACES_PER_SECOND),
operationToSamplers.get(OPERATION));
undertest.getOperationNameToSampler().get(OPERATION));
}

@Test
public void testAbsentOperationIsRemoved() {
String absentOp = "ShouldBeRemoved";
operationToSamplers.put(absentOp, mock(GuaranteedThroughputSampler.class));
PerOperationSamplingParameters perOperationSamplingParameters1 =
new PerOperationSamplingParameters(OPERATION, new ProbabilisticSamplingStrategy(SAMPLING_RATE));
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't operationToSamplers be passed in here to control the starting state of the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

undertest is inited with the operationToSamplers during setup, adding a sampler to the map should do the job regarding the starting state.

List<PerOperationSamplingParameters> parametersList = new ArrayList<>();
parametersList.add(perOperationSamplingParameters1);

undertest.update(new OperationSamplingParameters(DEFAULT_SAMPLING_PROBABILITY,
DEFAULT_LOWER_BOUND_TRACES_PER_SECOND,
parametersList));

assertEquals(1, undertest.getOperationNameToSampler().size());
assertEquals(new GuaranteedThroughputSampler(SAMPLING_RATE,
DEFAULT_LOWER_BOUND_TRACES_PER_SECOND),
undertest.getOperationNameToSampler().get(OPERATION));
assertFalse(undertest.getOperationNameToSampler().containsKey(absentOp));
}
}