summaryrefslogtreecommitdiff
path: root/src/lua_plugin_manage.c
blob: 27e1df36bc4660d5292e88fb23fcfb6f61d012fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*************************************************************************
    > File Name: lua_plugin_manage.c
    > Author:
    > Created Time: 2024-08
    > Encoding : UTF-8
 ************************************************************************/

/*************************************************************************
 * version
 * [ v0.1 ]
 * 08-05
 * 1. 实现函数
 *    void specific_instance_copy;
 *    void specific_instance_destory;
 *    int script_instance_init_byname;
 *    int script_instance_init_byrefid;
 *    void script_instance_clean;
 *    int script_execute; (暂未实现)
 *    int session_plugin_instance_init;
 *    void session_plugin_instance_destory;
 *    int plugin_env_instance_init;
 *    void plugin_env_instance_destory;
 *    int thread_state_instance_init;
 *    void thread_state_instance_destory;
 *    int thread_state_instance_load;
 *    int lua_plugin_manage_config_load;
 *    int lua_plugin_manage_thread_load;
 *
 * 08-06
 * 1. 实现函数
 *    int script_execute;
 *
 * 08-13
 * 1. 修改部分创建列表使用的结构
 * 2. 实现函数
 *    struct lua_plugin * search_plugin_by_id
 *    struct lua_message_mq * search_message_mq_by_id
 ************************************************************************/
#include "lua_plugin_manage_internal.h"

#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct lua_plugin_manage_schema *global_schema = NULL;

#if 0
void lua_message_mq_destory(void * elt)
{
    if (!elt) return;
    struct lua_message_mq * mq = (struct lua_message_mq *)elt;
    if ( mq->topic_name )
        free(mq->topic_name);
    return;
}
#endif
static UT_icd lua_message_mq_icd = {sizeof(struct lua_message_mq), NULL, NULL, NULL};

void lua_plugin_destory(void *elt)
{
    if (!elt)
        return;
    struct lua_plugin *plugin = (struct lua_plugin *)elt;
    if (plugin->sub_topic_array)
        utarray_free(plugin->sub_topic_array);
    return;
}
static UT_icd lua_plugin_icd = {sizeof(struct lua_plugin), NULL, NULL, lua_plugin_destory};

/*
 * Function: search_plugin_by_id
 * Input: | int | plugin_id | 需要查找plugin数据的id
 * Output:
 * Return: 查找得到的plugin结构
 * Description: 在global schema中根据plugin_id查找一个plugin
 */
struct lua_plugin *search_plugin_by_id(int plugin_id)
{
    for (int i = 0; i < global_schema->model_count; ++i)
    {
        struct lua_model *model = &global_schema->model[i];

        struct lua_plugin *plugin = NULL;
        while ((plugin = utarray_next(model->plugin_array, plugin)))
        {
            if (plugin->plugin_id == plugin_id)
                return plugin;
            else if (plugin->plugin_id > plugin_id)
                return NULL;
        }
    }
    return NULL;
}

/*
 * Function: search_message_mq_by_id
 * Input: | int | topic_id | 需要查找的topic_id
 * Output:
 * Return: 查找得到的message_mq结构
 * Description: 在global schema中根据topic_id查找一个message_mq
 */
struct lua_message_mq *search_message_mq_by_id(int topic_id)
{
    struct lua_message_mq *mq = NULL;
    while ((mq = utarray_next(global_schema->message_mq_array, mq)))
    {
        if (mq->topic_id == topic_id)
            return mq;
    }
    return NULL;
}

/*
 * Function: thread_state_init
 * Input: | int | thread_id | 创建状态机的线程号
 * Output:
 * Return: 创建完成的状态机实例指针
 * Description: 创建一个lua的状态机实例, 在每个线程中创建一个
 */
lua_State *thread_state_init(int thread_id)
{
    lua_State *new_state = luaL_newstate();
    if (__glibc_unlikely(!new_state))
        return NULL;

    luaL_openlibs(new_state);
    /* 绑定所有注册函数 */
    lua_cbinding_functions(new_state);
    /* 绑定所有全局变量 */
    lua_cbinding_datas(new_state);

    lua_pushinteger(new_state, thread_id);
    lua_setglobal(new_state, LUA_STATE_THREAD_ID_KEY);

    lua_settop(new_state, 0);

    return new_state;
}

