Skip to content

Commit ab6e84b

Browse files
committed
Merge branch 'master' of github.com:postgrespro/pg_dtm
2 parents b616354 + 635fa25 commit ab6e84b

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

dtmd/include/transaction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef struct L2List
1919

2020
typedef struct Transaction {
2121
L2List elem;
22+
struct Transaction* collision;
2223
xid_t xid;
2324
xid_t xmin;
2425

dtmd/src/main.c

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,20 @@ static xid_t get_global_xmin();
2323
L2List active_transactions = {&active_transactions, &active_transactions};
2424
L2List* free_transactions;
2525

26+
Transaction* transaction_hash[MAX_TRANSACTIONS];
27+
2628
// We reserve the local xids if they fit between (prev, next) range, and
2729
// reserve something in (next, x) range otherwise, moving 'next' after 'x'.
2830
xid_t prev_gxid, next_gxid;
2931
xid_t global_xmin = INVALID_XID;
3032

33+
static Transaction *find_transaction(xid_t xid) {
34+
Transaction *t;
35+
for (t = transaction_hash[xid % MAX_TRANSACTIONS]; t != NULL && t->xid != xid; t = t->collision);
36+
return t;
37+
38+
}
39+
3140
typedef struct client_userdata_t {
3241
int id;
3342
int snapshots_sent;
@@ -54,6 +63,9 @@ static void free_client_userdata(client_userdata_t *cd) {
5463
}
5564

5665
inline static void free_transaction(Transaction* t) {
66+
Transaction** tpp;
67+
for (tpp = &transaction_hash[t->xid % MAX_TRANSACTIONS]; *tpp != t; tpp = &(*tpp)->collision);
68+
*tpp = t->collision;
5769
l2_list_unlink(&t->elem);
5870
t->elem.next = free_transactions;
5971
free_transactions = &t->elem;
@@ -116,26 +128,19 @@ static void ondisconnect(client_t client) {
116128
debug("[%d] disconnected\n", CLIENT_ID(client));
117129

118130
if (CLIENT_XID(client) != INVALID_XID) {
119-
Transaction* t;
120-
121-
// need to abort the transaction this client is participating in
122-
for (t = (Transaction*)active_transactions.next; t != (Transaction*)&active_transactions; t = (Transaction*)t->elem.next) {
123-
if (t->xid == CLIENT_XID(client)) {
124-
if (clog_write(clg, t->xid, NEGATIVE)) {
125-
notify_listeners(t, NEGATIVE);
126-
free_transaction(t);
127-
} else {
128-
shout(
129-
"[%d] DISCONNECT: transaction %u"
130-
" failed to abort O_o\n",
131-
CLIENT_ID(client), t->xid
131+
Transaction* t = find_transaction(CLIENT_XID(client));
132+
if (t != NULL) {
133+
if (clog_write(clg, t->xid, NEGATIVE)) {
134+
notify_listeners(t, NEGATIVE);
135+
free_transaction(t);
136+
} else {
137+
shout(
138+
"[%d] DISCONNECT: transaction %u"
139+
" failed to abort O_o\n",
140+
CLIENT_ID(client), t->xid
132141
);
133-
}
134-
break;
135-
}
136-
}
137-
138-
if (t == (Transaction*)&active_transactions) {
142+
}
143+
} else {
139144
shout(
140145
"[%d] DISCONNECT: transaction %u not found O_o\n",
141146
CLIENT_ID(client), CLIENT_XID(client)
@@ -281,6 +286,8 @@ static void onbegin(client_t client, int argc, xid_t *argv) {
281286
}
282287
transaction_clear(t);
283288
l2_list_link(&active_transactions, &t->elem);
289+
t->collision = transaction_hash[t->xid % MAX_TRANSACTIONS];
290+
transaction_hash[t->xid % MAX_TRANSACTIONS] = t;
284291

285292
prev_gxid = t->xid = next_gxid++;
286293
t->snapshots_count = 0;
@@ -318,16 +325,6 @@ static void onbegin(client_t client, int argc, xid_t *argv) {
318325
} client_message_finish(client);
319326
}
320327

321-
static Transaction *find_transaction(xid_t xid) {
322-
Transaction *t;
323-
324-
for (t = (Transaction*)active_transactions.next; t != (Transaction*)&active_transactions; t = (Transaction*)t->elem.next) {
325-
if (t->xid == xid) {
326-
return t;
327-
}
328-
}
329-
return NULL;
330-
}
331328

332329
static bool queue_for_transaction_finish(client_t client, xid_t xid, char cmd) {
333330
assert((cmd >= 'a') && (cmd <= 'z'));

0 commit comments

Comments
 (0)