%%%------------------------------------------------------------------- %%% @author aresei %%% @copyright (C) 2023, %%% @doc %%% %%% @end %%% Created : 06. 7月 2023 12:02 %%%------------------------------------------------------------------- -module(endpoint_mysql). -include("endpoint.hrl"). -behaviour(gen_server). %% API -export([start_link/3]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %% 消息重发间隔 -define(RETRY_INTERVAL, 5000). -define(DISCONNECTED, disconnected). -define(CONNECTED, connected). -record(state, { endpoint :: #endpoint{}, buffer :: endpoint_buffer:buffer(), pool_pid :: undefined | pid(), status = ?DISCONNECTED }). %%%=================================================================== %%% API %%%=================================================================== %% @doc Creates a gen_statem process which calls Module:init/1 to %% initialize. To ensure a synchronized start-up procedure, this %% function does not return until Module:init/1 has returned. start_link(LocalName, AliasName, Endpoint = #endpoint{}) when is_atom(LocalName), is_atom(AliasName) -> gen_statem:start_link({local, LocalName}, ?MODULE, [AliasName, Endpoint], []). %%%=================================================================== %%% gen_statem callbacks %%%=================================================================== %% @private %% @doc Whenever a gen_statem is started using gen_statem:start/[3,4] or %% gen_statem:start_link/[3,4], this function is called by the new %% process to initialize. init([AliasName, Endpoint]) -> iot_name_server:register(AliasName, self()), erlang:process_flag(trap_exit, true), %% 创建转发器, 避免阻塞当前进程的创建,因此采用了延时初始化的机制 erlang:start_timer(0, self(), create_postman), %% 初始化存储 Buffer = endpoint_buffer:new(Endpoint, 10), {ok, #state{endpoint = Endpoint, buffer = Buffer, status = ?DISCONNECTED}}. %% @private %% @doc Handling call messages -spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()}, State :: #state{}) -> {reply, Reply :: term(), NewState :: #state{}} | {reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} | {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} | {stop, Reason :: term(), NewState :: #state{}}). handle_call(get_stat, _From, State = #state{buffer = Buffer}) -> Stat = endpoint_buffer:stat(Buffer), {reply, {ok, Stat}, State}. %% @private %% @doc Handling cast messages -spec(handle_cast(Request :: term(), State :: #state{}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). handle_cast({forward, ServiceId, Format, Metric}, State = #state{buffer = Buffer}) -> NBuffer = endpoint_buffer:append({ServiceId, Format, Metric}, Buffer), {noreply, State#state{buffer = NBuffer}}; handle_cast(cleanup, State = #state{buffer = Buffer}) -> endpoint_buffer:cleanup(Buffer), {noreply, State}. %% @private %% @doc Handling all non call/cast messages -spec(handle_info(Info :: timeout() | term(), State :: #state{}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). handle_info({timeout, _, create_postman}, State = #state{status = ?DISCONNECTED, buffer = Buffer, endpoint = #endpoint{title = Title, config = #mysql_endpoint{host = Host, port = Port, username = Username, password = Password, database = Database}}}) -> lager:debug("[iot_endpoint] endpoint: ~p, create postman", [Title]), WorkerArgs = [ {host, binary_to_list(Host)}, {port, Port}, {user, binary_to_list(Username)}, {password, binary_to_list(Password)}, {keep_alive, true}, {database, binary_to_list(Database)}, {queries, [<<"set names utf8">>]} ], %% 启动工作的线程池 PoolSize = 5, case poolboy:start_link([{size, PoolSize}, {max_overflow, PoolSize}, {worker_module, mysql}], WorkerArgs) of {ok, PoolPid} -> NBuffer = endpoint_buffer:trigger_n(Buffer), {noreply, State#state{pool_pid = PoolPid, buffer = NBuffer, status = ?CONNECTED}}; ignore -> retry_connect(), {noreply, State}; {error, Reason} -> lager:warning("[mqtt_postman] start connect pool, get error: ~p", [Reason]), retry_connect(), {noreply, State} end; %% 离线时,忽略数据发送逻辑 handle_info({next_data, _Id, _Tuple}, State = #state{status = ?DISCONNECTED}) -> {noreply, State}; %% 发送数据到mqtt服务器 handle_info({next_data, Id, {ServiceId, Metric}}, State = #state{status = ?CONNECTED, pool_pid = PoolPid, buffer = Buffer, endpoint = #endpoint{title = Title, config = #mysql_endpoint{table_name = Table, fields_map = FieldsMap}}}) -> case insert_sql(Table, ServiceId, FieldsMap, Metric) of {ok, InsertSql, Values} -> case poolboy:transaction(PoolPid, fun(ConnPid) -> mysql:query(ConnPid, InsertSql, Values) end) of ok -> NBuffer = endpoint_buffer:ack(Id, Buffer), {noreply, State#state{buffer = NBuffer}}; Error -> lager:warning("[endpoint_mysql] endpoint: ~p, insert mysql get error: ~p", [Title, Error]), {noreply, State} end; error -> lager:debug("[endpoint_mysql] endpoint: ~p, make sql error", [Title]), {noreply, State} end; %% postman进程挂掉时,重新建立新的 handle_info({'EXIT', PoolPid, Reason}, State = #state{endpoint = #endpoint{title = Title}, pool_pid = PoolPid}) -> lager:warning("[enpoint_mqtt] endpoint: ~p, conn pid exit with reason: ~p", [Title, Reason]), retry_connect(), {noreply, disconnected, State#state{pool_pid = undefined, status = ?DISCONNECTED}}; handle_info(Info, State = #state{status = Status}) -> lager:warning("[iot_endpoint] unknown message: ~p, status: ~p", [Info, Status]), {noreply, State}. %% @private %% @doc This function is called by a gen_server when it is about to %% terminate. It should be the opposite of Module:init/1 and do any %% necessary cleaning up. When it returns, the gen_server terminates %% with Reason. The return value is ignored. -spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()), State :: #state{}) -> term()). terminate(Reason, #state{endpoint = #endpoint{title = Title}, buffer = Buffer}) -> lager:debug("[iot_endpoint] endpoint: ~p, terminate with reason: ~p", [Title, Reason]), endpoint_buffer:cleanup(Buffer), ok. %% @private %% @doc Convert process state when code is changed -spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{}, Extra :: term()) -> {ok, NewState :: #state{}} | {error, Reason :: term()}). code_change(_OldVsn, State = #state{}, _Extra) -> {ok, State}. %%%=================================================================== %%% Internal functions %%%=================================================================== retry_connect() -> erlang:start_timer(?RETRY_INTERVAL, self(), create_postman). -spec insert_sql(Table :: binary(), ServiceId :: binary(), FieldsMap :: map(), Metric :: binary()) -> error | {ok, Sql :: binary(), Values :: list()}. insert_sql(Table, ServiceId, FieldsMap, Metric) when is_binary(Table), is_binary(ServiceId), is_binary(Metric) -> case line_format:parse(Metric) of error -> error; {ok, #{<<"measurement">> := Measurement, <<"tags">> := Tags, <<"fields">> := Fields, <<"timestamp">> := Timestamp}} -> Map = maps:merge(Tags, Fields), NMap = Map#{<<"measurement">> => Measurement, <<"timestamp">> => Timestamp}, TableFields = lists:flatmap(fun({TableField, F}) -> case maps:find(F, NMap) of error -> []; {ok, Val} -> [{TableField, Val}] end end, maps:to_list(FieldsMap)), {Keys, Values} = kvs(TableFields), FieldSql = iolist_to_binary(lists:join(<<", ">>, Keys)), Placeholders = lists:duplicate(length(Keys), <<"?">>), ValuesPlaceholder = iolist_to_binary(lists:join(<<", ">>, Placeholders)), {ok, <<"INSERT INTO ", Table/binary, "(", FieldSql/binary, ") VALUES(", ValuesPlaceholder/binary, ")">>, Values} end. -spec kvs(Fields :: list()) -> {Keys :: list(), Values :: list()}. kvs(Fields) when is_list(Fields) -> {Keys0, Values0} = lists:foldl(fun({K, V}, {Acc0, Acc1}) -> {[K|Acc0], [V|Acc1]} end, {[], []}, Fields), {lists:reverse(Keys0), lists:reverse(Values0)}.