1

感谢您花时间看这个问题......请耐心等待......

前几天,我们的一个 Web 服务器停止提供网页。Web 服务器是运行带有 IIS 6 的 Windows Server 2003 R2 Standard Edition Service Pack 2 的物理服务器。它运行带有 Java 1.6.0_24 的 ColdFusion 8.0.1 Standard。该服务器有 3 个面向公众的网站,它们的使用率并不高。所有三个网站上的页面都超时或返回 500 错误。

当我登录服务器查看问题所在时,我期待看到 JRUN 服务使用大量内存。内存很好,但是,我注意到 CPU 运行在 100% 或接近 100%。大约 50% 被 JRUN 使用,另外 50% 被我杀死的失控备份进程使用。但随后 JRUN 很快耗尽了 CPU 并且使用率接近 100%。

我查看了 ColdFusion 日志并注意到发生了几个Java 堆空间错误。

我查看了 IIS 日志,发现有一堆对应用程序的请求,它允许我们的一个客户使用 uploadify 为他们的产品上传多个图像文件。该应用程序是用 ColdFusion 编写的,并使用 jQuery 调用 Web 服务来处理上传并使用<CFIMAGE>.

在日志中看到这些信息后,我认为这个应用程序的某些部分一定是罪魁祸首。

我似乎无法找到导致 Java 堆空间错误和 CPU 峰值的确切原因。有什么想法吗?

WebService CFC方法:

<cffunction
    name="uploadFile"
    access="remote"
    returntype="Struct"
    output="false"
    returnformat="JSON">
    <cfargument name="FileName" type="String" default="" />
    <cfargument name="FileData" type="String" default="" />

    <cfscript>
        var _response = NewAPIResponse();
        var _tempFilePath = APPLICATION.TempDir & "\" & ARGUMENTS.FileName;
        var _qItem = QueryNew("");
        var _product = CreateObject("component", "cfc.Product");
        var _result = {};
        var _sku = "";

        /*
            Each file must be named [Part Number].[file extension], so, \
            parse the file name to get everything before the file extension
        */
        _sku =
            Trim(
                REQUEST.UDFLib.File.getFileNameWithoutExtension(
                    ARGUMENTS.FileName
                    )
                );
    </cfscript>

    <cfif Len(_sku) GT 20>
        <cfthrow
            message="#ARGUMENTS.FileName#: File Name does not correspond to an existing Part Number." />
    </cfif>

    <cfset _qItem = _product.readSKU(_sku) />

    <cfif NOT _qItem.RECORDCOUNT>
        <cfthrow
            message="#ARGUMENTS.FileName#: File Name does not correspond to an existing Part Number." />
    </cfif>

    <cfscript>
        FileCopy(ARGUMENTS.FileData, _tempFilePath);

        _aMessages =
            _product.setId(
                _qItem.SKU
                ).updateThumbnailImages(uploadFilePath = _tempFilePath);
    </cfscript>

    <cfif ArrayLen(_aMessages)>
        <cfthrow
            message="#ARGUMENTS.FileName#: #_aMessages[1].getText()#" />
    </cfif>

    <cfscript>
        _result["SKU"] = _product.getSKU();
        _result["SMALLIMAGESRC"] = _product.getSmallImageSrc();
        _result["LARGEIMAGESRC"] = _product.getLargeImageSrc();

        ArrayAppend(_response.data, _result);
    </cfscript>

    <cfreturn _response />
</cffunction>

图像调整功能:

<cffunction name="updateThumbnailImages" returntype="Array" output="false">
    <cfargument name="uploadFilePath" type="String" required="true" />

    <cfset var _image = {} />

    <cfif FileExists(ARGUMENTS.uploadFilePath)>
        <cfset _image =
            REQUEST.UDFLib.Image.scale(
                imagePath = ARGUMENTS.uploadFilePath,
                maxHeight = 500,
                maxWidth = 700
                ) />

        <cfimage
            action="write"
            source="#_image#"
            overwrite="true"
            destination="#getLargeImagePath()#" />

        <cfset _image =
            REQUEST.UDFLib.Image.scale(
                imagePath = ARGUMENTS.uploadFilePath,
                maxHeight = 300,
                maxWidth = 300
                ) />

        <cfimage
            action="write"
            source="#_image#"
            overwrite="true"
            destination="#getMediumImagePath()#" />

        <cfset _image =
            REQUEST.UDFLib.Image.scale(
                imagePath = ARGUMENTS.uploadFilePath,
                maxHeight = 50,
                maxWidth = 50
                ) />

        <cfimage
            action="write"
            source="#_image#"
            overwrite="true"
            destination="#getSmallImagePath()#" />
    </cfif>
</cffunction>

图像缩放 UDF:

<cffunction name="getDimensionsToEnlarge" returntype="Struct" output="false">
    <cfargument name="imageWidth" type="Numeric" required="true" />
    <cfargument name="imageHeight" type="Numeric" required="true" />
    <cfargument name="minWidth" type="Numeric" required="true" />
    <cfargument name="minHeight" type="Numeric" required="true" />

    <cfscript>
        var dimensions = {
            width = -1,
            height = -1
            };

        if  (
                ARGUMENTS.minHeight > 0
            &&  ARGUMENTS.minWidth > 0
            &&  imageHeight < ARGUMENTS.minHeight
            &&  imageWidth < ARGUMENTS.minWidth
            ) {
            dimensions.width = ARGUMENTS.minWidth;
            dimensions.height = ARGUMENTS.minHeight;
        }

        return dimensions;
    </cfscript>
</cffunction>

<cffunction name="getDimensionsToShrink" returntype="Struct" output="false">
    <cfargument name="imageWidth" type="Numeric" required="true" />
    <cfargument name="imageHeight" type="Numeric" required="true" />
    <cfargument name="maxWidth" type="Numeric" required="true" />
    <cfargument name="maxHeight" type="Numeric" required="true" />

    <cfscript>
        var dimensions = {
            width = -1,
            height = -1
            };

        if  (
                ARGUMENTS.maxHeight > 0
            &&  ARGUMENTS.maxWidth > 0
            &&  (
                    imageHeight > ARGUMENTS.maxHeight
                ||  imageWidth > ARGUMENTS.maxWidth
                )
            ) {
            dimensions.width = ARGUMENTS.maxWidth;
            dimensions.height = ARGUMENTS.maxHeight;
        }

        return dimensions;
    </cfscript>
</cffunction>

<cffunction name="getDimensionsToFit" returntype="Struct" output="false">
    <cfargument name="imageWidth" type="Numeric" required="true" />
    <cfargument name="imageHeight" type="Numeric" required="true" />
    <cfargument name="minWidth" type="Numeric" required="true" />
    <cfargument name="minHeight" type="Numeric" required="true" />
    <cfargument name="maxWidth" type="Numeric" required="true" />
    <cfargument name="maxHeight" type="Numeric" required="true" />

    <cfscript>
        var dimensions = {
            width = -1,
            height = -1
            };

        dimensions =
            getDimensionsToEnlarge(
                imageHeight = ARGUMENTS.imageHeight,
                imageWidth = ARGUMENTS.imageWidth,
                minWidth = ARGUMENTS.minWidth,
                minHeight = ARGUMENTS.minHeight
                );

        if (dimensions.width < 0 && dimensions.height < 0)
            dimensions =
                getDimensionsToShrink(
                    imageHeight = ARGUMENTS.imageHeight,
                    imageWidth = ARGUMENTS.imageWidth,
                    maxWidth = ARGUMENTS.maxWidth,
                    maxHeight = ARGUMENTS.maxHeight
                    );

        return dimensions;
    </cfscript>
</cffunction>

