/*
 * Copyright (c) 2022 Huawei Technologies Co.,Ltd.
 *
 * DSS is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *          http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * -------------------------------------------------------------------------
 *
 * dss_session.h
 *
 *
 * IDENTIFICATION
 *    src/common/dss_session.h
 *
 * -------------------------------------------------------------------------
 */

#ifndef __DSS_SESSION_H__
#define __DSS_SESSION_H__

#include "cm_defs.h"
#include "cm_thread_pool.h"
#include "cm_date.h"
#include "cs_packet.h"
#include "cs_pipe.h"
#include "dss_defs.h"
#include "dss_au.h"
#include "dss_log.h"
#include "dss_protocol.h"
#include "dss_latch.h"
#include "dss_stats.h"
#include "dss_shm.h"

#ifdef __cplusplus
extern "C" {
#endif

#define DSS_INVALID_SESSIONID CM_INVALID_ID32
#define DSS_MAX_LATCH_STACK_BOTTON 0
#define DSS_MAX_LATCH_STACK_DEPTH 8
#define DSS_LOCK_CLEAN_SLEEP_TIME 500
#define DSS_SESSION_PAUSED_WAIT 50

#define DSS_META_SYN_BG_TASK_NUM_MAX 4
#define DSS_RECYLE_META_TASK_NUM_MAX 4

#define DSS_SERVER_SESS_TIMEOUT 100
typedef enum st_dss_background_task_type {
    DSS_RECOVERY_BACKGROUND_TASK = 0,
    DSS_DELAY_CLEAN_BACKGROUND_TASK = 1,
    DSS_HASHMAP_DYNAMIC_EXTEND_TASK = 2,
    DSS_ALARM_CHECK_TASK = 3,
    DSS_DYN_LOG_TASK = 4,
    DSS_META_SYN_BG_TASK_BASE = 5,
    DSS_META_SYN_BG_TASK_END = DSS_META_SYN_BG_TASK_BASE + DSS_META_SYN_BG_TASK_NUM_MAX,
    DSS_RECYCLE_META_TASK_BASE = DSS_META_SYN_BG_TASK_END,
    DSS_RECYCLE_META_TASK_END = DSS_RECYCLE_META_TASK_BASE + DSS_RECYLE_META_TASK_NUM_MAX,
    DSS_BACKGROUND_TASK_NUM = DSS_RECYCLE_META_TASK_END,
} dss_background_task_type_e;

typedef struct st_dss_bg_task_info {
    uint32 task_num_max;
    uint32 my_task_id;
    uint32 vg_id_beg;
    uint32 vg_id_end;
    void *task_args;
} dss_bg_task_info_t;

typedef struct dss_cli_info {
    uint64 cli_pid;
    int64 start_time;
    char process_name[DSS_FILE_NAME_BUFFER_SIZE + 1];
    uint32 thread_id;
    uint64 connect_time;
} dss_cli_info_t;

typedef enum st_dss_latch_offset_type {
    DSS_LATCH_OFFSET_INVALID = 0,
    DSS_LATCH_OFFSET_UNIQ_ID,
    DSS_LATCH_OFFSET_SHMOFFSET,
} dss_latch_offset_type_e;

typedef struct st_dss_latch_offset {
    dss_latch_offset_type_e type;
    union {
        uint32 unique_id;
        uint64 shm_offset;
    } offset;
} dss_latch_offset_t;

typedef struct st_dss_latch_stack {
    dss_latch_offset_t latch_offset_stack[DSS_MAX_LATCH_STACK_DEPTH];
    uint32 stack_top;
    uint32 stack_top_bak;
    dss_latch_shared_op_e op;
} dss_latch_stack_t;

typedef enum en_protocol_type {
    PROTO_TYPE_UNKNOWN = 0,
    PROTO_TYPE_GS = 1,
} protocol_type_t;

typedef enum en_dss_session_status {
    DSS_SESSION_STATUS_IDLE = 0,
    DSS_SESSION_STATUS_RUNNING,
    DSS_SESSION_STATUS_PAUSING,
    DSS_SESSION_STATUS_PAUSED,
} dss_session_status_t;

typedef struct st_dss_session {
    spinlock_t lock;  // for control current rw of the same session in server
    uint32 id;
    bool32 is_closed;
    bool32 is_used;
    bool32 connected;
    bool32 reactor_added;
    cs_pipe_t pipe;
    dss_packet_t recv_pack;
    dss_packet_t send_pack;
    text_t send_info;  // send extra info, please use recv_pack.init_buf, len = 0 if no info ack
    ftid_t curr_dir;
    protocol_type_t proto_type;  // gauss or mysql (not realized)
    dss_cli_info_t cli_info;
    dss_latch_stack_t latch_stack;
    volatile uint64 curr_lsn;  // latest lsn generated by current session
    dss_audit_info_t audit_info;
    dss_session_status_t status;
    void *reactor;
    void *workthread_ctx;
    dss_stat_item_t dss_session_stat[DSS_EVT_COUNT];
    uint32 client_version; /* client version */
    uint32 proto_version;  /* client and server negotiated version */
    uint32 objectid;
    dss_stat_ctx_t stat_ctx;
    bool8 is_direct;
    bool8 put_log;
    bool8 is_holding_hotpatch_latch;
    bool8 is_remote_req;  // marks remote (standby) request, use force mode for lock to avoid deadlock
    spinlock_t shm_lock;  // for control current rw of the same session in shm
    char *thv_read_buf;
} dss_session_t;

static inline char *dss_init_sendinfo_buf(char *input)
{
    return (input + sizeof(dss_packet_head_t) + sizeof(int32));
}

static inline void dss_session_end_stat(dss_session_t *session, timeval_t *begin_tv, dss_wait_event_e event)
{
    if (session != NULL) {
        dss_end_stat_base(&session->dss_session_stat[event], begin_tv);
    }
}

typedef struct st_dss_session_ctrl {
    spinlock_t lock;
    bool32 is_inited;
    uint32 used_count;
    uint32 total;
    uint32 alloc_sessions;
    dss_session_t **sessions;
} dss_session_ctrl_t;

extern dss_session_ctrl_t g_dss_session_ctrl;
status_t dss_init_session_pool(uint32 max_session_num);
dss_session_ctrl_t *dss_get_session_ctrl(void);
status_t dss_create_session(const cs_pipe_t *pipe, dss_session_t **session);
void dss_destroy_session(dss_session_t *session);
void dss_destroy_session_inner(dss_session_t *session);
dss_session_t *dss_get_session(uint32 sid);

status_t dss_lock_shm_meta_s_with_stack(
    dss_session_t *session, dss_latch_offset_t *offset, dss_shared_latch_t *shared_latch, int32 timeout);
status_t dss_lock_shm_meta_s_without_stack(
    dss_session_t *session, dss_shared_latch_t *shared_latch, bool32 is_force, int32 timeout);
status_t dss_cli_lock_shm_meta_s(dss_session_t *session, dss_latch_offset_t *offset, dss_shared_latch_t *shared_latch,
    latch_should_exit should_exit);
void dss_lock_shm_meta_x(const dss_session_t *session, dss_shared_latch_t *shared_latch);
void dss_lock_shm_meta_x2ix(dss_session_t *session, dss_shared_latch_t *shared_latch);
void dss_lock_shm_meta_ix2x(dss_session_t *session, dss_shared_latch_t *shared_latch);
void dss_lock_shm_meta_degrade(dss_session_t *session, dss_shared_latch_t *shared_latch);
// only used by dssserver
void dss_unlock_shm_meta_without_stack(dss_session_t *session, dss_shared_latch_t *shared_latch);
// only used by api-client or by clean
bool32 dss_unlock_shm_meta_s_with_stack(dss_session_t *session, dss_shared_latch_t *shared_latch, bool32 is_try_lock);
status_t dss_lock_shm_meta_bucket_s(dss_session_t *session, uint32 id, dss_shared_latch_t *shared_latch);
void dss_lock_shm_meta_bucket_x(dss_session_t *session, dss_shared_latch_t *shared_latch);
void dss_unlock_shm_meta_bucket(dss_session_t *session, dss_shared_latch_t *shared_latch);
void dss_clean_session_latch(dss_session_t *session, bool32 is_daemon);
uint32 dss_get_udssession_startid(void);
uint32 dss_get_recover_task_idx(void);
uint32 dss_get_max_total_session_cnt(void);
uint32 dss_get_delay_clean_task_idx(void);
uint32 dss_get_hashmap_dynamic_extend_task_idx(void);
bool32 dss_lock_shm_meta_timed_x(const dss_session_t *session, dss_shared_latch_t *shared_latch, uint32 wait_ticks);
uint32 dss_get_delay_clean_task_idx(void);
typedef uint32 (*dss_get_bg_task_idx_func_t)(uint32 idx);
uint32 dss_get_meta_syn_task_idx(uint32 idx);
uint32 dss_get_recycle_meta_task_idx(uint32 idx);
uint32 dss_get_alarm_check_task_idx(void);
void dss_server_session_lock(dss_session_t *session);
void dss_server_session_unlock(dss_session_t *session);
dss_session_t *dss_get_reserv_session(uint32 idx);
uint32 dss_get_dyn_log_task_idx(void);

#ifdef __cplusplus
}
#endif

#endif