nizhikov commented on a change in pull request #8909: URL: https://github.com/apache/ignite/pull/8909#discussion_r598282127
########## File path: modules/core/src/main/java/org/apache/ignite/internal/cdc/CDCConsumerState.java ########## @@ -0,0 +1,113 @@ +/* + * 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.ignite.internal.cdc; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.util.Iterator; +import org.apache.ignite.IgniteException; +import org.apache.ignite.cdc.CDCConsumer; +import org.apache.ignite.cdc.ChangeEvent; +import org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer; +import org.apache.ignite.internal.util.typedef.internal.U; + +import static java.nio.file.StandardCopyOption.ATOMIC_MOVE; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; +import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.FILE_SUFFIX; +import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.TMP_SUFFIX; +import static org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer.POINTER_SIZE; + +/** + * CDC Consumer state. + * + * Each time {@link CDCConsumer#onChange(Iterator)} returns {@code true} current offset in WAL segment saved to file. + * This allows to the {@link CDCConsumer} to continue consumption of the {@link ChangeEvent} from the last saved offset in case of fail or restart. + * + * @see CDCConsumer#onChange(Iterator) + * @see IgniteCDC + */ +public class CDCConsumerState { + /** State file. */ + private final Path state; + + /** Temp state file. */ + private final Path tmp; + + /** + * @param stateDir State directory. + * @param consumerId Consumer ID. + */ + public CDCConsumerState(Path stateDir, String consumerId) { + String fileName = "state-" + U.maskForFileName(consumerId) + FILE_SUFFIX; + + state = stateDir.resolve(fileName); + tmp = stateDir.resolve(fileName + TMP_SUFFIX); + } + + /** + * Saves state to file. + * @param ptr WAL pointer. + */ + public void save(WALPointer ptr) throws IOException { + ByteBuffer buf = ByteBuffer.allocate(POINTER_SIZE); + + buf.putLong(ptr.index()); + buf.putInt(ptr.fileOffset()); + buf.putInt(ptr.length()); + buf.flip(); + + try (FileChannel ch = FileChannel.open(tmp, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) { Review comment: Actually, I reuse logic implemented in [`CheckpointMarkerStorage#writeCheckpointEntry`](https://github.com/apache/ignite/blob/4723bd6b1a0bbcd481d623c8907ae09ea2495aa1/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/checkpoint/CheckpointMarkersStorage.java#L380). If we follow your suggestion and keep tmp file opened then we should copy(not move) it on the "main" state file. I'm not sure there is an "atomic copy" operation available. -- 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: [email protected]
