Skip to content

Commit 81db712

Browse files
authored
Merge pull request #153 from blackav/feature/session_cache
feature/session cache implementation
2 parents 8caf3ac + cea49b2 commit 81db712

File tree

14 files changed

+1687
-172
lines changed

14 files changed

+1687
-172
lines changed

bin/ej-contests.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "ejudge/sha256utils.h"
4040
#include "ejudge/metrics_contest.h"
4141
#include "ejudge/teamdb.h"
42+
#include "ejudge/session_cache.h"
4243

4344
#include "ejudge/xalloc.h"
4445
#include "ejudge/osdeps.h"
@@ -95,6 +96,9 @@ unsigned char *ul_login;
9596
struct session_info *session_first, *session_last;
9697
//time_t server_start_time;
9798

99+
// global session cache
100+
extern struct id_cache main_id_cache;
101+
98102
// plugin information
99103
struct nsdb_loaded_plugin
100104
{
@@ -1046,6 +1050,7 @@ main(int argc, char *argv[])
10461050
return 1;
10471051
}
10481052

1053+
idc_init(&main_id_cache);
10491054
if (!(state = nsf_init(&params, 0, server_start_time))) return 1;
10501055
if (nsf_prepare(state) < 0) return 1;
10511056
nsf_main_loop(state);

files.make

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- Makefile -*-
22

3-
# Copyright (C) 2002-2022 Alexander Chernov <[email protected]> */
3+
# Copyright (C) 2002-2023 Alexander Chernov <[email protected]> */
44

55
# This program is free software; you can redistribute it and/or
66
# modify it under the terms of the GNU Lesser General Public
@@ -243,6 +243,7 @@ COMMON_CFILES=\
243243
lib/serve_2.c\
244244
lib/serve_state.c\
245245
lib/session.c\
246+
lib/session_cache.c\
246247
lib/sformat.c\
247248
lib/shellcfg_parse.c\
248249
lib/standings.c\
@@ -639,6 +640,7 @@ HFILES=\
639640
./include/ejudge/run_packet_priv.h\
640641
./include/ejudge/server_framework.h\
641642
./include/ejudge/serve_state.h\
643+
./include/ejudge/session_cache.h\
642644
./include/ejudge/sformat.h\
643645
./include/ejudge/shellcfg_parse.h\
644646
./include/ejudge/sock_op.h\

include/ejudge/http_request.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct ejudge_cfg;
3131
struct sid_state;
3232
struct userlist_clnt;
3333
struct cJSON;
34+
struct new_session_info;
3435

3536
struct http_request_info
3637
{
@@ -86,7 +87,8 @@ struct http_request_info
8687
// super-serve uses that
8788
unsigned char *html_name; // used by super-serve
8889
const unsigned char *hidden_vars;
89-
struct session_info *session_extra;
90+
struct session_info *session_extra; // TODO: remove in favor of nsi
91+
struct new_session_info *nsi; // cached session data
9092
opcap_t caps;
9193
opcap_t dbcaps;
9294
unsigned char *script_part;

include/ejudge/metrics_contest.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ struct metrics_contest_data
3030
int runs_submitted;
3131
long long total_compile_time_ms;
3232
long long total_testing_time_ms;
33+
long long get_cookie_tsc;
34+
long long get_cookie_count;
35+
long long hit_cookie_tsc;
36+
long long hit_cookie_count;
37+
long long get_key_tsc;
38+
long long get_key_count;
39+
long long hit_key_tsc;
40+
long long hit_key_count;
41+
long long cookie_cache_size;
42+
long long key_cache_size;
3343
};
3444

3545
struct metrics_desc

