|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.gravitino.listener; |
| 21 | + |
| 22 | +import java.util.Map; |
| 23 | +import org.apache.gravitino.NameIdentifier; |
| 24 | +import org.apache.gravitino.Namespace; |
| 25 | +import org.apache.gravitino.catalog.ModelDispatcher; |
| 26 | +import org.apache.gravitino.exceptions.ModelAlreadyExistsException; |
| 27 | +import org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException; |
| 28 | +import org.apache.gravitino.exceptions.NoSuchModelException; |
| 29 | +import org.apache.gravitino.exceptions.NoSuchModelVersionException; |
| 30 | +import org.apache.gravitino.exceptions.NoSuchSchemaException; |
| 31 | +import org.apache.gravitino.listener.api.event.DeleteModelPreEvent; |
| 32 | +import org.apache.gravitino.listener.api.event.DeleteModelVersionPreEvent; |
| 33 | +import org.apache.gravitino.listener.api.event.GetModelPreEvent; |
| 34 | +import org.apache.gravitino.listener.api.event.GetModelVersionPreEvent; |
| 35 | +import org.apache.gravitino.listener.api.event.LinkModelVersionPreEvent; |
| 36 | +import org.apache.gravitino.listener.api.event.ListModelPreEvent; |
| 37 | +import org.apache.gravitino.listener.api.event.ListModelVersionPreEvent; |
| 38 | +import org.apache.gravitino.listener.api.event.RegisterAndLinkModelPreEvent; |
| 39 | +import org.apache.gravitino.listener.api.event.RegisterModelPreEvent; |
| 40 | +import org.apache.gravitino.listener.api.info.ModelInfo; |
| 41 | +import org.apache.gravitino.listener.api.info.ModelVersionInfo; |
| 42 | +import org.apache.gravitino.model.Model; |
| 43 | +import org.apache.gravitino.model.ModelVersion; |
| 44 | +import org.apache.gravitino.utils.PrincipalUtils; |
| 45 | + |
| 46 | +/** |
| 47 | + * {@code ModelEventDispatcher} is a decorator for {@link ModelDispatcher} that not only delegates |
| 48 | + * model operations to the underlying catalog dispatcher but also dispatches corresponding events to |
| 49 | + * an {@link org.apache.gravitino.listener.EventBus} after each operation is completed. This allows |
| 50 | + * for event-driven workflows or monitoring of model operations. |
| 51 | + */ |
| 52 | +public class ModelEventDispatcher implements ModelDispatcher { |
| 53 | + private final EventBus eventBus; |
| 54 | + private final ModelDispatcher dispatcher; |
| 55 | + |
| 56 | + /** |
| 57 | + * Constructs a {@link ModelEventDispatcher} with a specified EventBus and {@link |
| 58 | + * ModelDispatcher}. |
| 59 | + * |
| 60 | + * @param eventBus The EventBus to which events will be dispatched. |
| 61 | + * @param dispatcher The underlying {@link ModelDispatcher} that will perform the actual model |
| 62 | + * operations. |
| 63 | + */ |
| 64 | + public ModelEventDispatcher(EventBus eventBus, ModelDispatcher dispatcher) { |
| 65 | + this.eventBus = eventBus; |
| 66 | + this.dispatcher = dispatcher; |
| 67 | + } |
| 68 | + |
| 69 | + /** {@inheritDoc} */ |
| 70 | + @Override |
| 71 | + public Model registerModel(NameIdentifier ident, String comment, Map<String, String> properties) |
| 72 | + throws NoSuchSchemaException, ModelAlreadyExistsException { |
| 73 | + ModelInfo registerRequest = new ModelInfo(ident.name(), properties, comment); |
| 74 | + eventBus.dispatchEvent( |
| 75 | + new RegisterModelPreEvent(PrincipalUtils.getCurrentUserName(), ident, registerRequest)); |
| 76 | + try { |
| 77 | + Model model = dispatcher.registerModel(ident, comment, properties); |
| 78 | + // TODO: ModelEvent |
| 79 | + return model; |
| 80 | + } catch (Exception e) { |
| 81 | + // TODO: failureEvent |
| 82 | + throw e; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** {@inheritDoc} */ |
| 87 | + @Override |
| 88 | + public Model registerModel( |
| 89 | + NameIdentifier ident, |
| 90 | + String uri, |
| 91 | + String[] aliases, |
| 92 | + String comment, |
| 93 | + Map<String, String> properties) |
| 94 | + throws NoSuchSchemaException, ModelAlreadyExistsException, |
| 95 | + ModelVersionAliasesAlreadyExistException { |
| 96 | + ModelInfo registerModelRequest = new ModelInfo(ident.name(), properties, comment); |
| 97 | + ModelVersionInfo linkModelVersionRequest = |
| 98 | + new ModelVersionInfo(uri, comment, properties, aliases); |
| 99 | + |
| 100 | + RegisterAndLinkModelPreEvent registerAndLinkModelPreEvent = |
| 101 | + new RegisterAndLinkModelPreEvent( |
| 102 | + PrincipalUtils.getCurrentUserName(), |
| 103 | + ident, |
| 104 | + registerModelRequest, |
| 105 | + linkModelVersionRequest); |
| 106 | + eventBus.dispatchEvent(registerAndLinkModelPreEvent); |
| 107 | + try { |
| 108 | + // TODO: ModelEvent |
| 109 | + return dispatcher.registerModel(ident, uri, aliases, comment, properties); |
| 110 | + } catch (Exception e) { |
| 111 | + throw e; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** {@inheritDoc} */ |
| 116 | + @Override |
| 117 | + public Model getModel(NameIdentifier ident) throws NoSuchModelException { |
| 118 | + eventBus.dispatchEvent(new GetModelPreEvent(PrincipalUtils.getCurrentUserName(), ident)); |
| 119 | + try { |
| 120 | + Model model = dispatcher.getModel(ident); |
| 121 | + // TODO: ModelEvent |
| 122 | + return model; |
| 123 | + } catch (Exception e) { |
| 124 | + // TODO: failureEvent |
| 125 | + throw e; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** {@inheritDoc} */ |
| 130 | + @Override |
| 131 | + public boolean deleteModel(NameIdentifier ident) { |
| 132 | + eventBus.dispatchEvent(new DeleteModelPreEvent(PrincipalUtils.getCurrentUserName(), ident)); |
| 133 | + try { |
| 134 | + // TODO: ModelEvent |
| 135 | + return dispatcher.deleteModel(ident); |
| 136 | + } catch (Exception e) { |
| 137 | + // TODO: failureEvent |
| 138 | + throw e; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** {@inheritDoc} */ |
| 143 | + @Override |
| 144 | + public NameIdentifier[] listModels(Namespace namespace) throws NoSuchSchemaException { |
| 145 | + eventBus.dispatchEvent(new ListModelPreEvent(PrincipalUtils.getCurrentUserName(), namespace)); |
| 146 | + try { |
| 147 | + NameIdentifier[] models = dispatcher.listModels(namespace); |
| 148 | + // TODO: ModelEvent |
| 149 | + return models; |
| 150 | + } catch (Exception e) { |
| 151 | + // TODO: failureEvent |
| 152 | + throw e; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /** {@inheritDoc} */ |
| 157 | + @Override |
| 158 | + public void linkModelVersion( |
| 159 | + NameIdentifier ident, |
| 160 | + String uri, |
| 161 | + String[] aliases, |
| 162 | + String comment, |
| 163 | + Map<String, String> properties) |
| 164 | + throws NoSuchModelException, ModelVersionAliasesAlreadyExistException { |
| 165 | + ModelVersionInfo linkModelRequest = new ModelVersionInfo(uri, comment, properties, aliases); |
| 166 | + eventBus.dispatchEvent( |
| 167 | + new LinkModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, linkModelRequest)); |
| 168 | + try { |
| 169 | + dispatcher.linkModelVersion(ident, uri, aliases, comment, properties); |
| 170 | + // TODO: ModelEvent |
| 171 | + } catch (Exception e) { |
| 172 | + // TODO: failureEvent |
| 173 | + throw e; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** {@inheritDoc} */ |
| 178 | + @Override |
| 179 | + public ModelVersion getModelVersion(NameIdentifier ident, int version) |
| 180 | + throws NoSuchModelVersionException { |
| 181 | + eventBus.dispatchEvent( |
| 182 | + new GetModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, null, version)); |
| 183 | + try { |
| 184 | + // TODO: ModelEvent |
| 185 | + return dispatcher.getModelVersion(ident, version); |
| 186 | + } catch (Exception e) { |
| 187 | + // TODO: failureEvent |
| 188 | + throw e; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** {@inheritDoc} */ |
| 193 | + @Override |
| 194 | + public ModelVersion getModelVersion(NameIdentifier ident, String alias) |
| 195 | + throws NoSuchModelVersionException { |
| 196 | + eventBus.dispatchEvent( |
| 197 | + new GetModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, alias, null)); |
| 198 | + try { |
| 199 | + ModelVersion modelVersion = dispatcher.getModelVersion(ident, alias); |
| 200 | + // TODO: ModelEvent |
| 201 | + return modelVersion; |
| 202 | + } catch (Exception e) { |
| 203 | + // TODO: failureEvent |
| 204 | + throw e; |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + /** {@inheritDoc} */ |
| 209 | + @Override |
| 210 | + public boolean deleteModelVersion(NameIdentifier ident, int version) { |
| 211 | + eventBus.dispatchEvent( |
| 212 | + new DeleteModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, null, version)); |
| 213 | + try { |
| 214 | + boolean isExists = dispatcher.deleteModelVersion(ident, version); |
| 215 | + // TODO: ModelEvent |
| 216 | + return isExists; |
| 217 | + } catch (Exception e) { |
| 218 | + // TODO: failureEvent |
| 219 | + throw e; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + /** {@inheritDoc} */ |
| 224 | + @Override |
| 225 | + public boolean deleteModelVersion(NameIdentifier ident, String alias) { |
| 226 | + eventBus.dispatchEvent( |
| 227 | + new DeleteModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, alias, null)); |
| 228 | + try { |
| 229 | + boolean isExists = dispatcher.deleteModelVersion(ident, alias); |
| 230 | + // TODO: ModelEvent |
| 231 | + return isExists; |
| 232 | + } catch (Exception e) { |
| 233 | + // TODO: failureEvent |
| 234 | + throw e; |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + /** {@inheritDoc} */ |
| 239 | + @Override |
| 240 | + public int[] listModelVersions(NameIdentifier ident) throws NoSuchModelException { |
| 241 | + eventBus.dispatchEvent( |
| 242 | + new ListModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident)); |
| 243 | + try { |
| 244 | + int[] versions = dispatcher.listModelVersions(ident); |
| 245 | + // TODO: ModelEvent |
| 246 | + return versions; |
| 247 | + } catch (Exception e) { |
| 248 | + // TODO: failureEvent |
| 249 | + throw e; |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + /** {@inheritDoc} */ |
| 254 | + @Override |
| 255 | + public boolean modelExists(NameIdentifier ident) { |
| 256 | + return dispatcher.modelExists(ident); |
| 257 | + } |
| 258 | + |
| 259 | + /** {@inheritDoc} */ |
| 260 | + @Override |
| 261 | + public boolean modelVersionExists(NameIdentifier ident, int version) { |
| 262 | + return dispatcher.modelVersionExists(ident, version); |
| 263 | + } |
| 264 | + |
| 265 | + /** {@inheritDoc} */ |
| 266 | + @Override |
| 267 | + public boolean modelVersionExists(NameIdentifier ident, String alias) { |
| 268 | + return dispatcher.modelVersionExists(ident, alias); |
| 269 | + } |
| 270 | +} |
0 commit comments