github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/workload/ledger/generate.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package ledger 12 13 import ( 14 "encoding/binary" 15 "hash" 16 "math" 17 "math/rand" 18 "strconv" 19 20 "github.com/cockroachdb/cockroach/pkg/sql/types" 21 "github.com/cockroachdb/cockroach/pkg/util/uint128" 22 "github.com/cockroachdb/cockroach/pkg/util/uuid" 23 ) 24 25 const ( 26 numTxnsPerCustomer = 2 27 numEntriesPerTxn = 4 28 numEntriesPerCustomer = numTxnsPerCustomer * numEntriesPerTxn 29 30 paymentIDPrefix = "payment:" 31 txnTypeReference = 400 32 cashMoneyType = "C" 33 ) 34 35 var ledgerCustomerTypes = []*types.T{ 36 types.Int, 37 types.Bytes, 38 types.Bytes, 39 types.Bytes, 40 types.Bool, 41 types.Bool, 42 types.Bytes, 43 types.Int, 44 types.Int, 45 types.Int, 46 } 47 48 func (w *ledger) ledgerCustomerInitialRow(rowIdx int) []interface{} { 49 rng := w.rngPool.Get().(*rand.Rand) 50 defer w.rngPool.Put(rng) 51 rng.Seed(w.seed + int64(rowIdx)) 52 53 return []interface{}{ 54 rowIdx, // id 55 strconv.Itoa(rowIdx), // identifier 56 nil, // name 57 randCurrencyCode(rng), // currency_code 58 true, // is_system_customer 59 true, // is_active 60 randTimestamp(rng), // created 61 0, // balance 62 nil, // credit_limit 63 -1, // sequence 64 } 65 } 66 67 func (w *ledger) ledgerCustomerSplitRow(splitIdx int) []interface{} { 68 return []interface{}{ 69 (splitIdx + 1) * (w.customers / w.splits), 70 } 71 } 72 73 var ledgerTransactionColTypes = []*types.T{ 74 types.Bytes, 75 types.Bytes, 76 types.Bytes, 77 types.Int, 78 types.Bytes, 79 types.Bytes, 80 types.Bytes, 81 types.Bytes, 82 types.Bytes, 83 } 84 85 func (w *ledger) ledgerTransactionInitialRow(rowIdx int) []interface{} { 86 rng := w.rngPool.Get().(*rand.Rand) 87 defer w.rngPool.Put(rng) 88 rng.Seed(w.seed + int64(rowIdx)) 89 90 h := w.hashPool.Get().(hash.Hash64) 91 defer w.hashPool.Put(h) 92 defer h.Reset() 93 94 return []interface{}{ 95 w.ledgerStablePaymentID(rowIdx), // external_id 96 nil, // tcomment 97 randContext(rng), // context 98 txnTypeReference, // transaction_type_reference 99 randUsername(rng), // username 100 randTimestamp(rng), // created_ts 101 randTimestamp(rng), // systimestamp 102 nil, // reversed_by 103 randResponse(rng), // response 104 } 105 } 106 107 func (w *ledger) ledgerTransactionSplitRow(splitIdx int) []interface{} { 108 rng := rand.New(rand.NewSource(w.seed + int64(splitIdx))) 109 u := uuid.FromUint128(uint128.FromInts(rng.Uint64(), rng.Uint64())) 110 return []interface{}{ 111 paymentIDPrefix + u.String(), 112 } 113 } 114 115 func (w *ledger) ledgerEntryInitialRow(rowIdx int) []interface{} { 116 rng := w.rngPool.Get().(*rand.Rand) 117 defer w.rngPool.Put(rng) 118 rng.Seed(w.seed + int64(rowIdx)) 119 120 // Alternate. 121 debit := rowIdx%2 == 0 122 123 var amount float64 124 if debit { 125 amount = -float64(rowIdx) / 100 126 } else { 127 amount = float64(rowIdx-1) / 100 128 } 129 130 systemAmount := 88.122259 131 if debit { 132 systemAmount *= -1 133 } 134 135 cRowIdx := rowIdx / numEntriesPerCustomer 136 137 tRowIdx := rowIdx / numEntriesPerTxn 138 tID := w.ledgerStablePaymentID(tRowIdx) 139 140 return []interface{}{ 141 rng.Int(), // id 142 amount, // amount 143 cRowIdx, // customer_id 144 tID, // transaction_id 145 systemAmount, // system_amount 146 randTimestamp(rng), // created_ts 147 cashMoneyType, // money_type 148 } 149 } 150 151 func (w *ledger) ledgerEntrySplitRow(splitIdx int) []interface{} { 152 return []interface{}{ 153 (splitIdx + 1) * (int(math.MaxInt64) / w.splits), 154 } 155 } 156 157 func (w *ledger) ledgerSessionInitialRow(rowIdx int) []interface{} { 158 rng := w.rngPool.Get().(*rand.Rand) 159 defer w.rngPool.Put(rng) 160 rng.Seed(w.seed + int64(rowIdx)) 161 162 return []interface{}{ 163 randSessionID(rng), // session_id 164 randTimestamp(rng), // expiry_timestamp 165 randSessionData(rng), // data 166 randTimestamp(rng), // last_update 167 } 168 } 169 170 func (w *ledger) ledgerSessionSplitRow(splitIdx int) []interface{} { 171 rng := rand.New(rand.NewSource(w.seed + int64(splitIdx))) 172 return []interface{}{ 173 randSessionID(rng), 174 } 175 } 176 177 func (w *ledger) ledgerStablePaymentID(tRowIdx int) string { 178 h := w.hashPool.Get().(hash.Hash64) 179 defer w.hashPool.Put(h) 180 defer h.Reset() 181 182 b := make([]byte, 8) 183 binary.LittleEndian.PutUint64(b, uint64(tRowIdx)) 184 if _, err := h.Write(b); err != nil { 185 panic(err) 186 } 187 hi := h.Sum64() 188 if _, err := h.Write(b); err != nil { 189 panic(err) 190 } 191 low := h.Sum64() 192 193 u := uuid.FromUint128(uint128.FromInts(hi, low)) 194 return paymentIDPrefix + u.String() 195 }