/*
 * Function: thread_state_load_specific
 * Input: | lua_State *                  | state    | 进行初始化的状态机
 *        | struct lua_model *           | model    | 需要加载的模块, 模块数量与具体配置实例的数量一致
 *        | struct lua_config_specific * | specific | 加载的配置
 * Output:
 * Return: enum LUA_PLUGIN_RETURN
 * Description: 在状态机中加载一个具体配置, 并将加载完成后生成的引用值保存在model中, 加载过程中会使用model进行校验
 */
int thread_state_load_specific(
    lua_State *state,
    struct lua_model *model,
    struct lua_config_specific *specific)
{
    if (__glibc_unlikely(!state || !model || !specific))
        return PARAM_ERR;
    if (__glibc_unlikely(!specific->config_specific_file || !specific->config_specific_load_func || !specific->config_specific_unload_func))
        return PARAM_ERR;

    /* 加载lua文件 */
    if (access(specific->config_specific_file, F_OK))
        return STATE_LOAD_FILE_ERR;
    if (luaL_loadfile(state, specific->config_specific_file))
        return STATE_LOAD_FILE_ERR;
    if (lua_pcall(state, 0, 0, 0))
    {
        LOGERROR("load specific failed, file is %s, err is %s", specific->config_specific_file, lua_tostring(state, -1));
        lua_settop(state, 0);
        return STATE_LOAD_FILE_ERR;
    }
    lua_settop(state, 0);

    /* 创建plugin_env */
    lua_newtable(state);
    int env_ref_id = luaL_ref(state, LUA_REGISTRYINDEX);
    if (env_ref_id == LUA_REFNIL)
        return STATE_CREATE_ENV_FAIL;
    if ((model->model_mark & MODEL_MARK_INIT_DONE) && (model->private_env_ref != env_ref_id))
    {
        LOGERROR("load new model, create plugin env id is not same, %d, %d", model->private_env_ref, env_ref_id);
        return STATE_CREATE_ENV_FAIL;
    }

    /* 加载load函数 */
    lua_getglobal(state, specific->config_specific_load_func);
    if (lua_type(state, -1) != LUA_TFUNCTION)
    {
        luaL_unref(state, LUA_REGISTRYINDEX, env_ref_id);
        lua_settop(state, 0);
        return STATE_GET_LOAD_FAIL;
    }
    int load_ref_id = luaL_ref(state, LUA_REGISTRYINDEX);
    if (load_ref_id == LUA_REFNIL)
    {
        luaL_unref(state, LUA_REGISTRYINDEX, env_ref_id);
        lua_settop(state, 0);
        return STATE_GET_LOAD_FAIL;
    }
    if ((model->model_mark & MODEL_MARK_INIT_DONE) && (model->load_ref != load_ref_id))
    {
        LOGERROR("load new model, load ref id is not same, %d, %d", model->load_ref, load_ref_id);
        return STATE_GET_LOAD_FAIL;
    }

    /* 加载unload函数 */
    lua_getglobal(state, specific->config_specific_unload_func);
    if (lua_type(state, -1) != LUA_TFUNCTION)
    {
        luaL_unref(state, LUA_REGISTRYINDEX, env_ref_id);
        luaL_unref(state, LUA_REGISTRYINDEX, load_ref_id);
        lua_settop(state, 0);
        return STATE_GET_UNLOAD_FAIL;
    }
    int unload_ref_id = luaL_ref(state, LUA_REGISTRYINDEX);
    if (unload_ref_id == LUA_REFNIL)
    {
        luaL_unref(state, LUA_REGISTRYINDEX, env_ref_id);
        luaL_unref(state, LUA_REGISTRYINDEX, load_ref_id);
        lua_settop(state, 0);
        return STATE_GET_UNLOAD_FAIL;
    }
    if ((model->model_mark & MODEL_MARK_INIT_DONE) && (model->unload_ref != unload_ref_id))
    {
        LOGERROR("load new model, unload ref id is not same, %d, %d", model->unload_ref, unload_ref_id);
        return STATE_GET_UNLOAD_FAIL;
    }

    /* 首次加载时完成model初始化 */
    if (!(model->model_mark & MODEL_MARK_INIT_DONE))
    {
        model->private_env_ref = env_ref_id;
        model->load_ref = load_ref_id;
        model->unload_ref = unload_ref_id;
        model->model_mark |= MODEL_MARK_INIT_DONE;
    }

