IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 30379


Ignore:
Timestamp:
Jan 26, 2011, 2:34:39 PM (15 years ago)
Author:
bills
Message:

in magictool -toprocess look for nodes with dependents (tree nodes) first, then check the skycells.
This results in better throughput and elminates the problem where skycells can
starve the tree nodes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tags/ipp-20101215/ippTools/src/magictool.c

    r29625 r30379  
    829829    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    830830
    831     // look for "inputs" that need to processed
    832     psString query = pxDataGet("magictool_toprocess_inputs.sql");
    833     if (!query) {
    834         psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    835         return false;
    836     }
    837 
    838831    psString whereString = NULL;
    839832    if (psListLength(where->list)) {
     
    844837    psFree(where);
    845838
    846     psStringAppend(&query, "\nORDER BY priority DESC, magic_id");
    847 
    848     // treat limit == 0 as "no limit"
    849     if (limit) {
    850         // cut limit in half
    851         // hack to prevent pending leaf nodes from blocking branch nodes
    852         psString limitString = psDBGenerateLimitSQL((limit + 1) / 2);
    853         psStringAppend(&query, " %s", limitString);
    854         psFree(limitString);
    855     }
    856 
    857     if (!p_psDBRunQueryF(config->dbh, query, whereString, whereString)) {
    858         psError(PS_ERR_UNKNOWN, false, "database error");
    859         psFree(whereString);
    860         psFree(query);
    861         return false;
    862     }
    863     psFree(query);
    864 
    865     psArray *output = p_psDBFetchResult(config->dbh);
    866     if (!output) {
    867         psErrorCode err = psErrorCodeLast();
    868         switch (err) {
    869             case PS_ERR_DB_CLIENT:
    870                 psError(PXTOOLS_ERR_SYS, false, "database error");
    871             case PS_ERR_DB_SERVER:
    872                 psError(PXTOOLS_ERR_PROG, false, "database error");
    873             default:
    874                 psError(PXTOOLS_ERR_PROG, false, "unknown error");
    875         }
    876 
    877         return false;
    878     }
    879     if (!psArrayLength(output)) {
    880         psTrace("magictool", PS_LOG_INFO, "no rows found");
    881     }
    882 
    883     if (limit) {
    884         if (psArrayLength(output) >= limit) {
    885             // we've found enough (note that limit was applied to the query so '> limit' won't happen)
    886             // negative simple so the default is true
    887             if (!ippdbPrintMetadatas(stdout, output, "magicMe", !simple)) {
    888                 psError(PS_ERR_UNKNOWN, false, "failed to print array");
    889                 psFree(output);
    890                 return false;
    891             }
    892             psFree(output);
    893             return true;
    894         }
    895     }
    896 
    897     // look for tree nodes that need to be processed
     839
     840    // First look for tree nodes that need to be processed
     841    // These get priority over skycells because they are from runs
     842    // that are already in progress
    898843
    899844    // first find incomplete magicRuns
    900     query = pxDataGet("magictool_toprocess_runs.sql");
     845    psString query = pxDataGet("magictool_toprocess_runs.sql");
    901846    if (!query) {
    902847        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     
    906851    // we limit the query even though it is cheap (only magic_id is selected)
    907852
     853    // XXX: The following comment no longer applies
    908854    // XXX: if the first 1000 unfinished magicRuns have no ready nodes
    909855    // that haven't faulted, later runs won't get returned even though
     
    940886        return false;
    941887    }
    942     if (!psArrayLength(magicRuns)) {
    943         psTrace("magictool", PS_LOG_INFO, "no rows found");
    944         psFree(magicRuns);
    945         return true;
    946     }
    947 
    948     query = pxDataGet("magictool_toprocess_tree.sql");
     888    psArray *output = NULL;
     889    if (psArrayLength(magicRuns)) {
     890        output = psArrayAllocEmpty(100);
     891
     892        query = pxDataGet("magictool_toprocess_tree.sql");
     893        if (!query) {
     894            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     895            return false;
     896        }
     897
     898        for (psS64 index = 0; index < psArrayLength(magicRuns); index++) {
     899            if (limit && (psArrayLength(output) >= limit)) {
     900                break;
     901            }
     902            bool status;
     903            psS64 magic_id = psMetadataLookupS64(&status, magicRuns->data[index], "magic_id");
     904            if (!status) {
     905                psAbort("failed to lookup value for magic_id column");
     906            }
     907
     908            whereString = NULL;
     909            psStringAppend(&whereString, "\nAND (magic_id = %" PRId64 ")", magic_id);
     910            if (!p_psDBRunQueryF(config->dbh, query, whereString )) {
     911                psError(PS_ERR_UNKNOWN, false, "database error");
     912                psFree(whereString);
     913                psFree(query);
     914                return false;
     915            }
     916            psFree(whereString);
     917            psArray *magicTree = p_psDBFetchResult(config->dbh);
     918            if (!magicTree) {
     919                psErrorCode err = psErrorCodeLast();
     920                switch (err) {
     921                    case PS_ERR_DB_CLIENT:
     922                        psError(PXTOOLS_ERR_SYS, false, "database error");
     923                    case PS_ERR_DB_SERVER:
     924                        psError(PXTOOLS_ERR_PROG, false, "database error");
     925                    default:
     926                        psError(PXTOOLS_ERR_PROG, false, "unknown error");
     927                }
     928
     929                return false;
     930            }
     931            psS64 length = psArrayLength(magicTree);
     932            if (!length) {
     933                psTrace("magictool", PS_LOG_INFO, "no rows found for magic_id %" PRId64, magic_id);
     934                psFree(magicTree);
     935                continue;
     936            }
     937
     938            psHash *forest = psHashAlloc(length);
     939
     940            // convert the array of metadata into a pxTree structure
     941            for (long i = 0; i < length; i++) {
     942                bool status;
     943                psString node = psMetadataLookupStr(&status, magicTree->data[i], "node");
     944                if (!status) {
     945                    psAbort("failed to lookup value for node column");
     946                }
     947
     948                psString dep = psMetadataLookupStr(&status, magicTree->data[i], "dep");
     949                if (!status) {
     950                    psAbort("failed to lookup value for dep column");
     951                }
     952
     953                pxTreeBuilder(forest, node, dep, magicTree->data[i]);
     954
     955            }
     956
     957            // find the root of the tree
     958            pxNode *root = psMemIncrRefCounter(psHashLookup(forest, "root"));
     959            psFree(forest);
     960            //    pxTreePrint(stdout, root);
     961
     962            // crawl through the tree and looking for nodes with children that are all
     963            // "done"
     964            pxTreeCrawl(root, findReadyNodes, output);
     965            psFree(root);
     966            psFree(magicTree);
     967        }
     968
     969        int len = psArrayLength(output);
     970        if (len) {
     971            if (limit) {
     972                if (limit < psArrayLength(output)) {
     973                    // truncate the array
     974                    long arrayLength = psArrayLength(output);
     975                    for (long i = arrayLength - 1; i >= limit; i--) {
     976                        psArrayRemoveIndex(output, i);
     977                    }
     978                    // negative simple so the default is true
     979                    if (!ippdbPrintMetadatas(stdout, output, "magicMe", !simple)) {
     980                        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     981                        psFree(output);
     982                        return false;
     983                    }
     984                    psFree(output);
     985                    return true;
     986                } else {
     987                    limit -= len;
     988                }
     989            }
     990        }
     991    }
     992
     993    // look for "inputs" that need to processed
     994    query = pxDataGet("magictool_toprocess_inputs.sql");
    949995    if (!query) {
    950996        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     
    952998    }
    953999
    954     for (psS64 index = 0; index < psArrayLength(magicRuns); index++) {
    955         if (limit && (psArrayLength(output) >= limit)) {
    956             break;
    957         }
    958         bool status;
    959         psS64 magic_id = psMetadataLookupS64(&status, magicRuns->data[index], "magic_id");
    960         if (!status) {
    961             psAbort("failed to lookup value for magic_id column");
    962         }
    963 
    964         whereString = NULL;
    965         psStringAppend(&whereString, "\nAND (magic_id = %" PRId64 ")", magic_id);
    966         if (!p_psDBRunQueryF(config->dbh, query, whereString )) {
    967             psError(PS_ERR_UNKNOWN, false, "database error");
    968             psFree(whereString);
    969             psFree(query);
    970             return false;
    971         }
     1000    psStringAppend(&query, "\nORDER BY priority DESC, magic_id");
     1001
     1002    // treat limit == 0 as "no limit"
     1003    if (limit) {
     1004        psString limitString = psDBGenerateLimitSQL(limit);
     1005        psStringAppend(&query, " %s", limitString);
     1006        psFree(limitString);
     1007    }
     1008
     1009    if (!p_psDBRunQueryF(config->dbh, query, whereString, whereString)) {
     1010        psError(PS_ERR_UNKNOWN, false, "database error");
    9721011        psFree(whereString);
    973         psArray *magicTree = p_psDBFetchResult(config->dbh);
    974         if (!magicTree) {
    975             psErrorCode err = psErrorCodeLast();
    976             switch (err) {
    977                 case PS_ERR_DB_CLIENT:
    978                     psError(PXTOOLS_ERR_SYS, false, "database error");
    979                 case PS_ERR_DB_SERVER:
    980                     psError(PXTOOLS_ERR_PROG, false, "database error");
    981                 default:
    982                     psError(PXTOOLS_ERR_PROG, false, "unknown error");
    983             }
    984 
    985             return false;
    986         }
    987         psS64 length = psArrayLength(magicTree);
    988         if (!length) {
    989             psTrace("magictool", PS_LOG_INFO, "no rows found for magic_id %" PRId64, magic_id);
    990             psFree(magicTree);
    991             continue;
    992         }
    993 
    994         psHash *forest = psHashAlloc(length);
    995 
    996         // convert the array of metadata into a pxTree structure
    997         for (long i = 0; i < length; i++) {
    998             bool status;
    999             psString node = psMetadataLookupStr(&status, magicTree->data[i], "node");
    1000             if (!status) {
    1001                 psAbort("failed to lookup value for node column");
    1002             }
    1003 
    1004             psString dep = psMetadataLookupStr(&status, magicTree->data[i], "dep");
    1005             if (!status) {
    1006                 psAbort("failed to lookup value for dep column");
    1007             }
    1008 
    1009             pxTreeBuilder(forest, node, dep, magicTree->data[i]);
    1010 
    1011         }
    1012 
    1013         // find the root of the tree
    1014         pxNode *root = psMemIncrRefCounter(psHashLookup(forest, "root"));
    1015         psFree(forest);
    1016         //    pxTreePrint(stdout, root);
    1017 
    1018         // crawl through the tree and looking for nodes with children that are all
    1019         // "done"
    1020         pxTreeCrawl(root, findReadyNodes, output);
    1021         psFree(root);
    1022         psFree(magicTree);
    1023     }
    1024 
     1012        psFree(query);
     1013        return false;
     1014    }
     1015    psFree(query);
     1016
     1017    psArray *skycellOutput = p_psDBFetchResult(config->dbh);
     1018    if (!skycellOutput) {
     1019        psErrorCode err = psErrorCodeLast();
     1020        switch (err) {
     1021            case PS_ERR_DB_CLIENT:
     1022                psError(PXTOOLS_ERR_SYS, false, "database error");
     1023            case PS_ERR_DB_SERVER:
     1024                psError(PXTOOLS_ERR_PROG, false, "database error");
     1025            default:
     1026                psError(PXTOOLS_ERR_PROG, false, "unknown error");
     1027        }
     1028
     1029        return false;
     1030    }
     1031    // merge the arrays so that we can print them all at once
     1032    if (psArrayLength(skycellOutput)) {
     1033        int len = psArrayLength(skycellOutput);
     1034        for (int i=0; i < len; i++) {
     1035            psArrayAdd(output, 0, skycellOutput->data[i]);
     1036        }
     1037    }
     1038    psFree(skycellOutput);
    10251039    if (psArrayLength(output)) {
    1026         if (limit && (limit < psArrayLength(output))) {
    1027             // truncate the array
    1028             long arrayLength = psArrayLength(output);
    1029             for (long i = arrayLength - 1; i >= limit; i--) {
    1030                 psArrayRemoveIndex(output, i);
    1031             }
    1032         }
    1033         // negative simple so the default is true
    10341040        if (!ippdbPrintMetadatas(stdout, output, "magicMe", !simple)) {
    10351041            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     
    10371043            return false;
    10381044        }
    1039     }
    1040 
     1045    } else {
     1046        psTrace("magictool", PS_LOG_INFO, "no rows found");
     1047    }
    10411048    psFree(output);
    1042 
    10431049    return true;
    10441050}
Note: See TracChangeset for help on using the changeset viewer.