@@ -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
235247Datum
236248raftable_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 );
0 commit comments