示例#1
0
 public TPCCNewOrderStoredProcedure(string sessionId, NewOrderInParameters inParams)
 {
     this.pid          = int.Parse(sessionId);
     this.sessionId    = sessionId;
     this.input        = inParams;
     this.currentState = NewOrderState.ToStart;
     this.errMsg       = "";
 }
        public void LoadNewOrderWorkload(string filepath)
        {
            Console.WriteLine("Loading New-Order workload");
            var    csvReader     = new System.IO.StreamReader(filepath);
            int    workloadTotal = workerCount * workloadCountPerWorker;
            string line          = null;
            int    lineNum       = 0;

            line = csvReader.ReadLine();    // ignore the first line

            int instanceIndex = 0;

            for (int i = 0; i < workerCount; i++)
            {
                Queue <TransactionRequest> reqQueue = new Queue <TransactionRequest>();

                for (int k = 0; k < workloadCountPerWorker; k++)
                {
                    line = csvReader.ReadLine();
                    lineNum++;
                    if (line == null)
                    {
                        throw new Exception("there is no enough workload");
                    }

                    string[] columns = line.Split(new string[] { Constants.WorkloadDelimiter }, StringSplitOptions.None);
                    for (int j = 0; j < columns.Length; j++)
                    {
                        columns[j] = columns[j].Substring(1);
                    }
                    columns[columns.Length - 1] = columns[columns.Length - 1].Substring(0, columns[columns.Length - 1].Length - 1); // remove `"`

                    var no = new NewOrderInParameters
                    {
                        timestamp       = columns[0],
                        W_ID            = Convert.ToUInt32(columns[5]),
                        D_ID            = Convert.ToUInt32(columns[3]),
                        C_ID            = Convert.ToUInt32(columns[1]),
                        OL_I_IDs        = JsonConvert.DeserializeObject <uint[]>(columns[6]),
                        OL_SUPPLY_W_IDs = JsonConvert.DeserializeObject <uint[]>(columns[4]),
                        OL_QUANTITYs    = JsonConvert.DeserializeObject <uint[]>(columns[2]),
                        O_ENTRY_D       = columns[7]
                    };

                    TPCCNewOrderStoredProcedure nosp = new TPCCNewOrderStoredProcedure(lineNum.ToString(), no);
                    // TODO: should adopt to the lastest storedprocedure rule
                    //TransactionRequest req = new TransactionRequest(lineNum.ToString(), nosp);
                    TransactionRequest req = null;
                    reqQueue.Enqueue(req);
                }

                List <Tuple <string, int> > executorInstances = instanceIndex >= this.partitionedInstances.Count ? null :
                                                                this.partitionedInstances[instanceIndex++];
                this.executorList.Add(new TransactionExecutor(this.redisVersionDb, null, reqQueue, i, i));
            }
        }
