forked from lf-edge/ekuiper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsink.go
More file actions
165 lines (151 loc) · 4.73 KB
/
sink.go
File metadata and controls
165 lines (151 loc) · 4.73 KB
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
// Copyright 2021-2025 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mqtt
import (
"fmt"
"strings"
"github.com/lf-edge/ekuiper/contract/v2/api"
"github.com/lf-edge/ekuiper/v2/internal/conf"
"github.com/lf-edge/ekuiper/v2/internal/pkg/util"
"github.com/lf-edge/ekuiper/v2/internal/topo/node/tracenode"
"github.com/lf-edge/ekuiper/v2/pkg/cast"
"github.com/lf-edge/ekuiper/v2/pkg/connection"
)
// AdConf is the advanced configuration for the mqtt sink
type AdConf struct {
Tpc string `json:"topic"`
Qos byte `json:"qos"`
Retained bool `json:"retained"`
SelId string `json:"connectionSelector"`
Props map[string]string `json:"properties"`
PVersion string `json:"protocolVersion"`
}
type Sink struct {
id string
cw *connection.ConnWrapper
adconf *AdConf
config map[string]interface{}
cli *Connection
}
func (ms *Sink) Provision(ctx api.StreamContext, ps map[string]any) error {
err := ValidateConfig(ps)
if err != nil {
return err
}
adconf := &AdConf{}
err = cast.MapToStruct(ps, adconf)
if err != nil {
return err
}
if adconf.Tpc == "" {
return fmt.Errorf("mqtt sink is missing property topic")
}
if err := validateMQTTSinkTopic(adconf.Tpc); err != nil {
return err
}
if adconf.Qos != 0 && adconf.Qos != 1 && adconf.Qos != 2 {
return fmt.Errorf("invalid qos value %v, the value could be only int 0 or 1 or 2", adconf.Qos)
}
ms.config = ps
ms.adconf = adconf
if adconf.PVersion != "5" && adconf.Props != nil {
ctx.GetLogger().Warnf("Only mqtt v5 supports properties, ignore the properties setting")
}
return nil
}
func (ms *Sink) Connect(ctx api.StreamContext, sch api.StatusChangeHandler) error {
ctx.GetLogger().Infof("Connecting to mqtt server")
var err error
ms.id = fmt.Sprintf("%s-%s-%s-mqtt-sink", ctx.GetRuleId(), ctx.GetOpId(), ms.adconf.Tpc)
ms.cw, err = connection.FetchConnection(ctx, ms.id, "mqtt", ms.config, sch)
if err != nil {
return err
}
if ms.adconf.SelId != "" {
ctx.GetLogger().Infof("action=use_shared_mqtt_connection role=sink connId=%s connectionKey=%s rule=%s topic=%s", ms.cw.ID, ms.adconf.SelId, ctx.GetRuleId(), ms.adconf.Tpc)
}
conn, err := ms.cw.Wait(ctx)
if conn == nil {
return fmt.Errorf("mqtt client not ready: %v", err)
}
c, ok := conn.(*Connection)
if !ok {
return fmt.Errorf("connection %s should be mqtt connection", ms.adconf.SelId)
}
ms.cli = c
conf.Log.Info("mqtt sink client ready")
return err
}
func validateMQTTSinkTopic(topic string) error {
if strings.Contains(topic, "#") || strings.Contains(topic, "+") {
return fmt.Errorf("mqtt sink topic shouldn't contain # or +")
}
return nil
}
func (ms *Sink) Collect(ctx api.StreamContext, item api.RawTuple) error {
tpc := ms.adconf.Tpc
props := ms.adconf.Props
// If tpc supports dynamic props(template), planner will guarantee the result has the parsed dynamic props
if dp, ok := item.(api.HasDynamicProps); ok {
temp, transformed := dp.DynamicProps(tpc)
if transformed {
tpc = temp
}
newProps := make(map[string]string, len(props))
for k, v := range props {
nv, ok := dp.DynamicProps(v)
if ok {
newProps[k] = nv
} else {
newProps[k] = v
}
}
props = newProps
}
traced, _, span := tracenode.TraceInput(ctx, item, fmt.Sprintf("%s_emit", ctx.GetOpId()))
if traced {
defer span.End()
traceID := span.SpanContext().TraceID()
spanID := span.SpanContext().SpanID()
if props == nil {
props = make(map[string]string)
}
props["traceparent"] = tracenode.BuildTraceParentId(traceID, spanID)
}
ctx.GetLogger().Debugf("publishing to topic %s", tpc)
return ms.cli.Publish(ctx, tpc, ms.adconf.Qos, ms.adconf.Retained, item.Raw(), props)
}
func (ms *Sink) Close(ctx api.StreamContext) error {
ctx.GetLogger().Infof("Closing mqtt sink connector, id:%v", ms.id)
if ms.cw != nil {
return connection.DetachConnection(ctx, ms.cw.ID)
}
return nil
}
func (ms *Sink) Ping(ctx api.StreamContext, props map[string]any) error {
cli := &Connection{}
err := cli.Provision(ctx, "test", props)
if err != nil {
return err
}
defer cli.Close(ctx)
return cli.Ping(ctx)
}
func GetSink() api.Sink {
return &Sink{}
}
var (
_ api.BytesCollector = &Sink{}
_ util.PingableConn = &Sink{}
)