You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kie.apache.org by "mcruzdev (via GitHub)" <gi...@apache.org> on 2024/08/13 14:06:03 UTC

[PR] Add correlation persistence for Infinispan [incubator-kie-kogito-runtimes]

mcruzdev opened a new pull request, #3617:
URL: https://github.com/apache/incubator-kie-kogito-runtimes/pull/3617

   <!--
     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.
     -->
   
   Many thanks for submitting your Pull Request :heart:! 
   
   <!-- Please don't forget your Issue link -->
   Closes/Fixes/Resolves #ISSUE-NUMBER
   
   <!-- Please provide a short description of what this PR does -->
   **Description:**
   
   <!-- Link to related PRs: -->
   
   Please make sure that your PR meets the following requirements:
   
   - [ ] You have read the [contributors guide](CONTRIBUTING.md)
   - [ ] Your code is properly formatted according to [this configuration](https://github.com/apache/incubator-kie-kogito-runtimes/tree/main/kogito-build/kogito-ide-config)
   - [ ] Pull Request title is properly formatted: `Issue-XYZ Subject`
   - [ ] Pull Request title contains the target branch if not targeting main: `[0.9.x] Issue-XYZ Subject`
   - [ ] Pull Request contains link to the JIRA issue
   - [ ] Pull Request contains link to any dependent or related Pull Request
   - [ ] Pull Request contains description of the issue
   - [ ] Pull Request does not include fixes for issues other than the main ticket
   
   <details>
   <summary>
   How to replicate CI configuration locally?
   </summary>
   
   Build Chain tool does "simple" maven build(s), the builds are just Maven commands, but because the repositories relates and depends on each other and any change in API or class method could affect several of those repositories there is a need to use [build-chain tool](https://github.com/kiegroup/github-action-build-chain) to handle cross repository builds and be sure that we always use latest version of the code for each repository.
    
   [build-chain tool](https://github.com/kiegroup/github-action-build-chain) is a build tool which can be used on command line locally or in Github Actions workflow(s), in case you need to change multiple repositories and send multiple dependent pull requests related with a change you can easily reproduce the same build by executing it on Github hosted environment or locally in your development environment. See [local execution](https://github.com/kiegroup/github-action-build-chain#local-execution) details to get more information about it.
   </details>
   
   
   


-- 
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] Add correlation persistence for Infinispan [incubator-kie-kogito-runtimes]

Posted by "mcruzdev (via GitHub)" <gi...@apache.org>.
mcruzdev closed pull request #3617: Add correlation persistence for Infinispan
URL: https://github.com/apache/incubator-kie-kogito-runtimes/pull/3617


-- 
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] Add correlation persistence for Infinispan [incubator-kie-kogito-runtimes]

Posted by "mcruzdev (via GitHub)" <gi...@apache.org>.
mcruzdev commented on code in PR #3617:
URL: https://github.com/apache/incubator-kie-kogito-runtimes/pull/3617#discussion_r1715363871


##########
addons/common/persistence/infinispan/src/main/java/org/kie/kogito/infinispan/correlation/InfinispanCorrelationRepository.java:
##########
@@ -0,0 +1,102 @@
+package org.kie.kogito.infinispan.correlation;
+
+import java.io.UncheckedIOException;
+import java.util.Optional;
+
+import org.infinispan.client.hotrod.DefaultTemplate;
+import org.infinispan.client.hotrod.RemoteCache;
+import org.infinispan.client.hotrod.RemoteCacheManager;
+import org.kie.kogito.correlation.CompositeCorrelation;
+import org.kie.kogito.correlation.Correlation;
+import org.kie.kogito.correlation.CorrelationInstance;
+import org.kie.kogito.correlation.SimpleCorrelation;
+import org.kie.kogito.internal.utils.ConversionUtils;
+import org.kie.kogito.jackson.utils.ObjectMapperFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+
+public class InfinispanCorrelationRepository {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(InfinispanCorrelationRepository.class);
+
+    private static final String ENCODED_CORRELATION_ID_FIELD = "encodedCorrelationId";
+    private static final String CORRELATED_ID_FIELD = "correlatedId";
+    private static final String CORRELATION_FIELD = "correlation";
+    public static final String CORRELATIONS_CACHE_NAME = "correlations";
+
+    private final RemoteCache<String, String> cache;
+    private final ObjectMapper objectMapper;
+
+    public InfinispanCorrelationRepository(
+            RemoteCacheManager cacheManager,
+            String templateName) {
+        if (ConversionUtils.isEmpty(templateName)) {
+            this.cache = cacheManager.administration().getOrCreateCache(CORRELATIONS_CACHE_NAME, DefaultTemplate.LOCAL);
+        } else {
+            this.cache = cacheManager.administration().getOrCreateCache(CORRELATIONS_CACHE_NAME, templateName);
+        }
+        SimpleModule simpleModule = new SimpleModule();
+        simpleModule.addAbstractTypeMapping(Correlation.class, SimpleCorrelation.class);
+        this.objectMapper = ObjectMapperFactory.get().copy().registerModule(simpleModule);
+    }
+
+    public CorrelationInstance insert(String encodedCorrelationId, String correlatedId, Correlation correlation) {
+        CorrelationInstance correlationInstance = new CorrelationInstance(encodedCorrelationId, correlatedId, correlation);
+
+        try {
+            String json = this.objectMapper.writeValueAsString(correlation);
+            CorrelationRecord correlationRecord = new CorrelationRecord(
+                    encodedCorrelationId, correlatedId, json);
+            String value = this.objectMapper.writeValueAsString(correlationRecord);
+            this.cache.put(encodedCorrelationId, value);
+            return correlationInstance;
+        } catch (JsonProcessingException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
+    public CorrelationInstance findByEncodedCorrelationId(String encodedCorrelationId) {
+        String json = this.cache.get(encodedCorrelationId);
+        if (json == null) {
+            return null;
+        }
+        try {
+            CorrelationRecord correlation = this.objectMapper.readValue(json, CorrelationRecord.class);
+            CompositeCorrelation compositeCorrelation = this.objectMapper.readValue(correlation.correlation(), CompositeCorrelation.class);
+            return new CorrelationInstance(
+                    correlation.encodedCorrelationId(),
+                    correlation.correlatedId(),
+                    compositeCorrelation);
+        } catch (JsonProcessingException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
+    public CorrelationInstance findByCorrelatedId(String correlatedId) {
+
+        Optional<String> first = this.cache.values().stream().filter(item -> item.contains("\"correlatedId\":\"%s\"".formatted(correlatedId)))

Review Comment:
   @fjtirado look here



-- 
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org