You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "korlov42 (via GitHub)" <gi...@apache.org> on 2023/05/06 17:01:25 UTC

[GitHub] [ignite-3] korlov42 opened a new pull request, #2035: IGNITE-19080 Save catalog changes to metastorage

korlov42 opened a new pull request, #2035:
URL: https://github.com/apache/ignite-3/pull/2035

   (no comment)


-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] AMashenkov merged pull request #2035: IGNITE-19080 Save catalog changes to metastorage

Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov merged PR #2035:
URL: https://github.com/apache/ignite-3/pull/2035


-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] AMashenkov commented on a diff in pull request #2035: IGNITE-19080 Save catalog changes to metastorage

Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #2035:
URL: https://github.com/apache/ignite-3/pull/2035#discussion_r1187235750


##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/storage/UpdateLogImpl.java:
##########
@@ -0,0 +1,223 @@
+/*
+ * 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.catalog.storage;
+
+import static java.util.concurrent.CompletableFuture.failedFuture;
+import static org.apache.ignite.internal.metastorage.dsl.Conditions.notExists;
+import static org.apache.ignite.internal.metastorage.dsl.Conditions.or;
+import static org.apache.ignite.internal.metastorage.dsl.Conditions.value;
+import static org.apache.ignite.internal.metastorage.dsl.Operations.ops;
+import static org.apache.ignite.internal.metastorage.dsl.Operations.put;
+import static org.apache.ignite.internal.metastorage.dsl.Statements.iif;
+import static org.apache.ignite.internal.util.ByteUtils.fromBytes;
+import static org.apache.ignite.internal.util.ByteUtils.intToBytes;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.ignite.internal.metastorage.EntryEvent;
+import org.apache.ignite.internal.metastorage.MetaStorageManager;
+import org.apache.ignite.internal.metastorage.WatchEvent;
+import org.apache.ignite.internal.metastorage.WatchListener;
+import org.apache.ignite.internal.metastorage.dsl.Condition;
+import org.apache.ignite.internal.metastorage.dsl.Iif;
+import org.apache.ignite.internal.metastorage.dsl.StatementResult;
+import org.apache.ignite.internal.metastorage.dsl.Update;
+import org.apache.ignite.internal.util.ByteUtils;
+import org.apache.ignite.internal.util.IgniteSpinBusyLock;
+import org.apache.ignite.internal.vault.VaultEntry;
+import org.apache.ignite.internal.vault.VaultManager;
+import org.apache.ignite.lang.ByteArray;
+import org.apache.ignite.lang.ErrorGroups.Common;
+import org.apache.ignite.lang.IgniteException;
+import org.apache.ignite.lang.IgniteInternalException;
+import org.apache.ignite.lang.NodeStoppingException;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Metastore-based implementation of UpdateLog.
+ */
+public class UpdateLogImpl implements UpdateLog {
+    private final IgniteSpinBusyLock busyLock = new IgniteSpinBusyLock();
+    private final AtomicBoolean stopGuard = new AtomicBoolean();
+
+    private final MetaStorageManager metastore;
+    private final VaultManager vault;
+
+    private volatile OnUpdateHandler onUpdateHandler;
+    private volatile @Nullable UpdateListener listener = null;
+
+    /**
+     * Creates the object.
+     *
+     * @param metastore A metastore is used to store and distribute updates across the cluster.
+     * @param vault A vault is used to recover state and replay updates on start.
+     */
+    public UpdateLogImpl(
+            MetaStorageManager metastore,
+            VaultManager vault
+    ) {
+        this.vault = vault;
+        this.metastore = metastore;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void start() {
+        if (!busyLock.enterBusy()) {
+            throw new IgniteException(Common.NODE_STOPPING_ERR, new NodeStoppingException());
+        }
+
+        try {
+            OnUpdateHandler handler = onUpdateHandler;
+
+            if (handler == null) {
+                throw new IgniteInternalException(
+                        Common.UNEXPECTED_ERR,
+                        "Handler must be registered prior to component start"
+                );
+            }
+
+            restoreStateFromVault(handler);
+
+            UpdateListener listener = new UpdateListener(handler);
+            this.listener = listener;
+
+            metastore.registerPrefixWatch(CatalogKey.updatePrefix(), listener);
+
+        } finally {
+            busyLock.leaveBusy();
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void stop() throws Exception {
+        if (!stopGuard.compareAndSet(false, true)) {
+            return;
+        }
+
+        busyLock.block();
+
+        UpdateListener listener = this.listener;
+        this.listener = null;
+
+        if (listener != null) {
+            metastore.unregisterWatch(listener);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void registerUpdateHandler(OnUpdateHandler handler) {
+        onUpdateHandler = handler;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletableFuture<Boolean> append(VersionedUpdate update) {
+        if (!busyLock.enterBusy()) {
+            return failedFuture(new IgniteException(Common.NODE_STOPPING_ERR, new NodeStoppingException()));
+        }
+
+        try {
+            int newVersion = update.version();
+            int expectedVersion = newVersion - 1;
+
+            Condition versionAsExpected = or(
+                    notExists(CatalogKey.currentVersion()),
+                    value(CatalogKey.currentVersion()).eq(intToBytes(expectedVersion))
+            );
+            Update appendUpdateEntryAndBumpVersion = ops(
+                    put(CatalogKey.update(newVersion), ByteUtils.toBytes(update)),
+                    put(CatalogKey.currentVersion(), intToBytes(newVersion))
+            ).yield(true);
+
+            Iif iif = iif(versionAsExpected, appendUpdateEntryAndBumpVersion, ops().yield(false));
+
+            return metastore.invoke(iif).thenApply(StatementResult::getAsBoolean);
+        } finally {
+            busyLock.leaveBusy();
+        }
+    }
+
+    private void restoreStateFromVault(OnUpdateHandler handler) {
+        int ver = 1;
+
+        while (true) {
+            VaultEntry entry = vault.get(CatalogKey.update(ver++)).join();
+
+            if (entry == null) {
+                break;
+            }
+
+            VersionedUpdate update = fromBytes(entry.value());
+
+            handler.handle(update);
+        }
+    }
+
+    private static class CatalogKey {
+        private CatalogKey() {
+            throw new AssertionError();
+        }
+
+        static ByteArray currentVersion() {
+            return ByteArray.fromString("catalog.version");
+        }
+
+        static ByteArray update(int version) {
+            return ByteArray.fromString("catalog.update." + version);
+        }
+
+        static ByteArray updatePrefix() {
+            return ByteArray.fromString("catalog.update.");
+        }
+    }
+
+    private static class UpdateListener implements WatchListener {
+        private final OnUpdateHandler onUpdateHandler;
+
+        UpdateListener(OnUpdateHandler onUpdateHandler) {
+            this.onUpdateHandler = onUpdateHandler;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public CompletableFuture<Void> onUpdate(WatchEvent event) {
+            for (EntryEvent eventEntry : event.entryEvents()) {
+                assert eventEntry.newEntry() != null;
+                assert !eventEntry.newEntry().empty();
+
+                byte[] payload = eventEntry.newEntry().value();
+
+                assert payload != null;
+
+                VersionedUpdate update = fromBytes(payload);
+
+                onUpdateHandler.handle(update);
+            }
+
+            return CompletableFuture.completedFuture(null);
+        }
+
+        @Override

Review Comment:
   ```suggestion
           /** {@inheritDoc} */
           @Override
   ```
   or remove similar javadoc from 'onUpdate' method above.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] AMashenkov commented on a diff in pull request #2035: IGNITE-19080 Save catalog changes to metastorage

Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #2035:
URL: https://github.com/apache/ignite-3/pull/2035#discussion_r1187257670


##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/storage/UpdateLog.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.catalog.storage;
+
+import java.util.concurrent.CompletableFuture;
+import org.apache.ignite.internal.manager.IgniteComponent;
+import org.apache.ignite.lang.IgniteInternalException;
+
+/**
+ * Distributed log of catalog updates.
+ */
+public interface UpdateLog extends IgniteComponent {
+    /**
+     * Appends the given update to the log if the update with that version does not exist.
+     *
+     * <p>The version of {@link VersionedUpdate} must be greater by 1 of the version of previously
+     * appended update, otherwise current update will be rejected. The versions of already appended
+     * updates should be tracked by registering {@link OnUpdateHandler update handler}
+     * (see {@link #registerUpdateHandler(OnUpdateHandler)}).
+     *
+     * @param update An update to append to the log.
+     * @return A {@code true} if update has been successfully appended, {@code false} otherwise
+     *      if update with the same version already exists, and operation should be retried with
+     *      new version.
+     */
+    CompletableFuture<Boolean> append(VersionedUpdate update);
+
+    /**
+     * Registers a handler to keep track of appended updates.
+     *
+     * @param handler A handler to notify when new update was appended.
+     */
+    void registerUpdateHandler(OnUpdateHandler handler);
+
+    /**
+     * Starts the component.
+     *
+     * <p>Log replay is a part of a component start up process, thus the handler must
+     * be registered prior to start is invoked, otherwise exception will be thrown.
+     *
+     * @throws IgniteInternalException If no handler has been registered.
+     */
+    @Override void start() throws IgniteInternalException;
+
+    /**
+     * An interface describing a handler that will receive notification
+     * when a new update is added to the log.
+     */
+    @FunctionalInterface
+    interface OnUpdateHandler {
+        void handle(VersionedUpdate update);

Review Comment:
   Javadoc



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] korlov42 commented on a diff in pull request #2035: IGNITE-19080 Save catalog changes to metastorage

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #2035:
URL: https://github.com/apache/ignite-3/pull/2035#discussion_r1188365205


##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/Catalog.java:
##########
@@ -51,9 +51,15 @@ public class CatalogDescriptor implements Serializable {
      * @param activationTimestamp Catalog activation timestamp.
      * @param descriptors Schema descriptors.

Review Comment:
   fixed



##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/storage/UpdateLogImpl.java:
##########
@@ -0,0 +1,223 @@
+/*
+ * 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.catalog.storage;
+
+import static java.util.concurrent.CompletableFuture.failedFuture;
+import static org.apache.ignite.internal.metastorage.dsl.Conditions.notExists;
+import static org.apache.ignite.internal.metastorage.dsl.Conditions.or;
+import static org.apache.ignite.internal.metastorage.dsl.Conditions.value;
+import static org.apache.ignite.internal.metastorage.dsl.Operations.ops;
+import static org.apache.ignite.internal.metastorage.dsl.Operations.put;
+import static org.apache.ignite.internal.metastorage.dsl.Statements.iif;
+import static org.apache.ignite.internal.util.ByteUtils.fromBytes;
+import static org.apache.ignite.internal.util.ByteUtils.intToBytes;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.ignite.internal.metastorage.EntryEvent;
+import org.apache.ignite.internal.metastorage.MetaStorageManager;
+import org.apache.ignite.internal.metastorage.WatchEvent;
+import org.apache.ignite.internal.metastorage.WatchListener;
+import org.apache.ignite.internal.metastorage.dsl.Condition;
+import org.apache.ignite.internal.metastorage.dsl.Iif;
+import org.apache.ignite.internal.metastorage.dsl.StatementResult;
+import org.apache.ignite.internal.metastorage.dsl.Update;
+import org.apache.ignite.internal.util.ByteUtils;
+import org.apache.ignite.internal.util.IgniteSpinBusyLock;
+import org.apache.ignite.internal.vault.VaultEntry;
+import org.apache.ignite.internal.vault.VaultManager;
+import org.apache.ignite.lang.ByteArray;
+import org.apache.ignite.lang.ErrorGroups.Common;
+import org.apache.ignite.lang.IgniteException;
+import org.apache.ignite.lang.IgniteInternalException;
+import org.apache.ignite.lang.NodeStoppingException;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Metastore-based implementation of UpdateLog.
+ */
+public class UpdateLogImpl implements UpdateLog {
+    private final IgniteSpinBusyLock busyLock = new IgniteSpinBusyLock();
+    private final AtomicBoolean stopGuard = new AtomicBoolean();
+
+    private final MetaStorageManager metastore;
+    private final VaultManager vault;
+
+    private volatile OnUpdateHandler onUpdateHandler;
+    private volatile @Nullable UpdateListener listener = null;
+
+    /**
+     * Creates the object.
+     *
+     * @param metastore A metastore is used to store and distribute updates across the cluster.
+     * @param vault A vault is used to recover state and replay updates on start.
+     */
+    public UpdateLogImpl(
+            MetaStorageManager metastore,
+            VaultManager vault
+    ) {
+        this.vault = vault;
+        this.metastore = metastore;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void start() {
+        if (!busyLock.enterBusy()) {
+            throw new IgniteException(Common.NODE_STOPPING_ERR, new NodeStoppingException());
+        }
+
+        try {
+            OnUpdateHandler handler = onUpdateHandler;
+
+            if (handler == null) {
+                throw new IgniteInternalException(
+                        Common.UNEXPECTED_ERR,
+                        "Handler must be registered prior to component start"
+                );
+            }
+
+            restoreStateFromVault(handler);
+
+            UpdateListener listener = new UpdateListener(handler);
+            this.listener = listener;
+
+            metastore.registerPrefixWatch(CatalogKey.updatePrefix(), listener);
+
+        } finally {
+            busyLock.leaveBusy();
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void stop() throws Exception {
+        if (!stopGuard.compareAndSet(false, true)) {
+            return;
+        }
+
+        busyLock.block();
+
+        UpdateListener listener = this.listener;
+        this.listener = null;
+
+        if (listener != null) {
+            metastore.unregisterWatch(listener);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void registerUpdateHandler(OnUpdateHandler handler) {
+        onUpdateHandler = handler;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletableFuture<Boolean> append(VersionedUpdate update) {
+        if (!busyLock.enterBusy()) {
+            return failedFuture(new IgniteException(Common.NODE_STOPPING_ERR, new NodeStoppingException()));
+        }
+
+        try {
+            int newVersion = update.version();
+            int expectedVersion = newVersion - 1;
+
+            Condition versionAsExpected = or(
+                    notExists(CatalogKey.currentVersion()),
+                    value(CatalogKey.currentVersion()).eq(intToBytes(expectedVersion))
+            );
+            Update appendUpdateEntryAndBumpVersion = ops(
+                    put(CatalogKey.update(newVersion), ByteUtils.toBytes(update)),
+                    put(CatalogKey.currentVersion(), intToBytes(newVersion))
+            ).yield(true);
+
+            Iif iif = iif(versionAsExpected, appendUpdateEntryAndBumpVersion, ops().yield(false));
+
+            return metastore.invoke(iif).thenApply(StatementResult::getAsBoolean);
+        } finally {
+            busyLock.leaveBusy();
+        }
+    }
+
+    private void restoreStateFromVault(OnUpdateHandler handler) {
+        int ver = 1;
+
+        while (true) {
+            VaultEntry entry = vault.get(CatalogKey.update(ver++)).join();
+
+            if (entry == null) {
+                break;
+            }
+
+            VersionedUpdate update = fromBytes(entry.value());
+
+            handler.handle(update);
+        }
+    }
+
+    private static class CatalogKey {
+        private CatalogKey() {
+            throw new AssertionError();
+        }
+
+        static ByteArray currentVersion() {
+            return ByteArray.fromString("catalog.version");
+        }
+
+        static ByteArray update(int version) {
+            return ByteArray.fromString("catalog.update." + version);
+        }
+
+        static ByteArray updatePrefix() {
+            return ByteArray.fromString("catalog.update.");
+        }
+    }
+
+    private static class UpdateListener implements WatchListener {
+        private final OnUpdateHandler onUpdateHandler;
+
+        UpdateListener(OnUpdateHandler onUpdateHandler) {
+            this.onUpdateHandler = onUpdateHandler;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public CompletableFuture<Void> onUpdate(WatchEvent event) {
+            for (EntryEvent eventEntry : event.entryEvents()) {
+                assert eventEntry.newEntry() != null;
+                assert !eventEntry.newEntry().empty();
+
+                byte[] payload = eventEntry.newEntry().value();
+
+                assert payload != null;
+
+                VersionedUpdate update = fromBytes(payload);
+
+                onUpdateHandler.handle(update);
+            }
+
+            return CompletableFuture.completedFuture(null);
+        }
+
+        @Override

Review Comment:
   fixed



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] AMashenkov commented on a diff in pull request #2035: IGNITE-19080 Save catalog changes to metastorage

Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #2035:
URL: https://github.com/apache/ignite-3/pull/2035#discussion_r1187235750


##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/storage/UpdateLogImpl.java:
##########
@@ -0,0 +1,223 @@
+/*
+ * 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.catalog.storage;
+
+import static java.util.concurrent.CompletableFuture.failedFuture;
+import static org.apache.ignite.internal.metastorage.dsl.Conditions.notExists;
+import static org.apache.ignite.internal.metastorage.dsl.Conditions.or;
+import static org.apache.ignite.internal.metastorage.dsl.Conditions.value;
+import static org.apache.ignite.internal.metastorage.dsl.Operations.ops;
+import static org.apache.ignite.internal.metastorage.dsl.Operations.put;
+import static org.apache.ignite.internal.metastorage.dsl.Statements.iif;
+import static org.apache.ignite.internal.util.ByteUtils.fromBytes;
+import static org.apache.ignite.internal.util.ByteUtils.intToBytes;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.ignite.internal.metastorage.EntryEvent;
+import org.apache.ignite.internal.metastorage.MetaStorageManager;
+import org.apache.ignite.internal.metastorage.WatchEvent;
+import org.apache.ignite.internal.metastorage.WatchListener;
+import org.apache.ignite.internal.metastorage.dsl.Condition;
+import org.apache.ignite.internal.metastorage.dsl.Iif;
+import org.apache.ignite.internal.metastorage.dsl.StatementResult;
+import org.apache.ignite.internal.metastorage.dsl.Update;
+import org.apache.ignite.internal.util.ByteUtils;
+import org.apache.ignite.internal.util.IgniteSpinBusyLock;
+import org.apache.ignite.internal.vault.VaultEntry;
+import org.apache.ignite.internal.vault.VaultManager;
+import org.apache.ignite.lang.ByteArray;
+import org.apache.ignite.lang.ErrorGroups.Common;
+import org.apache.ignite.lang.IgniteException;
+import org.apache.ignite.lang.IgniteInternalException;
+import org.apache.ignite.lang.NodeStoppingException;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Metastore-based implementation of UpdateLog.
+ */
+public class UpdateLogImpl implements UpdateLog {
+    private final IgniteSpinBusyLock busyLock = new IgniteSpinBusyLock();
+    private final AtomicBoolean stopGuard = new AtomicBoolean();
+
+    private final MetaStorageManager metastore;
+    private final VaultManager vault;
+
+    private volatile OnUpdateHandler onUpdateHandler;
+    private volatile @Nullable UpdateListener listener = null;
+
+    /**
+     * Creates the object.
+     *
+     * @param metastore A metastore is used to store and distribute updates across the cluster.
+     * @param vault A vault is used to recover state and replay updates on start.
+     */
+    public UpdateLogImpl(
+            MetaStorageManager metastore,
+            VaultManager vault
+    ) {
+        this.vault = vault;
+        this.metastore = metastore;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void start() {
+        if (!busyLock.enterBusy()) {
+            throw new IgniteException(Common.NODE_STOPPING_ERR, new NodeStoppingException());
+        }
+
+        try {
+            OnUpdateHandler handler = onUpdateHandler;
+
+            if (handler == null) {
+                throw new IgniteInternalException(
+                        Common.UNEXPECTED_ERR,
+                        "Handler must be registered prior to component start"
+                );
+            }
+
+            restoreStateFromVault(handler);
+
+            UpdateListener listener = new UpdateListener(handler);
+            this.listener = listener;
+
+            metastore.registerPrefixWatch(CatalogKey.updatePrefix(), listener);
+
+        } finally {
+            busyLock.leaveBusy();
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void stop() throws Exception {
+        if (!stopGuard.compareAndSet(false, true)) {
+            return;
+        }
+
+        busyLock.block();
+
+        UpdateListener listener = this.listener;
+        this.listener = null;
+
+        if (listener != null) {
+            metastore.unregisterWatch(listener);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void registerUpdateHandler(OnUpdateHandler handler) {
+        onUpdateHandler = handler;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletableFuture<Boolean> append(VersionedUpdate update) {
+        if (!busyLock.enterBusy()) {
+            return failedFuture(new IgniteException(Common.NODE_STOPPING_ERR, new NodeStoppingException()));
+        }
+
+        try {
+            int newVersion = update.version();
+            int expectedVersion = newVersion - 1;
+
+            Condition versionAsExpected = or(
+                    notExists(CatalogKey.currentVersion()),
+                    value(CatalogKey.currentVersion()).eq(intToBytes(expectedVersion))
+            );
+            Update appendUpdateEntryAndBumpVersion = ops(
+                    put(CatalogKey.update(newVersion), ByteUtils.toBytes(update)),
+                    put(CatalogKey.currentVersion(), intToBytes(newVersion))
+            ).yield(true);
+
+            Iif iif = iif(versionAsExpected, appendUpdateEntryAndBumpVersion, ops().yield(false));
+
+            return metastore.invoke(iif).thenApply(StatementResult::getAsBoolean);
+        } finally {
+            busyLock.leaveBusy();
+        }
+    }
+
+    private void restoreStateFromVault(OnUpdateHandler handler) {
+        int ver = 1;
+
+        while (true) {
+            VaultEntry entry = vault.get(CatalogKey.update(ver++)).join();
+
+            if (entry == null) {
+                break;
+            }
+
+            VersionedUpdate update = fromBytes(entry.value());
+
+            handler.handle(update);
+        }
+    }
+
+    private static class CatalogKey {
+        private CatalogKey() {
+            throw new AssertionError();
+        }
+
+        static ByteArray currentVersion() {
+            return ByteArray.fromString("catalog.version");
+        }
+
+        static ByteArray update(int version) {
+            return ByteArray.fromString("catalog.update." + version);
+        }
+
+        static ByteArray updatePrefix() {
+            return ByteArray.fromString("catalog.update.");
+        }
+    }
+
+    private static class UpdateListener implements WatchListener {
+        private final OnUpdateHandler onUpdateHandler;
+
+        UpdateListener(OnUpdateHandler onUpdateHandler) {
+            this.onUpdateHandler = onUpdateHandler;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public CompletableFuture<Void> onUpdate(WatchEvent event) {
+            for (EntryEvent eventEntry : event.entryEvents()) {
+                assert eventEntry.newEntry() != null;
+                assert !eventEntry.newEntry().empty();
+
+                byte[] payload = eventEntry.newEntry().value();
+
+                assert payload != null;
+
+                VersionedUpdate update = fromBytes(payload);
+
+                onUpdateHandler.handle(update);
+            }
+
+            return CompletableFuture.completedFuture(null);
+        }
+
+        @Override

Review Comment:
   ```suggestion
           /** {@inheritDoc} */
           @Override
   ```



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] AMashenkov commented on a diff in pull request #2035: IGNITE-19080 Save catalog changes to metastorage

Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #2035:
URL: https://github.com/apache/ignite-3/pull/2035#discussion_r1187223501


##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/Catalog.java:
##########
@@ -51,9 +51,15 @@ public class CatalogDescriptor implements Serializable {
      * @param activationTimestamp Catalog activation timestamp.
      * @param descriptors Schema descriptors.

Review Comment:
   ```suggestion
        * @param descriptors Schema descriptors.
        * @param objectIdGen Object id generator state.
   ```



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] korlov42 commented on a diff in pull request #2035: IGNITE-19080 Save catalog changes to metastorage

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #2035:
URL: https://github.com/apache/ignite-3/pull/2035#discussion_r1188365584


##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/storage/UpdateLog.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.catalog.storage;
+
+import java.util.concurrent.CompletableFuture;
+import org.apache.ignite.internal.manager.IgniteComponent;
+import org.apache.ignite.lang.IgniteInternalException;
+
+/**
+ * Distributed log of catalog updates.
+ */
+public interface UpdateLog extends IgniteComponent {
+    /**
+     * Appends the given update to the log if the update with that version does not exist.
+     *
+     * <p>The version of {@link VersionedUpdate} must be greater by 1 of the version of previously
+     * appended update, otherwise current update will be rejected. The versions of already appended
+     * updates should be tracked by registering {@link OnUpdateHandler update handler}
+     * (see {@link #registerUpdateHandler(OnUpdateHandler)}).
+     *
+     * @param update An update to append to the log.
+     * @return A {@code true} if update has been successfully appended, {@code false} otherwise
+     *      if update with the same version already exists, and operation should be retried with
+     *      new version.
+     */
+    CompletableFuture<Boolean> append(VersionedUpdate update);
+
+    /**
+     * Registers a handler to keep track of appended updates.
+     *
+     * @param handler A handler to notify when new update was appended.
+     */
+    void registerUpdateHandler(OnUpdateHandler handler);
+
+    /**
+     * Starts the component.
+     *
+     * <p>Log replay is a part of a component start up process, thus the handler must
+     * be registered prior to start is invoked, otherwise exception will be thrown.
+     *
+     * @throws IgniteInternalException If no handler has been registered.
+     */
+    @Override void start() throws IgniteInternalException;
+
+    /**
+     * An interface describing a handler that will receive notification
+     * when a new update is added to the log.
+     */
+    @FunctionalInterface
+    interface OnUpdateHandler {
+        void handle(VersionedUpdate update);

Review Comment:
   fixed



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org