Skip to content

Commit 0764730

Browse files
committed
Add the 'tries' parameter to the raftable_set() call.
1 parent 496ca14 commit 0764730

File tree

5 files changed

+47
-31
lines changed

5 files changed

+47
-31
lines changed

contrib/raftable/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ C API:
5656

5757
SQL API:
5858
-- set
59-
raftable(key varchar(64), value text);
59+
raftable(key varchar(64), value text, tries int);
6060

6161
-- get
6262
raftable(key varchar(64)) returns text;

contrib/raftable/raftable--1.0.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ AS 'MODULE_PATHNAME','raftable_sql_get'
88
LANGUAGE C;
99

1010
-- set
11-
CREATE FUNCTION raftable(key varchar(64), value text)
11+
CREATE FUNCTION raftable(key varchar(64), value text, tries int)
1212
RETURNS void
1313
AS 'MODULE_PATHNAME','raftable_sql_set'
1414
LANGUAGE C;

contrib/raftable/raftable.c

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,17 @@ raftable_sql_get(PG_FUNCTION_ARGS)
165165
PG_RETURN_NULL();
166166
}
167167

168-
void raftable_set(char *key, char *value)
168+
bool raftable_set(char *key, char *value, int tries)
169169
{
170170
RaftableUpdate *ru;
171171
size_t size = sizeof(RaftableUpdate);
172172
int keylen, vallen = 0;
173+
bool ok = false;
174+
175+
if (tries <= 0)
176+
{
177+
elog(ERROR, "raftable set should be called with 'tries' > 0");
178+
}
173179

174180
keylen = strlen(key) + 1;
175181
if (value) vallen = strlen(value) + 1;
@@ -188,60 +194,67 @@ void raftable_set(char *key, char *value)
188194
memcpy(f->data, key, keylen);
189195
memcpy(f->data + keylen, value, vallen);
190196

191-
bool ok = false;
192-
while (!ok)
197+
tryagain:
198+
if (tries--)
193199
{
194-
fprintf(stderr, "trying to send an update to the leader\n");
195200
int s = get_connection();
196-
int sent = 0;
197-
ok = true;
201+
int sent = 0, recved = 0;
202+
int status;
198203

199204
if (write(s, &size, sizeof(size)) != sizeof(size))
200205
{
201206
disconnect_leader();
202-
fprintf(stderr, "failed to send the update size to the leader\n");
203-
ok = false;
204-
continue;
207+
elog(WARNING, "failed[%d] to send the update size to the leader", tries);
208+
goto tryagain;
205209
}
206210

207-
while (ok && (sent < size))
211+
while (sent < size)
208212
{
209213
int newbytes = write(s, (char *)ru + sent, size - sent);
210214
if (newbytes == -1)
211215
{
212216
disconnect_leader();
213-
fprintf(stderr, "failed to send the update to the leader\n");
214-
ok = false;
217+
elog(WARNING, "failed[%d] to send the update to the leader", tries);
218+
goto tryagain;
215219
}
216220
sent += newbytes;
217221
}
218222

219-
if (ok)
223+
recved = read(s, &status, sizeof(status));
224+
if (recved != sizeof(status))
220225
{
221-
int status;
222-
int recved = read(s, &status, sizeof(status));
223-
if (recved != sizeof(status))
224-
{
225-
disconnect_leader();
226-
fprintf(stderr, "failed to recv the update status from the leader\n");
227-
ok = false;
228-
}
226+
disconnect_leader();
227+
elog(WARNING, "failed to recv the update status from the leader\n");
228+
goto tryagain;
229229
}
230+
goto success;
231+
}
232+
else
233+
{
234+
goto failure;
230235
}
231236

237+
failure:
238+
elog(WARNING, "failed all tries to set raftable value\n");
239+
pfree(ru);
240+
return false;
241+
242+
success:
232243
pfree(ru);
244+
return true;
233245
}
234246

235247
Datum
236248
raftable_sql_set(PG_FUNCTION_ARGS)
237249
{
238250
char *key = text_to_cstring(PG_GETARG_TEXT_P(0));
251+
int tries = PG_GETARG_INT32(2);
239252
if (PG_ARGISNULL(1))
240-
raftable_set(key, NULL);
253+
raftable_set(key, NULL, tries);
241254
else
242255
{
243256
char *value = text_to_cstring(PG_GETARG_TEXT_P(1));
244-
raftable_set(key, value);
257+
raftable_set(key, value, tries);
245258
pfree(value);
246259
}
247260
pfree(key);

contrib/raftable/raftable.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ char *raftable_get(char *key);
66

77
/*
88
* Adds/updates value by key. Returns when the value gets replicated.
9-
* Storing NULL will delete the item from the table.
9+
* Storing NULL will delete the item from the table. Give up after 'tries'
10+
* tries have failed.
1011
*/
11-
void raftable_set(char *key, char *value);
12+
bool raftable_set(char *key, char *value, int tries);
1213

1314
/*
1415
* Iterates over all items in the table, calling func(key, value, arg)

contrib/raftable/t/000_basic.pl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ sub start_nodes
6868
world => genstr(3000),
6969
);
7070

71-
$able->psql('postgres', "select raftable('hello', '$tests{hello}');");
72-
$baker->psql('postgres', "select raftable('and', '$tests{and}');");
73-
$charlie->psql('postgres', "select raftable('goodbye', '$tests{goodbye}');");
71+
my $tries = 10;
72+
73+
$able->psql('postgres', "select raftable('hello', '$tests{hello}', $tries);");
74+
$baker->psql('postgres', "select raftable('and', '$tests{and}', $tries);");
75+
$charlie->psql('postgres', "select raftable('goodbye', '$tests{goodbye}', $tries);");
7476
$baker->stop;
75-
$able->psql('postgres', "select raftable('world', '$tests{world}');");
77+
$able->psql('postgres', "select raftable('world', '$tests{world}', $tries);");
7678

7779
$baker->start;
7880
sleep(5);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy