Search in sources :

Example 6 with VoltTable

use of org.voltdb.VoltTable in project voltdb by VoltDB.

the class BeginCall method run.

/**
     * Procedure main logic.
     *
     * @param uuid Column value for tuple insertion and partitioning key for this procedure.
     * @param val Column value for tuple insertion.
     * @param update_ts Column value for tuple insertion.
     * @param newestToDiscard Try to remove any tuples as old or older than this value.
     * @param targetMaxRowsToDelete The upper limit on the number of rows to delete per transaction.
     * @return The number of deleted rows.
     * @throws VoltAbortException on bad input.
    */
public long run(int agent_id, String phone_no, long call_id, TimestampType start_ts) {
    voltQueueSQL(findOpenCall, EXPECT_ZERO_OR_ONE_ROW, call_id, agent_id, phone_no);
    voltQueueSQL(findCompletedCall, EXPECT_ZERO_OR_ONE_ROW, call_id, agent_id, phone_no);
    VoltTable[] results = voltExecuteSQL();
    boolean completedCall = results[1].getRowCount() > 0;
    if (completedCall) {
        return -1;
    }
    VoltTable openRowTable = results[0];
    if (openRowTable.getRowCount() > 0) {
        VoltTableRow existingCall = openRowTable.fetchRow(0);
        // check if this is the second begin we've seen for this open call
        existingCall.getTimestampAsTimestamp("start_ts");
        if (existingCall.wasNull() == false) {
            return -1;
        }
        // check if this completes the call
        TimestampType end_ts = existingCall.getTimestampAsTimestamp("end_ts");
        if (existingCall.wasNull() == false) {
            int durationms = (int) ((end_ts.getTime() - start_ts.getTime()) / 1000);
            // update per-day running stddev calculation
            computeRunningStdDev(agent_id, end_ts, durationms);
            // completes the call
            voltQueueSQL(deleteOpenCall, EXPECT_SCALAR_MATCH(1), call_id, agent_id, phone_no);
            voltQueueSQL(insertCompletedCall, EXPECT_SCALAR_MATCH(1), call_id, agent_id, phone_no, start_ts, end_ts, durationms);
            voltExecuteSQL(true);
            return 0;
        }
    }
    voltQueueSQL(upsertOpenCall, EXPECT_SCALAR_MATCH(1), call_id, agent_id, phone_no, start_ts);
    voltExecuteSQL(true);
    return 0;
}
Also used : TimestampType(org.voltdb.types.TimestampType) VoltTable(org.voltdb.VoltTable) VoltTableRow(org.voltdb.VoltTableRow)

Example 7 with VoltTable

use of org.voltdb.VoltTable in project voltdb by VoltDB.

the class BeginOrEndCallBase method computeRunningStdDev.

void computeRunningStdDev(int agent_id, TimestampType end_ts, long durationms) {
    voltQueueSQL(findTodaysStddevStatsForAgent, EXPECT_ZERO_OR_ONE_ROW, end_ts, agent_id);
    VoltTable stddevTable = voltExecuteSQL()[0];
    long nprev = 0, n = 0;
    long sumprev = 0, sum = 0;
    double qprev = 0, q = 0;
    double avgprev = 0, avg = 0;
    double stddev = 0;
    if (stddevTable.getRowCount() == 1) {
        VoltTableRow stddevRow = stddevTable.fetchRow(0);
        nprev = stddevRow.getLong("n");
        sumprev = stddevRow.getLong("sumk");
        qprev = stddevRow.getDouble("qk");
    }
    n = nprev + 1;
    sum = sumprev + durationms;
    avgprev = nprev > 0 ? (sumprev / (double) nprev) : 0;
    avg = sum / (double) n;
    q = qprev + (durationms - avgprev) * (durationms - avg);
    stddev = Math.sqrt(q / n);
    // really basic validity checks that the math hasn't corrupted something
    if (!Double.isFinite(q)) {
        throw new VoltAbortException("q is not finite");
    }
    if (!Double.isFinite(avg)) {
        throw new VoltAbortException("avg is not finite");
    }
    if (!Double.isFinite(stddev)) {
        throw new VoltAbortException("stddev is not finite");
    }
    voltQueueSQL(upsertTodaysStddevStatsForAgent, EXPECT_SCALAR_MATCH(1), agent_id, end_ts, n, sum, q, stddev);
}
Also used : VoltTable(org.voltdb.VoltTable) VoltTableRow(org.voltdb.VoltTableRow)

Example 8 with VoltTable

use of org.voltdb.VoltTable in project voltdb by VoltDB.

the class TrackEvent method run.

