File: bif_mts.c

package info (click to toggle)
virtuoso-opensource 6.1.6+dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 260,992 kB
  • ctags: 125,220
  • sloc: ansic: 652,748; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (186 lines) | stat: -rw-r--r-- 4,950 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 *  bif_mts.c
 *
 *  $Id$
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include "sqlnode.h"
#include "sqlbif.h"

#include "odbcinc.h"
#include "remote.h"
#include "mts.h"
#include "2pc.h"
#include "mts_client.h"

#include "import_gate_virtuoso.h"

long mts_txn_timeout = -1;
int vd_use_mts;

static caddr_t
bif_mts_get_rmcookie (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t cookie = mts_get_rmcookie ();
  if (!cookie)
    {
      sqlr_error("MX008","Could not get resource manager cookie");
      return NEW_DB_NULL;
    }
  else
    {
      caddr_t rmcookie_str = export_mts_bin_encode (cookie, box_length (cookie));
      return rmcookie_str;
    }
};

static caddr_t
bif_mts_enlist_transaction (caddr_t * qst, caddr_t * err_ret,
    state_slot_t ** args)
{
  /* get encoded transaction cookie */
  caddr_t tr_cookie_str =
      bif_string_arg (qst, args, 0, "mts_enlist_transaction");
  caddr_t tr_cookie;
  unsigned long len;

  if (!stricmp(tr_cookie_str,"LOCAL"))
    {
      /* should be changed */
      dbg_printf(("retire lt=%x\n",((query_instance_t*)QST_INSTANCE(qst))->qi_trx));
      tp_retire((query_instance_t*)QST_INSTANCE(qst));
      return box_num (0);
    }
  dbg_printf(("enlisting...\n"));

  if (export_mts_bin_decode (tr_cookie_str, &tr_cookie, &len) == -1)
    sqlr_error("MX001", "could not decode transaction cookie");

  if (mts_trx_enlist (QI_TRX (QST_INSTANCE (qst)), tr_cookie, len))
    {
      dk_free (tr_cookie, -1);
      sqlr_error("MX002", "could not enlist in transaction (%s)",tr_cookie_str);
    };

  dk_free (tr_cookie, -1);
  return box_num (0);
}

static caddr_t
bif_mts_connect (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  /* reconnect? */
  long reconnect_fl = bif_long_arg (qst, args, 0, "mts_connect");
  if (!mts_connect(reconnect_fl))
    {
      sqlr_error ("MX000", "connection to MS DTC failed");
    }

  return box_num(0);
};

static caddr_t
bif_mts_status (caddr_t* qst, caddr_t * err_ret, state_slot_t ** args)
{
    caddr_t param_str =
	bif_string_arg (qst, args, 0, "mts_status");
    if (!stricmp("TRANSACTION",param_str))
    {
	return (caddr_t)mts_transaction_status(QI_TRX(QST_INSTANCE(qst)));
    } else if (!stricmp("MTS",param_str))
    {
	return (caddr_t)mts_server_status();
    }
    sqlr_error("MX000", "unknown parameter %s", param_str);

    return 0;

};

static caddr_t
bif_mts_set_timeout(caddr_t* qst, caddr_t * err_ret, state_slot_t ** args)
{
    long param_timeout =
	bif_long_arg (qst, args, 0, "mts_set_timeout");
    mts_txn_timeout = param_timeout;
    return box_num(0);
};

static caddr_t
bif_mts_get_timeout(caddr_t* qst, caddr_t * err_ret, state_slot_t ** args)
{
    if (-1 == mts_txn_timeout )
      return box_num(((query_instance_t*)qst)->qi_rpc_timeout);
    else
      return box_num(mts_txn_timeout);
};

static caddr_t
bif_mts_usleep(caddr_t* qst, caddr_t * err_ret, state_slot_t ** args)
{
    long msec = bif_long_arg (qst, args, 0, "mts_usleep");
    Sleep(msec);
    return box_num(0);
};


static caddr_t
bif_mts_all_info(caddr_t* qst, caddr_t * err_ret, state_slot_t ** args)
{
  query_instance_t* qi = (query_instance_t*)qst;
  client_connection_t* cli =
      qi->qi_client;
  dbg_printf(("******** cli %x tr %x st %d nthr %d tr2 %x\n",
	 cli,
	 cli->cli_trx,
	 cli->cli_trx->lt_status,
	 cli->cli_trx->lt_threads,
	 qi->qi_trx));
#if 0
  if (!cli->cli_tp_data)
    GPF_T1("should in distributed transaction\n");
#endif
  return NEW_DB_NULL;
}

void
export_mts_bif_init ()
{
  mts_init ();
  if(!mts_connect (1))
    {
      twopc_log (_LOG_ERROR, "MS DTC is not available");
      exit (0);
    }
  bif_define ("mts_get_rmcookie", bif_mts_get_rmcookie);
  bif_define ("mts_enlist_transaction", bif_mts_enlist_transaction);
  bif_define ("mts_connect", bif_mts_connect);
  bif_define ("mts_status", bif_mts_status);

  bif_define ("mts_set_timeout", bif_mts_set_timeout);
  bif_define ("mts_get_timeout", bif_mts_get_timeout);

  bif_define ("mts_sleep", bif_mts_usleep);

  /* bif_define ("__mts_fail_after_prepare", bif_mts_fail_after_prepare); */
  bif_define ("__mts_all_info", bif_mts_all_info);
};