-
Notifications
You must be signed in to change notification settings - Fork 59
/
ExtensionsRunner.java
754 lines (670 loc) · 31.6 KB
/
ExtensionsRunner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.sdk;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.action.ActionType;
import org.opensearch.action.support.TransportAction;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.io.stream.NamedWriteableRegistry;
import org.opensearch.extensions.rest.ExtensionRestRequest;
import org.opensearch.extensions.rest.RegisterRestActionsRequest;
import org.opensearch.extensions.settings.RegisterCustomSettingsRequest;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.discovery.InitializeExtensionRequest;
import org.opensearch.extensions.DiscoveryExtensionNode;
import org.opensearch.extensions.AddSettingsUpdateConsumerRequest;
import org.opensearch.extensions.UpdateSettingsRequest;
import org.opensearch.extensions.action.ExtensionActionRequest;
import org.opensearch.extensions.ExtensionsManager.RequestType;
import org.opensearch.extensions.ExtensionRequest;
import org.opensearch.extensions.ExtensionsManager;
import org.opensearch.index.IndicesModuleRequest;
import org.opensearch.rest.RestHandler.Route;
import org.opensearch.sdk.api.ActionExtension;
import org.opensearch.sdk.handlers.ClusterSettingsResponseHandler;
import org.opensearch.sdk.handlers.ClusterStateResponseHandler;
import org.opensearch.sdk.handlers.EnvironmentSettingsResponseHandler;
import org.opensearch.sdk.handlers.ExtensionActionRequestHandler;
import org.opensearch.sdk.action.SDKActionModule;
import org.opensearch.sdk.handlers.AcknowledgedResponseHandler;
import org.opensearch.sdk.handlers.ExtensionDependencyResponseHandler;
import org.opensearch.sdk.handlers.ExtensionsIndicesModuleNameRequestHandler;
import org.opensearch.sdk.handlers.ExtensionsIndicesModuleRequestHandler;
import org.opensearch.sdk.handlers.ExtensionsInitRequestHandler;
import org.opensearch.sdk.handlers.ExtensionsRestRequestHandler;
import org.opensearch.sdk.handlers.UpdateSettingsRequestHandler;
import org.opensearch.tasks.TaskManager;
import org.opensearch.threadpool.ExecutorBuilder;
import org.opensearch.threadpool.RunnableTaskExecutionListener;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.TransportResponse;
import org.opensearch.transport.TransportResponseHandler;
import org.opensearch.transport.TransportService;
import org.opensearch.transport.TransportSettings;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
* The primary class to run an extension.
* <p>
* During instantiation, this class reads extension points from the extension based on its implemented interfaces and registers necessary information with the {@link ExtensionsManager} or other OpenSearch classes.
* <p>
* Extensions initialize by passing an instance of themselves to the {@link #run(Extension)} method.
*/
public class ExtensionsRunner {
private static final Logger logger = LogManager.getLogger(ExtensionsRunner.class);
/**
* The key for the extension runner's node name in its settings.
*/
public static final String NODE_NAME_SETTING = "node.name";
// The extension being run
private final Extension extension;
// Set when initialization is complete
private boolean initialized = false;
private String uniqueId;
/**
* This field is initialized by a call from {@link ExtensionsInitRequestHandler}.
*/
public DiscoveryNode opensearchNode;
private DiscoveryExtensionNode extensionNode;
/**
* This field is initialized by a call from {@link ExtensionsInitRequestHandler}.
*/
private TransportService extensionTransportService = null;
// The routes and classes which handle the REST requests
private final ExtensionRestPathRegistry extensionRestPathRegistry = new ExtensionRestPathRegistry();
/**
* Custom namedXContent from the extension's getNamedXContent. This field is initialized in the constructor.
*/
private final List<NamedXContentRegistry.Entry> customNamedXContent;
/**
* Custom namedWriteable from the extension's getNamedWriteable. This field is initialized in the constructor.
*/
private final List<NamedWriteableRegistry.Entry> customNamedWriteables;
/**
* Custom settings from the extension's getSettings. This field is initialized in the constructor.
*/
private final List<Setting<?>> customSettings;
/**
* Environment settings from OpenSearch. This field is initialized by a call from
* {@link ExtensionsInitRequestHandler}.
*/
private Settings environmentSettings = Settings.EMPTY;
/**
* Node name, host, and port. This field is initialized by a call from {@link ExtensionsInitRequestHandler}.
*/
private final Settings settings;
/**
* A thread pool for the extension.
*/
private final ThreadPool threadPool;
/**
* A task manager for the extension
*/
private final TaskManager taskManager;
/**
* The Guice injector
*/
private final Injector injector;
private final SDKNamedXContentRegistry sdkNamedXContentRegistry;
private final SDKNamedWriteableRegistry sdkNamedWriteableRegistry;
private final SDKClient sdkClient;
private final SDKClusterService sdkClusterService;
private final SDKTransportService sdkTransportService;
private final SDKActionModule sdkActionModule;
private final ExtensionsInitRequestHandler extensionsInitRequestHandler = new ExtensionsInitRequestHandler(this);
private final ExtensionsIndicesModuleRequestHandler extensionsIndicesModuleRequestHandler = new ExtensionsIndicesModuleRequestHandler();
private final ExtensionsIndicesModuleNameRequestHandler extensionsIndicesModuleNameRequestHandler =
new ExtensionsIndicesModuleNameRequestHandler();
private final ExtensionsRestRequestHandler extensionsRestRequestHandler;
private final ExtensionActionRequestHandler extensionsActionRequestHandler;
private final AtomicReference<RunnableTaskExecutionListener> runnableTaskListener;
private final IndexNameExpressionResolver indexNameExpressionResolver;
/**
* Instantiates a new update settings request handler
*/
UpdateSettingsRequestHandler updateSettingsRequestHandler = new UpdateSettingsRequestHandler();
/**
* Instantiates a new Extensions Runner using the specified extension.
*
* @param extension The settings with which to start the runner.
* @throws IOException if the runner failed to read settings or API.
*/
protected ExtensionsRunner(Extension extension) throws IOException {
// Link these classes together
this.extension = extension;
extension.setExtensionsRunner(this);
// Initialize concrete classes needed by extensions
// These must have getters from this class to be accessible via createComponents
// If they require later initialization, create a concrete wrapper class and update the internals
ExtensionSettings extensionSettings = extension.getExtensionSettings();
this.settings = Settings.builder()
.put(NODE_NAME_SETTING, extensionSettings.getExtensionName())
.put(TransportSettings.BIND_HOST.getKey(), extensionSettings.getHostAddress())
.put(TransportSettings.PORT.getKey(), extensionSettings.getHostPort())
.build();
final List<ExecutorBuilder<?>> executorBuilders = extension.getExecutorBuilders(settings);
this.runnableTaskListener = new AtomicReference<>();
this.threadPool = new ThreadPool(settings, runnableTaskListener, executorBuilders.toArray(new ExecutorBuilder[0]));
this.indexNameExpressionResolver = new IndexNameExpressionResolver(this.threadPool.getThreadContext());
this.taskManager = new TaskManager(settings, threadPool, Collections.emptySet());
// save custom settings
this.customSettings = extension.getSettings();
// save custom namedXContent
this.customNamedXContent = extension.getNamedXContent();
// save custom namedWriteable
this.customNamedWriteables = extension.getNamedWriteables();
// initialize NamedXContent Registry.
this.sdkNamedXContentRegistry = new SDKNamedXContentRegistry(this);
// initialize RestRequest Handler. Must happen after instantiating SDKNamedXContentRegistry
this.extensionsRestRequestHandler = new ExtensionsRestRequestHandler(extensionRestPathRegistry, sdkNamedXContentRegistry);
// initialize NamedWriteable Registry. Must happen after getting extension namedWriteable
this.sdkNamedWriteableRegistry = new SDKNamedWriteableRegistry(this);
// initialize SDKClient. Must happen after getting extensionSettings
this.sdkClient = new SDKClient(extensionSettings);
// initialize SDKClusterService. Must happen after extension field assigned
this.sdkClusterService = new SDKClusterService(this);
// initialize SDKTransportService. Must happen after extension field assigned
this.sdkTransportService = new SDKTransportService();
// Create Guice modules for injection
List<com.google.inject.Module> modules = new ArrayList<>();
// Bind the concrete classes defined above, via getter
modules.add(b -> {
b.bind(ExtensionsRunner.class).toInstance(this);
b.bind(Extension.class).toInstance(extension);
b.bind(SDKNamedXContentRegistry.class).toInstance(getNamedXContentRegistry());
b.bind(ThreadPool.class).toInstance(getThreadPool());
b.bind(TaskManager.class).toInstance(getTaskManager());
b.bind(IndexNameExpressionResolver.class).toInstance(indexNameExpressionResolver);
b.bind(SDKClient.class).toInstance(getSdkClient());
b.bind(SDKClusterService.class).toInstance(getSdkClusterService());
b.bind(SDKTransportService.class).toInstance(getSdkTransportService());
});
// Bind the return values from create components
modules.add(this::injectComponents);
// Bind actions from getActions
this.sdkActionModule = new SDKActionModule(extension);
modules.add(this.sdkActionModule);
// Finally, perform the injection
this.injector = Guice.createInjector(modules);
// Perform other initialization. These should have access to injected classes.
// initialize SDKClient action map
initializeSdkClient();
extensionsActionRequestHandler = new ExtensionActionRequestHandler(getSdkClient());
if (extension instanceof ActionExtension) {
// store REST handlers in the registry
for (ExtensionRestHandler extensionRestHandler : ((ActionExtension) extension).getExtensionRestHandlers()) {
for (Route route : extensionRestHandler.routes()) {
extensionRestPathRegistry.registerHandler(route.getMethod(), route.getPath(), extensionRestHandler);
}
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private void injectComponents(Binder b) {
extension.createComponents(this).stream().forEach(p -> b.bind((Class) p.getClass()).toInstance(p));
}
@SuppressWarnings("rawtypes")
private void initializeSdkClient() {
sdkClient.initialize(this.injector.getInstance(new Key<Map<ActionType, TransportAction>>() {
}));
}
/**
* Gets the {@link Extension} this class is running.
* @return the extension.
*/
public Extension getExtension() {
return this.extension;
}
/**
* Marks the extension initialized.
*/
public void setInitialized() {
this.initialized = true;
logger.info("Extension initialization is complete!");
}
/**
* Reports if the extension has finished initializing.
*
* @return true if the extension has been initialized
*/
boolean isInitialized() {
return this.initialized;
}
/**
* Sets the TransportService. Called from {@link ExtensionsInitRequestHandler}.
*
* @param extensionTransportService assign value for extensionTransportService
*/
void setExtensionTransportService(TransportService extensionTransportService) {
this.extensionTransportService = extensionTransportService;
}
/**
* Sets the Environment Settings. Called from {@link ExtensionsInitRequestHandler}.
*
* @param settings assign value for environmentSettings
*/
public void setEnvironmentSettings(Settings settings) {
this.environmentSettings = settings;
}
/**
* Gets the Environment Settings. Only valid if {@link #isInitialized()} returns true.
*
* @return the environment settings if initialized, an empty settings object otherwise.
*/
public Settings getEnvironmentSettings() {
return this.environmentSettings;
}
/**
* Updates the NamedXContentRegistry. Called from {@link ExtensionsInitRequestHandler}.
*/
public void updateNamedXContentRegistry() {
this.sdkNamedXContentRegistry.updateNamedXContentRegistry(this);
}
/**
* Updates the NamedXContentRegistry. Called from {@link ExtensionsInitRequestHandler}.
*/
public void updateNamedWriteableRegistry() {
this.sdkNamedWriteableRegistry.updateNamedWriteableRegistry(this);
}
/**
* Gets the NamedXContentRegistry. Only valid if {@link #isInitialized()} returns true.
*
* @return the NamedXContentRegistry if initialized, an empty registry otherwise.
*/
public SDKNamedXContentRegistry getNamedXContentRegistry() {
return this.sdkNamedXContentRegistry;
}
/**
* Gets the SDKNamedWriteableRegistry. Only valid if {@link #isInitialized()} returns true.
*
* @return the NamedWriteableRegistry if initialized, an empty registry otherwise.
*/
public SDKNamedWriteableRegistry getNamedWriteableRegistry() {
return this.sdkNamedWriteableRegistry;
}
/**
* Sets the Unique ID, used in REST requests to uniquely identify this extension
*
* @param id assign value for id
*/
public void setUniqueId(String id) {
this.uniqueId = id;
}
public String getUniqueId() {
return uniqueId;
}
public void setOpensearchNode(DiscoveryNode opensearchNode) {
this.opensearchNode = opensearchNode;
}
public void setExtensionNode(DiscoveryExtensionNode extensionNode) {
this.extensionNode = extensionNode;
}
/**
* Returns the discovery extension node set during extension initialization
*
* @return the extensionNode
*/
public DiscoveryExtensionNode getExtensionNode() {
return this.extensionNode;
}
public DiscoveryNode getOpensearchNode() {
return this.opensearchNode;
}
public List<NamedXContentRegistry.Entry> getCustomNamedXContent() {
return this.customNamedXContent;
}
public List<NamedWriteableRegistry.Entry> getCustomNamedWriteables() {
return this.customNamedWriteables;
}
public IndexNameExpressionResolver getIndexNameExpressionResolver() {
return this.indexNameExpressionResolver;
}
/**
* Starts a TransportService.
*
* @param transportService The TransportService to start.
*/
public void startTransportService(TransportService transportService) {
// start transport service and accept incoming requests
transportService.start();
transportService.acceptIncomingRequests();
// Extension Request is the first request for the transport communication.
// This request will initialize the extension and will be a part of OpenSearch bootstrap
transportService.registerRequestHandler(
ExtensionsManager.REQUEST_EXTENSION_ACTION_NAME,
ThreadPool.Names.GENERIC,
false,
false,
InitializeExtensionRequest::new,
(request, channel, task) -> channel.sendResponse(extensionsInitRequestHandler.handleExtensionInitRequest(request))
);
transportService.registerRequestHandler(
ExtensionsManager.INDICES_EXTENSION_POINT_ACTION_NAME,
ThreadPool.Names.GENERIC,
false,
false,
IndicesModuleRequest::new,
((request, channel, task) -> channel.sendResponse(
extensionsIndicesModuleRequestHandler.handleIndicesModuleRequest(request, transportService)
))
);
transportService.registerRequestHandler(
ExtensionsManager.INDICES_EXTENSION_NAME_ACTION_NAME,
ThreadPool.Names.GENERIC,
false,
false,
IndicesModuleRequest::new,
((request, channel, task) -> channel.sendResponse(
extensionsIndicesModuleNameRequestHandler.handleIndicesModuleNameRequest(request)
))
);
transportService.registerRequestHandler(
ExtensionsManager.REQUEST_REST_EXECUTE_ON_EXTENSION_ACTION,
ThreadPool.Names.GENERIC,
false,
false,
ExtensionRestRequest::new,
((request, channel, task) -> channel.sendResponse(extensionsRestRequestHandler.handleRestExecuteOnExtensionRequest(request)))
);
transportService.registerRequestHandler(
ExtensionsManager.REQUEST_EXTENSION_UPDATE_SETTINGS,
ThreadPool.Names.GENERIC,
false,
false,
UpdateSettingsRequest::new,
((request, channel, task) -> channel.sendResponse(updateSettingsRequestHandler.handleUpdateSettingsRequest(request)))
);
// This handles a remote extension request from OpenSearch or a plugin, sending an ExtensionActionResponse
transportService.registerRequestHandler(
ExtensionsManager.REQUEST_EXTENSION_HANDLE_TRANSPORT_ACTION,
ThreadPool.Names.GENERIC,
false,
false,
ExtensionActionRequest::new,
((request, channel, task) -> channel.sendResponse(extensionsActionRequestHandler.handleExtensionActionRequest(request)))
);
// This handles a remote extension request from another extension, sending a RemoteExtensionActionResponse
transportService.registerRequestHandler(
ExtensionsManager.REQUEST_EXTENSION_HANDLE_REMOTE_TRANSPORT_ACTION,
ThreadPool.Names.GENERIC,
false,
false,
ExtensionActionRequest::new,
((request, channel, task) -> channel.sendResponse(extensionsActionRequestHandler.handleRemoteExtensionActionRequest(request)))
);
}
/**
* Returns a list of interfaces implemented by the corresponding {@link Extension}.
*
* @return A list of strings matching the interface name.
*/
public List<String> getExtensionImplementedInterfaces() {
Set<Class<?>> interfaceSet = new HashSet<>();
Class<?> extensionClass = getExtension().getClass();
do {
interfaceSet.addAll(Arrays.stream(extensionClass.getInterfaces()).collect(Collectors.toSet()));
extensionClass = extensionClass.getSuperclass();
} while (extensionClass != null);
// we are making an assumption here that all the other Interfaces will be in the same package ( or will be in subpackage ) in which
// Extension Interface belongs.
String extensionInterfacePackageName = Extension.class.getPackageName();
return interfaceSet.stream()
.filter(i -> i.getPackageName().startsWith(extensionInterfacePackageName))
.map(Class::getSimpleName)
.collect(Collectors.toList());
}
/**
* Requests that OpenSearch register the REST Actions for this extension.
*
* @param transportService The TransportService defining the connection to OpenSearch.
*/
public void sendRegisterRestActionsRequest(TransportService transportService) {
List<String> extensionRestPaths = extensionRestPathRegistry.getRegisteredPaths();
logger.info("Sending Register REST Actions request to OpenSearch for " + extensionRestPaths);
AcknowledgedResponseHandler registerActionsResponseHandler = new AcknowledgedResponseHandler();
try {
transportService.sendRequest(
opensearchNode,
ExtensionsManager.REQUEST_EXTENSION_REGISTER_REST_ACTIONS,
new RegisterRestActionsRequest(getUniqueId(), extensionRestPaths),
registerActionsResponseHandler
);
} catch (Exception e) {
logger.info("Failed to send Register REST Actions request to OpenSearch", e);
}
}
/**
* Requests that OpenSearch register the custom settings for this extension.
*
* @param transportService The TransportService defining the connection to OpenSearch.
*/
public void sendRegisterCustomSettingsRequest(TransportService transportService) {
logger.info("Sending Settings request to OpenSearch");
AcknowledgedResponseHandler registerCustomSettingsResponseHandler = new AcknowledgedResponseHandler();
try {
transportService.sendRequest(
opensearchNode,
ExtensionsManager.REQUEST_EXTENSION_REGISTER_CUSTOM_SETTINGS,
new RegisterCustomSettingsRequest(getUniqueId(), customSettings),
registerCustomSettingsResponseHandler
);
} catch (Exception e) {
logger.info("Failed to send Register Settings request to OpenSearch", e);
}
}
private void sendGenericRequestWithExceptionHandling(
TransportService transportService,
RequestType requestType,
String orchestratorNameString,
TransportResponseHandler<? extends TransportResponse> responseHandler
) {
logger.info("Sending " + requestType + " request to OpenSearch");
try {
transportService.sendRequest(opensearchNode, orchestratorNameString, new ExtensionRequest(requestType), responseHandler);
} catch (Exception e) {
logger.info("Failed to send " + requestType + " request to OpenSearch", e);
}
}
/**
* Requests the cluster state from OpenSearch. The result will be handled by a {@link ClusterStateResponseHandler}.
*
* @param transportService The TransportService defining the connection to OpenSearch.
* @return The cluster state of OpenSearch
*/
public ClusterState sendClusterStateRequest(TransportService transportService) {
logger.info("Sending Cluster State request to OpenSearch");
ClusterStateResponseHandler clusterStateResponseHandler = new ClusterStateResponseHandler();
try {
transportService.sendRequest(
opensearchNode,
ExtensionsManager.REQUEST_EXTENSION_CLUSTER_STATE,
new ExtensionRequest(ExtensionsManager.RequestType.REQUEST_EXTENSION_CLUSTER_STATE),
clusterStateResponseHandler
);
// Wait on cluster state response
clusterStateResponseHandler.awaitResponse();
} catch (TimeoutException e) {
logger.info("Failed to receive Cluster State response from OpenSearch", e);
} catch (Exception e) {
logger.info("Failed to send Cluster State request to OpenSearch", e);
}
// At this point, response handler has read in the cluster state
return clusterStateResponseHandler.getClusterState();
}
/**
* Request the Dependency Information from Opensearch. The result will be handled by a {@link ExtensionDependencyResponseHandler}.
*
* @param transportService The TransportService defining the connection to OpenSearch
* @return A List contains details of this extension's dependencies
*/
public List<DiscoveryExtensionNode> sendExtensionDependencyRequest(TransportService transportService) {
logger.info("Sending Extension Dependency Information request to Opensearch");
ExtensionDependencyResponseHandler extensionDependencyResponseHandler = new ExtensionDependencyResponseHandler();
try {
transportService.sendRequest(
opensearchNode,
ExtensionsManager.REQUEST_EXTENSION_DEPENDENCY_INFORMATION,
new ExtensionRequest(ExtensionsManager.RequestType.REQUEST_EXTENSION_DEPENDENCY_INFORMATION, uniqueId),
extensionDependencyResponseHandler
);
// Wait on Extension Dependency response
extensionDependencyResponseHandler.awaitResponse();
} catch (TimeoutException e) {
logger.info("Failed to receive Extension Dependency response from OpenSearch", e);
} catch (Exception e) {
logger.info("Failed to send Extension Dependency request to OpenSearch", e);
}
// At this point, response handler has read in the extension dependency
return extensionDependencyResponseHandler.getExtensionDependencies();
}
/**
* Requests the cluster settings from OpenSearch. The result will be handled by a {@link ClusterSettingsResponseHandler}.
*
* @param transportService The TransportService defining the connection to OpenSearch.
*/
public void sendClusterSettingsRequest(TransportService transportService) {
sendGenericRequestWithExceptionHandling(
transportService,
ExtensionsManager.RequestType.REQUEST_EXTENSION_CLUSTER_SETTINGS,
ExtensionsManager.REQUEST_EXTENSION_CLUSTER_SETTINGS,
new ClusterSettingsResponseHandler()
);
}
/**
* Requests the environment settings from OpenSearch. The result will be handled by a {@link EnvironmentSettingsResponseHandler}.
*
* @param transportService The TransportService defining the connection to OpenSearch.
* @return A Setting object from the OpenSearch Node environment
*/
public Settings sendEnvironmentSettingsRequest(TransportService transportService) {
logger.info("Sending Environment Settings request to OpenSearch");
EnvironmentSettingsResponseHandler environmentSettingsResponseHandler = new EnvironmentSettingsResponseHandler();
try {
transportService.sendRequest(
opensearchNode,
ExtensionsManager.REQUEST_EXTENSION_ENVIRONMENT_SETTINGS,
new ExtensionRequest(ExtensionsManager.RequestType.REQUEST_EXTENSION_ENVIRONMENT_SETTINGS),
environmentSettingsResponseHandler
);
// Wait on environment settings response
environmentSettingsResponseHandler.awaitResponse();
} catch (TimeoutException e) {
logger.info("Failed to receive Environment Settings response from OpenSearch", e);
} catch (Exception e) {
logger.info("Failed to send Environment Settings request to OpenSearch", e);
}
// At this point, response handler has read in the environment settings
return environmentSettingsResponseHandler.getEnvironmentSettings();
}
/**
* Registers settings and setting consumers with the {@link UpdateSettingsRequestHandler} and then sends a request to OpenSearch to register these Setting objects with a callback to this extension.
* The result will be handled by a {@link AcknowledgedResponseHandler}.
*
* @param transportService The TransportService defining the connection to OpenSearch.
* @param settingUpdateConsumers A map of setting objects and their corresponding consumers
*/
public void sendAddSettingsUpdateConsumerRequest(
TransportService transportService,
Map<Setting<?>, Consumer<?>> settingUpdateConsumers
) {
// Determine if there are setting update consumers to be registered
if (!settingUpdateConsumers.isEmpty()) {
// Register setting update consumers to UpdateSettingsRequestHandler
this.updateSettingsRequestHandler.registerSettingUpdateConsumer(settingUpdateConsumers);
// Extract registered settings from setting update consumer map
List<Setting<?>> componentSettings = new ArrayList<>(settingUpdateConsumers.size());
componentSettings.addAll(settingUpdateConsumers.keySet());
logger.info(
"Sending Add Settings Update Consumer request to OpenSearch for {}",
componentSettings.stream().map(Setting::getKey).toArray()
);
AcknowledgedResponseHandler acknowledgedResponseHandler = new AcknowledgedResponseHandler();
transportService.sendRequest(
opensearchNode,
ExtensionsManager.REQUEST_EXTENSION_ADD_SETTINGS_UPDATE_CONSUMER,
new AddSettingsUpdateConsumerRequest(this.extensionNode, componentSettings),
acknowledgedResponseHandler
);
}
}
public Settings getSettings() {
return settings;
}
public ThreadPool getThreadPool() {
return threadPool;
}
public TaskManager getTaskManager() {
return taskManager;
}
public SDKClient getSdkClient() {
return sdkClient;
}
public SDKClusterService getSdkClusterService() {
return sdkClusterService;
}
/**
* Updates the SDKClusterService. Called from {@link ExtensionsInitRequestHandler}.
*/
public void updateSdkClusterService() {
this.sdkClusterService.updateSdkClusterSettings();
}
public SDKActionModule getSdkActionModule() {
return sdkActionModule;
}
public TransportService getExtensionTransportService() {
return extensionTransportService;
}
public SDKTransportService getSdkTransportService() {
return sdkTransportService;
}
/**
* Starts an ActionListener.
*
* @param timeout The timeout for the listener in milliseconds. A timeout of 0 means no timeout.
*/
public void startActionListener(int timeout) {
final ActionListener actionListener = new ActionListener();
actionListener.runActionListener(true, timeout);
}
/**
* Runs the specified extension.
*
* @param extension The extension to run.
* @throws IOException on failure to bind ports.
*/
public static void run(Extension extension) throws IOException {
logger.info("Starting extension " + extension.getExtensionSettings().getExtensionName());
ExtensionsRunner runner = new ExtensionsRunner(extension);
// initialize the transport service
NettyTransport nettyTransport = new NettyTransport(runner);
runner.extensionTransportService = nettyTransport.initializeExtensionTransportService(runner.getSettings(), runner.getThreadPool());
// TODO: merge above line with below line when refactoring out extensionTransportService
runner.getSdkTransportService().setTransportService(runner.extensionTransportService);
runner.startActionListener(0);
}
}