/* * Copyright (c) 2009-Present, Redis Ltd. * All rights reserved. * * Copyright (c) 2024-present, Valkey contributors. * All rights reserved. * * Licensed under your choice of (a) the Redis Source Available License 2.0 * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the * GNU Affero General Public License v3 (AGPLv3). * * Portions of this file are available under BSD3 terms; see REDISCONTRIBUTIONS for more information. */ #ifndef __CLUSTER_H #define __CLUSTER_H /*----------------------------------------------------------------------------- * Redis cluster exported API. *----------------------------------------------------------------------------*/ #define CLUSTER_SLOT_MASK_BITS 14 /* Number of bits used for slot id. */ #define CLUSTER_SLOTS (1< redis: initiates/advances/cancels ASM operations * - clusterAsmOnEvent(...) redis -> impl: notifies state changes * * Generic steps for an alternative implementation: * - On destination side, implementation calls clusterAsmProcess(ASM_EVENT_IMPORT_START) * to start an import operation. * - Redis calls clusterAsmOnEvent() when an ASM event occurs. * - On the source side, Redis will call clusterAsmOnEvent(ASM_EVENT_HANDOFF_PREP) * when slots are ready to be handed off and the write pause is needed. * - Implementation stops the traffic to the slots and calls clusterAsmProcess(ASM_EVENT_HANDOFF) * - On the destination side, Redis calls clusterAsmOnEvent(ASM_EVENT_TAKEOVER) * when destination node is ready to take over the slot, waiting for ownership change. * - Cluster implementation updates the config and calls clusterAsmProcess(ASM_EVENT_DONE) * to notify Redis that the slots ownership has changed. * * Sequence diagram for import: * - Note: shows only the events that cluster implementation needs to react. * * ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ * │ Destination │ │ Destination │ │ Source │ │ Source │ * │ Cluster impl │ │ Master │ │ Master │ │ Cluster impl │ * └───────┬───────┘ └───────┬───────┘ └───────┬───────┘ └───────┬───────┘ * │ │ │ │ * │ ASM_EVENT_IMPORT_START │ │ │ * ├─────────────────────────────►│ │ │ * │ │ CLUSTER SYNCSLOTS │ │ * │ ├────────────────────────►│ │ * │ │ │ │ * │ │ SNAPSHOT(restore cmds) │ │ * │ │◄────────────────────────┤ │ * │ │ Repl stream │ │ * │ │◄────────────────────────┤ │ * │ │ │ ASM_EVENT_HANDOFF_PREP │ * │ │ ├────────────────────────────►│ * │ │ │ ASM_EVENT_HANDOFF │ * │ │ │◄────────────────────────────┤ * │ │ Drain repl stream │ │ * │ │◄────────────────────────┤ │ * │ ASM_EVENT_TAKEOVER │ │ │ * │◄─────────────────────────────┤ │ │ * │ │ │ │ * │ ASM_EVENT_DONE │ │ │ * ├─────────────────────────────►│ │ ASM_EVENT_DONE │ * │ │ │◄────────────────────────────┤ * │ │ │ │ */ #define ASM_EVENT_IMPORT_START 1 /* Start a new import operation (destination side) */ #define ASM_EVENT_CANCEL 2 /* Cancel an ongoing import/migrate operation (source and destination side) */ #define ASM_EVENT_HANDOFF_PREP 3 /* Slot is ready to be handed off to the destination shard (source side) */ #define ASM_EVENT_HANDOFF 4 /* Notify that the slot can be handed off (source side) */ #define ASM_EVENT_TAKEOVER 5 /* Ready to take over the slot, waiting for config change (destination side) */ #define ASM_EVENT_DONE 6 /* Notify that import/migrate is completed, config is updated (source and destination side) */ #define ASM_EVENT_IMPORT_PREP 7 /* Import is about to start, the implementation may reject by returning C_ERR */ #define ASM_EVENT_IMPORT_STARTED 8 /* Import started */ #define ASM_EVENT_IMPORT_FAILED 9 /* Import failed */ #define ASM_EVENT_IMPORT_COMPLETED 10 /* Import completed (config updated) */ #define ASM_EVENT_MIGRATE_PREP 11 /* Migrate is about to start, the implementation may reject by returning C_ERR */ #define ASM_EVENT_MIGRATE_STARTED 12 /* Migrate started */ #define ASM_EVENT_MIGRATE_FAILED 13 /* Migrate failed */ #define ASM_EVENT_MIGRATE_COMPLETED 14 /* Migrate completed (config updated) */ /* Called by cluster implementation to request an ASM operation. (cluster impl --> redis) * Valid values for 'event': * ASM_EVENT_IMPORT_START * ASM_EVENT_CANCEL * ASM_EVENT_HANDOFF * ASM_EVENT_DONE * * For ASM_EVENT_IMPORT_START, 'task_id' should be a unique string. * For other events (ASM_EVENT_CANCEL, ASM_EVENT_HANDOFF, ASM_EVENT_DONE), * 'task_id' should match the ID from the corresponding import operation. * Usage: * char *task_id = malloc(CLUSTER_NAMELEN + 1); * getRandomHexChars(task_id, CLUSTER_NAMELEN); * task_id[CLUSTER_NAMELEN] = '\0'; * * slotRangeArray *slots = slotRangeArrayCreate(1); * slotRangeArraySet(slots, 0, 0, 1000); * * const char *err = NULL; * int ret = clusterAsmProcess(task_id, ASM_EVENT_IMPORT_START, slots, &err); * zfree(task_id); * slotRangeArrayFree(slots); * * if (ret != C_OK) { * perror(err); * return; * } * * For ASM_EVENT_CANCEL, if `task_id` is NULL, all tasks will be cancelled. * If `arg` parameter is provided, it should be a pointer to an int. It will be * set to the number of tasks cancelled. * * Return value: * - Returns C_OK on success, C_ERR on failure and 'err' will be set to the * error message. * * Memory management: * - There is no ownership transfer of 'task_id', 'err' or `slotRangeArray`. * - `task_id` and `slotRangeArray` should be allocated and be freed by the * caller. Redis internally will make a copy of these. * - `err` is allocated by Redis and should NOT be freed by the caller. **/ int clusterAsmProcess(const char *task_id, int event, void *arg, char **err); /* Called when an ASM event occurs to notify the cluster implementation. (redis --> cluster impl) * * `arg` will point to a `slotRangeArray` for the following events: * ASM_EVENT_IMPORT_PREP * ASM_EVENT_IMPORT_STARTED * ASM_EVENT_MIGRATE_PREP * ASM_EVENT_MIGRATE_STARTED * ASM_EVENT_HANDOFF_PREP * * Memory management: * - Redis owns the `task_id` and `slotRangeArray`. * * Returns C_OK on success. * * If the cluster implementation returns C_ERR for ASM_EVENT_IMPORT_PREP or * ASM_EVENT_MIGRATE_PREP, operation will not start. **/ int clusterAsmOnEvent(const char *task_id, int event, void *arg); #endif /* __CLUSTER_H */