jberragan commented on code in PR #99: URL: https://github.com/apache/cassandra-analytics/pull/99#discussion_r2001356175
########## cassandra-analytics-cdc-sidecar/src/main/java/org/apache/cassandra/cdc/sidecar/SidecarStatePersister.java: ########## @@ -0,0 +1,434 @@ +/* + * 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.cdc.sidecar; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.google.common.annotations.VisibleForTesting; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.datastax.driver.core.ResultSetFuture; +import com.datastax.driver.core.ThreadLocalMonotonicTimestampGenerator; +import org.apache.cassandra.bridge.CdcBridgeFactory; +import org.apache.cassandra.bridge.TokenRange; +import org.apache.cassandra.cdc.CdcKryoRegister; +import org.apache.cassandra.cdc.api.CdcOptions; +import org.apache.cassandra.cdc.api.StatePersister; +import org.apache.cassandra.cdc.state.CdcState; +import org.apache.cassandra.spark.utils.AsyncExecutor; +import org.apache.cassandra.spark.utils.ThrowableUtils; +import org.apache.cassandra.util.CompressionUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * SidecarStatePersister buffers CDC state and flushes at regular time intervals, so we only write the latest CDC state and don't wastefully write expired data. + */ +public class SidecarStatePersister implements StatePersister +{ + private static final Logger LOGGER = LoggerFactory.getLogger(SidecarStatePersister.class); + + // group latest state by jobId/token range, so we persist independently + protected final ConcurrentHashMap<PersistWrapper.Key, PersistWrapper> latestState = new ConcurrentHashMap<>(); + protected final ConcurrentLinkedQueue<TimedFutureWrapper> activeFlush = new ConcurrentLinkedQueue<>(); + private final ThreadLocalMonotonicTimestampGenerator timestampGenerator = new ThreadLocalMonotonicTimestampGenerator(); + private final SidecarCdcOptions sidecarCdcOptions; + private final CdcOptions cdcOptions; + private final SidecarCdcCassandraClient cassandraClient; + private final SidecarCdcStats sidecarCdcStats; + private final AsyncExecutor asyncExecutor; + volatile long timerId = -1L; + + public SidecarStatePersister(SidecarCdcOptions sidecarCdcOptions, + CdcOptions cdcOptions, + SidecarCdcStats sidecarCdcStats, + SidecarCdcCassandraClient cassandraClient, + AsyncExecutor asyncExecutor) + { + this.sidecarCdcOptions = sidecarCdcOptions; + this.cdcOptions = cdcOptions; + this.sidecarCdcStats = sidecarCdcStats; + this.cassandraClient = cassandraClient; + this.asyncExecutor = asyncExecutor; + } + + // StatePersister implemented methods + + @Override + public void persist(String jobId, int partitionId, @Nullable TokenRange tokenRange, @NotNull ByteBuffer buf) + { + final PersistWrapper latest = new PersistWrapper(jobId, partitionId, tokenRange, buf, timestampGenerator.next()); + PersistWrapper.Key key = latest.key(); + if (!latest.equals(this.latestState.get(key))) + { + this.latestState.compute(key, (k, prev) -> !latest.equals(prev) ? latest : prev); + } + } + + @NotNull + @Override + public List<CdcState> loadState(String jobId, int partitionId, @Nullable TokenRange tokenRange) + { + CompressionUtil compressionUtil = CdcBridgeFactory.get(cdcOptions.version()).compressionUtil(); + List<Integer> sizes = new ArrayList<>(); + // deserialize and merge the CDC state objects into canonical view + List<CdcState> result = loadStateForRange(jobId, tokenRange) + .peek(bytes -> sizes.add(bytes.length)) + .map(bytes -> CdcState.deserialize(CdcKryoRegister.kryo(), compressionUtil, bytes)) Review Comment: I mean...how does that improve it? -- 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: commits-unsubscr...@cassandra.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org For additional commands, e-mail: commits-h...@cassandra.apache.org