bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
28a311381d783cc06e56f9baf1cb9f25634cbfe4Timo Sirainen/* Disconnect from dict server after this many milliseconds of idling after
8f079b04c103e073e57fa8b85cf69b14b0260ea4Timo Sirainen sending a command. Because dict server does blocking dict accesses, it can
8f079b04c103e073e57fa8b85cf69b14b0260ea4Timo Sirainen handle only one client at a time. This is why the default timeout is zero,
8f079b04c103e073e57fa8b85cf69b14b0260ea4Timo Sirainen so that there won't be many dict processes just doing nothing. Zero means
8f079b04c103e073e57fa8b85cf69b14b0260ea4Timo Sirainen that the socket is disconnected immediately after returning to ioloop. */
98da0024e7340e036f0aa9371e9400176df18ebfTimo Sirainen/* Abort dict lookup after this many seconds. */
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen#define DICT_CLIENT_REQUEST_TIMEOUT_MSECS 30000
d640b628f27c9bbcbf6fe902b257002a3f0da57bTimo Sirainen/* When dict lookup timeout is reached, wait a bit longer if the last dict
d640b628f27c9bbcbf6fe902b257002a3f0da57bTimo Sirainen ioloop wait was shorter than this. */
d640b628f27c9bbcbf6fe902b257002a3f0da57bTimo Sirainen#define DICT_CLIENT_REQUEST_TIMEOUT_MIN_LAST_IOLOOP_WAIT_MSECS 1000
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen/* Log a warning if dict lookup takes longer than this many milliseconds. */
2525acc19ab4c8ce64c063a5be8e6b8d198a9fbeTimo Sirainen#define DICT_CLIENT_DEFAULT_WARN_SLOW_MSECS 5000
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen enum dict_protocol_reply reply, const char *value,
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen const char *const *extra_args, const char *error,
56558eb46c396db9c7a0cfd89413b1c50b126b7eTimo Sirainen struct client_dict_transaction_context *trans;
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen struct client_dict_transaction_context *transactions;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen ARRAY(struct client_dict_iter_result) results;
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen struct client_dict_transaction_context *prev, *next;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenstatic struct connection_list *dict_connections;
b5052fbfdbc2678cc8f12899afe55c998f43b740Timo Sirainenstatic int client_dict_connect(struct client_dict *dict, const char **error_r);
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainenstatic int client_dict_reconnect(struct client_dict *dict, const char *reason,
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainen const char **error_r);
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainenstatic void client_dict_disconnect(struct client_dict *dict, const char *reason);
87e7fc1f2804c6f2ae43952def1f30b8cdf00826Timo Sirainenstatic const char *dict_wait_warnings(const struct client_dict_cmd *cmd);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenclient_dict_cmd_init(struct client_dict *dict, const char *query)
e55b5cf82e6d109c658e0b5049cae1ba8edd3b91Timo Sirainen cmd->start_global_ioloop_usecs = ioloop_global_wait_usecs;
6d5a7004a7c0b528b7ba05b2656bea80b3f2743dTimo Sirainen cmd->start_dict_ioloop_usecs = io_wait_timer_get_usecs(dict->wait_timer);
e33462704d38d05c4109929da4923794f699e3ccTimo Sirainen cmd->start_lock_usecs = file_lock_wait_get_total_usecs();
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenstatic void client_dict_cmd_ref(struct client_dict_cmd *cmd)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenstatic bool client_dict_cmd_unref(struct client_dict_cmd *cmd)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenstatic void dict_pre_api_callback(struct client_dict *dict)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* Don't let callback see that we've created our
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen internal ioloop in case it wants to add some ios
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen or timeouts. */
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenstatic void dict_post_api_callback(struct client_dict *dict)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* stop client_dict_wait() */
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainendict_cmd_callback_line(struct client_dict_cmd *cmd, const char *const *args)
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen /* "" is a valid iteration reply */
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen cmd->callback(cmd, reply, value, args, NULL, FALSE);
c5d27aee77dad4b10d6dd915b9cb4c8757c0e988Timo Sirainendict_cmd_callback_error(struct client_dict_cmd *cmd, const char *error,
c1814529bdc9f1468b4fe75beb080991ee3feea0Timo Sirainenclient_dict_cmd_first_nonbg(struct client_dict *dict)
8d72f209049edf50f5cd40c56efd2c0b33f892a2Timo Sirainen for (i = 0; i < count; i++) {
c1814529bdc9f1468b4fe75beb080991ee3feea0Timo Sirainenstatic void client_dict_input_timeout(struct client_dict *dict)
c1814529bdc9f1468b4fe75beb080991ee3feea0Timo Sirainen /* find the first non-background command. there must be at least one. */
c1814529bdc9f1468b4fe75beb080991ee3feea0Timo Sirainen cmd_diff = timeval_diff_msecs(&ioloop_timeval, &cmd->start_time);
c1814529bdc9f1468b4fe75beb080991ee3feea0Timo Sirainen if (cmd_diff < DICT_CLIENT_REQUEST_TIMEOUT_MSECS) {
c1814529bdc9f1468b4fe75beb080991ee3feea0Timo Sirainen /* need to re-create this timeout. the currently-oldest
c1814529bdc9f1468b4fe75beb080991ee3feea0Timo Sirainen command was added when another command was still
c1814529bdc9f1468b4fe75beb080991ee3feea0Timo Sirainen running with an older timeout. */
c1814529bdc9f1468b4fe75beb080991ee3feea0Timo Sirainen timeout_add(DICT_CLIENT_REQUEST_TIMEOUT_MSECS - cmd_diff,
d640b628f27c9bbcbf6fe902b257002a3f0da57bTimo Sirainen /* If we've gotten here because all the time was spent in other ioloops
d640b628f27c9bbcbf6fe902b257002a3f0da57bTimo Sirainen or locks, make sure there's a bit of time waiting for the dict
d640b628f27c9bbcbf6fe902b257002a3f0da57bTimo Sirainen ioloop as well. There's a good chance that the reply can be read. */
d640b628f27c9bbcbf6fe902b257002a3f0da57bTimo Sirainen if (msecs_in_last_dict_ioloop_wait < DICT_CLIENT_REQUEST_TIMEOUT_MIN_LAST_IOLOOP_WAIT_MSECS) {
d640b628f27c9bbcbf6fe902b257002a3f0da57bTimo Sirainen timeout_add(DICT_CLIENT_REQUEST_TIMEOUT_MIN_LAST_IOLOOP_WAIT_MSECS -
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainen (void)client_dict_reconnect(dict, t_strdup_printf(
cf1b9b686bca3e0de3dda1a95dab66ad4590d8efTimo Sirainen "Dict server timeout: %s "
87e7fc1f2804c6f2ae43952def1f30b8cdf00826Timo Sirainen "(%u commands pending, oldest sent %u.%03u secs ago: %s, %s)",
c1814529bdc9f1468b4fe75beb080991ee3feea0Timo Sirainen connection_input_timeout_reason(&dict->conn.conn),
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenclient_dict_cmd_query_send(struct client_dict *dict, const char *query)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen ret = o_stream_sendv(dict->conn.conn.output, iov, 2);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenclient_dict_cmd_send(struct client_dict *dict, struct client_dict_cmd **_cmd,
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen const char **error_r)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* we're no longer idling. even with no_replies=TRUE we're going to
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen ret = client_dict_cmd_query_send(dict, cmd->query);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen error = t_strdup_printf("write(%s) failed: %s", dict->conn.conn.name,
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* Reconnect and try again. */
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainen if (client_dict_reconnect(dict, error, &error) < 0)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen else if (client_dict_cmd_query_send(dict, cmd->query) < 0) {
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen error = t_strdup_printf("write(%s) failed: %s", dict->conn.conn.name,
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* just send and forget */
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen } else if (ret < 0) {
c5d27aee77dad4b10d6dd915b9cb4c8757c0e988Timo Sirainen /* we didn't successfully send this command to dict */
904cdf7f28e5ba801bbd4a9baf3c50d847ac14e1Timo Sirainen dict_cmd_callback_error(cmd, error, cmd->reconnected);
878c4d5eed3307a4018595ded0f79424e9fc1a0dTimo Sirainen if (dict->to_requests == NULL && !cmd->background) {
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen timeout_add(DICT_CLIENT_REQUEST_TIMEOUT_MSECS,
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenclient_dict_transaction_send_begin(struct client_dict_transaction_context *ctx)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen struct client_dict *dict = (struct client_dict *)ctx->ctx.dict;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* transactions commands don't have replies. only COMMIT has. */
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen query = t_strdup_printf("%c%u", DICT_PROTOCOL_CMD_BEGIN, ctx->id);
e13bef0a49ee60e4886967b331a7f7d5c96377ccTimo Sirainen if (!client_dict_cmd_send(dict, &cmd, &error)) {
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenclient_dict_send_transaction_query(struct client_dict_transaction_context *ctx,
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen struct client_dict *dict = (struct client_dict *)ctx->ctx.dict;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen if (!client_dict_cmd_send(dict, &cmd, &error))
28a311381d783cc06e56f9baf1cb9f25634cbfe4Timo Sirainenstatic bool client_dict_is_finished(struct client_dict *dict)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen return dict->transactions == NULL && array_count(&dict->cmds) == 0;
28a311381d783cc06e56f9baf1cb9f25634cbfe4Timo Sirainenstatic void client_dict_timeout(struct client_dict *dict)
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen client_dict_disconnect(dict, "Idle disconnection");
9fa33a0c56e1d9a09c72698097ee269fa04e07b1Timo Sirainenstatic bool client_dict_have_nonbackground_cmds(struct client_dict *dict)
28a311381d783cc06e56f9baf1cb9f25634cbfe4Timo Sirainenstatic void client_dict_add_timeout(struct client_dict *dict)
9fa33a0c56e1d9a09c72698097ee269fa04e07b1Timo Sirainen /* we had non-background commands, but now we're back to
9fa33a0c56e1d9a09c72698097ee269fa04e07b1Timo Sirainen having only background commands. remove timeouts. */
878c4d5eed3307a4018595ded0f79424e9fc1a0dTimo Sirainenstatic void client_dict_cmd_backgrounded(struct client_dict *dict)
9fa33a0c56e1d9a09c72698097ee269fa04e07b1Timo Sirainen if (!client_dict_have_nonbackground_cmds(dict)) {
9fa33a0c56e1d9a09c72698097ee269fa04e07b1Timo Sirainen /* we only have background-commands.
9fa33a0c56e1d9a09c72698097ee269fa04e07b1Timo Sirainen remove the request timeout. */
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainendict_conn_assign_next_async_id(struct dict_connection *conn, const char *line)
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen i_assert(line[0] == DICT_PROTOCOL_REPLY_ASYNC_ID);
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen if (str_to_uint(line+1, &async_id) < 0 || async_id == 0) {
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen i_error("%s: Received invalid async-id line: %s",
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen for (i = 0; i < count; i++) {
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen cmds[i]->async_id_received_time = ioloop_timeval;
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen i_error("%s: Received async-id line, but all %u commands already have it: %s",
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainenstatic int dict_conn_find_async_id(struct dict_connection *conn,
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen i_assert(async_arg[0] == DICT_PROTOCOL_REPLY_ASYNC_REPLY);
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen if (str_to_uint(async_arg+1, &async_id) < 0 || async_id == 0) {
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen i_error("%s: Received invalid async-reply line: %s",
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen for (i = 0; i < count; i++) {
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen i_error("%s: Received reply for nonexistent async-id %u: %s",
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenstatic int dict_conn_input_line(struct connection *_conn, const char *line)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen struct dict_connection *conn = (struct dict_connection *)_conn;
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen return dict_conn_assign_next_async_id(conn, line) < 0 ? -1 : 1;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen i_error("%s: Received reply without pending commands: %s",
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen if (args[0] != NULL && args[0][0] == DICT_PROTOCOL_REPLY_ASYNC_REPLY) {
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen if (dict_conn_find_async_id(conn, args[0], line, &i) < 0)
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen finished = dict_cmd_callback_line(cmds[i], args);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* disconnected during command handling */
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* more lines needed for this command */
b5052fbfdbc2678cc8f12899afe55c998f43b740Timo Sirainenstatic int client_dict_connect(struct client_dict *dict, const char **error_r)
fc0c06ee7b1e5c29a3faabd05f40fa0d26785dffTimo Sirainen if (dict->last_failed_connect == ioloop_time) {
0b15bc85459ef4fea30ac1f8903af48dd6d5f8e2Timo Sirainen /* Try again later */
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen if (connection_client_connect(&dict->conn.conn) < 0) {
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen "net_connect_unix(%s) failed: %m", dict->conn.conn.name);
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen query = t_strdup_printf("%c%u\t%u\t%d\t%s\t%s\n",
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen o_stream_nsend_str(dict->conn.conn.output, query);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenclient_dict_abort_commands(struct client_dict *dict, const char *reason)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* abort all commands */
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen t_array_init(&cmds_copy, array_count(&dict->cmds));
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainenstatic void client_dict_disconnect(struct client_dict *dict, const char *reason)
b529524e924da0d86f8b7f0fa3c9e3e3f763a9f3Timo Sirainen struct client_dict_transaction_context *ctx, *next;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* all transactions that have sent BEGIN are no longer valid */
b529524e924da0d86f8b7f0fa3c9e3e3f763a9f3Timo Sirainen for (ctx = dict->transactions; ctx != NULL; ctx = next) {
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainenstatic int client_dict_reconnect(struct client_dict *dict, const char *reason,
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainen const char **error_r)
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainen t_array_init(&retry_cmds, array_count(&dict->cmds));
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainen for (unsigned int i = 0; i < array_count(&dict->cmds); ) {
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainen /* don't retry iteration that already returned
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainen something to the caller. otherwise we'd return
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainen duplicates. */
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainen reason = t_strdup_printf("%s - reconnect failed: %s",
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainen /* if it fails again, don't retry anymore */
8604a0675d3fa7986b872233fc7eb68068e9b80aTimo Sirainen } else if (!client_dict_cmd_send(dict, &cmd, &error))
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenstatic void dict_conn_destroy(struct connection *_conn)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen struct dict_connection *conn = (struct dict_connection *)_conn;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen client_dict_disconnect(conn->dict, connection_disconnect_reason(_conn));
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenstatic const struct connection_settings dict_conn_set = {
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenstatic const struct connection_vfuncs dict_conn_vfuncs = {
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainenclient_dict_init(struct dict *driver, const char *uri,
09cd60cebc01b2b25ada824f3d9f877d5efd8272Timo Sirainen unsigned int idle_msecs = DICT_CLIENT_DEFAULT_TIMEOUT_MSECS;
2525acc19ab4c8ce64c063a5be8e6b8d198a9fbeTimo Sirainen unsigned int warn_slow_msecs = DICT_CLIENT_DEFAULT_WARN_SLOW_MSECS;
2525acc19ab4c8ce64c063a5be8e6b8d198a9fbeTimo Sirainen /* uri = [idle_msecs=<n>:] [warn_slow_msecs=<n>:] [<path>] ":" <uri> */
2525acc19ab4c8ce64c063a5be8e6b8d198a9fbeTimo Sirainen *error_r = t_strdup_printf("Invalid URI: %s", uri);
2525acc19ab4c8ce64c063a5be8e6b8d198a9fbeTimo Sirainen if (str_to_uint(t_strdup_until(uri+11, p), &idle_msecs) < 0) {
2525acc19ab4c8ce64c063a5be8e6b8d198a9fbeTimo Sirainen } else if (strncmp(uri, "warn_slow_msecs=", 16) == 0) {
2525acc19ab4c8ce64c063a5be8e6b8d198a9fbeTimo Sirainen *error_r = t_strdup_printf("Invalid URI: %s", uri);
2525acc19ab4c8ce64c063a5be8e6b8d198a9fbeTimo Sirainen if (str_to_uint(t_strdup_until(uri+16, p), &warn_slow_msecs) < 0) {
eca38954bcf972618f6b85932a3690acbd2b673aTimo Sirainen *error_r = t_strdup_printf("Invalid URI: %s", uri);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen dict_connections = connection_list_init(&dict_conn_set,
02a0277d897dbf5172937158f3d828f981ff230dTimo Sirainen /* default path */
02a0277d897dbf5172937158f3d828f981ff230dTimo Sirainen /* absolute path */
02a0277d897dbf5172937158f3d828f981ff230dTimo Sirainen /* relative path to base_dir */
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen connection_init_client_unix(dict_connections, &dict->conn.conn, path);
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainenstatic void client_dict_deinit(struct dict *_dict)
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainen struct client_dict *dict = (struct client_dict *)_dict;
c4478af52de63804efef2055580adf1dfc8679c6Timo Sirainenstatic void client_dict_wait(struct dict *_dict)
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen struct client_dict *dict = (struct client_dict *)_dict;
a5ec9755556e3d97d7e6d78cb1b53046370e6598Timo Sirainenstatic bool client_dict_switch_ioloop(struct dict *_dict)
a5ec9755556e3d97d7e6d78cb1b53046370e6598Timo Sirainen struct client_dict *dict = (struct client_dict *)_dict;
6d5a7004a7c0b528b7ba05b2656bea80b3f2743dTimo Sirainen dict->wait_timer = io_wait_timer_move(&dict->wait_timer);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen dict->to_idle = io_loop_move_timeout(&dict->to_idle);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen dict->to_requests = io_loop_move_timeout(&dict->to_requests);
87e7fc1f2804c6f2ae43952def1f30b8cdf00826Timo Sirainenstatic const char *dict_wait_warnings(const struct client_dict_cmd *cmd)
e55b5cf82e6d109c658e0b5049cae1ba8edd3b91Timo Sirainen int global_ioloop_msecs = (ioloop_global_wait_usecs -
6d5a7004a7c0b528b7ba05b2656bea80b3f2743dTimo Sirainen int dict_ioloop_msecs = (io_wait_timer_get_usecs(cmd->dict->wait_timer) -
e55b5cf82e6d109c658e0b5049cae1ba8edd3b91Timo Sirainen int other_ioloop_msecs = global_ioloop_msecs - dict_ioloop_msecs;
e33462704d38d05c4109929da4923794f699e3ccTimo Sirainen int lock_msecs = (file_lock_wait_get_total_usecs() -
87e7fc1f2804c6f2ae43952def1f30b8cdf00826Timo Sirainen "%d.%03d in dict wait, %d.%03d in other ioloops, %d.%03d in locks",
e33462704d38d05c4109929da4923794f699e3ccTimo Sirainen dict_ioloop_msecs/1000, dict_ioloop_msecs%1000,
e55b5cf82e6d109c658e0b5049cae1ba8edd3b91Timo Sirainen other_ioloop_msecs/1000, other_ioloop_msecs%1000,
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainenstatic const char *
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainendict_warnings_sec(const struct client_dict_cmd *cmd, int msecs,
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen const char *const *extra_args)
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen str_printfa(str, "%d.%03d secs (%s", msecs/1000, msecs%1000,
d485968ffb1b2456452d79eeda33abeb1bdd5733Timo Sirainen str_printfa(str, ", reconnected %u.%03u secs ago",
d485968ffb1b2456452d79eeda33abeb1bdd5733Timo Sirainen reconnected_msecs/1000, reconnected_msecs%1000);
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen timeval_diff_msecs(&ioloop_timeval, &cmd->async_id_received_time);
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen str_printfa(str, ", async-id reply %u.%03u secs ago",
44c54229a117b8dab05eaff9c54ba61fbae9a39fTimo Sirainen async_reply_msecs/1000, async_reply_msecs%1000);
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen str_to_time(extra_args[0], &tv_start.tv_sec) == 0 &&
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen str_to_uint(extra_args[1], &tv_start_usec) == 0 &&
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen str_to_time(extra_args[2], &tv_end.tv_sec) == 0 &&
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen str_to_uint(extra_args[3], &tv_end_usec) == 0) {
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen timeval_diff_msecs(&ioloop_timeval, &tv_start);
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen int server_msecs = timeval_diff_msecs(&tv_end, &tv_start);
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen str_printfa(str, ", started on dict-server %u.%03d secs ago, "
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen "took %u.%03d secs",
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainenclient_dict_lookup_async_callback(struct client_dict_cmd *cmd,
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen const char *const *extra_args,
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen } else switch (reply) {
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen result.error = value[0] == '\0' ? "dict-server returned failure" :
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen t_strdup_printf("dict-server returned failure: %s",
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen "dict-client: Invalid lookup '%s' reply: %c%s",
5d234d36b44965f623246520ada9bea02bbf746fTimo Sirainen int diff = timeval_diff_msecs(&ioloop_timeval, &cmd->start_time);
5d234d36b44965f623246520ada9bea02bbf746fTimo Sirainen /* include timing info always in error messages */
9578698c44322dad310a9756f3b4dee8e7de646aTimo Sirainen result.error = t_strdup_printf("%s (reply took %s)",
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen result.error, dict_warnings_sec(cmd, diff, extra_args));
9578698c44322dad310a9756f3b4dee8e7de646aTimo Sirainen i_warning("read(%s): dict lookup took %s: %s",
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen dict->conn.conn.name, dict_warnings_sec(cmd, diff, extra_args),
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen cmd->api_callback.lookup(&result, cmd->api_callback.context);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenclient_dict_lookup_async(struct dict *_dict, const char *key,
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen dict_lookup_callback_t *callback, void *context)
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainen struct client_dict *dict = (struct client_dict *)_dict;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen query = t_strdup_printf("%c%s", DICT_PROTOCOL_CMD_LOOKUP,
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen cmd->callback = client_dict_lookup_async_callback;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenstatic void client_dict_lookup_callback(const struct dict_lookup_result *result,
48aee87c1ce3b3cd10d53f61b4665da81ef6623bTimo Sirainen struct client_dict_sync_lookup *lookup = context;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenstatic int client_dict_lookup(struct dict *_dict, pool_t pool, const char *key,
48aee87c1ce3b3cd10d53f61b4665da81ef6623bTimo Sirainen client_dict_lookup_async(_dict, key, client_dict_lookup_callback, &lookup);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenstatic void client_dict_iterate_free(struct client_dict_iterate_context *ctx)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenclient_dict_iter_api_callback(struct client_dict_iterate_context *ctx,
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen const char *const *extra_args)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* iterator was already deinitialized */
5d234d36b44965f623246520ada9bea02bbf746fTimo Sirainen int diff = timeval_diff_msecs(&ioloop_timeval, &cmd->start_time);
5d234d36b44965f623246520ada9bea02bbf746fTimo Sirainen /* include timing info always in error messages */
9578698c44322dad310a9756f3b4dee8e7de646aTimo Sirainen char *new_error = i_strdup_printf("%s (reply took %s)",
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen ctx->error, dict_warnings_sec(cmd, diff, extra_args));
9578698c44322dad310a9756f3b4dee8e7de646aTimo Sirainen i_warning("read(%s): dict iteration took %s: %s",
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen dict->conn.conn.name, dict_warnings_sec(cmd, diff, extra_args),
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen ctx->ctx.async_callback(ctx->ctx.async_context);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* synchronous lookup */
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainenclient_dict_iter_async_callback(struct client_dict_cmd *cmd,
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen const char *const *extra_args,
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen struct client_dict_iterate_context *ctx = cmd->iter;
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen const char *iter_key = NULL, *iter_value = NULL;
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen } else switch (reply) {
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* end of iteration */
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen client_dict_iter_api_callback(ctx, cmd, extra_args);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* key \t value */
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen error = t_strdup_printf("dict-server returned failure: %s", value);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* broken protocol */
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen error = t_strdup_printf("dict client (%s) sent broken iterate reply: %c%s",
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen client_dict_iter_api_callback(ctx, cmd, extra_args);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* iterator was already deinitialized */
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen result->key = p_strdup(ctx->results_pool, iter_key);
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen result->value = p_strdup(ctx->results_pool, iter_value);
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen client_dict_iter_api_callback(ctx, cmd, NULL);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainenclient_dict_iterate_init(struct dict *_dict, const char *const *paths,
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainen ctx = i_new(struct client_dict_iterate_context, 1);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen ctx->results_pool = pool_alloconly_create("client dict iteration", 512);
8c0e4118f21c30455d4911fabc4bf50bfaeca712Timo Sirainen ctx->paths = p_strarray_dup(system_pool, paths);
8c0e4118f21c30455d4911fabc4bf50bfaeca712Timo Sirainenclient_dict_iterate_cmd_send(struct client_dict_iterate_context *ctx)
8c0e4118f21c30455d4911fabc4bf50bfaeca712Timo Sirainen struct client_dict *dict = (struct client_dict *)ctx->ctx.dict;
8c0e4118f21c30455d4911fabc4bf50bfaeca712Timo Sirainen unsigned int i;
8c0e4118f21c30455d4911fabc4bf50bfaeca712Timo Sirainen /* we can't do this query in _iterate_init(), because
8c0e4118f21c30455d4911fabc4bf50bfaeca712Timo Sirainen _set_limit() hasn't been called yet at that point. */
47a5a7e8296f3b8f2fac9a0659d4de3f2723ba4aMartti Rannanjärvi str_printfa(query, "%c%d\t%"PRIu64, DICT_PROTOCOL_CMD_ITERATE,
8c0e4118f21c30455d4911fabc4bf50bfaeca712Timo Sirainen str_append(query, str_tabescape(ctx->paths[i]));
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen cmd = client_dict_cmd_init(dict, str_c(query));
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen cmd->callback = client_dict_iter_async_callback;
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainenstatic bool client_dict_iterate(struct dict_iterate_context *_ctx,
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen const struct client_dict_iter_result *results;
8c0e4118f21c30455d4911fabc4bf50bfaeca712Timo Sirainen return client_dict_iterate(_ctx, key_r, value_r);
980b0dbdcbeed1a15fdbf4ec1d00352d71a66c5dTimo Sirainen if ((ctx->flags & DICT_ITERATE_FLAG_ASYNC) == 0 && ctx->ctx.has_more) {
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen return client_dict_iterate(_ctx, key_r, value_r);
055389c58fa3915e12fb4e72ec86782ce77c5c72Timo Sirainenstatic int client_dict_iterate_deinit(struct dict_iterate_context *_ctx,
055389c58fa3915e12fb4e72ec86782ce77c5c72Timo Sirainen const char **error_r)
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainen struct client_dict *dict = (struct client_dict *)_ctx->dict;
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainenclient_dict_transaction_init(struct dict *_dict)
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainen struct client_dict *dict = (struct client_dict *)_dict;
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainen ctx = i_new(struct client_dict_transaction_context, 1);
56558eb46c396db9c7a0cfd89413b1c50b126b7eTimo Sirainenclient_dict_transaction_free(struct client_dict_transaction_context **_ctx)
56558eb46c396db9c7a0cfd89413b1c50b126b7eTimo Sirainen struct client_dict_transaction_context *ctx = *_ctx;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainenclient_dict_transaction_commit_callback(struct client_dict_cmd *cmd,
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen const char *const *extra_args,
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen } else switch (reply) {
c5d27aee77dad4b10d6dd915b9cb4c8757c0e988Timo Sirainen /* fallthrough */
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen /* value contains the obsolete trans_id */
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen result.error = t_strdup_printf("dict-server returned failure: %s",
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen error != NULL ? t_str_tabunescape(error) : "");
fc494f157828dee9bb9885dd3630e9b7b45cdb25Timo Sirainen "dict-client: Invalid commit reply: %c%s",
5d234d36b44965f623246520ada9bea02bbf746fTimo Sirainen int diff = timeval_diff_msecs(&ioloop_timeval, &cmd->start_time);
5d234d36b44965f623246520ada9bea02bbf746fTimo Sirainen /* include timing info always in error messages */
9578698c44322dad310a9756f3b4dee8e7de646aTimo Sirainen result.error = t_strdup_printf("%s (reply took %s)",
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen result.error, dict_warnings_sec(cmd, diff, extra_args));
aef407f147034a569591c0f59593342a8c7b39eaTimo Sirainen } else if (!cmd->background && !cmd->trans->ctx.no_slowness_warning &&
56558eb46c396db9c7a0cfd89413b1c50b126b7eTimo Sirainen "%s (%u commands, first: %s)",
8e1491e4924e3f9fc474a99438b6297b8d1ce1f3Timo Sirainen dict->conn.conn.name, dict_warnings_sec(cmd, diff, extra_args),
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen cmd->api_callback.commit(&result, cmd->api_callback.context);
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainenclient_dict_transaction_commit(struct dict_transaction_context *_ctx,
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainen (struct client_dict_transaction_context *)_ctx;
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainen struct client_dict *dict = (struct client_dict *)_ctx->dict;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen query = t_strdup_printf("%c%u", DICT_PROTOCOL_CMD_COMMIT, ctx->id);
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen cmd->callback = client_dict_transaction_commit_callback;
701eb90460d6c57845dc4e0bf595a5d0b90b01c1Timo Sirainen if (callback == dict_transaction_commit_async_noop_callback)
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* already failed */
9a382894724292e2af60ef94fc471d761f45e5d5Timo Sirainen .ret = DICT_COMMIT_RET_FAILED, .error = ctx->error
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen /* nothing changed */
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainenclient_dict_transaction_rollback(struct dict_transaction_context *_ctx)
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainen (struct client_dict_transaction_context *)_ctx;
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen struct client_dict *dict = (struct client_dict *)_ctx->dict;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen query = t_strdup_printf("%c%u", DICT_PROTOCOL_CMD_ROLLBACK,
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen client_dict_send_transaction_query(ctx, query);
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainenstatic void client_dict_set(struct dict_transaction_context *_ctx,
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainen (struct client_dict_transaction_context *)_ctx;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen client_dict_send_transaction_query(ctx, query);
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainenstatic void client_dict_unset(struct dict_transaction_context *_ctx,
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen const char *key)
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen (struct client_dict_transaction_context *)_ctx;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen client_dict_send_transaction_query(ctx, query);
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainenstatic void client_dict_atomic_inc(struct dict_transaction_context *_ctx,
ff52f5c52ee6a4c8a9c79964d32b5d0ff0ae92e6Timo Sirainen (struct client_dict_transaction_context *)_ctx;
a7e46c05358b059aad2b90f01e271ba6732c5eeeTimo Sirainen client_dict_send_transaction_query(ctx, query);
345fceae2f430dcad449f2a09598ba1a225116ddTimo Sirainenstatic void client_dict_set_timestamp(struct dict_transaction_context *_ctx,
345fceae2f430dcad449f2a09598ba1a225116ddTimo Sirainen (struct client_dict_transaction_context *)_ctx;
345fceae2f430dcad449f2a09598ba1a225116ddTimo Sirainen client_dict_send_transaction_query(ctx, query);
ade5567577dadb0b275c840208d3ad21a9f00a36Timo Sirainen .transaction_init = client_dict_transaction_init,
ade5567577dadb0b275c840208d3ad21a9f00a36Timo Sirainen .transaction_commit = client_dict_transaction_commit,