bdeggleston commented on code in PR #4192: URL: https://github.com/apache/cassandra/pull/4192#discussion_r2150893040
########## src/java/org/apache/cassandra/replication/CoordinatorLogOffsetsBuilder.java: ########## @@ -0,0 +1,121 @@ +/* + * 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.replication; + +import java.util.Iterator; +import java.util.Objects; + +import javax.annotation.concurrent.NotThreadSafe; + +import com.google.common.collect.Iterators; + +import org.agrona.collections.Long2ObjectHashMap; + +@NotThreadSafe +public class CoordinatorLogOffsetsBuilder +{ + private final Long2ObjectHashMap<Offsets.Mutable> ids; + + public CoordinatorLogOffsetsBuilder() + { + this.ids = new Long2ObjectHashMap<>(); + } + + public CoordinatorLogOffsetsBuilder(int size) + { + this.ids = new Long2ObjectHashMap<>(size, 0.9f); + } + + public CoordinatorLogOffsetsBuilder add(MutationId mutationId) + { + if (mutationId.isNone()) + return this; + ids.computeIfAbsent(mutationId.logId(), logId -> new Offsets.Mutable(new CoordinatorLogId(logId))) + .add(mutationId.offset()); + return this; + } + + public CoordinatorLogOffsetsBuilder addAll(CoordinatorLogOffsets<?> logOffsets) + { + for (long logId : logOffsets) + { + Offsets offsets = logOffsets.offsets(logId); + ids.computeIfAbsent(logId, log -> new Offsets.Mutable(new CoordinatorLogId(log))) + .addAll(offsets); + } + return this; + } + + public CoordinatorLogOffsetsBuilder addAll(Offsets.Immutable offsets) + { + ids.computeIfAbsent(offsets.logId().asLong(), log -> new Offsets.Mutable(new CoordinatorLogId(log))) + .addAll(offsets); + return this; + } + + public CoordinatorLogOffsets<Offsets.Immutable> build() + { + return new Wrapper(this); + } + + private static class Wrapper implements CoordinatorLogOffsets<Offsets.Immutable> Review Comment: The relationship between these 2 classes seems inverted. I think you'd want to have an immutable implementation of CoordinatorLogOffsets. That should have a static inner Builder class which emits an immutable collection ########## src/java/org/apache/cassandra/replication/CoordinatorLogOffsetsBuilder.java: ########## @@ -0,0 +1,121 @@ +/* + * 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.replication; + +import java.util.Iterator; +import java.util.Objects; + +import javax.annotation.concurrent.NotThreadSafe; + +import com.google.common.collect.Iterators; + +import org.agrona.collections.Long2ObjectHashMap; + +@NotThreadSafe +public class CoordinatorLogOffsetsBuilder +{ + private final Long2ObjectHashMap<Offsets.Mutable> ids; Review Comment: This should be `Offsets.Immutable.Builder`, that will let you convert the mutable builder into an immutable offsets object without doing any copying ########## src/java/org/apache/cassandra/replication/CoordinatorLogOffsets.java: ########## @@ -0,0 +1,87 @@ +/* + * 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.replication; + +import java.io.IOException; + +import org.apache.cassandra.io.IVersionedSerializer; +import org.apache.cassandra.io.sstable.metadata.StatsMetadata; +import org.apache.cassandra.io.util.DataInputPlus; +import org.apache.cassandra.io.util.DataOutputPlus; +import org.apache.cassandra.net.MessagingService; +import org.apache.cassandra.utils.vint.VIntCoding; + +/** + * Mutation ID offsets present in this SSTable for each coordinator log, to determine whether an SSTable is reconciled + * or not. + * <p> + * Note that peers may have reconciled all mutations included in an SSTable, but {@link StatsMetadata#repairedAt} is + * dependent on compaction timing, so "nodetool repair --validate" may report temporary disagreements on the repaired + * set. + * <p> + * A reference to this class should be treated as immutable. Do not cast to {@link CoordinatorLogOffsetsMap}. + * Iterable over {@link CoordinatorLogId}. + */ +public interface CoordinatorLogOffsets<OFFSETS extends Offsets> extends Iterable<Long> +{ + OFFSETS offsets(long logId); + int size(); + + IVersionedSerializer<CoordinatorLogOffsets<Offsets.Immutable>> serializer = new IVersionedSerializer<>() Review Comment: Since this only (de)serializes immutable versions of offsets, it should be part of the immutable offset implementation. ########## src/java/org/apache/cassandra/replication/CoordinatorLogOffsetsMap.java: ########## @@ -0,0 +1,205 @@ +/* + * 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.replication; + +import java.util.Iterator; +import java.util.Objects; +import java.util.concurrent.locks.ReentrantLock; + +import javax.annotation.Nullable; +import javax.annotation.concurrent.ThreadSafe; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Iterators; + +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.db.memtable.Memtable; +import org.apache.cassandra.db.memtable.SkipListMemtable; +import org.apache.cassandra.db.memtable.TrieMemtable; +import org.jctools.maps.NonBlockingHashMapLong; +import org.jctools.queues.MpscUnboundedArrayQueue; + +/** + * A replica can only receive writes from another replica it shares ranges with, and tracked writes are executed by + * coordinators, so this should contain up to (2*RF - 1) keys. + * <p> + * This is different from {@link Log2OffsetsMap} because it's focused on supporting fast, frequent updates from multiple + * threads at {@link Memtable#put}, and infrequent reads at {@link Memtable#getFlushSet}. + */ +@ThreadSafe +class CoordinatorLogOffsetsMap extends NonBlockingHashMapLong<CoordinatorLogOffsetsMap.Entry> implements MutableCoordinatorLogOffsets Review Comment: Can we rename this to something that implies it's special concurrent usage - NonBlockingCoordinatorLogOffsetsMap or something ########## src/java/org/apache/cassandra/replication/CoordinatorLogOffsets.java: ########## @@ -0,0 +1,87 @@ +/* + * 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.replication; + +import java.io.IOException; + +import org.apache.cassandra.io.IVersionedSerializer; +import org.apache.cassandra.io.sstable.metadata.StatsMetadata; +import org.apache.cassandra.io.util.DataInputPlus; +import org.apache.cassandra.io.util.DataOutputPlus; +import org.apache.cassandra.net.MessagingService; +import org.apache.cassandra.utils.vint.VIntCoding; + +/** + * Mutation ID offsets present in this SSTable for each coordinator log, to determine whether an SSTable is reconciled + * or not. + * <p> + * Note that peers may have reconciled all mutations included in an SSTable, but {@link StatsMetadata#repairedAt} is + * dependent on compaction timing, so "nodetool repair --validate" may report temporary disagreements on the repaired + * set. + * <p> + * A reference to this class should be treated as immutable. Do not cast to {@link CoordinatorLogOffsetsMap}. + * Iterable over {@link CoordinatorLogId}. + */ +public interface CoordinatorLogOffsets<OFFSETS extends Offsets> extends Iterable<Long> Review Comment: `OFFSETS` -> `O` would be more consistent with the rest of the repo -- 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]

