jdeppe-pivotal commented on a change in pull request #6794:
URL: https://github.com/apache/geode/pull/6794#discussion_r698810683



##########
File path: 
geode-apis-compatible-with-redis/src/commonTest/java/org/apache/geode/redis/ConcurrentLoopingThreads.java
##########
@@ -84,18 +86,49 @@ public void await() {
    * Start operations and only return once all are complete.
    */
   public void run() {
-    start(false);
+    start(false, null);
     await();
   }
 
   /**
    * Start operations and run each iteration in lockstep
    */
   public void runInLockstep() {
-    start(true);
+    start(true, null);
     await();
   }
 
+  /**
+   * Start operations and provide an action to be performed at the end of 
every iteration. This
+   * implies running in lockstep. This would typically be used to provide some 
form of validation.
+   */
+  public void runWithAction(Runnable action) {
+    Runnable innerRunnable = () -> {
+      try {
+        action.run();
+      } catch (Throwable e) {
+        actionThrowable.set(e);
+        throw e;
+      }
+    };
+
+    start(true, innerRunnable);
+
+    try {
+      await();
+    } catch (Throwable e) {
+      Throwable actionException = actionThrowable.get();
+      if (actionException != null) {
+        if (actionException instanceof Error) {
+          throw (Error) actionException;
+        }
+        throw new RuntimeException(actionThrowable.get());

Review comment:
       Good point. I think I was initially trying to set this elsewhere but now 
I've removed the `AtomicReference` wrapper since it can just be a regular field.

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/RegionProvider.java
##########
@@ -163,6 +164,19 @@ public RedisStats getRedisStats() {
     }
   }
 
+  public <T> T execute(Object key, List<Object> keysToLock, Callable<T> 
callable) {

Review comment:
       I initially didn't want to make it more specific, but your suggestion 
also allows the method signatures to be improved.

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/RegionProvider.java
##########
@@ -303,16 +317,14 @@ public RedisKeyCommands getKeyCommands() {
    *
    * @return the keys ordered in the sequence in which they should be locked.
    */
-  public List<RedisKey> orderForLocking(RedisKey key1, RedisKey key2) {
-    List<RedisKey> orderedKeys = new ArrayList<>();
-    if (stripedCoordinator.compareStripes(key1, key2) > 0) {
-      orderedKeys.add(key1);
-      orderedKeys.add(key2);
-    } else {
-      orderedKeys.add(key2);
-      orderedKeys.add(key1);
-    }
+  public List<RedisKey> orderForLocking(List<RedisKey> keys) {
+    Collections.sort(keys, stripedCoordinator::compareStripes);

Review comment:
       Done

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/RedisSortedSetCommands.java
##########
@@ -54,4 +54,8 @@
   long zrevrank(RedisKey key, byte[] member);
 
   byte[] zscore(RedisKey key, byte[] member);
+
+  long zunionstore(RedisKey key, List<RedisKey> sourceSets, List<Double> 
weights,

Review comment:
       Yup.

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZUnionStoreExecutor.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.geode.redis.internal.executor.sortedset;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX;
+import static 
org.apache.geode.redis.internal.RedisConstants.ERROR_WEIGHT_NOT_A_FLOAT;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_SLOT;
+import static org.apache.geode.redis.internal.netty.Coder.toUpperCaseBytes;
+import static 
org.apache.geode.redis.internal.netty.StringBytesGlossary.bAGGREGATE;
+import static 
org.apache.geode.redis.internal.netty.StringBytesGlossary.bWEIGHTS;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.executor.AbstractExecutor;
+import org.apache.geode.redis.internal.executor.RedisResponse;
+import org.apache.geode.redis.internal.netty.Coder;
+import org.apache.geode.redis.internal.netty.Command;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+
+public class ZUnionStoreExecutor extends AbstractExecutor {
+
+  @Override
+  public RedisResponse executeCommand(Command command, ExecutionHandlerContext 
context) {
+    List<byte[]> commandElements = command.getProcessedCommand();
+
+    Iterator<byte[]> argIterator = commandElements.iterator();
+    // Skip command and destination key
+    argIterator.next();
+    argIterator.next();
+
+    long numKeys;
+    try {
+      numKeys = Coder.bytesToLong(argIterator.next());
+    } catch (NumberFormatException nex) {
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    List<RedisKey> sourceKeys = new ArrayList<>();
+    List<Double> weights = new ArrayList<>();

Review comment:
       I applied Darrel's suggestion.

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java
##########
@@ -383,6 +387,44 @@ long zrevrank(byte[] member) {
     return null;
   }
 
+  long zunionstore(RegionProvider regionProvider, RedisKey key, List<RedisKey> 
sourceSets,
+      List<Double> weights, ZAggregator aggregator) {
+    for (int i = 0; i < sourceSets.size(); i++) {
+      RedisSortedSet set =
+          regionProvider.getTypedRedisData(REDIS_SORTED_SET, 
sourceSets.get(i), false);
+      if (set == NULL_REDIS_SORTED_SET) {
+        continue;
+      }
+      double weight = weights.get(i);
+
+      Iterator<AbstractOrderedSetEntry> scoreIterator =
+          set.scoreSet.getIndexRange(0, Integer.MAX_VALUE, false);
+      while (scoreIterator.hasNext()) {
+        OrderedSetEntry entry = (OrderedSetEntry) scoreIterator.next();

Review comment:
       Done.

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java
##########
@@ -383,6 +387,44 @@ long zrevrank(byte[] member) {
     return null;
   }
 
+  long zunionstore(RegionProvider regionProvider, RedisKey key, List<RedisKey> 
sourceSets,
+      List<Double> weights, ZAggregator aggregator) {
+    for (int i = 0; i < sourceSets.size(); i++) {
+      RedisSortedSet set =
+          regionProvider.getTypedRedisData(REDIS_SORTED_SET, 
sourceSets.get(i), false);
+      if (set == NULL_REDIS_SORTED_SET) {
+        continue;
+      }
+      double weight = weights.get(i);
+
+      Iterator<AbstractOrderedSetEntry> scoreIterator =
+          set.scoreSet.getIndexRange(0, Integer.MAX_VALUE, false);
+      while (scoreIterator.hasNext()) {
+        OrderedSetEntry entry = (OrderedSetEntry) scoreIterator.next();
+        OrderedSetEntry existingValue = members.get(entry.member);
+        if (existingValue == null) {
+          byte[] score;
+          if (weight == 1) {

Review comment:
       I'm very reluctant to add premature optimizations especially for corner 
cases.

##########
File path: 
geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/sortedset/AbstractZUnionStoreIntegrationTest.java
##########
@@ -0,0 +1,447 @@
+/*
+ * 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.geode.redis.internal.executor.sortedset;
+
+import static 
org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+import static 
org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS;
+import static 
org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import redis.clients.jedis.HostAndPort;
+import redis.clients.jedis.JedisCluster;
+import redis.clients.jedis.Protocol;
+import redis.clients.jedis.Tuple;
+import redis.clients.jedis.ZParams;
+
+import org.apache.geode.redis.ConcurrentLoopingThreads;
+import org.apache.geode.redis.RedisIntegrationTest;
+import org.apache.geode.redis.internal.RedisConstants;
+
+public abstract class AbstractZUnionStoreIntegrationTest implements 
RedisIntegrationTest {

Review comment:
       Good call out. This has led to the finding that Redis and Java don't 
produce the same result when multiplying infinity by zero (for example: 
`Double.POSITIVE_INFINITY * 0`). Redis will produce zero, but Java produces NaN.

##########
File path: 
geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/sortedset/AbstractZUnionStoreIntegrationTest.java
##########
@@ -0,0 +1,447 @@
+/*
+ * 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.geode.redis.internal.executor.sortedset;
+
+import static 
org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+import static 
org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS;
+import static 
org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import redis.clients.jedis.HostAndPort;
+import redis.clients.jedis.JedisCluster;
+import redis.clients.jedis.Protocol;
+import redis.clients.jedis.Tuple;
+import redis.clients.jedis.ZParams;
+
+import org.apache.geode.redis.ConcurrentLoopingThreads;
+import org.apache.geode.redis.RedisIntegrationTest;
+import org.apache.geode.redis.internal.RedisConstants;
+
+public abstract class AbstractZUnionStoreIntegrationTest implements 
RedisIntegrationTest {
+
+  private static final String NEW_SET = "{user1}new";
+  private static final String SORTED_SET_KEY1 = "{user1}sset1";
+  private static final String SORTED_SET_KEY2 = "{user1}sset2";
+  private static final String SORTED_SET_KEY3 = "{user1}sset3";
+
+  private JedisCluster jedis;
+
+  @Before
+  public void setUp() {
+    jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), 
REDIS_CLIENT_TIMEOUT);
+  }
+
+  @After
+  public void tearDown() {
+    flushAll();
+    jedis.close();
+  }
+
+  @Test
+  public void shouldError_givenWrongKeyType() {
+    final String NEW_KEY = "{user1}new";

Review comment:
       Done

##########
File path: 
geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/sortedset/AbstractZUnionStoreIntegrationTest.java
##########
@@ -0,0 +1,447 @@
+/*
+ * 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.geode.redis.internal.executor.sortedset;
+
+import static 
org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+import static 
org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS;
+import static 
org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import redis.clients.jedis.HostAndPort;
+import redis.clients.jedis.JedisCluster;
+import redis.clients.jedis.Protocol;
+import redis.clients.jedis.Tuple;
+import redis.clients.jedis.ZParams;
+
+import org.apache.geode.redis.ConcurrentLoopingThreads;
+import org.apache.geode.redis.RedisIntegrationTest;
+import org.apache.geode.redis.internal.RedisConstants;
+
+public abstract class AbstractZUnionStoreIntegrationTest implements 
RedisIntegrationTest {
+
+  private static final String NEW_SET = "{user1}new";
+  private static final String SORTED_SET_KEY1 = "{user1}sset1";
+  private static final String SORTED_SET_KEY2 = "{user1}sset2";
+  private static final String SORTED_SET_KEY3 = "{user1}sset3";
+
+  private JedisCluster jedis;
+
+  @Before
+  public void setUp() {
+    jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), 
REDIS_CLIENT_TIMEOUT);
+  }
+
+  @After
+  public void tearDown() {
+    flushAll();
+    jedis.close();
+  }
+
+  @Test
+  public void shouldError_givenWrongKeyType() {
+    final String NEW_KEY = "{user1}new";
+    final String STRING_KEY = "{user1}stringKey";
+    jedis.set(STRING_KEY, "value");
+    assertThatThrownBy(
+        () -> jedis.sendCommand(NEW_KEY, Protocol.Command.ZUNIONSTORE, 
NEW_KEY, "2", STRING_KEY,
+            SORTED_SET_KEY1))
+                .hasMessage("WRONGTYPE " + RedisConstants.ERROR_WRONG_TYPE);
+  }
+
+  @Test
+  public void shouldError_givenSetsCrossSlots() {
+    final String NEW_KEY = "{user1}new";
+    final String WRONG_KEY = "{user2}another";
+    assertThatThrownBy(
+        () -> jedis.sendCommand(NEW_KEY, Protocol.Command.ZUNIONSTORE, 
NEW_KEY, "2", WRONG_KEY,
+            SORTED_SET_KEY1))
+                .hasMessage("CROSSSLOT " + RedisConstants.ERROR_WRONG_SLOT);
+  }
+
+  @Test
+  public void shouldError_givenTooFewArguments() {
+    assertAtLeastNArgs(jedis, Protocol.Command.ZUNIONSTORE, 3);
+  }
+
+  @Test
+  public void shouldError_givenNumkeysTooLarge() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, 
NEW_SET, "2",
+            SORTED_SET_KEY1))
+                .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX);
+  }
+
+  @Test
+  public void shouldError_givenNumkeysTooSmall() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, 
NEW_SET, "1",
+            SORTED_SET_KEY1, SORTED_SET_KEY2))
+                .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX);
+  }
+
+  @Test
+  public void shouldError_givenTooManyWeights() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, 
NEW_SET, "1",
+            SORTED_SET_KEY1, "WEIGHTS", "2", "3"))
+                .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX);
+  }
+
+  @Test
+  public void shouldError_givenTooFewWeights() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, 
NEW_SET, "2",
+            SORTED_SET_KEY1, SORTED_SET_KEY2, "WEIGHTS", "1"))
+                .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX);
+  }
+
+  @Test
+  public void shouldError_givenWeightNotANumber() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, 
NEW_SET, "1",
+            SORTED_SET_KEY1, "WEIGHTS", "not-a-number"))
+                .hasMessage("ERR " + RedisConstants.ERROR_WEIGHT_NOT_A_FLOAT);
+  }
+
+  @Test
+  public void shouldError_givenWeightsWithoutAnyValues() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, 
NEW_SET, "1",
+            SORTED_SET_KEY1, "WEIGHTS"))
+                .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX);
+  }
+
+  @Test
+  public void shouldError_givenMultipleWeightKeywords() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, 
NEW_SET, "1",
+            SORTED_SET_KEY1, "WEIGHT", "1.0", "WEIGHT", "2.0"))
+                .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX);
+  }
+
+  @Test
+  public void shouldError_givenUnknownAggregate() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, 
NEW_SET, "1",
+            SORTED_SET_KEY1, "AGGREGATE", "UNKNOWN", "WEIGHTS", "1"))
+                .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX);
+  }
+
+  @Test
+  public void shouldError_givenAggregateKeywordWithoutValue() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, 
NEW_SET, "1",
+            SORTED_SET_KEY1, "AGGREGATE"))
+                .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX);
+  }
+
+  @Test
+  public void shouldError_givenMultipleAggregates() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, 
NEW_SET, "1",
+            SORTED_SET_KEY1, "WEIGHTS", "1", "AGGREGATE", "SUM", "MIN"))
+                .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX);
+  }
+
+  @Test
+  public void shouldUnionize_givenASingleSet() {
+    Map<String, Double> scores = makeScoreMap(10, x -> (double) x);
+    Set<Tuple> expectedResults = convertToTuples(scores, (i, x) -> x);
+    jedis.zadd(SORTED_SET_KEY1, scores);
+
+    assertThat(jedis.zunionstore(NEW_SET, SORTED_SET_KEY1)).isEqualTo(10);
+
+    Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void shouldUnionize_givenOneSetDoesNotExist() {
+    Map<String, Double> scores = makeScoreMap(10, x -> (double) x);
+    Set<Tuple> expectedResults = convertToTuples(scores, (i, x) -> x);
+    jedis.zadd(SORTED_SET_KEY1, scores);
+
+    jedis.zunionstore(NEW_SET, SORTED_SET_KEY1, SORTED_SET_KEY2);
+
+    Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void shouldUnionize_givenWeight() {
+    Map<String, Double> scores = makeScoreMap(10, x -> (double) x);
+    Set<Tuple> expectedResults = convertToTuples(scores, (i, x) -> x * 1.5);
+    jedis.zadd(SORTED_SET_KEY1, scores);
+
+    jedis.zunionstore(SORTED_SET_KEY1, new ZParams().weights(1.5), 
SORTED_SET_KEY1);
+
+    Set<Tuple> results = jedis.zrangeWithScores(SORTED_SET_KEY1, 0, 10);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void 
shouldUnionizeWithWeightAndDefaultAggregate_givenMultipleSetsWithWeights() {
+    Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x);
+    jedis.zadd(SORTED_SET_KEY1, scores1);
+
+    Map<String, Double> scores2 = makeScoreMap(10, x -> (double) (9 - x));
+    jedis.zadd(SORTED_SET_KEY2, scores2);
+
+    Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> (x * 2.0) 
+ ((9 - x) * 1.5));
+
+    jedis.zunionstore(NEW_SET, new ZParams().weights(2.0, 1.5), 
SORTED_SET_KEY1, SORTED_SET_KEY2);
+
+    Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void shouldUnionize_givenMinAggregate() {
+    Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x);
+    jedis.zadd(SORTED_SET_KEY1, scores1);
+
+    Map<String, Double> scores2 = makeScoreMap(10, x -> 0D);
+    jedis.zadd(SORTED_SET_KEY2, scores2);
+
+    Set<Tuple> expectedResults = convertToTuples(scores2, (i, x) -> x);
+
+    jedis.zunionstore(NEW_SET, new ZParams().aggregate(ZParams.Aggregate.MIN),
+        SORTED_SET_KEY1, SORTED_SET_KEY2);
+
+    Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void shouldUnionize_givenMaxAggregate() {
+    Map<String, Double> scores1 = makeScoreMap(10, x -> (double) ((x % 2 == 0) 
? 0 : x));
+    jedis.zadd(SORTED_SET_KEY1, scores1);
+
+    Map<String, Double> scores2 = makeScoreMap(10, x -> (double) ((x % 2 == 0) 
? x : 0));
+    jedis.zadd(SORTED_SET_KEY2, scores2);
+
+    Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> (double) 
i);
+
+    jedis.zunionstore(NEW_SET, new ZParams().aggregate(ZParams.Aggregate.MAX),
+        SORTED_SET_KEY1, SORTED_SET_KEY2);
+
+    Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void 
shouldUnionizeUsingLastAggregate_givenMultipleAggregateKeywords() {
+    Map<String, Double> scores1 = makeScoreMap(10, x -> (double) 0);
+    jedis.zadd(SORTED_SET_KEY1, scores1);
+
+    Map<String, Double> scores2 = makeScoreMap(10, x -> (double) 1);
+    jedis.zadd(SORTED_SET_KEY2, scores2);
+
+    Set<Tuple> expectedResults = convertToTuples(scores2, (i, x) -> x);
+
+    jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, NEW_SET, "2",
+        SORTED_SET_KEY1, SORTED_SET_KEY2, "AGGREGATE", "MIN", "AGGREGATE", 
"MAX");
+
+    Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void shouldUnionize_givenMaxAggregateAndMultipleWeights() {
+    Map<String, Double> scores1 = makeScoreMap(10, x -> (double) ((x % 2 == 0) 
? 0 : x));
+    jedis.zadd(SORTED_SET_KEY1, scores1);
+
+    Map<String, Double> scores2 = makeScoreMap(10, x -> (double) ((x % 2 == 0) 
? x : 0));
+    jedis.zadd(SORTED_SET_KEY2, scores2);
+
+    Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> (double) 
(i * 2));
+
+    jedis.zunionstore(NEW_SET, new 
ZParams().aggregate(ZParams.Aggregate.MAX).weights(2, 2),
+        SORTED_SET_KEY1, SORTED_SET_KEY2);
+
+    Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void shouldUnionize_givenSumAggregateAndMultipleSets() {
+    Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x);
+    jedis.zadd(SORTED_SET_KEY1, scores1);
+
+    Map<String, Double> scores2 = makeScoreMap(10, x -> (double) (x * 2));
+    jedis.zadd(SORTED_SET_KEY2, scores2);
+
+    Map<String, Double> scores3 = makeScoreMap(10, x -> (double) (x * 3));
+    jedis.zadd(SORTED_SET_KEY3, scores3);
+
+    Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> x * 6);
+
+    jedis.zunionstore(NEW_SET, new ZParams().aggregate(ZParams.Aggregate.SUM),
+        SORTED_SET_KEY1, SORTED_SET_KEY2, SORTED_SET_KEY3);
+
+    Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void shouldUnionize_givenSetsDoNotOverlap() {
+    Map<String, Double> scores1 = makeScoreMap(0, 2, 10, x -> (double) x);
+    jedis.zadd(SORTED_SET_KEY1, scores1);
+
+    Map<String, Double> scores2 = makeScoreMap(1, 2, 10, x -> (double) x);
+    jedis.zadd(SORTED_SET_KEY2, scores2);
+
+    Set<Tuple> expectedResults =
+        convertToTuples(makeScoreMap(0, 1, 20, x -> (double) x), (i, x) -> x);
+
+    jedis.zunionstore(NEW_SET, SORTED_SET_KEY1, SORTED_SET_KEY2);
+
+    Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 20);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void shouldUnionize_givenSetsPartiallyOverlap() {
+    Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x);
+    jedis.zadd(SORTED_SET_KEY1, scores1);
+
+    Map<String, Double> scores2 = makeScoreMap(5, 1, 10, x -> (double) (x < 10 
? x : x * 2));
+    jedis.zadd(SORTED_SET_KEY2, scores2);
+
+    Set<Tuple> expectedResults = convertToTuples(makeScoreMap(0, 1, 15, x -> 
(double) x),
+        (i, x) -> (double) (i < 5 ? i : i * 2));
+
+    jedis.zunionstore(NEW_SET, SORTED_SET_KEY1, SORTED_SET_KEY2);
+
+    Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 20);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void ensureWeightsAreAppliedBeforeAggregation() {
+    Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x * 5);
+    jedis.zadd(SORTED_SET_KEY1, scores1);
+
+    Map<String, Double> scores2 = makeScoreMap(10, x -> (double) x);
+    jedis.zadd(SORTED_SET_KEY2, scores2);
+
+    Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> (double) 
(i * 10));
+
+    jedis.zunionstore(SORTED_SET_KEY1,
+        new ZParams().weights(1, 10).aggregate(ZParams.Aggregate.MAX), 
SORTED_SET_KEY1,
+        SORTED_SET_KEY2);
+
+    Set<Tuple> results = jedis.zrangeWithScores(SORTED_SET_KEY1, 0, 20);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void shouldUnionize_whenTargetExistsAndSetsAreDuplicated() {
+    Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x);
+    jedis.zadd(SORTED_SET_KEY1, scores1);
+
+    Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> x * 2);
+
+    // Default aggregation is SUM
+    jedis.zunionstore(SORTED_SET_KEY1, SORTED_SET_KEY1, SORTED_SET_KEY1);
+
+    Set<Tuple> results = jedis.zrangeWithScores(SORTED_SET_KEY1, 0, 10);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void shouldPreserveSet_givenDestinationAndSourceAreTheSame() {
+    Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x);
+    jedis.zadd(SORTED_SET_KEY1, scores1);
+
+    Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> x);
+
+    jedis.zunionstore(SORTED_SET_KEY1, SORTED_SET_KEY1);
+
+    Set<Tuple> results = jedis.zrangeWithScores(SORTED_SET_KEY1, 0, 10);
+
+    assertThat(results).containsExactlyElementsOf(expectedResults);
+  }
+
+  @Test
+  public void ensureSetConsistency_andNoExceptions_whenRunningConcurrently() {
+    int scoreCount = 1000;
+    jedis.zadd("{A}ones", makeScoreMap(scoreCount, x -> 1D));

Review comment:
       Honestly, I'd prefer not to. Since it's constrained to this one test 
method it feels like unnecessary clutter.

##########
File path: 
geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/set/AbstractSUnionIntegrationTest.java
##########
@@ -198,4 +199,25 @@ public void testConcurrentSUnionStore() throws 
InterruptedException {
     assertThat(jedis.smembers("{user1}master").toArray())
         .containsExactlyInAnyOrder(masterSet.toArray());
   }
+
+  @Test
+  public void doesNotThrowExceptions_whenConcurrentSaddAndSunionExecute() {

Review comment:
       Yes

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZUnionStoreExecutor.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.geode.redis.internal.executor.sortedset;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX;
+import static 
org.apache.geode.redis.internal.RedisConstants.ERROR_WEIGHT_NOT_A_FLOAT;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_SLOT;
+import static org.apache.geode.redis.internal.netty.Coder.toUpperCaseBytes;
+import static 
org.apache.geode.redis.internal.netty.StringBytesGlossary.bAGGREGATE;
+import static 
org.apache.geode.redis.internal.netty.StringBytesGlossary.bWEIGHTS;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.executor.AbstractExecutor;
+import org.apache.geode.redis.internal.executor.RedisResponse;
+import org.apache.geode.redis.internal.netty.Coder;
+import org.apache.geode.redis.internal.netty.Command;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+
+public class ZUnionStoreExecutor extends AbstractExecutor {
+
+  @Override
+  public RedisResponse executeCommand(Command command, ExecutionHandlerContext 
context) {
+    List<byte[]> commandElements = command.getProcessedCommand();
+
+    Iterator<byte[]> argIterator = commandElements.iterator();
+    // Skip command and destination key
+    argIterator.next();
+    argIterator.next();
+
+    long numKeys;
+    try {
+      numKeys = Coder.bytesToLong(argIterator.next());
+    } catch (NumberFormatException nex) {
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    List<RedisKey> sourceKeys = new ArrayList<>();
+    List<Double> weights = new ArrayList<>();
+    ZAggregator aggregator = ZAggregator.SUM;
+
+    while (argIterator.hasNext()) {
+      byte[] arg = argIterator.next();
+
+      if (sourceKeys.size() < numKeys) {
+        sourceKeys.add(new RedisKey(arg));
+        continue;
+      }
+
+      arg = toUpperCaseBytes(arg);
+      if (Arrays.equals(arg, bWEIGHTS)) {
+        if (weights.size() > 0) {

Review comment:
       Done

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZUnionStoreExecutor.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.geode.redis.internal.executor.sortedset;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX;
+import static 
org.apache.geode.redis.internal.RedisConstants.ERROR_WEIGHT_NOT_A_FLOAT;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_SLOT;
+import static org.apache.geode.redis.internal.netty.Coder.toUpperCaseBytes;
+import static 
org.apache.geode.redis.internal.netty.StringBytesGlossary.bAGGREGATE;
+import static 
org.apache.geode.redis.internal.netty.StringBytesGlossary.bWEIGHTS;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.executor.AbstractExecutor;
+import org.apache.geode.redis.internal.executor.RedisResponse;
+import org.apache.geode.redis.internal.netty.Coder;
+import org.apache.geode.redis.internal.netty.Command;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+
+public class ZUnionStoreExecutor extends AbstractExecutor {
+
+  @Override
+  public RedisResponse executeCommand(Command command, ExecutionHandlerContext 
context) {
+    List<byte[]> commandElements = command.getProcessedCommand();
+
+    Iterator<byte[]> argIterator = commandElements.iterator();
+    // Skip command and destination key
+    argIterator.next();
+    argIterator.next();
+
+    long numKeys;
+    try {
+      numKeys = Coder.bytesToLong(argIterator.next());
+    } catch (NumberFormatException nex) {
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    List<RedisKey> sourceKeys = new ArrayList<>();
+    List<Double> weights = new ArrayList<>();
+    ZAggregator aggregator = ZAggregator.SUM;
+
+    while (argIterator.hasNext()) {
+      byte[] arg = argIterator.next();
+
+      if (sourceKeys.size() < numKeys) {
+        sourceKeys.add(new RedisKey(arg));
+        continue;
+      }
+
+      arg = toUpperCaseBytes(arg);
+      if (Arrays.equals(arg, bWEIGHTS)) {
+        if (weights.size() > 0) {
+          return RedisResponse.error(ERROR_SYNTAX);
+        }
+        for (int i = 0; i < numKeys; i++) {
+          if (!argIterator.hasNext()) {
+            return RedisResponse.error(ERROR_SYNTAX);
+          }
+          try {
+            weights.add(Coder.bytesToDouble(argIterator.next()));
+          } catch (NumberFormatException nex) {
+            return RedisResponse.error(ERROR_WEIGHT_NOT_A_FLOAT);
+          }
+        }
+        continue;
+      }
+
+      if (Arrays.equals(arg, bAGGREGATE)) {
+        try {
+          aggregator = 
ZAggregator.valueOf(Coder.bytesToString(argIterator.next()));
+        } catch (IllegalArgumentException | NoSuchElementException e) {
+          return RedisResponse.error(ERROR_SYNTAX);
+        }
+        continue;
+      }
+
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    if (sourceKeys.size() != numKeys) {
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    if (weights.size() == 0) {

Review comment:
       Done

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZUnionStoreExecutor.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.geode.redis.internal.executor.sortedset;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX;
+import static 
org.apache.geode.redis.internal.RedisConstants.ERROR_WEIGHT_NOT_A_FLOAT;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_SLOT;
+import static org.apache.geode.redis.internal.netty.Coder.toUpperCaseBytes;
+import static 
org.apache.geode.redis.internal.netty.StringBytesGlossary.bAGGREGATE;
+import static 
org.apache.geode.redis.internal.netty.StringBytesGlossary.bWEIGHTS;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.executor.AbstractExecutor;
+import org.apache.geode.redis.internal.executor.RedisResponse;
+import org.apache.geode.redis.internal.netty.Coder;
+import org.apache.geode.redis.internal.netty.Command;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+
+public class ZUnionStoreExecutor extends AbstractExecutor {
+
+  @Override
+  public RedisResponse executeCommand(Command command, ExecutionHandlerContext 
context) {
+    List<byte[]> commandElements = command.getProcessedCommand();
+
+    Iterator<byte[]> argIterator = commandElements.iterator();
+    // Skip command and destination key
+    argIterator.next();
+    argIterator.next();
+
+    long numKeys;
+    try {
+      numKeys = Coder.bytesToLong(argIterator.next());
+    } catch (NumberFormatException nex) {
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    List<RedisKey> sourceKeys = new ArrayList<>();
+    List<Double> weights = new ArrayList<>();
+    ZAggregator aggregator = ZAggregator.SUM;
+
+    while (argIterator.hasNext()) {
+      byte[] arg = argIterator.next();
+
+      if (sourceKeys.size() < numKeys) {
+        sourceKeys.add(new RedisKey(arg));
+        continue;
+      }
+
+      arg = toUpperCaseBytes(arg);
+      if (Arrays.equals(arg, bWEIGHTS)) {
+        if (weights.size() > 0) {
+          return RedisResponse.error(ERROR_SYNTAX);
+        }
+        for (int i = 0; i < numKeys; i++) {
+          if (!argIterator.hasNext()) {
+            return RedisResponse.error(ERROR_SYNTAX);
+          }
+          try {
+            weights.add(Coder.bytesToDouble(argIterator.next()));
+          } catch (NumberFormatException nex) {
+            return RedisResponse.error(ERROR_WEIGHT_NOT_A_FLOAT);
+          }
+        }
+        continue;
+      }
+
+      if (Arrays.equals(arg, bAGGREGATE)) {
+        try {
+          aggregator = 
ZAggregator.valueOf(Coder.bytesToString(argIterator.next()));
+        } catch (IllegalArgumentException | NoSuchElementException e) {
+          return RedisResponse.error(ERROR_SYNTAX);
+        }
+        continue;
+      }
+
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    if (sourceKeys.size() != numKeys) {
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    if (weights.size() == 0) {
+      for (int i = 0; i < numKeys; i++) {

Review comment:
       I've removed it in favor of introducing a `ZKeyWeight` class... more 
below....

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/netty/StringBytesGlossary.java
##########
@@ -163,6 +166,18 @@
   @MakeImmutable
   public static final byte[] bINCR = stringToBytes("INCR");
 
+  // ZUnionStoreExecutor
+  @MakeImmutable
+  public static final byte[] bWEIGHTS = stringToBytes("WEIGHTS");
+  @MakeImmutable
+  public static final byte[] bAGGREGATE = stringToBytes("AGGREGATE");
+  @MakeImmutable
+  public static final byte[] bSUM = stringToBytes("SUM");

Review comment:
       Done

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/services/SynchronizedStripedCoordinator.java
##########
@@ -51,6 +52,17 @@ public SynchronizedStripedCoordinator(int concurrencyLevel) {
     }
   }
 
+  @Override
+  public <T> T execute(List<Object> stripeIds, int index, Callable<T> 
callable) {

Review comment:
       Initially it needed this otherwise these signatures would be ambiguous:
   ```
     public <T> T execute(Object stripeId, Callable<T> callable)
     public <T> T execute(List<Object> stripeIds, Callable<T> callable)
   ```
   I've applied Donal's suggestion and made the locked object explicitly be a 
`RedisKey`.

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/services/SynchronizedStripedCoordinator.java
##########
@@ -51,6 +52,17 @@ public SynchronizedStripedCoordinator(int concurrencyLevel) {
     }
   }
 
+  @Override
+  public <T> T execute(List<Object> stripeIds, int index, Callable<T> 
callable) {
+    if (index + 1 == stripeIds.size()) {
+      return execute(stripeIds.get(index), callable);
+    }
+
+    synchronized (getSync(stripeIds.get(index))) {

Review comment:
       Yes, I agree. I think it's a good idea to apply your suggestion, but I'd 
prefer to do it in another PR if you don't mind.

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZAggregator.java
##########
@@ -0,0 +1,39 @@
+/*
+ * 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.geode.redis.internal.executor.sortedset;
+
+import java.util.function.BiFunction;
+
+/**
+ * Enums representing aggregation functions used in {@link 
ZUnionStoreExecutor} and
+ * {@link ZinterStoreExecutor}.
+ */
+public enum ZAggregator {
+
+  SUM(Double::sum),
+  MIN(Math::min),
+  MAX(Math::max);
+
+  private BiFunction<Double, Double, Double> function;

Review comment:
       Done

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSetCommandsFunctionExecutor.java
##########
@@ -126,4 +129,20 @@ public long zrevrank(RedisKey key, byte[] member) {
   public byte[] zscore(RedisKey key, byte[] member) {
     return stripedExecute(key, () -> getRedisSortedSet(key, 
true).zscore(member));
   }
+
+  @Override
+  public long zunionstore(RedisKey key, List<RedisKey> sourceSets, 
List<Double> weights,
+      ZAggregator aggregator) {
+    List<Object> keysToLock = new ArrayList<>(sourceSets);
+    keysToLock.add(key);
+    return stripedExecute(key, keysToLock,
+        () -> {
+          if (!getRegionProvider().getSlotAdvisor().isLocal(key)) {

Review comment:
       Done

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java
##########
@@ -383,6 +387,44 @@ long zrevrank(byte[] member) {
     return null;
   }
 
+  long zunionstore(RegionProvider regionProvider, RedisKey key, List<RedisKey> 
sourceSets,

Review comment:
       OK, so I introduced a `ZKeyWeight` class that does this. It's plumbed 
through from the executor, but the executor code still creates 2 separate lists 
of source sets and weights and then combines those into a `List<ZKeyWeight>`. I 
couldn't come up with an elegant way to parse the argument list directly into 
this structure. Suggestions are welcome!

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java
##########
@@ -383,6 +387,44 @@ long zrevrank(byte[] member) {
     return null;
   }
 
+  long zunionstore(RegionProvider regionProvider, RedisKey key, List<RedisKey> 
sourceSets,
+      List<Double> weights, ZAggregator aggregator) {
+    for (int i = 0; i < sourceSets.size(); i++) {
+      RedisSortedSet set =
+          regionProvider.getTypedRedisData(REDIS_SORTED_SET, 
sourceSets.get(i), false);
+      if (set == NULL_REDIS_SORTED_SET) {
+        continue;
+      }
+      double weight = weights.get(i);
+
+      Iterator<AbstractOrderedSetEntry> scoreIterator =

Review comment:
       Yes, I thought of that but, as I mentioned in previous comments, I'm 
reluctant to add code to optimize for corner cases.

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZUnionStoreExecutor.java
##########
@@ -36,6 +36,11 @@
 
 public class ZUnionStoreExecutor extends AbstractExecutor {
 
+  /**
+   * The position in the command array where the source set keys start.
+   */
+  private static final int SOURCE_KEY_OFFSET = 3;

Review comment:
       Doh! - Too much ^Z after some experimental code.

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZUnionStoreExecutor.java
##########
@@ -52,6 +57,11 @@ public RedisResponse executeCommand(Command command, 
ExecutionHandlerContext con
       return RedisResponse.error(ERROR_SYNTAX);
     }
 
+    // Rough validation so that we can use numKeys to initialize the array 
sizes below.

Review comment:
       Also too much ^Z

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/netty/StringBytesGlossary.java
##########
@@ -198,6 +192,8 @@
   public static final byte bPLUS = SIMPLE_STRING_ID; // +
   public static final byte bMINUS = ERROR_ID; // -
 
+  public static final byte[] bZERO = stringToBytes("0");

Review comment:
       `NUMBER_0_BYTE` could be replaced with `bZERO[0]` but I think that will 
reduce the semantics of how it reads now.




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


Reply via email to