malogan82 commented on a change in pull request #3983:
URL: https://github.com/apache/camel/pull/3983#discussion_r453482231



##########
File path: 
components/camel-etcd3/src/main/java/org/apache/camel/component/etcd3/processor/aggregate/Etcd3AggregationRepository.java
##########
@@ -0,0 +1,459 @@
+/*
+ * 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.etcd3.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.List;
+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.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultExchangeHolder;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository;
+import org.apache.camel.spi.RecoverableAggregationRepository;
+import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import io.etcd.jetcd.ByteSequence;
+import io.etcd.jetcd.Client;
+import io.etcd.jetcd.KV;
+import io.etcd.jetcd.KeyValue;
+import io.etcd.jetcd.Lock;
+import io.etcd.jetcd.Txn;
+import io.etcd.jetcd.kv.DeleteResponse;
+import io.etcd.jetcd.kv.GetResponse;
+import io.etcd.jetcd.kv.PutResponse;
+import io.etcd.jetcd.op.Cmp;
+import io.etcd.jetcd.op.CmpTarget;
+import io.etcd.jetcd.op.Op;
+import io.etcd.jetcd.options.DeleteOption;
+import io.etcd.jetcd.options.GetOption;
+import io.etcd.jetcd.options.PutOption;
+
+public class Etcd3AggregationRepository extends ServiceSupport
+               implements RecoverableAggregationRepository, 
OptimisticLockingAggregationRepository {
+       private static final Logger LOG = 
LoggerFactory.getLogger(Etcd3AggregationRepository.class.getName());
+       private static final String COMPLETED_SUFFIX = "-completed";
+
+       private boolean optimistic;
+       private boolean useRecovery = true;
+       private String endpoint;
+       private Client client;
+       private KV kvClient;
+       private String prefixName;
+       private String persistencePrefixName;
+       private String deadLetterChannel;
+       private long recoveryInterval = 5000;
+       private int maximumRedeliveries = 3;
+       private boolean allowSerializedHeaders;
+
+       public Etcd3AggregationRepository(final String prefixName, final String 
endpoint) {
+               this.prefixName = prefixName;
+               this.persistencePrefixName = String.format("%s%s", prefixName, 
COMPLETED_SUFFIX);
+               this.optimistic = false;
+               this.endpoint = endpoint;
+       }
+
+       public Etcd3AggregationRepository(final String prefixName, final String 
persistencePrefixName,
+                       final String endpoint) {
+               this.prefixName = prefixName;
+               this.persistencePrefixName = persistencePrefixName;
+               this.optimistic = false;
+               this.endpoint = endpoint;
+       }
+
+       public Etcd3AggregationRepository(final String prefixName, final String 
endpoint, boolean optimistic) {
+               this(prefixName, endpoint);
+               this.optimistic = optimistic;
+       }
+
+       public Etcd3AggregationRepository(final String repositoryName, final 
String persistentRepositoryName,
+                       final String endpoint, boolean optimistic) {
+               this(repositoryName, persistentRepositoryName, 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);
+               try {
+                       if (oldExchange == null) {
+                               DefaultExchangeHolder holder = 
DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
+                               CompletableFuture<GetResponse> 
completableGetResponse = kvClient
+                                               
.get(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+                               GetResponse getResponse = 
completableGetResponse.get();
+                               List<KeyValue> keyValues = getResponse.getKvs();
+                               if (keyValues.isEmpty()) {
+                                       ByteArrayOutputStream bos = new 
ByteArrayOutputStream();
+                                       ObjectOutputStream oos = new 
ObjectOutputStream(bos);
+                                       oos.writeObject(holder);
+                                       oos.flush();
+                                       byte[] data = bos.toByteArray();
+                                       CompletableFuture<PutResponse> 
completablePutResponse = kvClient.put(
+                                                       
ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()),
+                                                       
ByteSequence.from(data));
+                                       completablePutResponse.get();
+                               } else {
+                                       byte[] data = 
keyValues.get(0).getValue().getBytes();
+                                       ByteArrayInputStream in = new 
ByteArrayInputStream(data);
+                                       ObjectInputStream is = new 
ObjectInputStream(in);
+                                       DefaultExchangeHolder misbehaviorHolder 
= (DefaultExchangeHolder) is.readObject();
+                                       Exchange misbehaviorEx = 
unmarshallExchange(camelContext, misbehaviorHolder);
+                                       LOG.error(
+                                                       "Optimistic locking 
failed for exchange with key {}: kvClient.get returned Exchange with ID {}, 
while it's expected no exchanges to be returned",
+                                                       key, misbehaviorEx != 
null ? misbehaviorEx.getExchangeId() : "<null>");
+                                       throw new OptimisticLockingException();
+                               }
+                       } else {
+                               DefaultExchangeHolder newHolder = 
DefaultExchangeHolder.marshal(newExchange, true,
+                                               allowSerializedHeaders);
+                               ByteArrayOutputStream bos = new 
ByteArrayOutputStream();
+                               ObjectOutputStream oos = new 
ObjectOutputStream(bos);
+                               oos.writeObject(newHolder);
+                               oos.flush();
+                               byte[] data = bos.toByteArray();
+                               CompletableFuture<DeleteResponse> 
completableDeleteResponse = kvClient
+                                               
.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+                               DeleteResponse deleteResponse = 
completableDeleteResponse.get();
+                               if (deleteResponse.getDeleted() == 0) {
+                                       LOG.error(
+                                                       "Optimistic locking 
failed for exchange with key {}: kvClient.get returned no Exchanges, while it's 
expected to replace one",
+                                                       key);
+                                       throw new OptimisticLockingException();
+                               }
+                               CompletableFuture<PutResponse> 
completablePutResponse = kvClient.put(
+                                               
ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()), 
ByteSequence.from(data));
+                               completablePutResponse.get();
+                       }
+               } catch (InterruptedException | ExecutionException | 
IOException | ClassNotFoundException e) {
+                       LOG.error(e.getMessage(), e);
+                       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);
+               Lock lock = null;
+               DefaultExchangeHolder newHolder = 
DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
+               try {
+                       lock = client.getLockClient();
+                       lock.lock(ByteSequence.from(key.getBytes()), 60000);

Review comment:
       I think I should have fixed this issue, using transaction as you 
suggested, I've also fixed the other issues in Etcd3AggregationRepository 
class, meanwhile I'll fix indentation problems on xml files and add some test 
cases,
   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:
us...@infra.apache.org


Reply via email to