    lua_settop(state, 0);
    return SUCCESS;
}

/*
 * Function: thread_state_call_load
 * Input: | lua_State *                  | state    | 进行模块加载的状态机
 *        | struct lua_model *           | model    | 需要进行加载的lua模块
 *        | struct lua_config_specific * | specific | 加载模块的配置信息
 *        | struct stellar *             | st       | 与C插件管理保持一致, stellar作为参数传入
 * Output:
 * Return: enum LUA_PLUGIN_RETURN
 * Description: 在状态机中调用一个模块的load函数, 在状态机中完成该模块的加载
 */
int thread_state_call_load(
    lua_State *state,
    struct lua_model *model,
    struct lua_config_specific *specific,
    struct stellar *st)
{
    if (__glibc_unlikely(!state || !model || !specific || !st))
        return PARAM_ERR;

    if ((!(model->model_mark & MODEL_MARK_LOAD_DONE)) && (!model->plugin_array))
    {
        utarray_new(model->plugin_array, &lua_plugin_icd);
        model->model_mark |= MODEL_MARK_LOAD_DONE;
    }

    lua_rawgeti(state, LUA_REGISTRYINDEX, model->private_env_ref);
    lua_pushlightuserdata(state, model);
    /* 在该状态机对应模块的plugin_env中创建该结构对应的指针 */
    lua_setfield(state, -2, LUA_PLUGIN_ENV_DEFAULT_KEY);
    lua_settop(state, 0);
    luaL_loadfile(state, specific->config_specific_file);
    lua_pcall(state, 0, 0, 0);

    struct lua_cdata params[2];
    params[0].cdata_type = DATATYPE_POINTER;
    params[0].cdata_pointer = st;
    params[1].cdata_type = DATATYPE_TABLE;
    params[1].cdata_table = model->private_env_ref;

    return lua_chunk_execute(state, model->load_ref, 2, params, 0, NULL);
}

/*
 * Function: thread_state_call_unload
 * Input: | lua_State *        | 卸载模块的状态机
 *        | struct lua_model * | 需要卸载的模块
 * Output:
 * Return: enum LUA_PLUGIN_RETURN
 * Description: 在一个状态机中对某模块调用其卸载unload函数
 */
int thread_state_call_unload(
    lua_State *state,
    struct lua_model *model)
{
    if (__glibc_unlikely(!state || !model))
        return PARAM_ERR;

    struct lua_cdata param;
    param.cdata_type = DATATYPE_TABLE;
    param.cdata_table = model->private_env_ref;

    return lua_chunk_execute(state, model->unload_ref, 1, &param, 0, NULL);
}

/*
 * Function:
 * Input:
 * Output:
 * Return:
 * Description:
 */
struct lua_plugin_manage_schema *lua_plugin_manage_init(
    struct stellar *st,
    int specific_count,
    struct lua_config_specific *specific)
{
    if (__glibc_unlikely(!st))
        return NULL;
    if (__glibc_unlikely((specific_count && !specific) || (specific_count < 0)))
        return NULL;

    struct lua_plugin_manage_schema *new_schema = (struct lua_plugin_manage_schema *)calloc(1, sizeof(struct lua_plugin_manage_schema));
    if (__glibc_unlikely(!new_schema))
        return NULL;
    memset(new_schema, 0, sizeof(struct lua_plugin_manage_schema));
    global_schema = new_schema;

    new_schema->st = st;
    /* 可能运行过程中创建新的topic, 此处进行初始化 */
    new_schema->mq_count = 0;
    utarray_new(new_schema->message_mq_array, &lua_message_mq_icd);

    int thread_count = stellar_get_worker_thread_num(st);
    new_schema->state_count = thread_count;
    /* 为后续待创建的lua_State预分配内存 */
    new_schema->thread_state = (lua_State **)calloc(thread_count, sizeof(lua_State *));
    if (__glibc_unlikely(!new_schema->thread_state))
    {
        lua_plugin_manage_exit(new_schema);
        return NULL;
    }
    memset(new_schema->thread_state, 0, thread_count * sizeof(lua_State *));
    /* 为将要加载的模块预分配内存 */
    new_schema->model_count = specific_count;
    /* 如果没有配置, 也应该算是创建成功 */
    if (specific_count == 0)
        return new_schema;
    new_schema->model = (struct lua_model *)calloc(specific_count, sizeof(struct lua_model));
    if (__glibc_unlikely(!new_schema->model))
    {
        lua_plugin_manage_exit(new_schema);
        return NULL;
    }
    memset(new_schema->model, 0, specific_count * sizeof(struct lua_model));

