Skip to content

Commit d1c17b8

Browse files
Adding initial migration classes
1 parent f15069a commit d1c17b8

File tree

5 files changed

+523
-0
lines changed

5 files changed

+523
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.xpack.inference.services.elastic.authorization.ElasticInferenceServiceAuthorizationModel;
11+
12+
import java.util.Objects;
13+
14+
/**
15+
* Migration context for EIS (Elastic Inference Service) migrations.
16+
* Contains the authorization model which provides access to endpoint metadata
17+
* needed for batch migrations.
18+
*/
19+
public record EISMigrationContext(ElasticInferenceServiceAuthorizationModel authModel) implements MigrationContext {
20+
21+
public EISMigrationContext {
22+
Objects.requireNonNull(authModel);
23+
}
24+
25+
@Override
26+
public ContextType getContextType() {
27+
return ContextType.EIS;
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
/**
11+
* Empty migration context for generic migrations that don't require
12+
* migration-specific data. Used for single-endpoint migrations that
13+
* only need the endpoint data itself.
14+
*/
15+
public enum EmptyMigrationContext implements MigrationContext {
16+
INSTANCE;
17+
18+
@Override
19+
public ContextType getContextType() {
20+
return ContextType.EMPTY;
21+
}
22+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
/**
11+
* Context interface for passing migration-specific data to batch migrations.
12+
* Different migration types can use different context implementations to pass
13+
* the data they need (e.g., authorization models, service-specific data).
14+
*/
15+
public interface MigrationContext {
16+
/**
17+
* Returns the type of this context, which can be used to safely cast
18+
* the context to the appropriate implementation type.
19+
*
20+
* @return the context type
21+
*/
22+
ContextType getContextType();
23+
24+
/**
25+
* Enumeration of supported context types for type-safe context access.
26+
*/
27+
enum ContextType {
28+
EIS,
29+
EMPTY
30+
}
31+
}

0 commit comments

Comments
 (0)