pandaapo commented on code in PR #4599: URL: https://github.com/apache/eventmesh/pull/4599#discussion_r1413405564
########## eventmesh-connectors/eventmesh-connector-lark/src/main/java/org/apache/eventmesh/connector/lark/ConnectRecordExtensionKeys.java: ########## @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.eventmesh.connector.lark; + +/** + * Constants of record extension key. + */ +public interface ConnectRecordExtensionKeys { + + /** + * {@code text} or {@code markdown}, otherwise use {@code text} to replace. + */ + String TEMPLATE_TYPE_4_LARK = "templatetype4lark"; + + /** + * The value format is {@code id,name;id,name;}. <br/> Review Comment: https://github.com/apache/eventmesh/actions/runs/7080866475/job/19270211624 Java 11 does not support such doc code `<br/>`, and `<p/>` should also not. Java 11 不支持这样的doc代码`<br/>` ,`<p/>`应该也不行。 ########## eventmesh-connectors/eventmesh-connector-lark/src/main/resources/sink-config.yml: ########## @@ -17,14 +17,18 @@ pubSubConfig: meshAddress: 127.0.0.1:10000 - subject: TopicTest + subject: TEST-TOPIC-LARK idc: FT env: PRD - group: feishuSink + group: larkSink appId: 5031 - userName: feishuSinkUser - passWord: feishuPassWord -connectorConfig: - connectorName: feishuSink - reciveId: reciveIdValue - reciveType: open_id + userName: larkSinkUser + passWord: larkPassWord +sinkConnectorConfig: + connectorName: larkSink + appId: appId + appSecret: appSecret + receiveIdType: open_id + receiveId: receiveId Review Comment: Thank you. I am somewhat disappointed with my review work in PR #4522. 谢谢。我对自己在PR #4522的review工作感到有点失望。 ########## eventmesh-connectors/eventmesh-connector-lark/src/main/java/org/apache/eventmesh/connector/lark/ConnectRecordExtensionKeys.java: ########## @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.eventmesh.connector.lark; + +/** + * Constants of record extension key. + */ +public interface ConnectRecordExtensionKeys { + + /** + * {@code text} or {@code markdown}, otherwise use {@code text} to replace. + */ + String TEMPLATE_TYPE_4_LARK = "templatetype4lark"; + + /** + * The value format is {@code id,name;id,name;}. <br/> Review Comment: https://github.com/apache/eventmesh/actions/runs/7080866475/job/19270211624 Java 11 does not support such doc code `<br/>`, and `<p/>` should also not. Java 11 不支持这样的doc代码`<br/>` ,`<p/>`应该也不行。 ########## eventmesh-connectors/eventmesh-connector-lark/src/main/resources/sink-config.yml: ########## @@ -17,14 +17,18 @@ pubSubConfig: meshAddress: 127.0.0.1:10000 - subject: TopicTest + subject: TEST-TOPIC-LARK idc: FT env: PRD - group: feishuSink + group: larkSink appId: 5031 - userName: feishuSinkUser - passWord: feishuPassWord -connectorConfig: - connectorName: feishuSink - reciveId: reciveIdValue - reciveType: open_id + userName: larkSinkUser + passWord: larkPassWord +sinkConnectorConfig: + connectorName: larkSink + appId: appId + appSecret: appSecret + receiveIdType: open_id + receiveId: receiveId Review Comment: Thank you. I am somewhat disappointed with my review work in PR #4522. 谢谢。我对自己在PR #4522的review工作感到有点失望。 ########## eventmesh-connectors/eventmesh-connector-lark/src/main/java/org/apache/eventmesh/connector/lark/sink/connector/LarkSinkConnector.java: ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.eventmesh.connector.lark.sink.connector; + +import static org.apache.eventmesh.connector.lark.sink.ImServiceHandler.create; + +import org.apache.eventmesh.connector.lark.sink.ImServiceHandler; +import org.apache.eventmesh.connector.lark.sink.config.LarkSinkConfig; +import org.apache.eventmesh.connector.lark.sink.config.SinkConnectorConfig; +import org.apache.eventmesh.openconnect.api.config.Config; +import org.apache.eventmesh.openconnect.api.connector.ConnectorContext; +import org.apache.eventmesh.openconnect.api.connector.SinkConnectorContext; +import org.apache.eventmesh.openconnect.api.sink.Sink; +import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord; + +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import com.github.rholder.retry.RetryException; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.lark.oapi.Client; +import com.lark.oapi.core.enums.AppType; +import com.lark.oapi.core.request.SelfBuiltTenantAccessTokenReq; +import com.lark.oapi.core.response.TenantAccessTokenResp; + +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +public class LarkSinkConnector implements Sink { + + public static final String TENANT_ACCESS_TOKEN = "tenant_access_token"; + + /** + * replace lark build-in tokenCache + */ + public static final Cache<String, String> AUTH_CACHE = CacheBuilder.newBuilder() + .initialCapacity(12) + .maximumSize(10) + .concurrencyLevel(5) + .expireAfterWrite(30, TimeUnit.MINUTES) + .build(); + + private LarkSinkConfig sinkConfig; + + private ImServiceHandler imServiceHandler; + + private final AtomicBoolean started = new AtomicBoolean(false); + + @Override + public Class<? extends Config> configClass() { + return LarkSinkConfig.class; + } + + @Override + public void init(Config config) { + // Deprecated + } + + @Override + public void init(ConnectorContext connectorContext) { + // init config for lark sink connector + SinkConnectorContext sinkConnectorContext = (SinkConnectorContext) connectorContext; + this.sinkConfig = (LarkSinkConfig) sinkConnectorContext.getSinkConfig(); + + SinkConnectorConfig sinkConnectorConfig = sinkConfig.getSinkConnectorConfig(); + sinkConnectorConfig.validateSinkConfiguration(); + + imServiceHandler = create(sinkConnectorConfig); + } + + @Override + public void start() { + if (!started.compareAndSet(false, true)) { + log.info("LarkSinkConnector has been started."); + } + } + + @Override + public void commit(ConnectRecord record) { + // Sink does not need to implement + } + + @Override + public String name() { + return this.sinkConfig.getSinkConnectorConfig().getConnectorName(); + } + + @Override + public void stop() { + if (!started.compareAndSet(true, false)) { + log.info("LarkSinkConnector has not started yet."); + } + } + + @Override + public void put(List<ConnectRecord> sinkRecords) { + for (ConnectRecord connectRecord : sinkRecords) { + try { + imServiceHandler.sink(connectRecord); + } catch (ExecutionException | RetryException e) { + log.error("Failed to sink event to lark", e); + } + } + } + + @SneakyThrows + public static String getTenantAccessToken(String appId, String appSecret) { Review Comment: > Other services may also need to use the credentials management . If you put ImServiceHandler in, it feels like it is only serving ImServiceHandler. But it's also possible that I'm over-designing it. If it is possible to develop other services that require this credential to further enrich the connector, you can leave a note in this method for other pending services that may require this credential. 如果还有可能开发其他需要该凭证的服务来进一步丰富该Connector,您可以在该方法备注一下可能需要该凭证的其他等待实现的服务。 > Actually, there is one point that puzzles me. If there is only one access credential, why use the Cache collection? I encountered similar doubts in an other connector before https://github.com/apache/eventmesh/pull/4594#discussion_r1412702091, and the developer explained that because credentials will expire and new credential will be constantly obtained in the code https://github.com/apache/eventmesh/pull/4594#discussion_r1412803666. I'm not sure if the credential here will expire. If it do, not only does it require Cache, but also a mechanism for regular updates. 我之前在其他Connector遇到类似的疑惑 https://github.com/apache/eventmesh/pull/4594#discussion_r1412702091 ,开发者解释到因为凭证会过期 https://github.com/apache/eventmesh/pull/4594#discussion_r1412803666 ,并且在代码中会不断获取新的凭证。我不清楚这里的凭证会不会过期,如果会过期,不仅需要Cache,还需要定时更新的机制。 ########## eventmesh-connectors/eventmesh-connector-lark/src/main/java/org/apache/eventmesh/connector/lark/sink/ImServiceHandler.java: ########## @@ -0,0 +1,267 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.eventmesh.connector.lark.sink; + +import static org.apache.eventmesh.connector.lark.sink.connector.LarkSinkConnector.getTenantAccessToken; + +import org.apache.eventmesh.connector.lark.ConnectRecordExtensionKeys; +import org.apache.eventmesh.connector.lark.config.LarkMessageTemplateType; +import org.apache.eventmesh.connector.lark.sink.config.SinkConnectorConfig; +import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord; + +import org.apache.commons.text.StringEscapeUtils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import com.github.rholder.retry.Attempt; +import com.github.rholder.retry.RetryException; +import com.github.rholder.retry.RetryListener; +import com.github.rholder.retry.Retryer; +import com.github.rholder.retry.RetryerBuilder; +import com.github.rholder.retry.StopStrategies; +import com.github.rholder.retry.WaitStrategies; +import com.lark.oapi.Client; +import com.lark.oapi.card.enums.MessageCardHeaderTemplateEnum; +import com.lark.oapi.card.model.MessageCard; +import com.lark.oapi.card.model.MessageCardConfig; +import com.lark.oapi.card.model.MessageCardElement; +import com.lark.oapi.card.model.MessageCardHeader; +import com.lark.oapi.card.model.MessageCardMarkdown; +import com.lark.oapi.card.model.MessageCardPlainText; +import com.lark.oapi.core.httpclient.OkHttpTransport; +import com.lark.oapi.core.request.RequestOptions; +import com.lark.oapi.core.utils.Lists; +import com.lark.oapi.okhttp.OkHttpClient; +import com.lark.oapi.service.im.v1.ImService; +import com.lark.oapi.service.im.v1.enums.MsgTypeEnum; +import com.lark.oapi.service.im.v1.model.CreateMessageReq; +import com.lark.oapi.service.im.v1.model.CreateMessageReqBody; +import com.lark.oapi.service.im.v1.model.CreateMessageResp; +import com.lark.oapi.service.im.v1.model.ext.MessageText; + +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class ImServiceHandler { + + private static final ConcurrentHashMap<ConnectRecord, CreateMessageReq> UN_ACK_REQ = new ConcurrentHashMap<>(); Review Comment: > The original code will re-inject a new UUID every time it is retransmitted, which will lead to repeated consumption by Feishu. Could you explain what incorrect phenomenon or logic may occur when retry, which may lead to "repeated consumption by Feishu"? Because I don't understand the difference between retrying and "repeated consumption by Feishu". 能否说明一下在重试的时候“会导致飞书重复消费”会出现什么不正确的现象或者逻辑?因为我没明白重试和“飞书重复消费”有什么区别。 ########## eventmesh-connectors/eventmesh-connector-lark/src/main/java/org/apache/eventmesh/connector/lark/sink/connector/LarkSinkConnector.java: ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.eventmesh.connector.lark.sink.connector; + +import static org.apache.eventmesh.connector.lark.sink.ImServiceHandler.create; + +import org.apache.eventmesh.connector.lark.sink.ImServiceHandler; +import org.apache.eventmesh.connector.lark.sink.config.LarkSinkConfig; +import org.apache.eventmesh.connector.lark.sink.config.SinkConnectorConfig; +import org.apache.eventmesh.openconnect.api.config.Config; +import org.apache.eventmesh.openconnect.api.connector.ConnectorContext; +import org.apache.eventmesh.openconnect.api.connector.SinkConnectorContext; +import org.apache.eventmesh.openconnect.api.sink.Sink; +import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord; + +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import com.github.rholder.retry.RetryException; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.lark.oapi.Client; +import com.lark.oapi.core.enums.AppType; +import com.lark.oapi.core.request.SelfBuiltTenantAccessTokenReq; +import com.lark.oapi.core.response.TenantAccessTokenResp; + +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +public class LarkSinkConnector implements Sink { + + public static final String TENANT_ACCESS_TOKEN = "tenant_access_token"; + + /** + * replace lark build-in tokenCache + */ + public static final Cache<String, String> AUTH_CACHE = CacheBuilder.newBuilder() + .initialCapacity(12) + .maximumSize(10) + .concurrencyLevel(5) + .expireAfterWrite(30, TimeUnit.MINUTES) + .build(); + + private LarkSinkConfig sinkConfig; + + private ImServiceHandler imServiceHandler; + + private final AtomicBoolean started = new AtomicBoolean(false); + + @Override + public Class<? extends Config> configClass() { + return LarkSinkConfig.class; + } + + @Override + public void init(Config config) { + // Deprecated + } + + @Override + public void init(ConnectorContext connectorContext) { + // init config for lark sink connector + SinkConnectorContext sinkConnectorContext = (SinkConnectorContext) connectorContext; + this.sinkConfig = (LarkSinkConfig) sinkConnectorContext.getSinkConfig(); + + SinkConnectorConfig sinkConnectorConfig = sinkConfig.getSinkConnectorConfig(); + sinkConnectorConfig.validateSinkConfiguration(); + + imServiceHandler = create(sinkConnectorConfig); + } + + @Override + public void start() { + if (!started.compareAndSet(false, true)) { + log.info("LarkSinkConnector has been started."); + } + } + + @Override + public void commit(ConnectRecord record) { + // Sink does not need to implement + } + + @Override + public String name() { + return this.sinkConfig.getSinkConnectorConfig().getConnectorName(); + } + + @Override + public void stop() { + if (!started.compareAndSet(true, false)) { + log.info("LarkSinkConnector has not started yet."); + } + } + + @Override + public void put(List<ConnectRecord> sinkRecords) { + for (ConnectRecord connectRecord : sinkRecords) { + try { + imServiceHandler.sink(connectRecord); + } catch (ExecutionException | RetryException e) { + log.error("Failed to sink event to lark", e); + } + } + } + + @SneakyThrows + public static String getTenantAccessToken(String appId, String appSecret) { Review Comment: > Other services may also need to use the credentials management . If you put ImServiceHandler in, it feels like it is only serving ImServiceHandler. But it's also possible that I'm over-designing it. If it is possible to develop other services that require this credential to further enrich the connector, you can leave a note in this method for other pending services that may require this credential. 如果还有可能开发其他需要该凭证的服务来进一步丰富该Connector,您可以在该方法备注一下可能需要该凭证的其他等待实现的服务。 > Actually, there is one point that puzzles me. If there is only one access credential, why use the Cache collection? I encountered similar doubts in an other connector before https://github.com/apache/eventmesh/pull/4594#discussion_r1412702091, and the developer explained that because credentials will expire and new credential will be constantly obtained in the code https://github.com/apache/eventmesh/pull/4594#discussion_r1412803666. I'm not sure if the credential here will expire. If it do, not only does it require Cache, but also a mechanism for regular updates. 我之前在其他Connector遇到类似的疑惑 https://github.com/apache/eventmesh/pull/4594#discussion_r1412702091 ,开发者解释到因为凭证会过期 https://github.com/apache/eventmesh/pull/4594#discussion_r1412803666 ,并且在代码中会不断获取新的凭证。我不清楚这里的凭证会不会过期,如果会过期,不仅需要Cache,还需要定时更新的机制。 ########## eventmesh-connectors/eventmesh-connector-lark/src/main/java/org/apache/eventmesh/connector/lark/sink/ImServiceHandler.java: ########## @@ -0,0 +1,267 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.eventmesh.connector.lark.sink; + +import static org.apache.eventmesh.connector.lark.sink.connector.LarkSinkConnector.getTenantAccessToken; + +import org.apache.eventmesh.connector.lark.ConnectRecordExtensionKeys; +import org.apache.eventmesh.connector.lark.config.LarkMessageTemplateType; +import org.apache.eventmesh.connector.lark.sink.config.SinkConnectorConfig; +import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord; + +import org.apache.commons.text.StringEscapeUtils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import com.github.rholder.retry.Attempt; +import com.github.rholder.retry.RetryException; +import com.github.rholder.retry.RetryListener; +import com.github.rholder.retry.Retryer; +import com.github.rholder.retry.RetryerBuilder; +import com.github.rholder.retry.StopStrategies; +import com.github.rholder.retry.WaitStrategies; +import com.lark.oapi.Client; +import com.lark.oapi.card.enums.MessageCardHeaderTemplateEnum; +import com.lark.oapi.card.model.MessageCard; +import com.lark.oapi.card.model.MessageCardConfig; +import com.lark.oapi.card.model.MessageCardElement; +import com.lark.oapi.card.model.MessageCardHeader; +import com.lark.oapi.card.model.MessageCardMarkdown; +import com.lark.oapi.card.model.MessageCardPlainText; +import com.lark.oapi.core.httpclient.OkHttpTransport; +import com.lark.oapi.core.request.RequestOptions; +import com.lark.oapi.core.utils.Lists; +import com.lark.oapi.okhttp.OkHttpClient; +import com.lark.oapi.service.im.v1.ImService; +import com.lark.oapi.service.im.v1.enums.MsgTypeEnum; +import com.lark.oapi.service.im.v1.model.CreateMessageReq; +import com.lark.oapi.service.im.v1.model.CreateMessageReqBody; +import com.lark.oapi.service.im.v1.model.CreateMessageResp; +import com.lark.oapi.service.im.v1.model.ext.MessageText; + +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class ImServiceHandler { + + private static final ConcurrentHashMap<ConnectRecord, CreateMessageReq> UN_ACK_REQ = new ConcurrentHashMap<>(); Review Comment: > The original code will re-inject a new UUID every time it is retransmitted, which will lead to repeated consumption by Feishu. Could you explain what incorrect phenomenon or logic may occur when retry, which may lead to "repeated consumption by Feishu"? Because I don't understand the difference between retrying and "repeated consumption by Feishu". 能否说明一下在重试的时候“会导致飞书重复消费”会出现什么不正确的现象或者逻辑?因为我没明白重试和“飞书重复消费”有什么区别。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
