2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
16 #ifdef AFS_PTHREAD_ENV
17 # include <opr/lock.h>
20 #ifdef IGNORE_SOME_GCC_WARNINGS
21 # pragma GCC diagnostic warning "-Wstrict-prototypes"
25 #include "afsincludes.h"
28 #include <afs/pthread_glock.h>
32 #include <afs/rxgen_consts.h>
33 #define UBIK_LEGACY_CALLITER
36 short ubik_initializationState; /*!< initial state is zero */
40 * \brief Parse list for clients.
43 ubik_ParseClientList(int argc, char **argv, afs_uint32 * aothers)
52 inServer = 0; /* haven't seen -servers yet */
54 for (i = 1; i < argc; i++) {
55 /* look for -servers argument */
61 /* otherwise this is a new host name */
63 th = gethostbyname(tp);
68 memmove((void *)&temp, (const void *)th->h_addr,
71 if (counter++ >= MAXSERVERS)
75 /* haven't seen a -server yet */
76 if (!strcmp(tp, "-servers")) {
82 /* never saw a -server */
85 if (counter < MAXSERVERS)
86 *aothers++ = 0; /* null terminate if room */
90 #ifdef AFS_PTHREAD_ENV
93 static pthread_once_t random_once = PTHREAD_ONCE_INIT;
94 static int called_afs_random_once;
95 static pthread_key_t random_number_key;
100 opr_Verify(pthread_key_create(&random_number_key, NULL) == 0);
101 called_afs_random_once = 1;
106 #if !defined(UKERNEL)
108 * \brief use time and pid to try to get some initial randomness.
110 #define ranstage(x) (x)= (afs_uint32) (3141592621U*((afs_uint32)x)+1)
113 * \brief Random number generator and constants from KnuthV2 2d ed, p170
116 * X = (aX + c) % m \n
117 * m is a power of two \n
119 * a is 0.73m should be 0.01m .. 0.99m \n
120 * c is more or less immaterial. 1 or a is suggested. \n
122 * NB: LOW ORDER BITS are not very random. To get small random numbers,
123 * treat result as <1, with implied binary point, and multiply by
126 * NB: Has to be unsigned, since shifts on signed quantities may preserve
129 * In this case, m == 2^32, the mod operation is implicit. a == pi, which
130 * is used because it has some interesting characteristics (lacks any
131 * interesting bit-patterns).
136 #ifdef AFS_PTHREAD_ENV
139 if (!called_afs_random_once)
140 pthread_once(&random_once, afs_random_once);
142 state = (uintptr_t) pthread_getspecific(random_number_key);
144 static afs_uint32 state = 0;
149 state = time(0) + getpid();
150 for (i = 0; i < 15; i++) {
156 #ifdef AFS_PTHREAD_ENV
157 pthread_setspecific(random_number_key, (const void *)(uintptr_t)state);
164 * \brief Returns int 0..14 using the high bits of a pseudo-random number instead of
165 * the low bits, as the low bits are "less random" than the high ones...
167 * \todo Slight roundoff error exists, an excercise for the reader.
169 * Need to multiply by something with lots of ones in it, so multiply by
170 * 8 or 16 is right out.
173 afs_randomMod15(void)
177 temp = afs_random() >> 4;
178 temp = (temp * 15) >> 28;
182 #endif /* !defined(UKERNEL) */
187 #define abs(a) ((a) < 0 ? -1*(a) : (a))
189 ubik_ClientInit(struct rx_connection **serverconns,
190 struct ubik_client **aclient)
195 struct ubik_client *tc;
197 initialize_U_error_table();
199 if (*aclient) { /* the application is doing a re-initialization */
200 LOCK_UBIK_CLIENT((*aclient));
201 /* this is an important defensive check */
202 if (!((*aclient)->initializationState)) {
203 UNLOCK_UBIK_CLIENT((*aclient));
204 return UREINITIALIZE;
207 /* release all existing connections */
208 for (tc = *aclient, i = 0; i < MAXSERVERS; i++) {
209 struct rx_connection *rxConn = ubik_GetRPCConn(tc, i);
212 #ifdef AFS_PTHREAD_ENV
213 rx_ReleaseCachedConnection(rxConn);
215 rx_DestroyConnection(rxConn);
218 UNLOCK_UBIK_CLIENT((*aclient));
219 #ifdef AFS_PTHREAD_ENV
220 if (pthread_mutex_destroy(&((*aclient)->cm)))
221 return UMUTEXDESTROY;
224 tc = malloc(sizeof(struct ubik_client));
228 memset((void *)tc, 0, sizeof(*tc));
229 #ifdef AFS_PTHREAD_ENV
230 if (pthread_mutex_init(&(tc->cm), (const pthread_mutexattr_t *)0)) {
235 tc->initializationState = ++ubik_initializationState;
237 /* first count the # of server conns so we can randomize properly */
239 for (i = 0; i < MAXSERVERS; i++) {
240 if (serverconns[i] == (struct rx_connection *)0)
245 /* here count is the # of servers we're actually passed in. Compute
246 * offset, a number between 0..count-1, where we'll start copying from the
247 * client-provided array. */
248 for (i = 0; i < count; i++) {
249 offset = afs_randomMod15() % count;
250 for (j = abs(offset); j < 2 * count; j++) {
251 if (!tc->conns[abs(j % count)]) {
252 tc->conns[abs(j % count)] = serverconns[i];
263 * \brief Destroy an ubik connection.
265 * It calls rx to destroy the component rx connections, then frees the ubik
266 * connection structure.
269 ubik_ClientDestroy(struct ubik_client * aclient)
275 LOCK_UBIK_CLIENT(aclient);
276 for (c = 0; c < MAXSERVERS; c++) {
277 struct rx_connection *rxConn = ubik_GetRPCConn(aclient, c);
280 #ifdef AFS_PTHREAD_ENV
281 rx_ReleaseCachedConnection(rxConn);
283 rx_DestroyConnection(rxConn);
286 aclient->initializationState = 0; /* client in not initialized */
287 UNLOCK_UBIK_CLIENT(aclient);
288 #ifdef AFS_PTHREAD_ENV
289 pthread_mutex_destroy(&(aclient->cm)); /* ignore failure */
296 * \brief So that intermittent failures that cause connections to die
297 * don't kill whole ubik connection, refresh them when the connection is in
300 struct rx_connection *
301 ubik_RefreshConn(struct rx_connection *tc)
306 struct rx_securityClass *sc;
308 struct rx_connection *newTc;
310 host = rx_HostOf(rx_PeerOf(tc));
311 port = rx_PortOf(rx_PeerOf(tc));
312 service = rx_ServiceIdOf(tc);
313 sc = rx_SecurityObjectOf(tc);
314 si = rx_SecurityClassOf(tc);
317 * destroy old one after creating new one so that refCount on security
318 * object cannot reach zero.
320 newTc = rx_NewConnection(host, port, service, sc, si);
321 rx_DestroyConnection(tc);
325 #ifdef AFS_PTHREAD_ENV
327 pthread_once_t ubik_client_once = PTHREAD_ONCE_INIT;
328 pthread_mutex_t ubik_client_mutex;
329 #define LOCK_UCLNT_CACHE do { \
330 opr_Verify(pthread_once(&ubik_client_once, ubik_client_init_mutex) == 0); \
331 MUTEX_ENTER(&ubik_client_mutex); \
333 #define UNLOCK_UCLNT_CACHE MUTEX_EXIT(&ubik_client_mutex)
336 ubik_client_init_mutex(void)
338 MUTEX_INIT(&ubik_client_mutex, "client init", MUTEX_DEFAULT, 0);
343 #define LOCK_UCLNT_CACHE
344 #define UNLOCK_UCLNT_CACHE
349 static int *calls_needsync[SYNCCOUNT]; /* proc calls that need the sync site */
350 static int synccount = 0;
355 * \brief Call this after getting back a #UNOTSYNC.
357 * \note Getting a #UNOTSYNC error code back does \b not guarantee
358 * that there is a sync site yet elected. However, if there is a sync
359 * site out there somewhere, and you're trying an operation that
360 * requires a sync site, ubik will return #UNOTSYNC, indicating the
361 * operation won't work until you find a sync site
364 try_GetSyncSite(struct ubik_client *aclient, afs_int32 apos)
369 afs_int32 thisHost, newHost;
370 struct rx_connection *tc;
373 origLevel = aclient->initializationState;
376 tc = aclient->conns[apos];
377 if (tc && rx_ConnError(tc)) {
378 aclient->conns[apos] = (tc = ubik_RefreshConn(tc));
384 /* now see if we can find the sync site host */
385 code = VOTE_GetSyncSite(tc, &newHost);
386 if (aclient->initializationState != origLevel) {
387 return -1; /* somebody did a ubik_ClientInit */
390 if (!code && newHost) {
391 newHost = htonl(newHost); /* convert back to network order */
394 * position count at the appropriate slot in the client
395 * structure and retry. If we can't find in slot, we'll just
396 * continue through the whole list
398 for (i = 0; i < MAXSERVERS; i++) {
399 rxp = rx_PeerOf(aclient->conns[i]);
400 thisHost = rx_HostOf(rxp);
403 } else if (thisHost == newHost) {
404 return i; /* we were told to use this one */
415 * \brief Create an internal version of ubik_CallIter that takes an additional
416 * parameter - to indicate whether the ubik client handle has already
420 CallIter(int (*aproc) (), struct ubik_client *aclient,
421 afs_int32 aflags, int *apos, long p1, long p2, long p3, long p4,
422 long p5, long p6, long p7, long p8, long p9, long p10, long p11,
423 long p12, long p13, long p14, long p15, long p16, int needlock)
426 struct rx_connection *tc;
430 LOCK_UBIK_CLIENT(aclient);
432 origLevel = aclient->initializationState;
436 while (*apos < MAXSERVERS) {
437 /* tc is the next conn to try */
438 tc = aclient->conns[*apos];
442 if (rx_ConnError(tc)) {
443 tc = ubik_RefreshConn(tc);
444 aclient->conns[*apos] = tc;
447 if ((aflags & UPUBIKONLY) && (aclient->states[*apos] & CFLastFailed)) {
448 (*apos)++; /* try another one if this server is down */
450 break; /* this is the desired path */
453 if (*apos >= MAXSERVERS)
457 (*aproc) (tc, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13,
459 if (aclient->initializationState != origLevel)
460 /* somebody did a ubik_ClientInit */
463 /* what should I do in case of UNOQUORUM ? */
465 aclient->states[*apos] |= CFLastFailed; /* network errors */
467 /* either misc ubik code, or misc application code or success. */
468 aclient->states[*apos] &= ~CFLastFailed; /* operation worked */
474 UNLOCK_UBIK_CLIENT(aclient);
480 * \brief This is part of an iterator. It doesn't handle finding sync sites.
483 ubik_CallIter(int (*aproc) (), struct ubik_client *aclient,
484 afs_int32 aflags, int *apos, long p1, long p2,
485 long p3, long p4, long p5, long p6, long p7,
486 long p8, long p9, long p10, long p11, long p12,
487 long p13, long p14, long p15, long p16)
489 return CallIter(aproc, aclient, aflags, apos, p1, p2, p3, p4, p5, p6, p7,
490 p8, p9, p10, p11, p12, p13, p14, p15, p16, NEED_LOCK);
494 * \brief Call this instead of stub and we'll guarantee to find a host that's up.
496 * \todo In the future, we should also put in a protocol to find the sync site.
499 ubik_Call_New(int (*aproc) (), struct ubik_client *aclient,
500 afs_int32 aflags, long p1, long p2, long p3, long p4, long p5,
501 long p6, long p7, long p8, long p9, long p10, long p11,
502 long p12, long p13, long p14, long p15, long p16)
504 afs_int32 code, rcode;
511 LOCK_UBIK_CLIENT(aclient);
514 origLevel = aclient->initializationState;
516 /* Do two passes. First pass only checks servers known running */
517 for (aflags |= UPUBIKONLY, pass = 0; pass < 2;
518 pass++, aflags &= ~UPUBIKONLY) {
523 CallIter(aproc, aclient, aflags, &count, p1, p2, p3, p4, p5,
524 p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16,
526 if (code && (aclient->initializationState != origLevel)) {
529 if (code == UNOSERVERS) {
532 rcode = code; /* remember code from last good call */
534 if (code == UNOTSYNC) { /* means this requires a sync site */
535 if (aclient->conns[3]) { /* don't bother unless 4 or more srv */
536 temp = try_GetSyncSite(aclient, count);
537 if (aclient->initializationState != origLevel) {
538 goto restart; /* somebody did a ubik_ClientInit */
540 if ((temp >= 0) && ((temp > count) || (stepBack++ <= 2))) {
541 count = temp; /* generally try to make progress */
544 } else if ((code >= 0) && (code != UNOQUORUM)) {
545 UNLOCK_UBIK_CLIENT(aclient);
546 return code; /* success or global error condition */
550 UNLOCK_UBIK_CLIENT(aclient);
555 * call this instead of stub and we'll guarantee to find a host that's up.
557 * \todo In the future, we should also put in a protocol to find the sync site.
560 ubik_Call(int (*aproc) (), struct ubik_client *aclient,
561 afs_int32 aflags, long p1, long p2, long p3, long p4,
562 long p5, long p6, long p7, long p8, long p9, long p10,
563 long p11, long p12, long p13, long p14, long p15, long p16)
565 afs_int32 rcode, code, newHost, thisHost, i, count;
566 int chaseCount, pass, needsync, inlist, j;
567 struct rx_connection *tc;
571 if (aflags & UBIK_CALL_NEW)
572 return ubik_Call_New(aproc, aclient, aflags, p1, p2, p3, p4,
573 p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15,
578 LOCK_UBIK_CLIENT(aclient);
581 origLevel = aclient->initializationState;
583 chaseCount = inlist = needsync = 0;
586 for (j = 0; ((j < SYNCCOUNT) && calls_needsync[j]); j++) {
587 if (calls_needsync[j] == (int *)aproc) {
588 inlist = needsync = 1;
594 * First pass, we try all servers that are up.
595 * Second pass, we try all servers.
597 for (pass = 0; pass < 2; pass++) { /*p */
598 /* For each entry in our servers list */
599 for (count = 0;; count++) { /*s */
602 /* Need a sync site. Lets try to quickly find it */
603 if (aclient->syncSite) {
604 newHost = aclient->syncSite; /* already in network order */
605 aclient->syncSite = 0; /* Will reset if it works */
606 } else if (aclient->conns[3]) {
607 /* If there are fewer than four db servers in a cell,
608 * there's no point in making the GetSyncSite call.
609 * At best, it's a wash. At worst, it results in more
610 * RPCs than you would otherwise make.
612 tc = aclient->conns[count];
613 if (tc && rx_ConnError(tc)) {
614 aclient->conns[count] = tc = ubik_RefreshConn(tc);
618 code = VOTE_GetSyncSite(tc, &newHost);
619 if (aclient->initializationState != origLevel)
620 goto restart; /* somebody did a ubik_ClientInit */
623 newHost = htonl(newHost); /* convert to network order */
628 /* position count at the appropriate slot in the client
629 * structure and retry. If we can't find in slot, we'll
630 * just continue through the whole list
632 for (i = 0; i < MAXSERVERS && aclient->conns[i]; i++) {
633 rxp = rx_PeerOf(aclient->conns[i]);
634 thisHost = rx_HostOf(rxp);
637 if (thisHost == newHost) {
638 if (chaseCount++ > 2)
639 break; /* avoid loop asking */
640 count = i; /* this index is the sync site */
647 tc = aclient->conns[count];
648 if (tc && rx_ConnError(tc)) {
649 aclient->conns[count] = tc = ubik_RefreshConn(tc);
654 if ((pass == 0) && (aclient->states[count] & CFLastFailed)) {
655 continue; /* this guy's down */
659 (*aproc) (tc, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,
660 p12, p13, p14, p15, p16);
661 if (aclient->initializationState != origLevel) {
662 /* somebody did a ubik_ClientInit */
664 goto restart; /* call failed */
666 goto done; /* call suceeded */
668 if (rcode < 0) { /* network errors */
669 aclient->states[count] |= CFLastFailed; /* Mark serer down */
670 } else if (rcode == UNOTSYNC) {
672 } else if (rcode != UNOQUORUM) {
673 /* either misc ubik code, or misc appl code, or success. */
674 aclient->states[count] &= ~CFLastFailed; /* mark server up */
675 goto done; /* all done */
682 if (!inlist) { /* Remember proc call that needs sync site */
684 calls_needsync[synccount % SYNCCOUNT] = (int *)aproc;
689 if (!rcode) { /* Remember the sync site - cmd successful */
690 rxp = rx_PeerOf(aclient->conns[count]);
691 aclient->syncSite = rx_HostOf(rxp);
694 UNLOCK_UBIK_CLIENT(aclient);