|
| 1 | +// Copyright (c) 2025 ByteDance Ltd. and/or its affiliates |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "priskv-config.h" |
| 16 | +#include "priskv-log.h" |
| 17 | +#include <pthread.h> |
| 18 | + |
| 19 | +static const char *priskv_transport_backend_names[] = {[PRISKV_TRANSPORT_BACKEND_RDMA] = "RDMA", |
| 20 | + [PRISKV_TRANSPORT_BACKEND_UCX] = "UCX", |
| 21 | + [PRISKV_TRANSPORT_BACKEND_MAX] = NULL}; |
| 22 | + |
| 23 | +static ucs_config_field_t priskv_client_config_table[] = { |
| 24 | + {"NQUEUE", "0", "Number of worker threads for client.", |
| 25 | + ucs_offsetof(priskv_client_config_t, nqueue), |
| 26 | + UCS_CONFIG_TYPE_INT}, |
| 27 | + {"METADATA_KEY", "priskv_cluster_metadata", "Key to store cluster metadata.", |
| 28 | + ucs_offsetof(priskv_client_config_t, metadata_key), |
| 29 | + UCS_CONFIG_TYPE_STRING}, |
| 30 | + {"METADATA_UPDATE_INTERVAL_SEC", "5", "Interval to update metadata in seconds.", |
| 31 | + ucs_offsetof(priskv_client_config_t, metadata_update_interval_sec), |
| 32 | + UCS_CONFIG_TYPE_INT}, |
| 33 | + {"META_SERVER_CONNECT_TIMEOUT_SEC", "5", "Timeout to connect to meta server in seconds.", |
| 34 | + ucs_offsetof(priskv_client_config_t, meta_server_connect_timeout_sec), |
| 35 | + UCS_CONFIG_TYPE_INT}, |
| 36 | + |
| 37 | + {NULL} |
| 38 | +}; |
| 39 | + |
| 40 | +static ucs_config_field_t priskv_server_config_table[] = { |
| 41 | + {NULL} |
| 42 | +}; |
| 43 | + |
| 44 | +static ucs_config_field_t priskv_config_table[] = { |
| 45 | + {"TRANSPORT", "RDMA", "PrisKV Transport. Supported transports are [RDMA, UCX].", |
| 46 | + ucs_offsetof(priskv_config_t, transport), |
| 47 | + UCS_CONFIG_TYPE_ENUM(priskv_transport_backend_names)}, |
| 48 | + |
| 49 | + {"", "", NULL, ucs_offsetof(priskv_config_t, client), |
| 50 | + UCS_CONFIG_TYPE_TABLE(priskv_client_config_table)}, |
| 51 | + |
| 52 | + {"", "", NULL, ucs_offsetof(priskv_config_t, server), |
| 53 | + UCS_CONFIG_TYPE_TABLE(priskv_server_config_table)}, |
| 54 | + |
| 55 | + {NULL} |
| 56 | +}; |
| 57 | + |
| 58 | +PRISKV_CONFIG_DECLARE_TABLE(priskv_config_table, "PrisKV Config", NULL, priskv_config_t); |
| 59 | + |
| 60 | +static pthread_once_t g_priskv_config_once = PTHREAD_ONCE_INIT; |
| 61 | +priskv_config_t g_config = {0}; |
| 62 | + |
| 63 | +static void priskv_config_init_impl(void) |
| 64 | +{ |
| 65 | + ucs_status_t status = priskv_config_parser_fill_opts( |
| 66 | + &g_config, PRISKV_CONFIG_GET_TABLE(priskv_config_table), PRISKV_ENV_PREFIX, 1); |
| 67 | + if (status != UCS_OK) { |
| 68 | + priskv_log_error("Failed to initialize config: %s\n", ucs_status_string(status)); |
| 69 | + } else { |
| 70 | + ucs_config_parser_print_opts( |
| 71 | + stdout, "PrisKV Environment Variables", &g_config, priskv_config_table, NULL, |
| 72 | + PRISKV_ENV_PREFIX, |
| 73 | + UCS_CONFIG_PRINT_CONFIG | UCS_CONFIG_PRINT_HEADER | UCS_CONFIG_PRINT_DOC, NULL); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +void priskv_config_init(void) |
| 78 | +{ |
| 79 | + pthread_once(&g_priskv_config_once, priskv_config_init_impl); |
| 80 | +} |
| 81 | + |
| 82 | +ucs_status_t priskv_config_parser_fill_opts(void *opts, ucs_config_global_list_entry_t *entry, |
| 83 | + const char *env_prefix, int ignore_errors) |
| 84 | +{ |
| 85 | + return ucs_config_parser_fill_opts(opts, entry, env_prefix, ignore_errors); |
| 86 | +} |
| 87 | + |
| 88 | +void priskv_config_parser_release_opts(void *opts, ucs_config_field_t *fields) |
| 89 | +{ |
| 90 | + ucs_config_parser_release_opts(opts, fields); |
| 91 | +} |
| 92 | + |
| 93 | +ucs_status_t priskv_config_parser_set_value(void *opts, ucs_config_field_t *fields, |
| 94 | + const char *prefix, const char *name, const char *value) |
| 95 | +{ |
| 96 | + |
| 97 | + return ucs_config_parser_set_value(opts, fields, prefix, name, value); |
| 98 | +} |
0 commit comments