Lua C++ API Reference
This is a reference of the Lua API implemented directly in C++.
Note
Lua bindings are defined in libstormweaver/src/scripting/luactx.cpp
Global functions
sleep
Sleeps for the specified time
defaultActionRegistry
Returns a modifiable reference to the default action registry
initPostgresDatadir
Creates a new postgres data directory
initBasebackupFrom
Creates a new postgres data directory as a backup of a running postgres instance.
replica = initBasebackupFrom('pg/install/dir', 'new/data/dir', primaryNode) -- allows any number of additional parameters, added to the basebackup command line
debug
Writes a debug message to the log
info
Writes an info message to the log
warning
Writes a warning message to the log
getenv
Returns a the value of an environment variable, or a default.
setup_node_pg
Configures the connection parameters to a postgres installation.
return setup_node_pg({
host = "localhost",
port = 5432,
user = "username",
password = "",
database = "stormweaver",
on_connect = conn_callback,
})
ActionFactory
Used to create a specific action, stored in an ActionRegistry
weight
A property representing the chance/weight of an action
ActionRegistry
Represents a set of actions executed by worker(s).
remove
Removes the action with the name from the registry.
insert
Inserts the specified ActionFactory to the registry.
This function requires a specific Factory, which can be retrieved from another Registry using the get
function.
has
Tells if the registry has a factory with the specified name.
makeCustomSqlAction
Creates a new custom SQL action with a specific query and weight.
makeCustomTableSqlAction
Creates a new custom SQL action related to a table, with a specific weight.
The {table}
expression in the SQL command is replaced with a randomly choosen table.
Can be specified multiple times, and the same table will be used each time.
get
Returns a reference to the factory with the specified name.
use
Overwrites the contents of the registry using the specified other registry. The two registries will be equal after executing this function.
fs
A collection of filesystem operations.
is_directory
Returns true if a directory with the specified name exists
copy_directory
Copies the directory recursively
delete_directory
Deletes the specified directory
LoggedSQL
A class representing a database connection
execute_query
A simple funtion that execute a single SQL query. Result sets are not yet implemented in lua.
Node
A class representing a database server, created using the setupPgNode
global function.
init
Intended for node initialization, takes a callback that receives as a Worker as the single parameter.
initRandomWorkload
Creates a new random workload setup with the given parameters.
Currently supports the following parameters:
run_seconds
: how long to run the workloadworker_count
: how many workers to use in the workload
possibleActions
Returns a reference to the action registry associated with the node.
This is a copy made at the time of creating the node, and can be modified separately.
Will be copied into the workload when one is created using initRandomWorkload
.
Postgres
A class used to manage a Postgres installation/datadir.
An instance of this class can be created with the global functions initPostgresDatadir
and initBasebackupFrom
.
start
Starts the server, returns true if succeeds.
stop
Stops the server Has a parameter, the graceful wait period - after that, it executes kill9.
restart
Restarts (stops and starts) the server, returns true if succeeds. Has a parameter, the graceful wait period - after that, it executes kill9.
kill9
Kills the server without waiting for it to stop.
createdb
Creates a database with the specified name.
dropdb
Drops the database with the specified name.
createuser
Creates a user.
The first parameter is the name of the user, and the second parameter is an array of strings that gets added to the postgres createuser
command.
is_running
Returns true if the server is running.
serverPort
Returns the port the server is using
is_ready
Returns true if the server is ready to accept connections.
wait_ready
Waits up to the specified time in seconds, or until the server is ready.
add_config
Appends the specified settings to the postgres configuration.
As Postgres only uses the last entry if the same setting is specified multiple times, this can also be used to modify existing settings.
pg1:add_config({
logging_collector = "on",
log_directory = "'logs'",
log_filename = "'server.log'",
log_min_messages = "'info'",
})
add_hba
Adds an entry to the pg_hba.conf
.
BackgroundThread
Allows scripts to run something in the background.
For a more detailed example, see the section in the Lua Examples page.
run
Starts a background thread, using the specified logfile name and lua function.
join
Waits until the background thread completes.
send
Sends the specified string to the background thread.
receive
Receives a single string from the background thread - blocks and waits if it didn't send anything yet.
receiveIfAny
Receives a single string from the background thread, if it sent anything.
RandomWorker
Represents a randomized worker.
It supports the same functions as the Worker
, with the following additions:
possibleActions
Returns a reference to the action registry associated with the specfic worker. This can be used to modify the actions executed by a single Worker, without affecting other Workers.
Worker
A (non randomized) worker that can be used for initialization and other database related things.
create_random_tables
Creates a specified number of tables according to the DDL configuration.
Currently the DDL configuration can't be modified.
generate_initial_data
Generates some rows in all currently existing tables.
sql_connection
Returns the SQL connection (LoggedSQL) of the worker, which can be used to execute SQL statements directly.
Workload
Represents a testrun, it is created by Node
using initRandomWorkload
.
run
Starts the workload.
The workload is executed in the background, this functions returns immediately after starting. This allows the script to modify the workload, or interact with the SQL servers while the workload is running.
wait_completion
Waits until the currently running workload is completed.
worker
Returns the Worker with the specified index.
Indexing starts with 1.
worker_count
Returns the number of workers.
reconnect_workers
Reconnects any workers that lots their connection because of a restart.