public class LicenseManager extends Object
This manager is supplied configurations for an admin client to create the topic if necessary,
a consumer to read the contents of that topic, and a producer to store the updated license
information, if such is supplied when
registering and validating
licenses.
Additional admin client configurations can be provided for Kafka clusters
that the license manager should consider when validating a license.
The next step is to read the license information from the component's configuration, or use a
blank string if the user did not provide a license, and call
registerOrValidateLicense(String)
to register the new license or, if it is
blank, attempt to read a previously-stored license.
This method will thrown an InvalidLicenseException
if the license is not valid or has
expired. Otherwise, a valid license exists and is returned for use by the application.
Note that the registerOrValidateLicense(String)
logs information about the license,
either at WARN level if the license expires within 10 days, or at INFO level otherwise. The
message includes the type of license, the number of days until the license expires, and the
expiration date.
Finally, when the application stops, call stop()
so the LicenseManager can release
any resources it might have created.
Here is an example:
// Read the license string from the application or component's configuration String licenseStr = ... // Start the license manager String topicName = ... Map<String, Object> producerConfig = ... Map<String, Object> consumerConfig = ... Map<String, Object> adminConfig = ... LicenseManager licenseMgr = new LicenseManager( producerConfig, consumerConfig, adminConfig); License license = licenseMgr.registerOrValidateLicense(licenseStr); // At some point stop the license manager licenseMgr.stop();
Modifier and Type | Class and Description |
---|---|
protected static class |
LicenseManager.BasicClusterClient |
protected static interface |
LicenseManager.ClusterClient |
protected static class |
LicenseManager.LicenseChangedEvent |
Modifier and Type | Field and Description |
---|---|
protected static long |
WARN_THRESHOLD_DAYS |
Modifier | Constructor and Description |
---|---|
protected |
LicenseManager(LicenseManager.ClusterClient primaryClusterClient,
LicenseStore licenseStore,
org.apache.kafka.common.utils.Time time) |
protected |
LicenseManager(LicenseManager.ClusterClient primaryClusterClient,
LicenseStore licenseStore,
org.apache.kafka.common.utils.Time time,
String configuredLicenseStr,
boolean startStore,
boolean allowInvalidLicenseToRun) |
|
LicenseManager(Map<String,Object> topicConfig,
LicenseStore licenseStore,
String configuredLicense,
boolean startStore) |
|
LicenseManager(Map<String,Object> topicConfig,
LicenseStore licenseStore,
String configuredLicense,
boolean startStore,
boolean allowInvalidLicenseToRun) |
|
LicenseManager(String topic,
Map<String,Object> producerConfig,
Map<String,Object> consumerConfig,
Map<String,Object> topicConfig)
Create a new license manager that uses the specified topic for licenses, configurations for an
admin client, producer, and consumer to create, write to, and read from that topic in a
primary Kafka cluster.
|
|
LicenseManager(String topic,
Map<String,Object> producerConfig,
Map<String,Object> consumerConfig,
Map<String,Object> topicConfig,
Duration topicCreateTimeout,
Duration retryBackoffMinMs,
Duration retryBackoffMaxMs) |
Modifier and Type | Method and Description |
---|---|
void |
addCluster(String key,
LicenseManager.ClusterClient client)
Add another Kafka cluster to be considered when validating a license.
|
void |
addCluster(String key,
Map<String,Object> adminConfig)
Add another Kafka cluster to be considered when validating a license.
|
boolean |
addListener(Consumer<LicenseChanged> listener)
Add a listener for subsequent license changes.
|
License |
configuredLicense() |
protected void |
doStart() |
protected void |
doStop() |
protected License |
generateLicense(PublicKey publicKey,
long now) |
protected boolean |
hasOnlySingleNodeClusters() |
static PublicKey |
loadPublicKey() |
protected void |
notifyLicense(License newLicense,
License oldLicense,
String reason) |
protected License |
readLicense(PublicKey publicKey,
String licenseStr,
boolean stored) |
License |
registerOrValidateLicense(String license)
Validate the supplied license, or if blank register a new trial license.
|
boolean |
removeCluster(String key)
Remove the Kafka cluster client with the specific name from being considered when validating a
license.
|
boolean |
removeListener(Consumer<LicenseChanged> listener)
Remove a previously registered license change listener.
|
void |
start()
Begin running this license manager's check on a daily schedule, or more frequently if the
license is expired or has less than 1 day remaining.
|
void |
start(long initialDelay,
long period,
TimeUnit timeUnit)
Begin running this license manager's check with a configurable initial delay and period.
|
void |
stop()
Stop this license manager, close any resources, and remove any scheduled license checks.
|
long |
timeUntilLicenseExpirationMs() |
void |
timeUntilLicenseExpirationMs(long timeMs) |
protected static final long WARN_THRESHOLD_DAYS
public LicenseManager(String topic, Map<String,Object> producerConfig, Map<String,Object> consumerConfig, Map<String,Object> topicConfig)
topic
- the name of the topic where licenses are persisted; may not be nullproducerConfig
- the configuration for the producer used to write to the license topic;
may not be nullconsumerConfig
- the configuration for the consumer used to read from the license topic;
may not be nulltopicConfig
- the configuration for the admin client to create the license topic if
required; may not be nullpublic LicenseManager(String topic, Map<String,Object> producerConfig, Map<String,Object> consumerConfig, Map<String,Object> topicConfig, Duration topicCreateTimeout, Duration retryBackoffMinMs, Duration retryBackoffMaxMs)
public LicenseManager(Map<String,Object> topicConfig, LicenseStore licenseStore, String configuredLicense, boolean startStore)
public LicenseManager(Map<String,Object> topicConfig, LicenseStore licenseStore, String configuredLicense, boolean startStore, boolean allowInvalidLicenseToRun)
protected LicenseManager(LicenseManager.ClusterClient primaryClusterClient, LicenseStore licenseStore, org.apache.kafka.common.utils.Time time)
protected LicenseManager(LicenseManager.ClusterClient primaryClusterClient, LicenseStore licenseStore, org.apache.kafka.common.utils.Time time, String configuredLicenseStr, boolean startStore, boolean allowInvalidLicenseToRun)
public void addCluster(String key, Map<String,Object> adminConfig)
AdminClientConfig
for the cluster, and the principal requires
privilege to read the cluster metadata.
This method is safe to be called whether or not the license manager is
running
.
adminConfig
- the admin client configurations for another Kafka clusters that should be
considered during license validation; may be null or emptypublic void addCluster(String key, LicenseManager.ClusterClient client)
LicenseManager.ClusterClient
function, allowing callers to fully control
how the license manager determines the size of a cluster.
This method is safe to be called whether or not the license manager is
running
.
key
- the unique key for the cluster client; may not be nullclient
- the cluster client function that should be used to determine the size of a
cluster during license validation; may be null or emptypublic boolean removeCluster(String key)
This method is safe to be called whether or not the license manager is
running
.
public boolean addListener(Consumer<LicenseChanged> listener)
listener
- the function that is to be called when license changes are detectedremoveListener(Consumer)
public boolean removeListener(Consumer<LicenseChanged> listener)
listener
- the function that is no longer to be called when license changes are detectedregistered
addListener(Consumer)
public void start()
This is useful for automatically detecting changes in the license and notifying the
listeners
.
addListener(Consumer)
,
removeListener(Consumer)
public void start(long initialDelay, long period, TimeUnit timeUnit)
initialDelay
- the initial delay period for the first checkperiod
- the frequency of the following checkstimeUnit
- the timeunit used for the initialDelay and the periodpublic void stop()
started
.start()
protected void doStart()
protected void doStop()
public License configuredLicense()
public License registerOrValidateLicense(String license) throws InvalidLicenseException
start()
.license
- the new license string to be persisted in the topic; may be empty if there is
no updated licenseExpiredLicenseException
- if the supplied or stored license has expiredInvalidLicenseException
- if the supplied or stored license is not validprotected License readLicense(PublicKey publicKey, String licenseStr, boolean stored) throws InvalidLicenseException
InvalidLicenseException
protected License generateLicense(PublicKey publicKey, long now) throws InvalidLicenseException
InvalidLicenseException
protected boolean hasOnlySingleNodeClusters()
public static PublicKey loadPublicKey()
public void timeUntilLicenseExpirationMs(long timeMs)
public long timeUntilLicenseExpirationMs()