Search in sources :

Example 1 with Biz

use of com.alipay.sofa.ark.spi.model.Biz in project sofa-ark by alipay.

the class BizModel method start.

@Override
public void start(String[] args) throws Throwable {
    AssertUtils.isTrue(bizState == BizState.RESOLVED, "BizState must be RESOLVED");
    if (mainClass == null) {
        throw new ArkRuntimeException(String.format("biz: %s has no main method", getBizName()));
    }
    ClassLoader oldClassLoader = ClassLoaderUtils.pushContextClassLoader(this.classLoader);
    EventAdminService eventAdminService = ArkServiceContainerHolder.getContainer().getService(EventAdminService.class);
    try {
        eventAdminService.sendEvent(new BeforeBizStartupEvent(this));
        resetProperties();
        if (!isMasterBizAndEmbedEnable()) {
            long start = System.currentTimeMillis();
            MainMethodRunner mainMethodRunner = new MainMethodRunner(mainClass, args);
            mainMethodRunner.run();
            // this can trigger health checker handler
            eventAdminService.sendEvent(new AfterBizStartupEvent(this));
            LOGGER.info("Ark biz {} started in {} ms", getIdentity(), (System.currentTimeMillis() - start));
        }
    } catch (Throwable e) {
        bizState = BizState.BROKEN;
        throw e;
    } finally {
        ClassLoaderUtils.popContextClassLoader(oldClassLoader);
    }
    BizManagerService bizManagerService = ArkServiceContainerHolder.getContainer().getService(BizManagerService.class);
    if (Boolean.getBoolean(Constants.ACTIVATE_NEW_MODULE)) {
        Biz currentActiveBiz = bizManagerService.getActiveBiz(bizName);
        if (currentActiveBiz == null) {
            bizState = BizState.ACTIVATED;
        } else {
            ((BizModel) currentActiveBiz).setBizState(BizState.DEACTIVATED);
            bizState = BizState.ACTIVATED;
        }
    } else {
        if (bizManagerService.getActiveBiz(bizName) == null) {
            bizState = BizState.ACTIVATED;
        } else {
            bizState = BizState.DEACTIVATED;
        }
    }
}
Also used : BeforeBizStartupEvent(com.alipay.sofa.ark.spi.event.biz.BeforeBizStartupEvent) MainMethodRunner(com.alipay.sofa.ark.bootstrap.MainMethodRunner) Biz(com.alipay.sofa.ark.spi.model.Biz) EventAdminService(com.alipay.sofa.ark.spi.service.event.EventAdminService) AfterBizStartupEvent(com.alipay.sofa.ark.spi.event.biz.AfterBizStartupEvent) AbstractClasspathClassLoader(com.alipay.sofa.ark.container.service.classloader.AbstractClasspathClassLoader) BizManagerService(com.alipay.sofa.ark.spi.service.biz.BizManagerService) ArkRuntimeException(com.alipay.sofa.ark.exception.ArkRuntimeException)

Example 2 with Biz

use of com.alipay.sofa.ark.spi.model.Biz in project sofa-ark by alipay.

the class HandleArchiveStage method process.

