Skip to content

Commit a5658ca

Browse files
authored
Merge pull request #2 from seeker89/kille-whiles
Add source code for chapter 2: Killer whiles
2 parents 4c493e2 + e7f3c46 commit a5658ca

File tree

14 files changed

+156
-4
lines changed

14 files changed

+156
-4
lines changed

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
all: killer-whiles
2+
3+
killer-whiles:
4+
(cd examples/killer-whiles && make)
5+
6+
.PHONY: killer-whiles

examples/killer-whiles/Makefile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CFLAGS=-O0
2+
3+
all: mystery000 mystery001 faas
4+
5+
%.o: %.c
6+
cc -o $@ $< $(CFLAGS)
7+
8+
faas:
9+
# link our fake faas services
10+
sudo ln -f -s /home/chaos/src/examples/killer-whiles/faas001_a.service /lib/systemd/system/faas001_a.service
11+
sudo ln -f -s /home/chaos/src/examples/killer-whiles/faas001_b.service /lib/systemd/system/faas001_b.service
12+
sudo systemctl daemon-reload
13+
# link our fake load-balancer
14+
sudo ln -f -s /home/chaos/src/examples/killer-whiles/nginx.loadbalancer.conf /etc/nginx/conf.d/nginx.loadbalancer.conf
15+
sudo systemctl restart nginx
16+
17+
.PHONY: fass
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
echo "Listing backend services"
2+
ps auxf | grep killer-whiles | grep python
3+
echo
4+
5+
echo "Killing instance A (port 8001)"
6+
ps auxf | grep 8001 | awk '{system("sudo kill " $2)}'
7+
sleep 0.5
8+
systemctl status faas001_a
9+
echo
10+
11+
echo "Wait some time in-between killings"
12+
sleep 2
13+
echo
14+
15+
echo "Killing instance A (port 8002)"
16+
ps auxf | grep 8002 | awk '{system("sudo kill " $2)}'
17+
sleep 0.5
18+
systemctl status faas001_b
19+
echo
20+
21+
echo "Listing backend services"
22+
ps auxf | grep killer-whiles | grep python
23+
echo "Done here!"
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[Unit]
2+
Description=FizzBuzz as a Service API prototype - instance A
3+
4+
[Service]
5+
ExecStart=python3 -m http.server 8001 --directory /home/chaos/src/examples/killer-whiles/static
6+
Restart=always
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[Unit]
2+
Description=FizzBuzz as a Service API prototype - instance B
3+
4+
[Service]
5+
ExecStart=python3 -m http.server 8002 --directory /home/chaos/src/examples/killer-whiles/static
6+
Restart=always
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# restart instance A a few times, spaced out by 1.5 second delays
2+
i="0"
3+
while [ $i -le 5 ]
4+
do
5+
echo "Killing faas001_a ${i}th time"
6+
ps auxf | grep killer-whiles | grep python | grep 8001 | awk '{system("sudo kill " $2)}'
7+
sleep 0.5
8+
i=$[$i+1]
9+
done
10+
11+
systemctl status faas001_a --no-pager
12+
13+
sleep 2
14+
15+
# restart instance B a few times, spaced out by 1.5 second delays
16+
i="0"
17+
while [ $i -le 5 ]
18+
do
19+
echo "Killing faas001_b ${i}th time"
20+
ps auxf | grep killer-whiles | grep python | grep 8002 | awk '{system("sudo kill " $2)}'
21+
sleep 0.5
22+
i=$[$i+1]
23+
done
24+
25+
systemctl status faas001_b --no-pager

examples/killer-whiles/mystery000.c

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int main()
2+
{
3+
return 1/0;
4+
}

examples/killer-whiles/mystery001.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
5+
int main()
6+
{
7+
int wait = 1;
8+
int block_size = 64*1024*1024;
9+
int step = 1024*1024;
10+
char * allocated_block;
11+
12+
while (1) {
13+
14+
if ((allocated_block = (char*)malloc(block_size)) == NULL) {
15+
//printf("Couldn't malloc %d!\n", block_size);
16+
if (block_size > 2*step){
17+
block_size -= step;
18+
}
19+
} else {
20+
// slowly write data to our newly allocated memory to actually claim it and use some CPU
21+
for (int i=0; i < block_size; i++){
22+
*(allocated_block + i) = 'Y';
23+
}
24+
}
25+
sleep(wait);
26+
}
27+
28+
return 0;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# http {
2+
# http block already in /etc/nginx/nginx.conf
3+
upstream backend {
4+
server 127.0.0.1:8001 max_fails=1 fail_timeout=1s;
5+
server 127.0.0.1:8002 max_fails=1 fail_timeout=1s;
6+
}
7+
server {
8+
listen 8003;
9+
10+
location / {
11+
proxy_pass http://backend;
12+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
13+
}
14+
}
15+
# }

examples/killer-whiles/run_ab.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# print the command
2+
set -o xtrace
3+
4+
# run the benchmark
5+
ab -t 30 -c 10 -l http://127.0.0.1:8003/api/v1/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"FizzBuzz": true
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
packages_to_install:
3+
- git
4+
- vim
5+
- curl
6+
- nginx
7+
- apache2-utils
+8-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
---
2-
- name: Install git
2+
- name: Preinstall packages
33
become: true
44
apt:
5-
name: git
5+
name: "{{ item }}"
66
state: present
77
update_cache: yes
8+
loop: "{{ packages_to_install }}"
89

910
- name: Check out the book source code to home
1011
git:
1112
repo: 'https://github.com/seeker89/chaos-engineering-in-action.git'
1213
dest: /home/chaos/src
14+
15+
- name: Make all the examples
16+
shell: make
17+
args:
18+
chdir: /home/chaos/src

vm/http/preseed.cfg

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,13 @@ d-i partman-auto/choose_recipe select atomic
239239

240240
# Partitioning
241241
d-i partman-basicfilesystems/no_swap boolean false
242-
243-
d-i partman-auto/expert_recipe string boot-root : \
242+
d-i partman-auto/expert_recipe string boot-root :: \
244243
5120 1 -1 ext4 \
245244
$primary{ } $bootable{ } \
246245
method{ format } format{ } \
247246
use_filesystem{ } filesystem{ ext4 } \
248247
label { root } mountpoint{ / } .
248+
d-i partman-auto/choose_recipe select boot-root
249249

250250
# If you just want to change the default filesystem from ext3 to something
251251
# else, you can do that without providing a full recipe.

0 commit comments

Comments
 (0)