From 28f6b9ff25a69363f06aca960ac79ef3cba24c18 Mon Sep 17 00:00:00 2001 From: Darafei Praliaskouski Date: Sun, 22 Nov 2026 15:30:37 +0400 Subject: [PATCH 1/2] feat(renderd): configurable request queue limits and priority-ordered pending queue Port of the PR commits (queue limit config, priority queue, hash avalanche, tests) onto the current upstream structure and style. --- docs/man/renderd.conf.5 | 17 + docs/renderd-queue-tuning.md | 76 +++++ etc/renderd/renderd.conf | 2 + etc/renderd/renderd.conf.examples | 4 + etc/renderd/renderd.conf.in | 2 + includes/renderd.h | 4 + includes/request_queue.h | 16 +- src/renderd.c | 2 +- src/renderd_config.c | 16 + src/request_queue.c | 498 +++++++++++++++++++++++------ tests/gen_tile_test.cpp | 313 +++++++++++++++++- tests/unit_test_renderd_config.cpp | 54 ++++ tests/unit_test_request_queue.cpp | 14 +- 13 files changed, 909 insertions(+), 109 deletions(-) create mode 100644 docs/renderd-queue-tuning.md diff --git a/docs/man/renderd.conf.5 b/docs/man/renderd.conf.5 index 3c7a58347..a551f47f3 100644 --- a/docs/man/renderd.conf.5 +++ b/docs/man/renderd.conf.5 @@ -40,6 +40,19 @@ This option and \fBsocketname\fR are mutually exclusive. Default value is \fB'0' Specify the number of threads to be used for \fBrenderd\fR. A value of \fB'-1'\fR will configure \fBnum_threads\fR to the number of cores on the system. The default value is \fB'4'\fR (macro definition \fB'NUM_THREADS'\fR). +.TP +.B request_queue_limit +Specify the maximum number of requests kept in each time-critical request queue: priority, normal, low priority, and bulk. +When one of these queues is full, new requests overflow into the dirty queue if space is available. +The default value is \fB'256'\fR (macro definition \fB'REQ_LIMIT'\fR). + +.TP +.B dirty_queue_limit +Specify the maximum number of requests kept in the dirty queue. +The dirty queue is the background queue for expired or overflowed tiles where no client is waiting for the response. +Larger values can preserve more expired work for quiet periods, but they can also increase memory use and make it easier for important overflowed tiles to sit behind lower-priority background work. +The default value is \fB'8000'\fR (macro definition \fB'DIRTY_LIMIT'\fR). + .TP .B pid_file Specify the file path into which the PID will be written by \fBrenderd\fR. @@ -171,6 +184,10 @@ Specify the file path of the Mapnik XML stylesheet file for this section. Only used by \fBrenderd\fR. .SH SEE ALSO .BR renderd(1) +.PP +See \fBdocs/renderd-queue-tuning.md\fR in the source tree for queue tuning and rollout guidance. +.BR + .SH AUTHOR renderd was written by Jon Burgess, and other OpenStreetMap project members. .PP diff --git a/docs/renderd-queue-tuning.md b/docs/renderd-queue-tuning.md new file mode 100644 index 000000000..80e19e017 --- /dev/null +++ b/docs/renderd-queue-tuning.md @@ -0,0 +1,76 @@ +# renderd Queue Tuning + +`renderd` keeps incoming render requests in a priority queue with five priority +classes: + +- priority request queue: missing tiles where a client is waiting +- request queue: stale tiles where a client is waiting +- low priority request queue: less urgent stale/style refresh work +- dirty queue: background work where no client waits for the response +- bulk request queue: explicit bulk rendering work + +Requests are fetched by effective priority, preserving FIFO order within the +same priority class. When a time-critical request class is full, new requests +overflow into the dirty class. That prevents immediate drops, but it also means +the overflowed request no longer gets client-waiting priority until a later +client request for the same tile can promote it back into the appropriate +priority class. + +## Configuration + +Set the queue limits in the active `[renderd]` section of `renderd.conf`: + +```ini +[renderd] +num_threads=4 +request_queue_limit=256 +dirty_queue_limit=8000 +``` + +`request_queue_limit` applies separately to each of the priority, normal, low +priority, and bulk queues. `dirty_queue_limit` applies to the background dirty +queue. If the dirty queue is also full, new overflow work is dropped. + +The defaults keep the previous compiled-in behavior: + +- with metatiles: `request_queue_limit=256`, `dirty_queue_limit=8000` +- without metatiles: `request_queue_limit=512`, `dirty_queue_limit=10000` + +## Operating Guidance + +Increase `dirty_queue_limit` when dirty tiles are dropped during peak load but +the server has quiet periods later where it can catch up. This is useful for +large public tile services where preserving expired work can improve freshness +after the peak passes. + +Do not increase `dirty_queue_limit` blindly on an overloaded server. If the +higher-priority queues are continuously non-empty, dirty work may still starve, +and a larger dirty queue mainly stores more backlog. Watch queue length, queue +time, render throughput, and dropped-tile metrics before and after the change. +When a client requests a tile that is already queued as lower-priority work, +`renderd` raises that existing item in the priority queue if the target priority +class has capacity. If the target class is still full, the request remains +background work and the client receives the usual not-done response. + +Increase gradually. For example, move from the default to a limit sized for +roughly 10 minutes of dirty work, then one hour, before trying day-scale values. +The right value depends on metatile size, render throughput, expiry volume, and +whether the service has predictable low-load windows. + +## Rollout Checklist + +1. Record current `renderd_queue`, `renderd_queue_time`, and + `renderd_processed` Munin graphs. +2. Set `dirty_queue_limit` in `renderd.conf`. +3. Restart `renderd` during a maintenance window. +4. Confirm startup logs show the intended `request_queue_limit` and + `dirty_queue_limit`. +5. Watch whether dropped dirty work decreases without sustained growth in queue + time. +6. Roll back to the previous values if dirty queue time grows continuously or + missing-tile requests appear to be delayed by old background work. + +This setting changes per-priority capacity and works with duplicate promotion +for already queued tiles. It does not implement bounded overtaking or broader +fairness between unrelated queued tiles; those would require a larger queue +scheduler change. diff --git a/etc/renderd/renderd.conf b/etc/renderd/renderd.conf index 412f0f407..f8d19db21 100644 --- a/etc/renderd/renderd.conf +++ b/etc/renderd/renderd.conf @@ -6,6 +6,8 @@ stats_file=/run/renderd/renderd.stats socketname=/run/renderd/renderd.sock num_threads=4 tile_dir=/var/cache/renderd/tiles +;request_queue_limit=256 +;dirty_queue_limit=8000 [mapnik] plugins_dir=/usr/lib/mapnik/3.1/input diff --git a/etc/renderd/renderd.conf.examples b/etc/renderd/renderd.conf.examples index 7d82e2402..adbd72a6a 100644 --- a/etc/renderd/renderd.conf.examples +++ b/etc/renderd/renderd.conf.examples @@ -6,12 +6,16 @@ stats_file=/run/renderd/renderd.stats socketname=/run/renderd/renderd.sock num_threads=4 tile_dir=/var/cache/renderd/tiles +;request_queue_limit=256 +;dirty_queue_limit=8000 ;[renderd] ;iphostname=::1 ;ipport=7654 ;num_threads=4 ;tile_dir=rados://tiles/etc/ceph/ceph.conf +;request_queue_limit=256 +;dirty_queue_limit=8000 ;pid_file=/run/renderd/renderd_rados.pid ;stats_file=/run/renderd/renderd.stats diff --git a/etc/renderd/renderd.conf.in b/etc/renderd/renderd.conf.in index 36c82b5aa..8d96f9c58 100644 --- a/etc/renderd/renderd.conf.in +++ b/etc/renderd/renderd.conf.in @@ -3,6 +3,8 @@ pid_file=@RENDERD_PIDFILE@ socketname=@RENDERD_SOCKET@ stats_file=@RENDERD_RUN_DIR@/renderd.stats tile_dir=@RENDERD_TILE_DIR@ +; request_queue_limit=256 +; dirty_queue_limit=8000 [mapnik] font_dir=@MAPNIK_FONTS_DIR@ diff --git a/includes/renderd.h b/includes/renderd.h index 21425a914..c6073814e 100644 --- a/includes/renderd.h +++ b/includes/renderd.h @@ -56,12 +56,16 @@ extern "C" { const char *stats_filename; /** @brief Default root tile/metatile storage directory or backend connection string. */ const char *tile_dir; + /** @brief Maximum number of requests kept in the dirty queue. */ + int dirty_queue_limit; /** @brief TCP listen port number when `iphostname` is configured. */ int ipport; /** @brief Non-zero to recursively scan subdirectories under `mapnik_font_dir`. */ int mapnik_font_dir_recurse; /** @brief Number of worker rendering threads spawned for this daemon instance. */ int num_threads; + /** @brief Maximum number of requests kept in each time-critical request queue. */ + int request_queue_limit; } renderd_config; /** diff --git a/includes/request_queue.h b/includes/request_queue.h index a08306a34..5b0b74378 100644 --- a/includes/request_queue.h +++ b/includes/request_queue.h @@ -28,6 +28,7 @@ extern "C" { /** @brief Prime bucket count for the hash table indexing queued and in-flight metatile render requests. */ #define HASHIDX_SIZE 2213 +#define PENDING_QUEUE_RANKS 5 /** * @brief Cumulative render request counts and elapsed render durations tracked by `request_queue`. @@ -75,11 +76,17 @@ extern "C" { * @brief Thread-safe multi-priority metatile render queue with hash-based duplicate coalescing. */ struct request_queue { - /** @brief Number of buckets allocated in `item_hashidx` (typically `HASHIDX_SIZE`). */ + /** @brief Maximum number of requests kept in the dirty queue. */ + int dirtyLimit; + /** @brief Number of buckets allocated in `item_hashidx`, sized from the queue limits (at least `HASHIDX_SIZE`). */ int hashidxSize; - /** @brief Sentinel list heads for the standard, high-prio, low-prio, bulk, dirty, and active-render queues. */ - struct item reqHead, reqPrioHead, reqLowHead, reqBulkHead, dirtyHead, renderHead; - /** @brief Array of `hashidxSize` bucket heads indexing all queued and in-flight requests by `(xmlname, mx, my, z)`. */ + /** @brief Maximum number of requests kept in each of the standard, high-prio, low-prio, and bulk queues. */ + int requestLimit; + /** @brief Sentinel head of the single pending list ordered by queue rank, and of the active-render list. */ + struct item pendingHead, renderHead; + /** @brief Last pending item of each queue rank (`PENDING_QUEUE_RANKS`), or `&pendingHead` when the rank is empty. */ + struct item *pendingTail[PENDING_QUEUE_RANKS]; + /** @brief Array of `hashidxSize` bucket heads indexing all queued and in-flight requests by `(xmlname, mimetype, options, mx, my, z)`. */ struct item_idx *item_hashidx; /** @brief Current item counts in the standard, high-prio, low-prio, bulk, and dirty queues. */ int reqNum, reqPrioNum, reqLowNum, reqBulkNum, dirtyNum; @@ -92,6 +99,7 @@ extern "C" { }; struct request_queue *request_queue_init(); + struct request_queue *request_queue_init_with_limits(int request_limit, int dirty_limit); void request_queue_close(struct request_queue *queue); struct item *request_queue_fetch_request(struct request_queue *queue); diff --git a/src/renderd.c b/src/renderd.c index ea04a7a18..f7ca12c4c 100644 --- a/src/renderd.c +++ b/src/renderd.c @@ -980,7 +980,7 @@ int main(int argc, char **argv) } g_logger(G_LOG_LEVEL_INFO, "Initialising request queue"); - render_request_queue = request_queue_init(); + render_request_queue = request_queue_init_with_limits(config.request_queue_limit, config.dirty_queue_limit); if (render_request_queue == NULL) { g_logger(G_LOG_LEVEL_CRITICAL, "Failed to initialise request queue"); diff --git a/src/renderd_config.c b/src/renderd_config.c index 9644a3202..9c16824e0 100644 --- a/src/renderd_config.c +++ b/src/renderd_config.c @@ -696,7 +696,9 @@ void process_renderd_sections(const dictionary *ini, const char *config_file_nam copy_string(section, &configs_dest[renderd_section_num].name, renderd_strlen + 2); process_config_int(ini, section, "ipport", &configs_dest[renderd_section_num].ipport, 0); + process_config_int(ini, section, "dirty_queue_limit", &configs_dest[renderd_section_num].dirty_queue_limit, DIRTY_LIMIT); process_config_int(ini, section, "num_threads", &configs_dest[renderd_section_num].num_threads, NUM_THREADS); + process_config_int(ini, section, "request_queue_limit", &configs_dest[renderd_section_num].request_queue_limit, REQ_LIMIT); process_config_string(ini, section, "iphostname", &configs_dest[renderd_section_num].iphostname, "", INILINE_MAX); process_config_string(ini, section, "pid_file", &configs_dest[renderd_section_num].pid_filename, RENDERD_PIDFILE, PATH_MAX); process_config_string(ini, section, "socketname", &configs_dest[renderd_section_num].socketname, RENDERD_SOCKET, PATH_MAX); @@ -708,6 +710,16 @@ void process_renderd_sections(const dictionary *ini, const char *config_file_nam configs_dest[renderd_section_num].num_threads = sysconf(_SC_NPROCESSORS_ONLN); } + if (configs_dest[renderd_section_num].request_queue_limit < 1) { + g_logger(G_LOG_LEVEL_CRITICAL, "Specified request_queue_limit (%i) is too small, must be greater than or equal to %i.", configs_dest[renderd_section_num].request_queue_limit, 1); + exit(7); + } + + if (configs_dest[renderd_section_num].dirty_queue_limit < 0) { + g_logger(G_LOG_LEVEL_CRITICAL, "Specified dirty_queue_limit (%i) is too small, must be greater than or equal to %i.", configs_dest[renderd_section_num].dirty_queue_limit, 0); + exit(7); + } + if (strnlen(configs_dest[renderd_section_num].socketname, PATH_MAX) >= renderd_socketname_maxlen) { g_logger(G_LOG_LEVEL_CRITICAL, "Specified socketname (%s) exceeds maximum allowed length of %zu.", configs_dest[renderd_section_num].socketname, renderd_socketname_maxlen); exit(7); @@ -791,6 +803,8 @@ void process_config_file(const char *config_file_name, int active_renderd_sectio } g_logger(G_LOG_LEVEL_DEBUG, "\trenderd(%i): num_threads = '%i'", i, config_slaves[i].num_threads); + g_logger(G_LOG_LEVEL_DEBUG, "\trenderd(%i): request_queue_limit = '%i'", i, config_slaves[i].request_queue_limit); + g_logger(G_LOG_LEVEL_DEBUG, "\trenderd(%i): dirty_queue_limit = '%i'", i, config_slaves[i].dirty_queue_limit); g_logger(G_LOG_LEVEL_DEBUG, "\trenderd(%i): pid_file = '%s'", i, config_slaves[i].pid_filename); if (strnlen(config_slaves[i].stats_filename, PATH_MAX)) { @@ -808,6 +822,8 @@ void process_config_file(const char *config_file_name, int active_renderd_sectio } g_logger(log_level, "\trenderd: num_threads = '%i'", config->num_threads); + g_logger(log_level, "\trenderd: request_queue_limit = '%i'", config->request_queue_limit); + g_logger(log_level, "\trenderd: dirty_queue_limit = '%i'", config->dirty_queue_limit); if (active_renderd_section_num == 0 && num_slave_threads > 0) { g_logger(log_level, "\trenderd: num_slave_threads = '%i'", num_slave_threads); diff --git a/src/request_queue.c b/src/request_queue.c index a8ecc085a..71fe2c593 100644 --- a/src/request_queue.c +++ b/src/request_queue.c @@ -16,6 +16,7 @@ */ #include +#include #include #include #include @@ -28,29 +29,97 @@ #include "render_config.h" #include "request_queue.h" +/** @brief Smallest number of hash buckets used for the duplicate-detection index. */ +#define MIN_HASHIDX_SIZE HASHIDX_SIZE + +/** + * @brief Size the duplicate-detection hash index from the configured queue limits. + * + * Keeps the index roughly proportional to the configured queue capacity, so that duplicate lookup + * stays cheap while large queues are backlogged. + * + * @param request_limit Maximum number of requests in each time-critical queue. + * @param dirty_limit Maximum number of requests in the dirty queue. + * @return Number of hash buckets, at least `MIN_HASHIDX_SIZE`. + */ +static int request_queue_hash_size(int request_limit, int dirty_limit) +{ + size_t queue_capacity = (4 * (size_t)request_limit) + (size_t)dirty_limit; + + if (queue_capacity > INT_MAX) { + return INT_MAX; + } + + return MAX(MIN_HASHIDX_SIZE, (int)queue_capacity); +} + +/** + * @brief Hash at most `max_len` bytes of a NUL-terminated string (djb2-style multiplier 33). + * + * @param value String to hash. + * @param max_len Maximum number of bytes to read. + * @return 64-bit hash of the string. + */ +static uint64_t string_hash(const char *value, size_t max_len) +{ + uint64_t hash = 0; + + for (size_t i = 0; (i < max_len) && (value[i] != 0); i++) { + hash = (hash * 33) + (unsigned char)value[i]; + } + + return hash; +} + +/** + * @brief Check whether two request items address the same tile with the same style, mime type, and options. + * + * @param item First request item. + * @param test Second request item. + * @return Non-zero if both items identify the same render request. + */ +static int same_tile_request(struct item *item, struct item *test) +{ + return (item->mx == test->mx) && + (item->my == test->my) && + (item->req.z == test->req.z) && + (strncmp(item->req.xmlname, test->req.xmlname, sizeof(item->req.xmlname)) == 0) && + (strncmp(item->req.mimetype, test->req.mimetype, sizeof(item->req.mimetype)) == 0) && + (strncmp(item->req.options, test->req.options, sizeof(item->req.options)) == 0); +} + /** * @brief Compute a hash bucket index for a metatile render request item. * - * Combines a byte-sum hash of `item->req.xmlname` (9 bits), zoom level `z` (4 bits), - * metatile X coordinate `mx` (24 bits), and metatile Y coordinate `my` (24 bits) - * into a 64-bit integer and reduces it modulo `queue->hashidxSize`. + * Combines hashes of `item->req.xmlname`, `item->req.mimetype`, and `item->req.options` with the zoom level `z` + * and metatile coordinates `mx`, `my` into a 64-bit key, mixes it, and reduces it modulo `queue->hashidxSize`. * * @param queue Request queue containing `hashidxSize`. - * @param item Render request item whose style name and `(mx, my, z)` are hashed. + * @param item Render request item to hash. * @return Zero-based bucket index in `[0, queue->hashidxSize - 1]`. */ static int calcHashKey(struct request_queue *queue, struct item *item) { - /* Additive byte checksum of the map style name */ - uint64_t xmlnameHash = 0; - /* 64-bit composite key packing xmlnameHash, z, mx, and my */ uint64_t key; - - for (int i = 0; (i < (int)sizeof(item->req.xmlname)) && (item->req.xmlname[i] != 0); i++) { - xmlnameHash += item->req.xmlname[i]; - } - - key = ((uint64_t)(xmlnameHash & 0x1FF) << 52) + ((uint64_t)(item->req.z) << 48) + ((uint64_t)(item->mx & 0xFFFFFF) << 24) + (item->my & 0xFFFFFF); + uint64_t name_hash = string_hash(item->req.xmlname, sizeof(item->req.xmlname)); + uint64_t mimetype_hash = string_hash(item->req.mimetype, sizeof(item->req.mimetype)); + uint64_t options_hash = string_hash(item->req.options, sizeof(item->req.options)); + + key = ((name_hash & 0x1FF) << 52) + + ((mimetype_hash & 0xFF) << 44) + + ((options_hash & 0xFF) << 36) + + ((uint64_t)(item->req.z) << 32) + + ((uint64_t)(item->mx & 0xFFFF) << 16) + + (item->my & 0xFFFF); + /* Avalanche every identity field into the low bits. Configurable + * capacities can yield a power-of-two index size, where the modulo + * keeps only the low bits; without mixing, same-row metatiles would + * all land in one bucket and chain scans would turn quadratic. */ + key ^= key >> 33; + key *= 0xff51afd7ed558ccdULL; + key ^= key >> 33; + key *= 0xc4ceb9fe1a85ec53ULL; + key ^= key >> 33; return key % queue->hashidxSize; } @@ -80,7 +149,7 @@ static struct item *lookup_item_idx(struct request_queue *queue, struct item *it while (nextItem != NULL) { test = nextItem->item; - if ((item->mx == test->mx) && (item->my == test->my) && (item->req.z == test->req.z) && (!strcmp(item->req.xmlname, test->req.xmlname))) { + if (same_tile_request(item, test)) { return test; } else { nextItem = nextItem->next; @@ -157,7 +226,7 @@ static void remove_item_idx(struct request_queue *queue, struct item *item) while (nextItem != NULL) { test = nextItem->item; - if ((item->mx == test->mx) && (item->my == test->my) && (item->req.z == test->req.z) && (!strcmp(item->req.xmlname, test->req.xmlname))) { + if (same_tile_request(item, test)) { /* Found matching item; unlink it from the bucket chain */ nextItem->item = NULL; @@ -187,6 +256,212 @@ static void remove_item_idx(struct request_queue *queue, struct item *item) } } +/** + * @brief Return the counter tracking the number of items in the given queue type, or `NULL` for unknown types. + */ +static int *request_queue_counter(struct request_queue *queue, enum queueEnum queue_type) +{ + switch (queue_type) { + case queueRequestPrio: + return &(queue->reqPrioNum); + + case queueRequest: + return &(queue->reqNum); + + case queueRequestLow: + return &(queue->reqLowNum); + + case queueDirty: + return &(queue->dirtyNum); + + case queueRequestBulk: + return &(queue->reqBulkNum); + + default: + return NULL; + } +} + +/** + * @brief Return the configured maximum number of items for the given queue type. + */ +static int request_queue_limit(struct request_queue *queue, enum queueEnum queue_type) +{ + return (queue_type == queueDirty) ? queue->dirtyLimit : queue->requestLimit; +} + +/** + * @brief Return the position of a queue type in the pending order (0 is served first), or `INT_MAX` if it is not pending. + */ +static int request_queue_rank(enum queueEnum queue_type) +{ + switch (queue_type) { + case queueRequestPrio: + return 0; + + case queueRequest: + return 1; + + case queueRequestLow: + return 2; + + case queueDirty: + return 3; + + case queueRequestBulk: + return 4; + + default: + return INT_MAX; + } +} + +/** + * @brief Map a render command to its queue type; returns 0 if the command is not queued. + */ +static int request_queue_for_cmd(enum protoCmd cmd, enum queueEnum *queue_type) +{ + switch (cmd) { + case cmdRenderPrio: + *queue_type = queueRequestPrio; + return 1; + + case cmdRender: + *queue_type = queueRequest; + return 1; + + case cmdRenderLow: + *queue_type = queueRequestLow; + return 1; + + case cmdDirty: + *queue_type = queueDirty; + return 1; + + case cmdRenderBulk: + *queue_type = queueRequestBulk; + return 1; + + default: + return 0; + } +} + +/** + * @brief Attach `duplicate` to the `duplicates` list of `item` so it receives a response when `item` completes. + */ +static void request_queue_attach_duplicate(struct item *item, struct item *duplicate) +{ + duplicate->duplicates = item->duplicates; + item->duplicates = duplicate; + duplicate->inQueue = queueDuplicate; +} + +/** + * @brief Remove `item` from the circular list it is linked in. + */ +static void request_queue_unlink_item(struct item *item) +{ + item->next->prev = item->prev; + item->prev->next = item->next; +} + +/** + * @brief Remove `item` from the pending list and keep the per-rank tail pointers consistent. Caller must hold `queue->qLock`. + */ +static void request_queue_unlink_pending(struct request_queue *queue, struct item *item) +{ + int rank = request_queue_rank(item->inQueue); + + if (rank < PENDING_QUEUE_RANKS && queue->pendingTail[rank] == item) { + if ((item->prev != &(queue->pendingHead)) && (request_queue_rank(item->prev->inQueue) == rank)) { + queue->pendingTail[rank] = item->prev; + } else { + queue->pendingTail[rank] = &(queue->pendingHead); + } + } + + request_queue_unlink_item(item); +} + +/** + * @brief Insert `item` at the end of its rank in the pending list. Caller must hold `queue->qLock`. + */ +static void request_queue_insert_pending(struct request_queue *queue, struct item *item) +{ + struct item *pos; + int rank = request_queue_rank(item->inQueue); + + if (rank >= PENDING_QUEUE_RANKS) { + return; + } + + pos = &(queue->pendingHead); + + for (int i = rank; i >= 0; i--) { + if (queue->pendingTail[i] != &(queue->pendingHead)) { + pos = queue->pendingTail[i]; + break; + } + } + + item->next = pos->next; + item->prev = pos; + item->prev->next = item; + item->next->prev = item; + queue->pendingTail[rank] = item; +} + +/** + * @brief Move a pending `item` to the higher-ranked queue requested by `duplicate`, if that queue has room. + * + * The duplicate is attached to `item`, so its client still receives the response. + * Caller must hold `queue->qLock`. + * + * @return 1 if `item` was promoted and `duplicate` attached, 0 otherwise. + */ +static int request_queue_promote_pending(struct request_queue *queue, struct item *item, struct item *duplicate) +{ + enum queueEnum target_queue; + int *current_count; + int *target_count; + + if (!request_queue_for_cmd(duplicate->req.cmd, &target_queue)) { + return 0; + } + + if (target_queue == queueDirty) { + return 0; + } + + if (request_queue_rank(target_queue) >= request_queue_rank(item->inQueue)) { + return 0; + } + + target_count = request_queue_counter(queue, target_queue); + current_count = request_queue_counter(queue, item->inQueue); + + if (target_count == NULL || current_count == NULL) { + return 0; + } + + if (*target_count >= request_queue_limit(queue, target_queue)) { + return 0; + } + + request_queue_unlink_pending(queue, item); + (*current_count)--; + + item->inQueue = target_queue; + item->originatedQueue = target_queue; + request_queue_insert_pending(queue, item); + (*target_count)++; + request_queue_attach_duplicate(item, duplicate); + pthread_cond_signal(&queue->qCond); + + return 1; +} + /** * @brief Check whether a matching metatile request is already queued or being rendered. * @@ -212,11 +487,12 @@ static enum protoCmd pending(struct request_queue *queue, struct item *test) item = lookup_item_idx(queue, test); if (item != NULL) { + if (request_queue_promote_pending(queue, item, test)) { + return cmdIgnore; + } + if ((item->inQueue == queueRender) || (item->inQueue == queueRequest) || (item->inQueue == queueRequestPrio) || (item->inQueue == queueRequestLow)) { - /* Prepend `test` to the existing item's duplicate chain so it receives a reply on completion */ - test->duplicates = item->duplicates; - item->duplicates = test; - test->inQueue = queueDuplicate; + request_queue_attach_duplicate(item, test); return cmdIgnore; } else if ((item->inQueue == queueDirty) || (item->inQueue == queueRequestBulk)) { return cmdNotDone; @@ -254,32 +530,45 @@ struct item *request_queue_fetch_request(struct request_queue *queue) pthread_cond_wait(&(queue->qCond), &(queue->qLock)); } - if (queue->reqPrioNum) { - item = queue->reqPrioHead.next; - queue->reqPrioNum--; - queue->stats.noReqPrioRender++; - } else if (queue->reqNum) { - item = queue->reqHead.next; - queue->reqNum--; - queue->stats.noReqRender++; - } else if (queue->reqLowNum) { - item = queue->reqLowHead.next; - queue->reqLowNum--; - queue->stats.noReqLowRender++; - } else if (queue->dirtyNum) { - item = queue->dirtyHead.next; - queue->dirtyNum--; - queue->stats.noDirtyRender++; - } else if (queue->reqBulkNum) { - item = queue->reqBulkHead.next; - queue->reqBulkNum--; - queue->stats.noReqBulkRender++; - } + item = queue->pendingHead.next; if (item) { - /* Unlink item from its source priority queue */ - item->next->prev = item->prev; - item->prev->next = item->next; + switch (item->inQueue) { + case queueRequestPrio: { + queue->reqPrioNum--; + queue->stats.noReqPrioRender++; + break; + } + + case queueRequest: { + queue->reqNum--; + queue->stats.noReqRender++; + break; + } + + case queueRequestLow: { + queue->reqLowNum--; + queue->stats.noReqLowRender++; + break; + } + + case queueDirty: { + queue->dirtyNum--; + queue->stats.noDirtyRender++; + break; + } + + case queueRequestBulk: { + queue->reqBulkNum--; + queue->stats.noReqBulkRender++; + break; + } + + default: + break; + } + + request_queue_unlink_pending(queue, item); /* Insert item at the head of the active render list */ item->prev = &(queue->renderHead); @@ -310,13 +599,16 @@ void request_queue_clear_requests_by_fd(struct request_queue *queue, int fd) /* Current queue list item, duplicate-chain iterator, and current circular sentinel head */ struct item *item, *dupes, *queueHead; - /* Only need to scan the interactive/render queues; items on dirtyHead already have FD_INVALID */ + /**Only need to scan the pending priority queue and render queue. + * Most dirty queue entries have FD_INVALID, but promoted duplicates may + * carry a client fd while the primary item still represents the same tile. + */ pthread_mutex_lock(&(queue->qLock)); - for (int i = 0; i < 5; i++) { + for (int i = 0; i < 2; i++) { switch (i) { case 0: { - queueHead = &(queue->reqHead); + queueHead = &(queue->pendingHead); break; } @@ -324,26 +616,18 @@ void request_queue_clear_requests_by_fd(struct request_queue *queue, int fd) queueHead = &(queue->renderHead); break; } - - case 2: { - queueHead = &(queue->reqPrioHead); - break; - } - - case 3: { - queueHead = &(queue->reqBulkHead); - break; - } - - case 4: { - queueHead = &(queue->reqLowHead); - break; - } } item = queueHead->next; while (item != queueHead) { + if (queueHead == &(queue->pendingHead) && item->inQueue == queueDirty) { + int rank = request_queue_rank(queueDirty); + + item = queue->pendingTail[rank]->next; + continue; + } + if (item->fd == fd) { item->fd = FD_INVALID; } @@ -384,8 +668,8 @@ enum protoCmd request_queue_add_request(struct request_queue *queue, struct item enum protoCmd status; /* Pointer to the wire protocol payload inside `item` */ const struct protocol *req; - /* Sentinel head of the target circular doubly-linked queue */ - struct item *list = NULL; + enum protoCmd add_status = cmdIgnore; + int item_added = 0; req = &(item->req); if (queue == NULL) { @@ -411,33 +695,34 @@ enum protoCmd request_queue_add_request(struct request_queue *queue, struct item return cmdIgnore; } - /* New unique request: route to the priority queue matching req->cmd, or fall back to dirtyHead */ - if ((req->cmd == cmdRender) && (queue->reqNum < REQ_LIMIT)) { - list = &(queue->reqHead); + // New request, add it to render or dirty queue + if ((req->cmd == cmdRender) && (queue->reqNum < queue->requestLimit)) { item->inQueue = queueRequest; item->originatedQueue = queueRequest; queue->reqNum++; - } else if ((req->cmd == cmdRenderPrio) && (queue->reqPrioNum < REQ_LIMIT)) { - list = &(queue->reqPrioHead); + item_added = 1; + } else if ((req->cmd == cmdRenderPrio) && (queue->reqPrioNum < queue->requestLimit)) { item->inQueue = queueRequestPrio; item->originatedQueue = queueRequestPrio; queue->reqPrioNum++; - } else if ((req->cmd == cmdRenderLow) && (queue->reqLowNum < REQ_LIMIT)) { - list = &(queue->reqLowHead); + item_added = 1; + } else if ((req->cmd == cmdRenderLow) && (queue->reqLowNum < queue->requestLimit)) { item->inQueue = queueRequestLow; item->originatedQueue = queueRequestLow; queue->reqLowNum++; - } else if ((req->cmd == cmdRenderBulk) && (queue->reqBulkNum < REQ_LIMIT)) { - list = &(queue->reqBulkHead); + item_added = 1; + } else if ((req->cmd == cmdRenderBulk) && (queue->reqBulkNum < queue->requestLimit)) { item->inQueue = queueRequestBulk; item->originatedQueue = queueRequestBulk; queue->reqBulkNum++; - } else if (queue->dirtyNum < DIRTY_LIMIT) { - list = &(queue->dirtyHead); + item_added = 1; + } else if (queue->dirtyNum < queue->dirtyLimit) { item->inQueue = queueDirty; item->originatedQueue = queueDirty; queue->dirtyNum++; - item->fd = FD_INVALID; /* Background dirty render sends no client response */ + item->fd = FD_INVALID; // No response after render + add_status = cmdNotDone; + item_added = 1; } else { /* All queues are saturated; drop the request */ queue->stats.noReqDroped++; @@ -446,13 +731,11 @@ enum protoCmd request_queue_add_request(struct request_queue *queue, struct item return cmdNotDone; } - if (list) { - /* Append item to tail of circular list (immediately before sentinel `list`) */ - item->next = list; - item->prev = list->prev; - item->prev->next = item; - list->prev = item; - /* Register item in the hash table index for O(1) duplicate detection */ + if (item_added) { + request_queue_insert_pending(queue, item); + /* In addition to the linked list, add item to a hash table index + * for faster lookup of pending requests. + */ insert_item_idx(queue, item); pthread_cond_signal(&queue->qCond); @@ -465,7 +748,7 @@ enum protoCmd request_queue_add_request(struct request_queue *queue, struct item pthread_mutex_unlock(&queue->qLock); - return (list == &(queue->dirtyHead)) ? cmdNotDone : cmdIgnore; + return add_status; } /** @@ -522,8 +805,7 @@ void request_queue_remove_request(struct request_queue *queue, struct item *requ queue->stats.timeZoomRender[request->req.z] += render_time; } - request->next->prev = request->prev; - request->prev->next = request->next; + request_queue_unlink_item(request); remove_item_idx(queue, request); pthread_mutex_unlock(&(queue->qLock)); } @@ -585,15 +867,17 @@ void request_queue_copy_stats(struct request_queue *queue, stats_struct *stats) } /** - * @brief Allocate and initialize a new `request_queue` with empty circular queues and hash index. + * @brief Allocate and initialize a new `request_queue` with empty queues and a hash index sized from the queue limits. * - * Initializes `qLock`, `qCond`, all six circular doubly-linked list sentinel heads - * (`reqHead`, `reqPrioHead`, `reqLowHead`, `reqBulkHead`, `dirtyHead`, `renderHead`), - * zeroed statistics counters, and the `HASHIDX_SIZE`-bucket hash table. + * Initializes `qLock`, `qCond`, the circular doubly-linked pending and render list sentinel heads, + * zeroed statistics counters, and the hash table. * - * @return Newly allocated `struct request_queue`, or `NULL` if memory or pthread initialization fails. + * @param request_limit Maximum number of requests in each time-critical queue (must be at least 1). + * @param dirty_limit Maximum number of requests in the dirty queue (must not be negative). + * @return Newly allocated `struct request_queue`, or `NULL` if the limits are invalid or memory or pthread initialization fails. */ -struct request_queue *request_queue_init() +struct request_queue *request_queue_init_with_limits(int request_limit, int dirty_limit) + { /* Return code from pthread synchronization primitive initialization */ int res; @@ -604,6 +888,15 @@ struct request_queue *request_queue_init() return NULL; } + if (request_limit < 1 || dirty_limit < 0) { + g_logger(G_LOG_LEVEL_ERROR, "Invalid request queue limits: request_queue_limit=%i dirty_queue_limit=%i", request_limit, dirty_limit); + free(queue); + return NULL; + } + + queue->requestLimit = request_limit; + queue->dirtyLimit = dirty_limit; + res = pthread_mutex_init(&(queue->qLock), NULL); if (res != 0) { @@ -629,13 +922,14 @@ struct request_queue *request_queue_init() queue->stats.noReqBulkRender = 0; /* Initialize all circular doubly-linked list sentinels to point to themselves */ - queue->reqHead.next = queue->reqHead.prev = &(queue->reqHead); - queue->reqPrioHead.next = queue->reqPrioHead.prev = &(queue->reqPrioHead); - queue->reqLowHead.next = queue->reqLowHead.prev = &(queue->reqLowHead); - queue->reqBulkHead.next = queue->reqBulkHead.prev = &(queue->reqBulkHead); - queue->dirtyHead.next = queue->dirtyHead.prev = &(queue->dirtyHead); + queue->pendingHead.next = queue->pendingHead.prev = &(queue->pendingHead); queue->renderHead.next = queue->renderHead.prev = &(queue->renderHead); - queue->hashidxSize = HASHIDX_SIZE; + + for (int i = 0; i < PENDING_QUEUE_RANKS; i++) { + queue->pendingTail[i] = &(queue->pendingHead); + } + + queue->hashidxSize = request_queue_hash_size(request_limit, dirty_limit); queue->item_hashidx = (struct item_idx *)malloc(sizeof(struct item_idx) * queue->hashidxSize); if (queue->item_hashidx == NULL) { @@ -651,6 +945,16 @@ struct request_queue *request_queue_init() return queue; } +/** + * @brief Allocate and initialize a new `request_queue` with the default queue limits (`REQ_LIMIT`, `DIRTY_LIMIT`). + * + * @return Newly allocated `struct request_queue`, or `NULL` if memory or pthread initialization fails. + */ +struct request_queue *request_queue_init() +{ + return request_queue_init_with_limits(REQ_LIMIT, DIRTY_LIMIT); +} + /** * @brief Destroy the request queue mutex and free the hash index and queue structure. * diff --git a/tests/gen_tile_test.cpp b/tests/gen_tile_test.cpp index 2bca412ac..33afbb19f 100644 --- a/tests/gen_tile_test.cpp +++ b/tests/gen_tile_test.cpp @@ -250,7 +250,8 @@ TEST_CASE("renderd/queueing", "request queueing") struct item *itemL = init_render_request(cmdRenderLow); request_queue_add_request(queue, itemL); - // We should be retrieving items in the order RenderPrio, Render, Dirty, Bulk + // We should retrieve items in priority queue order while preserving FIFO + // order within each priority class. item2 = request_queue_fetch_request(queue); INFO("itemRP: " << itemRP); INFO("itemR: " << itemR); @@ -358,6 +359,256 @@ TEST_CASE("renderd/queueing", "request queueing") request_queue_close(queue); } + SECTION("renderd/queueing/promote pending lower priority request", "test if later interactive requests promote already queued background work") + { + enum protoCmd res; + struct item *item; + struct item *duplicate; + struct item *fetched; + request_queue *queue = request_queue_init_with_limits(1, 4); + + REQUIRE(queue != NULL); + + item = init_render_request(cmdRenderPrio); + item->mx = 100; + res = request_queue_add_request(queue, item); + REQUIRE(res == cmdIgnore); + REQUIRE(request_queue_no_requests_queued(queue, cmdRenderPrio) == 1); + + item = init_render_request(cmdRenderPrio); + item->mx = 200; + res = request_queue_add_request(queue, item); + REQUIRE(res == cmdNotDone); + REQUIRE(request_queue_no_requests_queued(queue, cmdRenderPrio) == 1); + REQUIRE(request_queue_no_requests_queued(queue, cmdDirty) == 1); + + duplicate = init_render_request(cmdRenderPrio); + duplicate->mx = 200; + duplicate->fd = 42; + res = request_queue_add_request(queue, duplicate); + REQUIRE(res == cmdNotDone); + REQUIRE(request_queue_no_requests_queued(queue, cmdRenderPrio) == 1); + REQUIRE(request_queue_no_requests_queued(queue, cmdDirty) == 1); + + fetched = request_queue_fetch_request(queue); + REQUIRE(fetched->mx == 100); + request_queue_remove_request(queue, fetched, 0); + free(fetched); + + duplicate = init_render_request(cmdRenderPrio); + duplicate->mx = 200; + duplicate->fd = 43; + res = request_queue_add_request(queue, duplicate); + REQUIRE(res == cmdIgnore); + REQUIRE(request_queue_no_requests_queued(queue, cmdRenderPrio) == 1); + REQUIRE(request_queue_no_requests_queued(queue, cmdDirty) == 0); + + fetched = request_queue_fetch_request(queue); + REQUIRE(fetched->mx == 200); + REQUIRE(fetched->req.cmd == cmdRenderPrio); + REQUIRE(fetched->duplicates != NULL); + REQUIRE(fetched->duplicates->fd == 43); + request_queue_remove_request(queue, fetched, 0); + free(fetched->duplicates); + free(fetched); + + request_queue_close(queue); + } + + SECTION("renderd/queueing/promote dirty request", "test if dirty work is requeued when a client starts waiting for the same tile") + { + enum protoCmd res; + struct item *dirty; + struct item *duplicate; + struct item *fetched; + request_queue *queue = request_queue_init_with_limits(2, 4); + + REQUIRE(queue != NULL); + + dirty = init_render_request(cmdDirty); + dirty->mx = 300; + res = request_queue_add_request(queue, dirty); + REQUIRE(res == cmdNotDone); + REQUIRE(request_queue_no_requests_queued(queue, cmdDirty) == 1); + + duplicate = init_render_request(cmdRender); + duplicate->mx = 300; + duplicate->fd = 44; + res = request_queue_add_request(queue, duplicate); + REQUIRE(res == cmdIgnore); + REQUIRE(request_queue_no_requests_queued(queue, cmdRender) == 1); + REQUIRE(request_queue_no_requests_queued(queue, cmdDirty) == 0); + + fetched = request_queue_fetch_request(queue); + REQUIRE(fetched == dirty); + REQUIRE(fetched->req.cmd == cmdDirty); + REQUIRE(fetched->duplicates != NULL); + REQUIRE(fetched->duplicates->fd == 44); + request_queue_remove_request(queue, fetched, 0); + free(fetched->duplicates); + free(fetched); + + request_queue_close(queue); + } + + SECTION("renderd/queueing/promotion preserves primary response metadata", "test a later duplicate does not overwrite an already queued request") + { + enum protoCmd res; + struct item *low; + struct item *priority; + struct item *fetched; + request_queue *queue = request_queue_init_with_limits(2, 4); + + REQUIRE(queue != NULL); + + low = init_render_request(cmdRenderLow); + low->mx = 375; + low->my = 375; + low->req.x = 376; + low->req.y = 377; + low->fd = 48; + res = request_queue_add_request(queue, low); + REQUIRE(res == cmdIgnore); + + priority = init_render_request(cmdRenderPrio); + priority->mx = 375; + priority->my = 375; + priority->req.x = 378; + priority->req.y = 379; + priority->fd = 49; + res = request_queue_add_request(queue, priority); + REQUIRE(res == cmdIgnore); + + fetched = request_queue_fetch_request(queue); + REQUIRE(fetched == low); + REQUIRE(fetched->req.cmd == cmdRenderLow); + REQUIRE(fetched->req.x == 376); + REQUIRE(fetched->req.y == 377); + REQUIRE(fetched->duplicates != NULL); + REQUIRE(fetched->duplicates->req.cmd == cmdRenderPrio); + REQUIRE(fetched->duplicates->req.x == 378); + REQUIRE(fetched->duplicates->req.y == 379); + request_queue_remove_request(queue, fetched, 0); + free(fetched->duplicates); + free(fetched); + + request_queue_close(queue); + } + + SECTION("renderd/queueing/options keep requests distinct", "test option variants are not deduplicated or promoted together") + { + enum protoCmd res; + struct item *dirty; + struct item *duplicate; + struct item *fetched; + request_queue *queue = request_queue_init_with_limits(2, 4); + + REQUIRE(queue != NULL); + + dirty = init_render_request(cmdDirty); + dirty->mx = 325; + strcpy(dirty->req.options, "first"); + res = request_queue_add_request(queue, dirty); + REQUIRE(res == cmdNotDone); + REQUIRE(request_queue_no_requests_queued(queue, cmdDirty) == 1); + + duplicate = init_render_request(cmdRender); + duplicate->mx = 325; + duplicate->fd = 46; + strcpy(duplicate->req.options, "second"); + res = request_queue_add_request(queue, duplicate); + REQUIRE(res == cmdIgnore); + REQUIRE(request_queue_no_requests_queued(queue, cmdRender) == 1); + REQUIRE(request_queue_no_requests_queued(queue, cmdDirty) == 1); + + fetched = request_queue_fetch_request(queue); + REQUIRE(fetched == duplicate); + REQUIRE((std::string)fetched->req.options == "second"); + request_queue_remove_request(queue, fetched, 0); + free(fetched); + + fetched = request_queue_fetch_request(queue); + REQUIRE(fetched == dirty); + REQUIRE((std::string)fetched->req.options == "first"); + request_queue_remove_request(queue, fetched, 0); + free(fetched); + + request_queue_close(queue); + } + + SECTION("renderd/queueing/full-width options keep requests distinct", "test fixed-width option fields do not require NUL termination") + { + enum protoCmd res; + struct item *first; + struct item *second; + struct item *fetched; + request_queue *queue = request_queue_init_with_limits(2, 4); + + REQUIRE(queue != NULL); + + first = init_render_request(cmdRender); + first->mx = 326; + memset(first->req.options, 'a', sizeof(first->req.options)); + res = request_queue_add_request(queue, first); + REQUIRE(res == cmdIgnore); + + second = init_render_request(cmdRender); + second->mx = 326; + memset(second->req.options, 'a', sizeof(second->req.options)); + second->req.options[sizeof(second->req.options) - 1] = 'b'; + res = request_queue_add_request(queue, second); + REQUIRE(res == cmdIgnore); + REQUIRE(request_queue_no_requests_queued(queue, cmdRender) == 2); + + fetched = request_queue_fetch_request(queue); + REQUIRE(fetched == first); + REQUIRE(memcmp(fetched->req.options, first->req.options, sizeof(fetched->req.options)) == 0); + request_queue_remove_request(queue, fetched, 0); + free(fetched); + + fetched = request_queue_fetch_request(queue); + REQUIRE(fetched == second); + REQUIRE(memcmp(fetched->req.options, second->req.options, sizeof(fetched->req.options)) == 0); + request_queue_remove_request(queue, fetched, 0); + free(fetched); + + request_queue_close(queue); + } + + SECTION("renderd/queueing/dirty duplicate does not demote bulk request", "test background work does not replace already queued bulk work") + { + enum protoCmd res; + struct item *bulk; + struct item *dirty; + struct item *fetched; + request_queue *queue = request_queue_init_with_limits(2, 4); + + REQUIRE(queue != NULL); + + bulk = init_render_request(cmdRenderBulk); + bulk->mx = 350; + bulk->fd = 45; + res = request_queue_add_request(queue, bulk); + REQUIRE(res == cmdIgnore); + REQUIRE(request_queue_no_requests_queued(queue, cmdRenderBulk) == 1); + + dirty = init_render_request(cmdDirty); + dirty->mx = 350; + res = request_queue_add_request(queue, dirty); + REQUIRE(res == cmdNotDone); + REQUIRE(request_queue_no_requests_queued(queue, cmdRenderBulk) == 1); + REQUIRE(request_queue_no_requests_queued(queue, cmdDirty) == 0); + + fetched = request_queue_fetch_request(queue); + REQUIRE(fetched == bulk); + REQUIRE(fetched->req.cmd == cmdRenderBulk); + REQUIRE(fetched->fd == 45); + request_queue_remove_request(queue, fetched, 0); + free(fetched); + + request_queue_close(queue); + } + SECTION("renderd/queueing/overflow requests", "test if requests correctly overflow from one request priority to the next") { enum protoCmd res; @@ -392,6 +643,35 @@ TEST_CASE("renderd/queueing", "request queueing") request_queue_close(queue); } + SECTION("renderd/queueing/configured overflow limits", "test if configurable queue limits are honoured") + { + enum protoCmd res; + struct item *item; + request_queue *queue = request_queue_init_with_limits(2, 3); + + REQUIRE(queue != NULL); + + for (int i = 1; i < 8; i++) { + item = init_render_request(cmdRenderPrio); + res = request_queue_add_request(queue, item); + + if (i <= 2) { + REQUIRE(res == cmdIgnore); + REQUIRE(request_queue_no_requests_queued(queue, cmdRenderPrio) == i); + } else if (i <= 5) { + REQUIRE(res == cmdNotDone); + REQUIRE(request_queue_no_requests_queued(queue, cmdRenderPrio) == 2); + REQUIRE(request_queue_no_requests_queued(queue, cmdDirty) == (i - 2)); + } else { + REQUIRE(res == cmdNotDone); + REQUIRE(request_queue_no_requests_queued(queue, cmdRenderPrio) == 2); + REQUIRE(request_queue_no_requests_queued(queue, cmdDirty) == 3); + } + } + + request_queue_close(queue); + } + SECTION("renderd/queueing/multithreading request addition", "test if there are any issues with multithreading") { pthread_t *addition_threads; @@ -559,6 +839,37 @@ TEST_CASE("renderd/queueing", "request queueing") request_queue_close(queue); } + + SECTION("renderd/queueing/clear fd skips dirty backlog but reaches bulk", "Test fd clearing avoids fd-less dirty entries") + { + struct request_queue *queue = request_queue_init_with_limits(4, 4); + struct item *item; + + for (int i = 0; i < 4; i++) { + item = init_render_request(cmdDirty); + request_queue_add_request(queue, item); + } + + item = init_render_request(cmdRenderBulk); + item->fd = 47; + request_queue_add_request(queue, item); + request_queue_clear_requests_by_fd(queue, 47); + + for (int i = 0; i < 4; i++) { + item = request_queue_fetch_request(queue); + REQUIRE(item->req.cmd == cmdDirty); + request_queue_remove_request(queue, item, 0); + free(item); + } + + item = request_queue_fetch_request(queue); + REQUIRE(item->req.cmd == cmdRenderBulk); + REQUIRE(item->fd == FD_INVALID); + request_queue_remove_request(queue, item, 0); + free(item); + + request_queue_close(queue); + } } TEST_CASE("renderd", "tile generation") diff --git a/tests/unit_test_renderd_config.cpp b/tests/unit_test_renderd_config.cpp index 64ddd7ca3..d6b8c164a 100644 --- a/tests/unit_test_renderd_config.cpp +++ b/tests/unit_test_renderd_config.cpp @@ -809,6 +809,60 @@ TEST_CASE("renderd_config.c", "[renderd_config]") REQUIRE_THAT(err_log_lines, Catch::Matchers::ContainsSubstring("renderd: ip socket = 'hostname:9999'")); } + SECTION("renderd.conf renderd section request_queue_limit too small", "should exit 7") + { + renderd_conf_file << "[mapnik]\n[map]\n"; + renderd_conf_file << "[renderd]\nrequest_queue_limit=0\n"; + renderd_conf_file.close(); + + if (setjmp(exit_jump) == 0) { + process_config_file(renderd_conf_path.c_str(), 0, 0); + FAIL("process_config_file should have called exit(), but did not"); + } else { + SUCCEED("process_config_file captured expected exit() call"); + } + + REQUIRE(exit_status == 7); + REQUIRE_THAT(err_log_lines, Catch::Matchers::ContainsSubstring("Specified request_queue_limit (0) is too small, must be greater than or equal to 1.")); + } + + SECTION("renderd.conf renderd section dirty_queue_limit too small", "should exit 7") + { + renderd_conf_file << "[mapnik]\n[map]\n"; + renderd_conf_file << "[renderd]\ndirty_queue_limit=-1\n"; + renderd_conf_file.close(); + + if (setjmp(exit_jump) == 0) { + process_config_file(renderd_conf_path.c_str(), 0, 0); + FAIL("process_config_file should have called exit(), but did not"); + } else { + SUCCEED("process_config_file captured expected exit() call"); + } + + REQUIRE(exit_status == 7); + REQUIRE_THAT(err_log_lines, Catch::Matchers::ContainsSubstring("Specified dirty_queue_limit (-1) is too small, must be greater than or equal to 0.")); + } + + SECTION("renderd.conf renderd section with configured queue limits", "should not exit") + { + renderd_conf_file << "[mapnik]\n[map]\n"; + renderd_conf_file << "[renderd]\nrequest_queue_limit=42\ndirty_queue_limit=4242\n"; + renderd_conf_file.close(); + + if (setjmp(exit_jump) == 0) { + process_config_file(renderd_conf_path.c_str(), 0, 0); + SUCCEED("process_config_file did not call exit(), as expected"); + } else { + FAIL("process_config_file captured unexpected exit() call"); + } + + REQUIRE(exit_status == 0); + REQUIRE_THAT(err_log_lines, Catch::Matchers::ContainsSubstring("renderd(0): request_queue_limit = '42'")); + REQUIRE_THAT(err_log_lines, Catch::Matchers::ContainsSubstring("renderd(0): dirty_queue_limit = '4242'")); + REQUIRE_THAT(err_log_lines, Catch::Matchers::ContainsSubstring("renderd: request_queue_limit = '42'")); + REQUIRE_THAT(err_log_lines, Catch::Matchers::ContainsSubstring("renderd: dirty_queue_limit = '4242'")); + } + SECTION("renderd.conf with overlapping URIs", "should return 7") { std::string map0_uri = GENERATE("/", "/map1/", "/map2/"); diff --git a/tests/unit_test_request_queue.cpp b/tests/unit_test_request_queue.cpp index b629052f8..fbda65792 100644 --- a/tests/unit_test_request_queue.cpp +++ b/tests/unit_test_request_queue.cpp @@ -126,7 +126,7 @@ TEST_CASE("request_queue.c", "[request_queue]") REQUIRE(queue != NULL); REQUIRE(queue->item_hashidx != NULL); - REQUIRE(queue->hashidxSize == HASHIDX_SIZE); + REQUIRE(queue->hashidxSize == (4 * REQ_LIMIT + DIRTY_LIMIT)); REQUIRE(request_queue_no_requests_queued(queue, cmdRenderPrio) == 0); REQUIRE(request_queue_no_requests_queued(queue, cmdRender) == 0); REQUIRE(request_queue_no_requests_queued(queue, cmdRenderLow) == 0); @@ -319,21 +319,23 @@ TEST_CASE("request_queue.c", "[request_queue]") free(duplicate2); } - SECTION("Dirty queue pending requests reject duplicates with cmdNotDone") + SECTION("Dirty queue pending requests are promoted by a waiting client") { struct item *dirtyItem = create_test_item(cmdDirty, XMLCONFIG_DEFAULT, 300, 400, 8, FD_INVALID); - struct item *dupDirty = create_test_item(cmdRender, XMLCONFIG_DEFAULT, 300, 400, 8, 20); + struct item *dupRender = create_test_item(cmdRender, XMLCONFIG_DEFAULT, 300, 400, 8, 20); REQUIRE(request_queue_add_request(queue, dirtyItem) == cmdNotDone); - - REQUIRE(request_queue_add_request(queue, dupDirty) == cmdNotDone); - REQUIRE(request_queue_no_requests_queued(queue, cmdDirty) == 1); + REQUIRE(request_queue_add_request(queue, dupRender) == cmdIgnore); + REQUIRE(request_queue_no_requests_queued(queue, cmdDirty) == 0); + REQUIRE(request_queue_no_requests_queued(queue, cmdRender) == 1); + REQUIRE(dirtyItem->duplicates == dupRender); struct item *fetched = request_queue_fetch_request(queue); REQUIRE(fetched == dirtyItem); request_queue_remove_request(queue, fetched, 0); free(dirtyItem); + free(dupRender); } SECTION("Requests overflow from render queues to dirty queue when REQ_LIMIT reached") From c6a11c6ea771749bc70de9b4d970b7d2098eb528 Mon Sep 17 00:00:00 2001 From: Darafei Praliaskouski Date: Sun, 22 Nov 2026 15:44:30 +0400 Subject: [PATCH 2/2] fix(renderd): read queue limits from the active config pointer --- src/renderd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderd.c b/src/renderd.c index f7ca12c4c..59fe0c1ff 100644 --- a/src/renderd.c +++ b/src/renderd.c @@ -980,7 +980,7 @@ int main(int argc, char **argv) } g_logger(G_LOG_LEVEL_INFO, "Initialising request queue"); - render_request_queue = request_queue_init_with_limits(config.request_queue_limit, config.dirty_queue_limit); + render_request_queue = request_queue_init_with_limits(config->request_queue_limit, config->dirty_queue_limit); if (render_request_queue == NULL) { g_logger(G_LOG_LEVEL_CRITICAL, "Failed to initialise request queue");