cluster, corosync: add two dprintf

It is helpful, really.

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
cluster, corosync: enlarge is_master() audience

We need this to do mastership transfer in join phase.

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
cluster, corosync: do mastership transfer when master is down in join phase

If master is down before sending response in join phase, we have to
revoke its mastership to avoid cluster hanging.

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
introduce coroutine

We have many works that need to be processed cooperatively
(e.g. object recovery, data I/Os, cluster management).  This library
will enable us to implement them simply and elegantly.

These files are copied from Accord project:
  https://github.com/collie/accord

The following comments are based on qemu git log 00dccaf1:

    Asynchronous code is becoming very complex.  At the same time
    synchronous code is growing because it is convenient to write.
    Sometimes duplicate code paths are even added, one synchronous and the
    other asynchronous.  This patch introduces coroutines which allow code
    that looks synchronous but is asynchronous under the covers.

    A coroutine has its own stack and is therefore able to preserve state
    across blocking operations, which traditionally require callback
    functions and manual marshalling of parameters.

    Creating and starting a coroutine is easy:

      coroutine = coroutine_create(my_coroutine);
      coroutine_enter(coroutine, my_data);

    The coroutine then executes until it returns or yields:

      void my_coroutine(void *opaque) {
          MyData *my_data = opaque;

          /* do some work */

          coroutine_yield();

          /* do some more work */
      }

    Yielding switches control back to the caller of coroutine_enter().
    This is typically used to switch back to the main thread's event
    loop after issuing an asynchronous I/O request.  The request
    callback will then invoke coroutine_enter() once more to switch
    back to the coroutine.

    Note that if coroutines are used only from the main thread, they
    will never execute concurrently.  This makes programming with
    coroutines easier than with threads.  Race conditions cannot occur
    since only one coroutine may be active at any time.  Other
    coroutines can only run across yield.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
sheep: fix cluster event sequences

Cluster drivers cannot call a 'check_join_cb' callback before sheep
finishes the previous event handling.  The simplest approach to solve
this is: 1) call coroutine_yield() before exiting the event handler,
and 2) re-enter the coroutine after the event is completely processed.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
sheep: use valloc for data object I/O

Data objects may be opened with O_DIRECT, so we must allocate aligned
memory here.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
cluster/corosync: stay in the cluster even after cdrv->leave() is called

Although the gateway node cannot work as a storage server, it must
stay in the cluster to keep a consistent hash ring up to date.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
sheep: clean up fill_obj_list()

We already have vnodes layout in 'rw' and it is safe to use it instead of
recalculating vnodes again.

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
sheep: split merge_objlist()

We actually use merge_objlist() for two different purposes, that is screening
and mergeing. Let's split them out to make code more readable, since nothing
these two share.

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
sheep: fix hval_to_sheep()

get_nth_node() doesn't return negative val, so we don't need to check it.

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
sheep: set send/recv timeout

send()/recv() could sleep long time if network failure happens during
network I/Os, and it prevents from incrementing epoch number because
we assumes that there is no outstanding I/O requests while updating
node membership info.  This patch fixes the problem.

It is not a problem to set a small value for timeout because I/Os are
retried automatically even if send/recv timeout has occurred.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
sheep: abstract out get_obj_list()

Current recovery logic needs to query underlying object store of object list
in the specified epoch, in order to calculate object relocation. We need to abstract
it out to adopt sheepdog to more stores.

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
sheep: simplify get_vdi_bitmap_from debug info

Current debug info for e.g,
"get_vdi_bitmap_from(474) getting the vdi bitmap from 127.0.0.1"
looks rather redundant. So let's cut it off.

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
sheep: resume pending I/O requests after setting next recovery

We need to set recovery_work before calling resume_pending_requests()
because sheep checks the value to decide whether object recovery is
running or not.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
add support for reading objects without strong consistency

To keep strong consistency, Sheepdog assumes that no two VMs can open
the same VDI at the same time.  This patch relaxes it and allows us to
read data from opened VDIs by specifying SD_FLAG_CMD_WEAK_CONSISTENCY.
Note that the obtained data may not the latest one.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
collie: use gateway for read I/O requests

Currently, collie sends read I/O requests without using gateway node
but calculating the target node as the sheep daemon does internally.
This is a hack to keep consistency even if the VM sends write requests
to the same objects at the same time.  But this enforces us to retry
the collie command manually when it fails due to node membership
changes.

This patch uses a gateway node for collie's read I/Os, and makes them
like QEMU's ones.  Data consistency of QEMU is not a problem if we use
SD_FLAG_CMD_WEAK_CONSISTENCY for collie's I/Os.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
bash_completion: add support for new collie commands

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
sheep: remove undefined struct

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
Standardize error messages and tidy up remaining messages

This patch includes a number of very trivial consistency and language fixes:

  - Standardize collie error messages printed to stderr to begin with a
    initial capital like other standard unix tools and the C library.
    (Messages written to the sheep.log are already standardized to be
    uncapitalised.)

  - Ensure all error messages are printed to stderr not stdout.

  - Fix some awkward wording, typos and grammatical issues in the messages.

  - Arrange for all of the tabular output from different collie commands to
    be similarly formatted and headings capitalised.

  - Spell VDI as 'VDI' not 'vdi' nor 'Vdi' in messages and comments.

Signed-off-by: Chris Webb <chris@arachsys.com>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
Correct the plural of 'one sheep' to be 'two sheep'

Signed-off-by: Chris Webb <chris@arachsys.com>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>