-
Notifications
You must be signed in to change notification settings - Fork 639
[ISSUE #4621] Implement TransformerEngine for EventMesh Transformer #4622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/TransformerEngine.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* 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.runtime.boot; | ||
|
||
import org.apache.eventmesh.api.meta.MetaServiceListener; | ||
import org.apache.eventmesh.common.utils.JsonUtils; | ||
import org.apache.eventmesh.common.utils.LogUtils; | ||
import org.apache.eventmesh.runtime.core.protocol.http.consumer.ConsumerGroupManager; | ||
import org.apache.eventmesh.runtime.core.protocol.http.consumer.ConsumerManager; | ||
import org.apache.eventmesh.runtime.core.protocol.producer.EventMeshProducer; | ||
import org.apache.eventmesh.runtime.core.protocol.producer.ProducerManager; | ||
import org.apache.eventmesh.runtime.meta.MetaStorage; | ||
import org.apache.eventmesh.transformer.Transformer; | ||
import org.apache.eventmesh.transformer.TransformerBuilder; | ||
import org.apache.eventmesh.transformer.TransformerParam; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class TransformerEngine { | ||
|
||
/** | ||
* key:group-topic | ||
**/ | ||
private final Map<String, Transformer> transformerMap = new HashMap<>(); | ||
|
||
private final String transformerPrefix = "transformer-"; | ||
|
||
private final MetaStorage metaStorage; | ||
|
||
private MetaServiceListener metaServiceListener; | ||
|
||
private final ProducerManager producerManager; | ||
|
||
private final ConsumerManager consumerManager; | ||
|
||
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); | ||
|
||
public TransformerEngine(MetaStorage metaStorage, ProducerManager producerManager, ConsumerManager consumerManager) { | ||
this.metaStorage = metaStorage; | ||
this.producerManager = producerManager; | ||
this.consumerManager = consumerManager; | ||
} | ||
|
||
public void start() { | ||
Map<String, String> transformerMetaData = metaStorage.getMetaData(transformerPrefix, true); | ||
for (Entry<String, String> transformerDataEntry : transformerMetaData.entrySet()) { | ||
// transformer-group | ||
String key = transformerDataEntry.getKey(); | ||
// topic-transformerParam list | ||
String value = transformerDataEntry.getValue(); | ||
updateTransformerMap(key, value); | ||
} | ||
metaServiceListener = this::updateTransformerMap; | ||
|
||
// addListeners for producerManager & consumerManager | ||
scheduledExecutorService.scheduleAtFixedRate(() -> { | ||
ConcurrentHashMap<String, EventMeshProducer> producerMap = producerManager.getProducerTable(); | ||
for (String producerGroup : producerMap.keySet()) { | ||
for (String transformerKey : transformerMap.keySet()) { | ||
if (!StringUtils.contains(transformerKey, producerGroup)) { | ||
addTransformerListener(producerGroup); | ||
LogUtils.info(log, "addTransformerListener for producer group: " + producerGroup); | ||
} | ||
} | ||
} | ||
ConcurrentHashMap<String, ConsumerGroupManager> consumerMap = consumerManager.getClientTable(); | ||
for (String consumerGroup : consumerMap.keySet()) { | ||
for (String transformerKey : transformerMap.keySet()) { | ||
if (!StringUtils.contains(transformerKey, consumerGroup)) { | ||
addTransformerListener(consumerGroup); | ||
LogUtils.info(log, "addTransformerListener for consumer group: " + consumerGroup); | ||
} | ||
} | ||
} | ||
}, 10_000, 5_000, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
private void updateTransformerMap(String key, String value) { | ||
String group = StringUtils.substringAfter(key, transformerPrefix); | ||
|
||
JsonNode transformerJsonNodeArray = JsonUtils.getJsonNode(value); | ||
|
||
if (transformerJsonNodeArray != null) { | ||
for (JsonNode transformerJsonNode : transformerJsonNodeArray) { | ||
String topic = transformerJsonNode.get("topic").asText(); | ||
String transformerParam = transformerJsonNode.get("transformerParam").toString(); | ||
TransformerParam tfp = JsonUtils.parseObject(transformerParam, TransformerParam.class); | ||
Transformer transformer = TransformerBuilder.buildTransformer(tfp); | ||
transformerMap.put(group + "-" + topic, transformer); | ||
} | ||
} | ||
addTransformerListener(group); | ||
} | ||
|
||
public void addTransformerListener(String group) { | ||
String transformerKey = transformerPrefix + group; | ||
try { | ||
metaStorage.getMetaDataWithListener(metaServiceListener, transformerKey); | ||
} catch (Exception e) { | ||
throw new RuntimeException("addTransformerListener exception", e); | ||
} | ||
} | ||
|
||
public void shutdown() { | ||
scheduledExecutorService.shutdown(); | ||
} | ||
|
||
public Transformer getTransformer(String key) { | ||
return transformerMap.get(key); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
eventmesh-transformer/src/main/java/org/apache/eventmesh/transformer/TransformerParam.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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.transformer; | ||
|
||
public class TransformerParam { | ||
|
||
private TransformerType transformerType; | ||
private String value; | ||
private String template; | ||
|
||
public TransformerParam() { | ||
} | ||
|
||
public TransformerParam(TransformerType transformerType, String value, String template) { | ||
this.transformerType = transformerType; | ||
this.value = value; | ||
this.template = template; | ||
} | ||
|
||
public TransformerParam(TransformerType transformerType, String value) { | ||
this(transformerType, value, null); | ||
} | ||
|
||
public TransformerType getTransformerType() { | ||
return transformerType; | ||
} | ||
|
||
public void setTransformerType(TransformerType transformerType) { | ||
this.transformerType = transformerType; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getTemplate() { | ||
return template; | ||
} | ||
|
||
public void setTemplate(String template) { | ||
this.template = template; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this step be placed before for( ), otherwise, is it possible for
metaServiceListener
to be null whenaddTransformerListener()
inupdateTransformerMap()
?这一步是不是应该放到for( )前面,否则的话,
updateTransformerMap()
中addTransformerListener()
时,metaServiceListener
是不是为null?