aweisberg commented on code in PR #2339:
URL: https://github.com/apache/cassandra/pull/2339#discussion_r1205898037


##########
src/java/org/apache/cassandra/service/accord/CommandsForRanges.java:
##########
@@ -0,0 +1,505 @@
+/*
+ * 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.cassandra.service.accord;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+import javax.annotation.Nullable;
+
+import com.google.common.collect.AbstractIterator;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSortedMap;
+
+import accord.api.Key;
+import accord.api.RoutingKey;
+import accord.impl.CommandTimeseries;
+import accord.impl.CommandTimeseriesHolder;
+import accord.local.Command;
+import accord.local.PreLoadContext;
+import accord.local.SafeCommand;
+import accord.local.SafeCommandStore;
+import accord.local.SaveStatus;
+import accord.primitives.AbstractKeys;
+import accord.primitives.PartialDeps;
+import accord.primitives.Range;
+import accord.primitives.Ranges;
+import accord.primitives.Routable;
+import accord.primitives.RoutableKey;
+import accord.primitives.Seekable;
+import accord.primitives.Seekables;
+import accord.primitives.Timestamp;
+import accord.primitives.TxnId;
+import accord.utils.Invariants;
+import org.apache.cassandra.service.accord.api.AccordRoutingKey;
+import org.apache.cassandra.service.accord.api.AccordRoutingKey.TokenKey;
+import org.apache.cassandra.service.accord.api.PartitionKey;
+import org.apache.cassandra.utils.Interval;
+import org.apache.cassandra.utils.IntervalTree;
+
+public class CommandsForRanges
+{
+    public enum TxnType
+    {
+        UNKNOWN, LOCAL, REMOTE;
+
+        private boolean isSafeToMix(TxnType other)
+        {
+            if (this == UNKNOWN || other == UNKNOWN) return true;
+            return this == other;
+        }
+    }
+
+    public static final class RangeCommandSummary
+    {
+        public final TxnId txnId;
+        public final Ranges ranges;
+        public final SaveStatus status;
+        public final @Nullable Timestamp executeAt;
+        public final List<TxnId> deps;
+
+        RangeCommandSummary(TxnId txnId, Ranges ranges, SaveStatus status, 
@Nullable Timestamp executeAt, List<TxnId> deps)
+        {
+            this.txnId = txnId;
+            this.ranges = ranges;
+            this.status = status;
+            this.executeAt = executeAt;
+            this.deps = deps;
+        }
+
+        public boolean equalsDeep(RangeCommandSummary other)
+        {
+            return Objects.equals(txnId, other.txnId)
+                   && Objects.equals(ranges, other.ranges)
+                   && Objects.equals(status, other.status)
+                   && Objects.equals(executeAt, other.executeAt)
+                   && Objects.equals(deps, other.deps);
+        }
+
+        @Override
+        public boolean equals(Object o)
+        {
+            if (this == o) return true;
+            if (o == null || getClass() != o.getClass()) return false;
+            RangeCommandSummary that = (RangeCommandSummary) o;
+            return txnId.equals(that.txnId);
+        }
+
+        @Override
+        public int hashCode()
+        {
+            return Objects.hash(txnId);
+        }
+
+        @Override
+        public String toString()
+        {
+            return "RangeCommandSummary{" +
+                   "txnId=" + txnId +
+                   ", status=" + status +
+                   ", ranges=" + ranges +
+                   '}';
+        }
+
+        public RangeCommandSummary withRanges(Ranges ranges, BiFunction<? 
super Ranges, ? super Ranges, ? extends Ranges> remappingFunction)
+        {
+            return new RangeCommandSummary(txnId, 
remappingFunction.apply(this.ranges, ranges), status, executeAt, deps);
+        }
+    }
+
+    private enum RangeCommandSummaryLoader implements 
CommandTimeseries.CommandLoader<RangeCommandSummary>
+    {
+        INSTANCE;
+
+        @Override
+        public RangeCommandSummary saveForCFK(Command command)
+        {
+            //TODO split write from read?
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public TxnId txnId(RangeCommandSummary data)
+        {
+            return data.txnId;
+        }
+
+        @Override
+        public Timestamp executeAt(RangeCommandSummary data)
+        {
+            return data.executeAt;
+        }
+
+        @Override
+        public SaveStatus saveStatus(RangeCommandSummary data)
+        {
+            return data.status;
+        }
+
+        @Override
+        public List<TxnId> depsIds(RangeCommandSummary data)
+        {
+            return data.deps;
+        }
+    }
+
+    public static abstract class AbstractBuilder<T extends AbstractBuilder<T>>
+    {
+        protected final Set<TxnId> localTxns = new HashSet<>();
+        protected final TreeMap<TxnId, RangeCommandSummary> txnToRange = new 
TreeMap<>();
+        protected final IntervalTree.Builder<RoutableKey, RangeCommandSummary, 
Interval<RoutableKey, RangeCommandSummary>> rangeToTxn = new 
IntervalTree.Builder<>();
+
+        public TxnType type(TxnId txnId)
+        {
+            if (!txnToRange.containsKey(txnId)) return TxnType.UNKNOWN;
+            return localTxns.contains(txnId) ? TxnType.LOCAL : TxnType.REMOTE;
+        }
+
+        public T put(TxnId txnId, Ranges ranges, SaveStatus status, Timestamp 
execteAt, List<TxnId> dependsOn)
+        {
+            remove(txnId);
+            RangeCommandSummary summary = new RangeCommandSummary(txnId, 
ranges, status, execteAt, dependsOn);
+            localTxns.add(txnId);
+            txnToRange.put(txnId, summary);
+            addRanges(summary);
+            return (T) this;
+        }
+
+        private void addRanges(RangeCommandSummary summary)
+        {
+            for (Range range : summary.ranges)
+            {
+                rangeToTxn.add(Interval.create(normalize(range.start(), 
range.startInclusive(), true),
+                                               normalize(range.end(), 
range.endInclusive(), false),
+                                               summary));
+            }
+        }
+
+        public T putAll(CommandsForRanges other)
+        {
+            for (TxnId id : other.localCommands)
+            {
+                TxnType thisType = type(id);
+                TxnType otherType = other.type(id);
+                Invariants.checkArgument(thisType.isSafeToMix(otherType), 
"Attempted to add %s; expected %s but was %s", id, thisType, otherType);
+            }
+            localTxns.addAll(other.localCommands);
+            txnToRange.putAll(other.commandsToRanges);
+            // If "put" was called before for a txn present in "other", to 
respect the "put" semantics that update must
+            // be removed from "rangeToTxn" (as it got removed from 
"txnToRange").
+            // The expected common case is that this method is called on an 
empty builder, so the removeIf is off an
+            // empty list (aka no-op)
+            rangeToTxn.removeIf(data -> 
other.commandsToRanges.containsKey(data.txnId));
+            rangeToTxn.addAll(other.rangesToCommands);
+            return (T) this;
+        }
+
+        public T mergeRemote(TxnId txnId, Ranges ranges, BiFunction<? super 
Ranges, ? super Ranges, ? extends Ranges> remappingFunction)
+        {
+            Invariants.checkArgument(!localTxns.contains(txnId), "Attempted to 
merge remote txn %s, but this is a local txn", txnId);
+            // accord.impl.CommandTimeseries.mapReduce does the check on 
status and deps type, and NotWitnessed should match the semantics hard coded in 
InMemorySafeStore...
+            // in that store, the remote history is only ever included when 
minStauts == null and deps == ANY... but mapReduce sees 
accord.local.Status.KnownDeps.hasProposedOrDecidedDeps == false
+            // as a mis-match, so will be excluded... since NotWitnessed will 
return false it will only be included IFF deps = ANY.
+            // When it comes to the minStatus check, the current usage is 
"null", "Committed", "Accepted"... so NotWitnessed will only be included in the 
null case;
+            // the only subtle difference is if minStatus = NotWitnessed, this 
API will include these but InMemoryStore won't
+            RangeCommandSummary oldValue = txnToRange.get(txnId);
+            RangeCommandSummary newValue = oldValue == null ?
+                                           new RangeCommandSummary(txnId, 
ranges, SaveStatus.NotWitnessed, null, Collections.emptyList())
+                                           : oldValue.withRanges(ranges, 
remappingFunction);
+            if (newValue == null)
+            {
+                remove(txnId);
+            }
+            else if (!oldValue.equalsDeep(newValue))

Review Comment:
   When do we hit this case where oldValue is not null, but `newValue` has more 
information? Is it when we are a minority in some transaction that is missing 
information? Seems like if we have a local value we should never be loading 
remote info?
   
   My only concern here is that this reverts our knowledge of the range 
transaction to a less knowledgable state in some situation.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to