    /* 依次初始化每个状态机 */
    for (int thread_index = 0; thread_index < thread_count; ++thread_index)
    {
        /* 创建状态机 */
        new_schema->thread_state[thread_index] = thread_state_init(thread_index);
        if (__glibc_unlikely(!new_schema->thread_state[thread_index]))
        {
            lua_plugin_manage_exit(new_schema);
            return NULL;
        }

        for (int spec_index = 0; spec_index < specific_count; ++spec_index)
        {
            /* 在状态机中加载specific中的配置 */
            int load_ret = thread_state_load_specific(new_schema->thread_state[thread_index],
                                                      &new_schema->model[spec_index],
                                                      &specific[spec_index]);
            if (load_ret)
            {
                LOGERROR("state load specific failed, ret is %d", load_ret);
                lua_plugin_manage_exit(new_schema);
                return NULL;
            }

            /* 调用该模块中的load函数 */
            int call_load_ret = thread_state_call_load(new_schema->thread_state[thread_index],
                                                       &new_schema->model[spec_index],
                                                       &specific[spec_index],
                                                       st);
            if (call_load_ret)
            {
                LOGERROR("call state load function failed, ret is %d", call_load_ret);
                lua_plugin_manage_exit(new_schema);
                return NULL;
            }
        }
    }
#ifdef LUAPLUGIN_BASIC_UNITTEST
    debug_lua_plugin_manage_schema(new_schema);
#endif

    /* 初始化全局状态统计 */
    global_plugin_statistics = (struct lua_plugin_statistics *)calloc((thread_count * global_max_plugin_id), sizeof(struct lua_plugin_statistics));
    if (!global_plugin_statistics)
    {
        LOGERROR("create plugin statistics failed");
        lua_plugin_manage_exit(new_schema);
        return NULL;
    }

    return new_schema;
}

/*
 * Function:
 * Input:
 * Output:
 * Return:
 * Description:
 */
int lua_plugin_manage_load_one_specific(
    struct lua_plugin_manage_schema *schema,
    struct lua_config_specific *specific)
{
    if (__glibc_unlikely(!schema || !specific))
        return -1;

    if (schema->model)
    {
        schema->model = (struct lua_model *)realloc(schema->model, (schema->model_count + 1) * sizeof(struct lua_model));
        schema->model_count += 1;
    }

    for (int thread_index = 0; thread_index < schema->state_count; ++thread_index)
    {
        /* 在状态机中加载specific中的配置 */
        int load_ret = thread_state_load_specific(schema->thread_state[thread_index],
                                                  &schema->model[schema->model_count - 1],
                                                  specific);
        if (load_ret)
        {
            LOGERROR("state load specific failed, ret is %d", load_ret);
            return -1;
        }

        /* 调用该模块中的load函数 */
        int call_load_ret = thread_state_call_load(schema->thread_state[thread_index],
                                                   &schema->model[schema->model_count - 1],
                                                   specific,
                                                   schema->st);
        if (call_load_ret)
        {
            LOGERROR("call state load function failed, ret is %d", call_load_ret);
            return -1;
        }
    }

    if (global_plugin_statistics)
        free(global_plugin_statistics);
    /* 初始化全局状态统计 */
    global_plugin_statistics = (struct lua_plugin_statistics *)calloc((schema->state_count * global_max_plugin_id), sizeof(struct lua_plugin_statistics));
    if (!global_plugin_statistics)
    {
        LOGERROR("create plugin statistics failed");
        return -1;
    }

    return 0;
}

/*
 * Function:
 * Input:
 * Output:
 * Return:
 * Description:
 */
