Skip to content

Commit ae7e7e1

Browse files
committed
Truncate ledger debug trace for large values
Some RWSet values are large, for example default chaincode is about 5MB. Having 5MB of binary data dumped to debug is not helpful. This change will truncate the RWSets in the debug trace after 2000 characters. Change-Id: I66de491edb396fc0b8bc95628a1e975151ea7904 Signed-off-by: denyeart <[email protected]>
1 parent 35efa2b commit ae7e7e1

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

core/ledger/kvledger/kv_ledger.go

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ type KVLedger struct {
6262

6363
// NewKVLedger constructs new `KVLedger`
6464
func NewKVLedger(conf *Conf) (*KVLedger, error) {
65+
66+
logger.Debugf("Creating KVLedger using config: ", conf)
67+
6568
attrsToIndex := []blkstorage.IndexableAttr{
6669
blkstorage.IndexableAttrBlockHash,
6770
blkstorage.IndexableAttrBlockNum,
@@ -153,9 +156,13 @@ func (l *KVLedger) Commit() error {
153156
if l.pendingBlockToCommit == nil {
154157
panic(fmt.Errorf(`Nothing to commit. RemoveInvalidTransactionsAndPrepare() method should have been called and should not have thrown error`))
155158
}
159+
160+
logger.Debugf("Committing block to storage")
156161
if err := l.blockStore.AddBlock(l.pendingBlockToCommit); err != nil {
157162
return err
158163
}
164+
165+
logger.Debugf("Committing block to state database")
159166
if err := l.txtmgmt.Commit(); err != nil {
160167
panic(fmt.Errorf(`Error during commit to txmgr:%s`, err))
161168
}

core/ledger/kvledger/txmgmt/lockbasedtxmgmt/lockbased_tx_simulator.go

+39-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"reflect"
2222

2323
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt"
24+
logging "github.com/op/go-logging"
2425
)
2526

2627
type kvReadCache struct {
@@ -61,19 +62,42 @@ func (s *LockBasedTxSimulator) GetState(ns string, key string) ([]byte, error) {
6162
// check if it was written
6263
kvWrite, ok := nsRWs.writeMap[key]
6364
if ok {
64-
logger.Debugf("Returing value for key=[%s:%s] from write set", ns, key, kvWrite.Value)
65+
// trace the first 500 bytes of value only, in case it is huge
66+
if logger.IsEnabledFor(logging.DEBUG) {
67+
if len(kvWrite.Value) < 500 {
68+
logger.Debugf("Returing value for key=[%s:%s] from write set", ns, key, kvWrite.Value)
69+
} else {
70+
logger.Debugf("Returing value for key=[%s:%s] from write set", ns, key, kvWrite.Value[0:500])
71+
}
72+
}
6573
return kvWrite.Value, nil
6674
}
6775
// check if it was read
6876
readCache, ok := nsRWs.readMap[key]
6977
if ok {
70-
logger.Debugf("Returing value for key=[%s:%s] from read set", ns, key, readCache.cachedValue)
78+
// trace the first 500 bytes of value only, in case it is huge
79+
if logger.IsEnabledFor(logging.DEBUG) {
80+
if len(readCache.cachedValue) < 500 {
81+
logger.Debugf("Returing value for key=[%s:%s] from read set", ns, key, readCache.cachedValue)
82+
} else {
83+
logger.Debugf("Returing value for key=[%s:%s] from read set", ns, key, readCache.cachedValue[0:500])
84+
}
85+
}
7186
return readCache.cachedValue, nil
7287
}
7388

7489
// read from storage
7590
value, version, err := s.txmgr.getCommittedValueAndVersion(ns, key)
76-
logger.Debugf("Read state from storage key=[%s:%s], value=[%#v], version=[%d]", ns, key, value, version)
91+
92+
// trace the first 500 bytes of value only, in case it is huge
93+
if logger.IsEnabledFor(logging.DEBUG) {
94+
if len(value) < 500 {
95+
logger.Debugf("Read state from storage key=[%s:%s], value=[%#v], version=[%d]", ns, key, value, version)
96+
} else {
97+
logger.Debugf("Read state from storage key=[%s:%s], value=[%#v...], version=[%d]", ns, key, value[0:500], version)
98+
}
99+
}
100+
77101
if err != nil {
78102
return nil, err
79103
}
@@ -129,7 +153,17 @@ func (s *LockBasedTxSimulator) getTxReadWriteSet() *txmgmt.TxReadWriteSet {
129153
nsRWs := &txmgmt.NsReadWriteSet{NameSpace: ns, Reads: reads, Writes: writes}
130154
txRWSet.NsRWs = append(txRWSet.NsRWs, nsRWs)
131155
}
132-
logger.Debugf("txRWSet = [%s]", txRWSet)
156+
157+
// trace the first 2000 characters of RWSet only, in case it is huge
158+
if logger.IsEnabledFor(logging.DEBUG) {
159+
txRWSetString := txRWSet.String()
160+
if len(txRWSetString) < 2000 {
161+
logger.Debugf("txRWSet = [%s]", txRWSetString)
162+
} else {
163+
logger.Debugf("txRWSet = [%s...]", txRWSetString[0:2000])
164+
}
165+
}
166+
133167
return txRWSet
134168
}
135169

@@ -145,6 +179,7 @@ func getSortedKeys(m interface{}) []string {
145179

146180
// GetTxSimulationResults implements method in interface `ledger.TxSimulator`
147181
func (s *LockBasedTxSimulator) GetTxSimulationResults() ([]byte, error) {
182+
logger.Debugf("Simulation completed, getting simulation results")
148183
return s.getTxReadWriteSet().Marshal()
149184
}
150185

core/ledger/kvledger/txmgmt/lockbasedtxmgmt/lockbased_txmgr.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,16 @@ func (txmgr *LockBasedTxMgr) ValidateAndPrepare(block *protos.Block2) (*protos.B
131131
return nil, nil, err
132132
}
133133

134-
logger.Debugf("validating txRWSet:[%s]", txRWSet)
134+
// trace the first 2000 characters of RWSet only, in case it is huge
135+
if logger.IsEnabledFor(logging.DEBUG) {
136+
txRWSetString := txRWSet.String()
137+
if len(txRWSetString) < 2000 {
138+
logger.Debugf("validating txRWSet:[%s]", txRWSetString)
139+
} else {
140+
logger.Debugf("validating txRWSet:[%s...]", txRWSetString[0:2000])
141+
}
142+
}
143+
135144
if valid, err = txmgr.validateTx(txRWSet); err != nil {
136145
return nil, nil, err
137146
}
@@ -155,7 +164,17 @@ func (txmgr *LockBasedTxMgr) Shutdown() {
155164
}
156165

157166
func (txmgr *LockBasedTxMgr) validateTx(txRWSet *txmgmt.TxReadWriteSet) (bool, error) {
158-
logger.Debugf("Validating txRWSet:%s", txRWSet)
167+
168+
// trace the first 2000 characters of RWSet only, in case it is huge
169+
if logger.IsEnabledFor(logging.DEBUG) {
170+
txRWSetString := txRWSet.String()
171+
if len(txRWSetString) < 2000 {
172+
logger.Debugf("Validating txRWSet:%s", txRWSetString)
173+
} else {
174+
logger.Debugf("Validating txRWSet:%s...", txRWSetString[0:2000])
175+
}
176+
}
177+
159178
var err error
160179
var currentVersion uint64
161180

0 commit comments

Comments
 (0)