public long run(TimestampType utc_time, long ip_address, long cookie_uid, int creative_id, int inventory_id, int type_id, BigDecimal cost) throws VoltAbortException {
    // derive counter fields from type_id
    //   0 = impression
    //   1 = clickthrough
    //   2 = conversion
    int is_impression = (type_id == 0) ? 1 : 0;
    int is_clickthrough = (type_id == 1) ? 1 : 0;
    int is_conversion = (type_id == 2) ? 1 : 0;
    // lookup creative_id and inventory_id
    voltQueueSQL(selectCreative, creative_id);
    voltQueueSQL(selectInventory, inventory_id);
    VoltTable[] lookups = voltExecuteSQL();
    VoltTableRow creative = lookups[0].fetchRow(0);
    int campaign_id = (int) creative.getLong(0);
    int advertiser_id = (int) creative.getLong(1);
    VoltTableRow inventory = lookups[1].fetchRow(0);
    int site_id = (int) inventory.getLong(0);
    int page_id = (int) inventory.getLong(1);
    voltQueueSQL(insertEvent, utc_time, ip_address, cookie_uid, creative_id, inventory_id, type_id, cost, campaign_id, advertiser_id, site_id, page_id, is_impression, is_clickthrough, is_conversion);
    voltExecuteSQL();
    return ClientResponse.SUCCESS;
}
Also used : VoltTable(org.voltdb.VoltTable) VoltTableRow(org.voltdb.VoltTableRow)

Example 9 with VoltTable

use of org.voltdb.VoltTable in project voltdb by VoltDB.

the class AuctionStatus method run.

/**
     * Get a tuple of useful status info about an auction.
     *
     * @param itemId The id of the item we'd like to know about.
     * @return A table with one row and fields:
     *  (item, bidder, seller, bidcount, price, endtime, status)
     * @throws VoltAbortException Currently doesn't abort.
     */
public VoltTable[] run(int itemId) throws VoltAbortException {
    // create a new VoltTable to store our results
    VoltTable retval = new VoltTable(new VoltTable.ColumnInfo("item", VoltType.STRING), new VoltTable.ColumnInfo("bidder", VoltType.STRING), new VoltTable.ColumnInfo("seller", VoltType.STRING), new VoltTable.ColumnInfo("bidcount", VoltType.BIGINT), new VoltTable.ColumnInfo("price", VoltType.FLOAT), new VoltTable.ColumnInfo("endtime", VoltType.TIMESTAMP), new VoltTable.ColumnInfo("status", VoltType.STRING));
    // get the seller id and item name from ITEM table
    voltQueueSQL(getItemInfo, itemId);
    VoltTable itemTable = voltExecuteSQL()[0];
    VoltTableRow itemRow = itemTable.fetchRow(0);
    // resulting info:
    long sellerId = itemRow.getLong("SELLERID");
    String itemName = itemRow.getString("ITEMNAME");
    long endTime = itemRow.getTimestampAsLong("ENDTIME");
    long highBidId = itemRow.getLong("HIGHBIDID");
    // get high bid info
    voltQueueSQL(getBidInfo, highBidId);
    VoltTable statusTable = voltExecuteSQL()[0];
    VoltTableRow row = statusTable.fetchRow(0);
    // resulting info:
    long bidderId = row.getLong("BIDDERID");
    double bidPrice = row.getDouble("BIDPRICE");
    // count the number of bids on the auction
    voltQueueSQL(getBidCount, itemId);
    VoltTable bidCountTable = voltExecuteSQL()[0];
    VoltTableRow bidCountRow = bidCountTable.fetchRow(0);
    // resulting info:
    // the minus one is for the fake initial bid
    long bidCount = bidCountRow.getLong(0) - 1;
    // get the names of the bidder and seller
    voltQueueSQL(getUserName, sellerId);
    if (bidderId >= 0) {
        voltQueueSQL(getUserName, bidderId);
    }
    VoltTable[] nameTables = voltExecuteSQL();
    VoltTableRow sellerNameRow = nameTables[0].fetchRow(0);
    // we should always have a seller name
    String sellerName = sellerNameRow.getString("FIRSTNAME") + " " + sellerNameRow.getString("LASTNAME");
    // we might not always have a bidder name, so need this if statement
    String bidderName = "NO BIDDER";
    if (bidderId >= 0) {
        VoltTableRow bidderNameRow = nameTables[1].fetchRow(0);
        bidderName = bidderNameRow.getString("FIRSTNAME") + " " + bidderNameRow.getString("LASTNAME");
    }
    // check the timing and set the auction status accordingly
    String status = "OPEN";
    long now = new TimestampType().getTime();
    if (endTime < now)
        status = "CLOSED";
    // add the single tuple to our return table
    retval.addRow(itemName, bidderName, sellerName, bidCount, bidPrice, endTime, status);
    return new VoltTable[] { retval };
}
Also used : TimestampType(org.voltdb.types.TimestampType) VoltTable(org.voltdb.VoltTable) VoltTableRow(org.voltdb.VoltTableRow)

