8
8
9
9
package org .opensearch .index .engine ;
10
10
11
+ import org .apache .logging .log4j .Logger ;
11
12
import org .apache .lucene .analysis .Analyzer ;
12
13
import org .apache .lucene .index .MergePolicy ;
13
14
import org .apache .lucene .search .QueryCache ;
14
15
import org .apache .lucene .search .QueryCachingPolicy ;
15
16
import org .apache .lucene .search .ReferenceManager ;
16
17
import org .apache .lucene .search .Sort ;
17
18
import org .apache .lucene .search .similarities .Similarity ;
19
+ import org .opensearch .common .Nullable ;
18
20
import org .opensearch .common .unit .TimeValue ;
19
21
import org .opensearch .index .IndexSettings ;
20
22
import org .opensearch .index .codec .CodecService ;
23
+ import org .opensearch .index .codec .CodecServiceConfig ;
24
+ import org .opensearch .index .codec .CodecServiceFactory ;
25
+ import org .opensearch .index .mapper .MapperService ;
21
26
import org .opensearch .index .seqno .RetentionLeases ;
22
27
import org .opensearch .index .shard .ShardId ;
23
28
import org .opensearch .index .store .Store ;
39
44
* A factory to create an EngineConfig based on custom plugin overrides
40
45
*/
41
46
public class EngineConfigFactory {
42
- private final CodecService codecService ;
47
+ private final CodecServiceFactory codecServiceFactory ;
43
48
private final TranslogDeletionPolicyFactory translogDeletionPolicyFactory ;
44
49
45
50
/** default ctor primarily used for tests without plugins */
@@ -58,14 +63,16 @@ public EngineConfigFactory(PluginsService pluginsService, IndexSettings idxSetti
58
63
EngineConfigFactory (Collection <EnginePlugin > enginePlugins , IndexSettings idxSettings ) {
59
64
Optional <CodecService > codecService = Optional .empty ();
60
65
String codecServiceOverridingPlugin = null ;
66
+ Optional <CodecServiceFactory > codecServiceFactory = Optional .empty ();
67
+ String codecServiceFactoryOverridingPlugin = null ;
61
68
Optional <TranslogDeletionPolicyFactory > translogDeletionPolicyFactory = Optional .empty ();
62
69
String translogDeletionPolicyOverridingPlugin = null ;
63
70
for (EnginePlugin enginePlugin : enginePlugins ) {
64
71
// get overriding codec service from EnginePlugin
65
72
if (codecService .isPresent () == false ) {
66
73
codecService = enginePlugin .getCustomCodecService (idxSettings );
67
74
codecServiceOverridingPlugin = enginePlugin .getClass ().getName ();
68
- } else {
75
+ } else if ( enginePlugin . getCustomCodecService ( idxSettings ). isPresent ()) {
69
76
throw new IllegalStateException (
70
77
"existing codec service already overridden in: "
71
78
+ codecServiceOverridingPlugin
@@ -76,20 +83,45 @@ public EngineConfigFactory(PluginsService pluginsService, IndexSettings idxSetti
76
83
if (translogDeletionPolicyFactory .isPresent () == false ) {
77
84
translogDeletionPolicyFactory = enginePlugin .getCustomTranslogDeletionPolicyFactory ();
78
85
translogDeletionPolicyOverridingPlugin = enginePlugin .getClass ().getName ();
79
- } else {
86
+ } else if ( enginePlugin . getCustomTranslogDeletionPolicyFactory (). isPresent ()) {
80
87
throw new IllegalStateException (
81
88
"existing TranslogDeletionPolicyFactory is already overridden in: "
82
89
+ translogDeletionPolicyOverridingPlugin
83
90
+ " attempting to override again by: "
84
91
+ enginePlugin .getClass ().getName ()
85
92
);
86
93
}
94
+ // get overriding CodecServiceFactory from EnginePlugin
95
+ if (codecServiceFactory .isPresent () == false ) {
96
+ codecServiceFactory = enginePlugin .getCustomCodecServiceFactory (idxSettings );
97
+ codecServiceFactoryOverridingPlugin = enginePlugin .getClass ().getName ();
98
+ } else if (enginePlugin .getCustomCodecServiceFactory (idxSettings ).isPresent ()) {
99
+ throw new IllegalStateException (
100
+ "existing codec service factory already overridden in: "
101
+ + codecServiceFactoryOverridingPlugin
102
+ + " attempting to override again by: "
103
+ + enginePlugin .getClass ().getName ()
104
+ );
105
+ }
106
+ }
107
+
108
+ if (codecService .isPresent () && codecServiceFactory .isPresent ()) {
109
+ throw new IllegalStateException (
110
+ "both codec service and codec service factory are present, codec service provided by: "
111
+ + codecServiceOverridingPlugin
112
+ + " conflicts with codec service factory provided by: "
113
+ + codecServiceFactoryOverridingPlugin
114
+ );
87
115
}
88
- this .codecService = codecService .orElse (null );
116
+
117
+ final CodecService instance = codecService .orElse (null );
118
+ this .codecServiceFactory = (instance != null ) ? (config ) -> instance : codecServiceFactory .orElse (null );
89
119
this .translogDeletionPolicyFactory = translogDeletionPolicyFactory .orElse ((idxs , rtls ) -> null );
90
120
}
91
121
92
- /** Instantiates a new EngineConfig from the provided custom overrides */
122
+ /**
123
+ * Instantiates a new EngineConfig from the provided custom overrides
124
+ */
93
125
public EngineConfig newEngineConfig (
94
126
ShardId shardId ,
95
127
ThreadPool threadPool ,
@@ -114,6 +146,10 @@ public EngineConfig newEngineConfig(
114
146
LongSupplier primaryTermSupplier ,
115
147
EngineConfig .TombstoneDocSupplier tombstoneDocSupplier
116
148
) {
149
+ CodecService codecServiceToUse = codecService ;
150
+ if (codecService == null && this .codecServiceFactory != null ) {
151
+ codecServiceToUse = newCodecServiceOrDefault (indexSettings , null , null , null );
152
+ }
117
153
118
154
return new EngineConfig (
119
155
shardId ,
@@ -124,7 +160,7 @@ public EngineConfig newEngineConfig(
124
160
mergePolicy ,
125
161
analyzer ,
126
162
similarity ,
127
- this . codecService != null ? this . codecService : codecService ,
163
+ codecServiceToUse ,
128
164
eventListener ,
129
165
queryCache ,
130
166
queryCachingPolicy ,
@@ -141,4 +177,15 @@ public EngineConfig newEngineConfig(
141
177
tombstoneDocSupplier
142
178
);
143
179
}
180
+
181
+ public CodecService newCodecServiceOrDefault (
182
+ IndexSettings indexSettings ,
183
+ @ Nullable MapperService mapperService ,
184
+ Logger logger ,
185
+ CodecService defaultCodecService
186
+ ) {
187
+ return this .codecServiceFactory != null
188
+ ? this .codecServiceFactory .createCodecService (new CodecServiceConfig (indexSettings , mapperService , logger ))
189
+ : defaultCodecService ;
190
+ }
144
191
}
0 commit comments