-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add license feature usage api #59342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
811286d
Add license feature usage api
rjernst 9c389b1
checkstyle
rjernst bc14423
fix compile
rjernst 2f36787
fix local state insanity
rjernst d54ff1a
testing conventions...
rjernst 057b83b
unit tests
rjernst 5bcbfb2
iter
rjernst c23c2e7
tests iter
rjernst e2c04bf
Merge branch 'master' into refactor_license13
rjernst 70042a7
another test
rjernst d95a4ea
fix a bunch of missed checkFeature -> isAllowed for telmetry
rjernst 228d882
address more feedback
rjernst fb6998d
Merge branch 'master' into refactor_license13
rjernst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
libs/core/src/main/java/org/elasticsearch/common/MemoizedSupplier.java
This file contains hidden or 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,40 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.common; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| public class MemoizedSupplier<T> implements Supplier<T> { | ||
| private Supplier<T> supplier; | ||
| private T value; | ||
|
|
||
| public MemoizedSupplier(Supplier<T> supplier) { | ||
| this.supplier = supplier; | ||
| } | ||
|
|
||
| @Override | ||
| public T get() { | ||
| if (supplier != null) { | ||
| value = supplier.get(); | ||
| supplier = null; | ||
| } | ||
| return value; | ||
| } | ||
| } |
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetFeatureUsageRequest.java
This file contains hidden or 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,27 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.license; | ||
|
|
||
| import org.elasticsearch.action.ActionRequest; | ||
| import org.elasticsearch.action.ActionRequestValidationException; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class GetFeatureUsageRequest extends ActionRequest { | ||
|
|
||
| public GetFeatureUsageRequest() {} | ||
|
|
||
| public GetFeatureUsageRequest(StreamInput in) throws IOException { | ||
| super(in); | ||
| } | ||
|
|
||
| @Override | ||
| public ActionRequestValidationException validate() { | ||
| return null; | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetFeatureUsageResponse.java
This file contains hidden or 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,84 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.license; | ||
|
|
||
| import org.elasticsearch.action.ActionResponse; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.io.stream.Writeable; | ||
| import org.elasticsearch.common.xcontent.ToXContentObject; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.Instant; | ||
| import java.time.ZoneOffset; | ||
| import java.time.ZonedDateTime; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class GetFeatureUsageResponse extends ActionResponse implements ToXContentObject { | ||
|
|
||
| public static class FeatureUsageInfo implements Writeable { | ||
| public final String name; | ||
| public final ZonedDateTime lastUsedTime; | ||
| public final String licenseLevel; | ||
|
|
||
| public FeatureUsageInfo(String name, ZonedDateTime lastUsedTime, String licenseLevel) { | ||
| this.name = name; | ||
| this.lastUsedTime = lastUsedTime; | ||
| this.licenseLevel = licenseLevel; | ||
| } | ||
|
|
||
| public FeatureUsageInfo(StreamInput in) throws IOException { | ||
| this.name = in.readString(); | ||
| this.lastUsedTime = ZonedDateTime.ofInstant(Instant.ofEpochSecond(in.readLong()), ZoneOffset.UTC); | ||
| this.licenseLevel = in.readString(); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| out.writeString(name); | ||
| out.writeLong(lastUsedTime.toEpochSecond()); | ||
| out.writeString(licenseLevel); | ||
| } | ||
| } | ||
|
|
||
| private List<FeatureUsageInfo> features; | ||
|
|
||
| public GetFeatureUsageResponse(List<FeatureUsageInfo> features) { | ||
| this.features = Collections.unmodifiableList(features); | ||
| } | ||
|
|
||
| public GetFeatureUsageResponse(StreamInput in) throws IOException { | ||
| this.features = in.readList(FeatureUsageInfo::new); | ||
| } | ||
|
|
||
| public List<FeatureUsageInfo> getFeatures() { | ||
| return features; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| out.writeList(features); | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
| builder.startObject(); | ||
| builder.startArray("features"); | ||
| for (FeatureUsageInfo feature : features) { | ||
| builder.startObject(); | ||
| builder.field("name", feature.name); | ||
| builder.field("last_used", feature.lastUsedTime.toString()); | ||
| builder.field("license_level", feature.licenseLevel); | ||
| builder.endObject(); | ||
| } | ||
| builder.endArray(); | ||
| builder.endObject(); | ||
| return builder; | ||
| } | ||
| } |
This file contains hidden or 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
36 changes: 36 additions & 0 deletions
36
x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetFeatureUsageAction.java
This file contains hidden or 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,36 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.license; | ||
|
|
||
| import org.elasticsearch.client.node.NodeClient; | ||
| import org.elasticsearch.rest.BaseRestHandler; | ||
| import org.elasticsearch.rest.RestRequest; | ||
| import org.elasticsearch.rest.action.RestToXContentListener; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| import static org.elasticsearch.rest.RestRequest.Method.GET; | ||
|
|
||
| public class RestGetFeatureUsageAction extends BaseRestHandler { | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "get_feature_usage"; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Route> routes() { | ||
| return List.of(new Route(GET, "/_license/feature_usage")); | ||
| } | ||
|
|
||
| @Override | ||
| protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
| return channel -> client.execute(TransportGetFeatureUsageAction.TYPE, new GetFeatureUsageRequest(), | ||
| new RestToXContentListener<>(channel)); | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
...k/plugin/core/src/main/java/org/elasticsearch/license/TransportGetFeatureUsageAction.java
This file contains hidden or 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,53 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.license; | ||
|
|
||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.ActionType; | ||
| import org.elasticsearch.action.support.ActionFilters; | ||
| import org.elasticsearch.action.support.HandledTransportAction; | ||
| import org.elasticsearch.common.inject.Inject; | ||
| import org.elasticsearch.tasks.Task; | ||
| import org.elasticsearch.transport.TransportService; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.ZoneOffset; | ||
| import java.time.ZonedDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
|
|
||
| public class TransportGetFeatureUsageAction extends HandledTransportAction<GetFeatureUsageRequest, GetFeatureUsageResponse> { | ||
|
|
||
| public static final ActionType<GetFeatureUsageResponse> TYPE = | ||
| new ActionType<>("cluster:admin/xpack/license/feature_usage", GetFeatureUsageResponse::new); | ||
|
|
||
| private final XPackLicenseState licenseState; | ||
|
|
||
| @Inject | ||
| public TransportGetFeatureUsageAction(TransportService transportService, ActionFilters actionFilters, | ||
| XPackLicenseState licenseState) { | ||
| super(TYPE.name(), transportService, actionFilters, GetFeatureUsageRequest::new); | ||
| this.licenseState = licenseState; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| protected void doExecute(Task task, GetFeatureUsageRequest request, ActionListener<GetFeatureUsageResponse> listener) { | ||
| Map<XPackLicenseState.Feature, Long> featureUsage = licenseState.getLastUsed(); | ||
| List<GetFeatureUsageResponse.FeatureUsageInfo> usageInfos = new ArrayList<>(); | ||
| for (var entry : featureUsage.entrySet()) { | ||
| XPackLicenseState.Feature feature = entry.getKey(); | ||
| String name = feature.name().toLowerCase(Locale.ROOT); | ||
| ZonedDateTime lastUsedTime = Instant.ofEpochMilli(entry.getValue()).atZone(ZoneOffset.UTC); | ||
| String licenseLevel = feature.minimumOperationMode.name().toLowerCase(Locale.ROOT); | ||
| usageInfos.add(new GetFeatureUsageResponse.FeatureUsageInfo(name, lastUsedTime, licenseLevel)); | ||
| } | ||
| listener.onResponse(new GetFeatureUsageResponse(usageInfos)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, the feature usage is reported for the local node only? So in cloud with coordinating node setup, how can user get the feature usages for nodes other than the coordinating ones?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cloud will need to hit every node in the cluster, not just coordinating nodes. AFAIU they can do this (this would be on their backend infrastructure side, and so would not be going through eg load balancers). It would be similar to eg health checks that hit each node.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok so this API is more geared towards Cloud internal management instead of end-users. Then it is fine.