void lua_plugin_manage_exit(struct lua_plugin_manage_schema *lua_plug_mgr)
{
    if (__glibc_unlikely(!lua_plug_mgr))
        return;

    /* 释放所有的状态机 */
    for (int state_index = 0; state_index < lua_plug_mgr->state_count; ++state_index)
    {
        for (int model_index = 0; model_index < lua_plug_mgr->model_count; ++model_index)
        {
            /* 在状态机中对每一个模块调用对应的卸载函数 */
            int call_unload_ret = thread_state_call_unload(lua_plug_mgr->thread_state[state_index],
                                                           &lua_plug_mgr->model[model_index]);
            if (call_unload_ret)
            {
                LOGERROR("call state unload function failed, ret is %d", call_unload_ret);
            }
        }
        /* 销毁状态机 */
        lua_close(lua_plug_mgr->thread_state[state_index]);
    }
    free(lua_plug_mgr->thread_state);

    /* 释放所有加载的模块 */
    for (int model_index = 0; model_index < lua_plug_mgr->model_count; ++model_index)
    {
        utarray_free(lua_plug_mgr->model[model_index].plugin_array);
    }
    free(lua_plug_mgr->model);

    if (lua_plug_mgr->message_mq_array)
        utarray_free(lua_plug_mgr->message_mq_array);

    free(lua_plug_mgr);
    return;
}

/*
 * Function:
 * Input:
 * Output:
 * Return:
 * Description: 获取某插件的运行成功及失败次数
 */
void lua_plugin_get_statistics(int plugin_id, int thread_id, int *new_success, int *new_fail, int *free_success, int *free_fail)
{
    int statistic_id = thread_id * global_max_plugin_id + plugin_id;
    if (new_success)
        *new_success = global_plugin_statistics[statistic_id].new_success_count;
    if (new_fail)
        *new_fail = global_plugin_statistics[statistic_id].new_failed_count;
    if (free_success)
        *free_success = global_plugin_statistics[statistic_id].free_success_count;
    if (free_fail)
        *free_fail = global_plugin_statistics[statistic_id].free_failed_count;
    return;
}

#ifdef LUAPLUGIN_BASIC_UNITTEST
void debug_lua_state_stack(lua_State *state, int mod, const char *message)
{
    int stackcount = lua_gettop(state);
    /* nothing in stack */
    if (!stackcount)
    {
        printf("debug stack, but stack is empty");
        return;
    }

    printf("\n***** begin to debug one lua stack *****\n");
    if (message)
        printf("debug here: %s\n", message);
    int i = stackcount;
    /* print variables one by one */
    for (; i > 0; --i)
    {
        /* adjust variables according to mod */
        if (mod)
            i = 0 - i;
        int type = lua_type(state, i);
        printf("stack[%d]: ", i);
        switch (type)
        {
        case LUA_TBOOLEAN:
            printf(lua_toboolean(state, i) ? "true\n" : "false\n");
            break;
        case LUA_TNUMBER:
            printf("%g\n", lua_tonumber(state, i));
            break;
        case LUA_TSTRING:
            printf("%s\n", lua_tostring(state, i));
            break;
        default:
            printf("this is not normal type, type is: %d[%s]\n", type, lua_typename(state, type));
            break;
        }
        /* adjust variables according to mod */
        if (mod)
            i = 0 - i;
    }
    printf("***** end of debug one lua stack *****\n\n");
}

void debug_lua_plugin_manage_schema(struct lua_plugin_manage_schema *schema)
{
    printf("\n***** begin to debug one lua schema *****\n");
    printf("schema.st is %p\n", schema->st);
    printf("schema state count is %d\n", schema->state_count);
    for (int i = 0; i < schema->state_count; ++i)
    {
        printf("schema state[%d]pointer is %p\n", i, schema->thread_state[i]);
    }
    printf("schema model count is %d\n", schema->model_count);
    for (int i = 0; i < schema->model_count; ++i)
    {
        printf("debug model[%d]\n", i);
        printf("array %p, load %d, unload %d, env %d, mark %04x, count %04x\n",
               schema->model[i].plugin_array, schema->model[i].load_ref, schema->model[i].unload_ref,
               schema->model[i].private_env_ref, schema->model[i].model_mark, schema->model[i].plugin_count);
        struct lua_plugin *plugin = NULL;
        while ((plugin = utarray_next(schema->model[i].plugin_array, plugin)))
        {
            printf("%d, %d, %d\n", plugin->plugin_id, plugin->ctx_new_ref, plugin->ctx_free_ref);
        }
    }

    printf("***** end of debug one lua schema *****\n\n");
}
#endif