|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.drill.exec.planner.sql.handlers; |
| 19 | + |
| 20 | +import com.google.common.collect.Lists; |
| 21 | +import org.apache.calcite.sql.SqlCharStringLiteral; |
| 22 | +import org.apache.calcite.sql.SqlNode; |
| 23 | +import org.apache.drill.common.exceptions.DrillRuntimeException; |
| 24 | +import org.apache.drill.exec.exception.VersionMismatchException; |
| 25 | +import org.apache.drill.exec.physical.PhysicalPlan; |
| 26 | +import org.apache.drill.exec.planner.sql.DirectPlan; |
| 27 | +import org.apache.drill.exec.planner.sql.parser.SqlDropDaffodilSchema; |
| 28 | +import org.apache.drill.exec.proto.UserBitShared.Jar; |
| 29 | +import org.apache.drill.exec.proto.UserBitShared.Registry; |
| 30 | +import org.apache.drill.exec.schema.daffodil.RemoteDaffodilSchemaRegistry; |
| 31 | +import org.apache.drill.exec.store.sys.store.DataChangeVersion; |
| 32 | +import org.apache.drill.exec.work.foreman.ForemanSetupException; |
| 33 | +import org.apache.hadoop.fs.FileSystem; |
| 34 | +import org.apache.hadoop.fs.Path; |
| 35 | +import org.slf4j.Logger; |
| 36 | +import org.slf4j.LoggerFactory; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +public class DropDaffodilSchemaHandler extends DefaultSqlHandler { |
| 42 | + private static Logger logger = LoggerFactory.getLogger(DropDaffodilSchemaHandler.class); |
| 43 | + |
| 44 | + public DropDaffodilSchemaHandler(SqlHandlerConfig config) { |
| 45 | + super(config); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Unregisters Daffodil schema JARs dynamically. Process consists of several steps: |
| 50 | + * <ol> |
| 51 | + * <li>Registering jar in jar registry to ensure that the jar is not being unregistered elsewhere.</li> |
| 52 | + * <li>Starts remote unregistration process, gets list of all jars and excludes jar to be deleted.</li> |
| 53 | + * <li>Removes jar from registry area.</li> |
| 54 | + * </ol> |
| 55 | + * |
| 56 | + * Only jars registered dynamically can be unregistered. |
| 57 | + * |
| 58 | + * Limitation: before jar unregistration make sure no one is using schemas from this jar. |
| 59 | + * There is no guarantee that running queries will finish successfully or give correct result. |
| 60 | + * |
| 61 | + * @return - Single row indicating successful unregistration, raise exception otherwise |
| 62 | + */ |
| 63 | + @Override |
| 64 | + public PhysicalPlan getPlan(SqlNode sqlNode) throws ForemanSetupException, IOException { |
| 65 | + SqlDropDaffodilSchema node = unwrap(sqlNode, SqlDropDaffodilSchema.class); |
| 66 | + String jarName = ((SqlCharStringLiteral) node.getJar()).toValue(); |
| 67 | + RemoteDaffodilSchemaRegistry remoteSchemaRegistry = context.getDaffodilSchemaRegistry(); |
| 68 | + |
| 69 | + boolean inProgress = false; |
| 70 | + try { |
| 71 | + final String action = remoteSchemaRegistry.addToJars(jarName, RemoteDaffodilSchemaRegistry.Action.UNREGISTRATION); |
| 72 | + if (!(inProgress = action == null)) { |
| 73 | + return DirectPlan.createDirectPlan(context, false, String.format("Jar with %s name is used. Action: %s", jarName, action)); |
| 74 | + } |
| 75 | + |
| 76 | + Jar deletedJar = unregister(jarName, remoteSchemaRegistry); |
| 77 | + if (deletedJar == null) { |
| 78 | + return DirectPlan.createDirectPlan(context, false, String.format("Jar %s is not registered in remote registry", jarName)); |
| 79 | + } |
| 80 | + |
| 81 | + removeJarFromArea(jarName, remoteSchemaRegistry.getFs(), remoteSchemaRegistry.getRegistryArea()); |
| 82 | + |
| 83 | + return DirectPlan.createDirectPlan(context, true, |
| 84 | + String.format("Daffodil schema jar %s has been unregistered successfully.", jarName)); |
| 85 | + |
| 86 | + } catch (Exception e) { |
| 87 | + logger.error("Error during Daffodil schema unregistration", e); |
| 88 | + return DirectPlan.createDirectPlan(context, false, e.getMessage()); |
| 89 | + } finally { |
| 90 | + if (inProgress) { |
| 91 | + remoteSchemaRegistry.removeFromJars(jarName); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Gets remote schema registry with version. |
| 98 | + * Version is used to ensure that we update the same registry we removed jars from. |
| 99 | + * Looks for a jar to be deleted, if found, |
| 100 | + * attempts to update remote registry with list of jars, that excludes jar to be deleted. |
| 101 | + * If during update {@link VersionMismatchException} was detected, |
| 102 | + * attempts to repeat unregistration process till retry attempts exceeds the limit. |
| 103 | + * If retry attempts number hits 0, throws exception that failed to update remote schema registry. |
| 104 | + * |
| 105 | + * @param jarName jar name |
| 106 | + * @param remoteSchemaRegistry remote schema registry |
| 107 | + * @return jar that was unregistered, null otherwise |
| 108 | + */ |
| 109 | + private Jar unregister(String jarName, RemoteDaffodilSchemaRegistry remoteSchemaRegistry) { |
| 110 | + int retryAttempts = remoteSchemaRegistry.getRetryAttempts(); |
| 111 | + while (retryAttempts >= 0) { |
| 112 | + DataChangeVersion version = new DataChangeVersion(); |
| 113 | + Registry registry = remoteSchemaRegistry.getRegistry(version); |
| 114 | + Jar jarToBeDeleted = null; |
| 115 | + List<Jar> jars = Lists.newArrayList(); |
| 116 | + for (Jar j : registry.getJarList()) { |
| 117 | + if (j.getName().equals(jarName)) { |
| 118 | + jarToBeDeleted = j; |
| 119 | + } else { |
| 120 | + jars.add(j); |
| 121 | + } |
| 122 | + } |
| 123 | + if (jarToBeDeleted == null) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + Registry updatedRegistry = Registry.newBuilder().addAllJar(jars).build(); |
| 127 | + try { |
| 128 | + remoteSchemaRegistry.updateRegistry(updatedRegistry, version); |
| 129 | + return jarToBeDeleted; |
| 130 | + } catch (VersionMismatchException ex) { |
| 131 | + logger.debug("Failed to update schema registry during unregistration, version mismatch was detected.", ex); |
| 132 | + retryAttempts--; |
| 133 | + } |
| 134 | + } |
| 135 | + throw new DrillRuntimeException("Failed to update remote schema registry. Exceeded retry attempts limit."); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Removes jar from indicated area, in case of error log it and proceeds. |
| 140 | + * |
| 141 | + * @param jarName jar name |
| 142 | + * @param fs file system |
| 143 | + * @param area path to area |
| 144 | + */ |
| 145 | + private void removeJarFromArea(String jarName, FileSystem fs, Path area) { |
| 146 | + try { |
| 147 | + fs.delete(new Path(area, jarName), false); |
| 148 | + } catch (IOException e) { |
| 149 | + logger.error("Error removing jar {} from area {}", jarName, area.toUri().getPath()); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments