Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 由于内存分配问题,redis无法在后台保存时对lua脚本的影响_Django_Lua_Redis - Fatal编程技术网

Django 由于内存分配问题,redis无法在后台保存时对lua脚本的影响

Django 由于内存分配问题,redis无法在后台保存时对lua脚本的影响,django,lua,redis,Django,Lua,Redis,我最近在Django应用程序中添加了一些Lua脚本(用于与Redis后端接口)。在生产过程中(虽然不是在开发过程中),设置最终给了我以下错误: Error running script (call to f_8c07b227bc796743f66bad8dbe75a5bf8fcc8cd6): @user_script:2: @user_script: 2: -MISCONF Redis is configured to save RDB snapshots, but is currently n

我最近在Django应用程序中添加了一些Lua脚本(用于与Redis后端接口)。在生产过程中(虽然不是在开发过程中),设置最终给了我以下错误:

Error running script (call to f_8c07b227bc796743f66bad8dbe75a5bf8fcc8cd6): @user_script:2: @user_script: 2: -MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.
当这种情况发生时,如果我查看redis日志,我会看到
无法在后台保存:fork:无法分配内存

我知道发生了什么。我的问题是,当内存无法分配用于后台保存,但lua脚本崩溃时,redis如何保持功能?有没有办法避免这种情况


仅供参考,以下是我的
lua_scripts.py
模块的布局方式:

import redis
from location import REDLOC2 #location of relevant unix socket

my_server = redis.StrictRedis(unix_socket_path=REDLOC2)

# Get recent
lualatestlogins = """
local l = redis.call('ZRANGEBYSCORE', KEYS[1], ARGV[1]-600, '+inf') -- returns key:value pairs
local c = {}
for _, v in pairs(l) do
  local p = v:find(':')
  local i = v:sub(1,p-1)
  c[#c+1] = i
end
return c"""
getlatestlogins = my_server.register_script(lualatestlogins)

# Evict old
luacleanselogins = """
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', '(' .. ARGV[1]-600)"""
cleanselogins = my_server.register_script(luacleanselogins)

# -- Store new
luastorelogin = """
redis.call('ZADD', KEYS[1], ARGV[1], ARGV[2] .. ':' .. ARGV[3])
redis.call('SET',KEYS[2],ARGV[3])
redis.call('EXPIRE',KEYS[2],600)"""
storelogin = my_server.register_script(luastorelogin)

# Retrieve collisions
luaretrieveclones = """
local q = redis.call('GET',KEYS[2]) 
if q == nil or q == '' then
  return nil               
else
  local l = redis.call('ZRANGEBYSCORE', KEYS[1], ARGV[1]-600, '+inf')
  local c = {}
  for _, v in pairs(l) do  
    local p = v:find(':')  
    local i = v:sub(1,p-1) 
    local n = v:sub(p+1)   
    if n == q then         
      c[#c+1] = i          
    end
  end

  return c
end"""
retrieveclones = my_server.register_script(luaretrieveclones)

错误消息告诉您一切:可能修改数据集的命令被禁用。

Redis尝试用一个新进程来保存后台,但由于内存不足而失败。在这种情况下,Redis将禁用对数据库的任何更新。否则,内存中的数据和磁盘上的数据将不一致

当内存无法分配用于后台保存时,redis为何仍能正常工作

Redis仍然可以提供只读请求,并拒绝任何可能修改数据库的请求

但是lua脚本崩溃了吗

lua脚本试图修改数据库,例如,
redis.call('ZADD',键[1],ARGV[1],ARGV[2]…':'…ARGV[3])
,但失败

有没有办法避免这种情况

  • 您应该监视这种错误,并将Redis移动到具有更多内存的新机器上进行扩展,或者使用Redis群集进行扩展

  • 在lua脚本中,使用
    redis.pcall
    而不是
    redis.call
    。如果
    redis.call
    失败,整个脚本将终止。但是,如果redis.pcall失败,它将捕获错误并返回错误消息的lua表。您可以检查返回的表以查看您的呼叫是否成功运行


  • 你说“Redis仍能正常工作”,但所有命令都是这样吗?我只是在猜测,但错误消息(“可能修改数据集的命令被禁用”)让我认为读取将继续工作,但潜在的写入(包括Lua脚本)将被禁用。回答很酷。谢谢