@@ -65,12 +65,14 @@ public class OpenSearchSink extends AbstractSink<Record<Event>> {
6565 private final IndexManagerFactory indexManagerFactory ;
6666 private final IndexType indexType ;
6767 private final PluginConfigObservable pluginConfigObservable ;
68- private final Ingester ingester ;
69-
70- private final RestHighLevelClient restHighLevelClient ;
71- private final OpenSearchClient openSearchClient ;
72- private final OpenSearchClientRefresher openSearchClientRefresher ;
73- private final ConnectionConfiguration connectionConfiguration ;
68+ private final ExpressionEvaluator expressionEvaluator ;
69+ private final SinkContext sinkContext ;
70+ private final String pipeline ;
71+ private Ingester ingester ;
72+
73+ private RestHighLevelClient restHighLevelClient ;
74+ private OpenSearchClient openSearchClient ;
75+ private OpenSearchClientRefresher openSearchClientRefresher ;
7476 private IndexManager indexManager ;
7577 private volatile boolean initialized ;
7678
@@ -85,40 +87,16 @@ public OpenSearchSink(final PluginSetting pluginSetting,
8587 super (pluginSetting , Integer .MAX_VALUE , INITIALIZE_RETRY_WAIT_TIME_MS );
8688 this .awsCredentialsSupplier = awsCredentialsSupplier ;
8789 this .pluginConfigObservable = pluginConfigObservable ;
90+ this .expressionEvaluator = expressionEvaluator ;
8891
89- final SinkContext resolvedSinkContext = sinkContext != null ? sinkContext :
92+ this . sinkContext = sinkContext != null ? sinkContext :
9093 new SinkContext (null , Collections .emptyList (), Collections .emptyList (), Collections .emptyList ());
9194
9295 this .openSearchSinkConfig = OpenSearchSinkConfiguration .readOSConfig (openSearchSinkConfiguration , expressionEvaluator );
9396 this .indexType = openSearchSinkConfig .getIndexConfiguration ().getIndexType ();
9497 this .indexManagerFactory = new IndexManagerFactory (new ClusterSettingsParser ());
98+ this .pipeline = pipelineDescription .getPipelineName ();
9599 this .initialized = false ;
96-
97- connectionConfiguration = openSearchSinkConfig .getConnectionConfiguration ();
98- restHighLevelClient = connectionConfiguration .createClient (awsCredentialsSupplier );
99- openSearchClient = connectionConfiguration .createOpenSearchClient (restHighLevelClient , awsCredentialsSupplier );
100- final Function <ConnectionConfiguration , OpenSearchClient > clientFunction =
101- (connConfig ) -> {
102- final RestHighLevelClient client = connConfig .createClient (awsCredentialsSupplier );
103- return connConfig .createOpenSearchClient (client , awsCredentialsSupplier ).withTransportOptions (
104- TransportOptions .builder ()
105- .setParameter ("filter_path" , "errors,took,items.*.error,items.*.status,items.*._index,items.*._id" )
106- .build ());
107- };
108- openSearchClientRefresher = new OpenSearchClientRefresher (
109- pluginMetrics , connectionConfiguration , clientFunction );
110-
111- final String pipeline = pipelineDescription .getPipelineName ();
112- final EventActionResolver eventActionResolver = new EventActionResolver (
113- openSearchSinkConfig .getIndexConfiguration ().getAction (),
114- openSearchSinkConfig .getIndexConfiguration ().getActions (),
115- expressionEvaluator );
116-
117- this .ingester = new BulkIngester (openSearchSinkConfig , expressionEvaluator , resolvedSinkContext ,
118- pluginMetrics , pipeline , eventActionResolver ,
119- openSearchClient , () -> openSearchClientRefresher .get (),
120- this ::getIndexManager , this ::getFailurePipeline ,
121- new CustomDocumentBuilderFactory ().create (this .indexType ));
122100 }
123101
124102 @ Override
@@ -145,6 +123,20 @@ public void doInitialize() {
145123 private void doInitializeInternal () throws IOException {
146124 LOG .info ("Initializing OpenSearch sink" );
147125
126+ final ConnectionConfiguration connectionConfiguration = openSearchSinkConfig .getConnectionConfiguration ();
127+ restHighLevelClient = connectionConfiguration .createClient (awsCredentialsSupplier );
128+ openSearchClient = connectionConfiguration .createOpenSearchClient (restHighLevelClient , awsCredentialsSupplier );
129+ final Function <ConnectionConfiguration , OpenSearchClient > clientFunction =
130+ (connConfig ) -> {
131+ final RestHighLevelClient client = connConfig .createClient (awsCredentialsSupplier );
132+ return connConfig .createOpenSearchClient (client , awsCredentialsSupplier ).withTransportOptions (
133+ TransportOptions .builder ()
134+ .setParameter ("filter_path" , "errors,took,items.*.error,items.*.status,items.*._index,items.*._id" )
135+ .build ());
136+ };
137+ openSearchClientRefresher = new OpenSearchClientRefresher (
138+ pluginMetrics , connectionConfiguration , clientFunction );
139+
148140 pluginConfigObservable .addPluginConfigObserver (
149141 newOpenSearchSinkConfig -> openSearchClientRefresher .update ((OpenSearchSinkConfig ) newOpenSearchSinkConfig ));
150142
@@ -167,6 +159,17 @@ private void doInitializeInternal() throws IOException {
167159
168160 indexManager .setupIndex ();
169161
162+ final EventActionResolver eventActionResolver = new EventActionResolver (
163+ openSearchSinkConfig .getIndexConfiguration ().getAction (),
164+ openSearchSinkConfig .getIndexConfiguration ().getActions (),
165+ expressionEvaluator );
166+
167+ ingester = new BulkIngester (openSearchSinkConfig , expressionEvaluator , sinkContext ,
168+ pluginMetrics , pipeline , eventActionResolver ,
169+ openSearchClient , () -> openSearchClientRefresher .get (),
170+ this ::getIndexManager , this ::getFailurePipeline ,
171+ new CustomDocumentBuilderFactory ().create (this .indexType ));
172+
170173 ingester .initialize ();
171174
172175 this .initialized = true ;
@@ -190,7 +193,9 @@ public void doOutput(final Collection<Record<Event>> records) {
190193 @ Override
191194 public void shutdown () {
192195 super .shutdown ();
193- ingester .shutdown ();
196+ if (ingester != null ) {
197+ ingester .shutdown ();
198+ }
194199 if (restHighLevelClient != null ) {
195200 try {
196201 restHighLevelClient .close ();
0 commit comments