Exercise 3: using Bedrock and composing with other services
In this exercise we will use Bedrock to deploy a daemon containing an instance of our phonebook provider. We will then implement a phonebook backend that uses Yokan, and organize the composition of the two within the same daemon. Everything in this exercise relies on the codebase from Exercise 2, however you don’t need to have completed Exercise 2 to do this exercise.
First, make sure that the Spack environment from Exercise 2 is activated.
From the build
directory, re-run cmake as follows.
cmake .. -DENABLE_TESTS=ON -DENABLE_BEDROCK=ON
make
This time a libYP-bedrock-module.so is being built. This is the Bedrock module for our phonebook service, i.e. the library that tells Bedrock how to instanciate and use our phonebook provider.
To make sure Bedrock finds this library, execute the following command from the build directory.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`/src
examples/bedrock-config.json
is an example of Bedrock
configuration that spins up a phonebook provider with provider ID 42.
This provider manages one phonebook of type “dummy”. You can try
our this configuration using the bedrock program as follows.
bedrock na+sm -c ../examples/bedrock-config.json
You can copy the address printed by bedrock on the last line of its log, and in another terminal (don’t forget to activate your spack environment), run the following command.
bedrock-query na+sm -a <copied-address> -p
You will see the current configuration of the service, including a phonebook provider that manages a phonebook. Bedrock has completed the input configuration with a lot of information about Mercury, Argobots, etc. These information can be very useful to communicate to Mochi developers when you try to find out what’s wrong with your service.
We will now add Yokan in our service. To add Yokan as dependency to our spack environment, run the following command.
spack add mochi-yokan+bedrock
spack install
This will install Yokan.
Important
spack add
will add the dependency to your active environment,
but you should also make sure to list this new dependency in the list of
specs in spack.yaml
at the root of your project so that it is
taken into account next time you want to build a new environment from it.
Edit CMakeLists.txt
to add find_package(yokan REQUIRED)
(e.g. after the call to find_package(PkgConfig REQUIRED)
).
Note
When developing your own service, don’t forget to also edit
the src/*.cmake.in
and src/*.pc.in
files to add
relevant dependencies there. Those are the files used by cmake
and pkg-config respectively to search for dependencies when
people are using your code.
Edit src/CMakeLists.txt
to add yokan-client
as a
dependency for the YP-server
library (i.e. find the call
to target_link_libraries
for YP-server
and add yokan-client
in the list of public dependencies).
From the build
directory, re-run cmake ..
to make it find Yokan.
Open examples/bedrock-config.json
and add the Yokan
library in the libraries section.
"yokan": "libyokan-bedrock-module.so"
In this file as well, we will instanciate a Yokan provider with a Yokan database. In the providers section, before the phonebook provider, add the following provider definition.
{
"type": "yokan",
"name": "my-yokan-provider",
"provider_id": 123,
"config": {
"databases": [
{
"type": "map",
"name": "my-db"
}
]
}
},
If you re-run bedrock
with this new configuration then
call bedrock-query
, you should be able to confirm that
your Bedrock daemon is now running two providers: one YP provider
and one Yokan provider. Of course, these two don’t know about each
other, they simply share the resources of the same process.
We will now introduce a dependency between YP and Yokan.
Edit src/bedrock-module.c
and find the struct bedrock_module
definition at the end. Its provider_dependencies
field is where we
will be able to introduce this dependency on Yokan. Before the declaration
of the bedrock_module
structure, add the following declaration:
static struct bedrock_dependency provider_dependencies[] = {
{ "yokan_ph", "yokan", BEDROCK_REQUIRED },
BEDROCK_NO_MORE_DEPENDENCIES
};
The first field, "yokan_ph"
, is the name by which YP
will reference this dependency. "yokan"
is the type of dependency.
BEDROCK_REQUIRED
indicates that this dependency is required.
You can now assign the field in the bedrock_module
structure.
.provider_dependencies = provider_dependencies
If you rebuild your code now and re-run the Bedrock configuration, it will display an error message:
[critical] Missing dependency yokan_ph in configuration
So let’s fix that by going again into examples/bedrock-config.json
,
and add the following in the field in the definition of our YP provider.
"dependencies": {
"yokan_ph": "yokan:123@local"
}
You can also use "my-yokan-provider"
instead of "yokan:123"
.
Now Bedrock should restart accepting your configuration.
In include/YP/YP-server.h
, include the yokan/provider-handle.h
header and add a yokan_provider_handle_t yokan_ph
field in the
YP_provider_args
structure.
Edit src/bedrock-module.c
once again. This time we will look at the
YP_register_provider
function at the beginning of the file. Use
bedrock_args_get_dependency(args, "yokan_ph", 0);
to retrieve a
pointer to a Yokan provider handle (yoken_provider_handle_t
),
which you can now use to set the corresponding field in the YP_args
structure. You have successfully injected a Yokan dependency into the YP provider!
Bonus: continuing to wire YP with Yokan
At this point, you have learned what this exercise aimed for you to learn, namely how to write a Bedrock module, a Bedrock configuration with multiple providers, and how to manage dependencies in Bedrock configurations and Bedrock modules.
This bonus section invites you to complete the dependency injection of Yokan into YP. It is a lot more complicated and a lot less guided, hence feel free to stop there and ignore this bonus section.
The goal is now to pass this Yokan provider handle down to the dummy phonebook
so that it can use Yokan as an implementation of a key/value store instead of
relying on the phonebook.h
implementation. You should now be familiar
enough with the code to make the necessary changes bellow without
too much guidance. Keep the
API of Yokan
open in a web browser for reference.
First, you will need to register a Yokan provider in your
tests (tests/test-admin.cpp
and tests/test-client.cpp
) and
make sure it manages at least one database by passing it an appropriate
configuration string. Here we are doing manually what Bedrock would normally do
from a JSON configuration file.
To add and keep a reference to the Yokan provider handle in the YP_provider
structure (in src/provider.h
), you will need to copy the provided
yokan_provider_handle_t
in YP_provider_register
, and free
this copy in YP_finalize_provider
(in src/provider.c
).
yokan_provider_handle_t
is a public structure with no
yokan_provider_handle_ref_incr
function. You will have to manually copy
its fields, and call margo_addr_ref_incr
on the hg_addr_t
field to
increase the reference count of the address (and call margo_addr_free
on
it in YP_finalize_provider
).
To be able to pass the Yokan provider handle down to a backend
(e.g. a dummy phonebook), you will need to change the signature of
the functions that create and open a phonebook (in include/YP/YP-backend.h
).
This then implies changing their implementation (in src/dummy/dummy-backend.c
).
You will need to tell your dummy phonebook backend which database to use.
Yokan databases can be identified by a name, so you may want to implement
a way to look for the name of this database in the configuration passed
to the phonebook (in the dummy_create_phonebook
and
dummy_open_phonebook
functions in src/dummy/dummy-backend.c
).
Once a backend knows the name of the database it should use, you can use
yk_database_find_by_name
to look for its ID, then yk_database_handle_create
to create a handle for the database (don’t forget to call yk_database_handle_release
when you no longer need it, e.g. when closing/destroying the dummy backend).
In the insert and lookup functions of the dummy phonebook, you may
now use yk_put
and yk_get
to put and get phone numbers.
In practice, you could copy the dummy backend implementation into a new type of backend that specifically uses Yokan. Don’t hesitate to implement multiple backends for your service, with different dependencies or different strategies for solving the same problem.