ibessonov commented on code in PR #4530: URL: https://github.com/apache/ignite-3/pull/4530#discussion_r1798971461
########## modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/impl/raft/MetaStorageSnapshotStorageFactory.java: ########## @@ -0,0 +1,109 @@ +/* + * 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.metastorage.impl.raft; + +import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.ignite.internal.metastorage.server.KeyValueStorage; +import org.apache.ignite.internal.raft.IndexWithTerm; +import org.apache.ignite.internal.raft.storage.SnapshotStorageFactory; +import org.apache.ignite.internal.util.ByteUtils; +import org.apache.ignite.raft.jraft.RaftMessagesFactory; +import org.apache.ignite.raft.jraft.entity.RaftOutter; +import org.apache.ignite.raft.jraft.entity.RaftOutter.SnapshotMeta; +import org.apache.ignite.raft.jraft.option.RaftOptions; +import org.apache.ignite.raft.jraft.storage.SnapshotStorage; +import org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader; +import org.apache.ignite.raft.jraft.storage.snapshot.local.LocalSnapshotStorage; +import org.jetbrains.annotations.Nullable; + +/** + * Snapshot storage factory implementation for Meta Storage. + */ +public class MetaStorageSnapshotStorageFactory implements SnapshotStorageFactory { + private final KeyValueStorage storage; + + /** Snapshot meta, constructed from the storage data and raft group configuration at startup. {@code null} if the storage is empty. */ + private final @Nullable RaftOutter.SnapshotMeta startupSnapshotMeta; + + /** + * Constructor. + * + * @param storage Key-value storage instance. + */ + public MetaStorageSnapshotStorageFactory(KeyValueStorage storage) { + this.storage = storage; + + startupSnapshotMeta = readStartupSnapshotMeta(); + } + + private @Nullable SnapshotMeta readStartupSnapshotMeta() { + IndexWithTerm indexWithTerm = storage.getIndexWithTerm(); + + if (indexWithTerm == null) { + return null; + } + + byte[] configBytes = storage.getConfiguration(); + assert configBytes != null; + + RaftGroupConfiguration configuration = ByteUtils.fromBytes(configBytes); + assert configuration != null; + + return new RaftMessagesFactory().snapshotMeta() + .lastIncludedIndex(indexWithTerm.index()) + .lastIncludedTerm(indexWithTerm.term()) + .peersList(configuration.peers()) + .oldPeersList(configuration.oldPeers()) + .learnersList(configuration.learners()) + .oldLearnersList(configuration.oldLearners()) + .build(); + } + + /** + * Returns a snapshot meta, constructed from the storage data and raft group configuration at startup. + */ + SnapshotMeta startupSnapshotMeta() { + if (startupSnapshotMeta == null) { + throw new IllegalStateException("Storage is empty, so startup snapshot should not be read"); + } + + return startupSnapshotMeta; + } + + @Override + public @Nullable SnapshotStorage createSnapshotStorage(String uri, RaftOptions raftOptions) { + return new LocalSnapshotStorage(uri, raftOptions) { + private final AtomicBoolean startupSnapshotOpened = new AtomicBoolean(false); + + @Override + public SnapshotReader open() { + if (startupSnapshotOpened.compareAndSet(false, true)) { + if (startupSnapshotMeta == null) { + // The storage is empty, let's behave how JRaft does: return null, avoiding an attempt to load a snapshot + // when it's not there. + return null; + } + + return new StartupMetaStorageSnapshotReader(MetaStorageSnapshotStorageFactory.this); Review Comment: > read the startup meta and instead it is done by the factory for some reason I have to read meta in order to determine whether I need to return `null` or `StartupMetaStorageSnapshotReader`. When it comes to circular references, my bad -- 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]
