os/LevelDBStore: do compact_prefix() work asynchronously

We generally do not want to block while compacting a range of leveldb.
Push the blocking+waiting off to a separate thread.  (leveldb will do what
it can to avoid blocking internally; no reason for us to wait explicitly.)

This addresses part of #5176.

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit 4af917d4478ec07734a69447420280880d775fa2)
os/LevelDBStore: allow compaction of key ranges

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit e20c9a3f79ccfeb816ed634ca25de29fc5975ea8)
mon/MonitorDBStore: allow compaction of ranges

Allow a transaction to describe the compaction of a range of keys.  Do this
in a backward compatible say, such that older code will interpret the
compaction of a prefix + range as compaction of the entire prefix.  This
allows us to avoid introducing any new feature bits.

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit ab09f1e5c1305a64482ebbb5a6156a0bb12a63a4)

Conflicts:

	src/mon/MonitorDBStore.h
mon: compact trimmed range, not entire prefix

This will reduce the work that leveldb is asked to do by only triggering
compaction of the keys that were just trimmed.

We ma want to further reduce the work by compacting less frequently, but
this is at least a step in that direction.

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit 6da4b20ca53fc8161485c8a99a6b333e23ace30e)
os/LevelDBStore: merge adjacent ranges in compactionqueue

If we get behind and multiple adjacent ranges end up in the queue, merge
them so that we fire off compaction on larger ranges.

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit f628dd0e4a5ace079568773edfab29d9f764d4f0)
mon: make compaction bounds overlap

When we trim items N to M, compact over range (N-1) to M so that the
items in the queue will share bounds and get merged.  There is no harm in
compacting over a larger range here when the lower bound is a key that
doesn't exist anyway.

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit a47ca583980523ee0108774b466718b303bd3f46)
os/LevelDBStore: add perfcounters

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit 7802292e0a49be607d7ba139b44d5ea1f98e07e6)
mon: return instead of exit(3) via preforker

This lets us run all the locally-scoped dtors so that leak checking will
work.

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit 3c5706163b72245768958155d767abf561e6d96d)
mon: fix leak of health_monitor and config_key_service

Switch to using regular pointers here.  The lifecycle of these services is
very simple such that refcounting is overkill.

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit c888d1d3f1b77e62d1a8796992e918d12a009b9d)
mon: destroy MonitorDBStore before g_ceph_context

Put it on the heap so that we can destroy it before the g_ceph_context
cct that it references.  This fixes a crash like

*** Caught signal (Segmentation fault) **
in thread 4034a80
ceph version 0.63-204-gcf9aa7a (cf9aa7a0037e56eada8b3c1bb59d59d0bfe7bba5)
1: ceph-mon() [0x59932a]
2: (()+0xfcb0) [0x4e41cb0]
3: (Mutex::Lock(bool)+0x1b) [0x6235bb]
4: (PerfCountersCollection::remove(PerfCounters*)+0x27) [0x6a0877]
5: (LevelDBStore::~LevelDBStore()+0x1b) [0x582b2b]
6: (LevelDBStore::~LevelDBStore()+0x9) [0x582da9]
7: (main()+0x1386) [0x48db16]
8: (__libc_start_main()+0xed) [0x658076d]
9: ceph-mon() [0x4909ad]

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit df2d06db6f3f7e858bdadcc8cd2b0ade432df413)
fix test users of LevelDBStore

Need to pass in cct.

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit 446e0770c77de5d72858dcf7a95c5b19f642cf98)
common/Preforker: fix warnings

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit a284c9ece85f11d020d492120be66a9f4c997416)
mon/Paxos: adjust trimming defaults up; rename options

- trim more at a time (by an order of magnitude)
- rename fields to paxos_trim_{min,max}; only trim when there are min items
  that are trimmable, and trim at most max items at a time.
- adjust the paxos_service_trim_{min,max} values up by a factor of 2.

Since we are compacting every time we trim, adjusting these up mean less
frequent compactions and less overall work for the monitor.

Signed-off-by: Sage Weil <sage@inktank.com>
Reviewed-by: Greg Farnum <greg@inktank.com>
(cherry picked from commit 6b8e74f0646a7e0d31db24eb29f3663fafed4ecc)
msgr: add Messenger reference to Connection

This allows us to get the messenger associated with a connection.

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit 92a558bf0e5fee6d5250e1085427bff22fe4bbe4)
mon: discard messages from disconnected clients

If the client is not connected, discard the message.  They will
reconnect and resend anyway, so there is no point in processing it
twice (now and later).

Signed-off-by: Sage Weil <sage@inktank.com>
Reviewed-by: Greg Farnum <greg@inktank.com>
(cherry picked from commit fb3cd0c2a8f27a1c8d601a478fd896cc0b609011)
mon: start lease timer from peon_init()

In the scenario:

 - leader wins, peons lose
 - leader sees it is too far behind on paxos and bootstraps
 - leader tries to sync with someone, waits for a quorum of the others
 - peons sit around forever waiting

The problem is that they never time out because paxos never issues a lease,
which is the normal timeout that lets them detect a leader failure.

Avoid this by starting the lease timeout as soon as we lose the election.
The timeout callback just does a bootstrap and does not rely on any other
state.

I see one possible danger here: there may be some "normal" cases where the
leader takes a long time to issue its first lease that we currently
tolerate, but won't with this new check in place.  I hope that raising
the lease interval/timeout or reducing the allowed paxos drift will make
that a non-issue.  If it is problematic, we will need a separate explicit
"i am alive" from the leader while it is getting ready to issue the lease
to prevent a live-lock.

Backport: cuttlefish, bobtail
Signed-off-by: Sage Weil <sage@inktank.com>
Reviewed-by: Greg Farnum <greg@inktank.com>
(cherry picked from commit f1ccb2d808453ad7ef619c2faa41a8f6e0077bd9)
msgr: add get_messenger() to Connection

This was part of commit 27381c0c6259ac89f5f9c592b4bfb585937a1cfc.

Signed-off-by: Sage Weil <sage@inktank.com>
os/LevelDBStore: fix merge loop

We were double-incrementing p, both in the for statement and in the
body.  While we are here, drop the unnecessary else's.

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit eb6d5fcf994d2a25304827d7384eee58f40939af)
test_librbd: use correct type for varargs snap test

uint64_t is passed in, but int was extracted. This fails on 32-bit builds.

Fixes: #5220
Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
(cherry picked from commit 17029b270dee386e12e5f42c2494a5feffd49b08)