MCPcopy Create free account
hub / github.com/bloomberg/comdb2 / cdb2_connect_sqlhost

Function cdb2_connect_sqlhost

cdb2api/cdb2api.c:2702–2754  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2700}
2701
2702static int cdb2_connect_sqlhost(cdb2_hndl_tp *hndl)
2703{
2704 if (hndl->flags & CDB2_TYPE_IS_FD) {
2705 return newsql_connect_via_fd(hndl);
2706 }
2707
2708 if (hndl->sb) {
2709 newsql_disconnect(hndl, hndl->sb, __LINE__);
2710 }
2711
2712 int requery_done = 0;
2713
2714retry_connect:
2715 debugprint("node_seq=%d flags=0x%x, num_hosts=%d, num_hosts_sameroom=%d\n",
2716 hndl->node_seq, hndl->flags, hndl->num_hosts,
2717 hndl->num_hosts_sameroom);
2718
2719 if ((hndl->node_seq == 0) &&
2720 ((hndl->flags & CDB2_RANDOM) || ((hndl->flags & CDB2_RANDOMROOM) &&
2721 (hndl->num_hosts_sameroom == 0)))) {
2722 hndl->node_seq = getRandomExclude(hndl->num_hosts, hndl->master);
2723 } else if ((hndl->flags & CDB2_RANDOMROOM) && (hndl->node_seq == 0) &&
2724 (hndl->num_hosts_sameroom > 0)) {
2725 hndl->node_seq =
2726 getRandomExclude(hndl->num_hosts_sameroom, hndl->master);
2727 /* First try on same room. */
2728 if (0 == cdb2_try_connect_range(hndl, hndl->node_seq,
2729 hndl->num_hosts_sameroom))
2730 return 0;
2731 }
2732
2733 if (0 == cdb2_try_connect_range(hndl, hndl->node_seq, hndl->num_hosts))
2734 return 0;
2735
2736 if (hndl->sb == NULL) {
2737 /* Can't connect to any of the non-master nodes, try connecting to
2738 * master.*/
2739 /* After this retry on other nodes. */
2740 bzero(hndl->hosts_connected, sizeof(hndl->hosts_connected));
2741 if (newsql_connect(hndl, hndl->master) == 0)
2742 return 0;
2743 }
2744
2745 /* Can't connect to any of the nodes, re-check information about db. */
2746 if (!(hndl->flags & CDB2_DIRECT_CPU) && requery_done == 0 &&
2747 cdb2_get_dbhosts(hndl) == 0) {
2748 requery_done = 1;
2749 goto retry_connect;
2750 }
2751
2752 hndl->connected_host = -1;
2753 return -1;
2754}
2755
2756static inline void ack(cdb2_hndl_tp *hndl)
2757{

Callers 2

cdb2_next_record_intFunction · 0.85

Calls 6

newsql_connect_via_fdFunction · 0.85
newsql_disconnectFunction · 0.85
getRandomExcludeFunction · 0.85
cdb2_try_connect_rangeFunction · 0.85
newsql_connectFunction · 0.85
cdb2_get_dbhostsFunction · 0.85

Tested by

no test coverage detected