Skip to content

Developer guide

周鹏飞 edited this page Nov 14, 2019 · 2 revisions

This article introduces the communication interface API between chaosblade and chaosblade-exec-cplus. This API follows 《Chaos Experiments Model》.

Interface definition

chaosblade-exec-cplus implements two interfaces: create and destroy, which be used to create a chaos experiment and stop a chaos experiment. Let us introduce a C++ application chaos experiment DEMO. chaosblade-exec-cplus implements request processing based on the built-in tomcat server of springboot.

C++ Application Chaos Experiments’ DEMO

In this experiment, we exercised the communication delay 3 seconds between socket server application and the socket client application, these two application are both developed using C++ Language and deployed in Linux system. Now we download the Socket Demo:

Socket-server.tar.gz

Once downloaded, extract and compile:

g++ -g -c tcp_server.cpp -o tcp_server.o

g++ -g tcp_server.o main.cpp -I. -o server



Socket-client.tar.gz

Once downloaded, extract and compile:

g++ -g -c tcp_client.cpp -o tcp_client.o

g++ -g tcp_client.o main.cpp -I. -o client

启动 socket server

./server 9527

Start socket client:

./client 127.0.0.1 9527

After the startup is successful, you will be prompted: send message to server:

Then, type what you want to pass to socket server. For example: 666

The socket server application prompts after receiving the message:

Before print: 2019-07-25 10:03:16

Received a connection from 127.0.0.1

After print: 2019-07-25 10:03:16

Received message: 666

Completion of these steps represents a socket server application and the socket client application deployment success.

In addition, the reason for printing the time before and after Received a connection from 127.0.0.1 is because the experimental demo will simulate the delay fault before the program is executed to Received a connection from 127.0.0.1. The execution time before and after printing is convenient for observing the experimental results.

Next we need to use the blade tool for chaos experiments. Before executing the experiment, we need to execute the prepare command to mount the required c++ agent:

./blade p cplus --port 8370 --script-location=/home/staragent/plugins/monkeyking/chaosblade/lib/cplus/ --wait-time 10

Returning the following results, indicating that the experiment is ready:

{"code":200,"success":true,"result":"e669d57f079a00cc"}

Let us start the chaos experiment and call the socket server interface for a delay of 3 seconds. We execute the following command:

./blade c cplus delay --delayDuration 3 --breakLine tcp_server.cpp:33 --fileLocateAndName /home/admin/socketServer/server --forkMode parent --processName /home/admin/socketServer/server --initParams 9527 --libLoad ""

or:

./blade c cplus delay --delayDuration 3 --breakLine tcp_server.cpp:33 --fileLocateAndName /home/admin/socketServer/server --forkMode parent --processName /home/admin/socketServer/server --initParams 9527

Return the following results, indicating successful execution:

{"code":200,"success":true,"result":"ec695fee1e458fc6"}

You can check the delay effect in the log after stopping the chaotic experiment. The log address:

{user.home}/logs/chaosblade/chaosblade_cplus.log

An experiment of command parsing:

  • --delayDuration: 3,represents a delay 3 seconds; unit is second
  • --breakLine: tcp_server.cpp:33, the position of the breakpoint, which can be a certain line, or a method name
  • --fileLocateAndName: /home/admin/socketServer/server,the location and name of the C++ application executable file
  • --forkMode: child(or: parent),indicates whether we injected the fault into the child process or the parent process
  • --processName: server(or: /home/admin/socketServer/server),can uniquely identify the identity of the C++ application process, such as the process name
  • --initParams: 9527,when the C++ process starts normally, the parameters following the execution file in the startup command
  • --libLoad: /home/lib(or: ""),if you need to set the class library folder address when starting the C++ process, you can set it here (such as /home/lib). If you do not need to load the custom class library when starting, you can fill in the space, or neglect this parameter.

Another way to call is by sending a url request: chaosblade/create?suid=e669d57f079a00cc&target=cplus&action=delay&breakLine=tcp_server.cpp:33&fileLocateAndName=/home/admin/socketServer/server&forkMode=parent&processName=/home/admin/socketServer/server&delayDuration=3&initParams=9527&libLoad=

Request parameter is

{
	"suid": "e669d57f079a00cc",
	"target": "cplus",
	"action": "delay",
	"breakLine": "tcp_server.cpp:33",
	"fileLocateAndName": "/home/admin/socketServer/server",
	"forkMode": "parent",
	"processName": "/home/admin/socketServer/server",
	"delayDuration": “3”,
	"initParams": "9527",
	"libLoad": ""
}

This method adds several parameters, which are parsed as follows:

  • create: create a chaos experiment request
  • suid: request parameter, experiment ID, follow-up strop experiment will use this ID
  • target: request parameters, experimental component targets, cplus represents experiments for C++ applications
  • action: request parameters, execute the experiment scene, delay

Note:

  • suid, target, and action are required parameters for the create request. The breakLine, fileLocateAndName, forkMode, processName, delayDuration, initParams, and libLoad parameters vary depending on the target and action.
  • Upon receiving the request, the parameter checker is called according to the target and action to verify whether the parameter value is legal. If it is legal, the test is recorded;

Stop the current delay chaos experiment, and access the url again to verify that it returns to normal:

./blade destroy ec695fee1e458fc6

Another way to call is by sending a url request: chaosblade/destroy?suid=ec695fee1e458fc6

Request parameter is:

{
    "suid": "ec695fee1e458fc6"
}
  • suid: request parameter, It gets the UID returned since the experiment was created Note: After receiving the destroy request, the chaos experiment rules corresponding to the UID are deleted.

If you are not happy, we will implement the modification of the internal variables of the server, and change the listening port 9527 that the server opens to 9529.

It is similar to the delay command parameter, because the same parameters are needed for the walkthrough C++ application, except that there is no --delayDuration, and there are more --varaibleName and --varaibleValue parameters. We simulated the call to the newly opened listening port 9527 and changed it to 9529:

./blade c cplus modify --varaibleName listen_port --varaibleValue 9529 --breakLine tcp_server.cpp:11 --fileLocateAndName /home/admin/socketServer/server --forkMode child --processName /home/admin/socketServer/server  --initParams 9527 --libLoad /home/lib

or:

./blade c cplus modify --varaibleName listen_port --varaibleValue 9529 --breakLine tcp_server.cpp:11 --fileLocateAndName /home/admin/socketServer/server --forkMode child --processName /home/admin/socketServer/server  --initParams 9527

Return the following results and verify that the port has been modified in socket client.

{"code":200,"success":true,"result":"09dd96f4c062df69"}

Stop the test:

./blade destroy 09dd96f4c062df69 

Finally, we undo the experimental preparation, that is, uninstall the c++ Agent:

./blade revoke e669d57f079a00cc