混沌工具包不重命名文件

Posted

技术标签:

【中文标题】混沌工具包不重命名文件【英文标题】:Chaos toolkit not renaming a file 【发布时间】:2022-01-11 12:56:18 【问题描述】:

我正在学习混沌工程,并且正在学习教程,但我的代码没有按应有的方式运行。

我正在测试的服务。

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"
        
      
    
  ]

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

enter image description here

请帮忙。

【问题讨论】:

首先使用print() 来查看变量中有什么以及执行了哪部分代码。它被称为"print debuging" 我不知道您在哪里重命名文件。我看不到你在哪里加载experiment.json。至于我,这段代码无法创建example.dat.old。所有这些代码都与重命名无关。 我正在使用混沌库。我运行service.py,然后运行chaos run experiment.json 命令。重命名方法在实验文件中,chaos 使用它来创建 example.dat.old。这很好用。但问题是它会创建一个新文件,而不是重命名当前文件。 除非,如果每次service.py 运行,它会再次创建文件。 我有一个想法:也许它会创建文件.dat.old,复制数据,但删除旧文件有问题。也许原始文件需要不同的权限 【参考方案1】:

终于!

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

一种解决方案:将update_file 设置为在更长的时间后运行。就我而言,time.sleep(60) 有效!

来自chaos run experiment.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

【讨论】:

以上是关于混沌工具包不重命名文件的主要内容,如果未能解决你的问题,请参考以下文章

为啥 R8 不重命名所有方法和类?

apache_conf 如何在.NET MVC4中创建自定义BundleTransform,特别是为了不重命名变量。因为我找不到这个

将非 bat 扩展文件作为批处理文件运行

苹果Mac强大的批量重命名工具:A Better Finder Rename

如何在 Vega Lite 中重命名图例标签?

js 如何批量重命名文件