Deleted Added
full compact
primary.c (211881) primary.c (211882)
1/*-
2 * Copyright (c) 2009 The FreeBSD Foundation
3 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4 * All rights reserved.
5 *
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
8 *

--- 15 unchanged lines hidden (view full) ---

24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2009 The FreeBSD Foundation
3 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4 * All rights reserved.
5 *
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
8 *

--- 15 unchanged lines hidden (view full) ---

24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sbin/hastd/primary.c 211881 2010-08-27 14:12:53Z pjd $");
32__FBSDID("$FreeBSD: head/sbin/hastd/primary.c 211882 2010-08-27 14:26:37Z pjd $");
33
34#include <sys/types.h>
35#include <sys/time.h>
36#include <sys/bio.h>
37#include <sys/disk.h>
38#include <sys/refcount.h>
39#include <sys/stat.h>
40

--- 105 unchanged lines hidden (view full) ---

146#define HAST_HIO_MAX 256
147/*
148 * Number of components. At this point there are only two components: local
149 * and remote, but in the future it might be possible to use multiple local
150 * and remote components.
151 */
152#define HAST_NCOMPONENTS 2
153/*
33
34#include <sys/types.h>
35#include <sys/time.h>
36#include <sys/bio.h>
37#include <sys/disk.h>
38#include <sys/refcount.h>
39#include <sys/stat.h>
40

--- 105 unchanged lines hidden (view full) ---

146#define HAST_HIO_MAX 256
147/*
148 * Number of components. At this point there are only two components: local
149 * and remote, but in the future it might be possible to use multiple local
150 * and remote components.
151 */
152#define HAST_NCOMPONENTS 2
153/*
154 * Number of seconds to sleep before next reconnect try.
154 * Number of seconds to sleep between keepalive packets.
155 */
155 */
156#define KEEPALIVE_SLEEP 10
157/*
158 * Number of seconds to sleep between reconnect retries.
159 */
156#define RECONNECT_SLEEP 5
157
158#define ISCONNECTED(res, no) \
159 ((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL)
160
161#define QUEUE_INSERT1(hio, name, ncomp) do { \
162 bool _wakeup; \
163 \

--- 717 unchanged lines hidden (view full) ---

881 pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr);
882
883 /*
884 * Stop synchronization if in-progress.
885 */
886 sync_stop();
887
888 /*
160#define RECONNECT_SLEEP 5
161
162#define ISCONNECTED(res, no) \
163 ((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL)
164
165#define QUEUE_INSERT1(hio, name, ncomp) do { \
166 bool _wakeup; \
167 \

--- 717 unchanged lines hidden (view full) ---

885 pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr);
886
887 /*
888 * Stop synchronization if in-progress.
889 */
890 sync_stop();
891
892 /*
889 * Wake up guard thread, so it can immediately start reconnect.
893 * Wake up guard thread (if we are not called from within guard thread),
894 * so it can immediately start reconnect.
890 */
895 */
891 mtx_lock(&hio_guard_lock);
892 cv_signal(&hio_guard_cond);
893 mtx_unlock(&hio_guard_lock);
896 if (!mtx_owned(&hio_guard_lock)) {
897 mtx_lock(&hio_guard_lock);
898 cv_signal(&hio_guard_cond);
899 mtx_unlock(&hio_guard_lock);
900 }
894}
895
896/*
897 * Thread receives ggate I/O requests from the kernel and passes them to
898 * appropriate threads:
899 * WRITE - always goes to both local_send and remote_send threads
900 * READ (when the block is up-to-date on local component) -
901 * only local_send thread

--- 827 unchanged lines hidden (view full) ---

1729 break;
1730 case SIGHUP:
1731 sighup_received = true;
1732 break;
1733 default:
1734 assert(!"invalid condition");
1735 }
1736 /*
901}
902
903/*
904 * Thread receives ggate I/O requests from the kernel and passes them to
905 * appropriate threads:
906 * WRITE - always goes to both local_send and remote_send threads
907 * READ (when the block is up-to-date on local component) -
908 * only local_send thread

--- 827 unchanged lines hidden (view full) ---

1736 break;
1737 case SIGHUP:
1738 sighup_received = true;
1739 break;
1740 default:
1741 assert(!"invalid condition");
1742 }
1743 /*
1737 * XXX: Racy, but if we cannot obtain hio_guard_lock here, we don't
1744 * Racy, but if we cannot obtain hio_guard_lock here, we don't
1738 * want to risk deadlock.
1739 */
1740 unlock = mtx_trylock(&hio_guard_lock);
1741 cv_signal(&hio_guard_cond);
1742 if (unlock)
1743 mtx_unlock(&hio_guard_lock);
1744}
1745

--- 100 unchanged lines hidden (view full) ---

