[英]Chaos toolkit not renaming a file
我正在學習混沌工程,並且正在學習教程,但是我的代碼沒有按應有的方式運行。
我正在測試的服務。
service.py
import io
import time
import threading
from wsgiref.validate import validator
from wsgiref.simple_server import make_server
EXAMPLE_FILE = './example.dat'
def update_file():
"""Write the current time to the file every second."""
print('Updating file...')
while True:
with open(EXAMPLE_FILE, 'w') as f:
f.write(datetime.now().isoformat())
time.sleep(1)
def simple_app(environ, start_response):
"""A simple WSGI application.
This application just writes the current time to the response.
"""
status = '200 OK'
headers = [('Content-type', 'text/plain; charset=utf-8')]
start_response(status, headers)
with open(EXAMPLE_FILE, 'r') as f:
return [f.read().encode('utf-8')]
if __name__ == '__main__':
# Start the file update thread.
t = threading.Thread(target=update_file)
t.start()
httpd = make_server('', 8000, simple_app)
print("Serving on port 8000...")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nKeyboard interrupt received, exiting.")
httpd.shutdown()
t.join(timeout=1)
print("Exiting.")
我的混沌實驗文件experiment.json
"title": "Does our service tolerate the loss of its example file?",
"description": "Our service reads data from a file, can it work without it?",
"tags": ["tutorial", "filesystem"],
"steady-state-hypothesis": {
"title": "The exchange file must exist",
"probes": [
{
"type": "probe",
"name": "service-is-unavailable",
"tolerance": [200, 503],
"provider": {
"type": "http",
"url": "http://localhost:8000"
}
}
]
},
"method": [
{
"name": "move-example-file",
"type": "action",
"provider": {
"type": "python",
"module": "os",
"func": "rename",
"arguments": {
"src": "./example.dat",
"dst": "./example.dat.old"
}
}
}
]
}
但是,混亂並沒有重命名我的舊文件,而是使用提供的名稱創建了一個新文件,並且實驗以成功結束,這是我沒想到的。
請幫忙。
最后!
問題: update_file
更新example.dat
文件,如果它不存在,它就會創建它! 因此,當 Chaos 將example.dat
重命名為example.dat.old
時, update_file
只是創建了另一個example.dat
,看起來chaos
穩態假設一直都在滿足。
一種解決方案:將update_file
設置為在更長的時間后運行。 就我而言, time.sleep(60) 有效!
來自chaos run experiment.json
的日志。json
[2021-12-07 01:11:19 INFO] Validating the experiment's syntax
[2021-12-07 01:11:19 INFO] Experiment looks valid
[2021-12-07 01:11:19 INFO] Running experiment: Does our service tolerate the loss of its example file?
[2021-12-07 01:11:19 INFO] Steady-state strategy: default
[2021-12-07 01:11:19 INFO] Rollbacks strategy: default
[2021-12-07 01:11:19 INFO] Steady state hypothesis: The exchange file must exist
[2021-12-07 01:11:19 INFO] Probe: service-is-unavailable
[2021-12-07 01:11:21 INFO] Steady state hypothesis is met!
[2021-12-07 01:11:21 INFO] Playing your experiment's method now...
[2021-12-07 01:11:21 INFO] Action: move-example-file
[2021-12-07 01:11:21 INFO] Steady state hypothesis: The exchange file must exist
[2021-12-07 01:11:21 INFO] Probe: service-is-unavailable
[2021-12-07 01:11:23 CRITICAL] Steady state probe 'service-is-unavailable' is not in the given tolerance so failing this experiment
[2021-12-07 01:11:23 INFO] Experiment ended with status: deviated
[2021-12-07 01:11:23 INFO] The steady-state has deviated, a weakness may have been discovered
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.