Repository: valkey-io/valkey
File: src/server.h (C/C++)

Code before:
    uint64_t ever_authenticated : 1;       /* Indicate a client was ever successfully authenticated during it's lifetime */
    uint64_t protected_rdb_channel : 1;    /* Dual channel replication sync: Protects the RDB client from premature \
                                            * release during full sync. This flag is used to ensure that the RDB client, which \
                                            * references the first replication data block required by the replica, is not \
                                            * released prematurely. Protecting the client is crucial for prevention of \
                                            * synchronization failures: \
                                            * If the RDB client is released before the replica initiates PSYNC, the primary \
                                            * will reduce the reference count (o->refcount) of the block needed by the replica.
                                            * \
                                            * This could potentially lead to the removal of the required data block, resulting \
                                            * in synchronization failures. Such failures could occur even in scenarios where \
                                            * the replica only needs an additional 4KB beyond the minimum size of the
                                            * repl_backlog.
                                            * By using this flag, we ensure that the RDB client remains intact until the replica
                                            * \ has successfully initiated PSYNC. */
    uint64_t repl_rdb_channel : 1;         /* Dual channel replication sync: track a connection which is used for rdb snapshot */
    uint64_t dont_cache_primary : 1;       /* In some cases we don't want to cache the primary. For example, the replica
                                            * knows that it does not need the cache and required a full sync. With this
                                            * flag, we won't cache the primary in freeClient. */
    uint64_t fake : 1;                     /* This is a fake client without a real connection. */
    uint64_t import_source : 1;            /* This client is importing data to server and can visit expired key. */
    uint64_t buffered_reply : 1;           /* Indicates the reply for the current command was buffered, either in client::reply
                                              or client::buf. */
    uint64_t keyspace_notified : 1;        /* Indicates that a keyspace notification was triggered during the execution of the
                                              current command. */
    uint64_t track_net_output_bytes : 1;   /* Track net_output_bytes_curr_cmd (disabled when commandlog-request-larger-than is -1). */
} ClientFlags;

typedef struct ClientPubSubData {
    hashtable *pubsub_channels;      /* channels a client is interested in (SUBSCRIBE) */
    hashtable *pubsub_patterns;      /* patterns a client is interested in (PSUBSCRIBE) */
    hashtable *pubsubshard_channels; /* shard level channels a client is interested in (SSUBSCRIBE) */
    /* If this client is in tracking mode and this field is non zero,
     * invalidation messages for keys fetched by this client will be sent to
     * the specified client ID. */
    uint64_t client_tracking_redirection;
    rax *client_tracking_prefixes; /* A dictionary of prefixes we are already
                                      subscribed to in BCAST mode, in the
                                      context of client side caching. */
} ClientPubSubData;

typedef struct ClientReplicationData {
    int repl_state;                      /* Replication state if this is a replica. */
    int repl_start_cmd_stream_on_ack;    /* Install replica write handler on first ACK. */
    int repldbfd;                        /* Replication DB file descriptor. */
    off_t repldboff;                     /* Replication DB file offset. */
    off_t repldbsize;                    /* Replication DB file size. */
    sds replpreamble;                    /* Replication DB preamble. */
    long long read_reploff;              /* Read replication offset if this is a primary. */
    long long reploff;                   /* Applied replication offset if this is a primary. */
    long long repl_applied;              /* Applied replication data count in querybuf, if this is a replica. */

Review comment (suggestion):
I don't think a client flag is the right way. Client flags are not thread safe between main thread and IO threads. Therefore, we don't access them from the IO threads. The main thread might change them while an IO thread is performing some work for the same client.

We have client `read_flags` and `write_flags` instead to communicate flags from/to IO threads. `c->write_flag` is guarded by `c->c->io_write_state`. If the latter is `CLIENT_PENDING_IO`, the IO thread owns the write flags. Otherwise, the main thread owns them and can modify them. Something like that. I hope we can use a write flag. I will think about it more.

Another detail: There are already 64 flags. With one more, it is 65 and it doesn't fit in one 64-bit integer anymore. In one place, there is a `union { ClientFlags flag; uint64_t raw_flags }` to be able to clear all flags by setting raw_flags = 0, but this doesn't work with > 64 flags. (We should have added a static assert for it.)


Code after:
    uint64_t ever_authenticated : 1;       /* Indicate a client was ever successfully authenticated during it's lifetime */
    uint64_t protected_rdb_channel : 1;    /* Dual channel replication sync: Protects the RDB client from premature \
                                            * release during full sync. This flag is used to ensure that the RDB client, which \
                                            * references the first replication data block required by the replica, is not \
                                            * released prematurely. Protecting the client is crucial for prevention of \
                                            * synchronization failures: \
                                            * If the RDB client is released before the replica initiates PSYNC, the primary \
                                            * will reduce the reference count (o->refcount) of the block needed by the replica.
                                            * \
                                            * This could potentially lead to the removal of the required data block, resulting \
                                            * in synchronization failures. Such failures could occur even in scenarios where \
                                            * the replica only needs an additional 4KB beyond the minimum size of the
                                            * repl_backlog.
                                            * By using this flag, we ensure that the RDB client remains intact until the replica
                                            * \ has successfully initiated PSYNC. */
    uint64_t repl_rdb_channel : 1;         /* Dual channel replication sync: track a connection which is used for rdb snapshot */
    uint64_t dont_cache_primary : 1;       /* In some cases we don't want to cache the primary. For example, the replica
                                            * knows that it does not need the cache and required a full sync. With this
                                            * flag, we won't cache the primary in freeClient. */
    uint64_t fake : 1;                     /* This is a fake client without a real connection. */
    uint64_t import_source : 1;            /* This client is importing data to server and can visit expired key. */
    uint64_t buffered_reply : 1;           /* Indicates the reply for the current command was buffered, either in client::reply
                                              or client::buf. */
    uint64_t keyspace_notified : 1;        /* Indicates that a keyspace notification was triggered during the execution of the
                                              current command. */
} ClientFlags;

typedef struct ClientPubSubData {
    hashtable *pubsub_channels;      /* channels a client is interested in (SUBSCRIBE) */
    hashtable *pubsub_patterns;      /* patterns a client is interested in (PSUBSCRIBE) */
    hashtable *pubsubshard_channels; /* shard level channels a client is interested in (SSUBSCRIBE) */
    /* If this client is in tracking mode and this field is non zero,
     * invalidation messages for keys fetched by this client will be sent to
     * the specified client ID. */
    uint64_t client_tracking_redirection;
    rax *client_tracking_prefixes; /* A dictionary of prefixes we are already
                                      subscribed to in BCAST mode, in the
                                      context of client side caching. */
} ClientPubSubData;

typedef struct ClientReplicationData {
    int repl_state;                      /* Replication state if this is a replica. */
    int repl_start_cmd_stream_on_ack;    /* Install replica write handler on first ACK. */
    int repldbfd;                        /* Replication DB file descriptor. */
    off_t repldboff;                     /* Replication DB file offset. */
    off_t repldbsize;                    /* Replication DB file size. */
    sds replpreamble;                    /* Replication DB preamble. */
    long long read_reploff;              /* Read replication offset if this is a primary. */
    long long reploff;                   /* Applied replication offset if this is a primary. */
    long long repl_applied;              /* Applied replication data count in querybuf, if this is a replica. */
    long long repl_ack_off;              /* Replication ack offset, if this is a replica. */