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

[GitHub] [ignite-3] ibessonov commented on a diff in pull request #2231: IGNITE-19641 Catalog events are triggered too early.

ibessonov commented on code in PR #2231:
URL: https://github.com/apache/ignite-3/pull/2231#discussion_r1237119534


##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogServiceImpl.java:
##########
@@ -277,82 +259,62 @@ private Catalog catalogAt(long timestamp) {
         return entry.getValue();
     }
 
-    /** {@inheritDoc} */
     @Override
     public CompletableFuture<Void> createTable(CreateTableParams params) {
         return saveUpdate(catalog -> {
-            String schemaName = Objects.requireNonNullElse(params.schemaName(), CatalogService.PUBLIC);
-
-            CatalogSchemaDescriptor schema = Objects.requireNonNull(catalog.schema(schemaName), "No schema found: " + schemaName);
+            CatalogSchemaDescriptor schema = getSchema(catalog, params.schemaName());
 
             if (schema.table(params.tableName()) != null) {
-                throw new TableAlreadyExistsException(schemaName, params.tableName());
+                throw new TableAlreadyExistsException(schema.name(), params.tableName());
             }
 
-            params.columns().stream().map(ColumnParams::name).filter(Predicate.not(new HashSet<>()::add))
-                    .findAny().ifPresent(columnName -> {
-                        throw new IgniteInternalException(
-                                ErrorGroups.Index.INVALID_INDEX_DEFINITION_ERR, "Can't create table with duplicate columns: "
-                                + params.columns().stream().map(ColumnParams::name).collect(Collectors.joining(", "))
-                        );
-                    });
+            validateParams(params);
+
+            CatalogZoneDescriptor zone = getZone(catalog, Objects.requireNonNullElse(params.zone(), DEFAULT_ZONE_NAME));
 
-            String zoneName = Objects.requireNonNullElse(params.zone(), CatalogService.DEFAULT_ZONE_NAME);
+            int id = catalog.objectIdGenState();
 
-            CatalogZoneDescriptor zone = Objects.requireNonNull(catalog.zone(zoneName), "No zone found: " + zoneName);
+            CatalogTableDescriptor table = CatalogUtils.fromParams(id++, zone.id(), params);
 
-            CatalogTableDescriptor table = CatalogUtils.fromParams(catalog.objectIdGenState(), zone.id(), params);
+            CatalogHashIndexDescriptor pkIndex = createHashIndexDescriptor(createPkIndexParams(params), table, id++);
 
             return List.of(
                     new NewTableEntry(table),
-                    new ObjectIdGenUpdateEntry(1)
+                    new NewIndexEntry(pkIndex),
+                    new ObjectIdGenUpdateEntry(id - catalog.objectIdGenState())
             );
         });
     }
 
