You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2020/09/15 06:56:12 UTC

[GitHub] [hive] sankarh commented on a change in pull request #1237: HIVE-23618: Enable event replication for default/check constraints

sankarh commented on a change in pull request #1237:
URL: https://github.com/apache/hive/pull/1237#discussion_r488391678



##########
File path: hcatalog/server-extensions/src/main/java/org/apache/hive/hcatalog/listener/DbNotificationListener.java
##########
@@ -703,6 +709,49 @@ public void onAddNotNullConstraint(AddNotNullConstraintEvent addNotNullConstrain
     }
   }
 
+  /***
+   * @param addDefaultConstraintEvent add default constraint event
+   * @throws MetaException
+   */
+  @Override
+  public void onAddDefaultConstraint(AddDefaultConstraintEvent addDefaultConstraintEvent) throws MetaException {
+    List<SQLDefaultConstraint> cols = addDefaultConstraintEvent.getDefaultConstraintCols();
+    if (cols.size() > 0) {
+      AddDefaultConstraintMessage msg = MessageBuilder.getInstance()
+        .buildAddDefaultConstraintMessage(addDefaultConstraintEvent.getDefaultConstraintCols());

Review comment:
       Shall use "cols" here.

##########
File path: hcatalog/server-extensions/src/main/java/org/apache/hive/hcatalog/listener/DbNotificationListener.java
##########
@@ -703,6 +709,49 @@ public void onAddNotNullConstraint(AddNotNullConstraintEvent addNotNullConstrain
     }
   }
 
+  /***
+   * @param addDefaultConstraintEvent add default constraint event
+   * @throws MetaException
+   */
+  @Override
+  public void onAddDefaultConstraint(AddDefaultConstraintEvent addDefaultConstraintEvent) throws MetaException {
+    List<SQLDefaultConstraint> cols = addDefaultConstraintEvent.getDefaultConstraintCols();
+    if (cols.size() > 0) {
+      AddDefaultConstraintMessage msg = MessageBuilder.getInstance()
+        .buildAddDefaultConstraintMessage(addDefaultConstraintEvent.getDefaultConstraintCols());
+      NotificationEvent event =
+        new NotificationEvent(0, now(), EventType.ADD_DEFAULTCONSTRAINT.toString(),
+          msgEncoder.getSerializer().serialize(msg)
+        );
+      event.setCatName(cols.get(0).isSetCatName() ? cols.get(0).getCatName() : DEFAULT_CATALOG_NAME);
+      event.setDbName(cols.get(0).getTable_db());
+      event.setTableName(cols.get(0).getTable_name());
+      process(event, addDefaultConstraintEvent);
+    }
+  }
+
+  /***
+   * @param addCheckConstraintEvent add check constraint event
+   * @throws MetaException
+   */
+  @Override
+  public void onAddCheckConstraint(AddCheckConstraintEvent addCheckConstraintEvent) throws MetaException {
+    LOG.info("Inside DBNotification listener for check constraint.");
+    List<SQLCheckConstraint> cols = addCheckConstraintEvent.getCheckConstraintCols();
+    if (cols.size() > 0) {
+      AddCheckConstraintMessage msg = MessageBuilder.getInstance()
+        .buildAddCheckConstraintMessage(addCheckConstraintEvent.getCheckConstraintCols());

Review comment:
       Use "cols".

##########
File path: standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/events/AddCheckConstraintEvent.java
##########
@@ -0,0 +1,42 @@
+/*
+ * 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.hadoop.hive.metastore.events;
+
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.hadoop.hive.metastore.IHMSHandler;
+import org.apache.hadoop.hive.metastore.api.SQLCheckConstraint;
+
+import java.util.List;
+
+@InterfaceAudience.Public
+@InterfaceStability.Stable
+public class AddCheckConstraintEvent extends ListenerEvent {
+  private final List<SQLCheckConstraint> ds;

Review comment:
       nit: Use "cc" instead of "ds"

##########
File path: standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/messaging/MessageBuilder.java
##########
@@ -241,6 +247,16 @@ public AddNotNullConstraintMessage buildAddNotNullConstraintMessage(
     return new JSONAddNotNullConstraintMessage(MS_SERVER_URL, MS_SERVICE_PRINCIPAL, nns, now());
   }
 
+  public AddDefaultConstraintMessage buildAddDefaultConstraintMessage(
+    List<SQLDefaultConstraint> dcs) {
+    return new JSONAddDefaultConstraintMessage(MS_SERVER_URL, MS_SERVICE_PRINCIPAL, dcs, now());
+  }
+
+  public AddCheckConstraintMessage buildAddCheckConstraintMessage(
+    List<SQLCheckConstraint> dcs) {

Review comment:
       nit: "ccs" instead of "dcs".

##########
File path: itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/parse/TestReplicationScenarios.java
##########
@@ -3243,6 +3265,15 @@ public void testConstraints() throws IOException {
       assertTrue(fks.isEmpty());
       List<SQLNotNullConstraint> nns = metaStoreClientMirror.getNotNullConstraints(new NotNullConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName , "tbl6"));
       assertTrue(nns.isEmpty());
+      List<SQLDefaultConstraint> dks = metaStoreClientMirror.getDefaultConstraints(new DefaultConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName , "tbl10"));
+      assertTrue(dks.isEmpty());
+      List<SQLCheckConstraint> cks = metaStoreClientMirror.getCheckConstraints(new CheckConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName , "tbl9"));
+      assertTrue(cks.isEmpty());
+      dks = metaStoreClientMirror.getDefaultConstraints(new DefaultConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName , "tbl12"));
+      assertTrue(dks.isEmpty());
+      cks = metaStoreClientMirror.getCheckConstraints(new CheckConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName , "tbl12"));

Review comment:
       nit: Remove extra space before ","

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/AddCheckConstraintHandler.java
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.hadoop.hive.ql.parse.repl.load.message;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.metastore.api.SQLCheckConstraint;
+import org.apache.hadoop.hive.metastore.messaging.AddCheckConstraintMessage;
+import org.apache.hadoop.hive.ql.ddl.DDLWork;
+import org.apache.hadoop.hive.ql.ddl.table.constraint.Constraints;
+import org.apache.hadoop.hive.ql.ddl.table.constraint.add.AlterTableAddConstraintDesc;
+import org.apache.hadoop.hive.ql.exec.Task;
+import org.apache.hadoop.hive.ql.exec.TaskFactory;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
+
+/**
+ * AddCheckConstraintHandler
+ * Target(Load) side handler for add check constraint event.
+ */
+public class AddCheckConstraintHandler extends AbstractMessageHandler {
+  @Override
+  public List<Task<?>> handle(Context context)
+    throws SemanticException {
+    AddCheckConstraintMessage msg = deserializer.getAddCheckConstraintMessage(context.dmd.getPayload());
+
+    List<SQLCheckConstraint> ccs;
+    try {
+      ccs = msg.getCheckConstraints();
+    } catch (Exception e) {
+      if (!(e instanceof SemanticException)){
+        throw new SemanticException("Error reading message members", e);
+      } else {
+        throw (SemanticException)e;
+      }
+    }
+
+    List<Task<?>> tasks = new ArrayList<Task<?>>();
+    if (ccs.isEmpty()) {
+      return tasks;
+    }
+
+    final String actualDbName = context.isDbNameEmpty() ? ccs.get(0).getTable_db() : context.dbName;
+    final String actualTblName = ccs.get(0).getTable_name();
+    final TableName tName = TableName.fromString(actualTblName, null, actualDbName);
+
+    for (SQLCheckConstraint nn : ccs) {

Review comment:
       nit: Variable name can be uniform nn -> ck

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/AddDefaultConstraintHandler.java
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.hadoop.hive.ql.parse.repl.load.message;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint;
+import org.apache.hadoop.hive.metastore.messaging.AddDefaultConstraintMessage;
+import org.apache.hadoop.hive.ql.ddl.DDLWork;
+import org.apache.hadoop.hive.ql.ddl.table.constraint.Constraints;
+import org.apache.hadoop.hive.ql.ddl.table.constraint.add.AlterTableAddConstraintDesc;
+import org.apache.hadoop.hive.ql.exec.Task;
+import org.apache.hadoop.hive.ql.exec.TaskFactory;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
+/**
+ * AddDefaultConstraintHandler
+ * Target(Load) side handler for add default constraint event.
+ */
+public class AddDefaultConstraintHandler extends AbstractMessageHandler {
+  @Override
+  public List<Task<?>> handle(Context context)
+    throws SemanticException {
+    AddDefaultConstraintMessage msg = deserializer.getAddDefaultConstraintMessage(context.dmd.getPayload());
+
+    List<SQLDefaultConstraint> dcs;
+    try {
+      dcs = msg.getDefaultConstraints();
+    } catch (Exception e) {
+      if (!(e instanceof SemanticException)){
+        throw new SemanticException("Error reading message members", e);
+      } else {
+        throw (SemanticException)e;
+      }
+    }
+
+    List<Task<?>> tasks = new ArrayList<Task<?>>();
+    if (dcs.isEmpty()) {
+      return tasks;
+    }
+
+    final String actualDbName = context.isDbNameEmpty() ? dcs.get(0).getTable_db() : context.dbName;
+    final String actualTblName = dcs.get(0).getTable_name();
+    final TableName tName = TableName.fromString(actualTblName, null, actualDbName);
+
+    for (SQLDefaultConstraint nn : dcs) {

Review comment:
       nit: nn -> dc

##########
File path: itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/parse/TestReplicationScenarios.java
##########
@@ -3195,13 +3201,17 @@ public void testConstraints() throws IOException {
       assertEquals(fks.size(), 2);
       List<SQLNotNullConstraint> nns = metaStoreClientMirror.getNotNullConstraints(new NotNullConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName , "tbl3"));
       assertEquals(nns.size(), 1);
+      List<SQLCheckConstraint> cks = metaStoreClientMirror.getCheckConstraints(new CheckConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName , "tbl7"));
+      assertEquals(cks.size(), 2);
     } catch (TException te) {
       assertNull(te);
     }
 
     run("CREATE TABLE " + dbName + ".tbl4(a string, b string, primary key (a, b) disable novalidate rely)", driver);
     run("CREATE TABLE " + dbName + ".tbl5(a string, b string, foreign key (a, b) references " + dbName + ".tbl4(a, b) disable novalidate)", driver);
     run("CREATE TABLE " + dbName + ".tbl6(a string, b string not null disable, unique (a) disable)", driver);
+    run("CREATE TABLE " + dbName + ".tbl9(a string CHECK (a like 'a%'), price double CHECK (price > 0 AND price <= 1000))", driver);
+    run("CREATE TABLE " + dbName + ".tbl10(a string, b int DEFAULT 0)", driver);

Review comment:
       Add ALTER operations to change the constraints and validate it for incremental repl.

##########
File path: standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/messaging/MessageDeserializer.java
##########
@@ -62,6 +62,10 @@ public EventMessage getEventMessage(String eventTypeString, String messageBody)
       return getAddUniqueConstraintMessage(messageBody);
     case ADD_NOTNULLCONSTRAINT:
       return getAddNotNullConstraintMessage(messageBody);
+    case ADD_DEFAULTCONSTRAINT:
+      return getAddDefaultConstraintMessage(messageBody);
+      case ADD_CHECKCONSTRAINT:

Review comment:
       nit: Statement alignment.

##########
File path: itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/parse/TestReplicationScenarios.java
##########
@@ -3243,6 +3265,15 @@ public void testConstraints() throws IOException {
       assertTrue(fks.isEmpty());
       List<SQLNotNullConstraint> nns = metaStoreClientMirror.getNotNullConstraints(new NotNullConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName , "tbl6"));
       assertTrue(nns.isEmpty());
+      List<SQLDefaultConstraint> dks = metaStoreClientMirror.getDefaultConstraints(new DefaultConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName , "tbl10"));
+      assertTrue(dks.isEmpty());
+      List<SQLCheckConstraint> cks = metaStoreClientMirror.getCheckConstraints(new CheckConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName , "tbl9"));
+      assertTrue(cks.isEmpty());
+      dks = metaStoreClientMirror.getDefaultConstraints(new DefaultConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName , "tbl12"));

Review comment:
       nit: Remove extra space before ","




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org