@Override
public void process(PipelineContext pipelineContext) throws ArkRuntimeException {
    try {
        if (ArkConfigs.isEmbedEnable()) {
            processEmbed(pipelineContext);
            return;
        }
        ExecutableArchive executableArchive = pipelineContext.getExecutableArchive();
        List<BizArchive> bizArchives = executableArchive.getBizArchives();
        List<PluginArchive> pluginArchives = executableArchive.getPluginArchives();
        if (useDynamicConfig()) {
            AssertUtils.isFalse(StringUtils.isEmpty(ArkConfigs.getStringValue(Constants.MASTER_BIZ)), "Master biz should be configured when using dynamic config.");
        }
        int bizCount = 0;
        for (BizArchive bizArchive : bizArchives) {
            Biz biz = bizFactoryService.createBiz(bizArchive);
            if (bizArchive instanceof DirectoryBizArchive) {
                if (!((DirectoryBizArchive) bizArchive).isTestMode()) {
                    bizManagerService.registerBiz(biz);
                    bizCount += 1;
                }
            } else if (useDynamicConfig()) {
                if (biz.getBizName().equals(ArkConfigs.getStringValue(Constants.MASTER_BIZ))) {
                    bizManagerService.registerBiz(biz);
                    bizCount += 1;
                } else {
                    LOGGER.warn("The biz of {} is ignored when using dynamic config.", biz.getIdentity());
                }
            } else {
                if (!isBizExcluded(biz)) {
                    bizManagerService.registerBiz(biz);
                    bizCount += 1;
                } else {
                    LOGGER.warn(String.format("The biz of %s is excluded.", biz.getIdentity()));
                }
            }
        }
        // master biz should be specified when deploy multi biz, otherwise the only biz would be token as master biz
        if (bizCount > 1) {
            AssertUtils.isFalse(StringUtils.isEmpty(ArkConfigs.getStringValue(Constants.MASTER_BIZ)), "Master biz should be configured when deploy multi biz.");
            String masterBizName = ArkConfigs.getStringValue(Constants.MASTER_BIZ);
            for (Biz biz : bizManagerService.getBizInOrder()) {
                if (masterBizName.equals(biz.getBizName())) {
                    ArkClient.setMasterBiz(biz);
                }
            }
        } else {
            List<Biz> bizList = bizManagerService.getBizInOrder();
            if (!bizList.isEmpty() && StringUtils.isEmpty(ArkConfigs.getStringValue(Constants.MASTER_BIZ))) {
                ArkConfigs.putStringValue(Constants.MASTER_BIZ, bizList.get(0).getBizName());
                ArkClient.setMasterBiz(bizList.get(0));
            }
        }
        URL[] exportUrls = null;
        Set<String> exportPackages = new HashSet<>();
        Biz masterBiz = ArkClient.getMasterBiz();
        for (BizArchive bizArchive : bizArchives) {
            Attributes mainAttributes = bizArchive.getManifest().getMainAttributes();
            String bizName = mainAttributes.getValue(ARK_BIZ_NAME);
            // extension from master biz
            if (bizArchive instanceof JarBizArchive && masterBiz.getBizName().equalsIgnoreCase(bizName)) {
                String exportPackageStr = mainAttributes.getValue(INJECT_EXPORT_PACKAGES);
                exportPackages.addAll(StringUtils.strToSet(exportPackageStr, MANIFEST_VALUE_SPLIT));
                exportUrls = ((JarBizArchive) bizArchive).getExportUrls();
            }
        }
        for (PluginArchive pluginArchive : pluginArchives) {
            Plugin plugin = pluginFactoryService.createPlugin(pluginArchive, exportUrls, exportPackages);
            if (!isPluginExcluded(plugin)) {
                pluginManagerService.registerPlugin(plugin);
            } else {
                LOGGER.warn(String.format("The plugin of %s is excluded.", plugin.getPluginName()));
            }
        }
    } catch (Throwable ex) {
        throw new ArkRuntimeException(ex.getMessage(), ex);
    }
}
Also used : ExecutableArchive(com.alipay.sofa.ark.spi.archive.ExecutableArchive) DirectoryBizArchive(com.alipay.sofa.ark.loader.DirectoryBizArchive) PluginArchive(com.alipay.sofa.ark.spi.archive.PluginArchive) Attributes(java.util.jar.Attributes) URL(java.net.URL) ArkRuntimeException(com.alipay.sofa.ark.exception.ArkRuntimeException) Biz(com.alipay.sofa.ark.spi.model.Biz) JarBizArchive(com.alipay.sofa.ark.loader.JarBizArchive) BizArchive(com.alipay.sofa.ark.spi.archive.BizArchive) DirectoryBizArchive(com.alipay.sofa.ark.loader.DirectoryBizArchive) JarBizArchive(com.alipay.sofa.ark.loader.JarBizArchive) HashSet(java.util.HashSet) Plugin(com.alipay.sofa.ark.spi.model.Plugin)

Example 3 with Biz

use of com.alipay.sofa.ark.spi.model.Biz in project sofa-ark by alipay.

the class BizManagerServiceImpl method getActiveBiz.

@Override
public Biz getActiveBiz(String bizName) {
    AssertUtils.isFalse(StringUtils.isEmpty(bizName), "Biz name must not be empty.");
    Map<String, Biz> bizCache = bizRegistration.get(bizName);
    if (bizCache != null) {
        for (Biz biz : bizCache.values()) {
            if (biz.getBizState() == BizState.ACTIVATED) {
                return biz;
            }
        }
    }
    return null;
}
Also used : Biz(com.alipay.sofa.ark.spi.model.Biz)

Example 4 with Biz

use of com.alipay.sofa.ark.spi.model.Biz in project sofa-ark by alipay.

the class BizManagerServiceImpl method getBizInOrder.