-    /** {@inheritDoc} */
     @Override
     public CompletableFuture<Void> dropTable(DropTableParams params) {
         return saveUpdate(catalog -> {
-            String schemaName = Objects.requireNonNullElse(params.schemaName(), CatalogService.PUBLIC);
-
-            CatalogSchemaDescriptor schema = Objects.requireNonNull(catalog.schema(schemaName), "No schema found: " + schemaName);
+            CatalogSchemaDescriptor schema = getSchema(catalog, params.schemaName());
 
-            CatalogTableDescriptor table = schema.table(params.tableName());
-
-            if (table == null) {
-                throw new TableNotFoundException(schemaName, params.tableName());
-            }
+            CatalogTableDescriptor table = getTable(schema, params.tableName());
 
             List<UpdateEntry> updateEntries = new ArrayList<>();
 
             Arrays.stream(schema.indexes())
                     .filter(index -> index.tableId() == table.id())
-                    .forEach(index -> updateEntries.add(new DropIndexEntry(index.id())));
+                    .forEach(index -> updateEntries.add(new DropIndexEntry(index.id(), index.tableId())));

Review Comment:
   Why do you need a tableId here?



##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/commands/AbstractIndexCommandParams.java:
##########
@@ -41,6 +47,20 @@ public String schemaName() {
         return schema;
     }
 
+    /**
+     * Gets table name.
+     */
+    public String tableName() {
+        return tableName;
+    }
+
+    /**
+     * Returns {@code true} if index is unique, {@code false} otherwise.
+     */
+    public boolean unique() {

Review Comment:
   What does this have to do with current fix?



##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogServiceImpl.java:
##########
@@ -277,82 +259,62 @@ private Catalog catalogAt(long timestamp) {
         return entry.getValue();
     }
 
-    /** {@inheritDoc} */
     @Override
     public CompletableFuture<Void> createTable(CreateTableParams params) {
         return saveUpdate(catalog -> {
-            String schemaName = Objects.requireNonNullElse(params.schemaName(), CatalogService.PUBLIC);
-
-            CatalogSchemaDescriptor schema = Objects.requireNonNull(catalog.schema(schemaName), "No schema found: " + schemaName);
+            CatalogSchemaDescriptor schema = getSchema(catalog, params.schemaName());
 
             if (schema.table(params.tableName()) != null) {
-                throw new TableAlreadyExistsException(schemaName, params.tableName());
+                throw new TableAlreadyExistsException(schema.name(), params.tableName());
             }
 
-            params.columns().stream().map(ColumnParams::name).filter(Predicate.not(new HashSet<>()::add))
-                    .findAny().ifPresent(columnName -> {
-                        throw new IgniteInternalException(
-                                ErrorGroups.Index.INVALID_INDEX_DEFINITION_ERR, "Can't create table with duplicate columns: "
-                                + params.columns().stream().map(ColumnParams::name).collect(Collectors.joining(", "))
-                        );
-                    });
+            validateParams(params);
+
+            CatalogZoneDescriptor zone = getZone(catalog, Objects.requireNonNullElse(params.zone(), DEFAULT_ZONE_NAME));

Review Comment:
   This and similar refactorings could have been done in another PR, they have nothing to do with the fix itself.



##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/events/AddColumnEventParameters.java:
##########
@@ -24,25 +24,27 @@
  * Add column event parameters contains descriptors of added columns.
  */
 public class AddColumnEventParameters extends CatalogEventParameters {
-
     private final int tableId;
-    private final List<CatalogTableColumnDescriptor> columnDescriptors;
+    private final List<CatalogTableColumnDescriptor> descriptors;

Review Comment:
   Why did you rename it? Old name was fine



##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/events/AlterZoneEventParameters.java:
##########
@@ -23,25 +23,25 @@
  * Alter zone event parameters contains a distribution zone descriptor for newly created distribution zone.
  */
 public class AlterZoneEventParameters extends CatalogEventParameters {
-
-    private final CatalogZoneDescriptor zoneDescriptor;
+    private final CatalogZoneDescriptor descriptor;

Review Comment:
   Same here. Why did you rename all these fields?



##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/events/AlterColumnEventParameters.java:
##########
@@ -23,32 +23,36 @@
  * Create table event parameters contains a column descriptor for the modified column.
  */
 public class AlterColumnEventParameters extends CatalogEventParameters {
-
     private final int tableId;
 
-    private final CatalogTableColumnDescriptor columnDescriptor;
+    private final CatalogTableColumnDescriptor descriptor;
 
     /**
      * Constructor.
      *
      * @param causalityToken Causality token.
-     * @param tableId Returns an id the table to be modified.
-     * @param columnDescriptor Descriptor for the column to be replaced.
+     * @param catalogVersion Catalog version.
+     * @param tableId Returns ID the table to be modified.
+     * @param descriptor Descriptor for the column to be replaced.
      */
-    public AlterColumnEventParameters(long causalityToken, int tableId, CatalogTableColumnDescriptor columnDescriptor) {
-        super(causalityToken);
+    public AlterColumnEventParameters(long causalityToken, int catalogVersion, int tableId, CatalogTableColumnDescriptor descriptor) {
+        super(causalityToken, catalogVersion);
 
         this.tableId = tableId;
-        this.columnDescriptor = columnDescriptor;
+        this.descriptor = descriptor;
     }
 
-    /** Returns an id of a modified table. */
+    /**
+     * Returns ID of a modified table.
+     */

Review Comment:
   Was this necessary? Do we have code style guide that recommends this formatting?
   If not, then you simply change someone-else's code by your own preference, without any other objective reason.
   I wouldn't mind it, but this PR is 1000+ lines of changes, and some of them are completely unnecessary. This forces me to look at more code than I should.



##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/events/AlterColumnEventParameters.java:
##########
@@ -23,32 +23,36 @@
  * Create table event parameters contains a column descriptor for the modified column.
  */
 public class AlterColumnEventParameters extends CatalogEventParameters {
-
     private final int tableId;
 
-    private final CatalogTableColumnDescriptor columnDescriptor;
+    private final CatalogTableColumnDescriptor descriptor;

Review Comment:
   And here. These are changes for the sake of changes



##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/storage/UpdateLogImpl.java:
##########
@@ -167,7 +169,7 @@ private void restoreStateFromVault(OnUpdateHandler handler) {
 
             VersionedUpdate update = fromBytes(entry.value());
 
-            handler.handle(update);
+            handler.handle(update, metastore.appliedRevision());

Review Comment:
   This has nothing to do with the fix. Please do it in a separate issue.



##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/storage/CatalogFireEvent.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * 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 org.apache.ignite.internal.catalog.events.CatalogEvent;
+import org.apache.ignite.internal.catalog.events.CatalogEventParameters;
+
+/**
+ * Interface for firing events.

Review Comment:
   for updates that require firing events, I guess



##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/storage/AlterZoneEntry.java:
##########
@@ -37,12 +43,36 @@ public AlterZoneEntry(CatalogZoneDescriptor descriptor) {
         this.descriptor = descriptor;
     }
 
-    /** Returns descriptor of a zone to alter. */
+    /**
+     * Returns descriptor of a zone to alter.
+     */
     public CatalogZoneDescriptor descriptor() {
         return descriptor;
     }
 
-    /** {@inheritDoc} */
+    @Override
+    public CatalogEvent eventType() {
+        return CatalogEvent.ZONE_ALTER;
+    }
+
+    @Override
+    public CatalogEventParameters createEventParameters(long causalityToken, int catalogVersion) {
+        return new AlterZoneEventParameters(causalityToken, catalogVersion, descriptor);
+    }
+
+    @Override
+    public Catalog applyUpdate(Catalog catalog, VersionedUpdate update) {
+        return new Catalog(
+                update.version(),
+                update.activationTimestamp(),
+                catalog.objectIdGenState(),
+                catalog.zones().stream()
+                        .map(z -> z.id() == descriptor.id() ? descriptor : z)

Review Comment:
   `z`, is this the original name?



##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/events/AlterZoneEventParameters.java:
##########
@@ -23,25 +23,25 @@
  * Alter zone event parameters contains a distribution zone descriptor for newly created distribution zone.
  */
 public class AlterZoneEventParameters extends CatalogEventParameters {
-
-    private final CatalogZoneDescriptor zoneDescriptor;
+    private final CatalogZoneDescriptor descriptor;

Review Comment:
   I won't be leaving the same comment over and over again. I see these renames multiple times later.



##########
modules/catalog/src/main/java/org/apache/ignite/internal/catalog/storage/UpdateEntry.java:
##########
@@ -18,9 +18,11 @@
 package org.apache.ignite.internal.catalog.storage;
 
 import java.io.Serializable;
+import org.apache.ignite.internal.catalog.Catalog;
 
 /**
- * A marker interface describing a particular change within the {@link VersionedUpdate group}.
+ * Interface describing a particular change within the {@link VersionedUpdate group}.
  */
 public interface UpdateEntry extends Serializable {
+    Catalog applyUpdate(Catalog catalog, VersionedUpdate update);

Review Comment:
   Where's 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