|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.inference.migration; |
| 9 | + |
| 10 | +import org.elasticsearch.core.Nullable; |
| 11 | +import org.elasticsearch.core.Strings; |
| 12 | +import org.elasticsearch.inference.Model; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import java.util.Set; |
| 16 | + |
| 17 | +/** |
| 18 | + * Unified interface for endpoint migrations supporting both single-endpoint |
| 19 | + * and batch execution modes. Migrations can be run lazily when endpoints are |
| 20 | + * read (single-endpoint) or efficiently during batch operations (batch mode). |
| 21 | + */ |
| 22 | +public interface EndpointMigration { |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns the target version this migration migrates endpoints to. |
| 26 | + * Migrations are executed in order from lowest to highest target version. |
| 27 | + * |
| 28 | + * @return the target version number |
| 29 | + */ |
| 30 | + long getTargetVersion(); |
| 31 | + |
| 32 | + /** |
| 33 | + * Returns the set of service names this migration applies to. |
| 34 | + * Only endpoints from these services will be considered for migration. |
| 35 | + * |
| 36 | + * @return set of applicable service names |
| 37 | + */ |
| 38 | + Set<String> getApplicableServices(); |
| 39 | + |
| 40 | + /** |
| 41 | + * Checks if this migration is applicable to the given model. |
| 42 | + * This is used to filter which migrations should be run for a specific endpoint. |
| 43 | + * |
| 44 | + * @param model the model to check |
| 45 | + * @return true if this migration applies to the model |
| 46 | + */ |
| 47 | + default boolean isApplicable(Model model) { |
| 48 | + return getApplicableServices().contains(model.getConfigurations().getService()); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns whether this migration supports batch execution mode. |
| 53 | + * Batch migrations can process multiple endpoints efficiently with access |
| 54 | + * to migration context data (e.g., authorization responses). |
| 55 | + * |
| 56 | + * @return true if batch mode is supported, false otherwise |
| 57 | + */ |
| 58 | + default boolean supportsBatch() { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Migrates a single endpoint. This method is called for single-endpoint |
| 64 | + * migrations when endpoints are read (lazy migration). |
| 65 | + * |
| 66 | + * @param model the model to migrate |
| 67 | + * @param context optional migration context (may be null for single-endpoint migrations) |
| 68 | + * @return the migration result |
| 69 | + */ |
| 70 | + MigrationResult migrate(Model model, @Nullable MigrationContext context); |
| 71 | + |
| 72 | + /** |
| 73 | + * Migrates multiple endpoints in batch mode. This method is called when |
| 74 | + * batch migrations are supported and context data is available. |
| 75 | + * |
| 76 | + * @param models the models to migrate |
| 77 | + * @param context optional migration context (typically provided for batch migrations) |
| 78 | + * @return list of migration results, one per model |
| 79 | + */ |
| 80 | + default List<MigrationResult> migrateBatch(List<Model> models, @Nullable MigrationContext context) { |
| 81 | + throw new UnsupportedOperationException("Batch migration not supported by this migration"); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Record representing a successfully migrated model. |
| 86 | + * |
| 87 | + * @param model the migrated model |
| 88 | + */ |
| 89 | + record MigratedModel(Model model) { |
| 90 | + public MigratedModel { |
| 91 | + if (model == null) { |
| 92 | + throw new IllegalArgumentException("Migrated model cannot be null"); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Record representing the result of a migration operation. |
| 99 | + * |
| 100 | + * @param success whether the migration succeeded |
| 101 | + * @param migratedModel the migrated model if successful, null otherwise |
| 102 | + * @param errorMessage error message if migration failed, null otherwise |
| 103 | + */ |
| 104 | + record MigrationResult(boolean success, @Nullable MigratedModel migratedModel, @Nullable String errorMessage) { |
| 105 | + |
| 106 | + /** |
| 107 | + * Creates a successful migration result. |
| 108 | + * |
| 109 | + * @param model the migrated model |
| 110 | + * @return successful migration result |
| 111 | + */ |
| 112 | + public static MigrationResult success(Model model) { |
| 113 | + return new MigrationResult(true, new MigratedModel(model), null); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Creates a failed migration result. |
| 118 | + * |
| 119 | + * @param errorMessage the error message describing the failure |
| 120 | + * @return failed migration result |
| 121 | + */ |
| 122 | + public static MigrationResult failure(String errorMessage) { |
| 123 | + return new MigrationResult(false, null, errorMessage); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Creates a failed migration result with a formatted error message. |
| 128 | + * |
| 129 | + * @param format the format string |
| 130 | + * @param args the format arguments |
| 131 | + * @return failed migration result |
| 132 | + */ |
| 133 | + public static MigrationResult failure(String format, Object... args) { |
| 134 | + return failure(Strings.format(format, args)); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments