static void _NOLOCK_local_score_update (const struct service_info_s *si0) { gchar *k = service_info_key (si0); STRING_STACKIFY(k); struct service_info_s *si = lru_tree_get (srv_registered, k); if (si) si->score.value = si0->score.value; }
static enum http_rc_e _registration (struct req_args_s *args, enum reg_op_e op, struct json_object *jsrv) { GError *err; if (!push_queue) return _reply_bad_gateway(args, NEWERROR(CODE_INTERNAL_ERROR, "Service upstream disabled")); if (NULL != (err = _cs_check_tokens(args))) return _reply_notfound_error (args, err); struct service_info_s *si = NULL; err = service_info_load_json_object (jsrv, &si); if (err) { if (err->code == CODE_BAD_REQUEST) return _reply_format_error (args, err); else return _reply_system_error (args, err); } if (!validate_namespace (si->ns_name)) { service_info_clean (si); return _reply_system_error (args, NEWERROR (CODE_NAMESPACE_NOTMANAGED, "Unexpected NS")); } si->score.timestamp = network_server_bogonow(args->rq->client->server); if (op == REGOP_PUSH) si->score.value = SCORE_UNSET; else if (op == REGOP_UNLOCK) si->score.value = SCORE_UNLOCK; else /* if (op == REGOP_LOCK) */ si->score.value = CLAMP(si->score.value, SCORE_DOWN, SCORE_MAX); gchar *key = service_info_key(si); PUSH_DO(lru_tree_insert(push_queue, key, si)); GString *gstr = g_string_new (""); service_info_encode_json (gstr, si); return _reply_success_json (args, gstr); }
void service_info_to_lb_item(const struct service_info_s *si, struct oio_lb_item_s *item) { g_assert_nonnull(si); g_assert_nonnull(item); /* Take location from: * - tag.loc as a hexadecimal number or * - tag.log as a hash dot-separated string or * - IP address and port */ const gchar *loc_str = service_info_get_tag_value(si, "tag.loc", NULL); if (!loc_str) { item->location = location_from_addr_info(&(si->addr)); } else if (!g_str_has_prefix(loc_str, "0x") || !(item->location = g_ascii_strtoull(loc_str, NULL, 16))) { item->location = location_from_dotted_string(loc_str); } item->weight = CLAMP(si->score.value, 0, 100); gchar *key = service_info_key(si); g_strlcpy(item->id, key, LIMIT_LENGTH_SRVID); g_free(key); }
static void _reload_srvtype(const char *type) { CSURL(cs); GSList *list = NULL; GError *err = conscience_remote_get_services (cs, type, FALSE, &list); if (err) { GRID_WARN("Services listing error for type[%s]: code=%d %s", type, err->code, err->message); g_clear_error(&err); return; } if (GRID_TRACE_ENABLED()) { GRID_TRACE ("SRV loaded %u [%s]", g_slist_length(list), type); } /* reloads the known services */ gulong now = oio_ext_monotonic_seconds (); SRV_WRITE(for (GSList *l=list; l ;l=l->next) { gchar *k = service_info_key (l->data); lru_tree_insert (srv_known, k, (void*)now); });
static enum http_rc_e _registration (struct req_args_s *args, enum reg_op_e op, struct json_object *jsrv) { GError *err; if (!jsrv || !json_object_is_type (jsrv, json_type_object)) return _reply_common_error (args, BADREQ("Expected: json object")); if (!push_queue) return _reply_bad_gateway(args, NEWERROR(CODE_INTERNAL_ERROR, "Service upstream disabled")); if (NULL != (err = _cs_check_tokens(args))) return _reply_notfound_error (args, err); struct service_info_s *si = NULL; err = service_info_load_json_object (jsrv, &si, TRUE); if (err) { if (err->code == CODE_BAD_REQUEST) return _reply_format_error (args, err); else return _reply_system_error (args, err); } if (!si->type[0]) { service_info_clean (si); return _reply_format_error (args, BADREQ("Service type not specified")); } if (!si->ns_name[0]) { g_strlcpy (si->ns_name, nsname, sizeof(si->ns_name)); } else if (!validate_namespace (si->ns_name)) { service_info_clean (si); return _reply_format_error (args, NEWERROR (CODE_NAMESPACE_NOTMANAGED, "Unexpected NS")); } si->score.timestamp = oio_ext_real_time () / G_TIME_SPAN_SECOND; if (op == REGOP_PUSH) si->score.value = SCORE_UNSET; else if (op == REGOP_UNLOCK) si->score.value = SCORE_UNLOCK; else /* if (op == REGOP_LOCK) */ si->score.value = CLAMP(si->score.value, SCORE_DOWN, SCORE_MAX); // TODO follow the DRY principle and factorize this! if (flag_cache_enabled) { GString *gstr = g_string_new (""); service_info_encode_json (gstr, si, TRUE); PUSH_DO(lru_tree_insert(push_queue, service_info_key(si), si)); return _reply_success_json (args, gstr); } else { CSURL(cs); GSList l = {.data = si, .next = NULL}; if (NULL != (err = conscience_remote_push_services (cs, &l))) { service_info_clean (si); return _reply_common_error (args, err); } else { GString *gstr = g_string_new (""); service_info_encode_json (gstr, si, TRUE); service_info_clean (si); return _reply_success_json (args, gstr); } } }
static enum http_rc_e _registration (struct req_args_s *args, enum reg_op_e op, struct json_object *jsrv) { GError *err; if (!jsrv || !json_object_is_type (jsrv, json_type_object)) return _reply_common_error (args, BADREQ("Expected: json object")); if (!push_queue) return _reply_bad_gateway(args, SYSERR("Service upstream disabled")); if (NULL != (err = _cs_check_tokens(args))) return _reply_notfound_error (args, err); struct service_info_s *si = NULL; err = service_info_load_json_object (jsrv, &si, TRUE); if (err) { g_prefix_error (&err, "JSON error: "); if (err->code == CODE_BAD_REQUEST) return _reply_format_error (args, err); else return _reply_system_error (args, err); } if (!si->type[0]) { service_info_clean (si); return _reply_format_error (args, BADREQ("Service type not specified")); } if (!si->ns_name[0]) { GRID_TRACE2("%s NS forced to %s", __FUNCTION__, si->ns_name); g_strlcpy (si->ns_name, nsname, sizeof(si->ns_name)); } else if (!validate_namespace (si->ns_name)) { service_info_clean (si); return _reply_format_error (args, BADNS()); } gchar *k = service_info_key (si); STRING_STACKIFY(k); GRID_TRACE2("%s op=%s score=%d key=[%s]", __FUNCTION__, _regop_2str(op), si->score.value, k); switch (op) { case REGOP_PUSH: si->score.value = SCORE_UNSET; if (!service_is_known (k)) { service_learn (k); service_tag_set_value_boolean (service_info_ensure_tag ( si->tags, NAME_TAGNAME_RAWX_FIRST), TRUE); } break; case REGOP_LOCK: si->score.value = CLAMP(si->score.value, SCORE_DOWN, SCORE_MAX); break; case REGOP_UNLOCK: si->score.value = SCORE_UNLOCK; break; default: g_assert_not_reached(); } if (cs_expire_local_services > 0) { struct service_info_s *v = service_info_dup (si); v->score.timestamp = oio_ext_monotonic_seconds (); PUSH_DO( const struct service_info_s *si0 = lru_tree_get(srv_registered, k); if (si0) v->score.value = si0->score.value; lru_tree_insert (srv_registered, g_strdup(k), v); ); }