Skip to content

Commit a29c2a7

Browse files
adservice add highcpu load ff (#1510)
* adservice add highcpu load ff This commit adds a new ff to the adservice. When enabled, the adservice uses an abnormal amout of CPU. Should you want to demo cpu throtteling, you need to add cpu limits to the pod/container * update changelog --------- Co-authored-by: Juliano Costa <julianocosta89@outlook.com>
1 parent 6f89f56 commit a29c2a7

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ the release.
6767
([#1462](https://github.com/open-telemetry/opentelemetry-demo/pull/1462))
6868
* [loadgenerator] added loadgeneratorFloodHomepage flagd
6969
([#1486](https://github.com/open-telemetry/opentelemetry-demo/pull/1486))
70+
* [adservice] add adServiceHighCpu feature flag
71+
([#1510](https://github.com/open-telemetry/opentelemetry-demo/pull/1510))
7072

7173
## 1.8.0
7274

src/adservice/src/main/java/oteldemo/AdService.java

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import oteldemo.Demo.AdRequest;
3636
import oteldemo.Demo.AdResponse;
3737
import oteldemo.problempattern.GarbageCollectionTrigger;
38+
import oteldemo.problempattern.CPULoad;
3839
import dev.openfeature.contrib.providers.flagd.FlagdOptions;
3940
import dev.openfeature.contrib.providers.flagd.FlagdProvider;
4041
import dev.openfeature.sdk.Client;
@@ -130,6 +131,7 @@ private static class AdServiceImpl extends oteldemo.AdServiceGrpc.AdServiceImplB
130131

131132
private static final String ADSERVICE_FAILURE = "adServiceFailure";
132133
private static final String ADSERVICE_MANUAL_GC_FEATURE_FLAG = "adServiceManualGc";
134+
private static final String ADSERVICE_HIGH_CPU_FEATURE_FLAG = "adServiceHighCpu";
133135

134136
private AdServiceImpl() {}
135137

@@ -143,6 +145,8 @@ private AdServiceImpl() {}
143145
@Override
144146
public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
145147
AdService service = AdService.getInstance();
148+
CPULoad cpuload = CPULoad.getInstance();
149+
cpuload.execute(getFeatureFlagEnabled(ADSERVICE_HIGH_CPU_FEATURE_FLAG));
146150

147151
// get the current span in context
148152
Span span = Span.current();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package oteldemo.problempattern;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Optional;
10+
11+
import io.grpc.ManagedChannelBuilder;
12+
13+
import org.apache.logging.log4j.LogManager;
14+
import org.apache.logging.log4j.Logger;
15+
16+
/**
17+
* This class is designed to simulate a high CPU load scenario.
18+
* It contains methods to start and stop a specified number of worker threads designed to
19+
* perform CPU-intensive calculations.
20+
*/
21+
public class CPULoad {
22+
private static final Logger logger = LogManager.getLogger(CPULoad.class.getName());
23+
private static final int THREAD_COUNT = 4;
24+
private boolean running = false;
25+
private final List<Logarithmizer> runningWorkers = new ArrayList<>();
26+
27+
private static CPULoad instance;
28+
29+
/**
30+
* Singleton pattern to get the instance of CPULoad.
31+
* @return The singleton instance of CPULoad.
32+
*/
33+
public static CPULoad getInstance() {
34+
if (instance == null) {
35+
instance = new CPULoad();
36+
}
37+
return instance;
38+
}
39+
40+
/**
41+
* Starts or stops the CPU load generation based on the input parameter.
42+
* If enabled, it launches worker threads. If disabled, it stops any running threads.
43+
*
44+
* @param enabled Flag to start (true) or stop (false) the CPU load simulation.
45+
*/
46+
public void execute(Boolean enabled) {
47+
if (enabled) {
48+
logger.info("High CPU-Load problempattern enabled");
49+
if (!running) {
50+
spawnLoadWorkers(THREAD_COUNT);
51+
running = true;
52+
}
53+
} else {
54+
running = false;
55+
stopWorkers();
56+
}
57+
}
58+
59+
/**
60+
* Creates and starts a specified number of Logarithmizer threads to simulate CPU load.
61+
*
62+
* @param threadCount The number of threads to be started.
63+
*/
64+
private void spawnLoadWorkers(int threadCount) {
65+
synchronized(runningWorkers) {
66+
for (int i = 0; i < threadCount; i++) {
67+
Logarithmizer logarithmizer = new Logarithmizer();
68+
Thread thread = new Thread(logarithmizer);
69+
thread.setDaemon(true);
70+
thread.start();
71+
runningWorkers.add(logarithmizer);
72+
}
73+
}
74+
}
75+
76+
/**
77+
* Signals all running Logarithmizer threads to stop and clears the list of running workers.
78+
*/
79+
private void stopWorkers() {
80+
synchronized(runningWorkers) {
81+
for (Logarithmizer logarithmizer : runningWorkers) {
82+
logarithmizer.setShouldRun(false);
83+
}
84+
runningWorkers.clear();
85+
}
86+
}
87+
88+
/**
89+
* Inner class representing a worker focused on calculating logarithms to consume CPU resources.
90+
*/
91+
private static class Logarithmizer implements Runnable {
92+
93+
private volatile boolean shouldRun = true;
94+
95+
/**
96+
* Continuously calculates the logarithm of the current system time until
97+
* requested to stop.
98+
*/
99+
@Override
100+
public void run() {
101+
while (shouldRun) {
102+
Math.log(System.currentTimeMillis());
103+
}
104+
}
105+
106+
/**
107+
* Sets the shouldRun flag to control whether this Logarithmizer should continue
108+
* to run.
109+
*
110+
* @param shouldRun A boolean flag to continue (true) or stop (false) the logarithm computation.
111+
*/
112+
public void setShouldRun(boolean shouldRun) {
113+
this.shouldRun = shouldRun;
114+
}
115+
}
116+
}

src/flagd/demo.flagd.json

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
},
2929
"defaultVariant": "off"
3030
},
31+
"adServiceHighCpu": {
32+
"description": "Triggers high cpu load in the ad service",
33+
"state": "ENABLED",
34+
"variants": {
35+
"on": true,
36+
"off": false
37+
},
38+
"defaultVariant": "off"
39+
},
3140
"adServiceFailure": {
3241
"description": "Fail ad service",
3342
"state": "ENABLED",

0 commit comments

Comments
 (0)