首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >混沌工具包不重命名文件

混沌工具包不重命名文件
EN

Stack Overflow用户
提问于 2021-12-06 09:37:29
回答 1查看 100关注 0票数 0

我正在学习混沌工程,我正在学习一个教程,但是我的代码并没有按照它应该运行的方式运行。

我正在测试的服务。

service.py

代码语言:javascript
复制
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

代码语言:javascript
复制
  "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"
        }
      }
    }
  ]
}

但是,混沌没有重新命名我的旧文件,而是用提供的名称创建了一个新文件,实验以成功结束,这是我没想到的。

在这里输入图像描述

请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-06 23:19:41

终于来了!

问题:update_file每秒钟更新一次example.dat文件,如果它不存在,它就会创建它!因此,当chaosexample.dat重命名为example.dat.old时,update_file只是创建了另一个example.dat,似乎一直满足混沌稳态假设。

一种解决方案:将update_file设置为运行时间长得多的时间。在我的例子中,time.sleep(60)起作用了!

来自chaos run experiment.json的日志

代码语言:javascript
复制
[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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70243406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档