Skip to content
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

Module deployments are deduplicated when static merge deployments(#338) #1035

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private void addStaticBiz(List<BizArchive> bizArchives) throws IOException {

for (BizArchive bizArchive : bizArchives) {
Biz biz = ArkClient.getBizFactoryService().createBiz(bizArchive);
ArkClient.getBizManagerService().registerBiz(biz);
ArkClient.getBizManagerService().registerBizIfAbsent(biz);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void processStaticBizFromClasspath(PipelineContext pipelineContext) throw
List<BizArchive> bizArchives = executableArchive.getBizArchives();
for (BizArchive bizArchive : bizArchives) {
Biz biz = bizFactoryService.createBiz(bizArchive);
bizManagerService.registerBiz(biz);
bizManagerService.registerBizIfAbsent(biz);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,13 @@ public boolean removeAndAddBiz(Biz addingBiz, Biz removingBiz) {
public ConcurrentHashMap<String, ConcurrentHashMap<String, Biz>> getBizRegistration() {
return bizRegistration;
}

@Override
public boolean registerBizIfAbsent(Biz biz) {
AssertUtils.assertNotNull(biz, "Biz must not be null.");
AssertUtils.isTrue(biz.getBizState() == BizState.RESOLVED, "BizState must be RESOLVED.");
bizRegistration.putIfAbsent(biz.getBizName(), new ConcurrentHashMap<>(16));
ConcurrentHashMap<String, Biz> bizCache = bizRegistration.get(biz.getBizName());
return bizCache.putIfAbsent(biz.getBizVersion(), biz) == null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
*/
package com.alipay.sofa.ark.container.pipeline;

import com.alipay.sofa.ark.api.ArkClient;
import com.alipay.sofa.ark.container.model.BizModel;
import com.alipay.sofa.ark.loader.DirectoryBizArchive;
import com.alipay.sofa.ark.loader.JarBizArchive;
import com.alipay.sofa.ark.spi.archive.BizArchive;
import com.alipay.sofa.ark.spi.archive.ExecutableArchive;
import com.alipay.sofa.ark.spi.archive.PluginArchive;
import com.alipay.sofa.ark.spi.model.Biz;
import com.alipay.sofa.ark.spi.model.Plugin;
import com.alipay.sofa.ark.spi.pipeline.PipelineContext;
import com.alipay.sofa.ark.spi.service.biz.BizFactoryService;
Expand All @@ -31,16 +33,17 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import static com.alipay.sofa.ark.api.ArkConfigs.getStringValue;
import static com.alipay.sofa.ark.api.ArkConfigs.setSystemProperty;
import static com.alipay.sofa.ark.spi.constant.Constants.CONFIG_SERVER_ADDRESS;
import static com.alipay.sofa.ark.spi.constant.Constants.MASTER_BIZ;
import static com.alipay.sofa.ark.spi.constant.Constants.*;
import static java.util.Arrays.asList;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -152,13 +155,25 @@ public void testProcess() throws Exception {

@Test
public void testProcessStaticBizFromClasspath() throws Exception {

BizArchive bizArchive = mock(BizArchive.class);
when(executableArchive.getBizArchives()).thenReturn(asList(bizArchive));

handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
verify(bizFactoryService, times(1)).createBiz((bizArchive));
verify(bizManagerService, times(1)).registerBiz(null);
MockedStatic<ArkClient> arkClientMockedStatic = mockStatic(ArkClient.class);
arkClientMockedStatic.when(ArkClient::getBizManagerService).thenReturn(bizManagerService);
try {
BizArchive bizArchive = mock(BizArchive.class);
Manifest manifest = new Manifest();
Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.putValue(ARK_BIZ_NAME, "biz1");
mainAttributes.putValue(ARK_BIZ_VERSION, "0.0.1");
when(bizArchive.getManifest()).thenReturn(manifest);
ConcurrentHashMap<String, ConcurrentHashMap<String, Biz>> map = new ConcurrentHashMap<>();
when(bizManagerService.getBizRegistration()).thenReturn(map);
when(executableArchive.getBizArchives()).thenReturn(asList(bizArchive));

handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
verify(bizFactoryService, times(1)).createBiz((bizArchive));
verify(bizManagerService, times(1)).registerBizIfAbsent(null);
Comment on lines +171 to +173
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect test data: Biz object is null

The test is verifying registerBizIfAbsent(null), which doesn't properly test the deduplication logic. The Biz object should be created from the mocked BizArchive.

Apply this diff to fix the test:

-            handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
-            verify(bizFactoryService, times(1)).createBiz((bizArchive));
-            verify(bizManagerService, times(1)).registerBizIfAbsent(null);
+            Biz mockBiz = mock(Biz.class);
+            when(bizFactoryService.createBiz(bizArchive)).thenReturn(mockBiz);
+            
+            handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
+            
+            verify(bizFactoryService, times(1)).createBiz(bizArchive);
+            verify(bizManagerService, times(1)).registerBizIfAbsent(mockBiz);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
verify(bizFactoryService, times(1)).createBiz((bizArchive));
verify(bizManagerService, times(1)).registerBizIfAbsent(null);
Biz mockBiz = mock(Biz.class);
when(bizFactoryService.createBiz(bizArchive)).thenReturn(mockBiz);
handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
verify(bizFactoryService, times(1)).createBiz(bizArchive);
verify(bizManagerService, times(1)).registerBizIfAbsent(mockBiz);

}finally {
arkClientMockedStatic.close();
}
Comment on lines +158 to +176
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance test coverage for deduplication scenarios

The test should verify the actual deduplication behavior by testing scenarios where:

  1. A Biz doesn't exist (should register)
  2. A Biz already exists (should skip registration)

Consider adding these test cases:

+    @Test
+    public void testProcessStaticBizFromClasspathWithExistingBiz() throws Exception {
+        MockedStatic<ArkClient> arkClientMockedStatic = mockStatic(ArkClient.class);
+        arkClientMockedStatic.when(ArkClient::getBizManagerService).thenReturn(bizManagerService);
+        try {
+            // Setup existing biz
+            ConcurrentHashMap<String, ConcurrentHashMap<String, Biz>> map = new ConcurrentHashMap<>();
+            ConcurrentHashMap<String, Biz> versionMap = new ConcurrentHashMap<>();
+            Biz existingBiz = mock(Biz.class);
+            when(existingBiz.getBizName()).thenReturn("biz1");
+            when(existingBiz.getBizVersion()).thenReturn("0.0.1");
+            versionMap.put("0.0.1", existingBiz);
+            map.put("biz1", versionMap);
+            when(bizManagerService.getBizRegistration()).thenReturn(map);
+
+            // Setup new biz with same name and version
+            BizArchive bizArchive = mock(BizArchive.class);
+            Manifest manifest = new Manifest();
+            Attributes mainAttributes = manifest.getMainAttributes();
+            mainAttributes.putValue(ARK_BIZ_NAME, "biz1");
+            mainAttributes.putValue(ARK_BIZ_VERSION, "0.0.1");
+            when(bizArchive.getManifest()).thenReturn(manifest);
+            when(executableArchive.getBizArchives()).thenReturn(asList(bizArchive));
+
+            Biz newBiz = mock(Biz.class);
+            when(bizFactoryService.createBiz(bizArchive)).thenReturn(newBiz);
+
+            handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
+
+            // Verify biz was created but not registered due to existing registration
+            verify(bizFactoryService, times(1)).createBiz(bizArchive);
+            verify(bizManagerService, times(1)).registerBizIfAbsent(newBiz);
+            verify(bizManagerService, never()).registerBiz(any());
+        } finally {
+            arkClientMockedStatic.close();
+        }
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
MockedStatic<ArkClient> arkClientMockedStatic = mockStatic(ArkClient.class);
arkClientMockedStatic.when(ArkClient::getBizManagerService).thenReturn(bizManagerService);
try {
BizArchive bizArchive = mock(BizArchive.class);
Manifest manifest = new Manifest();
Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.putValue(ARK_BIZ_NAME, "biz1");
mainAttributes.putValue(ARK_BIZ_VERSION, "0.0.1");
when(bizArchive.getManifest()).thenReturn(manifest);
ConcurrentHashMap<String, ConcurrentHashMap<String, Biz>> map = new ConcurrentHashMap<>();
when(bizManagerService.getBizRegistration()).thenReturn(map);
when(executableArchive.getBizArchives()).thenReturn(asList(bizArchive));
handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
verify(bizFactoryService, times(1)).createBiz((bizArchive));
verify(bizManagerService, times(1)).registerBizIfAbsent(null);
}finally {
arkClientMockedStatic.close();
}
MockedStatic<ArkClient> arkClientMockedStatic = mockStatic(ArkClient.class);
arkClientMockedStatic.when(ArkClient::getBizManagerService).thenReturn(bizManagerService);
try {
BizArchive bizArchive = mock(BizArchive.class);
Manifest manifest = new Manifest();
Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.putValue(ARK_BIZ_NAME, "biz1");
mainAttributes.putValue(ARK_BIZ_VERSION, "0.0.1");
when(bizArchive.getManifest()).thenReturn(manifest);
ConcurrentHashMap<String, ConcurrentHashMap<String, Biz>> map = new ConcurrentHashMap<>();
when(bizManagerService.getBizRegistration()).thenReturn(map);
when(executableArchive.getBizArchives()).thenReturn(asList(bizArchive));
handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
verify(bizFactoryService, times(1)).createBiz((bizArchive));
verify(bizManagerService, times(1)).registerBizIfAbsent(null);
}finally {
arkClientMockedStatic.close();
}
@Test
public void testProcessStaticBizFromClasspathWithExistingBiz() throws Exception {
MockedStatic<ArkClient> arkClientMockedStatic = mockStatic(ArkClient.class);
arkClientMockedStatic.when(ArkClient::getBizManagerService).thenReturn(bizManagerService);
try {
// Setup existing biz
ConcurrentHashMap<String, ConcurrentHashMap<String, Biz>> map = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Biz> versionMap = new ConcurrentHashMap<>();
Biz existingBiz = mock(Biz.class);
when(existingBiz.getBizName()).thenReturn("biz1");
when(existingBiz.getBizVersion()).thenReturn("0.0.1");
versionMap.put("0.0.1", existingBiz);
map.put("biz1", versionMap);
when(bizManagerService.getBizRegistration()).thenReturn(map);
// Setup new biz with same name and version
BizArchive bizArchive = mock(BizArchive.class);
Manifest manifest = new Manifest();
Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.putValue(ARK_BIZ_NAME, "biz1");
mainAttributes.putValue(ARK_BIZ_VERSION, "0.0.1");
when(bizArchive.getManifest()).thenReturn(manifest);
when(executableArchive.getBizArchives()).thenReturn(asList(bizArchive));
Biz newBiz = mock(Biz.class);
when(bizFactoryService.createBiz(bizArchive)).thenReturn(newBiz);
handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
// Verify biz was created but not registered due to existing registration
verify(bizFactoryService, times(1)).createBiz(bizArchive);
verify(bizManagerService, times(1)).registerBizIfAbsent(newBiz);
verify(bizManagerService, never()).registerBiz(any());
} finally {
arkClientMockedStatic.close();
}
}

Comment on lines +158 to +176
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Test implementation needs improvement

Several issues need to be addressed:

  1. The verification registerBizIfAbsent(null) on line 173 appears incorrect as we're creating a biz archive and factory but not verifying the actual biz object.
  2. The test doesn't verify the deduplication behavior which is the main objective of this PR.
  3. Missing verification of manifest attributes usage.

Consider refactoring the test like this:

 @Test
 public void testProcessStaticBizFromClasspath() throws Exception {
     MockedStatic<ArkClient> arkClientMockedStatic = mockStatic(ArkClient.class);
     arkClientMockedStatic.when(ArkClient::getBizManagerService).thenReturn(bizManagerService);
     try {
         BizArchive bizArchive = mock(BizArchive.class);
         Manifest manifest = new Manifest();
         Attributes mainAttributes = manifest.getMainAttributes();
         mainAttributes.putValue(ARK_BIZ_NAME, "biz1");
         mainAttributes.putValue(ARK_BIZ_VERSION, "0.0.1");
         when(bizArchive.getManifest()).thenReturn(manifest);
         ConcurrentHashMap<String, ConcurrentHashMap<String, Biz>> map = new ConcurrentHashMap<>();
         when(bizManagerService.getBizRegistration()).thenReturn(map);
         when(executableArchive.getBizArchives()).thenReturn(asList(bizArchive));
+        // Create and configure mock Biz
+        Biz mockBiz = mock(Biz.class);
+        when(mockBiz.getBizName()).thenReturn("biz1");
+        when(mockBiz.getBizVersion()).thenReturn("0.0.1");
+        when(bizFactoryService.createBiz(bizArchive)).thenReturn(mockBiz);
 
         handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
-        verify(bizFactoryService, times(1)).createBiz((bizArchive));
-        verify(bizManagerService, times(1)).registerBizIfAbsent(null);
+        verify(bizFactoryService, times(1)).createBiz(bizArchive);
+        verify(bizManagerService, times(1)).registerBizIfAbsent(mockBiz);
+        
+        // Test deduplication
+        handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
+        // Should still be called only once total
+        verify(bizManagerService, times(1)).registerBizIfAbsent(mockBiz);
     }finally {
         arkClientMockedStatic.close();
     }
 }

This refactored version:

  1. Properly creates and verifies a mock Biz object
  2. Tests the deduplication behavior by calling the method twice
  3. Verifies that registration happens only once
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
MockedStatic<ArkClient> arkClientMockedStatic = mockStatic(ArkClient.class);
arkClientMockedStatic.when(ArkClient::getBizManagerService).thenReturn(bizManagerService);
try {
BizArchive bizArchive = mock(BizArchive.class);
Manifest manifest = new Manifest();
Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.putValue(ARK_BIZ_NAME, "biz1");
mainAttributes.putValue(ARK_BIZ_VERSION, "0.0.1");
when(bizArchive.getManifest()).thenReturn(manifest);
ConcurrentHashMap<String, ConcurrentHashMap<String, Biz>> map = new ConcurrentHashMap<>();
when(bizManagerService.getBizRegistration()).thenReturn(map);
when(executableArchive.getBizArchives()).thenReturn(asList(bizArchive));
handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
verify(bizFactoryService, times(1)).createBiz((bizArchive));
verify(bizManagerService, times(1)).registerBizIfAbsent(null);
}finally {
arkClientMockedStatic.close();
}
MockedStatic<ArkClient> arkClientMockedStatic = mockStatic(ArkClient.class);
arkClientMockedStatic.when(ArkClient::getBizManagerService).thenReturn(bizManagerService);
try {
BizArchive bizArchive = mock(BizArchive.class);
Manifest manifest = new Manifest();
Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.putValue(ARK_BIZ_NAME, "biz1");
mainAttributes.putValue(ARK_BIZ_VERSION, "0.0.1");
when(bizArchive.getManifest()).thenReturn(manifest);
ConcurrentHashMap<String, ConcurrentHashMap<String, Biz>> map = new ConcurrentHashMap<>();
when(bizManagerService.getBizRegistration()).thenReturn(map);
when(executableArchive.getBizArchives()).thenReturn(asList(bizArchive));
// Create and configure mock Biz
Biz mockBiz = mock(Biz.class);
when(mockBiz.getBizName()).thenReturn("biz1");
when(mockBiz.getBizVersion()).thenReturn("0.0.1");
when(bizFactoryService.createBiz(bizArchive)).thenReturn(mockBiz);
handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
verify(bizFactoryService, times(1)).createBiz(bizArchive);
verify(bizManagerService, times(1)).registerBizIfAbsent(mockBiz);
// Test deduplication
handleArchiveStage.processStaticBizFromClasspath(pipelineContext);
// Should still be called only once total
verify(bizManagerService, times(1)).registerBizIfAbsent(mockBiz);
}finally {
arkClientMockedStatic.close();
}

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,6 @@ public interface BizManagerService {

ConcurrentHashMap<String, ConcurrentHashMap<String, Biz>> getBizRegistration();

boolean registerBizIfAbsent(Biz biz);

}
Loading