| 4757 | } |
| 4758 | |
| 4759 | static int register_queue_with_berkdb_and_master(Lua L, const char *type) |
| 4760 | { |
| 4761 | SP sp = getsp(L); |
| 4762 | struct sqlclntstate *clnt = sp->clnt; |
| 4763 | |
| 4764 | if (sp->consumer) { |
| 4765 | return luaL_error(L, "%s:%s already registered\n", sp->consumer->type, sp->spname); |
| 4766 | } |
| 4767 | if (sp->parent != sp) { |
| 4768 | return luaL_error(L, "attempt to run consumer in child thread"); |
| 4769 | } |
| 4770 | if (clnt->dbtran.mode != TRANLEVEL_SOSQL) { |
| 4771 | return luaL_error(L, "%s is only supported under default transaction mode", type); |
| 4772 | } |
| 4773 | |
| 4774 | dbconsumer_t *consumer; |
| 4775 | size_t sz = dbconsumer_sz(sp->spname); |
| 4776 | new_lua_t_sz(L, consumer, DBTYPES_DBCONSUMER, sz); |
| 4777 | |
| 4778 | sp->consumer = consumer; |
| 4779 | consumer->type = type; |
| 4780 | consumer->emit_timeoutms = 60000; /* emit times-out after 1 min */ |
| 4781 | consumer->osql_max_trans = clnt->osql_max_trans; |
| 4782 | |
| 4783 | if (lua_gettop(L) == 2) { |
| 4784 | lua_insert(L, 1); /* move dbconsumer to bottom of stack */ |
| 4785 | dbconsumer_getargs(L, consumer); |
| 4786 | } |
| 4787 | |
| 4788 | /* register with master */ |
| 4789 | trigger_reg_t *info = &consumer->info; |
| 4790 | info->elect_cookie = ATOMIC_LOAD32(gbl_master_changes); |
| 4791 | info->trigger_cookie = get_id(thedb->bdb_env); |
| 4792 | info->spname_len = strlen(sp->spname); |
| 4793 | memcpy(info->spname, sp->spname, info->spname_len + 1); |
| 4794 | int hostname_len = strlen(gbl_myhostname); |
| 4795 | memcpy(trigger_hostname(info), gbl_myhostname, hostname_len + 1); |
| 4796 | ctrace("%s:%s %016" PRIx64 " register req\n", type, info->spname, info->trigger_cookie); |
| 4797 | int rc = luabb_trigger_register(L, info, consumer->register_timeoutms); |
| 4798 | if (rc != CDB2_TRIG_REQ_SUCCESS) { |
| 4799 | ctrace("%s:%s %016" PRIx64 " register failed rc:%d\n", type, info->spname, info->trigger_cookie, rc); |
| 4800 | force_unregister(L, info); |
| 4801 | if (rc == -2) { /* register timeout */ |
| 4802 | sp->consumer = NULL; |
| 4803 | return 0; |
| 4804 | } |
| 4805 | return luaL_error(L, sp->error); |
| 4806 | } |
| 4807 | ctrace("%s:%s %016" PRIx64 " register success\n", type, info->spname, info->trigger_cookie); |
| 4808 | |
| 4809 | init_fake_ireq(thedb, &consumer->iq); |
| 4810 | consumer->iq.usedb = find_and_lock_queue_table(L); |
| 4811 | |
| 4812 | /* register with berkdb */ |
| 4813 | if (bdb_trigger_subscribe(consumer->iq.usedb->handle, &consumer->cond, &consumer->lock, &consumer->status) != 0) { |
| 4814 | return luaL_error(L, "bdb_trigger_subscribe failed for %s:%s", type, sp->spname); |
| 4815 | } |
| 4816 |
no test coverage detected