@Override
public List<Biz> getBizInOrder() {
    List<Biz> bizList = new ArrayList<>();
    for (String bizName : bizRegistration.keySet()) {
        bizList.addAll(bizRegistration.get(bizName).values());
    }
    Collections.sort(bizList, new OrderComparator());
    return bizList;
}
Also used : Biz(com.alipay.sofa.ark.spi.model.Biz) OrderComparator(com.alipay.sofa.ark.common.util.OrderComparator)

Example 5 with Biz

use of com.alipay.sofa.ark.spi.model.Biz in project sofa-ark by alipay.

the class OperationTransformerTest method testTransformUnstableState.

@Test
public void testTransformUnstableState() {
    Exception ex = null;
    try {
        List<Biz> bizList = new ArrayList<>();
        Biz biz1 = Mockito.mock(Biz.class);
        Biz biz2 = Mockito.mock(Biz.class);
        Biz biz3 = Mockito.mock(Biz.class);
        bizList.add(biz1);
        bizList.add(biz2);
        bizList.add(biz3);
        when(biz1.getBizName()).thenReturn("nameA");
        when(biz1.getBizVersion()).thenReturn("vA");
        when(biz1.getBizState()).thenReturn(BizState.ACTIVATED);
        when(biz1.getBizName()).thenReturn("nameA");
        when(biz1.getBizVersion()).thenReturn("vB");
        when(biz1.getBizState()).thenReturn(BizState.RESOLVED);
        when(biz1.getBizName()).thenReturn("nameB");
        when(biz1.getBizVersion()).thenReturn("vA");
        when(biz1.getBizState()).thenReturn(BizState.ACTIVATED);
        PluginContext pluginContext = Mockito.mock(PluginContext.class);
        ServiceReference serviceReference = Mockito.mock(ServiceReference.class);
        BizManagerService bizManagerService = Mockito.mock(BizManagerService.class);
        when(serviceReference.getService()).thenReturn(bizManagerService);
        when(pluginContext.referenceService(any(Class.class))).thenReturn(serviceReference);
        when(bizManagerService.getBizInOrder()).thenReturn(bizList);
        OperationTransformer.transformToBizOperation("nameA:vA:activated;nameA:vB:deactivated;nameB:vB:activated", pluginContext);
    } catch (Exception e) {
        ex = e;
    }
    Assert.assertNotNull(ex);
    Assert.assertTrue(ex.getMessage().contains("Exist illegal biz"));
}
Also used : Biz(com.alipay.sofa.ark.spi.model.Biz) PluginContext(com.alipay.sofa.ark.spi.model.PluginContext) ArrayList(java.util.ArrayList) BizManagerService(com.alipay.sofa.ark.spi.service.biz.BizManagerService) ServiceReference(com.alipay.sofa.ark.spi.registry.ServiceReference) Test(org.junit.Test)

Aggregations

Biz (com.alipay.sofa.ark.spi.model.Biz)38 Test (org.junit.Test)14 BaseTest (com.alipay.sofa.ark.container.BaseTest)13 BizModel (com.alipay.sofa.ark.container.model.BizModel)12 ArkRuntimeException (com.alipay.sofa.ark.exception.ArkRuntimeException)6 BizManagerService (com.alipay.sofa.ark.spi.service.biz.BizManagerService)6 Plugin (com.alipay.sofa.ark.spi.model.Plugin)5 AfterBizStartupEvent (com.alipay.sofa.ark.spi.event.biz.AfterBizStartupEvent)3 PluginModel (com.alipay.sofa.ark.container.model.PluginModel)2 ExecutableArchive (com.alipay.sofa.ark.spi.archive.ExecutableArchive)2 PluginArchive (com.alipay.sofa.ark.spi.archive.PluginArchive)2 AfterBizSwitchEvent (com.alipay.sofa.ark.spi.event.biz.AfterBizSwitchEvent)2 BeforeBizStartupEvent (com.alipay.sofa.ark.spi.event.biz.BeforeBizStartupEvent)2 BeforeBizSwitchEvent (com.alipay.sofa.ark.spi.event.biz.BeforeBizSwitchEvent)2 BeforePluginStartupEvent (com.alipay.sofa.ark.spi.event.plugin.BeforePluginStartupEvent)2 EventAdminService (com.alipay.sofa.ark.spi.service.event.EventAdminService)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 MainMethodRunner (com.alipay.sofa.ark.bootstrap.MainMethodRunner)1 OrderComparator (com.alipay.sofa.ark.common.util.OrderComparator)1