<cffunction name="scale" returntype="Any" output="false">
    <cfargument name="imagePath" type="String" required="true" />
    <cfargument name="action" type="String" default="fit" hint="shrink, enlarge, or fit"/>
    <cfargument name="minWidth" type="Numeric" default="-1" />
    <cfargument name="minHeight" type="Numeric" default="-1" />
    <cfargument name="maxWidth" type="Numeric" default="-1" />
    <cfargument name="maxHeight" type="Numeric" default="-1" />

    <cfscript>
        var scaledDimensions = {
                width = -1,
                height = -1
            };
        var scaledImage = ImageNew();

        scaledImage = ImageNew(ARGUMENTS.imagePath);

        switch (ARGUMENTS.action) {
            case "shrink":
                scaledDimensions =
                    getDimensionsToShrink(
                        imageHeight = scaledImage.getHeight(),
                        imageWidth = scaledImage.getWidth(),
                        maxWidth = ARGUMENTS.maxWidth,
                        maxHeight = ARGUMENTS.maxHeight
                    );

                break;
            case "enlarge":
                scaledDimensions =
                    getDimensionsToEnlarge(
                        imageHeight = scaledImage.getHeight(),
                        imageWidth = scaledImage.getWidth(),
                        minWidth = ARGUMENTS.minWidth,
                        minHeight = ARGUMENTS.minHeight
                    );

                break;
            default:
                scaledDimensions =
                    getDimensionsToFit(
                        imageHeight = scaledImage.getHeight(),
                        imageWidth = scaledImage.getWidth(),
                        minWidth = ARGUMENTS.minWidth,
                        minHeight = ARGUMENTS.minHeight,
                        maxWidth = ARGUMENTS.maxWidth,
                        maxHeight = ARGUMENTS.maxHeight
                    );

                break;
        }

        if (scaledDimensions.width > 0 && scaledDimensions.height > 0) {
            // This helps the image quality
            ImageSetAntialiasing(scaledImage, "on");

            ImageScaleToFit(
                scaledImage,
                scaledDimensions.width,
                scaledDimensions.height
                );
        }

        return scaledImage;
    </cfscript>
</cffunction>
4

1 回答 1

0

下面是我在评论部分提到的示例代码。这提高了我们的可靠性,但在极端负载下我们发现问题仍然存在,这就是我们迁移到 Java 1.7 的原因。然而 1.7 有它自己的问题,因为性能急剧下降,如此所述。希望能帮助到你。

<cffunction name="init" access="private" output="false" returntype="void">
    <cfset variables.instance = StructNew() />
    <!--- create object and set to variables.instance --->
    <cfset setProductObject()>
</cffunction>

<cffunction name="setProductObject" access="private" returntype="void" output="false">
    <!--- create object once and set it to the variables.instance scope --->
    <cfset var product = createObject("component","cfc.Product")>
    <cfset variables.instance.product = product/>
    <cfset product = javacast('null', '')>
</cffunction>

<cffunction name="getProductObject" access="private" returntype="cfc.Product" output="false">
    <!--- instead of creating object each time use the one already in memory and duplicate it --->
    <cfset var productObj = duplicate(variables.instance.product)>
    <cfreturn productObj />
</cffunction>

<cffunction name="uploadFile" access="remote" returntype="struct" returnformat="JSON" output="false">
    <cfargument name="FileName" type="String" default="" />
    <cfargument name="FileData" type="String" default="" />
        <cfscript>
            var _response = NewAPIResponse();
            var _tempFilePath = APPLICATION.TempDir & "\" & ARGUMENTS.FileName;
            var _qItem = QueryNew("");
            var _product = "";
            var _result = {};
            var _sku = "";
            //check init() function has been run if not run it
            if NOT structKeyExists(variables,'instance') {
                init();         
            }

            _product = getProductObject();
            /*
                Each file must be named [Part Number].[file extension], so, \
                parse the file name to get everything before the file extension
            */
            _sku =
                Trim(
                    REQUEST.UDFLib.File.getFileNameWithoutExtension(
                        ARGUMENTS.FileName
                        )
                    );
        </cfscript>

        <cfif Len(_sku) GT 20>
            <cfthrow
                message="#ARGUMENTS.FileName#: File Name does not correspond to an existing Part Number." />
        </cfif>

        <cfset _qItem = _product.readSKU(_sku) />

        <cfif NOT _qItem.RECORDCOUNT>
            <cfthrow
                message="#ARGUMENTS.FileName#: File Name does not correspond to an existing Part Number." />
        </cfif>

        <cfscript>
            FileCopy(ARGUMENTS.FileData, _tempFilePath);

            _aMessages =
                _product.setId(
                    _qItem.SKU
                    ).updateThumbnailImages(uploadFilePath = _tempFilePath);
        </cfscript>

        <cfif ArrayLen(_aMessages)>
            <cfthrow
                message="#ARGUMENTS.FileName#: #_aMessages[1].getText()#" />
        </cfif>

        <cfscript>
            _result["SKU"] = _product.getSKU();
            _result["SMALLIMAGESRC"] = _product.getSmallImageSrc();
            _result["LARGEIMAGESRC"] = _product.getLargeImageSrc();

            ArrayAppend(_response.data, _result);
        </cfscript>

        <cfreturn _response />
</cffunction>

于 2014-08-12T19:59:37.573 回答