object cache: use the correct unlock flag for lockf()

Manual says we should use F_ULOCK to unlock lockf()

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
zookeeper: fix node_btree_clear()

reset zk_nr_nodes to 0 when clear node_btree

Signed-off-by: Yunkai Zhang <qiushu.zyk@taobao.com>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
zookeeper: fix master transfer logic

When a sheep joins into the cluster, master will call
sd_check_join_cb() to get the join_result which will be
update to ev.buf, and all sheep will receive this update.

If join_result equals to CJ_RES_MASTER_TRANSFER, master will
kill itself by exit(). Zookeeper needs at most SESSION_TIMEOUT
to detect master's leaving action, it's better to call zk_leave()
explicitly before master exit. On the other hand, other sheeps
will continue to process the updated JOIN EVENT.

But now, Sheepdog assumes that only one sheep(the joining sheep)
is alive in MASTER_TRANSFER scenario, this can simplify processing
logic(maybe we will overthrow this assumption in the future for other
corner-case).

Based on this assumption, the joining sheep just need to reset
its member_list(saved in node_btree in zookeeper driver), make
it only contains itself.

Signed-off-by: Yunkai Zhang <qiushu.zyk@taobao.com>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
sheep: lock against concurrent access to the store directory

This patch uses lockf on a specially create lock file in the base directory
to detect early on if another sheep is using this directory and abort with
a useful error message.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
local: do not zero the event queue

Reading, including through mmap from sparse files always returns zeroes,
so there is no need to memset the shm queue.  Zeroing it causes backing
pages to be allocated to it in tmpfs, and thus increases the memory
usage for a not fully used queue.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
consolidate the MAX_EVENT_BUF_SIZE definitions

The accord, local and zookeeper drivers all need an upper bound of the
message size the sheepdog might send for their communication queues.

Centralize the definition into a single place, and make sure we never
send larger messages from the core sheepdog code.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
farm: use access(2) to check file existence

This simplifies the code a bit.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
cluster/accord: fix compile errors

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
sheep: set sys->epoch correctly before becoming master

This fixes a bug introduced by commit 43a2f684e, and necessary to pass
the following testcase:

  #!/bin/bash
  set -ex

  for i in 0 1 2; do ./sheep/sheep /store/$i -z $i -p 700$i; sleep 1; done
  ./collie/collie cluster format
  for i in 0 1 2; do pkill -f "sheep /store/$i"; sleep 1; done
  for i in 1 0 2; do ./sheep/sheep /store/$i -z $i -p 700$i; sleep 1; done
  for i in 0 1 2; do ./sheep/sheep /store/$i -z $i -p 700$i; sleep 1; done
  for i in 0 1 2; do ./collie/collie cluster info -p 700$i; done

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
sheep: update sd_store only when the correct driver name is specified

Users may run 'collie cluster format -b ?' to get the store driver
list.  In such cases, we shouldn't update sd_store.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
script: add a script to transform simple store into Farm

Usage:
script/simple2farm /path/to/store/obj

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
doc: add a API documentation for strbuf

This is excerpted from 'git'

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
sheep: remove useless type casting

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
object cache: fix bmap calculation

It uses 64 bits to represent 4M objects, so each bits will represent 64K data
or object_cache_flush_and_delete() will fail.

- add two eprintf for read/write_cache_object()

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
start traversing from a random node in fill_obj_list()

Every node has the same sd_node order in its epoch, so in
fill_obj_list(), every node starts from a same node to request
the object list, which may cause the node overload.

Indeed, we meet this problem when there's 960 nodes in our
cluster, when in the period of fill_obj_list, some node get
'too many requests' in client_rx_handler(), so I change it
to start from a random node in fill_obj_list() to make load blance.

Signed-off-by: levin li <xingke.lwp@taobao.com>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
farm: fix read_working_object()

We should clear O_DIRECT flag for vdi object

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
sheep: fix recovery logic

Current recovey will fail for following simple test case (really surprised)

=====
set -ex

pkill -9 sheep
rm store/* -rf

# start three sheep daemons
for i in 0 1 2 3; do
    ./sheep/sheep -d /home/tailai.ly/sheepdog/store/$i -z $i -p 700$i -W
done
sleep 1
./collie/collie cluster format -c 2
./collie/collie vdi create test0 40M
./collie/collie vdi create test1 40M
pkill -f "sheep -d /home/tailai.ly/sheepdog/store/3"

=====

after running the script, we can see copy isn't recoveried!

tailai.ly@taobao:~/sheepdog$ find -name '80fd32fc00000000'
./store/3/obj/80fd32fc00000000 # <-- this node is killed
./store/0/obj/80fd32fc00000000

With the patch, we get a expected result: 
tailai.ly@taobao:~/sheepdog$ find -name '80fd32fc00000000'
./store/1/obj/80fd32fc00000000 # copy migrated from node 3
./store/3/obj/80fd32fc00000000 # killed
./store/0/obj/80fd32fc00000000

The failture is rooted in the original algorithm: we just did one shot search
and didn't try a breadth-first search before diving into older configuration.

The fix is rather straightforward, do a breadth-first search!. With the patch,
we also end up with much more simplified code.

Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
obj list cache: add a buffer to object list cache as a second level cache

It's inefficient for get_obj_list() to traverse the object
list rbtree to get the object list every time, so I add a buffer
to cache the list, every time in get_obj_list() if the rbtree
has changed which is detected by the version number, we update
the buffer, then we can just read the buffer by memcpy() if the
object list has not been changed, instead of traverse the full
rbtree.

Signed-off-by: levin li <xingke.lwp@taobao.com>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
sheep: split do_io_request

Split do_io_request into a local store and a gateway version to make the
code more obvious.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>
sheep: move gateway handling out of store.c

Create a new gateway.c file for the gateway handling instead of mixing it
up with the unrelated backend store.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Liu Yuan <tailai.ly@taobao.com>