Writing a simple RADOS class in Lua

One of the default RADOS classes in Ceph from the Kraken release onward is one that can run Lua scripts. The Lua script is dynamically passed to the Lua RADOS object class, which then executes the contents of the script. The scripts are typically passed in a JSON-formatted string to the object class. Although this brings advantages over the traditional RADOS object classes, which need to be compiled before they can be used, it also limits the complexity of what the Lua scripts can accomplish and as such thought should be given as to what method is appropriate for the task you wish to accomplish.

The following Python code example demonstrates how to create and pass a Lua script to be executed on an OSD. The Lua scripts reads the contents of the specified object and returns the string of text back in upper case, all processing is done on the remote OSD, which holds the object; the original object contents are never sent to the client.

Place the following into a file named rados_lua.py:

import rados, json, sys

try: #Read ceph.conf config file to obtain monitors
cluster = rados.Rados(conffile='/etc/ceph/ceph.conf')
except:
print "Error reading Ceph configuration"
exit(1)

try: #Connect to the Ceph cluster
cluster.connect()
except:
print "Error connecting to Ceph Cluster"
exit(1)

try: #Open specified RADOS pool
ioctx = cluster.open_ioctx("rbd")
except:
print "Error opening pool"
cluster.shutdown()
exit(1)

cmd = {
"script": """
function upper(input, output)
size = objclass.stat()
data = objclass.read(0, size)
upper_str = string.upper(data:str())
output:append(upper_str)
end
objclass.register(upper)
""",
"handler": "upper",
}

ret, data = ioctx.execute(str(sys.argv[1]), 'lua', 'eval_json', json.dumps(cmd))
print data[:ret]

ioctx.close() #Close connection to pool
cluster.shutdown() #Close connection to Ceph

Let's now create a test object with all lowercase characters:

    echo this string was in lowercase | sudo rados -p rbd put LowerObject –

The Lua object class by default is not allowed to be called by OSDs; we need to add the following to all the OSDs in their ceph.conf:

[osd]
osd class load list = *
osd class default list = *

And now run our Python librados application:

    sudo python rados_lua.py LowerObject

The preceding command gives the following output:

You should see that the text from our object has been converted all into uppercase. You can see from the Python code earlier that we are not doing any of the conversion in the local Python code and it's all being done remotely on OSD.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.234.150