-
Notifications
You must be signed in to change notification settings - Fork 308
restapi: Optimization of the GET /vms?detail=main REST API request #1026
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
Open
sermakov-orion
wants to merge
1
commit into
oVirt:master
Choose a base branch
from
sermakov-orion:vms_rest_api_optimization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
71 changes: 71 additions & 0 deletions
71
...es/bll/src/main/java/org/ovirt/engine/core/bll/GetVdsCertificateSubjectsByVmIdsQuery.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,71 @@ | ||
| package org.ovirt.engine.core.bll; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| import org.ovirt.engine.core.bll.context.EngineContext; | ||
| import org.ovirt.engine.core.common.businessentities.VdsStatic; | ||
| import org.ovirt.engine.core.common.businessentities.VmDynamic; | ||
| import org.ovirt.engine.core.common.queries.IdsQueryParameters; | ||
| import org.ovirt.engine.core.common.queries.QueryReturnValue; | ||
| import org.ovirt.engine.core.compat.Guid; | ||
| import org.ovirt.engine.core.dao.VdsStaticDao; | ||
| import org.ovirt.engine.core.dao.VmDynamicDao; | ||
| import org.ovirt.engine.core.utils.CertificateSubjectHelper; | ||
|
|
||
|
|
||
| public class GetVdsCertificateSubjectsByVmIdsQuery<P extends IdsQueryParameters> extends QueriesCommandBase<P> { | ||
| @Inject | ||
| private VmDynamicDao vmDynamicDao; | ||
|
|
||
| @Inject | ||
| private VdsStaticDao vdsStaticDao; | ||
|
|
||
| public GetVdsCertificateSubjectsByVmIdsQuery(P parameters, EngineContext engineContext) { | ||
| super(parameters, engineContext); | ||
| } | ||
|
|
||
| @Override | ||
| protected void executeQueryCommand() { | ||
| // Initially we set the command as failed: | ||
| QueryReturnValue queryReturnValue = getQueryReturnValue(); | ||
| queryReturnValue.setSucceeded(false); | ||
|
|
||
| // Check if the virtual machines are running on hosts, and if so then retrieve the hosts and copy the subject | ||
| // of the certificate to the value returned by the query: | ||
| List<VmDynamic> vms = vmDynamicDao.getByIds(getParameters().getIds()); | ||
| List<Guid> vdsIds = vms.stream() | ||
| .map(VmDynamic::getRunOnVds) | ||
| .filter(Objects::nonNull) | ||
| .distinct() | ||
| .collect(Collectors.toList()); | ||
| if (!vdsIds.isEmpty()) { | ||
| List<VdsStatic> vdss = vdsStaticDao.getByIds(vdsIds); | ||
| // Collect certificate subjects for all hosts running the VMs | ||
| Map<Guid, String> certificateSubjects = vdss.stream() | ||
| .collect(Collectors.toMap( | ||
| VdsStatic::getId, | ||
| vds -> CertificateSubjectHelper.getCertificateSubject(vds.getHostName()))); | ||
| // Populate the certificate subjects for corresponding VMs | ||
| Map<Guid, String> certificateForVms = new HashMap<>(); | ||
| for (VmDynamic vm : vms) { | ||
| Guid vdsId = vm.getRunOnVds(); | ||
| if (vdsId != null) { | ||
| String subject = certificateSubjects.get(vdsId); | ||
| if (subject != null) { | ||
| certificateForVms.put(vm.getId(), subject); | ||
| } | ||
| } | ||
| } | ||
| if (!certificateForVms.isEmpty()) { | ||
| queryReturnValue.setSucceeded(true); | ||
| queryReturnValue.setReturnValue(certificateForVms); | ||
| } | ||
| } | ||
| } | ||
| } |
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
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
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
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
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
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
93 changes: 93 additions & 0 deletions
93
...ules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/util/DisplayHelperTest.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,93 @@ | ||
| package org.ovirt.engine.api.restapi.util; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.junit.jupiter.MockitoSettings; | ||
| import org.mockito.quality.Strictness; | ||
| import org.ovirt.engine.api.model.Certificate; | ||
| import org.ovirt.engine.api.model.Vm; | ||
| import org.ovirt.engine.api.restapi.resource.BackendResource; | ||
| import org.ovirt.engine.core.common.config.ConfigValues; | ||
| import org.ovirt.engine.core.common.queries.QueryReturnValue; | ||
| import org.ovirt.engine.core.common.queries.QueryType; | ||
| import org.ovirt.engine.core.compat.Guid; | ||
| import org.ovirt.engine.core.utils.MockConfigDescriptor; | ||
| import org.ovirt.engine.core.utils.MockConfigExtension; | ||
|
|
||
| @MockitoSettings(strictness = Strictness.LENIENT) | ||
| @ExtendWith(MockConfigExtension.class) | ||
| public class DisplayHelperTest { | ||
| private static final String CERTIFICATE = "O=Redhat,CN=X.Y.Z.Q"; | ||
| private static final String CA_CERT = "dummy-cert"; | ||
| private static final String ORG = "ORG"; | ||
| private static final List<Guid> GUIDS = Arrays.asList( | ||
| new Guid("11111111-1111-1111-1111-111111111111"), | ||
| new Guid("22222222-2222-2222-2222-222222222222")); | ||
|
|
||
| public static Stream<MockConfigDescriptor<?>> mockConfiguration() { | ||
| return Stream.of(MockConfigDescriptor.of(ConfigValues.OrganizationName, ORG)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAddDisplayCertificate() { | ||
| Vm vm = new Vm(); | ||
| Certificate certificate = new Certificate(); | ||
| DisplayHelper.addDisplayCertificate(vm, certificate); | ||
|
|
||
| assertSame(certificate, vm.getDisplay().getCertificate()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetDisplayCertificatesForMultipleEntitiesNoResult() { | ||
| BackendResource res = mock(BackendResource.class); | ||
| QueryReturnValue result = new QueryReturnValue(); | ||
| result.setSucceeded(false); | ||
| when(res.runQuery(eq(QueryType.GetVdsCertificateSubjectsByVmIds), any())).thenReturn(result); | ||
|
|
||
| Map<Guid, Certificate> certificates = DisplayHelper.getDisplayCertificatesForMultipleEntities(res, GUIDS); | ||
| assertTrue(certificates.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetDisplayCertificatesForMultipleEntities() { | ||
| BackendResource res = mock(BackendResource.class); | ||
|
|
||
| QueryReturnValue result = new QueryReturnValue(); | ||
| result.setSucceeded(true); | ||
| Map<Guid, String> subjects = GUIDS.stream().collect(Collectors.toMap(Function.identity(), id -> CERTIFICATE)); | ||
| result.setReturnValue(subjects); | ||
| when(res.runQuery(eq(QueryType.GetVdsCertificateSubjectsByVmIds), any())).thenReturn(result); | ||
|
|
||
| result = new QueryReturnValue(); | ||
| result.setSucceeded(true); | ||
| result.setReturnValue(CA_CERT); | ||
| when(res.runQuery(eq(QueryType.GetCACertificate), any())).thenReturn(result); | ||
|
|
||
|
|
||
| Map<Guid, Certificate> certificates = DisplayHelper.getDisplayCertificatesForMultipleEntities(res, GUIDS); | ||
| assertEquals(GUIDS.size(), certificates.size()); | ||
| for (Guid guid : GUIDS) { | ||
| Certificate cert = certificates.get(guid); | ||
| assertNotNull(cert); | ||
| assertEquals(CERTIFICATE, cert.getSubject()); | ||
| assertEquals(CA_CERT, cert.getContent()); | ||
| assertEquals(ORG, cert.getOrganization()); | ||
| } | ||
| } | ||
| } |
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.
Instead of adding this
addDisplayCertificateandgetDisplayCertificatesForMultipleEntitiescould it not be a possibility to instead break out of theaddDisplayCertificatefunction on cases that it is known there will not be a certificate present? This would reduce the added complexity but still result in the wanted output of less queries being run towards the backend. For example, a VM that is down returns null when the QueryType.GetVdsCertificateSubjectByVmId is ran. Thus this could be seen as an unneeded query and the function could be exited before this is executed.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.
Any follow up on this?