1846 proto_close(newcfg->hc_controlconn);
1847 if (newcfg->hc_listenconn != NULL)
1848 proto_close(newcfg->hc_listenconn);
1849 yy_config_free(newcfg);
1850 }
1851 pjdlog_warning("Configuration not reloaded.");
1852}
1853
1745 * want to risk deadlock.
1746 */
1747 unlock = mtx_trylock(&hio_guard_lock);
1748 cv_signal(&hio_guard_cond);
1749 if (unlock)
1750 mtx_unlock(&hio_guard_lock);
1751}
1752

--- 100 unchanged lines hidden (view full) ---

1853 proto_close(newcfg->hc_controlconn);
1854 if (newcfg->hc_listenconn != NULL)
1855 proto_close(newcfg->hc_listenconn);
1856 yy_config_free(newcfg);
1857 }
1858 pjdlog_warning("Configuration not reloaded.");
1859}
1860
1861static void
1862keepalive_send(struct hast_resource *res, unsigned int ncomp)
1863{
1864 struct nv *nv;
1865
1866 nv = nv_alloc();
1867 nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
1868 if (nv_error(nv) != 0) {
1869 nv_free(nv);
1870 pjdlog_debug(1,
1871 "keepalive_send: Unable to prepare header to send.");
1872 return;
1873 }
1874 if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) < 0) {
1875 pjdlog_common(LOG_DEBUG, 1, errno,
1876 "keepalive_send: Unable to send request");
1877 nv_free(nv);
1878 rw_unlock(&hio_remote_lock[ncomp]);
1879 remote_close(res, ncomp);
1880 rw_rlock(&hio_remote_lock[ncomp]);
1881 return;
1882 }
1883 nv_free(nv);
1884 pjdlog_debug(2, "keepalive_send: Request sent.");
1885}
1886
1854/*
1855 * Thread guards remote connections and reconnects when needed, handles
1856 * signals, etc.
1857 */
1858static void *
1859guard_thread(void *arg)
1860{
1861 struct hast_resource *res = arg;

--- 7 unchanged lines hidden (view full) ---

1869 if (sigexit_received) {
1870 primary_exitx(EX_OK,
1871 "Termination signal received, exiting.");
1872 }
1873 if (sighup_received) {
1874 sighup_received = false;
1875 config_reload();
1876 }
1887/*
1888 * Thread guards remote connections and reconnects when needed, handles
1889 * signals, etc.
1890 */
1891static void *
1892guard_thread(void *arg)
1893{
1894 struct hast_resource *res = arg;

--- 7 unchanged lines hidden (view full) ---

1902 if (sigexit_received) {
1903 primary_exitx(EX_OK,
1904 "Termination signal received, exiting.");
1905 }
1906 if (sighup_received) {
1907 sighup_received = false;
1908 config_reload();
1909 }
1877 /*
1878 * If all the connection will be fine, we will sleep until
1879 * someone wakes us up.
1880 * If any of the connections will be broken and we won't be
1881 * able to connect, we will sleep only for RECONNECT_SLEEP
1882 * seconds so we can retry soon.
1883 */
1884 timeout = 0;
1910
1911 timeout = KEEPALIVE_SLEEP;
1885 pjdlog_debug(2, "remote_guard: Checking connections.");
1886 mtx_lock(&hio_guard_lock);
1887 for (ii = 0; ii < ncomps; ii++) {
1888 if (!ISREMOTE(ii))
1889 continue;
1890 rw_rlock(&hio_remote_lock[ii]);
1891 if (ISCONNECTED(res, ii)) {
1892 assert(res->hr_remotein != NULL);
1893 assert(res->hr_remoteout != NULL);
1912 pjdlog_debug(2, "remote_guard: Checking connections.");
1913 mtx_lock(&hio_guard_lock);
1914 for (ii = 0; ii < ncomps; ii++) {
1915 if (!ISREMOTE(ii))
1916 continue;
1917 rw_rlock(&hio_remote_lock[ii]);
1918 if (ISCONNECTED(res, ii)) {
1919 assert(res->hr_remotein != NULL);
1920 assert(res->hr_remoteout != NULL);
1921 keepalive_send(res, ii);
1922 }
1923 if (ISCONNECTED(res, ii)) {
1924 assert(res->hr_remotein != NULL);
1925 assert(res->hr_remoteout != NULL);
1894 rw_unlock(&hio_remote_lock[ii]);
1895 pjdlog_debug(2,
1896 "remote_guard: Connection to %s is ok.",
1897 res->hr_remoteaddr);
1898 } else if (real_remote(res)) {
1899 assert(res->hr_remotein == NULL);
1900 assert(res->hr_remoteout == NULL);
1901 /*

--- 41 unchanged lines hidden ---
1926 rw_unlock(&hio_remote_lock[ii]);
1927 pjdlog_debug(2,
1928 "remote_guard: Connection to %s is ok.",
1929 res->hr_remoteaddr);
1930 } else if (real_remote(res)) {
1931 assert(res->hr_remotein == NULL);
1932 assert(res->hr_remoteout == NULL);
1933 /*

--- 41 unchanged lines hidden ---