示例#3
0
        object ExecuteStoredProcedure(SyncExecution exec, WorkloadParam param)
        {
            NewOrderInParameters input = (NewOrderInParameters)param;

            // Transaction tx = new Transaction(null, vdb);

            // check input
            if (input.OL_I_IDs.Length <= 0 ||
                input.OL_I_IDs.Length != input.OL_QUANTITYs.Length ||
                input.OL_I_IDs.Length != input.OL_SUPPLY_W_IDs.Length)
            {
                throw new Exception("invalid input");
            }

            // read Warehouse,District, Customer
            this.wpk.Set(W_ID: input.W_ID);
            // WarehousePayload wpl = JsonConvert.DeserializeObject<WarehousePayload>((string)tx.Read(Constants.DefaultTbl, wpk.ToString()));
            WarehousePayload wpl;

            if (exec.ReadCopy(this.wpk, out wpl).IsAborted())
            {
                return(null);
            }
            double W_TAX = wpl.W_TAX;

            this.dpk.Set(D_ID: input.D_ID, D_W_ID: input.W_ID);
            // DistrictPayload dpl = JsonConvert.DeserializeObject<DistrictPayload>((string)tx.Read(Constants.DefaultTbl, dpk.ToString()));
            DistrictPayload dpl;

            if (exec.Read(this.dpk, out dpl).IsAborted())
            {
                return(null);
            }
            double D_TAX       = dpl.D_TAX;
            uint   D_NEXT_O_ID = dpl.D_NEXT_O_ID;

            dpl.D_NEXT_O_ID = D_NEXT_O_ID + 1;
            // tx.Update(Constants.DefaultTbl, dpk.ToString(), JsonConvert.SerializeObject(dpl));
            if (exec.Update(dpk, dpl).IsAborted())
            {
                return(null);
            }

            this.cpk.Set(C_ID: input.C_ID, C_D_ID: input.D_ID, C_W_ID: input.W_ID);
            // CustomerPayload cpl = JsonConvert.DeserializeObject<CustomerPayload>((string)tx.Read(Constants.DefaultTbl, cpk.ToString()));
            CustomerPayload cpl;

            if (exec.Read(this.cpk, out cpl).IsAborted())
            {
                return(null);
            }
            double C_DISCOUNT = cpl.C_DISCOUNT;

            // all local or not
            bool allLocal = true;

            for (int i = 0; i < input.OL_I_IDs.Length; i++)
            {
                allLocal = allLocal & input.OL_I_IDs[i] == input.W_ID;
            }

            /*
             * // insert order/new-order, update next-order-id
             * OrderPkey opk = new OrderPkey
             * {
             *  O_ID = D_NEXT_O_ID,
             *  O_D_ID = input.D_ID,
             *  O_W_ID = input.W_ID
             * };
             * OrderPayload opl = new OrderPayload
             * {
             *  O_C_ID = input.C_ID,
             *  O_ENTRY_D = input.O_ENTRY_D,
             *  O_CARRIER_ID = Constants.NullCarrierID,
             *  O_OL_CNT = (uint)input.OL_I_IDs.Length,
             *  O_ALL_LOCAL = Convert.ToUInt32(allLocal)
             * };
             * // tx.ReadAndInitialize(Constants.DefaultTbl, opk.ToString());
             * // tx.Insert(Constants.DefaultTbl, opk.ToString(), JsonConvert.SerializeObject(opl));
             * if (false && exec.Insert(opk, opl).IsAborted())
             * {
             *  return null;
             * }
             */
            /*
             * NewOrderPkey nopk = new NewOrderPkey
             * {
             *  NO_O_ID = D_NEXT_O_ID,
             *  NO_D_ID = input.D_ID,
             *  NO_W_ID = input.W_ID
             * };
             * // tx.ReadAndInitialize(Constants.DefaultTbl, nopk.ToString());
             * // tx.Insert(Constants.DefaultTbl, nopk.ToString(), Constants.PlaceHolder);
             * if (false && exec.Insert(nopk, NewOrderPayload.Placeholder()).IsAborted())
             * {
             *  return null;
             * }
             */

            // whether all items exist without a wrong item number
            ipks.Clear();
            for (int i = 0; i < input.OL_I_IDs.Length; i++)
            {
                ItemPkey ipk = ipks.AllocateNew();
                ipk.Set(I_ID: input.OL_I_IDs[i]);
                // var str = (string)tx.Read(Constants.DefaultTbl, ipk.ToString());
                // items[i] = JsonConvert.DeserializeObject<ItemPayload>(str);
                TpccTablePayload payload;
                if (exec.Read(ipk, out payload).IsAborted())
                {
                    return(null);
                }
                this.items[i] = payload as ItemPayload;
            }

            // insert order lines
            this.spks.Clear();
            this.noOutput.itemOutputs.Clear();
            var total = 0.0;

            for (int i = 0; i < input.OL_I_IDs.Length; i++)
            {
                var OL_NUMBER      = (uint)i + 1;
                var OL_SUPPLY_W_ID = input.OL_SUPPLY_W_IDs[i];
                var OL_I_ID        = input.OL_I_IDs[i];
                var OL_QUANTITY    = input.OL_QUANTITYs[i];

                var I_NAME  = this.items[i].I_NAME;
                var I_DATA  = this.items[i].I_DATA;
                var I_PRICE = this.items[i].I_PRICE;

                // read & update stock info
                StockPkey spk = this.spks.AllocateNew();
                spk.Set(S_I_ID: OL_I_ID, S_W_ID: OL_SUPPLY_W_ID);
                // StockPayload spl = JsonConvert.DeserializeObject<StockPayload>((string)tx.Read(Constants.DefaultTbl, spk.ToString()));
                StockPayload spl;
                if (exec.ReadCopy(spk, out spl).IsAborted())
                {
                    return(null);
                }
                spl.S_YTD += OL_QUANTITY;
                if (spl.S_QUANTITY >= OL_QUANTITY + 10)
                {
                    spl.S_QUANTITY -= (int)OL_QUANTITY;
                }
                else
                {
                    spl.S_QUANTITY += 91 - (int)OL_QUANTITY;
                }
                spl.S_ORDER_CNT += 1;
                if (input.OL_SUPPLY_W_IDs[i] != input.W_ID)
                {
                    spl.S_REMOTE_CNT += 1;
                }
                // tx.Update(Constants.DefaultTbl, spk.ToString(), JsonConvert.SerializeObject(spl));
                if (exec.Update(spk, spl).IsAborted())
                {
                    return(null);
                }

                var OL_AMOUNT = OL_QUANTITY * I_PRICE;
                total += OL_AMOUNT;

                /*
                 * // insert order line
                 * OrderLinePkey olpk = new OrderLinePkey
                 * {
                 *  OL_O_ID = D_NEXT_O_ID,
                 *  OL_D_ID = input.D_ID,
                 *  OL_W_ID = input.W_ID,
                 *  OL_NUMBER = OL_NUMBER
                 * };
                 *
                 * OrderLinePayload olpl = new OrderLinePayload
                 * {
                 *  OL_I_ID = OL_I_ID,
                 *  OL_SUPPLY_W_ID = OL_SUPPLY_W_ID,
                 *  OL_DELIVERY_D = null,
                 *  OL_QUANTITY = OL_QUANTITY,
                 *  OL_AMOUNT = OL_AMOUNT,
                 *  OL_DIST_INFO = spl.S_DIST_01        // TODO, assign to S_DIST_XX, where XX equals to D_ID
                 * };
                 * // tx.ReadAndInitialize(Constants.DefaultTbl, olpk.ToString());
                 * // tx.Insert(Constants.DefaultTbl, olpk.ToString(), JsonConvert.SerializeObject(olpl));
                 * if (false && exec.Insert(olpk, olpl).IsAborted())
                 * {
                 *  return null;
                 * }
                 *
                 * // add to return
                 * var brand = (I_DATA.Contains("ORIGINAL") && spl.S_DATA.Contains("ORIGINAL")) ? 'B' : 'G';
                 * this.noOutput.itemOutputs.AllocateNew().Set(
                 *  this.items[i].I_NAME, brand,
                 *  this.items[i].I_PRICE, olpl.OL_AMOUNT);
                 */
            }

            // tx.Commit();
            if (exec.Commit().IsAborted())
            {
                return(null);
            }

            // to return
            total *= (1 - C_DISCOUNT) * (1 + W_TAX + D_TAX);
            this.noOutput.Set(
                wpk.W_ID, cpl.C_LAST, cpl.C_CREDIT, cpl.C_DISCOUNT,
                wpl.W_TAX, dpl.D_TAX, (uint)input.OL_I_IDs.Length, D_NEXT_O_ID,
                input.O_ENTRY_D, total);

            return(this.noOutput);
        }