Example 10 with VoltTable

use of org.voltdb.VoltTable in project voltdb by VoltDB.

the class Site method statsTick.

/**
     * Cache the current statistics.
     *
     * @param time
     */
private void statsTick(long time) {
    /*
         * grab the table statistics from ee and put it into the statistics
         * agent.
         */
    if (m_tableStats != null) {
        CatalogMap<Table> tables = m_context.database.getTables();
        int[] tableIds = new int[tables.size()];
        int i = 0;
        for (Table table : tables) {
            tableIds[i++] = table.getRelativeIndex();
        }
        // data to aggregate
        long tupleCount = 0;
        long tupleDataMem = 0;
        long tupleAllocatedMem = 0;
        long indexMem = 0;
        long stringMem = 0;
        // update table stats
        final VoltTable[] s1 = m_ee.getStats(StatsSelector.TABLE, tableIds, false, time);
        if ((s1 != null) && (s1.length > 0)) {
            VoltTable stats = s1[0];
            assert (stats != null);
            // rollup the table memory stats for this site
            while (stats.advanceRow()) {
                //Assert column index matches name for ENG-4092
                assert (stats.getColumnName(7).equals("TUPLE_COUNT"));
                assert (stats.getColumnName(6).equals("TABLE_TYPE"));
                if ("PersistentTable".equals(stats.getString(6))) {
                    tupleCount += stats.getLong(7);
                }
                assert (stats.getColumnName(8).equals("TUPLE_ALLOCATED_MEMORY"));
                tupleAllocatedMem += stats.getLong(8);
                assert (stats.getColumnName(9).equals("TUPLE_DATA_MEMORY"));
                tupleDataMem += stats.getLong(9);
                assert (stats.getColumnName(10).equals("STRING_DATA_MEMORY"));
                stringMem += stats.getLong(10);
            }
            stats.resetRowPosition();
            m_tableStats.setStatsTable(stats);
        } else {
            // the EE returned no table stats, which means there are no tables.
            // Need to ensure the cached stats are cleared to reflect that
            m_tableStats.resetStatsTable();
        }
        // update index stats
        final VoltTable[] s2 = m_ee.getStats(StatsSelector.INDEX, tableIds, false, time);
        if ((s2 != null) && (s2.length > 0)) {
            VoltTable stats = s2[0];
            assert (stats != null);
            // rollup the index memory stats for this site
            while (stats.advanceRow()) {
                //Assert column index matches name for ENG-4092
                assert (stats.getColumnName(11).equals("MEMORY_ESTIMATE"));
                indexMem += stats.getLong(11);
            }
            stats.resetRowPosition();
            m_indexStats.setStatsTable(stats);
        } else {
            // the EE returned no index stats, which means there are no indexes.
            // Need to ensure the cached stats are cleared to reflect that
            m_indexStats.resetStatsTable();
        }
        // update the rolled up memory statistics
        if (m_memStats != null) {
            m_memStats.eeUpdateMemStats(m_siteId, tupleCount, tupleDataMem, tupleAllocatedMem, indexMem, stringMem, m_ee.getThreadLocalPoolAllocations());
        }
    }
}
Also used : VoltTable(org.voltdb.VoltTable) Table(org.voltdb.catalog.Table) VoltTable(org.voltdb.VoltTable)

Aggregations

VoltTable (org.voltdb.VoltTable)887 Client (org.voltdb.client.Client)497 ClientResponse (org.voltdb.client.ClientResponse)193 ProcCallException (org.voltdb.client.ProcCallException)144 IOException (java.io.IOException)100 VoltTableRow (org.voltdb.VoltTableRow)57 NoConnectionsException (org.voltdb.client.NoConnectionsException)52 ColumnInfo (org.voltdb.VoltTable.ColumnInfo)42 TimestampType (org.voltdb.types.TimestampType)37 BigDecimal (java.math.BigDecimal)30 ArrayList (java.util.ArrayList)27 Test (org.junit.Test)26 File (java.io.File)25 HashMap (java.util.HashMap)21 ClientResponseImpl (org.voltdb.ClientResponseImpl)20 Timestamp (java.sql.Timestamp)15 Date (java.util.Date)15 VoltDB (org.voltdb.VoltDB)15 DependencyPair (org.voltdb.DependencyPair)14 Configuration (org.voltdb.VoltDB.Configuration)14