doujiao9426 2019-05-02 07:35
浏览 39
已采纳

锁定文件以更新一部分数据或所有文件内容

I’ve file which can be updated from several of process, hence I want to use locking and found https://github.com/gofrs/flock which might helps

But I think that issue is a bit more complicated , for example to update the file the api needs to read the file/ section before (we are providing api to read the file or the application objects by name )
Get the json data modify it in the memory and update the file.

There is two options:

  1. updating the all file content
  2. updating section on the file i.e. one application properties

The problem is like following:


 1. Process A & Process B (can be more…) Reads the object  of
    application name  `node1`

 2. Process A update the section (node1) with new data (for example
        change the kind property and update)

 3. Process B want to do the same , the problem is that between the time
        it reads the data and the time it wants to update ,the data is not
        valid since other process already update it.

in addition the same scenario is valid to all the file context

Race condition issue…

This is a short example of the file that some stateless process can update at any given time

ID: demo
version: 0.0.1

applications:
 - name: node1
   kind: html5
   path: node1
   expose:
    - name: node_api
      properties:
         url: ${app-url}


 - name: node2
   kind: nodejs
   path: node2
   parameters:
      quota: 256M
      memory: 256M

How we can overcome this issue, or maybe simplify it to avoid race condition and collusion?

  • 写回答

3条回答 默认 最新

  • doushan6161 2019-05-02 11:17
    关注

    This is a common problem so look for known solutions. For example, optimistic locking.

    Something like this pseudocode:

    lock file for read
    read file into data1
    release file lock
    hash data1 as hash1
    update data1
    lock file for update
    read file into data2
    hash data2 as hash2
    if hash1 != hash2
        release file lock
        return error
    write file from (updated) data1
    release file lock
    return success
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