gfarm_error_t gfarm_paraccess_alloc( int concurrency, int try_auth, struct gfarm_paraccess **pap) { int syserr; struct gfarm_paraccess *pa; int i; GFARM_MALLOC(pa); if (pa == NULL) return (GFARM_ERR_NO_MEMORY); syserr = gfarm_eventqueue_alloc(concurrency, &pa->q); if (syserr !=0) { free(pa); return (gfarm_errno_to_error(syserr)); } GFARM_MALLOC_ARRAY(pa->access_state, concurrency); if (pa->access_state == NULL) { gfarm_eventqueue_free(pa->q); free(pa); return (GFARM_ERR_NO_MEMORY); } /* construct free slot list */ i = concurrency - 1; pa->access_state[i].next = NULL; while (--i >= 0) pa->access_state[i].next = &pa->access_state[i + 1]; pa->free_list = &pa->access_state[0]; pa->concurrency = pa->nfree = concurrency; pa->try_auth = try_auth; *pap = pa; return (GFARM_ERR_NO_ERROR); }
void run2(int host1_socket, int host2_socket) { int error; struct gfarm_eventqueue *q; struct p1_finalize_closure fc1, fc2; /* initialize */ if ((q = gfarm_eventqueue_alloc()) == NULL) { fprintf(stderr, "out of memory\n"); return; } fc1.name = "host1"; fc1.state = NULL; error = proto1_request_multiplexed(q, host1_socket, p1_finalize, &fc1, &fc1.state); if (error != 0) fprintf(stderr, "host1: %s\n", strerror(error)); fc2.name = "host2"; fc2.state = NULL; error = proto1_request_multiplexed(q, host2_socket, p1_finalize, &fc2, &fc2.state); if (error != 0) fprintf(stderr, "host2: %s\n", strerror(error)); /* run */ error = gfarm_eventqueue_loop(q, NULL); if (error != 0) fprintf(stderr, "host2: %s\n", strerror(error)); /* terminate */ gfarm_eventqueue_free(q); }
static char * search_idle(int concurrency, int enough_number, struct gfarm_hash_table *hosts_state, int nihosts, struct string_filter *ihost_filter, struct get_next_iterator *ihost_iterator, int *nohostsp, char **ohosts) { char *e, *ihost; int i, rv, desired_number = *nohostsp; struct search_idle_state s; struct gfarm_hash_entry *entry; struct search_idle_host_state *h; struct search_idle_callback_closure *c; struct sockaddr addr; struct gfs_client_get_load_state *gls; if (nihosts == 0) return (GFARM_ERR_NO_HOST); s.q = gfarm_eventqueue_alloc(); if (s.q == NULL) return (GFARM_ERR_NO_MEMORY); s.available_hosts_number = s.idle_hosts_number = s.semi_idle_hosts_number = 0; s.available_hosts = malloc(nihosts * sizeof(*s.available_hosts)); if (s.available_hosts == NULL) { gfarm_eventqueue_free(s.q); return (GFARM_ERR_NO_MEMORY); } s.concurrency = 0; for (i = 0; i < nihosts; i++) { do { ihost = (*ihost_iterator->get_next)(ihost_iterator); } while (!(*ihost_filter->suitable)(ihost_filter, ihost)); entry = gfarm_hash_lookup(hosts_state, ihost, strlen(ihost) + 1); if (entry == NULL) continue; /* never happen, if metadata is consistent */ h = gfarm_hash_entry_data(entry); if ((h->flags & HOST_STATE_FLAG_LOADAVG_TRIED) != 0) { if ((h->flags & (default_search_method == GFARM_SCHEDULE_SEARCH_BY_LOADAVG ? HOST_STATE_FLAG_LOADAVG_AVAIL : HOST_STATE_FLAG_AUTH_SUCCEED)) != 0) search_idle_record_host(&s, h, ihost); } else { e = gfarm_host_info_address_get(ihost, gfarm_spool_server_port, h->host_info, &addr, NULL); if (e != NULL) continue; /* We limit concurrency here */ rv = 0; while (s.concurrency >= concurrency) { rv = gfarm_eventqueue_turn(s.q, NULL); /* XXX - how to report this error? */ if (rv != 0 && rv != EAGAIN && rv != EINTR) break; } if (rv != 0 && rv != EAGAIN && rv != EINTR) break; c = malloc(sizeof(*c)); if (c == NULL) break; c->state = &s; c->peer_addr = addr; c->ah.host_state = h; c->ah.hostname = ihost; /* record return value */ h->flags |= HOST_STATE_FLAG_LOADAVG_TRIED; e = gfs_client_get_load_request_multiplexed(s.q, &c->peer_addr, default_search_method == GFARM_SCHEDULE_SEARCH_BY_LOADAVG ? search_idle_load_callback : search_idle_load_and_connect_callback, c, &gls); if (e != NULL) { free(c); } else { c->protocol_state = gls; s.concurrency++; } } if (s.idle_hosts_number >= desired_number || s.semi_idle_hosts_number >= enough_number) break; } /* XXX - how to report this error? */ rv = gfarm_eventqueue_loop(s.q, NULL); gfarm_eventqueue_free(s.q); if (s.available_hosts_number == 0) { free(s.available_hosts); *nohostsp = 0; return (GFARM_ERR_NO_HOST); } /* sort hosts in the order of load average */ qsort(s.available_hosts, s.available_hosts_number, sizeof(*s.available_hosts), loadavg_compare); for (i = 0; i < s.available_hosts_number && i < desired_number; i++) ohosts[i] = s.available_hosts[i].hostname; /* return value */ *nohostsp = i; free(s.available_hosts); return (NULL); }