Search in sources :

Example 16 with ApplicationResource

use of psiprobe.model.ApplicationResource in project psi-probe by psi-probe.

the class ApplicationUtils method getApplicationDataSourceUsageScores.

/**
 * Gets the application data source usage scores.
 *
 * @param context the context
 * @param resolver the resolver
 * @param containerWrapper the container wrapper
 * @return the application data source usage scores
 */
public static int[] getApplicationDataSourceUsageScores(Context context, ResourceResolver resolver, ContainerWrapperBean containerWrapper) {
    logger.debug("Calculating datasource usage score");
    int[] scores = new int[] { 0, 0 };
    List<ApplicationResource> appResources;
    try {
        appResources = resolver.getApplicationResources(context, containerWrapper);
    } catch (NamingException e) {
        throw new RuntimeException(e);
    }
    for (ApplicationResource appResource : appResources) {
        if (appResource.getDataSourceInfo() != null) {
            scores[0] = Math.max(scores[0], appResource.getDataSourceInfo().getBusyScore());
            scores[1] = Math.max(scores[1], appResource.getDataSourceInfo().getEstablishedScore());
        }
    }
    return scores;
}
Also used : ApplicationResource(psiprobe.model.ApplicationResource) NamingException(javax.naming.NamingException)

Example 17 with ApplicationResource

use of psiprobe.model.ApplicationResource in project psi-probe by psi-probe.

the class BaseTomcatAvailabilityController method handleRequestInternal.

@Override
public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    final long start = System.currentTimeMillis();
    TomcatTestReport tomcatTestReport = new TomcatTestReport();
    // check datasource status
    tomcatTestReport.setDatasourceUsageScore(0);
    boolean allContextsAvailable = true;
    if (getContainerWrapper().getResourceResolver().supportsPrivateResources()) {
        for (Context appContext : getContainerWrapper().getTomcatContainer().findContexts()) {
            allContextsAvailable = allContextsAvailable && getContainerWrapper().getTomcatContainer().getAvailable(appContext);
            List<ApplicationResource> applicationResources = getContainerWrapper().getResourceResolver().getApplicationResources(appContext, getContainerWrapper());
            for (ApplicationResource appResource : applicationResources) {
                DataSourceInfo dsi = appResource.getDataSourceInfo();
                if (dsi != null && dsi.getBusyScore() > tomcatTestReport.getDatasourceUsageScore()) {
                    tomcatTestReport.setContextName(appContext.getName());
                    tomcatTestReport.setDatasourceUsageScore(dsi.getBusyScore());
                    tomcatTestReport.setDataSourceName(appResource.getName());
                }
            }
        }
        tomcatTestReport.setWebappAvailabilityTest(allContextsAvailable ? TomcatTestReport.TEST_PASSED : TomcatTestReport.TEST_FAILED);
    } else {
        List<ApplicationResource> resources = getContainerWrapper().getResourceResolver().getApplicationResources();
        for (ApplicationResource resource : resources) {
            DataSourceInfo dsi = resource.getDataSourceInfo();
            if (dsi != null && dsi.getBusyScore() > tomcatTestReport.getDatasourceUsageScore()) {
                tomcatTestReport.setDatasourceUsageScore(dsi.getBusyScore());
                tomcatTestReport.setDataSourceName(resource.getName());
            }
        }
    }
    tomcatTestReport.setDatasourceTest(TomcatTestReport.TEST_PASSED);
    // try to allocate some memory
    String word = "hello";
    int count = TomcatTestReport.DEFAULT_MEMORY_SIZE / word.length();
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        for (; count > 0; count--) {
            bos.write(word.getBytes(StandardCharsets.UTF_8));
        }
        tomcatTestReport.setMemoryTest(TomcatTestReport.TEST_PASSED);
    } catch (IOException e) {
        tomcatTestReport.setMemoryTest(TomcatTestReport.TEST_FAILED);
        logger.trace("", e);
    }
    // try to open some files
    File tmpDir = new File(System.getProperty("java.io.tmpdir"));
    int fileCount = tomcatTestReport.getDefaultFileCount();
    List<File> files = new ArrayList<>();
    List<OutputStream> fileStreams = new ArrayList<>();
    try {
        for (; fileCount > 0; fileCount--) {
            File file = new File(tmpDir, "tctest_" + fileCount);
            try (OutputStream fos = Files.newOutputStream(file.toPath())) {
                files.add(file);
                fileStreams.add(fos);
                fos.write("this is a test".getBytes(StandardCharsets.UTF_8));
            }
        }
        tomcatTestReport.setFileTest(TomcatTestReport.TEST_PASSED);
    } catch (IOException e) {
        tomcatTestReport.setFileTest(TomcatTestReport.TEST_FAILED);
        logger.trace("", e);
    } finally {
        for (OutputStream fileStream : fileStreams) {
            try {
                fileStream.close();
            } catch (IOException e) {
                logger.trace("", e);
            }
        }
        for (File file : files) {
            Files.delete(file.toPath());
        }
    }
    tomcatTestReport.setTestDuration(System.currentTimeMillis() - start);
    long maxServiceTime = 0;
    // TODO JWL 12/11/2016 - Why is this commented out? If not needed, delete it.
    // check the maximum execution time
    // List<ThreadPool> pools = containerListenerBean.getThreadPools();
    // for (int iPool = 0; iPool < pools.size(); iPool++) {
    // ThreadPool threadPool = (ThreadPool) pools.get(iPool);
    // List<RequestProcessor> threads = threadPool.getRequestProcessors();
    // for (int iThread = 0; iThread < threads.size(); iThread++) {
    // RequestProcessor rp = (RequestProcessor) threads.get(iThread);
    // if (rp.getStage() == 3) {
    // // the request processor is in SERVICE state
    // maxServiceTime = Math.max(maxServiceTime, rp.getProcessingTime());
    // }
    // }
    // }
    tomcatTestReport.setMaxServiceTime(maxServiceTime);
    return new ModelAndView(getViewName(), "testReport", tomcatTestReport);
}
Also used : Context(org.apache.catalina.Context) TomcatTestReport(psiprobe.model.TomcatTestReport) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DataSourceInfo(psiprobe.model.DataSourceInfo) ApplicationResource(psiprobe.model.ApplicationResource) File(java.io.File)

Aggregations

ApplicationResource (psiprobe.model.ApplicationResource)17 ArrayList (java.util.ArrayList)6 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)6 NamingException (javax.naming.NamingException)4 DataSourceInfo (psiprobe.model.DataSourceInfo)4 ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)3 ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 MBeanServer (javax.management.MBeanServer)2 MalformedObjectNameException (javax.management.MalformedObjectNameException)2 ObjectName (javax.management.ObjectName)2 Context (org.apache.catalina.Context)2 NamingResources (org.apache.catalina.deploy.NamingResources)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 AttributeNotFoundException (javax.management.AttributeNotFoundException)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 MBeanException (javax.management.MBeanException)1