include/ejudge/session_cache.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* -*- mode: c; c-basic-offset: 4 -*- */
2+
#ifndef __SESSION_CACHE_H__
3+
#define __SESSION_CACHE_H__
4+
5+
/* Copyright (C) 2023 Alexander Chernov <[email protected]> */
6+
7+
/*
8+
* This program is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation; either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*/
18+
19+
#include "ejudge/config.h"
20+
#include "ejudge/ej_types.h"
21+
22+
#include <time.h>
23+
24+
/* session cache to reduce ej-users request ratio */
25+
struct new_session_info
26+
{
27+
ej_cookie_t session_id;
28+
ej_cookie_t client_key;
29+
30+
ej_ip_t origin_ip; // access address
31+
time_t access_time; // time of the last access
32+
time_t refresh_time; // time to refresh the values
33+
time_t expire_time; // cookie expiration time
34+
35+
unsigned char *login;
36+
unsigned char *name;
37+
struct userlist_user *user_info;
38+
39+
int cmd; // command (PRIV_GET_COOKIE or TEAM_GET_COOKIE)
40+
int user_id;
41+
int contest_id;
42+
unsigned int reg_flags;
43+
44+
unsigned char ssl_flag;
45+
signed char locale_id;
46+
unsigned char priv_level;
47+
unsigned char role;
48+
unsigned char is_ws;
49+
unsigned char is_job;
50+
unsigned char team_login;
51+
unsigned char reg_status;
52+
unsigned char passwd_method;
53+
54+
// these fields are from 'session_info'
55+
int user_view_all_runs;
56+
int user_view_all_clars;
57+
int user_viewed_section;
58+
};
59+
60+
struct new_session_cache
61+
{
62+
struct new_session_info *info;
63+
int reserved;
64+
int res_index;
65+
int rehash_threshold;
66+
int used;
67+
};
68+
69+
struct cached_token_info
70+
{
71+
unsigned char token[32];
72+
ej_ip_t origin_ip; // access address
73+
time_t access_time; // time of the last access
74+
time_t expiry_time;
75+
time_t refresh_time;
76+
77+
unsigned char *login;
78+
unsigned char *name;
79+
80+
int cmd;
81+
int user_id;
82+
int contest_id;
83+
unsigned int reg_flags;
84+
85+
unsigned char ssl_flag;
86+
unsigned char role;
87+
unsigned char reg_status;
88+
unsigned char all_contests;
89+
unsigned char used; // 1 if this entry is used
90+
};
91+
92+
struct token_cache
93+
{
94+
struct cached_token_info *info;
95+
int reserved;
96+
int res_index;
97+
int rehash_threshold;
98+
int used;
99+
};
100+
101+
struct id_cache
102+
{
103+
struct new_session_cache s;
104+
struct token_cache t;
105+
};
106+
107+
#ifdef __cplusplus
108+
extern "C" {
109+
#endif
110+
111+
void idc_init(struct id_cache *idc);
112+
113+
struct new_session_info * nsc_insert(struct new_session_cache *nsc, ej_cookie_t session_id, ej_cookie_t client_key);
114+
struct new_session_info * nsc_find(struct new_session_cache *nsc, ej_cookie_t session_id, ej_cookie_t client_key);
115+
int nsc_remove(struct new_session_cache *nsc, ej_cookie_t session_id, ej_cookie_t client_key, struct new_session_info *out);
116+
117+
struct cached_token_info *tc_insert(struct token_cache *tc, const unsigned char *token);
118+
struct cached_token_info *tc_find(struct token_cache *tc, const unsigned char *token);
119+
int tc_remove(struct token_cache *tc, const unsigned char *token, struct cached_token_info *out);
120+
121+
#ifdef __cplusplus
122+
}
123+
#endif
124+
125+
#endif /* __SESSION_CACHE_H__ */

include/ejudge/userlist_clnt.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,24 @@ userlist_clnt_get_cookie(
126126
unsigned char **p_login,
127127
unsigned char **p_name);
128128

129+
struct userlist_ok_login_ok;
130+
struct UserlistGetCookieResult
131+
{
132+
struct userlist_pk_login_ok *data; // to be freed
133+
unsigned char *login; // points somewhere in data
134+
unsigned char *name; // points somewhere in data
135+
};
136+
137+
int
138+
userlist_clnt_get_cookie_2(
139+
struct userlist_clnt *clnt,
140+
int cmd,
141+
const ej_ip_t *origin_ip,
142+
int ssl,
143+
ej_cookie_t cookie,
144+
ej_cookie_t client_key,
145+
struct UserlistGetCookieResult *p_res);
146+
129147
int
130148
userlist_clnt_set_cookie(
131149
struct userlist_clnt *clnt,

0 commit comments

Comments
 (0)