You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2020/07/11 20:59:27 UTC

[GitHub] [camel] malogan82 opened a new pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

malogan82 opened a new pull request #3993:
URL: https://github.com/apache/camel/pull/3993


   [ ] Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it).  Trivial changes like typos do not require a JIRA issue.  Your pull request should address just this issue, without pulling in other changes.
   [ ] Each commit in the pull request should have a meaningful subject line and body.
   [ ] If you're unsure, you can format the pull request title like `[CAMEL-XXX] Fixes bug in camel-file component`, where you replace `CAMEL-XXX` with the appropriate JIRA issue.
   [ ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
   [ ] Run `mvn clean install -Psourcecheck` in your module with source check enabled to make sure basic checks pass and there are no checkstyle violations. A more thorough check will be performed on your pull request automatically.
   Below are the contribution guidelines:
   https://github.com/apache/camel/blob/master/CONTRIBUTING.md


----------------------------------------------------------------
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.

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



[GitHub] [camel] lburgazzoli commented on a change in pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on a change in pull request #3993:
URL: https://github.com/apache/camel/pull/3993#discussion_r453236039



##########
File path: components/camel-redis/src/main/java/org/apache/camel/component/redis/processor/aggregate/RedisAggregationRepository.java
##########
@@ -0,0 +1,338 @@
+/*
+ * 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.camel.component.redis.processor.aggregate;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository;
+import org.apache.camel.spi.RecoverableAggregationRepository;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository.OptimisticLockingException;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.support.DefaultExchangeHolder;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+import org.redisson.Redisson;
+import org.redisson.api.RLock;
+import org.redisson.api.RMap;
+import org.redisson.api.RTransaction;
+import org.redisson.api.RedissonClient;
+import org.redisson.api.TransactionOptions;
+import org.redisson.config.Config;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RedisAggregationRepository extends ServiceSupport
+		implements RecoverableAggregationRepository, OptimisticLockingAggregationRepository {
+	private static final Logger LOG = LoggerFactory.getLogger(RedisAggregationRepository.class.getName());
+	private static final String COMPLETED_SUFFIX = "-completed";
+
+	private boolean optimistic;
+	private boolean useRecovery = true;
+	private Map<String, DefaultExchangeHolder> cache;
+    private Map<String, DefaultExchangeHolder> persistedCache;
+	private String endpoint;
+	private String mapName;
+	private String persistenceMapName;
+	private RedissonClient redisson;
+	private String deadLetterChannel;
+	private long recoveryInterval = 5000;
+	private int maximumRedeliveries = 3;
+	private boolean allowSerializedHeaders;
+
+	public RedisAggregationRepository(final String mapName, final String endpoint) {
+		this.mapName = mapName;
+		this.persistenceMapName = String.format("%s%s", mapName, COMPLETED_SUFFIX);
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public RedisAggregationRepository(final String mapName, final String persistenceMapName,
+			final String endpoint) {
+		this.mapName = mapName;
+		this.persistenceMapName = persistenceMapName;
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public RedisAggregationRepository(final String mapName, final String endpoint, boolean optimistic) {
+		this(mapName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	public RedisAggregationRepository(final String mapName, final String persistenceMapName,
+			final String endpoint, boolean optimistic) {
+		this(mapName, persistenceMapName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	@Override
+    public Exchange add(CamelContext camelContext, String key, Exchange oldExchange, Exchange newExchange) throws OptimisticLockingException {
+        if (!optimistic) {
+            throw new UnsupportedOperationException();
+        }
+        LOG.trace("Adding an Exchange with ID {} for key {} in an optimistic manner.", newExchange.getExchangeId(), key);
+        if (oldExchange == null) {
+            DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
+            final DefaultExchangeHolder misbehaviorHolder = cache.putIfAbsent(key, holder);
+            if (misbehaviorHolder != null) {
+                Exchange misbehaviorEx = unmarshallExchange(camelContext, misbehaviorHolder);
+                LOG.error("Optimistic locking failed for exchange with key {}: IMap#putIfAbsend returned Exchange with ID {}, while it's expected no exchanges to be returned",
+                        key, misbehaviorEx != null ? misbehaviorEx.getExchangeId() : "<null>");
+                throw  new OptimisticLockingException();
+            }
+        } else {
+            DefaultExchangeHolder oldHolder = DefaultExchangeHolder.marshal(oldExchange, true, allowSerializedHeaders);
+            DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
+            if (!cache.replace(key, oldHolder, newHolder)) {
+                LOG.error("Optimistic locking failed for exchange with key {}: IMap#replace returned no Exchanges, while it's expected to replace one",
+                        key);
+                throw new OptimisticLockingException();
+            }
+        }
+        LOG.trace("Added an Exchange with ID {} for key {} in optimistic manner.", newExchange.getExchangeId(), key);
+        return oldExchange;
+    }
+
+	@Override
+	public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
+		if (optimistic) {
+			throw new UnsupportedOperationException();
+		}
+		LOG.trace("Adding an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
+		Config config = new Config();
+		config.useSingleServer().setAddress(String.format("redis://%s",endpoint));
+		RedissonClient redisson = Redisson.create(config);

Review comment:
       The global `redisson` instance is created by `onStart` so I guess this code can be replaced by using the shared instance




----------------------------------------------------------------
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.

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



[GitHub] [camel] lburgazzoli commented on a change in pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on a change in pull request #3993:
URL: https://github.com/apache/camel/pull/3993#discussion_r453235881



##########
File path: components/camel-redis/src/main/java/org/apache/camel/component/redis/processor/aggregate/RedisAggregationRepository.java
##########
@@ -0,0 +1,338 @@
+/*
+ * 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.camel.component.redis.processor.aggregate;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository;
+import org.apache.camel.spi.RecoverableAggregationRepository;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository.OptimisticLockingException;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.support.DefaultExchangeHolder;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+import org.redisson.Redisson;
+import org.redisson.api.RLock;
+import org.redisson.api.RMap;
+import org.redisson.api.RTransaction;
+import org.redisson.api.RedissonClient;
+import org.redisson.api.TransactionOptions;
+import org.redisson.config.Config;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RedisAggregationRepository extends ServiceSupport
+		implements RecoverableAggregationRepository, OptimisticLockingAggregationRepository {
+	private static final Logger LOG = LoggerFactory.getLogger(RedisAggregationRepository.class.getName());

Review comment:
       `.getClass()` can be omitted




----------------------------------------------------------------
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.

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



[GitHub] [camel] malogan82 removed a comment on pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
malogan82 removed a comment on pull request #3993:
URL: https://github.com/apache/camel/pull/3993#issuecomment-657123240


   wrong commit removed


----------------------------------------------------------------
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.

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



[GitHub] [camel] malogan82 commented on pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
malogan82 commented on pull request #3993:
URL: https://github.com/apache/camel/pull/3993#issuecomment-678405790


   @lburgazzoli @omarsmak I'm sorry, now I've rebased and fixed those two open points, now it should be ok


----------------------------------------------------------------
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.

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



[GitHub] [camel] lburgazzoli commented on a change in pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on a change in pull request #3993:
URL: https://github.com/apache/camel/pull/3993#discussion_r453235804



##########
File path: components/camel-redis/pom.xml
##########
@@ -0,0 +1,86 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.camel</groupId>
+        <artifactId>components</artifactId>
+        <version>3.5.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-redis</artifactId>
+    <packaging>jar</packaging>
+    <name>Camel :: Redis</name>
+    <description>Camel Redis support</description>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-cloud</artifactId>
+        </dependency>
+
+        <!-- etcd -->
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+            <version>${commons-lang3-version}</version>
+        </dependency>
+        
+        <dependency>
+	       <groupId>org.redisson</groupId>
+           <artifactId>redisson</artifactId>
+           <version>${redisson-version}</version>
+        </dependency>
+
+        <!-- logging -->
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-slf4j-impl</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <!-- testing -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-testcontainers-spring-junit5</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-testcontainers-junit5</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-http</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-jetty</artifactId>
+            <scope>test</scope>
+        </dependency>

Review comment:
       not sure about what dependencies are really needed since there's no tests




----------------------------------------------------------------
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.

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



[GitHub] [camel] lburgazzoli commented on a change in pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on a change in pull request #3993:
URL: https://github.com/apache/camel/pull/3993#discussion_r453235881



##########
File path: components/camel-redis/src/main/java/org/apache/camel/component/redis/processor/aggregate/RedisAggregationRepository.java
##########
@@ -0,0 +1,338 @@
+/*
+ * 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.camel.component.redis.processor.aggregate;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository;
+import org.apache.camel.spi.RecoverableAggregationRepository;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository.OptimisticLockingException;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.support.DefaultExchangeHolder;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+import org.redisson.Redisson;
+import org.redisson.api.RLock;
+import org.redisson.api.RMap;
+import org.redisson.api.RTransaction;
+import org.redisson.api.RedissonClient;
+import org.redisson.api.TransactionOptions;
+import org.redisson.config.Config;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RedisAggregationRepository extends ServiceSupport
+		implements RecoverableAggregationRepository, OptimisticLockingAggregationRepository {
+	private static final Logger LOG = LoggerFactory.getLogger(RedisAggregationRepository.class.getName());

Review comment:
       `.getName()` can be omitted




----------------------------------------------------------------
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.

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



[GitHub] [camel] malogan82 commented on pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
malogan82 commented on pull request #3993:
URL: https://github.com/apache/camel/pull/3993#issuecomment-660041305


   sorry @lburgazzoli I should have fixed all the main bugs, at the moment there are only errors due to checkstyle, apart these do you think it is ok? thanks


----------------------------------------------------------------
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.

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



[GitHub] [camel] malogan82 commented on pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
malogan82 commented on pull request #3993:
URL: https://github.com/apache/camel/pull/3993#issuecomment-657123240


   wrong commit removed


----------------------------------------------------------------
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.

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



[GitHub] [camel] lburgazzoli commented on pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on pull request #3993:
URL: https://github.com/apache/camel/pull/3993#issuecomment-678255869


   @malogan82 @omarsmak there's still two open point if I'm not mistaking


----------------------------------------------------------------
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.

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



[GitHub] [camel] davsclaus commented on pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
davsclaus commented on pull request #3993:
URL: https://github.com/apache/camel/pull/3993#issuecomment-680775311


   Okay merged to master. @malogan82 you are welcome to keep improving it - for example the documentation
   https://github.com/apache/camel/blob/master/components/camel-redis/src/main/docs/redis.adoc


----------------------------------------------------------------
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.

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



[GitHub] [camel] malogan82 commented on pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
malogan82 commented on pull request #3993:
URL: https://github.com/apache/camel/pull/3993#issuecomment-657128949


   Wrong commit removed


----------------------------------------------------------------
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.

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



[GitHub] [camel] davsclaus commented on pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
davsclaus commented on pull request #3993:
URL: https://github.com/apache/camel/pull/3993#issuecomment-680758173


   Okay working on merging this


----------------------------------------------------------------
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.

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



[GitHub] [camel] lburgazzoli commented on a change in pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on a change in pull request #3993:
URL: https://github.com/apache/camel/pull/3993#discussion_r453236537



##########
File path: components/pom.xml
##########
@@ -306,6 +306,7 @@
         <module>camel-reactive-executor-vertx</module>
         <module>camel-reactive-streams</module>
         <module>camel-reactor</module>
+		<module>camel-redis</module>

Review comment:
       would be better to fix the indentation in this and the other xml files




----------------------------------------------------------------
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.

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



[GitHub] [camel] malogan82 closed pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
malogan82 closed pull request #3993:
URL: https://github.com/apache/camel/pull/3993


   


----------------------------------------------------------------
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.

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



[GitHub] [camel] lburgazzoli commented on a change in pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on a change in pull request #3993:
URL: https://github.com/apache/camel/pull/3993#discussion_r453236150



##########
File path: components/camel-redis/src/main/java/org/apache/camel/component/redis/processor/aggregate/RedisAggregationRepository.java
##########
@@ -0,0 +1,338 @@
+/*
+ * 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.camel.component.redis.processor.aggregate;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository;
+import org.apache.camel.spi.RecoverableAggregationRepository;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository.OptimisticLockingException;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.support.DefaultExchangeHolder;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+import org.redisson.Redisson;
+import org.redisson.api.RLock;
+import org.redisson.api.RMap;
+import org.redisson.api.RTransaction;
+import org.redisson.api.RedissonClient;
+import org.redisson.api.TransactionOptions;
+import org.redisson.config.Config;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RedisAggregationRepository extends ServiceSupport
+		implements RecoverableAggregationRepository, OptimisticLockingAggregationRepository {
+	private static final Logger LOG = LoggerFactory.getLogger(RedisAggregationRepository.class.getName());
+	private static final String COMPLETED_SUFFIX = "-completed";
+
+	private boolean optimistic;
+	private boolean useRecovery = true;
+	private Map<String, DefaultExchangeHolder> cache;
+    private Map<String, DefaultExchangeHolder> persistedCache;
+	private String endpoint;
+	private String mapName;
+	private String persistenceMapName;
+	private RedissonClient redisson;
+	private String deadLetterChannel;
+	private long recoveryInterval = 5000;
+	private int maximumRedeliveries = 3;
+	private boolean allowSerializedHeaders;
+
+	public RedisAggregationRepository(final String mapName, final String endpoint) {
+		this.mapName = mapName;
+		this.persistenceMapName = String.format("%s%s", mapName, COMPLETED_SUFFIX);
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public RedisAggregationRepository(final String mapName, final String persistenceMapName,
+			final String endpoint) {
+		this.mapName = mapName;
+		this.persistenceMapName = persistenceMapName;
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public RedisAggregationRepository(final String mapName, final String endpoint, boolean optimistic) {
+		this(mapName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	public RedisAggregationRepository(final String mapName, final String persistenceMapName,
+			final String endpoint, boolean optimistic) {
+		this(mapName, persistenceMapName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	@Override
+    public Exchange add(CamelContext camelContext, String key, Exchange oldExchange, Exchange newExchange) throws OptimisticLockingException {
+        if (!optimistic) {
+            throw new UnsupportedOperationException();
+        }
+        LOG.trace("Adding an Exchange with ID {} for key {} in an optimistic manner.", newExchange.getExchangeId(), key);
+        if (oldExchange == null) {
+            DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
+            final DefaultExchangeHolder misbehaviorHolder = cache.putIfAbsent(key, holder);
+            if (misbehaviorHolder != null) {
+                Exchange misbehaviorEx = unmarshallExchange(camelContext, misbehaviorHolder);
+                LOG.error("Optimistic locking failed for exchange with key {}: IMap#putIfAbsend returned Exchange with ID {}, while it's expected no exchanges to be returned",
+                        key, misbehaviorEx != null ? misbehaviorEx.getExchangeId() : "<null>");
+                throw  new OptimisticLockingException();
+            }
+        } else {
+            DefaultExchangeHolder oldHolder = DefaultExchangeHolder.marshal(oldExchange, true, allowSerializedHeaders);
+            DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
+            if (!cache.replace(key, oldHolder, newHolder)) {
+                LOG.error("Optimistic locking failed for exchange with key {}: IMap#replace returned no Exchanges, while it's expected to replace one",
+                        key);
+                throw new OptimisticLockingException();
+            }
+        }
+        LOG.trace("Added an Exchange with ID {} for key {} in optimistic manner.", newExchange.getExchangeId(), key);
+        return oldExchange;
+    }
+
+	@Override
+	public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
+		if (optimistic) {
+			throw new UnsupportedOperationException();
+		}
+		LOG.trace("Adding an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
+		Config config = new Config();
+		config.useSingleServer().setAddress(String.format("redis://%s",endpoint));
+		RedissonClient redisson = Redisson.create(config);
+		RLock lock = redisson.getLock("aggregationLock");
+		try {
+            lock.lock();
+            DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
+            DefaultExchangeHolder oldHolder = cache.put(key, newHolder);
+            return unmarshallExchange(camelContext, oldHolder);
+        } finally {
+            LOG.trace("Added an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
+            lock.unlock();
+        }
+	}
+
+	@Override
+    public Set<String> scan(CamelContext camelContext) {
+        if (useRecovery) {
+            LOG.trace("Scanning for exchanges to recover in {} context", camelContext.getName());
+            Set<String> scanned = Collections.unmodifiableSet(persistedCache.keySet());
+            LOG.trace("Found {} keys for exchanges to recover in {} context", scanned.size(), camelContext.getName());
+            return scanned;
+        } else {
+            LOG.warn("What for to run recovery scans in {} context while repository {} is running in non-recoverable aggregation repository mode?!",
+                    camelContext.getName(), mapName);
+            return Collections.emptySet();
+        }
+    }
+
+    @Override
+    public Exchange recover(CamelContext camelContext, String exchangeId) {
+        LOG.trace("Recovering an Exchange with ID {}.", exchangeId);
+        return useRecovery ? unmarshallExchange(camelContext, persistedCache.get(exchangeId)) : null;
+    }
+
+    @Override
+    public void setRecoveryInterval(long interval, TimeUnit timeUnit) {
+        this.recoveryInterval = timeUnit.toMillis(interval);
+    }
+
+    @Override
+    public void setRecoveryInterval(long interval) {
+        this.recoveryInterval = interval;
+    }
+
+    @Override
+    public long getRecoveryIntervalInMillis() {
+        return recoveryInterval;
+    }
+
+    @Override
+    public void setUseRecovery(boolean useRecovery) {
+        this.useRecovery = useRecovery;
+    }
+
+    @Override
+    public boolean isUseRecovery() {
+        return useRecovery;
+    }
+
+    @Override
+    public void setDeadLetterUri(String deadLetterUri) {
+        this.deadLetterChannel = deadLetterUri;
+    }
+
+    @Override
+    public String getDeadLetterUri() {
+        return deadLetterChannel;
+    }
+
+    @Override
+    public void setMaximumRedeliveries(int maximumRedeliveries) {
+        this.maximumRedeliveries = maximumRedeliveries;
+    }
+
+    @Override
+    public int getMaximumRedeliveries() {
+        return maximumRedeliveries;
+    }
+
+    @Override
+    public Exchange get(CamelContext camelContext, String key) {
+        return unmarshallExchange(camelContext, cache.get(key));
+    }
+    
+    /**
+     * Checks if the key in question is in the repository.
+     * 
+     * @param key Object - key in question
+     */
+    public boolean containsKey(Object key) {
+        if (cache != null) {
+            return cache.containsKey(key);
+        } else {
+            return false;
+        }
+    }
+    
+    public boolean isAllowSerializedHeaders() {
+        return allowSerializedHeaders;
+    }
+
+    public void setAllowSerializedHeaders(boolean allowSerializedHeaders) {
+        this.allowSerializedHeaders = allowSerializedHeaders;
+    }
+
+    @Override
+    public void remove(CamelContext camelContext, String key, Exchange exchange) {
+        DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
+        if (optimistic) {
+            LOG.trace("Removing an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(), key);
+            if (!cache.remove(key, holder)) {
+                LOG.error("Optimistic locking failed for exchange with key {}: IMap#remove removed no Exchanges, while it's expected to remove one.",
+                        key);
+                throw new OptimisticLockingException();
+            }
+            LOG.trace("Removed an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(), key);
+            if (useRecovery) {
+                LOG.trace("Putting an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.",
+                        exchange.getExchangeId(), key);
+                persistedCache.put(exchange.getExchangeId(), holder);
+                LOG.trace("Put an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.",
+                        exchange.getExchangeId(), key);
+            }
+        } else {
+            if (useRecovery) {
+                LOG.trace("Removing an exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
+                TransactionOptions tOpts = TransactionOptions.defaults();
+                RTransaction transaction = redisson.createTransaction(tOpts);
+
+                try {
+                	RMap<String, DefaultExchangeHolder> tCache = transaction.getMap(mapName);
+                	RMap<String, DefaultExchangeHolder> tPersistentCache = transaction.getMap(persistenceMapName);
+
+                    DefaultExchangeHolder removedHolder = tCache.remove(key);
+                    LOG.trace("Putting an exchange with ID {} for key {} into a recoverable storage in a thread-safe manner.",
+                            exchange.getExchangeId(), key);
+                    tPersistentCache.put(exchange.getExchangeId(), removedHolder);
+
+                    transaction.commit();
+                    LOG.trace("Removed an exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
+                    LOG.trace("Put an exchange with ID {} for key {} into a recoverable storage in a thread-safe manner.",
+                            exchange.getExchangeId(), key);
+                } catch (Throwable throwable) {
+                	transaction.rollback();
+
+                    final String msg = String.format("Transaction was rolled back for remove operation with a key %s and an Exchange ID %s.",
+                    		key, exchange.getExchangeId());
+                    LOG.warn(msg, throwable);
+                    throw new RuntimeException(msg, throwable);
+                }
+            } else {
+                cache.remove(key);
+            }
+        }
+    }
+
+    @Override
+    public void confirm(CamelContext camelContext, String exchangeId) {
+        LOG.trace("Confirming an exchange with ID {}.", exchangeId);
+        if (useRecovery) {
+            persistedCache.remove(exchangeId);
+        }
+    }
+
+    @Override
+    public Set<String> getKeys() {
+        return Collections.unmodifiableSet(cache.keySet());
+    }
+
+    /**
+     * @return Persistent repository {@link IMap} name;
+     */
+    public String getPersistentRepositoryName() {
+        return persistenceMapName;
+    }
+
+	@Override
+	protected void doStart() throws Exception {
+		if (maximumRedeliveries < 0) {
+			throw new IllegalArgumentException("Maximum redelivery retries must be zero or a positive integer.");
+		}
+		if (recoveryInterval < 0) {
+			throw new IllegalArgumentException("Recovery interval must be zero or a positive integer.");
+		}
+		StringHelper.notEmpty(mapName, "repositoryName");
+		Config config = new Config();
+		config.useSingleServer().setAddress(String.format("redis://%s",endpoint));
+		redisson = Redisson.create(config);
+		cache = redisson.getMap(mapName);
+		if (useRecovery) {
+            persistedCache = redisson.getMap(persistenceMapName);
+        }
+	}
+
+	@Override
+	protected void doStop() throws Exception {
+		redisson.shutdown();

Review comment:
       you may need to check if the `redisson` instance is `null` 




----------------------------------------------------------------
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.

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



[GitHub] [camel] omarsmak commented on pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
omarsmak commented on pull request #3993:
URL: https://github.com/apache/camel/pull/3993#issuecomment-678254362


   @lburgazzoli any feedback on this PR?


----------------------------------------------------------------
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.

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



[GitHub] [camel] malogan82 commented on pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
malogan82 commented on pull request #3993:
URL: https://github.com/apache/camel/pull/3993#issuecomment-680783733


   @davsclaus ok thanks


----------------------------------------------------------------
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.

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



[GitHub] [camel] lburgazzoli commented on a change in pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on a change in pull request #3993:
URL: https://github.com/apache/camel/pull/3993#discussion_r453235664



##########
File path: components/camel-redis/pom.xml
##########
@@ -0,0 +1,86 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.camel</groupId>
+        <artifactId>components</artifactId>
+        <version>3.5.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-redis</artifactId>
+    <packaging>jar</packaging>
+    <name>Camel :: Redis</name>
+    <description>Camel Redis support</description>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-cloud</artifactId>
+        </dependency>
+
+        <!-- etcd -->

Review comment:
       seems not right




----------------------------------------------------------------
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.

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



[GitHub] [camel] davsclaus closed pull request #3993: Feature camel redis - RedisAggregationRepository using Redisson

Posted by GitBox <gi...@apache.org>.
davsclaus closed pull request #3993:
URL: https://github.com/apache/camel/pull/3993


   


----------------------------------------------------------------
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.

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