-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: automatic credential refresher opensearch sink (#4283)
ENH: automatic credential refresher opensearch sink Signed-off-by: George Chen <qchea@amazon.com>
- Loading branch information
1 parent
6765098
commit 5a7dbda
Showing
11 changed files
with
310 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...in/java/org/opensearch/dataprepper/plugins/sink/opensearch/OpenSearchClientRefresher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.opensearch.dataprepper.plugins.sink.opensearch; | ||
|
||
import io.micrometer.core.instrument.Counter; | ||
import org.opensearch.client.opensearch.OpenSearchClient; | ||
import org.opensearch.dataprepper.metrics.PluginMetrics; | ||
import org.opensearch.dataprepper.model.configuration.PluginSetting; | ||
import org.opensearch.dataprepper.model.plugin.PluginComponentRefresher; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.locks.ReadWriteLock; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import java.util.function.Function; | ||
|
||
public class OpenSearchClientRefresher implements PluginComponentRefresher<OpenSearchClient, PluginSetting> { | ||
static final String CREDENTIALS_CHANGED = "credentialsChanged"; | ||
static final String CLIENT_REFRESH_ERRORS = "clientRefreshErrors"; | ||
private static final Logger LOG = LoggerFactory.getLogger(OpenSearchClientRefresher.class); | ||
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); | ||
private final Function<ConnectionConfiguration, OpenSearchClient> clientFunction; | ||
private OpenSearchClient currentClient; | ||
private ConnectionConfiguration currentConfig; | ||
|
||
private final Counter credentialsChangeCounter; | ||
private final Counter clientRefreshErrorsCounter; | ||
|
||
public OpenSearchClientRefresher(final PluginMetrics pluginMetrics, | ||
final ConnectionConfiguration connectionConfiguration, | ||
final Function<ConnectionConfiguration, OpenSearchClient> clientFunction) { | ||
this.clientFunction = clientFunction; | ||
this.currentConfig = connectionConfiguration; | ||
this.currentClient = clientFunction.apply(connectionConfiguration); | ||
credentialsChangeCounter = pluginMetrics.counter(CREDENTIALS_CHANGED); | ||
clientRefreshErrorsCounter = pluginMetrics.counter(CLIENT_REFRESH_ERRORS); | ||
} | ||
|
||
@Override | ||
public Class<OpenSearchClient> getComponentClass() { | ||
return OpenSearchClient.class; | ||
} | ||
|
||
@Override | ||
public OpenSearchClient get() { | ||
readWriteLock.readLock().lock(); | ||
try { | ||
return currentClient; | ||
} finally { | ||
readWriteLock.readLock().unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public void update(PluginSetting pluginSetting) { | ||
final ConnectionConfiguration newConfig = ConnectionConfiguration.readConnectionConfiguration(pluginSetting); | ||
if (basicAuthChanged(newConfig)) { | ||
credentialsChangeCounter.increment(); | ||
readWriteLock.writeLock().lock(); | ||
try { | ||
currentClient = clientFunction.apply(newConfig); | ||
currentConfig = newConfig; | ||
} catch (Exception e) { | ||
clientRefreshErrorsCounter.increment(); | ||
LOG.error("Refreshing {} failed.", getComponentClass(), e); | ||
} finally { | ||
readWriteLock.writeLock().unlock(); | ||
} | ||
} | ||
} | ||
|
||
private boolean basicAuthChanged(final ConnectionConfiguration newConfig) { | ||
return !Objects.equals(currentConfig.getUsername(), newConfig.getUsername()) || | ||
!Objects.equals(currentConfig.getPassword(), newConfig.getPassword()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.