-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
emqx_bridge_gcp_pubsub_impl_consumer.erl
425 lines (389 loc) · 14.3 KB
/
emqx_bridge_gcp_pubsub_impl_consumer.erl
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
%%--------------------------------------------------------------------
%% Copyright (c) 2023-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
%%--------------------------------------------------------------------
-module(emqx_bridge_gcp_pubsub_impl_consumer).
-behaviour(emqx_resource).
%% `emqx_resource' API
-export([
callback_mode/0,
query_mode/1,
on_start/2,
on_stop/2,
on_get_status/2,
on_add_channel/4,
on_remove_channel/3,
on_get_channels/1,
on_get_channel_status/3
]).
%% health check API
-export([
mark_as_unhealthy/2,
clear_unhealthy/1,
check_if_unhealthy/1
]).
-include_lib("emqx/include/logger.hrl").
-include_lib("snabbkaffe/include/snabbkaffe.hrl").
-include_lib("emqx_resource/include/emqx_resource.hrl").
-type mqtt_config() :: #{
mqtt_topic := emqx_types:topic(),
qos := emqx_types:qos(),
payload_template := string()
}.
-type connector_config() :: #{
connect_timeout := emqx_schema:duration_ms(),
max_retries := non_neg_integer(),
pool_size := non_neg_integer(),
resource_opts := #{request_ttl := infinity | emqx_schema:duration_ms(), any() => term()},
service_account_json := emqx_bridge_gcp_pubsub_client:service_account_json(),
any() => term()
}.
-type connector_state() :: #{
client := emqx_bridge_gcp_pubsub_client:state(),
installed_sources := #{source_resource_id() => source_state()},
project_id := binary()
}.
-type topic_mapping() :: #{
pubsub_topic := binary(),
mqtt_topic := binary(),
qos := emqx_types:qos(),
payload_template := binary()
}.
-type source_config() :: #{
bridge_name := binary(),
hookpoints := [binary()],
parameters := #{
consumer_workers_per_topic := pos_integer(),
topic_mapping := [topic_mapping(), ...]
},
resource_opts := #{request_ttl := infinity | emqx_schema:duration_ms(), any() => term()},
topic := binary()
}.
-type source_state() :: #{}.
-export_type([mqtt_config/0]).
-define(AUTO_RECONNECT_S, 2).
-define(DEFAULT_FORGET_INTERVAL, timer:seconds(60)).
-define(OPTVAR_UNHEALTHY(INSTANCE_ID), {?MODULE, topic_not_found, INSTANCE_ID}).
-define(TOPIC_MESSAGE,
"GCP PubSub topics are invalid. Please check the logs, check if the "
"topics exist in GCP and if the service account has permissions to use them."
).
-define(PERMISSION_MESSAGE,
"Permission denied while verifying topic existence. Please check that the "
"provided service account has the correct permissions configured."
).
%%-------------------------------------------------------------------------------------------------
%% `emqx_resource' API
%%-------------------------------------------------------------------------------------------------
-spec callback_mode() -> callback_mode().
callback_mode() -> async_if_possible.
-spec query_mode(any()) -> query_mode().
query_mode(_Config) -> no_queries.
-spec on_start(connector_resource_id(), connector_config()) ->
{ok, connector_state()} | {error, term()}.
on_start(ConnectorResId, Config0) ->
Config = maps:update_with(
service_account_json, fun(X) -> emqx_utils_json:decode(X, [return_maps]) end, Config0
),
#{service_account_json := #{<<"project_id">> := ProjectId}} = Config,
case emqx_bridge_gcp_pubsub_client:start(ConnectorResId, Config) of
{ok, Client} ->
ConnectorState = #{
client => Client,
installed_sources => #{},
project_id => ProjectId
},
{ok, ConnectorState};
Error ->
Error
end.
-spec on_stop(resource_id(), connector_state()) -> ok | {error, term()}.
on_stop(ConnectorResId, ConnectorState) ->
?tp(gcp_pubsub_consumer_stop_enter, #{}),
clear_unhealthy(ConnectorState),
ok = stop_consumers(ConnectorState),
emqx_bridge_gcp_pubsub_client:stop(ConnectorResId).
-spec on_get_status(resource_id(), connector_state()) ->
?status_connected | ?status_connecting.
on_get_status(_ConnectorResId, ConnectorState) ->
#{client := Client} = ConnectorState,
get_client_status(Client).
-spec on_add_channel(
connector_resource_id(),
connector_state(),
source_resource_id(),
source_config()
) ->
{ok, connector_state()}.
on_add_channel(ConnectorResId, ConnectorState0, SourceResId, SourceConfig) ->
#{
client := Client,
installed_sources := InstalledSources0,
project_id := ProjectId
} = ConnectorState0,
case start_consumers(ConnectorResId, SourceResId, Client, ProjectId, SourceConfig) of
{ok, SourceState} ->
InstalledSources = InstalledSources0#{SourceResId => SourceState},
ConnectorState = ConnectorState0#{installed_sources := InstalledSources},
{ok, ConnectorState};
Error = {error, _} ->
Error
end.
-spec on_remove_channel(
connector_resource_id(),
connector_state(),
source_resource_id()
) ->
{ok, connector_state()}.
on_remove_channel(_ConnectorResId, ConnectorState0, SourceResId) ->
#{installed_sources := InstalledSources0} = ConnectorState0,
case maps:take(SourceResId, InstalledSources0) of
{SourceState, InstalledSources} ->
stop_consumers1(SourceState),
ok;
error ->
InstalledSources = InstalledSources0
end,
ConnectorState = ConnectorState0#{installed_sources := InstalledSources},
{ok, ConnectorState}.
-spec on_get_channels(connector_resource_id()) ->
[{action_resource_id(), source_config()}].
on_get_channels(ConnectorResId) ->
emqx_bridge_v2:get_channels_for_connector(ConnectorResId).
-spec on_get_channel_status(connector_resource_id(), source_resource_id(), connector_state()) ->
health_check_status().
on_get_channel_status(
_ConnectorResId,
SourceResId,
ConnectorState = #{installed_sources := InstalledSources}
) when is_map_key(SourceResId, InstalledSources) ->
%% We need to check this flag separately because the workers might be gone when we
%% check them.
case check_if_unhealthy(SourceResId) of
{error, topic_not_found} ->
{?status_disconnected, {unhealthy_target, ?TOPIC_MESSAGE}};
{error, permission_denied} ->
{?status_disconnected, {unhealthy_target, ?PERMISSION_MESSAGE}};
{error, bad_credentials} ->
{?status_disconnected, {unhealthy_target, ?PERMISSION_MESSAGE}};
ok ->
#{client := Client} = ConnectorState,
#{SourceResId := #{pool_name := PoolName}} = InstalledSources,
check_workers(PoolName, Client)
end;
on_get_channel_status(_ConnectorResId, _SourceResId, _ConnectorState) ->
?status_disconnected.
%%-------------------------------------------------------------------------------------------------
%% Health check API (signalled by consumer worker)
%%-------------------------------------------------------------------------------------------------
-spec mark_as_unhealthy(
source_resource_id(),
topic_not_found
| permission_denied
| bad_credentials
) -> ok.
mark_as_unhealthy(SourceResId, Reason) ->
optvar:set(?OPTVAR_UNHEALTHY(SourceResId), Reason),
ok.
-spec clear_unhealthy(connector_state()) -> ok.
clear_unhealthy(ConnectorState) ->
#{installed_sources := InstalledSources} = ConnectorState,
maps:foreach(
fun(SourceResId, _SourceState) ->
optvar:unset(?OPTVAR_UNHEALTHY(SourceResId))
end,
InstalledSources
),
?tp(gcp_pubsub_consumer_clear_unhealthy, #{}),
ok.
-spec check_if_unhealthy(source_resource_id()) ->
ok
| {error,
topic_not_found
| permission_denied
| bad_credentials}.
check_if_unhealthy(SourceResId) ->
case optvar:peek(?OPTVAR_UNHEALTHY(SourceResId)) of
{ok, Reason} ->
{error, Reason};
undefined ->
ok
end.
%%-------------------------------------------------------------------------------------------------
%% Internal fns
%%-------------------------------------------------------------------------------------------------
start_consumers(ConnectorResId, SourceResId, Client, ProjectId, SourceConfig) ->
#{
bridge_name := BridgeName,
parameters := ConsumerConfig0,
hookpoints := Hookpoints,
resource_opts := #{request_ttl := RequestTTL}
} = SourceConfig,
ConsumerConfig1 = ensure_topic_mapping(ConsumerConfig0),
TopicMapping = maps:get(topic_mapping, ConsumerConfig1),
ConsumerWorkersPerTopic = maps:get(consumer_workers_per_topic, ConsumerConfig1),
PoolSize = map_size(TopicMapping) * ConsumerWorkersPerTopic,
ConsumerConfig = ConsumerConfig1#{
auto_reconnect => ?AUTO_RECONNECT_S,
bridge_name => BridgeName,
client => Client,
forget_interval => forget_interval(RequestTTL),
hookpoints => Hookpoints,
connector_resource_id => ConnectorResId,
source_resource_id => SourceResId,
pool_size => PoolSize,
project_id => ProjectId,
pull_retry_interval => RequestTTL,
request_ttl => RequestTTL
},
ConsumerOpts = maps:to_list(ConsumerConfig),
ReqOpts = #{request_ttl => RequestTTL},
case validate_pubsub_topics(TopicMapping, Client, ReqOpts) of
ok ->
ok;
{error, not_found} ->
throw(
{unhealthy_target, ?TOPIC_MESSAGE}
);
{error, permission_denied} ->
throw(
{unhealthy_target, ?PERMISSION_MESSAGE}
);
{error, bad_credentials} ->
throw(
{unhealthy_target, ?PERMISSION_MESSAGE}
);
{error, _} ->
%% connection might be down; we'll have to check topic existence during health
%% check, or the workers will kill themselves when they realized there's no
%% topic when upserting their subscription.
ok
end,
case
emqx_resource_pool:start(SourceResId, emqx_bridge_gcp_pubsub_consumer_worker, ConsumerOpts)
of
ok ->
State = #{pool_name => SourceResId},
{ok, State};
{error, Reason} ->
{error, Reason}
end.
stop_consumers(ConnectorState) ->
#{installed_sources := InstalledSources} = ConnectorState,
maps:foreach(
fun(_SourceResId, SourceState) ->
stop_consumers1(SourceState)
end,
InstalledSources
).
stop_consumers1(SourceState) ->
#{pool_name := PoolName} = SourceState,
_ = log_when_error(
fun() ->
ok = emqx_resource_pool:stop(PoolName)
end,
#{
msg => "failed_to_stop_pull_worker_pool",
pool_name => PoolName
}
),
ok.
%% This is to ensure backwards compatibility with the deprectated topic mapping.
ensure_topic_mapping(ConsumerConfig0 = #{topic_mapping := [_ | _]}) ->
%% There is an existing topic mapping: legacy config. We use it and ignore the single
%% pubsub topic so that the bridge keeps working as before.
maps:update_with(topic_mapping, fun convert_topic_mapping/1, ConsumerConfig0);
ensure_topic_mapping(ConsumerConfig0 = #{topic := PubsubTopic}) ->
%% No topic mapping: generate one without MQTT templates.
maps:put(topic_mapping, #{PubsubTopic => #{}}, ConsumerConfig0).
convert_topic_mapping(TopicMappingList) ->
lists:foldl(
fun(Fields, Acc) ->
#{
pubsub_topic := PubSubTopic,
mqtt_topic := MQTTTopic,
qos := QoS,
payload_template := PayloadTemplate0
} = Fields,
PayloadTemplate = emqx_placeholder:preproc_tmpl(PayloadTemplate0),
Acc#{
PubSubTopic => #{
payload_template => PayloadTemplate,
mqtt_topic => MQTTTopic,
qos => QoS
}
}
end,
#{},
TopicMappingList
).
validate_pubsub_topics(TopicMapping, Client, ReqOpts) ->
PubSubTopics = maps:keys(TopicMapping),
do_validate_pubsub_topics(Client, PubSubTopics, ReqOpts).
do_validate_pubsub_topics(Client, [Topic | Rest], ReqOpts) ->
case check_for_topic_existence(Topic, Client, ReqOpts) of
ok ->
do_validate_pubsub_topics(Client, Rest, ReqOpts);
{error, _} = Err ->
Err
end;
do_validate_pubsub_topics(_Client, [], _ReqOpts) ->
%% we already validate that the mapping is not empty in the config schema.
ok.
check_for_topic_existence(Topic, Client, ReqOpts) ->
Res = emqx_bridge_gcp_pubsub_client:get_topic(Topic, Client, ReqOpts),
case Res of
{ok, _} ->
ok;
{error, #{status_code := 404}} ->
{error, not_found};
{error, #{status_code := 403}} ->
{error, permission_denied};
{error, #{status_code := 401}} ->
{error, bad_credentials};
{error, Reason} ->
?tp(warning, "gcp_pubsub_consumer_check_topic_error", #{reason => Reason}),
{error, Reason}
end.
-spec get_client_status(emqx_bridge_gcp_pubsub_client:state()) ->
?status_connected | ?status_connecting.
get_client_status(Client) ->
case emqx_bridge_gcp_pubsub_client:get_status(Client) of
?status_disconnected -> ?status_connecting;
?status_connected -> ?status_connected
end.
-spec check_workers(source_resource_id(), emqx_bridge_gcp_pubsub_client:state()) ->
?status_connected | ?status_connecting.
check_workers(SourceResId, Client) ->
case
emqx_resource_pool:health_check_workers(
SourceResId,
fun emqx_bridge_gcp_pubsub_consumer_worker:health_check/1,
emqx_resource_pool:health_check_timeout(),
#{return_values => true}
)
of
{ok, []} ->
?status_connecting;
{ok, Values} ->
AllOk = lists:all(fun(S) -> S =:= subscription_ok end, Values),
case AllOk of
true ->
get_client_status(Client);
false ->
?status_connecting
end;
{error, _} ->
?status_connecting
end.
log_when_error(Fun, Log) ->
try
Fun()
catch
C:E ->
?SLOG(error, Log#{
exception => C,
reason => E
})
end.
forget_interval(infinity) -> ?DEFAULT_FORGET_INTERVAL;
forget_interval(Timeout) -> 2 * Timeout.