From 1923a47bba6f5eef5f3f9ae6b3b07b688a183f8a Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Wed, 13 Sep 2023 10:04:45 -0500 Subject: [PATCH 01/36] Add occ todos / comments (#317) This adds some comments with some useful code pointers for existing logic and discussing future OCC work NA --- baseapp/abci.go | 2 ++ baseapp/baseapp.go | 4 ++++ store/cachekv/store.go | 2 ++ 3 files changed, 8 insertions(+) diff --git a/baseapp/abci.go b/baseapp/abci.go index b70874ace..8ae2ab157 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -240,6 +240,8 @@ func (app *BaseApp) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abc // Otherwise, the ResponseDeliverTx will contain releveant error information. // Regardless of tx execution outcome, the ResponseDeliverTx will contain relevant // gas execution context. +// TODO: (occ) this is the function called from sei-chain to perform execution of a transaction. +// We'd likely replace this with an execution task that is scheduled by the OCC scheduler func (app *BaseApp) DeliverTx(ctx sdk.Context, req abci.RequestDeliverTx) (res abci.ResponseDeliverTx) { defer telemetry.MeasureSince(time.Now(), "abci", "deliver_tx") defer func() { diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index ea9c31134..b4146226d 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -795,6 +795,7 @@ func (app *BaseApp) getContextForTx(mode runTxMode, txBytes []byte) sdk.Context // cacheTxContext returns a new context based off of the provided context with // a branched multi-store. +// TODO: (occ) This is an example of where we wrap the multistore with a cache multistore, and then return a modified context using that multistore func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (sdk.Context, sdk.CacheMultiStore) { ms := ctx.MultiStore() // TODO: https://github.com/cosmos/cosmos-sdk/issues/2824 @@ -948,6 +949,7 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, txBytes []byte) (gInf storeAccessOpEvents := msCache.GetEvents() accessOps := ctx.TxMsgAccessOps()[acltypes.ANTE_MSG_INDEX] + // TODO: (occ) This is an example of where we do our current validation. Note that this validation operates on the declared dependencies for a TX / antehandler + the utilized dependencies, whereas the validation missingAccessOps := ctx.MsgValidator().ValidateAccessOperations(accessOps, storeAccessOpEvents) if len(missingAccessOps) != 0 { for op := range missingAccessOps { @@ -1092,6 +1094,8 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (*s storeAccessOpEvents := msgMsCache.GetEvents() accessOps := ctx.TxMsgAccessOps()[i] missingAccessOps := ctx.MsgValidator().ValidateAccessOperations(accessOps, storeAccessOpEvents) + // TODO: (occ) This is where we are currently validating our per message dependencies, + // whereas validation will be done holistically based on the mvkv for OCC approach if len(missingAccessOps) != 0 { for op := range missingAccessOps { ctx.Logger().Info((fmt.Sprintf("eventMsgName=%s Missing Access Operation:%s ", eventMsgName, op.String()))) diff --git a/store/cachekv/store.go b/store/cachekv/store.go index f9dee6cbb..0317f03cf 100644 --- a/store/cachekv/store.go +++ b/store/cachekv/store.go @@ -176,6 +176,8 @@ func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator { defer store.mtx.Unlock() // TODO: (occ) Note that for iterators, we'll need to have special handling (discussed in RFC) to ensure proper validation + // TODO: (occ) Note that for iterators, we'll need to have special handling (discussed in RFC) to ensure proper validation + var parent, cache types.Iterator if ascending { From 5ad76e0c93a18f638ce1cc0e2c68be29f43ccb49 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Tue, 26 Sep 2023 15:55:13 -0400 Subject: [PATCH 02/36] Multiversion Item Implementation and Tests (#318) ## Describe your changes and provide context Add multiversion store data structures file, and implement the multiversioned item ## Testing performed to validate your change Added unit tests to verify behavior --- store/multiversion/data_structures.go | 160 +++++++++++++++++ store/multiversion/data_structures_test.go | 192 +++++++++++++++++++++ 2 files changed, 352 insertions(+) create mode 100644 store/multiversion/data_structures.go create mode 100644 store/multiversion/data_structures_test.go diff --git a/store/multiversion/data_structures.go b/store/multiversion/data_structures.go new file mode 100644 index 000000000..a382a6f0a --- /dev/null +++ b/store/multiversion/data_structures.go @@ -0,0 +1,160 @@ +package multiversion + +import ( + "sync" + + "github.com/cosmos/cosmos-sdk/store/types" + "github.com/google/btree" +) + +const ( + // The approximate number of items and children per B-tree node. Tuned with benchmarks. + multiVersionBTreeDegree = 2 // should be equivalent to a binary search tree TODO: benchmark this +) + +type MultiVersionValue interface { + GetLatest() (value MultiVersionValueItem, found bool) + GetLatestBeforeIndex(index int) (value MultiVersionValueItem, found bool) + Set(index int, value []byte) + SetEstimate(index int) + Delete(index int) +} + +type MultiVersionValueItem interface { + IsDeleted() bool + IsEstimate() bool + Value() []byte + Index() int +} + +type multiVersionItem struct { + valueTree *btree.BTree // contains versions values written to this key + mtx sync.RWMutex // manages read + write accesses +} + +var _ MultiVersionValue = (*multiVersionItem)(nil) + +func NewMultiVersionItem() *multiVersionItem { + return &multiVersionItem{ + valueTree: btree.New(multiVersionBTreeDegree), + } +} + +// GetLatest returns the latest written value to the btree, and returns a boolean indicating whether it was found. +// +// A `nil` value along with `found=true` indicates a deletion that has occurred and the underlying parent store doesn't need to be hit. +func (item *multiVersionItem) GetLatest() (MultiVersionValueItem, bool) { + item.mtx.RLock() + defer item.mtx.RUnlock() + + bTreeItem := item.valueTree.Max() + if bTreeItem == nil { + return nil, false + } + valueItem := bTreeItem.(*valueItem) + return valueItem, true +} + +// GetLatest returns the latest written value to the btree prior to the index passed in, and returns a boolean indicating whether it was found. +// +// A `nil` value along with `found=true` indicates a deletion that has occurred and the underlying parent store doesn't need to be hit. +func (item *multiVersionItem) GetLatestBeforeIndex(index int) (MultiVersionValueItem, bool) { + item.mtx.RLock() + defer item.mtx.RUnlock() + + // we want to find the value at the index that is LESS than the current index + pivot := NewDeletedItem(index - 1) + + var vItem *valueItem + var found bool + // start from pivot which contains our current index, and return on first item we hit. + // This will ensure we get the latest indexed value relative to our current index + item.valueTree.DescendLessOrEqual(pivot, func(bTreeItem btree.Item) bool { + vItem = bTreeItem.(*valueItem) + found = true + return false + }) + return vItem, found +} + +func (item *multiVersionItem) Set(index int, value []byte) { + types.AssertValidValue(value) + item.mtx.Lock() + defer item.mtx.Unlock() + + valueItem := NewValueItem(index, value) + item.valueTree.ReplaceOrInsert(valueItem) +} + +func (item *multiVersionItem) Delete(index int) { + item.mtx.Lock() + defer item.mtx.Unlock() + + deletedItem := NewDeletedItem(index) + item.valueTree.ReplaceOrInsert(deletedItem) +} + +func (item *multiVersionItem) SetEstimate(index int) { + item.mtx.Lock() + defer item.mtx.Unlock() + + estimateItem := NewEstimateItem(index) + item.valueTree.ReplaceOrInsert(estimateItem) +} + +type valueItem struct { + index int + value []byte + estimate bool +} + +var _ MultiVersionValueItem = (*valueItem)(nil) + +// Index implements MultiVersionValueItem. +func (v *valueItem) Index() int { + return v.index +} + +// IsDeleted implements MultiVersionValueItem. +func (v *valueItem) IsDeleted() bool { + return v.value == nil && !v.estimate +} + +// IsEstimate implements MultiVersionValueItem. +func (v *valueItem) IsEstimate() bool { + return v.estimate +} + +// Value implements MultiVersionValueItem. +func (v *valueItem) Value() []byte { + return v.value +} + +// implement Less for btree.Item for valueItem +func (i *valueItem) Less(other btree.Item) bool { + return i.index < other.(*valueItem).index +} + +func NewValueItem(index int, value []byte) *valueItem { + return &valueItem{ + index: index, + value: value, + estimate: false, + } +} + +func NewEstimateItem(index int) *valueItem { + return &valueItem{ + index: index, + value: nil, + estimate: true, + } +} + +func NewDeletedItem(index int) *valueItem { + return &valueItem{ + index: index, + value: nil, + estimate: false, + } +} diff --git a/store/multiversion/data_structures_test.go b/store/multiversion/data_structures_test.go new file mode 100644 index 000000000..92975462d --- /dev/null +++ b/store/multiversion/data_structures_test.go @@ -0,0 +1,192 @@ +package multiversion_test + +import ( + "testing" + + mv "github.com/cosmos/cosmos-sdk/store/multiversion" + "github.com/stretchr/testify/require" +) + +func TestMultiversionItemGetLatest(t *testing.T) { + mvItem := mv.NewMultiVersionItem() + // We have no value, should get found == false and a nil value + value, found := mvItem.GetLatest() + require.False(t, found) + require.Nil(t, value) + + // assert that we find a value after it's set + one := []byte("one") + mvItem.Set(1, one) + value, found = mvItem.GetLatest() + require.True(t, found) + require.Equal(t, one, value.Value()) + + // assert that we STILL get the "one" value since it is the latest + zero := []byte("zero") + mvItem.Set(0, zero) + value, found = mvItem.GetLatest() + require.True(t, found) + require.Equal(t, one, value.Value()) + + // we should see a deletion as the latest now, aka nil value and found == true + mvItem.Delete(2) + value, found = mvItem.GetLatest() + require.True(t, found) + require.True(t, value.IsDeleted()) + require.Nil(t, value.Value()) + + // Overwrite the deleted value with some data + two := []byte("two") + mvItem.Set(2, two) + value, found = mvItem.GetLatest() + require.True(t, found) + require.Equal(t, two, value.Value()) +} + +func TestMultiversionItemGetByIndex(t *testing.T) { + mvItem := mv.NewMultiVersionItem() + // We have no value, should get found == false and a nil value + value, found := mvItem.GetLatestBeforeIndex(9) + require.False(t, found) + require.Nil(t, value) + + // assert that we find a value after it's set + one := []byte("one") + mvItem.Set(1, one) + // should not be found because we specifically search "LESS THAN" + value, found = mvItem.GetLatestBeforeIndex(1) + require.False(t, found) + require.Nil(t, value) + // querying from "two" should be found + value, found = mvItem.GetLatestBeforeIndex(2) + require.True(t, found) + require.Equal(t, one, value.Value()) + + // verify that querying for an earlier index returns nil + value, found = mvItem.GetLatestBeforeIndex(0) + require.False(t, found) + require.Nil(t, value) + + // assert that we STILL get the "one" value when querying with a later index + zero := []byte("zero") + mvItem.Set(0, zero) + // verify that querying for zero should ALWAYS return nil + value, found = mvItem.GetLatestBeforeIndex(0) + require.False(t, found) + require.Nil(t, value) + + value, found = mvItem.GetLatestBeforeIndex(2) + require.True(t, found) + require.Equal(t, one, value.Value()) + // verify we get zero when querying with index 1 + value, found = mvItem.GetLatestBeforeIndex(1) + require.True(t, found) + require.Equal(t, zero, value.Value()) + + // we should see a deletion as the latest now, aka nil value and found == true, but index 4 still returns `one` + mvItem.Delete(4) + value, found = mvItem.GetLatestBeforeIndex(4) + require.True(t, found) + require.Equal(t, one, value.Value()) + // should get deletion item for a later index + value, found = mvItem.GetLatestBeforeIndex(5) + require.True(t, found) + require.True(t, value.IsDeleted()) + + // verify that we still read the proper underlying item for an older index + value, found = mvItem.GetLatestBeforeIndex(3) + require.True(t, found) + require.Equal(t, one, value.Value()) + + // Overwrite the deleted value with some data and verify we read it properly + four := []byte("four") + mvItem.Set(4, four) + // also reads the four + value, found = mvItem.GetLatestBeforeIndex(6) + require.True(t, found) + require.Equal(t, four, value.Value()) + // still reads the `one` + value, found = mvItem.GetLatestBeforeIndex(4) + require.True(t, found) + require.Equal(t, one, value.Value()) +} + +func TestMultiversionItemEstimate(t *testing.T) { + mvItem := mv.NewMultiVersionItem() + // We have no value, should get found == false and a nil value + value, found := mvItem.GetLatestBeforeIndex(9) + require.False(t, found) + require.Nil(t, value) + + // assert that we find a value after it's set + one := []byte("one") + mvItem.Set(1, one) + // should not be found because we specifically search "LESS THAN" + value, found = mvItem.GetLatestBeforeIndex(1) + require.False(t, found) + require.Nil(t, value) + // querying from "two" should be found + value, found = mvItem.GetLatestBeforeIndex(2) + require.True(t, found) + require.False(t, value.IsEstimate()) + require.Equal(t, one, value.Value()) + // set as estimate + mvItem.SetEstimate(1) + // should not be found because we specifically search "LESS THAN" + value, found = mvItem.GetLatestBeforeIndex(1) + require.False(t, found) + require.Nil(t, value) + // querying from "two" should be found as ESTIMATE + value, found = mvItem.GetLatestBeforeIndex(2) + require.True(t, found) + require.True(t, value.IsEstimate()) + + // verify that querying for an earlier index returns nil + value, found = mvItem.GetLatestBeforeIndex(0) + require.False(t, found) + require.Nil(t, value) + + // assert that we STILL get the "one" value when querying with a later index + zero := []byte("zero") + mvItem.Set(0, zero) + // verify that querying for zero should ALWAYS return nil + value, found = mvItem.GetLatestBeforeIndex(0) + require.False(t, found) + require.Nil(t, value) + + value, found = mvItem.GetLatestBeforeIndex(2) + require.True(t, found) + require.True(t, value.IsEstimate()) + // verify we get zero when querying with index 1 + value, found = mvItem.GetLatestBeforeIndex(1) + require.True(t, found) + require.Equal(t, zero, value.Value()) + // reset one to no longer be an estiamte + mvItem.Set(1, one) + // we should see a deletion as the latest now, aka nil value and found == true, but index 4 still returns `one` + mvItem.Delete(4) + value, found = mvItem.GetLatestBeforeIndex(4) + require.True(t, found) + require.Equal(t, one, value.Value()) + // should get deletion item for a later index + value, found = mvItem.GetLatestBeforeIndex(5) + require.True(t, found) + require.True(t, value.IsDeleted()) + + // verify that we still read the proper underlying item for an older index + value, found = mvItem.GetLatestBeforeIndex(3) + require.True(t, found) + require.Equal(t, one, value.Value()) + + // Overwrite the deleted value with an estimate and verify we read it properly + mvItem.SetEstimate(4) + // also reads the four + value, found = mvItem.GetLatestBeforeIndex(6) + require.True(t, found) + require.True(t, value.IsEstimate()) + require.False(t, value.IsDeleted()) + // still reads the `one` + value, found = mvItem.GetLatestBeforeIndex(4) + require.True(t, found) + require.Equal(t, one, value.Value()) +} From 5e5b4ce402287b236cd5254c84317d0c06cb5566 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Fri, 29 Sep 2023 15:28:02 -0400 Subject: [PATCH 03/36] [occ] Add incarnation field (#321) ## Describe your changes and provide context This adds the incarnation field to the multiversion item data structure. ## Testing performed to validate your change updated unit tests --- store/multiversion/data_structures.go | 60 +++++++++++++--------- store/multiversion/data_structures_test.go | 36 ++++++++----- 2 files changed, 57 insertions(+), 39 deletions(-) diff --git a/store/multiversion/data_structures.go b/store/multiversion/data_structures.go index a382a6f0a..c4ca7b995 100644 --- a/store/multiversion/data_structures.go +++ b/store/multiversion/data_structures.go @@ -15,15 +15,16 @@ const ( type MultiVersionValue interface { GetLatest() (value MultiVersionValueItem, found bool) GetLatestBeforeIndex(index int) (value MultiVersionValueItem, found bool) - Set(index int, value []byte) - SetEstimate(index int) - Delete(index int) + Set(index int, incarnation int, value []byte) + SetEstimate(index int, incarnation int) + Delete(index int, incarnation int) } type MultiVersionValueItem interface { IsDeleted() bool IsEstimate() bool Value() []byte + Incarnation() int Index() int } @@ -63,7 +64,7 @@ func (item *multiVersionItem) GetLatestBeforeIndex(index int) (MultiVersionValue defer item.mtx.RUnlock() // we want to find the value at the index that is LESS than the current index - pivot := NewDeletedItem(index - 1) + pivot := &valueItem{index: index - 1} var vItem *valueItem var found bool @@ -77,35 +78,36 @@ func (item *multiVersionItem) GetLatestBeforeIndex(index int) (MultiVersionValue return vItem, found } -func (item *multiVersionItem) Set(index int, value []byte) { +func (item *multiVersionItem) Set(index int, incarnation int, value []byte) { types.AssertValidValue(value) item.mtx.Lock() defer item.mtx.Unlock() - valueItem := NewValueItem(index, value) + valueItem := NewValueItem(index, incarnation, value) item.valueTree.ReplaceOrInsert(valueItem) } -func (item *multiVersionItem) Delete(index int) { +func (item *multiVersionItem) Delete(index int, incarnation int) { item.mtx.Lock() defer item.mtx.Unlock() - deletedItem := NewDeletedItem(index) + deletedItem := NewDeletedItem(index, incarnation) item.valueTree.ReplaceOrInsert(deletedItem) } -func (item *multiVersionItem) SetEstimate(index int) { +func (item *multiVersionItem) SetEstimate(index int, incarnation int) { item.mtx.Lock() defer item.mtx.Unlock() - estimateItem := NewEstimateItem(index) + estimateItem := NewEstimateItem(index, incarnation) item.valueTree.ReplaceOrInsert(estimateItem) } type valueItem struct { - index int - value []byte - estimate bool + index int + incarnation int + value []byte + estimate bool } var _ MultiVersionValueItem = (*valueItem)(nil) @@ -115,6 +117,11 @@ func (v *valueItem) Index() int { return v.index } +// Incarnation implements MultiVersionValueItem. +func (v *valueItem) Incarnation() int { + return v.incarnation +} + // IsDeleted implements MultiVersionValueItem. func (v *valueItem) IsDeleted() bool { return v.value == nil && !v.estimate @@ -135,26 +142,29 @@ func (i *valueItem) Less(other btree.Item) bool { return i.index < other.(*valueItem).index } -func NewValueItem(index int, value []byte) *valueItem { +func NewValueItem(index int, incarnation int, value []byte) *valueItem { return &valueItem{ - index: index, - value: value, - estimate: false, + index: index, + incarnation: incarnation, + value: value, + estimate: false, } } -func NewEstimateItem(index int) *valueItem { +func NewEstimateItem(index int, incarnation int) *valueItem { return &valueItem{ - index: index, - value: nil, - estimate: true, + index: index, + incarnation: incarnation, + value: nil, + estimate: true, } } -func NewDeletedItem(index int) *valueItem { +func NewDeletedItem(index int, incarnation int) *valueItem { return &valueItem{ - index: index, - value: nil, - estimate: false, + index: index, + incarnation: incarnation, + value: nil, + estimate: false, } } diff --git a/store/multiversion/data_structures_test.go b/store/multiversion/data_structures_test.go index 92975462d..31696d366 100644 --- a/store/multiversion/data_structures_test.go +++ b/store/multiversion/data_structures_test.go @@ -16,20 +16,22 @@ func TestMultiversionItemGetLatest(t *testing.T) { // assert that we find a value after it's set one := []byte("one") - mvItem.Set(1, one) + mvItem.Set(1, 0, one) value, found = mvItem.GetLatest() require.True(t, found) require.Equal(t, one, value.Value()) // assert that we STILL get the "one" value since it is the latest zero := []byte("zero") - mvItem.Set(0, zero) + mvItem.Set(0, 0, zero) value, found = mvItem.GetLatest() require.True(t, found) require.Equal(t, one, value.Value()) + require.Equal(t, 1, value.Index()) + require.Equal(t, 0, value.Incarnation()) // we should see a deletion as the latest now, aka nil value and found == true - mvItem.Delete(2) + mvItem.Delete(2, 0) value, found = mvItem.GetLatest() require.True(t, found) require.True(t, value.IsDeleted()) @@ -37,10 +39,12 @@ func TestMultiversionItemGetLatest(t *testing.T) { // Overwrite the deleted value with some data two := []byte("two") - mvItem.Set(2, two) + mvItem.Set(2, 3, two) value, found = mvItem.GetLatest() require.True(t, found) require.Equal(t, two, value.Value()) + require.Equal(t, 2, value.Index()) + require.Equal(t, 3, value.Incarnation()) } func TestMultiversionItemGetByIndex(t *testing.T) { @@ -52,7 +56,7 @@ func TestMultiversionItemGetByIndex(t *testing.T) { // assert that we find a value after it's set one := []byte("one") - mvItem.Set(1, one) + mvItem.Set(1, 0, one) // should not be found because we specifically search "LESS THAN" value, found = mvItem.GetLatestBeforeIndex(1) require.False(t, found) @@ -69,7 +73,7 @@ func TestMultiversionItemGetByIndex(t *testing.T) { // assert that we STILL get the "one" value when querying with a later index zero := []byte("zero") - mvItem.Set(0, zero) + mvItem.Set(0, 0, zero) // verify that querying for zero should ALWAYS return nil value, found = mvItem.GetLatestBeforeIndex(0) require.False(t, found) @@ -84,7 +88,7 @@ func TestMultiversionItemGetByIndex(t *testing.T) { require.Equal(t, zero, value.Value()) // we should see a deletion as the latest now, aka nil value and found == true, but index 4 still returns `one` - mvItem.Delete(4) + mvItem.Delete(4, 0) value, found = mvItem.GetLatestBeforeIndex(4) require.True(t, found) require.Equal(t, one, value.Value()) @@ -100,7 +104,7 @@ func TestMultiversionItemGetByIndex(t *testing.T) { // Overwrite the deleted value with some data and verify we read it properly four := []byte("four") - mvItem.Set(4, four) + mvItem.Set(4, 0, four) // also reads the four value, found = mvItem.GetLatestBeforeIndex(6) require.True(t, found) @@ -120,7 +124,7 @@ func TestMultiversionItemEstimate(t *testing.T) { // assert that we find a value after it's set one := []byte("one") - mvItem.Set(1, one) + mvItem.Set(1, 0, one) // should not be found because we specifically search "LESS THAN" value, found = mvItem.GetLatestBeforeIndex(1) require.False(t, found) @@ -131,7 +135,7 @@ func TestMultiversionItemEstimate(t *testing.T) { require.False(t, value.IsEstimate()) require.Equal(t, one, value.Value()) // set as estimate - mvItem.SetEstimate(1) + mvItem.SetEstimate(1, 2) // should not be found because we specifically search "LESS THAN" value, found = mvItem.GetLatestBeforeIndex(1) require.False(t, found) @@ -140,6 +144,8 @@ func TestMultiversionItemEstimate(t *testing.T) { value, found = mvItem.GetLatestBeforeIndex(2) require.True(t, found) require.True(t, value.IsEstimate()) + require.Equal(t, 1, value.Index()) + require.Equal(t, 2, value.Incarnation()) // verify that querying for an earlier index returns nil value, found = mvItem.GetLatestBeforeIndex(0) @@ -148,7 +154,7 @@ func TestMultiversionItemEstimate(t *testing.T) { // assert that we STILL get the "one" value when querying with a later index zero := []byte("zero") - mvItem.Set(0, zero) + mvItem.Set(0, 0, zero) // verify that querying for zero should ALWAYS return nil value, found = mvItem.GetLatestBeforeIndex(0) require.False(t, found) @@ -162,9 +168,9 @@ func TestMultiversionItemEstimate(t *testing.T) { require.True(t, found) require.Equal(t, zero, value.Value()) // reset one to no longer be an estiamte - mvItem.Set(1, one) + mvItem.Set(1, 0, one) // we should see a deletion as the latest now, aka nil value and found == true, but index 4 still returns `one` - mvItem.Delete(4) + mvItem.Delete(4, 1) value, found = mvItem.GetLatestBeforeIndex(4) require.True(t, found) require.Equal(t, one, value.Value()) @@ -172,6 +178,8 @@ func TestMultiversionItemEstimate(t *testing.T) { value, found = mvItem.GetLatestBeforeIndex(5) require.True(t, found) require.True(t, value.IsDeleted()) + require.Equal(t, 4, value.Index()) + require.Equal(t, 1, value.Incarnation()) // verify that we still read the proper underlying item for an older index value, found = mvItem.GetLatestBeforeIndex(3) @@ -179,7 +187,7 @@ func TestMultiversionItemEstimate(t *testing.T) { require.Equal(t, one, value.Value()) // Overwrite the deleted value with an estimate and verify we read it properly - mvItem.SetEstimate(4) + mvItem.SetEstimate(4, 0) // also reads the four value, found = mvItem.GetLatestBeforeIndex(6) require.True(t, found) From cda8c316ce91c1f885081a81ef18c96650962d59 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Thu, 5 Oct 2023 21:18:11 -0500 Subject: [PATCH 04/36] [occ] Implement basic multiversion store (#322) ## Describe your changes and provide context This implements the multiversion with basic functionality, but still needs additional work to implement the iterator functionality and/or persisting readsets for validation ## Testing performed to validate your change Added unit tests for basic multiversion store --- store/multiversion/store.go | 120 +++++++++++++++++++++++++++++++ store/multiversion/store_test.go | 54 ++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 store/multiversion/store.go create mode 100644 store/multiversion/store_test.go diff --git a/store/multiversion/store.go b/store/multiversion/store.go new file mode 100644 index 000000000..b52c6af1a --- /dev/null +++ b/store/multiversion/store.go @@ -0,0 +1,120 @@ +package multiversion + +import ( + "sync" +) + +type MultiVersionStore interface { + GetLatest(key []byte) (value MultiVersionValueItem) + GetLatestBeforeIndex(index int, key []byte) (value MultiVersionValueItem) + Set(index int, incarnation int, key []byte, value []byte) + SetEstimate(index int, incarnation int, key []byte) + Delete(index int, incarnation int, key []byte) + Has(index int, key []byte) bool + // TODO: do we want to add helper functions for validations with readsets / applying writesets ? +} + +type Store struct { + mtx sync.RWMutex + // map that stores the key -> MultiVersionValue mapping for accessing from a given key + multiVersionMap map[string]MultiVersionValue + // TODO: do we need to add something here to persist readsets for later validation + // TODO: we need to support iterators as well similar to how cachekv does it + // TODO: do we need secondary indexing on index -> keys - this way if we need to abort we can replace those keys with ESTIMATE values? - maybe this just means storing writeset +} + +func NewMultiVersionStore() *Store { + return &Store{ + multiVersionMap: make(map[string]MultiVersionValue), + } +} + +// GetLatest implements MultiVersionStore. +func (s *Store) GetLatest(key []byte) (value MultiVersionValueItem) { + s.mtx.RLock() + defer s.mtx.RUnlock() + + keyString := string(key) + // if the key doesn't exist in the overall map, return nil + if _, ok := s.multiVersionMap[keyString]; !ok { + return nil + } + val, found := s.multiVersionMap[keyString].GetLatest() + if !found { + return nil // this shouldn't be possible + } + return val +} + +// GetLatestBeforeIndex implements MultiVersionStore. +func (s *Store) GetLatestBeforeIndex(index int, key []byte) (value MultiVersionValueItem) { + s.mtx.RLock() + defer s.mtx.RUnlock() + + keyString := string(key) + // if the key doesn't exist in the overall map, return nil + if _, ok := s.multiVersionMap[keyString]; !ok { + return nil + } + val, found := s.multiVersionMap[keyString].GetLatestBeforeIndex(index) + // otherwise, we may have found a value for that key, but its not written before the index passed in + if !found { + return nil + } + // found a value prior to the passed in index, return that value (could be estimate OR deleted, but it is a definitive value) + return val +} + +// Has implements MultiVersionStore. It checks if the key exists in the multiversion store at or before the specified index. +func (s *Store) Has(index int, key []byte) bool { + s.mtx.RLock() + defer s.mtx.RUnlock() + + keyString := string(key) + if _, ok := s.multiVersionMap[keyString]; !ok { + return false // this is okay because the caller of this will THEN need to access the parent store to verify that the key doesnt exist there + } + _, found := s.multiVersionMap[keyString].GetLatestBeforeIndex(index) + return found +} + +// This function will try to intialize the multiversion item if it doesn't exist for a key specified by byte array +// NOTE: this should be used within an acquired mutex lock +func (s *Store) tryInitMultiVersionItem(keyString string) { + if _, ok := s.multiVersionMap[keyString]; !ok { + multiVersionValue := NewMultiVersionItem() + s.multiVersionMap[keyString] = multiVersionValue + } +} + +// Set implements MultiVersionStore. +func (s *Store) Set(index int, incarnation int, key []byte, value []byte) { + s.mtx.Lock() + defer s.mtx.Unlock() + + keyString := string(key) + s.tryInitMultiVersionItem(keyString) + s.multiVersionMap[keyString].Set(index, incarnation, value) +} + +// SetEstimate implements MultiVersionStore. +func (s *Store) SetEstimate(index int, incarnation int, key []byte) { + s.mtx.Lock() + defer s.mtx.Unlock() + + keyString := string(key) + s.tryInitMultiVersionItem(keyString) + s.multiVersionMap[keyString].SetEstimate(index, incarnation) +} + +// Delete implements MultiVersionStore. +func (s *Store) Delete(index int, incarnation int, key []byte) { + s.mtx.Lock() + defer s.mtx.Unlock() + + keyString := string(key) + s.tryInitMultiVersionItem(keyString) + s.multiVersionMap[keyString].Delete(index, incarnation) +} + +var _ MultiVersionStore = (*Store)(nil) diff --git a/store/multiversion/store_test.go b/store/multiversion/store_test.go new file mode 100644 index 000000000..91465c435 --- /dev/null +++ b/store/multiversion/store_test.go @@ -0,0 +1,54 @@ +package multiversion_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/store/multiversion" + "github.com/stretchr/testify/require" +) + +func TestMultiVersionStore(t *testing.T) { + store := multiversion.NewMultiVersionStore() + + // Test Set and GetLatest + store.Set(1, 1, []byte("key1"), []byte("value1")) + store.Set(2, 1, []byte("key1"), []byte("value2")) + store.Set(3, 1, []byte("key2"), []byte("value3")) + require.Equal(t, []byte("value2"), store.GetLatest([]byte("key1")).Value()) + require.Equal(t, []byte("value3"), store.GetLatest([]byte("key2")).Value()) + + // Test SetEstimate + store.SetEstimate(4, 1, []byte("key1")) + require.True(t, store.GetLatest([]byte("key1")).IsEstimate()) + + // Test Delete + store.Delete(5, 1, []byte("key1")) + require.True(t, store.GetLatest([]byte("key1")).IsDeleted()) + + // Test GetLatestBeforeIndex + store.Set(6, 1, []byte("key1"), []byte("value4")) + require.True(t, store.GetLatestBeforeIndex(5, []byte("key1")).IsEstimate()) + require.Equal(t, []byte("value4"), store.GetLatestBeforeIndex(7, []byte("key1")).Value()) + + // Test Has + require.True(t, store.Has(2, []byte("key1"))) + require.False(t, store.Has(0, []byte("key1"))) + require.False(t, store.Has(5, []byte("key4"))) +} + +func TestMultiVersionStoreHasLaterValue(t *testing.T) { + store := multiversion.NewMultiVersionStore() + + store.Set(5, 1, []byte("key1"), []byte("value2")) + + require.Nil(t, store.GetLatestBeforeIndex(4, []byte("key1"))) + require.Equal(t, []byte("value2"), store.GetLatestBeforeIndex(6, []byte("key1")).Value()) +} + +func TestMultiVersionStoreKeyDNE(t *testing.T) { + store := multiversion.NewMultiVersionStore() + + require.Nil(t, store.GetLatest([]byte("key1"))) + require.Nil(t, store.GetLatestBeforeIndex(0, []byte("key1"))) + require.False(t, store.Has(0, []byte("key1"))) +} From 703b28a2a891a216ead6556af591aaec2ba2e4a2 Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Mon, 9 Oct 2023 10:48:27 -0400 Subject: [PATCH 05/36] [occ] Add concurrency worker configuration (#324) ## Describe your changes and provide context - `ConcurrencyWorkers` represents the number of workers to use for concurrent transactions - since concurrrency-workers is a baseapp-level setting, implementations (like sei-chain) shouldn't have to pass it (but can) - it defaults to 10 if not set (via cli default value) - it defaults to 10 in app.toml only if that file is being created (and doesn't exist) - if explicitly set to zero on command line, it will override with the default (for safety) - cli takes precedence over the config file - no one has to do anything to get it to be 10 (no config changes no sei-chain changes required (aside from new cosmos version)) ## Testing performed to validate your change - Unit Tests for setting the value - Manually testing scenarios with sei-chain --- baseapp/baseapp.go | 21 ++++++++++++++++++++- baseapp/options.go | 11 +++++++++++ server/config/config.go | 9 +++++++++ server/config/config_test.go | 5 +++++ server/config/toml.go | 3 +++ server/start.go | 2 ++ 6 files changed, 50 insertions(+), 1 deletion(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index b4146226d..1c430eb96 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -14,6 +14,7 @@ import ( "go.opentelemetry.io/otel/trace" "github.com/armon/go-metrics" + "github.com/cosmos/cosmos-sdk/server/config" "github.com/cosmos/cosmos-sdk/utils/tracing" "github.com/gogo/protobuf/proto" sdbm "github.com/sei-protocol/sei-tm-db/backends" @@ -57,7 +58,8 @@ const ( FlagArchivalArweaveIndexDBFullPath = "archival-arweave-index-db-full-path" FlagArchivalArweaveNodeURL = "archival-arweave-node-url" - FlagChainID = "chain-id" + FlagChainID = "chain-id" + FlagConcurrencyWorkers = "concurrency-workers" ) var ( @@ -163,6 +165,8 @@ type BaseApp struct { //nolint: maligned TmConfig *tmcfg.Config TracingInfo *tracing.Info + + concurrencyWorkers int } type appStore struct { @@ -286,6 +290,16 @@ func NewBaseApp( } app.startCompactionRoutine(db) + // if no option overrode already, initialize to the flags value + // this avoids forcing every implementation to pass an option, but allows it + if app.concurrencyWorkers == 0 { + app.concurrencyWorkers = cast.ToInt(appOpts.Get(FlagConcurrencyWorkers)) + } + // safely default this to the default value if 0 + if app.concurrencyWorkers == 0 { + app.concurrencyWorkers = config.DefaultConcurrencyWorkers + } + return app } @@ -299,6 +313,11 @@ func (app *BaseApp) AppVersion() uint64 { return app.appVersion } +// ConcurrencyWorkers returns the number of concurrent workers for the BaseApp. +func (app *BaseApp) ConcurrencyWorkers() int { + return app.concurrencyWorkers +} + // Version returns the application's version string. func (app *BaseApp) Version() string { return app.version diff --git a/baseapp/options.go b/baseapp/options.go index 75ccf93db..e7a07de2a 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -82,6 +82,10 @@ func SetSnapshotInterval(interval uint64) func(*BaseApp) { return func(app *BaseApp) { app.SetSnapshotInterval(interval) } } +func SetConcurrencyWorkers(workers int) func(*BaseApp) { + return func(app *BaseApp) { app.SetConcurrencyWorkers(workers) } +} + // SetSnapshotKeepRecent sets the recent snapshots to keep. func SetSnapshotKeepRecent(keepRecent uint32) func(*BaseApp) { return func(app *BaseApp) { app.SetSnapshotKeepRecent(keepRecent) } @@ -290,6 +294,13 @@ func (app *BaseApp) SetSnapshotInterval(snapshotInterval uint64) { app.snapshotInterval = snapshotInterval } +func (app *BaseApp) SetConcurrencyWorkers(workers int) { + if app.sealed { + panic("SetConcurrencyWorkers() on sealed BaseApp") + } + app.concurrencyWorkers = workers +} + // SetSnapshotKeepRecent sets the number of recent snapshots to keep. func (app *BaseApp) SetSnapshotKeepRecent(snapshotKeepRecent uint32) { if app.sealed { diff --git a/server/config/config.go b/server/config/config.go index 5d2c7c00d..042366f1d 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -21,6 +21,9 @@ const ( // DefaultGRPCWebAddress defines the default address to bind the gRPC-web server to. DefaultGRPCWebAddress = "0.0.0.0:9091" + + // DefaultConcurrencyWorkers defines the default workers to use for concurrent transactions + DefaultConcurrencyWorkers = 10 ) // BaseConfig defines the server's basic configuration @@ -88,6 +91,10 @@ type BaseConfig struct { SeparateOrphanVersionsToKeep int64 `mapstructure:"separate-orphan-versions-to-keep"` NumOrphanPerFile int `mapstructure:"num-orphan-per-file"` OrphanDirectory string `mapstructure:"orphan-dir"` + + // ConcurrencyWorkers defines the number of workers to use for concurrent + // transaction execution. A value of -1 means unlimited workers. Default value is 10. + ConcurrencyWorkers int `mapstructure:"concurrency-workers"` } // APIConfig defines the API listener configuration. @@ -238,6 +245,7 @@ func DefaultConfig() *Config { IAVLDisableFastNode: true, CompactionInterval: 0, NoVersioning: false, + ConcurrencyWorkers: DefaultConcurrencyWorkers, }, Telemetry: telemetry.Config{ Enabled: false, @@ -314,6 +322,7 @@ func GetConfig(v *viper.Viper) (Config, error) { SeparateOrphanVersionsToKeep: v.GetInt64("separate-orphan-versions-to-keep"), NumOrphanPerFile: v.GetInt("num-orphan-per-file"), OrphanDirectory: v.GetString("orphan-dir"), + ConcurrencyWorkers: v.GetInt("concurrency-workers"), }, Telemetry: telemetry.Config{ ServiceName: v.GetString("telemetry.service-name"), diff --git a/server/config/config_test.go b/server/config/config_test.go index ce733c346..040bfa788 100644 --- a/server/config/config_test.go +++ b/server/config/config_test.go @@ -23,3 +23,8 @@ func TestSetSnapshotDirectory(t *testing.T) { cfg := DefaultConfig() require.Equal(t, "", cfg.StateSync.SnapshotDirectory) } + +func TestSetConcurrencyWorkers(t *testing.T) { + cfg := DefaultConfig() + require.Equal(t, DefaultConcurrencyWorkers, cfg.ConcurrencyWorkers) +} diff --git a/server/config/toml.go b/server/config/toml.go index 5c1e2b9f2..2b35d4ae5 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -104,6 +104,9 @@ num-orphan-per-file = {{ .BaseConfig.NumOrphanPerFile }} # if separate-orphan-storage is true, where to store orphan data orphan-dir = "{{ .BaseConfig.OrphanDirectory }}" +# concurrency-workers defines how many workers to run for concurrent transaction execution +# concurrency-workers = {{ .BaseConfig.ConcurrencyWorkers }} + ############################################################################### ### Telemetry Configuration ### ############################################################################### diff --git a/server/start.go b/server/start.go index 14f4e9770..aedc274e4 100644 --- a/server/start.go +++ b/server/start.go @@ -70,6 +70,7 @@ const ( FlagSeparateOrphanVersionsToKeep = "separate-orphan-versions-to-keep" FlagNumOrphanPerFile = "num-orphan-per-file" FlagOrphanDirectory = "orphan-dir" + FlagConcurrencyWorkers = "concurrency-workers" // state sync-related flags FlagStateSyncSnapshotInterval = "state-sync.snapshot-interval" @@ -252,6 +253,7 @@ is performed. Note, when enabled, gRPC will also be automatically enabled. cmd.Flags().Int64(FlagSeparateOrphanVersionsToKeep, 2, "Number of versions to keep if storing orphans separately") cmd.Flags().Int(FlagNumOrphanPerFile, 100000, "Number of orphans to store on each file if storing orphans separately") cmd.Flags().String(FlagOrphanDirectory, path.Join(defaultNodeHome, "orphans"), "Directory to store orphan files if storing orphans separately") + cmd.Flags().Int(FlagConcurrencyWorkers, config.DefaultConcurrencyWorkers, "Number of workers to process concurrent transactions") cmd.Flags().Bool(flagGRPCOnly, false, "Start the node in gRPC query only mode (no Tendermint process is started)") cmd.Flags().Bool(flagGRPCEnable, true, "Define if the gRPC server should be enabled") From 1e602463a634b7a94786a945ac1b8dc581ff29f8 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Tue, 10 Oct 2023 08:36:35 -0500 Subject: [PATCH 06/36] [occ] Occ multiversion store (#326) ## Describe your changes and provide context This adds in functionality to write the latest multiversion values to another store (to be used for writing to parent after transaction execution), and also adds in helpers for writeset management such as setting, invalidating, and setting estimated writesets. ## Testing performed to validate your change Unit testing for added functionality --- store/multiversion/data_structures.go | 34 ++++- store/multiversion/data_structures_test.go | 28 ++++ store/multiversion/store.go | 153 +++++++++++++++++++-- store/multiversion/store_test.go | 88 ++++++++++++ 4 files changed, 292 insertions(+), 11 deletions(-) diff --git a/store/multiversion/data_structures.go b/store/multiversion/data_structures.go index c4ca7b995..cba10d0f4 100644 --- a/store/multiversion/data_structures.go +++ b/store/multiversion/data_structures.go @@ -14,10 +14,12 @@ const ( type MultiVersionValue interface { GetLatest() (value MultiVersionValueItem, found bool) + GetLatestNonEstimate() (value MultiVersionValueItem, found bool) GetLatestBeforeIndex(index int) (value MultiVersionValueItem, found bool) Set(index int, incarnation int, value []byte) SetEstimate(index int, incarnation int) Delete(index int, incarnation int) + Remove(index int) } type MultiVersionValueItem interface { @@ -42,8 +44,6 @@ func NewMultiVersionItem() *multiVersionItem { } // GetLatest returns the latest written value to the btree, and returns a boolean indicating whether it was found. -// -// A `nil` value along with `found=true` indicates a deletion that has occurred and the underlying parent store doesn't need to be hit. func (item *multiVersionItem) GetLatest() (MultiVersionValueItem, bool) { item.mtx.RLock() defer item.mtx.RUnlock() @@ -56,6 +56,29 @@ func (item *multiVersionItem) GetLatest() (MultiVersionValueItem, bool) { return valueItem, true } +// GetLatestNonEstimate returns the latest written value that isn't an ESTIMATE and returns a boolean indicating whether it was found. +// This can be used when we want to write finalized values, since ESTIMATEs can be considered to be irrelevant at that point +func (item *multiVersionItem) GetLatestNonEstimate() (MultiVersionValueItem, bool) { + item.mtx.RLock() + defer item.mtx.RUnlock() + + var vItem *valueItem + var found bool + item.valueTree.Descend(func(bTreeItem btree.Item) bool { + // only return if non-estimate + item := bTreeItem.(*valueItem) + if item.IsEstimate() { + // if estimate, continue + return true + } + // else we want to return + vItem = item + found = true + return false + }) + return vItem, found +} + // GetLatest returns the latest written value to the btree prior to the index passed in, and returns a boolean indicating whether it was found. // // A `nil` value along with `found=true` indicates a deletion that has occurred and the underlying parent store doesn't need to be hit. @@ -95,6 +118,13 @@ func (item *multiVersionItem) Delete(index int, incarnation int) { item.valueTree.ReplaceOrInsert(deletedItem) } +func (item *multiVersionItem) Remove(index int) { + item.mtx.Lock() + defer item.mtx.Unlock() + + item.valueTree.Delete(&valueItem{index: index}) +} + func (item *multiVersionItem) SetEstimate(index int, incarnation int) { item.mtx.Lock() defer item.mtx.Unlock() diff --git a/store/multiversion/data_structures_test.go b/store/multiversion/data_structures_test.go index 31696d366..fccc26a8b 100644 --- a/store/multiversion/data_structures_test.go +++ b/store/multiversion/data_structures_test.go @@ -198,3 +198,31 @@ func TestMultiversionItemEstimate(t *testing.T) { require.True(t, found) require.Equal(t, one, value.Value()) } + +func TestMultiversionItemRemove(t *testing.T) { + mvItem := mv.NewMultiVersionItem() + + mvItem.Set(1, 0, []byte("one")) + mvItem.Set(2, 0, []byte("two")) + + mvItem.Remove(2) + value, found := mvItem.GetLatest() + require.True(t, found) + require.Equal(t, []byte("one"), value.Value()) +} + +func TestMultiversionItemGetLatestNonEstimate(t *testing.T) { + mvItem := mv.NewMultiVersionItem() + + mvItem.SetEstimate(3, 0) + + value, found := mvItem.GetLatestNonEstimate() + require.False(t, found) + require.Nil(t, value) + + mvItem.Set(1, 0, []byte("one")) + value, found = mvItem.GetLatestNonEstimate() + require.True(t, found) + require.Equal(t, []byte("one"), value.Value()) + +} diff --git a/store/multiversion/store.go b/store/multiversion/store.go index b52c6af1a..3aa4800f3 100644 --- a/store/multiversion/store.go +++ b/store/multiversion/store.go @@ -1,31 +1,43 @@ package multiversion import ( + "sort" "sync" + + "github.com/cosmos/cosmos-sdk/store/types" ) type MultiVersionStore interface { GetLatest(key []byte) (value MultiVersionValueItem) GetLatestBeforeIndex(index int, key []byte) (value MultiVersionValueItem) - Set(index int, incarnation int, key []byte, value []byte) - SetEstimate(index int, incarnation int, key []byte) - Delete(index int, incarnation int, key []byte) + Set(index int, incarnation int, key []byte, value []byte) // TODO: maybe we don't need these if all writes are coming from writesets + SetEstimate(index int, incarnation int, key []byte) // TODO: maybe we don't need these if all writes are coming from writesets + Delete(index int, incarnation int, key []byte) // TODO: maybe we don't need these if all writes are coming from writesets Has(index int, key []byte) bool - // TODO: do we want to add helper functions for validations with readsets / applying writesets ? + WriteLatestToStore(parentStore types.KVStore) + SetWriteset(index int, incarnation int, writeset WriteSet) + InvalidateWriteset(index int, incarnation int) + SetEstimatedWriteset(index int, incarnation int, writeset WriteSet) + GetAllWritesetKeys() map[int][]string } +type WriteSet map[string][]byte + +var _ MultiVersionStore = (*Store)(nil) + type Store struct { mtx sync.RWMutex // map that stores the key -> MultiVersionValue mapping for accessing from a given key multiVersionMap map[string]MultiVersionValue - // TODO: do we need to add something here to persist readsets for later validation - // TODO: we need to support iterators as well similar to how cachekv does it - // TODO: do we need secondary indexing on index -> keys - this way if we need to abort we can replace those keys with ESTIMATE values? - maybe this just means storing writeset + // TODO: do we need to support iterators as well similar to how cachekv does it - yes + + txWritesetKeys map[int][]string // map of tx index -> writeset keys } func NewMultiVersionStore() *Store { return &Store{ multiVersionMap: make(map[string]MultiVersionValue), + txWritesetKeys: make(map[int][]string), } } @@ -41,7 +53,7 @@ func (s *Store) GetLatest(key []byte) (value MultiVersionValueItem) { } val, found := s.multiVersionMap[keyString].GetLatest() if !found { - return nil // this shouldn't be possible + return nil // this is possible IF there is are writeset that are then removed for that key } return val } @@ -97,6 +109,95 @@ func (s *Store) Set(index int, incarnation int, key []byte, value []byte) { s.multiVersionMap[keyString].Set(index, incarnation, value) } +func (s *Store) removeOldWriteset(index int, newWriteSet WriteSet) { + writeset := make(map[string][]byte) + if newWriteSet != nil { + // if non-nil writeset passed in, we can use that to optimize removals + writeset = newWriteSet + } + // if there is already a writeset existing, we should remove that fully + if keys, ok := s.txWritesetKeys[index]; ok { + // we need to delete all of the keys in the writeset from the multiversion store + for _, key := range keys { + // small optimization to check if the new writeset is going to write this key, if so, we can leave it behind + if _, ok := writeset[key]; ok { + // we don't need to remove this key because it will be overwritten anyways - saves the operation of removing + rebalancing underlying btree + continue + } + // remove from the appropriate item if present in multiVersionMap + if val, ok := s.multiVersionMap[key]; ok { + val.Remove(index) + } + } + } + // unset the writesetKeys for this index + delete(s.txWritesetKeys, index) +} + +// SetWriteset sets a writeset for a transaction index, and also writes all of the multiversion items in the writeset to the multiversion store. +func (s *Store) SetWriteset(index int, incarnation int, writeset WriteSet) { + s.mtx.Lock() + defer s.mtx.Unlock() + + // remove old writeset if it exists + s.removeOldWriteset(index, writeset) + + writeSetKeys := make([]string, 0, len(writeset)) + for key, value := range writeset { + writeSetKeys = append(writeSetKeys, key) + s.tryInitMultiVersionItem(key) + if value == nil { + // delete if nil value + s.multiVersionMap[key].Delete(index, incarnation) + } else { + s.multiVersionMap[key].Set(index, incarnation, value) + } + } + sort.Strings(writeSetKeys) + s.txWritesetKeys[index] = writeSetKeys +} + +// InvalidateWriteset iterates over the keys for the given index and incarnation writeset and replaces with ESTIMATEs +func (s *Store) InvalidateWriteset(index int, incarnation int) { + s.mtx.Lock() + defer s.mtx.Unlock() + + if keys, ok := s.txWritesetKeys[index]; ok { + for _, key := range keys { + // invalidate all of the writeset items - is this suboptimal? - we could potentially do concurrently if slow because locking is on an item specific level + s.tryInitMultiVersionItem(key) // this SHOULD no-op because we're invalidating existing keys + s.multiVersionMap[key].SetEstimate(index, incarnation) + } + } + // we leave the writeset in place because we'll need it for key removal later if/when we replace with a new writeset +} + +// SetEstimatedWriteset is used to directly write estimates instead of writing a writeset and later invalidating +func (s *Store) SetEstimatedWriteset(index int, incarnation int, writeset WriteSet) { + s.mtx.Lock() + defer s.mtx.Unlock() + + // remove old writeset if it exists + s.removeOldWriteset(index, writeset) + + writeSetKeys := make([]string, 0, len(writeset)) + // still need to save the writeset so we can remove the elements later: + for key := range writeset { + writeSetKeys = append(writeSetKeys, key) + s.tryInitMultiVersionItem(key) + s.multiVersionMap[key].SetEstimate(index, incarnation) + } + sort.Strings(writeSetKeys) + s.txWritesetKeys[index] = writeSetKeys +} + +// GetWritesetKeys implements MultiVersionStore. +func (s *Store) GetAllWritesetKeys() map[int][]string { + s.mtx.RLock() + defer s.mtx.RUnlock() + return s.txWritesetKeys +} + // SetEstimate implements MultiVersionStore. func (s *Store) SetEstimate(index int, incarnation int, key []byte) { s.mtx.Lock() @@ -117,4 +218,38 @@ func (s *Store) Delete(index int, incarnation int, key []byte) { s.multiVersionMap[keyString].Delete(index, incarnation) } -var _ MultiVersionStore = (*Store)(nil) +func (s *Store) WriteLatestToStore(parentStore types.KVStore) { + s.mtx.Lock() + defer s.mtx.Unlock() + + // sort the keys + keys := make([]string, 0, len(s.multiVersionMap)) + for key := range s.multiVersionMap { + keys = append(keys, key) + } + sort.Strings(keys) + + for _, key := range keys { + mvValue, found := s.multiVersionMap[key].GetLatestNonEstimate() + if !found { + // this means that at some point, there was an estimate, but we have since removed it so there isn't anything writeable at the key, so we can skip + continue + } + // we shouldn't have any ESTIMATE values when performing the write, because we read the latest non-estimate values only + if mvValue.IsEstimate() { + panic("should not have any estimate values when writing to parent store") + } + // if the value is deleted, then delete it from the parent store + if mvValue.IsDeleted() { + // We use []byte(key) instead of conv.UnsafeStrToBytes because we cannot + // be sure if the underlying store might do a save with the byteslice or + // not. Once we get confirmation that .Delete is guaranteed not to + // save the byteslice, then we can assume only a read-only copy is sufficient. + parentStore.Delete([]byte(key)) + continue + } + if mvValue.Value() != nil { + parentStore.Set([]byte(key), mvValue.Value()) + } + } +} diff --git a/store/multiversion/store_test.go b/store/multiversion/store_test.go index 91465c435..732a5a6ba 100644 --- a/store/multiversion/store_test.go +++ b/store/multiversion/store_test.go @@ -3,8 +3,10 @@ package multiversion_test import ( "testing" + "github.com/cosmos/cosmos-sdk/store/dbadapter" "github.com/cosmos/cosmos-sdk/store/multiversion" "github.com/stretchr/testify/require" + dbm "github.com/tendermint/tm-db" ) func TestMultiVersionStore(t *testing.T) { @@ -52,3 +54,89 @@ func TestMultiVersionStoreKeyDNE(t *testing.T) { require.Nil(t, store.GetLatestBeforeIndex(0, []byte("key1"))) require.False(t, store.Has(0, []byte("key1"))) } + +func TestMultiVersionStoreWriteToParent(t *testing.T) { + // initialize cachekv store + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore() + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + + mvs.Set(1, 1, []byte("key1"), []byte("value1")) + mvs.Set(2, 1, []byte("key1"), []byte("value2")) + mvs.Set(3, 1, []byte("key2"), []byte("value3")) + mvs.Delete(1, 1, []byte("key3")) + mvs.Delete(1, 1, []byte("key4")) + + mvs.WriteLatestToStore(parentKVStore) + + // assert state in parent store + require.Equal(t, []byte("value2"), parentKVStore.Get([]byte("key1"))) + require.Equal(t, []byte("value3"), parentKVStore.Get([]byte("key2"))) + require.False(t, parentKVStore.Has([]byte("key3"))) + require.False(t, parentKVStore.Has([]byte("key4"))) + + // verify no-op if mvs contains ESTIMATE + mvs.SetEstimate(1, 2, []byte("key5")) + mvs.WriteLatestToStore(parentKVStore) + require.False(t, parentKVStore.Has([]byte("key5"))) +} + +func TestMultiVersionStoreWritesetSetAndInvalidate(t *testing.T) { + mvs := multiversion.NewMultiVersionStore() + + writeset := make(map[string][]byte) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + + mvs.SetWriteset(1, 2, writeset) + require.Equal(t, []byte("value1"), mvs.GetLatest([]byte("key1")).Value()) + require.Equal(t, []byte("value2"), mvs.GetLatest([]byte("key2")).Value()) + require.True(t, mvs.GetLatest([]byte("key3")).IsDeleted()) + + writeset2 := make(map[string][]byte) + writeset2["key1"] = []byte("value3") + + mvs.SetWriteset(2, 1, writeset2) + require.Equal(t, []byte("value3"), mvs.GetLatest([]byte("key1")).Value()) + + // invalidate writeset1 + mvs.InvalidateWriteset(1, 2) + + // verify estimates + require.True(t, mvs.GetLatestBeforeIndex(2, []byte("key1")).IsEstimate()) + require.True(t, mvs.GetLatestBeforeIndex(2, []byte("key2")).IsEstimate()) + require.True(t, mvs.GetLatestBeforeIndex(2, []byte("key3")).IsEstimate()) + + // third writeset + writeset3 := make(map[string][]byte) + writeset3["key4"] = []byte("foo") + writeset3["key5"] = nil + + // write the writeset directly as estimate + mvs.SetEstimatedWriteset(3, 1, writeset3) + + require.True(t, mvs.GetLatest([]byte("key4")).IsEstimate()) + require.True(t, mvs.GetLatest([]byte("key5")).IsEstimate()) + + // try replacing writeset1 to verify old keys removed + writeset1_b := make(map[string][]byte) + writeset1_b["key1"] = []byte("value4") + + mvs.SetWriteset(1, 2, writeset1_b) + require.Equal(t, []byte("value4"), mvs.GetLatestBeforeIndex(2, []byte("key1")).Value()) + require.Nil(t, mvs.GetLatestBeforeIndex(2, []byte("key2"))) + // verify that GetLatest for key3 returns nil - because of removal from writeset + require.Nil(t, mvs.GetLatest([]byte("key3"))) + + // verify output for GetAllWritesetKeys + writesetKeys := mvs.GetAllWritesetKeys() + // we have 3 writesets + require.Equal(t, 3, len(writesetKeys)) + require.Equal(t, []string{"key1"}, writesetKeys[1]) + require.Equal(t, []string{"key1"}, writesetKeys[2]) + require.Equal(t, []string{"key4", "key5"}, writesetKeys[3]) + +} From a3aec8a06639ced20acef73c3864a2da450a35de Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Tue, 10 Oct 2023 14:51:23 -0400 Subject: [PATCH 07/36] [occ] Add batch tx delivery interface (#327) ## Describe your changes and provide context - `sei-cosmos` will receive a list of transactions, so that sei-chain does not need to hold the logic for OCC - This will make the logic easier to test, as sei-cosmos will be fairly self-contained - Types can be extended within a tx and within request/response Example interaction: ## Testing performed to validate your change - This is a skeleton for a batch interface --- baseapp/abci.go | 16 +++++++++++++++- types/tx_batch.go | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 types/tx_batch.go diff --git a/baseapp/abci.go b/baseapp/abci.go index 8ae2ab157..1274f5d6e 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -235,9 +235,23 @@ func (app *BaseApp) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abc }, nil } +// DeliverTxBatch executes multiple txs +// TODO: support occ logic with scheduling +func (app *BaseApp) DeliverTxBatch(ctx sdk.Context, req sdk.DeliverTxBatchRequest) (res sdk.DeliverTxBatchResponse) { + // TODO: replace with actual scheduler logic + // This is stubbed so that it does something sensible + responses := make([]*sdk.DeliverTxResult, 0, len(req.TxEntries)) + for _, tx := range req.TxEntries { + responses = append(responses, &sdk.DeliverTxResult{ + Response: app.DeliverTx(ctx, tx.Request), + }) + } + return sdk.DeliverTxBatchResponse{Results: responses} +} + // DeliverTx implements the ABCI interface and executes a tx in DeliverTx mode. // State only gets persisted if all messages are valid and get executed successfully. -// Otherwise, the ResponseDeliverTx will contain releveant error information. +// Otherwise, the ResponseDeliverTx will contain relevant error information. // Regardless of tx execution outcome, the ResponseDeliverTx will contain relevant // gas execution context. // TODO: (occ) this is the function called from sei-chain to perform execution of a transaction. diff --git a/types/tx_batch.go b/types/tx_batch.go new file mode 100644 index 000000000..a54742fae --- /dev/null +++ b/types/tx_batch.go @@ -0,0 +1,27 @@ +package types + +import abci "github.com/tendermint/tendermint/abci/types" + +// DeliverTxEntry represents an individual transaction's request within a batch. +// This can be extended to include tx-level tracing or metadata +type DeliverTxEntry struct { + Request abci.RequestDeliverTx +} + +// DeliverTxBatchRequest represents a request object for a batch of transactions. +// This can be extended to include request-level tracing or metadata +type DeliverTxBatchRequest struct { + TxEntries []*DeliverTxEntry +} + +// DeliverTxResult represents an individual transaction's response within a batch. +// This can be extended to include tx-level tracing or metadata +type DeliverTxResult struct { + Response abci.ResponseDeliverTx +} + +// DeliverTxBatchResponse represents a response object for a batch of transactions. +// This can be extended to include response-level tracing or metadata +type DeliverTxBatchResponse struct { + Results []*DeliverTxResult +} From 0520ced2b610ecc9e165877c0eebb630b8e03b8b Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Tue, 10 Oct 2023 14:30:14 -0500 Subject: [PATCH 08/36] [occ] MVKV store implementation and tests (#323) ## Describe your changes and provide context This implements an mvkv store that will manage access from a transaction execution to the underlying multiversion store and underlying parent store if the multiversion store doesn't have that key. It will first serve any reads from its own writeset and readset, but if it does have to fall through to multiversion store or parent store, it will add those values to the readset. ## Testing performed to validate your change Unit tests --- store/multiversion/mvkv.go | 268 ++++++++++++++++++++++++++++++++ store/multiversion/mvkv_test.go | 250 +++++++++++++++++++++++++++++ types/occ/scheduler.go | 20 +++ 3 files changed, 538 insertions(+) create mode 100644 store/multiversion/mvkv.go create mode 100644 store/multiversion/mvkv_test.go create mode 100644 types/occ/scheduler.go diff --git a/store/multiversion/mvkv.go b/store/multiversion/mvkv.go new file mode 100644 index 000000000..697561355 --- /dev/null +++ b/store/multiversion/mvkv.go @@ -0,0 +1,268 @@ +package multiversion + +import ( + "io" + "sort" + "sync" + "time" + + "github.com/cosmos/cosmos-sdk/store/types" + "github.com/cosmos/cosmos-sdk/telemetry" + scheduler "github.com/cosmos/cosmos-sdk/types/occ" + dbm "github.com/tendermint/tm-db" +) + +// Version Indexed Store wraps the multiversion store in a way that implements the KVStore interface, but also stores the index of the transaction, and so store actions are applied to the multiversion store using that index +type VersionIndexedStore struct { + mtx sync.Mutex + // used for tracking reads and writes for eventual validation + persistence into multi-version store + readset map[string][]byte // contains the key -> value mapping for all keys read from the store (not mvkv, underlying store) + writeset map[string][]byte // contains the key -> value mapping for all keys written to the store + // TODO: need to add iterateset here as well + + // TODO: do we need this? - I think so? / maybe we just treat `nil` value in the writeset as a delete + deleted *sync.Map + // dirty keys that haven't been sorted yet for iteration + dirtySet map[string]struct{} + // used for iterators - populated at the time of iterator instantiation + // TODO: when we want to perform iteration, we need to move all the dirty keys (writeset and readset) into the sortedTree and then combine with the iterators for the underlying stores + sortedStore *dbm.MemDB // always ascending sorted + // parent stores (both multiversion and underlying parent store) + multiVersionStore MultiVersionStore + parent types.KVStore + // transaction metadata for versioned operations + transactionIndex int + incarnation int + // have abort channel here for aborting transactions + abortChannel chan scheduler.Abort +} + +var _ types.KVStore = (*VersionIndexedStore)(nil) + +func NewVersionIndexedStore(parent types.KVStore, multiVersionStore MultiVersionStore, transactionIndex, incarnation int, abortChannel chan scheduler.Abort) *VersionIndexedStore { + return &VersionIndexedStore{ + readset: make(map[string][]byte), + writeset: make(map[string][]byte), + deleted: &sync.Map{}, + dirtySet: make(map[string]struct{}), + sortedStore: dbm.NewMemDB(), + parent: parent, + multiVersionStore: multiVersionStore, + transactionIndex: transactionIndex, + incarnation: incarnation, + abortChannel: abortChannel, + } +} + +// GetReadset returns the readset +func (store *VersionIndexedStore) GetReadset() map[string][]byte { + return store.readset +} + +// GetWriteset returns the writeset +func (store *VersionIndexedStore) GetWriteset() map[string][]byte { + return store.writeset +} + +// Get implements types.KVStore. +func (store *VersionIndexedStore) Get(key []byte) []byte { + // first try to get from writeset cache, if cache miss, then try to get from multiversion store, if that misses, then get from parent store + // if the key is in the cache, return it + + // don't have RW mutex because we have to update readset + store.mtx.Lock() + defer store.mtx.Unlock() + defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "get") + + types.AssertValidKey(key) + strKey := string(key) + // first check the MVKV writeset, and return that value if present + cacheValue, ok := store.writeset[strKey] + if ok { + // return the value from the cache, no need to update any readset stuff + return cacheValue + } + // read the readset to see if the value exists - and return if applicable + if readsetVal, ok := store.readset[strKey]; ok { + return readsetVal + } + + // if we didn't find it, then we want to check the multivalue store + add to readset if applicable + mvsValue := store.multiVersionStore.GetLatestBeforeIndex(store.transactionIndex, key) + if mvsValue != nil { + if mvsValue.IsEstimate() { + store.abortChannel <- scheduler.NewEstimateAbort(mvsValue.Index()) + return nil + } else { + // This handles both detecting readset conflicts and updating readset if applicable + return store.parseValueAndUpdateReadset(strKey, mvsValue) + } + } + // if we didn't find it in the multiversion store, then we want to check the parent store + add to readset + parentValue := store.parent.Get(key) + store.updateReadSet(key, parentValue) + return parentValue +} + +// This functions handles reads with deleted items and values and verifies that the data is consistent to what we currently have in the readset (IF we have a readset value for that key) +func (store *VersionIndexedStore) parseValueAndUpdateReadset(strKey string, mvsValue MultiVersionValueItem) []byte { + value := mvsValue.Value() + if mvsValue.IsDeleted() { + value = nil + } + store.updateReadSet([]byte(strKey), value) + return value +} + +// This function iterates over the readset, validating that the values in the readset are consistent with the values in the multiversion store and underlying parent store, and returns a boolean indicating validity +func (store *VersionIndexedStore) ValidateReadset() bool { + store.mtx.Lock() + defer store.mtx.Unlock() + defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "validate_readset") + + // sort the readset keys - this is so we have consistent behavior when theres varying conflicts within the readset (eg. read conflict vs estimate) + readsetKeys := make([]string, 0, len(store.readset)) + for key := range store.readset { + readsetKeys = append(readsetKeys, key) + } + sort.Strings(readsetKeys) + + // iterate over readset keys and values + for _, strKey := range readsetKeys { + key := []byte(strKey) + value := store.readset[strKey] + mvsValue := store.multiVersionStore.GetLatestBeforeIndex(store.transactionIndex, key) + if mvsValue != nil { + if mvsValue.IsEstimate() { + // if we see an estimate, that means that we need to abort and rerun + store.abortChannel <- scheduler.NewEstimateAbort(mvsValue.Index()) + return false + } else { + if mvsValue.IsDeleted() { + // check for `nil` + if value != nil { + return false + } + } else { + // check for equality + if string(value) != string(mvsValue.Value()) { + return false + } + } + } + continue // value is valid, continue to next key + } + + parentValue := store.parent.Get(key) + if string(parentValue) != string(value) { + // this shouldnt happen because if we have a conflict it should always happen within multiversion store + panic("we shouldn't ever have a readset conflict in parent store") + } + // value was correct, we can continue to the next value + } + return true +} + +// Delete implements types.KVStore. +func (store *VersionIndexedStore) Delete(key []byte) { + store.mtx.Lock() + defer store.mtx.Unlock() + defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "delete") + + types.AssertValidKey(key) + store.setValue(key, nil, true, true) +} + +// Has implements types.KVStore. +func (store *VersionIndexedStore) Has(key []byte) bool { + // necessary locking happens within store.Get + return store.Get(key) != nil +} + +// Set implements types.KVStore. +func (store *VersionIndexedStore) Set(key []byte, value []byte) { + store.mtx.Lock() + defer store.mtx.Unlock() + defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "set") + + types.AssertValidKey(key) + store.setValue(key, value, false, true) +} + +// Iterator implements types.KVStore. +func (v *VersionIndexedStore) Iterator(start []byte, end []byte) dbm.Iterator { + panic("unimplemented") +} + +// ReverseIterator implements types.KVStore. +func (v *VersionIndexedStore) ReverseIterator(start []byte, end []byte) dbm.Iterator { + panic("unimplemented") +} + +// GetStoreType implements types.KVStore. +func (v *VersionIndexedStore) GetStoreType() types.StoreType { + return v.parent.GetStoreType() +} + +// CacheWrap implements types.KVStore. +func (*VersionIndexedStore) CacheWrap(storeKey types.StoreKey) types.CacheWrap { + panic("CacheWrap not supported for version indexed store") +} + +// CacheWrapWithListeners implements types.KVStore. +func (*VersionIndexedStore) CacheWrapWithListeners(storeKey types.StoreKey, listeners []types.WriteListener) types.CacheWrap { + panic("CacheWrapWithListeners not supported for version indexed store") +} + +// CacheWrapWithTrace implements types.KVStore. +func (*VersionIndexedStore) CacheWrapWithTrace(storeKey types.StoreKey, w io.Writer, tc types.TraceContext) types.CacheWrap { + panic("CacheWrapWithTrace not supported for version indexed store") +} + +// GetWorkingHash implements types.KVStore. +func (v *VersionIndexedStore) GetWorkingHash() ([]byte, error) { + panic("should never attempt to get working hash from version indexed store") +} + +// Only entrypoint to mutate writeset +func (store *VersionIndexedStore) setValue(key, value []byte, deleted bool, dirty bool) { + types.AssertValidKey(key) + + keyStr := string(key) + store.writeset[keyStr] = value + if deleted { + store.deleted.Store(keyStr, struct{}{}) + } else { + store.deleted.Delete(keyStr) + } + if dirty { + store.dirtySet[keyStr] = struct{}{} + } +} + +func (store *VersionIndexedStore) WriteToMultiVersionStore() { + store.mtx.Lock() + defer store.mtx.Unlock() + defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "write_mvs") + store.multiVersionStore.SetWriteset(store.transactionIndex, store.incarnation, store.writeset) +} + +func (store *VersionIndexedStore) WriteEstimatesToMultiVersionStore() { + store.mtx.Lock() + defer store.mtx.Unlock() + defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "write_mvs") + store.multiVersionStore.SetEstimatedWriteset(store.transactionIndex, store.incarnation, store.writeset) +} + +func (store *VersionIndexedStore) updateReadSet(key []byte, value []byte) { + // add to readset + keyStr := string(key) + store.readset[keyStr] = value + // add to dirty set + store.dirtySet[keyStr] = struct{}{} +} + +func (store *VersionIndexedStore) isDeleted(key string) bool { + _, ok := store.deleted.Load(key) + return ok +} diff --git a/store/multiversion/mvkv_test.go b/store/multiversion/mvkv_test.go new file mode 100644 index 000000000..df1692d1f --- /dev/null +++ b/store/multiversion/mvkv_test.go @@ -0,0 +1,250 @@ +package multiversion_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/store/cachekv" + "github.com/cosmos/cosmos-sdk/store/dbadapter" + "github.com/cosmos/cosmos-sdk/store/multiversion" + "github.com/cosmos/cosmos-sdk/store/types" + scheduler "github.com/cosmos/cosmos-sdk/types/occ" + "github.com/stretchr/testify/require" + dbm "github.com/tendermint/tm-db" +) + +func TestVersionIndexedStoreGetters(t *testing.T) { + mem := dbadapter.Store{DB: dbm.NewMemDB()} + parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) + mvs := multiversion.NewMultiVersionStore() + // initialize a new VersionIndexedStore + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 1, 2, make(chan scheduler.Abort)) + + // mock a value in the parent store + parentKVStore.Set([]byte("key1"), []byte("value1")) + + // read key that doesn't exist + val := vis.Get([]byte("key2")) + require.Nil(t, val) + require.False(t, vis.Has([]byte("key2"))) + + // read key that falls down to parent store + val2 := vis.Get([]byte("key1")) + require.Equal(t, []byte("value1"), val2) + require.True(t, vis.Has([]byte("key1"))) + // verify value now in readset + require.Equal(t, []byte("value1"), vis.GetReadset()["key1"]) + + // read the same key that should now be served from the readset (can be verified by setting a different value for the key in the parent store) + parentKVStore.Set([]byte("key1"), []byte("value2")) // realistically shouldn't happen, modifying to verify readset access + val3 := vis.Get([]byte("key1")) + require.True(t, vis.Has([]byte("key1"))) + require.Equal(t, []byte("value1"), val3) + + // test deleted value written to MVS but not parent store + mvs.Delete(0, 2, []byte("delKey")) + parentKVStore.Set([]byte("delKey"), []byte("value4")) + valDel := vis.Get([]byte("delKey")) + require.Nil(t, valDel) + require.False(t, vis.Has([]byte("delKey"))) + + // set different key in MVS - for various indices + mvs.Set(0, 2, []byte("key3"), []byte("value3")) + mvs.Set(2, 1, []byte("key3"), []byte("value4")) + mvs.SetEstimate(5, 0, []byte("key3")) + + // read the key that falls down to MVS + val4 := vis.Get([]byte("key3")) + // should equal value3 because value4 is later than the key in question + require.Equal(t, []byte("value3"), val4) + require.True(t, vis.Has([]byte("key3"))) + + // try a read that falls through to MVS with a later tx index + vis2 := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 3, 2, make(chan scheduler.Abort)) + val5 := vis2.Get([]byte("key3")) + // should equal value3 because value4 is later than the key in question + require.Equal(t, []byte("value4"), val5) + require.True(t, vis2.Has([]byte("key3"))) + + // test estimate values writing to abortChannel + abortChannel := make(chan scheduler.Abort) + vis3 := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 6, 2, abortChannel) + go func() { + vis3.Get([]byte("key3")) + }() + abort := <-abortChannel // read the abort from the channel + require.Equal(t, 5, abort.DependentTxIdx) + require.Equal(t, scheduler.ErrReadEstimate, abort.Err) + + vis.Set([]byte("key4"), []byte("value4")) + // verify proper response for GET + val6 := vis.Get([]byte("key4")) + require.True(t, vis.Has([]byte("key4"))) + require.Equal(t, []byte("value4"), val6) + // verify that its in the writeset + require.Equal(t, []byte("value4"), vis.GetWriteset()["key4"]) + // verify that its not in the readset + require.Nil(t, vis.GetReadset()["key4"]) +} + +func TestVersionIndexedStoreSetters(t *testing.T) { + mem := dbadapter.Store{DB: dbm.NewMemDB()} + parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) + mvs := multiversion.NewMultiVersionStore() + // initialize a new VersionIndexedStore + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 1, 2, make(chan scheduler.Abort)) + + // test simple set + vis.Set([]byte("key1"), []byte("value1")) + require.Equal(t, []byte("value1"), vis.GetWriteset()["key1"]) + + mvs.Set(0, 1, []byte("key2"), []byte("value2")) + vis.Delete([]byte("key2")) + require.Nil(t, vis.Get([]byte("key2"))) + // because the delete should be at the writeset level, we should not have populated the readset + require.Zero(t, len(vis.GetReadset())) + + // try setting the value again, and then read + vis.Set([]byte("key2"), []byte("value3")) + require.Equal(t, []byte("value3"), vis.Get([]byte("key2"))) + require.Zero(t, len(vis.GetReadset())) +} + +func TestVersionIndexedStoreBoilerplateFunctions(t *testing.T) { + mem := dbadapter.Store{DB: dbm.NewMemDB()} + parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) + mvs := multiversion.NewMultiVersionStore() + // initialize a new VersionIndexedStore + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 1, 2, make(chan scheduler.Abort)) + + // asserts panics where appropriate + require.Panics(t, func() { vis.CacheWrap(types.NewKVStoreKey("mock")) }) + require.Panics(t, func() { vis.CacheWrapWithListeners(types.NewKVStoreKey("mock"), nil) }) + require.Panics(t, func() { vis.CacheWrapWithTrace(types.NewKVStoreKey("mock"), nil, nil) }) + require.Panics(t, func() { vis.GetWorkingHash() }) + + // assert properly returns store type + require.Equal(t, types.StoreTypeDB, vis.GetStoreType()) +} + +func TestVersionIndexedStoreWrite(t *testing.T) { + mem := dbadapter.Store{DB: dbm.NewMemDB()} + parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) + mvs := multiversion.NewMultiVersionStore() + // initialize a new VersionIndexedStore + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 1, 2, make(chan scheduler.Abort)) + + mvs.Set(0, 1, []byte("key3"), []byte("value3")) + + require.False(t, mvs.Has(3, []byte("key1"))) + require.False(t, mvs.Has(3, []byte("key2"))) + require.True(t, mvs.Has(3, []byte("key3"))) + + // write some keys + vis.Set([]byte("key1"), []byte("value1")) + vis.Set([]byte("key2"), []byte("value2")) + vis.Delete([]byte("key3")) + + vis.WriteToMultiVersionStore() + + require.Equal(t, []byte("value1"), mvs.GetLatest([]byte("key1")).Value()) + require.Equal(t, []byte("value2"), mvs.GetLatest([]byte("key2")).Value()) + require.True(t, mvs.GetLatest([]byte("key3")).IsDeleted()) +} + +func TestVersionIndexedStoreWriteEstimates(t *testing.T) { + mem := dbadapter.Store{DB: dbm.NewMemDB()} + parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) + mvs := multiversion.NewMultiVersionStore() + // initialize a new VersionIndexedStore + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 1, 2, make(chan scheduler.Abort)) + + mvs.Set(0, 1, []byte("key3"), []byte("value3")) + + require.False(t, mvs.Has(3, []byte("key1"))) + require.False(t, mvs.Has(3, []byte("key2"))) + require.True(t, mvs.Has(3, []byte("key3"))) + + // write some keys + vis.Set([]byte("key1"), []byte("value1")) + vis.Set([]byte("key2"), []byte("value2")) + vis.Delete([]byte("key3")) + + vis.WriteEstimatesToMultiVersionStore() + + require.True(t, mvs.GetLatest([]byte("key1")).IsEstimate()) + require.True(t, mvs.GetLatest([]byte("key2")).IsEstimate()) + require.True(t, mvs.GetLatest([]byte("key3")).IsEstimate()) +} + +func TestVersionIndexedStoreValidation(t *testing.T) { + mem := dbadapter.Store{DB: dbm.NewMemDB()} + parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) + mvs := multiversion.NewMultiVersionStore() + // initialize a new VersionIndexedStore + abortC := make(chan scheduler.Abort) + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 2, 2, abortC) + // set some initial values + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + parentKVStore.Set([]byte("deletedKey"), []byte("foo")) + mvs.Set(0, 1, []byte("key1"), []byte("value1")) + mvs.Set(0, 1, []byte("key2"), []byte("value2")) + mvs.Delete(0, 1, []byte("deletedKey")) + + // load those into readset + vis.Get([]byte("key1")) + vis.Get([]byte("key2")) + vis.Get([]byte("key4")) + vis.Get([]byte("key5")) + vis.Get([]byte("keyDNE")) + vis.Get([]byte("deletedKey")) + + // everything checks out, so we should be able to validate successfully + require.True(t, vis.ValidateReadset()) + // modify underlying transaction key that is unrelated + mvs.Set(1, 1, []byte("key3"), []byte("value3")) + // should still have valid readset + require.True(t, vis.ValidateReadset()) + + // modify underlying transaction key that is related + mvs.Set(1, 1, []byte("key1"), []byte("value1_b")) + // should now have invalid readset + require.False(t, vis.ValidateReadset()) + // reset so readset is valid again + mvs.Set(1, 1, []byte("key1"), []byte("value1")) + require.True(t, vis.ValidateReadset()) + + // mvs has a value that was initially read from parent + mvs.Set(1, 2, []byte("key4"), []byte("value4_b")) + require.False(t, vis.ValidateReadset()) + // reset key + mvs.Set(1, 2, []byte("key4"), []byte("value4")) + require.True(t, vis.ValidateReadset()) + + // mvs has a value that was initially read from parent - BUT in a later tx index + mvs.Set(4, 2, []byte("key4"), []byte("value4_c")) + // readset should remain valid + require.True(t, vis.ValidateReadset()) + + // mvs has an estimate + mvs.SetEstimate(1, 2, []byte("key2")) + // readset should be invalid now - but via abort channel write + go func() { + vis.ValidateReadset() + }() + abort := <-abortC // read the abort from the channel + require.Equal(t, 1, abort.DependentTxIdx) + + // test key deleted later + mvs.Delete(1, 1, []byte("key2")) + require.False(t, vis.ValidateReadset()) + // reset key2 + mvs.Set(1, 1, []byte("key2"), []byte("value2")) + + // lastly verify panic if parent kvstore has a conflict - this shouldn't happen but lets assert that it would panic + parentKVStore.Set([]byte("keyDNE"), []byte("foobar")) + require.Equal(t, []byte("foobar"), parentKVStore.Get([]byte("keyDNE"))) + require.Panics(t, func() { + vis.ValidateReadset() + }) +} diff --git a/types/occ/scheduler.go b/types/occ/scheduler.go new file mode 100644 index 000000000..3905be395 --- /dev/null +++ b/types/occ/scheduler.go @@ -0,0 +1,20 @@ +package scheduler + +import "errors" + +var ( + ErrReadEstimate = errors.New("multiversion store value contains estimate, cannot read, aborting") +) + +// define the return struct for abort due to conflict +type Abort struct { + DependentTxIdx int + Err error +} + +func NewEstimateAbort(dependentTxIdx int) Abort { + return Abort{ + DependentTxIdx: dependentTxIdx, + Err: ErrReadEstimate, + } +} From 0864c32171dcfd1533c8d3f152bb6e3f74a98061 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Fri, 13 Oct 2023 09:42:46 -0500 Subject: [PATCH 09/36] [occ] Add validation function for transaction state to multiversionstore (#330) ## Describe your changes and provide context This adds in validation for transaction state to multiversion store, and implements readset validation for it as well. ## Testing performed to validate your change Unit Test --- store/multiversion/mvkv_test.go | 98 +++++++++++++++++------ store/multiversion/store.go | 98 +++++++++++++++-------- store/multiversion/store_test.go | 129 ++++++++++++++++++++++++++----- 3 files changed, 249 insertions(+), 76 deletions(-) diff --git a/store/multiversion/mvkv_test.go b/store/multiversion/mvkv_test.go index df1692d1f..e17cba65c 100644 --- a/store/multiversion/mvkv_test.go +++ b/store/multiversion/mvkv_test.go @@ -15,7 +15,7 @@ import ( func TestVersionIndexedStoreGetters(t *testing.T) { mem := dbadapter.Store{DB: dbm.NewMemDB()} parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) - mvs := multiversion.NewMultiVersionStore() + mvs := multiversion.NewMultiVersionStore(parentKVStore) // initialize a new VersionIndexedStore vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 1, 2, make(chan scheduler.Abort)) @@ -41,16 +41,25 @@ func TestVersionIndexedStoreGetters(t *testing.T) { require.Equal(t, []byte("value1"), val3) // test deleted value written to MVS but not parent store - mvs.Delete(0, 2, []byte("delKey")) + mvs.SetWriteset(0, 2, map[string][]byte{ + "delKey": nil, + }) parentKVStore.Set([]byte("delKey"), []byte("value4")) valDel := vis.Get([]byte("delKey")) require.Nil(t, valDel) require.False(t, vis.Has([]byte("delKey"))) // set different key in MVS - for various indices - mvs.Set(0, 2, []byte("key3"), []byte("value3")) - mvs.Set(2, 1, []byte("key3"), []byte("value4")) - mvs.SetEstimate(5, 0, []byte("key3")) + mvs.SetWriteset(0, 2, map[string][]byte{ + "delKey": nil, + "key3": []byte("value3"), + }) + mvs.SetWriteset(2, 1, map[string][]byte{ + "key3": []byte("value4"), + }) + mvs.SetEstimatedWriteset(5, 0, map[string][]byte{ + "key3": nil, + }) // read the key that falls down to MVS val4 := vis.Get([]byte("key3")) @@ -89,7 +98,7 @@ func TestVersionIndexedStoreGetters(t *testing.T) { func TestVersionIndexedStoreSetters(t *testing.T) { mem := dbadapter.Store{DB: dbm.NewMemDB()} parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) - mvs := multiversion.NewMultiVersionStore() + mvs := multiversion.NewMultiVersionStore(parentKVStore) // initialize a new VersionIndexedStore vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 1, 2, make(chan scheduler.Abort)) @@ -97,7 +106,9 @@ func TestVersionIndexedStoreSetters(t *testing.T) { vis.Set([]byte("key1"), []byte("value1")) require.Equal(t, []byte("value1"), vis.GetWriteset()["key1"]) - mvs.Set(0, 1, []byte("key2"), []byte("value2")) + mvs.SetWriteset(0, 1, map[string][]byte{ + "key2": []byte("value2"), + }) vis.Delete([]byte("key2")) require.Nil(t, vis.Get([]byte("key2"))) // because the delete should be at the writeset level, we should not have populated the readset @@ -112,7 +123,7 @@ func TestVersionIndexedStoreSetters(t *testing.T) { func TestVersionIndexedStoreBoilerplateFunctions(t *testing.T) { mem := dbadapter.Store{DB: dbm.NewMemDB()} parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) - mvs := multiversion.NewMultiVersionStore() + mvs := multiversion.NewMultiVersionStore(parentKVStore) // initialize a new VersionIndexedStore vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 1, 2, make(chan scheduler.Abort)) @@ -129,11 +140,13 @@ func TestVersionIndexedStoreBoilerplateFunctions(t *testing.T) { func TestVersionIndexedStoreWrite(t *testing.T) { mem := dbadapter.Store{DB: dbm.NewMemDB()} parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) - mvs := multiversion.NewMultiVersionStore() + mvs := multiversion.NewMultiVersionStore(parentKVStore) // initialize a new VersionIndexedStore vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 1, 2, make(chan scheduler.Abort)) - mvs.Set(0, 1, []byte("key3"), []byte("value3")) + mvs.SetWriteset(0, 1, map[string][]byte{ + "key3": []byte("value3"), + }) require.False(t, mvs.Has(3, []byte("key1"))) require.False(t, mvs.Has(3, []byte("key2"))) @@ -154,11 +167,13 @@ func TestVersionIndexedStoreWrite(t *testing.T) { func TestVersionIndexedStoreWriteEstimates(t *testing.T) { mem := dbadapter.Store{DB: dbm.NewMemDB()} parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) - mvs := multiversion.NewMultiVersionStore() + mvs := multiversion.NewMultiVersionStore(parentKVStore) // initialize a new VersionIndexedStore vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 1, 2, make(chan scheduler.Abort)) - mvs.Set(0, 1, []byte("key3"), []byte("value3")) + mvs.SetWriteset(0, 1, map[string][]byte{ + "key3": []byte("value3"), + }) require.False(t, mvs.Has(3, []byte("key1"))) require.False(t, mvs.Has(3, []byte("key2"))) @@ -179,7 +194,7 @@ func TestVersionIndexedStoreWriteEstimates(t *testing.T) { func TestVersionIndexedStoreValidation(t *testing.T) { mem := dbadapter.Store{DB: dbm.NewMemDB()} parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) - mvs := multiversion.NewMultiVersionStore() + mvs := multiversion.NewMultiVersionStore(parentKVStore) // initialize a new VersionIndexedStore abortC := make(chan scheduler.Abort) vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 2, 2, abortC) @@ -187,9 +202,12 @@ func TestVersionIndexedStoreValidation(t *testing.T) { parentKVStore.Set([]byte("key4"), []byte("value4")) parentKVStore.Set([]byte("key5"), []byte("value5")) parentKVStore.Set([]byte("deletedKey"), []byte("foo")) - mvs.Set(0, 1, []byte("key1"), []byte("value1")) - mvs.Set(0, 1, []byte("key2"), []byte("value2")) - mvs.Delete(0, 1, []byte("deletedKey")) + + mvs.SetWriteset(0, 1, map[string][]byte{ + "key1": []byte("value1"), + "key2": []byte("value2"), + "deletedKey": nil, + }) // load those into readset vis.Get([]byte("key1")) @@ -202,32 +220,52 @@ func TestVersionIndexedStoreValidation(t *testing.T) { // everything checks out, so we should be able to validate successfully require.True(t, vis.ValidateReadset()) // modify underlying transaction key that is unrelated - mvs.Set(1, 1, []byte("key3"), []byte("value3")) + mvs.SetWriteset(1, 1, map[string][]byte{ + "key3": []byte("value3"), + }) // should still have valid readset require.True(t, vis.ValidateReadset()) // modify underlying transaction key that is related - mvs.Set(1, 1, []byte("key1"), []byte("value1_b")) + mvs.SetWriteset(1, 1, map[string][]byte{ + "key3": []byte("value3"), + "key1": []byte("value1_b"), + }) // should now have invalid readset require.False(t, vis.ValidateReadset()) // reset so readset is valid again - mvs.Set(1, 1, []byte("key1"), []byte("value1")) + mvs.SetWriteset(1, 1, map[string][]byte{ + "key3": []byte("value3"), + "key1": []byte("value1"), + }) require.True(t, vis.ValidateReadset()) // mvs has a value that was initially read from parent - mvs.Set(1, 2, []byte("key4"), []byte("value4_b")) + mvs.SetWriteset(1, 1, map[string][]byte{ + "key3": []byte("value3"), + "key1": []byte("value1"), + "key4": []byte("value4_b"), + }) require.False(t, vis.ValidateReadset()) // reset key - mvs.Set(1, 2, []byte("key4"), []byte("value4")) + mvs.SetWriteset(1, 1, map[string][]byte{ + "key3": []byte("value3"), + "key1": []byte("value1"), + "key4": []byte("value4"), + }) require.True(t, vis.ValidateReadset()) // mvs has a value that was initially read from parent - BUT in a later tx index - mvs.Set(4, 2, []byte("key4"), []byte("value4_c")) + mvs.SetWriteset(4, 2, map[string][]byte{ + "key4": []byte("value4_c"), + }) // readset should remain valid require.True(t, vis.ValidateReadset()) // mvs has an estimate - mvs.SetEstimate(1, 2, []byte("key2")) + mvs.SetEstimatedWriteset(1, 1, map[string][]byte{ + "key2": nil, + }) // readset should be invalid now - but via abort channel write go func() { vis.ValidateReadset() @@ -236,10 +274,20 @@ func TestVersionIndexedStoreValidation(t *testing.T) { require.Equal(t, 1, abort.DependentTxIdx) // test key deleted later - mvs.Delete(1, 1, []byte("key2")) + mvs.SetWriteset(1, 1, map[string][]byte{ + "key3": []byte("value3"), + "key1": []byte("value1"), + "key4": []byte("value4"), + "key2": nil, + }) require.False(t, vis.ValidateReadset()) // reset key2 - mvs.Set(1, 1, []byte("key2"), []byte("value2")) + mvs.SetWriteset(1, 1, map[string][]byte{ + "key3": []byte("value3"), + "key1": []byte("value1"), + "key4": []byte("value4"), + "key2": []byte("value2"), + }) // lastly verify panic if parent kvstore has a conflict - this shouldn't happen but lets assert that it would panic parentKVStore.Set([]byte("keyDNE"), []byte("foobar")) diff --git a/store/multiversion/store.go b/store/multiversion/store.go index 3aa4800f3..08c45204b 100644 --- a/store/multiversion/store.go +++ b/store/multiversion/store.go @@ -1,27 +1,31 @@ package multiversion import ( + "bytes" "sort" "sync" + "time" "github.com/cosmos/cosmos-sdk/store/types" + "github.com/cosmos/cosmos-sdk/telemetry" ) type MultiVersionStore interface { GetLatest(key []byte) (value MultiVersionValueItem) GetLatestBeforeIndex(index int, key []byte) (value MultiVersionValueItem) - Set(index int, incarnation int, key []byte, value []byte) // TODO: maybe we don't need these if all writes are coming from writesets - SetEstimate(index int, incarnation int, key []byte) // TODO: maybe we don't need these if all writes are coming from writesets - Delete(index int, incarnation int, key []byte) // TODO: maybe we don't need these if all writes are coming from writesets Has(index int, key []byte) bool - WriteLatestToStore(parentStore types.KVStore) + WriteLatestToStore() SetWriteset(index int, incarnation int, writeset WriteSet) InvalidateWriteset(index int, incarnation int) SetEstimatedWriteset(index int, incarnation int, writeset WriteSet) GetAllWritesetKeys() map[int][]string + SetReadset(index int, readset ReadSet) + GetReadset(index int) ReadSet + ValidateTransactionState(index int) []int } type WriteSet map[string][]byte +type ReadSet map[string][]byte var _ MultiVersionStore = (*Store)(nil) @@ -32,12 +36,17 @@ type Store struct { // TODO: do we need to support iterators as well similar to how cachekv does it - yes txWritesetKeys map[int][]string // map of tx index -> writeset keys + txReadSets map[int]ReadSet + + parentStore types.KVStore } -func NewMultiVersionStore() *Store { +func NewMultiVersionStore(parentStore types.KVStore) *Store { return &Store{ multiVersionMap: make(map[string]MultiVersionValue), txWritesetKeys: make(map[int][]string), + txReadSets: make(map[int]ReadSet), + parentStore: parentStore, } } @@ -99,16 +108,6 @@ func (s *Store) tryInitMultiVersionItem(keyString string) { } } -// Set implements MultiVersionStore. -func (s *Store) Set(index int, incarnation int, key []byte, value []byte) { - s.mtx.Lock() - defer s.mtx.Unlock() - - keyString := string(key) - s.tryInitMultiVersionItem(keyString) - s.multiVersionMap[keyString].Set(index, incarnation, value) -} - func (s *Store) removeOldWriteset(index int, newWriteSet WriteSet) { writeset := make(map[string][]byte) if newWriteSet != nil { @@ -135,6 +134,7 @@ func (s *Store) removeOldWriteset(index int, newWriteSet WriteSet) { } // SetWriteset sets a writeset for a transaction index, and also writes all of the multiversion items in the writeset to the multiversion store. +// TODO: returns a list of NEW keys added func (s *Store) SetWriteset(index int, incarnation int, writeset WriteSet) { s.mtx.Lock() defer s.mtx.Unlock() @@ -153,7 +153,7 @@ func (s *Store) SetWriteset(index int, incarnation int, writeset WriteSet) { s.multiVersionMap[key].Set(index, incarnation, value) } } - sort.Strings(writeSetKeys) + sort.Strings(writeSetKeys) // TODO: if we're sorting here anyways, maybe we just put it into a btree instead of a slice s.txWritesetKeys[index] = writeSetKeys } @@ -198,27 +198,63 @@ func (s *Store) GetAllWritesetKeys() map[int][]string { return s.txWritesetKeys } -// SetEstimate implements MultiVersionStore. -func (s *Store) SetEstimate(index int, incarnation int, key []byte) { +func (s *Store) SetReadset(index int, readset ReadSet) { s.mtx.Lock() defer s.mtx.Unlock() - keyString := string(key) - s.tryInitMultiVersionItem(keyString) - s.multiVersionMap[keyString].SetEstimate(index, incarnation) + s.txReadSets[index] = readset } -// Delete implements MultiVersionStore. -func (s *Store) Delete(index int, incarnation int, key []byte) { - s.mtx.Lock() - defer s.mtx.Unlock() +func (s *Store) GetReadset(index int) ReadSet { + s.mtx.RLock() + defer s.mtx.RUnlock() - keyString := string(key) - s.tryInitMultiVersionItem(keyString) - s.multiVersionMap[keyString].Delete(index, incarnation) + return s.txReadSets[index] +} + +func (s *Store) ValidateTransactionState(index int) []int { + defer telemetry.MeasureSince(time.Now(), "store", "mvs", "validate") + conflictSet := map[int]struct{}{} + + // validate readset + readset := s.GetReadset(index) + // iterate over readset and check if the value is the same as the latest value relateive to txIndex in the multiversion store + for key, value := range readset { + // get the latest value from the multiversion store + latestValue := s.GetLatestBeforeIndex(index, []byte(key)) + if latestValue == nil { + // TODO: maybe we don't even do this check? + parentVal := s.parentStore.Get([]byte(key)) + if !bytes.Equal(parentVal, value) { + panic("there shouldn't be readset conflicts with parent kv store, since it shouldn't change") + } + } else { + // if estimate, mark as conflict index + if latestValue.IsEstimate() { + conflictSet[latestValue.Index()] = struct{}{} + } else if latestValue.IsDeleted() { + if value != nil { + // conflict + conflictSet[latestValue.Index()] = struct{}{} + } + } else if !bytes.Equal(latestValue.Value(), value) { + conflictSet[latestValue.Index()] = struct{}{} + } + } + } + // TODO: validate iterateset + + // convert conflictset into sorted indices + conflictIndices := make([]int, 0, len(conflictSet)) + for index := range conflictSet { + conflictIndices = append(conflictIndices, index) + } + + sort.Ints(conflictIndices) + return conflictIndices } -func (s *Store) WriteLatestToStore(parentStore types.KVStore) { +func (s *Store) WriteLatestToStore() { s.mtx.Lock() defer s.mtx.Unlock() @@ -245,11 +281,11 @@ func (s *Store) WriteLatestToStore(parentStore types.KVStore) { // be sure if the underlying store might do a save with the byteslice or // not. Once we get confirmation that .Delete is guaranteed not to // save the byteslice, then we can assume only a read-only copy is sufficient. - parentStore.Delete([]byte(key)) + s.parentStore.Delete([]byte(key)) continue } if mvValue.Value() != nil { - parentStore.Set([]byte(key), mvValue.Value()) + s.parentStore.Set([]byte(key), mvValue.Value()) } } } diff --git a/store/multiversion/store_test.go b/store/multiversion/store_test.go index 732a5a6ba..bb56d1e71 100644 --- a/store/multiversion/store_test.go +++ b/store/multiversion/store_test.go @@ -10,25 +10,38 @@ import ( ) func TestMultiVersionStore(t *testing.T) { - store := multiversion.NewMultiVersionStore() + store := multiversion.NewMultiVersionStore(nil) // Test Set and GetLatest - store.Set(1, 1, []byte("key1"), []byte("value1")) - store.Set(2, 1, []byte("key1"), []byte("value2")) - store.Set(3, 1, []byte("key2"), []byte("value3")) + store.SetWriteset(1, 1, map[string][]byte{ + "key1": []byte("value1"), + }) + store.SetWriteset(2, 1, map[string][]byte{ + "key1": []byte("value2"), + }) + store.SetWriteset(3, 1, map[string][]byte{ + "key2": []byte("value3"), + }) + require.Equal(t, []byte("value2"), store.GetLatest([]byte("key1")).Value()) require.Equal(t, []byte("value3"), store.GetLatest([]byte("key2")).Value()) // Test SetEstimate - store.SetEstimate(4, 1, []byte("key1")) + store.SetEstimatedWriteset(4, 1, map[string][]byte{ + "key1": nil, + }) require.True(t, store.GetLatest([]byte("key1")).IsEstimate()) // Test Delete - store.Delete(5, 1, []byte("key1")) + store.SetWriteset(5, 1, map[string][]byte{ + "key1": nil, + }) require.True(t, store.GetLatest([]byte("key1")).IsDeleted()) // Test GetLatestBeforeIndex - store.Set(6, 1, []byte("key1"), []byte("value4")) + store.SetWriteset(6, 1, map[string][]byte{ + "key1": []byte("value4"), + }) require.True(t, store.GetLatestBeforeIndex(5, []byte("key1")).IsEstimate()) require.Equal(t, []byte("value4"), store.GetLatestBeforeIndex(7, []byte("key1")).Value()) @@ -39,16 +52,18 @@ func TestMultiVersionStore(t *testing.T) { } func TestMultiVersionStoreHasLaterValue(t *testing.T) { - store := multiversion.NewMultiVersionStore() + store := multiversion.NewMultiVersionStore(nil) - store.Set(5, 1, []byte("key1"), []byte("value2")) + store.SetWriteset(5, 1, map[string][]byte{ + "key1": []byte("value2"), + }) require.Nil(t, store.GetLatestBeforeIndex(4, []byte("key1"))) require.Equal(t, []byte("value2"), store.GetLatestBeforeIndex(6, []byte("key1")).Value()) } func TestMultiVersionStoreKeyDNE(t *testing.T) { - store := multiversion.NewMultiVersionStore() + store := multiversion.NewMultiVersionStore(nil) require.Nil(t, store.GetLatest([]byte("key1"))) require.Nil(t, store.GetLatestBeforeIndex(0, []byte("key1"))) @@ -58,18 +73,24 @@ func TestMultiVersionStoreKeyDNE(t *testing.T) { func TestMultiVersionStoreWriteToParent(t *testing.T) { // initialize cachekv store parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} - mvs := multiversion.NewMultiVersionStore() + mvs := multiversion.NewMultiVersionStore(parentKVStore) parentKVStore.Set([]byte("key2"), []byte("value0")) parentKVStore.Set([]byte("key4"), []byte("value4")) - mvs.Set(1, 1, []byte("key1"), []byte("value1")) - mvs.Set(2, 1, []byte("key1"), []byte("value2")) - mvs.Set(3, 1, []byte("key2"), []byte("value3")) - mvs.Delete(1, 1, []byte("key3")) - mvs.Delete(1, 1, []byte("key4")) + mvs.SetWriteset(1, 1, map[string][]byte{ + "key1": []byte("value1"), + "key3": nil, + "key4": nil, + }) + mvs.SetWriteset(2, 1, map[string][]byte{ + "key1": []byte("value2"), + }) + mvs.SetWriteset(3, 1, map[string][]byte{ + "key2": []byte("value3"), + }) - mvs.WriteLatestToStore(parentKVStore) + mvs.WriteLatestToStore() // assert state in parent store require.Equal(t, []byte("value2"), parentKVStore.Get([]byte("key1"))) @@ -78,13 +99,18 @@ func TestMultiVersionStoreWriteToParent(t *testing.T) { require.False(t, parentKVStore.Has([]byte("key4"))) // verify no-op if mvs contains ESTIMATE - mvs.SetEstimate(1, 2, []byte("key5")) - mvs.WriteLatestToStore(parentKVStore) + mvs.SetEstimatedWriteset(1, 2, map[string][]byte{ + "key1": []byte("value1"), + "key3": nil, + "key4": nil, + "key5": nil, + }) + mvs.WriteLatestToStore() require.False(t, parentKVStore.Has([]byte("key5"))) } func TestMultiVersionStoreWritesetSetAndInvalidate(t *testing.T) { - mvs := multiversion.NewMultiVersionStore() + mvs := multiversion.NewMultiVersionStore(nil) writeset := make(map[string][]byte) writeset["key1"] = []byte("value1") @@ -140,3 +166,66 @@ func TestMultiVersionStoreWritesetSetAndInvalidate(t *testing.T) { require.Equal(t, []string{"key4", "key5"}, writesetKeys[3]) } + +func TestMultiVersionStoreValidateState(t *testing.T) { + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore(parentKVStore) + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key3"), []byte("value3")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + + writeset := make(multiversion.WriteSet) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + mvs.SetWriteset(1, 2, writeset) + + readset := make(multiversion.ReadSet) + readset["key1"] = []byte("value1") + readset["key2"] = []byte("value2") + readset["key3"] = nil + readset["key4"] = []byte("value4") + readset["key5"] = []byte("value5") + mvs.SetReadset(5, readset) + + // assert no readset is valid + conflicts := mvs.ValidateTransactionState(4) + require.Empty(t, conflicts) + + // assert readset index 5 is valid + conflicts = mvs.ValidateTransactionState(5) + require.Empty(t, conflicts) + + // introduce conflict + mvs.SetWriteset(2, 1, map[string][]byte{ + "key3": []byte("value6"), + }) + + // expect index 2 to be returned + conflicts = mvs.ValidateTransactionState(5) + require.Equal(t, []int{2}, conflicts) + + // add a conflict due to deletion + mvs.SetWriteset(3, 1, map[string][]byte{ + "key1": nil, + }) + + // expect indices 2 and 3 to be returned + conflicts = mvs.ValidateTransactionState(5) + require.Equal(t, []int{2, 3}, conflicts) + + // add a conflict due to estimate + mvs.SetEstimatedWriteset(4, 1, map[string][]byte{ + "key2": []byte("test"), + }) + + // expect indices 2, 3, and 4to be returned + conflicts = mvs.ValidateTransactionState(5) + require.Equal(t, []int{2, 3, 4}, conflicts) + + // assert panic for parent store mismatch + parentKVStore.Set([]byte("key5"), []byte("value6")) + require.Panics(t, func() { mvs.ValidateTransactionState(5) }) +} From de92cbc5aca9da094bef6640cff1870cd3ddc59b Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Tue, 17 Oct 2023 10:20:41 -0400 Subject: [PATCH 10/36] [occ] Add basic worker task and scheduler shell (#328) ## Describe your changes and provide context - Adds a basic scheduler shell (see TODOs) - Adds a basic task definition with request/response/index - Listens to abort channel after an execution to determine conflict ## Testing performed to validate your change - Compiles (holding off until shape is validated) - Basic Unit Test for ProcessAll --- baseapp/abci.go | 25 +++- tasks/scheduler.go | 187 +++++++++++++++++++++++++++ tasks/scheduler_test.go | 59 +++++++++ types/occ/{scheduler.go => types.go} | 8 +- 4 files changed, 269 insertions(+), 10 deletions(-) create mode 100644 tasks/scheduler.go create mode 100644 tasks/scheduler_test.go rename types/occ/{scheduler.go => types.go} (76%) diff --git a/baseapp/abci.go b/baseapp/abci.go index 1274f5d6e..84a74a247 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/cosmos/cosmos-sdk/tasks" "os" "sort" "strings" @@ -238,13 +239,23 @@ func (app *BaseApp) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abc // DeliverTxBatch executes multiple txs // TODO: support occ logic with scheduling func (app *BaseApp) DeliverTxBatch(ctx sdk.Context, req sdk.DeliverTxBatchRequest) (res sdk.DeliverTxBatchResponse) { - // TODO: replace with actual scheduler logic - // This is stubbed so that it does something sensible - responses := make([]*sdk.DeliverTxResult, 0, len(req.TxEntries)) + //TODO: inject multiversion store without import cycle (figure out right place for this) + // ctx = ctx.WithMultiVersionStore(multiversion.NewMultiVersionStore()) + + reqList := make([]abci.RequestDeliverTx, 0, len(req.TxEntries)) for _, tx := range req.TxEntries { - responses = append(responses, &sdk.DeliverTxResult{ - Response: app.DeliverTx(ctx, tx.Request), - }) + reqList = append(reqList, tx.Request) + } + + scheduler := tasks.NewScheduler(app.concurrencyWorkers, app.DeliverTx) + txRes, err := scheduler.ProcessAll(ctx, reqList) + if err != nil { + //TODO: handle error + } + + responses := make([]*sdk.DeliverTxResult, 0, len(req.TxEntries)) + for _, tx := range txRes { + responses = append(responses, &sdk.DeliverTxResult{Response: tx}) } return sdk.DeliverTxBatchResponse{Results: responses} } @@ -255,7 +266,7 @@ func (app *BaseApp) DeliverTxBatch(ctx sdk.Context, req sdk.DeliverTxBatchReques // Regardless of tx execution outcome, the ResponseDeliverTx will contain relevant // gas execution context. // TODO: (occ) this is the function called from sei-chain to perform execution of a transaction. -// We'd likely replace this with an execution task that is scheduled by the OCC scheduler +// We'd likely replace this with an execution tasks that is scheduled by the OCC scheduler func (app *BaseApp) DeliverTx(ctx sdk.Context, req abci.RequestDeliverTx) (res abci.ResponseDeliverTx) { defer telemetry.MeasureSince(time.Now(), "abci", "deliver_tx") defer func() { diff --git a/tasks/scheduler.go b/tasks/scheduler.go new file mode 100644 index 000000000..c8b063fe2 --- /dev/null +++ b/tasks/scheduler.go @@ -0,0 +1,187 @@ +package tasks + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/tendermint/tendermint/abci/types" + "golang.org/x/sync/errgroup" +) + +type status string + +const ( + // statusPending tasks are ready for execution + // all executing tasks are in pending state + statusPending status = "pending" + // statusExecuted tasks are ready for validation + // these tasks did not abort during execution + statusExecuted status = "executed" + // statusAborted means the task has been aborted + // these tasks transition to pending upon next execution + statusAborted status = "aborted" + // statusValidated means the task has been validated + // tasks in this status can be reset if an earlier task fails validation + statusValidated status = "validated" +) + +type deliverTxTask struct { + Status status + Index int + Incarnation int + Request types.RequestDeliverTx + Response *types.ResponseDeliverTx +} + +// Scheduler processes tasks concurrently +type Scheduler interface { + ProcessAll(ctx sdk.Context, reqs []types.RequestDeliverTx) ([]types.ResponseDeliverTx, error) +} + +type scheduler struct { + deliverTx func(ctx sdk.Context, req types.RequestDeliverTx) (res types.ResponseDeliverTx) + workers int +} + +// NewScheduler creates a new scheduler +func NewScheduler(workers int, deliverTxFunc func(ctx sdk.Context, req types.RequestDeliverTx) (res types.ResponseDeliverTx)) Scheduler { + return &scheduler{ + workers: workers, + deliverTx: deliverTxFunc, + } +} + +func toTasks(reqs []types.RequestDeliverTx) []*deliverTxTask { + res := make([]*deliverTxTask, 0, len(reqs)) + for idx, r := range reqs { + res = append(res, &deliverTxTask{ + Request: r, + Index: idx, + Status: statusPending, + }) + } + return res +} + +func collectResponses(tasks []*deliverTxTask) []types.ResponseDeliverTx { + res := make([]types.ResponseDeliverTx, 0, len(tasks)) + for _, t := range tasks { + res = append(res, *t.Response) + } + return res +} + +func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []types.RequestDeliverTx) ([]types.ResponseDeliverTx, error) { + tasks := toTasks(reqs) + toExecute := tasks + for len(toExecute) > 0 { + + // execute sets statuses of tasks to either executed or aborted + err := s.executeAll(ctx, toExecute) + if err != nil { + return nil, err + } + + // validate returns any that should be re-executed + // note this processes ALL tasks, not just those recently executed + toExecute, err = s.validateAll(ctx, tasks) + if err != nil { + return nil, err + } + for _, t := range toExecute { + t.Incarnation++ + t.Status = statusPending + t.Response = nil + //TODO: reset anything that needs resetting + } + } + return collectResponses(tasks), nil +} + +// TODO: validate each tasks +// TODO: return list of tasks that are invalid +func (s *scheduler) validateAll(ctx sdk.Context, tasks []*deliverTxTask) ([]*deliverTxTask, error) { + var res []*deliverTxTask + + // find first non-validated entry + var startIdx int + for idx, t := range tasks { + if t.Status != statusValidated { + startIdx = idx + break + } + } + + for i := startIdx; i < len(tasks); i++ { + // any aborted tx is known to be suspect here + if tasks[i].Status == statusAborted { + res = append(res, tasks[i]) + } else { + //TODO: validate the tasks and add it if invalid + //TODO: create and handle abort for validation + tasks[i].Status = statusValidated + } + } + return res, nil +} + +// ExecuteAll executes all tasks concurrently +// Tasks are updated with their status +// TODO: retries on aborted tasks +// TODO: error scenarios +func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { + ch := make(chan *deliverTxTask, len(tasks)) + grp, gCtx := errgroup.WithContext(ctx.Context()) + + // a workers value < 1 means no limit + workers := s.workers + if s.workers < 1 { + workers = len(tasks) + } + + for i := 0; i < workers; i++ { + grp.Go(func() error { + for { + select { + case <-gCtx.Done(): + return gCtx.Err() + case task, ok := <-ch: + if !ok { + return nil + } + //TODO: ensure version multi store is on context + // buffered so it doesn't block on write + // abortCh := make(chan occ.Abort, 1) + + //TODO: consume from abort in non-blocking way (give it a length) + resp := s.deliverTx(ctx, task.Request) + + // close(abortCh) + + //if _, ok := <-abortCh; ok { + // tasks.status = TaskStatusAborted + // continue + //} + + task.Status = statusExecuted + task.Response = &resp + } + } + }) + } + grp.Go(func() error { + defer close(ch) + for _, task := range tasks { + select { + case <-gCtx.Done(): + return gCtx.Err() + case ch <- task: + } + } + return nil + }) + + if err := grp.Wait(); err != nil { + return err + } + + return nil +} diff --git a/tasks/scheduler_test.go b/tasks/scheduler_test.go new file mode 100644 index 000000000..ba9d97846 --- /dev/null +++ b/tasks/scheduler_test.go @@ -0,0 +1,59 @@ +package tasks + +import ( + "context" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/assert" + "github.com/tendermint/tendermint/abci/types" + "testing" +) + +type mockDeliverTxFunc func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx + +func (f mockDeliverTxFunc) DeliverTx(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { + return f(ctx, req) +} + +func requestList(n int) []types.RequestDeliverTx { + tasks := make([]types.RequestDeliverTx, n) + for i := 0; i < n; i++ { + tasks[i] = types.RequestDeliverTx{} + } + return tasks +} + +func TestProcessAll(t *testing.T) { + tests := []struct { + name string + workers int + requests []types.RequestDeliverTx + deliverTxFunc mockDeliverTxFunc + expectedErr error + }{ + { + name: "All tasks processed without aborts", + workers: 2, + requests: requestList(5), + deliverTxFunc: func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { + return types.ResponseDeliverTx{} + }, + expectedErr: nil, + }, + //TODO: Add more test cases + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := NewScheduler(tt.workers, tt.deliverTxFunc.DeliverTx) + ctx := sdk.Context{}.WithContext(context.Background()) + + res, err := s.ProcessAll(ctx, tt.requests) + if err != tt.expectedErr { + t.Errorf("Expected error %v, got %v", tt.expectedErr, err) + } else { + // response for each request exists + assert.Len(t, res, len(tt.requests)) + } + }) + } +} diff --git a/types/occ/scheduler.go b/types/occ/types.go similarity index 76% rename from types/occ/scheduler.go rename to types/occ/types.go index 3905be395..de321b7cb 100644 --- a/types/occ/scheduler.go +++ b/types/occ/types.go @@ -1,12 +1,14 @@ -package scheduler +package occ -import "errors" +import ( + "errors" +) var ( ErrReadEstimate = errors.New("multiversion store value contains estimate, cannot read, aborting") ) -// define the return struct for abort due to conflict +// Abort contains the information for a transaction's conflict type Abort struct { DependentTxIdx int Err error From 6406dd910796040b93042bd99475d6fba11dfb94 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Tue, 17 Oct 2023 13:15:23 -0500 Subject: [PATCH 11/36] [occ] Implement iterator for mvkv (#329) ## Describe your changes and provide context This implements Iterator and ReverseIterator for mvkv for the KVStore interface. The memiterator will be composed of versionindexedstore and multiversionstore, and will yield values in a cascading fashion firstly from the writeset, and then second from the multiversion store. This still needs optimization to persisted sorted keys instead of reconstructing sorted keys each time. ## Testing performed to validate your change Unit test to verify basic functionality --- store/multiversion/memiterator.go | 74 ++++++++ store/multiversion/mergeiterator.go | 256 ++++++++++++++++++++++++++++ store/multiversion/mvkv.go | 68 ++++++-- store/multiversion/mvkv_test.go | 74 ++++++++ 4 files changed, 457 insertions(+), 15 deletions(-) create mode 100644 store/multiversion/memiterator.go create mode 100644 store/multiversion/mergeiterator.go diff --git a/store/multiversion/memiterator.go b/store/multiversion/memiterator.go new file mode 100644 index 000000000..a3fb74323 --- /dev/null +++ b/store/multiversion/memiterator.go @@ -0,0 +1,74 @@ +package multiversion + +import ( + dbm "github.com/tendermint/tm-db" + + "github.com/cosmos/cosmos-sdk/store/types" + scheduler "github.com/cosmos/cosmos-sdk/types/occ" +) + +// Iterates over iterKVCache items. +// if key is nil, means it was deleted. +// Implements Iterator. +type memIterator struct { + types.Iterator + + mvStore MultiVersionStore + writeset map[string][]byte + index int + abortChannel chan scheduler.Abort +} + +func (store *VersionIndexedStore) newMemIterator( + start, end []byte, + items *dbm.MemDB, + ascending bool, +) *memIterator { + var iter types.Iterator + var err error + + if ascending { + iter, err = items.Iterator(start, end) + } else { + iter, err = items.ReverseIterator(start, end) + } + + if err != nil { + if iter != nil { + iter.Close() + } + panic(err) + } + + return &memIterator{ + Iterator: iter, + mvStore: store.multiVersionStore, + index: store.transactionIndex, + abortChannel: store.abortChannel, + writeset: store.GetWriteset(), + } +} + +// try to get value from the writeset, otherwise try to get from multiversion store, otherwise try to get from parent iterator +func (mi *memIterator) Value() []byte { + key := mi.Iterator.Key() + + // try fetch from writeset - return if exists + if val, ok := mi.writeset[string(key)]; ok { + return val + } + + // get the value from the multiversion store + val := mi.mvStore.GetLatestBeforeIndex(mi.index, key) + + // if we have an estiamte, write to abort channel + if val.IsEstimate() { + mi.abortChannel <- scheduler.NewEstimateAbort(val.Index()) + } + + // if we have a deleted value, return nil + if val.IsDeleted() { + return nil + } + return val.Value() +} diff --git a/store/multiversion/mergeiterator.go b/store/multiversion/mergeiterator.go new file mode 100644 index 000000000..c0a9d23ef --- /dev/null +++ b/store/multiversion/mergeiterator.go @@ -0,0 +1,256 @@ +package multiversion + +import ( + "bytes" + "errors" + + "github.com/cosmos/cosmos-sdk/store/types" +) + +// mvsMergeIterator merges a parent Iterator and a cache Iterator. +// The cache iterator may return nil keys to signal that an item +// had been deleted (but not deleted in the parent). +// If the cache iterator has the same key as the parent, the +// cache shadows (overrides) the parent. +type mvsMergeIterator struct { + parent types.Iterator + cache types.Iterator + ascending bool +} + +var _ types.Iterator = (*mvsMergeIterator)(nil) + +func NewMVSMergeIterator( + parent, cache types.Iterator, + ascending bool, +) *mvsMergeIterator { + iter := &mvsMergeIterator{ + parent: parent, + cache: cache, + ascending: ascending, + } + + return iter +} + +// Domain implements Iterator. +// It returns the union of the iter.Parent doman, and the iter.Cache domain. +// If the domains are disjoint, this includes the domain in between them as well. +func (iter *mvsMergeIterator) Domain() (start, end []byte) { + startP, endP := iter.parent.Domain() + startC, endC := iter.cache.Domain() + + if iter.compare(startP, startC) < 0 { + start = startP + } else { + start = startC + } + + if iter.compare(endP, endC) < 0 { + end = endC + } else { + end = endP + } + + return start, end +} + +// Valid implements Iterator. +func (iter *mvsMergeIterator) Valid() bool { + return iter.skipUntilExistsOrInvalid() +} + +// Next implements Iterator +func (iter *mvsMergeIterator) Next() { + iter.skipUntilExistsOrInvalid() + iter.assertValid() + + // If parent is invalid, get the next cache item. + if !iter.parent.Valid() { + iter.cache.Next() + return + } + + // If cache is invalid, get the next parent item. + if !iter.cache.Valid() { + iter.parent.Next() + return + } + + // Both are valid. Compare keys. + keyP, keyC := iter.parent.Key(), iter.cache.Key() + switch iter.compare(keyP, keyC) { + case -1: // parent < cache + iter.parent.Next() + case 0: // parent == cache + iter.parent.Next() + iter.cache.Next() + case 1: // parent > cache + iter.cache.Next() + } +} + +// Key implements Iterator +func (iter *mvsMergeIterator) Key() []byte { + iter.skipUntilExistsOrInvalid() + iter.assertValid() + + // If parent is invalid, get the cache key. + if !iter.parent.Valid() { + return iter.cache.Key() + } + + // If cache is invalid, get the parent key. + if !iter.cache.Valid() { + return iter.parent.Key() + } + + // Both are valid. Compare keys. + keyP, keyC := iter.parent.Key(), iter.cache.Key() + + cmp := iter.compare(keyP, keyC) + switch cmp { + case -1: // parent < cache + return keyP + case 0: // parent == cache + return keyP + case 1: // parent > cache + return keyC + default: + panic("invalid compare result") + } +} + +// Value implements Iterator +func (iter *mvsMergeIterator) Value() []byte { + iter.skipUntilExistsOrInvalid() + iter.assertValid() + + // If parent is invalid, get the cache value. + if !iter.parent.Valid() { + value := iter.cache.Value() + return value + } + + // If cache is invalid, get the parent value. + if !iter.cache.Valid() { + value := iter.parent.Value() + return value + } + + // Both are valid. Compare keys. + keyP, keyC := iter.parent.Key(), iter.cache.Key() + + cmp := iter.compare(keyP, keyC) + switch cmp { + case -1: // parent < cache + value := iter.parent.Value() + return value + case 0, 1: // parent >= cache + value := iter.cache.Value() + return value + default: + panic("invalid comparison result") + } +} + +// Close implements Iterator +func (iter *mvsMergeIterator) Close() error { + if err := iter.parent.Close(); err != nil { + // still want to close cache iterator regardless + iter.cache.Close() + return err + } + + return iter.cache.Close() +} + +// Error returns an error if the mvsMergeIterator is invalid defined by the +// Valid method. +func (iter *mvsMergeIterator) Error() error { + if !iter.Valid() { + return errors.New("invalid mvsMergeIterator") + } + + return nil +} + +// If not valid, panics. +// NOTE: May have side-effect of iterating over cache. +func (iter *mvsMergeIterator) assertValid() { + if err := iter.Error(); err != nil { + panic(err) + } +} + +// Like bytes.Compare but opposite if not ascending. +func (iter *mvsMergeIterator) compare(a, b []byte) int { + if iter.ascending { + return bytes.Compare(a, b) + } + + return bytes.Compare(a, b) * -1 +} + +// Skip all delete-items from the cache w/ `key < until`. After this function, +// current cache item is a non-delete-item, or `until <= key`. +// If the current cache item is not a delete item, does nothing. +// If `until` is nil, there is no limit, and cache may end up invalid. +// CONTRACT: cache is valid. +func (iter *mvsMergeIterator) skipCacheDeletes(until []byte) { + for iter.cache.Valid() && + iter.cache.Value() == nil && + (until == nil || iter.compare(iter.cache.Key(), until) < 0) { + iter.cache.Next() + } +} + +// Fast forwards cache (or parent+cache in case of deleted items) until current +// item exists, or until iterator becomes invalid. +// Returns whether the iterator is valid. +func (iter *mvsMergeIterator) skipUntilExistsOrInvalid() bool { + for { + // If parent is invalid, fast-forward cache. + if !iter.parent.Valid() { + iter.skipCacheDeletes(nil) + return iter.cache.Valid() + } + // Parent is valid. + if !iter.cache.Valid() { + return true + } + // Parent is valid, cache is valid. + + // Compare parent and cache. + keyP := iter.parent.Key() + keyC := iter.cache.Key() + + switch iter.compare(keyP, keyC) { + case -1: // parent < cache. + return true + + case 0: // parent == cache. + // Skip over if cache item is a delete. + valueC := iter.cache.Value() + if valueC == nil { + iter.parent.Next() + iter.cache.Next() + + continue + } + // Cache is not a delete. + + return true // cache exists. + case 1: // cache < parent + // Skip over if cache item is a delete. + valueC := iter.cache.Value() + if valueC == nil { + iter.skipCacheDeletes(keyP) + continue + } + // Cache is not a delete. + + return true // cache exists. + } + } +} diff --git a/store/multiversion/mvkv.go b/store/multiversion/mvkv.go index 697561355..b96338c1e 100644 --- a/store/multiversion/mvkv.go +++ b/store/multiversion/mvkv.go @@ -20,8 +20,6 @@ type VersionIndexedStore struct { writeset map[string][]byte // contains the key -> value mapping for all keys written to the store // TODO: need to add iterateset here as well - // TODO: do we need this? - I think so? / maybe we just treat `nil` value in the writeset as a delete - deleted *sync.Map // dirty keys that haven't been sorted yet for iteration dirtySet map[string]struct{} // used for iterators - populated at the time of iterator instantiation @@ -43,7 +41,6 @@ func NewVersionIndexedStore(parent types.KVStore, multiVersionStore MultiVersion return &VersionIndexedStore{ readset: make(map[string][]byte), writeset: make(map[string][]byte), - deleted: &sync.Map{}, dirtySet: make(map[string]struct{}), sortedStore: dbm.NewMemDB(), parent: parent, @@ -191,12 +188,63 @@ func (store *VersionIndexedStore) Set(key []byte, value []byte) { // Iterator implements types.KVStore. func (v *VersionIndexedStore) Iterator(start []byte, end []byte) dbm.Iterator { - panic("unimplemented") + return v.iterator(start, end, true) } // ReverseIterator implements types.KVStore. func (v *VersionIndexedStore) ReverseIterator(start []byte, end []byte) dbm.Iterator { - panic("unimplemented") + return v.iterator(start, end, false) +} + +// TODO: still needs iterateset tracking +// Iterator implements types.KVStore. +func (store *VersionIndexedStore) iterator(start []byte, end []byte, ascending bool) dbm.Iterator { + store.mtx.Lock() + defer store.mtx.Unlock() + // TODO: ideally we persist writeset keys into a sorted btree for later use + // make a set of total keys across mvkv and mvs to iterate + keysToIterate := make(map[string]struct{}) + for key := range store.writeset { + keysToIterate[key] = struct{}{} + } + + // TODO: ideally we take advantage of mvs keys already being sorted + // get the multiversion store sorted keys + writesetMap := store.multiVersionStore.GetAllWritesetKeys() + for i := 0; i < store.transactionIndex; i++ { + // add all the writesets keys up until current index + for _, key := range writesetMap[i] { + keysToIterate[key] = struct{}{} + } + } + // TODO: ideally merge btree and mvs keys into a single sorted btree + + // TODO: this is horribly inefficient, fix this + sortedKeys := make([]string, len(keysToIterate)) + for key := range keysToIterate { + sortedKeys = append(sortedKeys, key) + } + sort.Strings(sortedKeys) + + memDB := dbm.NewMemDB() + for _, key := range sortedKeys { + memDB.Set([]byte(key), []byte{}) + } + + var parent, memIterator types.Iterator + + // make a memIterator + memIterator = store.newMemIterator(start, end, memDB, ascending) + + if ascending { + parent = store.parent.Iterator(start, end) + } else { + parent = store.parent.ReverseIterator(start, end) + } + + // mergeIterator + return NewMVSMergeIterator(parent, memIterator, ascending) + } // GetStoreType implements types.KVStore. @@ -230,11 +278,6 @@ func (store *VersionIndexedStore) setValue(key, value []byte, deleted bool, dirt keyStr := string(key) store.writeset[keyStr] = value - if deleted { - store.deleted.Store(keyStr, struct{}{}) - } else { - store.deleted.Delete(keyStr) - } if dirty { store.dirtySet[keyStr] = struct{}{} } @@ -261,8 +304,3 @@ func (store *VersionIndexedStore) updateReadSet(key []byte, value []byte) { // add to dirty set store.dirtySet[keyStr] = struct{}{} } - -func (store *VersionIndexedStore) isDeleted(key string) bool { - _, ok := store.deleted.Load(key) - return ok -} diff --git a/store/multiversion/mvkv_test.go b/store/multiversion/mvkv_test.go index e17cba65c..23bcc472a 100644 --- a/store/multiversion/mvkv_test.go +++ b/store/multiversion/mvkv_test.go @@ -296,3 +296,77 @@ func TestVersionIndexedStoreValidation(t *testing.T) { vis.ValidateReadset() }) } + +func TestIterator(t *testing.T) { + mem := dbadapter.Store{DB: dbm.NewMemDB()} + parentKVStore := cachekv.NewStore(mem, types.NewKVStoreKey("mock"), 1000) + mvs := multiversion.NewMultiVersionStore(parentKVStore) + // initialize a new VersionIndexedStore + abortC := make(chan scheduler.Abort) + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 2, 2, abortC) + + // set some initial values + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + parentKVStore.Set([]byte("deletedKey"), []byte("foo")) + mvs.SetWriteset(0, 1, map[string][]byte{ + "key1": []byte("value1"), + "key2": []byte("value2"), + "deletedKey": nil, + }) + // add an estimate to MVS + mvs.SetEstimatedWriteset(3, 1, map[string][]byte{ + "key3": []byte("value1_b"), + }) + + // iterate over the keys - exclusive on key5 + iter := vis.Iterator([]byte("000"), []byte("key5")) + vals := []string{} + defer iter.Close() + for ; iter.Valid(); iter.Next() { + vals = append(vals, string(iter.Value())) + } + require.Equal(t, []string{"value1", "value2", "value4"}, vals) + iter.Close() + + // test reverse iteration + vals2 := []string{} + iter2 := vis.ReverseIterator([]byte("000"), []byte("key6")) + defer iter2.Close() + for ; iter2.Valid(); iter2.Next() { + vals2 = append(vals2, string(iter2.Value())) + } + // has value5 because of end being key6 + require.Equal(t, []string{"value5", "value4", "value2", "value1"}, vals2) + iter2.Close() + + // add items to writeset + vis.Set([]byte("key3"), []byte("value3")) + vis.Set([]byte("key4"), []byte("valueNew")) + + // iterate over the keys - exclusive on key5 + iter3 := vis.Iterator([]byte("000"), []byte("key5")) + vals3 := []string{} + defer iter3.Close() + for ; iter3.Valid(); iter3.Next() { + vals3 = append(vals3, string(iter3.Value())) + } + require.Equal(t, []string{"value1", "value2", "value3", "valueNew"}, vals3) + iter3.Close() + + // add an estimate to MVS + mvs.SetEstimatedWriteset(1, 1, map[string][]byte{ + "key2": []byte("value1_b"), + }) + + go func() { + // new iter + iter4 := vis.Iterator([]byte("000"), []byte("key5")) + defer iter4.Close() + for ; iter4.Valid(); iter4.Next() { + } + }() + abort := <-abortC // read the abort from the channel + require.Equal(t, 1, abort.DependentTxIdx) + +} From f6b0b9fac1dd492809850df24b1feb6c895dbdc3 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Tue, 17 Oct 2023 13:52:21 -0500 Subject: [PATCH 12/36] fix dependency (#334) ## Describe your changes and provide context This fixes a dependency that was refactored, and enables commit push CI for occ-main ## Testing performed to validate your change CI --- .github/workflows/test.yml | 1 + store/multiversion/memiterator.go | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 61fe636ac..160148f78 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,6 +6,7 @@ on: push: branches: - main + - occ-main # TODO: remove after occ work is done permissions: contents: read diff --git a/store/multiversion/memiterator.go b/store/multiversion/memiterator.go index a3fb74323..17ffdd0d6 100644 --- a/store/multiversion/memiterator.go +++ b/store/multiversion/memiterator.go @@ -4,7 +4,7 @@ import ( dbm "github.com/tendermint/tm-db" "github.com/cosmos/cosmos-sdk/store/types" - scheduler "github.com/cosmos/cosmos-sdk/types/occ" + occtypes "github.com/cosmos/cosmos-sdk/types/occ" ) // Iterates over iterKVCache items. @@ -16,7 +16,7 @@ type memIterator struct { mvStore MultiVersionStore writeset map[string][]byte index int - abortChannel chan scheduler.Abort + abortChannel chan occtypes.Abort } func (store *VersionIndexedStore) newMemIterator( @@ -63,7 +63,7 @@ func (mi *memIterator) Value() []byte { // if we have an estiamte, write to abort channel if val.IsEstimate() { - mi.abortChannel <- scheduler.NewEstimateAbort(val.Index()) + mi.abortChannel <- occtypes.NewEstimateAbort(val.Index()) } // if we have a deleted value, return nil From f3a6cf4989a15fe3794e6c2f3887c2b6c03447d0 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Thu, 19 Oct 2023 09:51:08 -0500 Subject: [PATCH 13/36] [occ] Iterateset tracking and validation implementation (#337) ## Describe your changes and provide context This implements a tracked iterator that is used to keep track of keys that have been iterated, and to also save metadata about the iteration for LATER validation. The iterator will be replayed and if there are any new keys / any keys missing within the iteration range, it will fail validation. the actual values served by the iterator are covered by readset validation. Additionally, the early stop behavior allows the iterateset to ONLY be sensitive to changes to the keys available WITHIN the iteration range. In the event that we perform iteration, and THEN write a key within the range of iteration, this will not fail iteration because we take a snapshot of the mvkv writeset at the moment of iteration, so when we replay the iterator, we populate that iterator with the writeset at that time, so we appropriately replicate the iterator behavior. In the case that we encounter an ESTIMATE, we have to terminate the iterator validation and mark it as failed because it is impossible to know whether that ESTIMATE represents a value change or a delete, since the latter, will affect the keys available for iteration. This change also implements handlers that iterators receive for updating readset and iterateset in the `mvkv` ## Testing performed to validate your change Unit tests for various iteration scenarios --- store/multiversion/memiterator.go | 52 +++- store/multiversion/mergeiterator.go | 9 +- store/multiversion/mvkv.go | 116 ++++++-- store/multiversion/mvkv_test.go | 17 ++ store/multiversion/store.go | 120 +++++++- store/multiversion/store_test.go | 392 +++++++++++++++++++++++++- store/multiversion/trackediterator.go | 57 ++++ 7 files changed, 706 insertions(+), 57 deletions(-) create mode 100644 store/multiversion/trackediterator.go diff --git a/store/multiversion/memiterator.go b/store/multiversion/memiterator.go index 17ffdd0d6..43e8e306b 100644 --- a/store/multiversion/memiterator.go +++ b/store/multiversion/memiterator.go @@ -14,15 +14,17 @@ type memIterator struct { types.Iterator mvStore MultiVersionStore - writeset map[string][]byte + writeset WriteSet index int abortChannel chan occtypes.Abort + ReadsetHandler } func (store *VersionIndexedStore) newMemIterator( start, end []byte, items *dbm.MemDB, ascending bool, + readsetHandler ReadsetHandler, ) *memIterator { var iter types.Iterator var err error @@ -41,11 +43,12 @@ func (store *VersionIndexedStore) newMemIterator( } return &memIterator{ - Iterator: iter, - mvStore: store.multiVersionStore, - index: store.transactionIndex, - abortChannel: store.abortChannel, - writeset: store.GetWriteset(), + Iterator: iter, + mvStore: store.multiVersionStore, + index: store.transactionIndex, + abortChannel: store.abortChannel, + writeset: store.GetWriteset(), + ReadsetHandler: readsetHandler, } } @@ -66,9 +69,46 @@ func (mi *memIterator) Value() []byte { mi.abortChannel <- occtypes.NewEstimateAbort(val.Index()) } + // need to update readset // if we have a deleted value, return nil if val.IsDeleted() { + defer mi.ReadsetHandler.UpdateReadSet(key, nil) return nil } + defer mi.ReadsetHandler.UpdateReadSet(key, val.Value()) return val.Value() } + +func (store *Store) newMVSValidationIterator( + index int, + start, end []byte, + items *dbm.MemDB, + ascending bool, + writeset WriteSet, + abortChannel chan occtypes.Abort, +) *memIterator { + var iter types.Iterator + var err error + + if ascending { + iter, err = items.Iterator(start, end) + } else { + iter, err = items.ReverseIterator(start, end) + } + + if err != nil { + if iter != nil { + iter.Close() + } + panic(err) + } + + return &memIterator{ + Iterator: iter, + mvStore: store, + index: index, + abortChannel: abortChannel, + ReadsetHandler: NoOpHandler{}, + writeset: writeset, + } +} diff --git a/store/multiversion/mergeiterator.go b/store/multiversion/mergeiterator.go index c0a9d23ef..3b5cee741 100644 --- a/store/multiversion/mergeiterator.go +++ b/store/multiversion/mergeiterator.go @@ -16,6 +16,7 @@ type mvsMergeIterator struct { parent types.Iterator cache types.Iterator ascending bool + ReadsetHandler } var _ types.Iterator = (*mvsMergeIterator)(nil) @@ -23,11 +24,13 @@ var _ types.Iterator = (*mvsMergeIterator)(nil) func NewMVSMergeIterator( parent, cache types.Iterator, ascending bool, + readsetHandler ReadsetHandler, ) *mvsMergeIterator { iter := &mvsMergeIterator{ - parent: parent, - cache: cache, - ascending: ascending, + parent: parent, + cache: cache, + ascending: ascending, + ReadsetHandler: readsetHandler, } return iter diff --git a/store/multiversion/mvkv.go b/store/multiversion/mvkv.go index b96338c1e..1b2f947c1 100644 --- a/store/multiversion/mvkv.go +++ b/store/multiversion/mvkv.go @@ -12,12 +12,70 @@ import ( dbm "github.com/tendermint/tm-db" ) +// exposes a handler for adding items to readset, useful for iterators +type ReadsetHandler interface { + UpdateReadSet(key []byte, value []byte) +} + +type NoOpHandler struct{} + +func (NoOpHandler) UpdateReadSet(key []byte, value []byte) {} + +// exposes a handler for adding items to iterateset, to be called upon iterator close +type IterateSetHandler interface { + UpdateIterateSet(iterationTracker) +} + +type iterationTracker struct { + startKey []byte // start of the iteration range + endKey []byte // end of the iteration range + earlyStopKey []byte // key that caused early stop + iteratedKeys map[string]struct{} // TODO: is a map okay because the ordering will be enforced when we replay the iterator? + ascending bool + + writeset WriteSet + + // TODO: is it possible that terimation is affected by keys later in iteration that weren't reached? eg. number of keys affecting iteration? + // TODO: i believe to get number of keys the iteration would need to be done fully so its not a concern? + + // TODO: maybe we need to store keys served from writeset for the transaction? that way if theres OTHER keys within the writeset and the iteration range, and were written to the writeset later, we can discriminate between the groups? + // keysServedFromWriteset map[string]struct{} + + // actually its simpler to just store a copy of the writeset at the time of iterator creation +} + +func NewIterationTracker(startKey, endKey []byte, ascending bool, writeset WriteSet) iterationTracker { + copyWriteset := make(WriteSet, len(writeset)) + + for key, value := range writeset { + copyWriteset[key] = value + } + + return iterationTracker{ + startKey: startKey, + endKey: endKey, + iteratedKeys: make(map[string]struct{}), + ascending: ascending, + writeset: copyWriteset, + } +} + +func (item *iterationTracker) AddKey(key []byte) { + item.iteratedKeys[string(key)] = struct{}{} +} + +func (item *iterationTracker) SetEarlyStopKey(key []byte) { + item.earlyStopKey = key +} + // Version Indexed Store wraps the multiversion store in a way that implements the KVStore interface, but also stores the index of the transaction, and so store actions are applied to the multiversion store using that index type VersionIndexedStore struct { mtx sync.Mutex // used for tracking reads and writes for eventual validation + persistence into multi-version store - readset map[string][]byte // contains the key -> value mapping for all keys read from the store (not mvkv, underlying store) - writeset map[string][]byte // contains the key -> value mapping for all keys written to the store + // TODO: does this need sync.Map? + readset map[string][]byte // contains the key -> value mapping for all keys read from the store (not mvkv, underlying store) + writeset map[string][]byte // contains the key -> value mapping for all keys written to the store + iterateset Iterateset // TODO: need to add iterateset here as well // dirty keys that haven't been sorted yet for iteration @@ -36,11 +94,14 @@ type VersionIndexedStore struct { } var _ types.KVStore = (*VersionIndexedStore)(nil) +var _ ReadsetHandler = (*VersionIndexedStore)(nil) +var _ IterateSetHandler = (*VersionIndexedStore)(nil) func NewVersionIndexedStore(parent types.KVStore, multiVersionStore MultiVersionStore, transactionIndex, incarnation int, abortChannel chan scheduler.Abort) *VersionIndexedStore { return &VersionIndexedStore{ readset: make(map[string][]byte), writeset: make(map[string][]byte), + iterateset: []iterationTracker{}, dirtySet: make(map[string]struct{}), sortedStore: dbm.NewMemDB(), parent: parent, @@ -97,7 +158,7 @@ func (store *VersionIndexedStore) Get(key []byte) []byte { } // if we didn't find it in the multiversion store, then we want to check the parent store + add to readset parentValue := store.parent.Get(key) - store.updateReadSet(key, parentValue) + store.UpdateReadSet(key, parentValue) return parentValue } @@ -107,7 +168,7 @@ func (store *VersionIndexedStore) parseValueAndUpdateReadset(strKey string, mvsV if mvsValue.IsDeleted() { value = nil } - store.updateReadSet([]byte(strKey), value) + store.UpdateReadSet([]byte(strKey), value) return value } @@ -201,40 +262,22 @@ func (v *VersionIndexedStore) ReverseIterator(start []byte, end []byte) dbm.Iter func (store *VersionIndexedStore) iterator(start []byte, end []byte, ascending bool) dbm.Iterator { store.mtx.Lock() defer store.mtx.Unlock() - // TODO: ideally we persist writeset keys into a sorted btree for later use - // make a set of total keys across mvkv and mvs to iterate - keysToIterate := make(map[string]struct{}) - for key := range store.writeset { - keysToIterate[key] = struct{}{} - } + // get the sorted keys from MVS // TODO: ideally we take advantage of mvs keys already being sorted - // get the multiversion store sorted keys - writesetMap := store.multiVersionStore.GetAllWritesetKeys() - for i := 0; i < store.transactionIndex; i++ { - // add all the writesets keys up until current index - for _, key := range writesetMap[i] { - keysToIterate[key] = struct{}{} - } - } // TODO: ideally merge btree and mvs keys into a single sorted btree + memDB := store.multiVersionStore.CollectIteratorItems(store.transactionIndex) - // TODO: this is horribly inefficient, fix this - sortedKeys := make([]string, len(keysToIterate)) - for key := range keysToIterate { - sortedKeys = append(sortedKeys, key) - } - sort.Strings(sortedKeys) - - memDB := dbm.NewMemDB() - for _, key := range sortedKeys { + // TODO: ideally we persist writeset keys into a sorted btree for later use + // make a set of total keys across mvkv and mvs to iterate + for key := range store.writeset { memDB.Set([]byte(key), []byte{}) } var parent, memIterator types.Iterator // make a memIterator - memIterator = store.newMemIterator(start, end, memDB, ascending) + memIterator = store.newMemIterator(start, end, memDB, ascending, store) if ascending { parent = store.parent.Iterator(start, end) @@ -242,8 +285,13 @@ func (store *VersionIndexedStore) iterator(start []byte, end []byte, ascending b parent = store.parent.ReverseIterator(start, end) } + mergeIterator := NewMVSMergeIterator(parent, memIterator, ascending, store) + + iterationTracker := NewIterationTracker(start, end, ascending, store.writeset) + trackedIterator := NewTrackedIterator(mergeIterator, iterationTracker, store) + // mergeIterator - return NewMVSMergeIterator(parent, memIterator, ascending) + return trackedIterator } @@ -288,6 +336,8 @@ func (store *VersionIndexedStore) WriteToMultiVersionStore() { defer store.mtx.Unlock() defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "write_mvs") store.multiVersionStore.SetWriteset(store.transactionIndex, store.incarnation, store.writeset) + store.multiVersionStore.SetReadset(store.transactionIndex, store.readset) + store.multiVersionStore.SetIterateset(store.transactionIndex, store.iterateset) } func (store *VersionIndexedStore) WriteEstimatesToMultiVersionStore() { @@ -295,12 +345,18 @@ func (store *VersionIndexedStore) WriteEstimatesToMultiVersionStore() { defer store.mtx.Unlock() defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "write_mvs") store.multiVersionStore.SetEstimatedWriteset(store.transactionIndex, store.incarnation, store.writeset) + // TODO: do we need to write readset and iterateset in this case? I don't think so since if this is called it means we aren't doing validation } -func (store *VersionIndexedStore) updateReadSet(key []byte, value []byte) { +func (store *VersionIndexedStore) UpdateReadSet(key []byte, value []byte) { // add to readset keyStr := string(key) store.readset[keyStr] = value // add to dirty set store.dirtySet[keyStr] = struct{}{} } + +func (store *VersionIndexedStore) UpdateIterateSet(iterationTracker iterationTracker) { + // append to iterateset + store.iterateset = append(store.iterateset, iterationTracker) +} diff --git a/store/multiversion/mvkv_test.go b/store/multiversion/mvkv_test.go index 23bcc472a..44304fd50 100644 --- a/store/multiversion/mvkv_test.go +++ b/store/multiversion/mvkv_test.go @@ -321,6 +321,12 @@ func TestIterator(t *testing.T) { // iterate over the keys - exclusive on key5 iter := vis.Iterator([]byte("000"), []byte("key5")) + + // verify domain is superset + start, end := iter.Domain() + require.Equal(t, []byte("000"), start) + require.Equal(t, []byte("key5"), end) + vals := []string{} defer iter.Close() for ; iter.Valid(); iter.Next() { @@ -354,6 +360,17 @@ func TestIterator(t *testing.T) { require.Equal(t, []string{"value1", "value2", "value3", "valueNew"}, vals3) iter3.Close() + vis.Set([]byte("key6"), []byte("value6")) + // iterate over the keys, writeset being the last of the iteration range + iter4 := vis.Iterator([]byte("000"), []byte("key7")) + vals4 := []string{} + defer iter4.Close() + for ; iter4.Valid(); iter4.Next() { + vals4 = append(vals4, string(iter4.Value())) + } + require.Equal(t, []string{"value1", "value2", "value3", "valueNew", "value5", "value6"}, vals4) + iter4.Close() + // add an estimate to MVS mvs.SetEstimatedWriteset(1, 1, map[string][]byte{ "key2": []byte("value1_b"), diff --git a/store/multiversion/store.go b/store/multiversion/store.go index 08c45204b..0d16f12d6 100644 --- a/store/multiversion/store.go +++ b/store/multiversion/store.go @@ -8,6 +8,8 @@ import ( "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/telemetry" + occtypes "github.com/cosmos/cosmos-sdk/types/occ" + db "github.com/tendermint/tm-db" ) type MultiVersionStore interface { @@ -19,13 +21,17 @@ type MultiVersionStore interface { InvalidateWriteset(index int, incarnation int) SetEstimatedWriteset(index int, incarnation int, writeset WriteSet) GetAllWritesetKeys() map[int][]string + CollectIteratorItems(index int) *db.MemDB SetReadset(index int, readset ReadSet) GetReadset(index int) ReadSet - ValidateTransactionState(index int) []int + SetIterateset(index int, iterateset Iterateset) + GetIterateset(index int) Iterateset + ValidateTransactionState(index int) (bool, []int) } type WriteSet map[string][]byte type ReadSet map[string][]byte +type Iterateset []iterationTracker var _ MultiVersionStore = (*Store)(nil) @@ -37,6 +43,7 @@ type Store struct { txWritesetKeys map[int][]string // map of tx index -> writeset keys txReadSets map[int]ReadSet + txIterateSets map[int]Iterateset parentStore types.KVStore } @@ -46,6 +53,7 @@ func NewMultiVersionStore(parentStore types.KVStore) *Store { multiVersionMap: make(map[string]MultiVersionValue), txWritesetKeys: make(map[int][]string), txReadSets: make(map[int]ReadSet), + txIterateSets: make(map[int]Iterateset), parentStore: parentStore, } } @@ -212,9 +220,107 @@ func (s *Store) GetReadset(index int) ReadSet { return s.txReadSets[index] } -func (s *Store) ValidateTransactionState(index int) []int { +func (s *Store) SetIterateset(index int, iterateset Iterateset) { + s.mtx.Lock() + defer s.mtx.Unlock() + + s.txIterateSets[index] = iterateset +} + +func (s *Store) GetIterateset(index int) Iterateset { + s.mtx.RLock() + defer s.mtx.RUnlock() + + return s.txIterateSets[index] +} + +// CollectIteratorItems implements MultiVersionStore. It will return a memDB containing all of the keys present in the multiversion store within the iteration range prior to (exclusive of) the index. +func (s *Store) CollectIteratorItems(index int) *db.MemDB { + sortedItems := db.NewMemDB() + + // get all writeset keys prior to index + keys := s.GetAllWritesetKeys() + for i := 0; i < index; i++ { + indexedWriteset, ok := keys[i] + if !ok { + continue + } + // TODO: do we want to exclude keys out of the range or just let the iterator handle it? + for _, key := range indexedWriteset { + // TODO: inefficient because (logn) for each key + rebalancing? maybe theres a better way to add to a tree to reduce rebalancing overhead + sortedItems.Set([]byte(key), []byte{}) + } + } + return sortedItems +} + +func (s *Store) validateIterator(index int, tracker iterationTracker) bool { + // collect items from multiversion store + sortedItems := s.CollectIteratorItems(index) + // add the iterationtracker writeset keys to the sorted items + for key := range tracker.writeset { + sortedItems.Set([]byte(key), []byte{}) + } + validChannel := make(chan bool, 1) + abortChannel := make(chan occtypes.Abort, 1) + + // listen for abort while iterating + go func(iterationTracker iterationTracker, items *db.MemDB, returnChan chan bool, abortChan chan occtypes.Abort) { + var parentIter types.Iterator + expectedKeys := iterationTracker.iteratedKeys + iter := s.newMVSValidationIterator(index, iterationTracker.startKey, iterationTracker.endKey, items, iterationTracker.ascending, iterationTracker.writeset, abortChan) + if iterationTracker.ascending { + parentIter = s.parentStore.Iterator(iterationTracker.startKey, iterationTracker.endKey) + } else { + parentIter = s.parentStore.ReverseIterator(iterationTracker.startKey, iterationTracker.endKey) + } + // create a new MVSMergeiterator + mergeIterator := NewMVSMergeIterator(parentIter, iter, iterationTracker.ascending, NoOpHandler{}) + defer mergeIterator.Close() + for ; mergeIterator.Valid(); mergeIterator.Next() { + if len(expectedKeys) == 0 { + // if we have no more expected keys, then the iterator is invalid + returnChan <- false + return + } + key := mergeIterator.Key() + if _, ok := expectedKeys[string(key)]; !ok { + // if key isn't found + returnChan <- false + return + } + // remove from expected keys + delete(expectedKeys, string(key)) + + // if our iterator key was the early stop, then we can break + if bytes.Equal(key, iterationTracker.earlyStopKey) { + returnChan <- true + return + } + } + returnChan <- !(len(expectedKeys) > 0) + }(tracker, sortedItems, validChannel, abortChannel) + select { + case <-abortChannel: + // if we get an abort, then we know that the iterator is invalid + return false + case valid := <-validChannel: + return valid + } +} + +// TODO: do we want to return bool + []int where bool indicates whether it was valid and then []int indicates only ones for which we need to wait due to estimates? - yes i think so? +func (s *Store) ValidateTransactionState(index int) (bool, []int) { defer telemetry.MeasureSince(time.Now(), "store", "mvs", "validate") conflictSet := map[int]struct{}{} + valid := true + + // TODO: can we parallelize for all iterators? + iterateset := s.GetIterateset(index) + for _, iterationTracker := range iterateset { + iteratorValid := s.validateIterator(index, iterationTracker) + valid = valid && iteratorValid + } // validate readset readset := s.GetReadset(index) @@ -229,20 +335,20 @@ func (s *Store) ValidateTransactionState(index int) []int { panic("there shouldn't be readset conflicts with parent kv store, since it shouldn't change") } } else { - // if estimate, mark as conflict index + // if estimate, mark as conflict index - but don't invalidate if latestValue.IsEstimate() { conflictSet[latestValue.Index()] = struct{}{} } else if latestValue.IsDeleted() { if value != nil { // conflict - conflictSet[latestValue.Index()] = struct{}{} + // TODO: would we want to return early? + valid = false } } else if !bytes.Equal(latestValue.Value(), value) { - conflictSet[latestValue.Index()] = struct{}{} + valid = false } } } - // TODO: validate iterateset // convert conflictset into sorted indices conflictIndices := make([]int, 0, len(conflictSet)) @@ -251,7 +357,7 @@ func (s *Store) ValidateTransactionState(index int) []int { } sort.Ints(conflictIndices) - return conflictIndices + return valid, conflictIndices } func (s *Store) WriteLatestToStore() { diff --git a/store/multiversion/store_test.go b/store/multiversion/store_test.go index bb56d1e71..84e9f77ac 100644 --- a/store/multiversion/store_test.go +++ b/store/multiversion/store_test.go @@ -1,10 +1,12 @@ package multiversion_test import ( + "bytes" "testing" "github.com/cosmos/cosmos-sdk/store/dbadapter" "github.com/cosmos/cosmos-sdk/store/multiversion" + "github.com/cosmos/cosmos-sdk/types/occ" "github.com/stretchr/testify/require" dbm "github.com/tendermint/tm-db" ) @@ -191,11 +193,13 @@ func TestMultiVersionStoreValidateState(t *testing.T) { mvs.SetReadset(5, readset) // assert no readset is valid - conflicts := mvs.ValidateTransactionState(4) + valid, conflicts := mvs.ValidateTransactionState(4) + require.True(t, valid) require.Empty(t, conflicts) // assert readset index 5 is valid - conflicts = mvs.ValidateTransactionState(5) + valid, conflicts = mvs.ValidateTransactionState(5) + require.True(t, valid) require.Empty(t, conflicts) // introduce conflict @@ -203,29 +207,395 @@ func TestMultiVersionStoreValidateState(t *testing.T) { "key3": []byte("value6"), }) - // expect index 2 to be returned - conflicts = mvs.ValidateTransactionState(5) - require.Equal(t, []int{2}, conflicts) + // expect failure with empty conflicts + valid, conflicts = mvs.ValidateTransactionState(5) + require.False(t, valid) + require.Empty(t, conflicts) // add a conflict due to deletion mvs.SetWriteset(3, 1, map[string][]byte{ "key1": nil, }) - // expect indices 2 and 3 to be returned - conflicts = mvs.ValidateTransactionState(5) - require.Equal(t, []int{2, 3}, conflicts) + // expect failure with empty conflicts + valid, conflicts = mvs.ValidateTransactionState(5) + require.False(t, valid) + require.Empty(t, conflicts) // add a conflict due to estimate mvs.SetEstimatedWriteset(4, 1, map[string][]byte{ "key2": []byte("test"), }) - // expect indices 2, 3, and 4to be returned - conflicts = mvs.ValidateTransactionState(5) - require.Equal(t, []int{2, 3, 4}, conflicts) + // expect index 4 to be returned + valid, conflicts = mvs.ValidateTransactionState(5) + require.False(t, valid) + require.Equal(t, []int{4}, conflicts) // assert panic for parent store mismatch parentKVStore.Set([]byte("key5"), []byte("value6")) require.Panics(t, func() { mvs.ValidateTransactionState(5) }) } + +func TestMVSValidationWithOnlyEstimate(t *testing.T) { + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore(parentKVStore) + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key3"), []byte("value3")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + + writeset := make(multiversion.WriteSet) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + mvs.SetWriteset(1, 2, writeset) + + readset := make(multiversion.ReadSet) + readset["key1"] = []byte("value1") + readset["key2"] = []byte("value2") + readset["key3"] = nil + readset["key4"] = []byte("value4") + readset["key5"] = []byte("value5") + mvs.SetReadset(5, readset) + + // add a conflict due to estimate + mvs.SetEstimatedWriteset(4, 1, map[string][]byte{ + "key2": []byte("test"), + }) + + valid, conflicts := mvs.ValidateTransactionState(5) + require.True(t, valid) + require.Equal(t, []int{4}, conflicts) + +} + +func TestMVSIteratorValidation(t *testing.T) { + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore(parentKVStore) + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 5, 1, make(chan occ.Abort)) + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key3"), []byte("value3")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + + writeset := make(multiversion.WriteSet) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + mvs.SetWriteset(1, 2, writeset) + + // test basic iteration + iter := vis.ReverseIterator([]byte("key1"), []byte("key6")) + for ; iter.Valid(); iter.Next() { + // read value + iter.Value() + } + iter.Close() + vis.WriteToMultiVersionStore() + + // should be valid + valid, conflicts := mvs.ValidateTransactionState(5) + require.True(t, valid) + require.Empty(t, conflicts) +} + +func TestMVSIteratorValidationWithEstimate(t *testing.T) { + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore(parentKVStore) + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 5, 1, make(chan occ.Abort)) + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key3"), []byte("value3")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + + writeset := make(multiversion.WriteSet) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + mvs.SetWriteset(1, 2, writeset) + + iter := vis.Iterator([]byte("key1"), []byte("key6")) + for ; iter.Valid(); iter.Next() { + // read value + iter.Value() + } + iter.Close() + vis.WriteToMultiVersionStore() + + writeset2 := make(multiversion.WriteSet) + writeset2["key2"] = []byte("value2") + mvs.SetEstimatedWriteset(2, 2, writeset2) + + // should be invalid + valid, conflicts := mvs.ValidateTransactionState(5) + require.False(t, valid) + require.Equal(t, []int{2}, conflicts) +} + +func TestMVSIteratorValidationWithKeySwitch(t *testing.T) { + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore(parentKVStore) + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 5, 1, make(chan occ.Abort)) + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key3"), []byte("value3")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + + writeset := make(multiversion.WriteSet) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + mvs.SetWriteset(1, 2, writeset) + + iter := vis.Iterator([]byte("key1"), []byte("key6")) + for ; iter.Valid(); iter.Next() { + // read value + iter.Value() + } + iter.Close() + vis.WriteToMultiVersionStore() + + // deletion of 2 and introduction of 3 + writeset2 := make(multiversion.WriteSet) + writeset2["key2"] = nil + writeset2["key3"] = []byte("valueX") + mvs.SetWriteset(2, 2, writeset2) + + // should be invalid + valid, conflicts := mvs.ValidateTransactionState(5) + require.False(t, valid) + require.Empty(t, conflicts) +} + +func TestMVSIteratorValidationWithKeyAdded(t *testing.T) { + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore(parentKVStore) + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 5, 1, make(chan occ.Abort)) + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key3"), []byte("value3")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + + writeset := make(multiversion.WriteSet) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + mvs.SetWriteset(1, 2, writeset) + + iter := vis.Iterator([]byte("key1"), []byte("key7")) + for ; iter.Valid(); iter.Next() { + // read value + iter.Value() + } + iter.Close() + vis.WriteToMultiVersionStore() + + // addition of key6 + writeset2 := make(multiversion.WriteSet) + writeset2["key6"] = []byte("value6") + mvs.SetWriteset(2, 2, writeset2) + + // should be invalid + valid, conflicts := mvs.ValidateTransactionState(5) + require.False(t, valid) + require.Empty(t, conflicts) +} + +func TestMVSIteratorValidationWithWritesetValues(t *testing.T) { + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore(parentKVStore) + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 5, 1, make(chan occ.Abort)) + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key3"), []byte("value3")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + + writeset := make(multiversion.WriteSet) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + mvs.SetWriteset(1, 2, writeset) + + // set a key BEFORE iteration occurred + vis.Set([]byte("key6"), []byte("value6")) + + iter := vis.Iterator([]byte("key1"), []byte("key7")) + for ; iter.Valid(); iter.Next() { + } + iter.Close() + vis.WriteToMultiVersionStore() + + // should be valid + valid, conflicts := mvs.ValidateTransactionState(5) + require.True(t, valid) + require.Empty(t, conflicts) +} + +func TestMVSIteratorValidationWithWritesetValuesSetAfterIteration(t *testing.T) { + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore(parentKVStore) + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 5, 1, make(chan occ.Abort)) + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key3"), []byte("value3")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + + writeset := make(multiversion.WriteSet) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + mvs.SetWriteset(1, 2, writeset) + + readset := make(multiversion.ReadSet) + readset["key1"] = []byte("value1") + readset["key2"] = []byte("value2") + readset["key3"] = nil + readset["key4"] = []byte("value4") + readset["key5"] = []byte("value5") + mvs.SetReadset(5, readset) + + // no key6 because the iteration was performed BEFORE the write + iter := vis.Iterator([]byte("key1"), []byte("key7")) + for ; iter.Valid(); iter.Next() { + } + iter.Close() + + // write key 6 AFTER iterator went + vis.Set([]byte("key6"), []byte("value6")) + vis.WriteToMultiVersionStore() + + // should be valid + valid, conflicts := mvs.ValidateTransactionState(5) + require.True(t, valid) + require.Empty(t, conflicts) +} + +func TestMVSIteratorValidationReverse(t *testing.T) { + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore(parentKVStore) + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 5, 1, make(chan occ.Abort)) + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key3"), []byte("value3")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + + writeset := make(multiversion.WriteSet) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + mvs.SetWriteset(1, 2, writeset) + + readset := make(multiversion.ReadSet) + readset["key1"] = []byte("value1") + readset["key2"] = []byte("value2") + readset["key3"] = nil + readset["key4"] = []byte("value4") + readset["key5"] = []byte("value5") + mvs.SetReadset(5, readset) + + // set a key BEFORE iteration occurred + vis.Set([]byte("key6"), []byte("value6")) + + iter := vis.ReverseIterator([]byte("key1"), []byte("key7")) + for ; iter.Valid(); iter.Next() { + } + iter.Close() + vis.WriteToMultiVersionStore() + + // should be valid + valid, conflicts := mvs.ValidateTransactionState(5) + require.True(t, valid) + require.Empty(t, conflicts) +} + +func TestMVSIteratorValidationEarlyStop(t *testing.T) { + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore(parentKVStore) + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 5, 1, make(chan occ.Abort)) + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key3"), []byte("value3")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + + writeset := make(multiversion.WriteSet) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + mvs.SetWriteset(1, 2, writeset) + + readset := make(multiversion.ReadSet) + readset["key1"] = []byte("value1") + readset["key2"] = []byte("value2") + readset["key3"] = nil + readset["key4"] = []byte("value4") + mvs.SetReadset(5, readset) + + iter := vis.Iterator([]byte("key1"), []byte("key7")) + for ; iter.Valid(); iter.Next() { + // read the value and see if we want to break + if bytes.Equal(iter.Key(), []byte("key4")) { + break + } + } + iter.Close() + vis.WriteToMultiVersionStore() + + // removal of key5 - but irrelevant because of early stop + writeset2 := make(multiversion.WriteSet) + writeset2["key5"] = nil + mvs.SetWriteset(2, 2, writeset2) + + // should be valid + valid, conflicts := mvs.ValidateTransactionState(5) + require.True(t, valid) + require.Empty(t, conflicts) +} + +// TODO: what about early stop with a new key added in the range? - especially if its the last key that we stopped at? +func TestMVSIteratorValidationEarlyStopAtEndOfRange(t *testing.T) { + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore(parentKVStore) + vis := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 5, 1, make(chan occ.Abort)) + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key3"), []byte("value3")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + + writeset := make(multiversion.WriteSet) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + mvs.SetWriteset(1, 2, writeset) + + // test basic iteration + iter := vis.Iterator([]byte("key1"), []byte("key7")) + for ; iter.Valid(); iter.Next() { + // read the value and see if we want to break + if bytes.Equal(iter.Key(), []byte("key5")) { + break + } + } + iter.Close() + vis.WriteToMultiVersionStore() + + // add key6 + writeset2 := make(multiversion.WriteSet) + writeset2["key6"] = []byte("value6") + mvs.SetWriteset(2, 2, writeset2) + + // should be valid + valid, conflicts := mvs.ValidateTransactionState(5) + require.True(t, valid) + require.Empty(t, conflicts) +} diff --git a/store/multiversion/trackediterator.go b/store/multiversion/trackediterator.go new file mode 100644 index 000000000..361d848cb --- /dev/null +++ b/store/multiversion/trackediterator.go @@ -0,0 +1,57 @@ +package multiversion + +import "github.com/cosmos/cosmos-sdk/store/types" + +// tracked iterator is a wrapper around an existing iterator to track the iterator progress and monitor which keys are iterated. +type trackedIterator struct { + types.Iterator + + iterateset iterationTracker + IterateSetHandler +} + +// TODO: test + +func NewTrackedIterator(iter types.Iterator, iterationTracker iterationTracker, iterateSetHandler IterateSetHandler) *trackedIterator { + return &trackedIterator{ + Iterator: iter, + iterateset: iterationTracker, + IterateSetHandler: iterateSetHandler, + } +} + +// Close calls first updates the iterateset from the iterator, and then calls iterator.Close() +func (ti *trackedIterator) Close() error { + // TODO: if there are more keys to the iterator, then we consider it early stopped? + if ti.Iterator.Valid() { + // TODO: test whether reaching end of iteration range means valid is true or false + ti.iterateset.SetEarlyStopKey(ti.Iterator.Key()) + } + // Update iterate set + ti.IterateSetHandler.UpdateIterateSet(ti.iterateset) + return ti.Iterator.Close() +} + +// Key calls the iterator.Key() and adds the key to the iterateset, then returns the key from the iterator +func (ti *trackedIterator) Key() []byte { + key := ti.Iterator.Key() + // add key to the tracker + ti.iterateset.AddKey(key) + return key +} + +// Value calls the iterator.Key() and adds the key to the iterateset, then returns the value from the iterator +func (ti *trackedIterator) Value() []byte { + key := ti.Iterator.Key() + // add key to the tracker + ti.iterateset.AddKey(key) + return ti.Iterator.Value() +} + +func (ti *trackedIterator) Next() { + // add current key to the tracker + key := ti.Iterator.Key() + ti.iterateset.AddKey(key) + // call next + ti.Iterator.Next() +} From 60b21132838210906c656f210a92f74ea00391e9 Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Thu, 19 Oct 2023 13:41:46 -0400 Subject: [PATCH 14/36] [occ] Add scheduler logic for validation (#336) - This was copied from #332 which became unwieldy due to commit history (merges/rebases) - Adds scheduler logic for validation - In this initial version it completes all executions then performs validations (which feed retries) - Once we start benchmarking we can make performance improvements to this - Retries tasks that fail validation and have no dependencies - Scheduler Test verifies multi-worker with conflicts --- baseapp/abci.go | 4 - baseapp/deliver_tx_batch_test.go | 131 ++++++++++++++++++++ server/mock/store.go | 8 ++ store/cachemulti/store.go | 17 +++ store/multiversion/mvkv.go | 17 +++ store/multiversion/store.go | 7 ++ store/rootmulti/store.go | 12 ++ store/types/store.go | 6 + tasks/scheduler.go | 198 +++++++++++++++++++++++++------ tasks/scheduler_test.go | 88 ++++++++++---- 10 files changed, 426 insertions(+), 62 deletions(-) create mode 100644 baseapp/deliver_tx_batch_test.go diff --git a/baseapp/abci.go b/baseapp/abci.go index 84a74a247..bd67eaaa7 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -237,11 +237,7 @@ func (app *BaseApp) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abc } // DeliverTxBatch executes multiple txs -// TODO: support occ logic with scheduling func (app *BaseApp) DeliverTxBatch(ctx sdk.Context, req sdk.DeliverTxBatchRequest) (res sdk.DeliverTxBatchResponse) { - //TODO: inject multiversion store without import cycle (figure out right place for this) - // ctx = ctx.WithMultiVersionStore(multiversion.NewMultiVersionStore()) - reqList := make([]abci.RequestDeliverTx, 0, len(req.TxEntries)) for _, tx := range req.TxEntries { reqList = append(reqList, tx.Request) diff --git a/baseapp/deliver_tx_batch_test.go b/baseapp/deliver_tx_batch_test.go new file mode 100644 index 000000000..13cd9fd60 --- /dev/null +++ b/baseapp/deliver_tx_batch_test.go @@ -0,0 +1,131 @@ +package baseapp + +import ( + "context" + "fmt" + "strconv" + "testing" + + "github.com/stretchr/testify/require" + abci "github.com/tendermint/tendermint/abci/types" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func toInt(b []byte) int { + r, _ := strconv.Atoi(string(b)) + return r +} + +func toByteArr(i int) []byte { + return []byte(fmt.Sprintf("%d", i)) +} + +func handlerKVStore(capKey sdk.StoreKey) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + ctx = ctx.WithEventManager(sdk.NewEventManager()) + res := &sdk.Result{} + + // Extract the unique ID from the message (assuming you have added this) + txIndex := ctx.TxIndex() + + // Use the unique ID to get a specific key for this transaction + sharedKey := []byte(fmt.Sprintf("shared")) + txKey := []byte(fmt.Sprintf("tx-%d", txIndex)) + + // Similar steps as before: Get the store, retrieve a value, increment it, store back, emit an event + // Get the store + store := ctx.KVStore(capKey) + + // increment per-tx key (no conflict) + val := toInt(store.Get(txKey)) + store.Set(txKey, toByteArr(val+1)) + + // increment shared key + sharedVal := toInt(store.Get(sharedKey)) + store.Set(sharedKey, toByteArr(sharedVal+1)) + + // Emit an event with the incremented value and the unique ID + ctx.EventManager().EmitEvent( + sdk.NewEvent(sdk.EventTypeMessage, + sdk.NewAttribute("shared-val", fmt.Sprintf("%d", sharedVal+1)), + sdk.NewAttribute("tx-val", fmt.Sprintf("%d", val+1)), + sdk.NewAttribute("tx-id", fmt.Sprintf("%d", txIndex)), + ), + ) + + res.Events = ctx.EventManager().Events().ToABCIEvents() + return res, nil + } +} + +func requireAttribute(t *testing.T, evts []abci.Event, name string, val string) { + for _, evt := range evts { + for _, att := range evt.Attributes { + if string(att.Key) == name { + require.Equal(t, val, string(att.Value)) + return + } + } + } + require.Fail(t, fmt.Sprintf("attribute %s not found via value %s", name, val)) +} + +func TestDeliverTxBatch(t *testing.T) { + // test increments in the ante + //anteKey := []byte("ante-key") + anteOpt := func(bapp *BaseApp) {} + + // test increments in the handler + routerOpt := func(bapp *BaseApp) { + r := sdk.NewRoute(routeMsgCounter, handlerKVStore(capKey1)) + bapp.Router().AddRoute(r) + } + + app := setupBaseApp(t, anteOpt, routerOpt) + app.InitChain(context.Background(), &abci.RequestInitChain{}) + + // Create same codec used in txDecoder + codec := codec.NewLegacyAmino() + registerTestCodec(codec) + + nBlocks := 3 + txPerHeight := 5 + + for blockN := 0; blockN < nBlocks; blockN++ { + header := tmproto.Header{Height: int64(blockN) + 1} + app.setDeliverState(header) + app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) + app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) + + var requests []*sdk.DeliverTxEntry + for i := 0; i < txPerHeight; i++ { + counter := int64(blockN*txPerHeight + i) + tx := newTxCounter(counter, counter) + + txBytes, err := codec.Marshal(tx) + require.NoError(t, err) + requests = append(requests, &sdk.DeliverTxEntry{ + Request: abci.RequestDeliverTx{Tx: txBytes}, + }) + } + + responses := app.DeliverTxBatch(app.deliverState.ctx, sdk.DeliverTxBatchRequest{TxEntries: requests}) + require.Len(t, responses.Results, txPerHeight) + + for idx, deliverTxRes := range responses.Results { + res := deliverTxRes.Response + require.Equal(t, abci.CodeTypeOK, res.Code) + requireAttribute(t, res.Events, "tx-id", fmt.Sprintf("%d", idx)) + requireAttribute(t, res.Events, "tx-val", fmt.Sprintf("%d", blockN+1)) + requireAttribute(t, res.Events, "shared-val", fmt.Sprintf("%d", blockN*txPerHeight+idx+1)) + } + + app.EndBlock(app.deliverState.ctx, abci.RequestEndBlock{}) + require.Empty(t, app.deliverState.ctx.MultiStore().GetEvents()) + app.SetDeliverStateToCommit() + app.Commit(context.Background()) + } +} diff --git a/server/mock/store.go b/server/mock/store.go index 0a47cc08c..ab12f961a 100644 --- a/server/mock/store.go +++ b/server/mock/store.go @@ -233,3 +233,11 @@ func NewCommitMultiStore() sdk.CommitMultiStore { func (ms multiStore) Close() error { return nil } + +func (ms multiStore) SetKVStores(handler func(key store.StoreKey, s sdk.KVStore) store.CacheWrap) store.MultiStore { + panic("not implemented") +} + +func (ms multiStore) StoreKeys() []sdk.StoreKey { + panic("not implemented") +} diff --git a/store/cachemulti/store.go b/store/cachemulti/store.go index 43e00c32b..96ce20dfc 100644 --- a/store/cachemulti/store.go +++ b/store/cachemulti/store.go @@ -208,3 +208,20 @@ func (cms Store) GetKVStore(key types.StoreKey) types.KVStore { func (cms Store) GetWorkingHash() ([]byte, error) { panic("should never attempt to get working hash from cache multi store") } + +// StoreKeys returns a list of all store keys +func (cms Store) StoreKeys() []types.StoreKey { + keys := make([]types.StoreKey, 0, len(cms.stores)) + for _, key := range cms.keys { + keys = append(keys, key) + } + return keys +} + +// SetKVStores sets the underlying KVStores via a handler for each key +func (cms Store) SetKVStores(handler func(sk types.StoreKey, s types.KVStore) types.CacheWrap) types.MultiStore { + for k, s := range cms.stores { + cms.stores[k] = handler(k, s.(types.KVStore)) + } + return cms +} diff --git a/store/multiversion/mvkv.go b/store/multiversion/mvkv.go index 1b2f947c1..6eeabd517 100644 --- a/store/multiversion/mvkv.go +++ b/store/multiversion/mvkv.go @@ -6,6 +6,8 @@ import ( "sync" "time" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/telemetry" scheduler "github.com/cosmos/cosmos-sdk/types/occ" @@ -356,6 +358,21 @@ func (store *VersionIndexedStore) UpdateReadSet(key []byte, value []byte) { store.dirtySet[keyStr] = struct{}{} } +// Write implements types.CacheWrap so this store can exist on the cache multi store +func (store *VersionIndexedStore) Write() { + panic("not implemented") +} + +// GetEvents implements types.CacheWrap so this store can exist on the cache multi store +func (store *VersionIndexedStore) GetEvents() []abci.Event { + panic("not implemented") +} + +// ResetEvents implements types.CacheWrap so this store can exist on the cache multi store +func (store *VersionIndexedStore) ResetEvents() { + panic("not implemented") +} + func (store *VersionIndexedStore) UpdateIterateSet(iterationTracker iterationTracker) { // append to iterateset store.iterateset = append(store.iterateset, iterationTracker) diff --git a/store/multiversion/store.go b/store/multiversion/store.go index 0d16f12d6..7c14c6415 100644 --- a/store/multiversion/store.go +++ b/store/multiversion/store.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/telemetry" + "github.com/cosmos/cosmos-sdk/types/occ" occtypes "github.com/cosmos/cosmos-sdk/types/occ" db "github.com/tendermint/tm-db" ) @@ -24,6 +25,7 @@ type MultiVersionStore interface { CollectIteratorItems(index int) *db.MemDB SetReadset(index int, readset ReadSet) GetReadset(index int) ReadSet + VersionedIndexedStore(index int, incarnation int, abortChannel chan occ.Abort) *VersionIndexedStore SetIterateset(index int, iterateset Iterateset) GetIterateset(index int) Iterateset ValidateTransactionState(index int) (bool, []int) @@ -58,6 +60,11 @@ func NewMultiVersionStore(parentStore types.KVStore) *Store { } } +// VersionedIndexedStore creates a new versioned index store for a given incarnation and transaction index +func (s *Store) VersionedIndexedStore(index int, incarnation int, abortChannel chan occ.Abort) *VersionIndexedStore { + return NewVersionIndexedStore(s.parentStore, s, index, incarnation, abortChannel) +} + // GetLatest implements MultiVersionStore. func (s *Store) GetLatest(key []byte) (value MultiVersionValueItem) { s.mtx.RLock() diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index a8488bfea..77f7a728c 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -1201,3 +1201,15 @@ func flushPruningHeights(batch dbm.Batch, pruneHeights []int64) { func (rs *Store) Close() error { return rs.db.Close() } + +func (rs *Store) SetKVStores(handler func(key types.StoreKey, s types.KVStore) types.CacheWrap) types.MultiStore { + panic("SetKVStores is not implemented for rootmulti") +} + +func (rs *Store) StoreKeys() []types.StoreKey { + res := make([]types.StoreKey, len(rs.keysByName)) + for _, sk := range rs.keysByName { + res = append(res, sk) + } + return res +} diff --git a/store/types/store.go b/store/types/store.go index 19115ab72..1bc699b24 100644 --- a/store/types/store.go +++ b/store/types/store.go @@ -145,6 +145,12 @@ type MultiStore interface { // Resets the tracked event list ResetEvents() + + // SetKVStores is a generalized wrapper method + SetKVStores(handler func(key StoreKey, s KVStore) CacheWrap) MultiStore + + // StoreKeys returns a list of store keys + StoreKeys() []StoreKey } // From MultiStore.CacheMultiStore().... diff --git a/tasks/scheduler.go b/tasks/scheduler.go index c8b063fe2..575fc1547 100644 --- a/tasks/scheduler.go +++ b/tasks/scheduler.go @@ -1,9 +1,15 @@ package tasks import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "sort" + "github.com/tendermint/tendermint/abci/types" "golang.org/x/sync/errgroup" + + "github.com/cosmos/cosmos-sdk/store/multiversion" + store "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/occ" ) type status string @@ -21,14 +27,32 @@ const ( // statusValidated means the task has been validated // tasks in this status can be reset if an earlier task fails validation statusValidated status = "validated" + // statusWaiting tasks are waiting for another tx to complete + statusWaiting status = "waiting" ) type deliverTxTask struct { - Status status - Index int - Incarnation int - Request types.RequestDeliverTx - Response *types.ResponseDeliverTx + Ctx sdk.Context + AbortCh chan occ.Abort + + Status status + Dependencies []int + Abort *occ.Abort + Index int + Incarnation int + Request types.RequestDeliverTx + Response *types.ResponseDeliverTx + VersionStores map[sdk.StoreKey]*multiversion.VersionIndexedStore +} + +func (dt *deliverTxTask) Increment() { + dt.Incarnation++ + dt.Status = statusPending + dt.Response = nil + dt.Abort = nil + dt.AbortCh = nil + dt.Dependencies = nil + dt.VersionStores = nil } // Scheduler processes tasks concurrently @@ -37,8 +61,9 @@ type Scheduler interface { } type scheduler struct { - deliverTx func(ctx sdk.Context, req types.RequestDeliverTx) (res types.ResponseDeliverTx) - workers int + deliverTx func(ctx sdk.Context, req types.RequestDeliverTx) (res types.ResponseDeliverTx) + workers int + multiVersionStores map[sdk.StoreKey]multiversion.MultiVersionStore } // NewScheduler creates a new scheduler @@ -49,6 +74,31 @@ func NewScheduler(workers int, deliverTxFunc func(ctx sdk.Context, req types.Req } } +func (s *scheduler) invalidateTask(task *deliverTxTask) { + for _, mv := range s.multiVersionStores { + mv.InvalidateWriteset(task.Index, task.Incarnation) + } +} + +func (s *scheduler) findConflicts(task *deliverTxTask) (bool, []int) { + var conflicts []int + uniq := make(map[int]struct{}) + valid := true + for _, mv := range s.multiVersionStores { + ok, mvConflicts := mv.ValidateTransactionState(task.Index) + for _, c := range mvConflicts { + if _, ok := uniq[c]; !ok { + conflicts = append(conflicts, c) + uniq[c] = struct{}{} + } + } + // any non-ok value makes valid false + valid = ok && valid + } + sort.Ints(conflicts) + return valid, conflicts +} + func toTasks(reqs []types.RequestDeliverTx) []*deliverTxTask { res := make([]*deliverTxTask, 0, len(reqs)) for idx, r := range reqs { @@ -69,36 +119,65 @@ func collectResponses(tasks []*deliverTxTask) []types.ResponseDeliverTx { return res } +func (s *scheduler) initMultiVersionStore(ctx sdk.Context) { + mvs := make(map[sdk.StoreKey]multiversion.MultiVersionStore) + keys := ctx.MultiStore().StoreKeys() + for _, sk := range keys { + mvs[sk] = multiversion.NewMultiVersionStore(ctx.MultiStore().GetKVStore(sk)) + } + s.multiVersionStores = mvs +} + +func indexesValidated(tasks []*deliverTxTask, idx []int) bool { + for _, i := range idx { + if tasks[i].Status != statusValidated { + return false + } + } + return true +} + +func allValidated(tasks []*deliverTxTask) bool { + for _, t := range tasks { + if t.Status != statusValidated { + return false + } + } + return true +} + func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []types.RequestDeliverTx) ([]types.ResponseDeliverTx, error) { + s.initMultiVersionStore(ctx) tasks := toTasks(reqs) toExecute := tasks - for len(toExecute) > 0 { + for !allValidated(tasks) { + var err error // execute sets statuses of tasks to either executed or aborted - err := s.executeAll(ctx, toExecute) - if err != nil { - return nil, err + if len(toExecute) > 0 { + err = s.executeAll(ctx, toExecute) + if err != nil { + return nil, err + } } // validate returns any that should be re-executed // note this processes ALL tasks, not just those recently executed - toExecute, err = s.validateAll(ctx, tasks) + toExecute, err = s.validateAll(tasks) if err != nil { return nil, err } for _, t := range toExecute { - t.Incarnation++ - t.Status = statusPending - t.Response = nil - //TODO: reset anything that needs resetting + t.Increment() } } + for _, mv := range s.multiVersionStores { + mv.WriteLatestToStore() + } return collectResponses(tasks), nil } -// TODO: validate each tasks -// TODO: return list of tasks that are invalid -func (s *scheduler) validateAll(ctx sdk.Context, tasks []*deliverTxTask) ([]*deliverTxTask, error) { +func (s *scheduler) validateAll(tasks []*deliverTxTask) ([]*deliverTxTask, error) { var res []*deliverTxTask // find first non-validated entry @@ -111,13 +190,33 @@ func (s *scheduler) validateAll(ctx sdk.Context, tasks []*deliverTxTask) ([]*del } for i := startIdx; i < len(tasks); i++ { - // any aborted tx is known to be suspect here - if tasks[i].Status == statusAborted { + switch tasks[i].Status { + case statusAborted: + // aborted means it can be re-run immediately res = append(res, tasks[i]) - } else { - //TODO: validate the tasks and add it if invalid - //TODO: create and handle abort for validation - tasks[i].Status = statusValidated + + // validated tasks can become unvalidated if an earlier re-run task now conflicts + case statusExecuted, statusValidated: + if valid, conflicts := s.findConflicts(tasks[i]); !valid { + s.invalidateTask(tasks[i]) + + // if the conflicts are now validated, then rerun this task + if indexesValidated(tasks, conflicts) { + res = append(res, tasks[i]) + } else { + // otherwise, wait for completion + tasks[i].Dependencies = conflicts + tasks[i].Status = statusWaiting + } + } else if len(conflicts) == 0 { + tasks[i].Status = statusValidated + } + + case statusWaiting: + // if conflicts are done, then this task is ready to run again + if indexesValidated(tasks, tasks[i].Dependencies) { + res = append(res, tasks[i]) + } } } return res, nil @@ -125,7 +224,6 @@ func (s *scheduler) validateAll(ctx sdk.Context, tasks []*deliverTxTask) ([]*del // ExecuteAll executes all tasks concurrently // Tasks are updated with their status -// TODO: retries on aborted tasks // TODO: error scenarios func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { ch := make(chan *deliverTxTask, len(tasks)) @@ -147,19 +245,21 @@ func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { if !ok { return nil } - //TODO: ensure version multi store is on context - // buffered so it doesn't block on write - // abortCh := make(chan occ.Abort, 1) - //TODO: consume from abort in non-blocking way (give it a length) - resp := s.deliverTx(ctx, task.Request) + resp := s.deliverTx(task.Ctx, task.Request) + + close(task.AbortCh) - // close(abortCh) + if abt, ok := <-task.AbortCh; ok { + task.Status = statusAborted + task.Abort = &abt + continue + } - //if _, ok := <-abortCh; ok { - // tasks.status = TaskStatusAborted - // continue - //} + // write from version store to multiversion stores + for _, v := range task.VersionStores { + v.WriteToMultiVersionStore() + } task.Status = statusExecuted task.Response = &resp @@ -170,6 +270,30 @@ func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { grp.Go(func() error { defer close(ch) for _, task := range tasks { + // initialize the context + ctx = ctx.WithTxIndex(task.Index) + + // non-blocking + cms := ctx.MultiStore().CacheMultiStore() + abortCh := make(chan occ.Abort, len(s.multiVersionStores)) + + // init version stores by store key + vs := make(map[store.StoreKey]*multiversion.VersionIndexedStore) + for storeKey, mvs := range s.multiVersionStores { + vs[storeKey] = mvs.VersionedIndexedStore(task.Index, task.Incarnation, abortCh) + } + + // save off version store so we can ask it things later + task.VersionStores = vs + ms := cms.SetKVStores(func(k store.StoreKey, kvs sdk.KVStore) store.CacheWrap { + return vs[k] + }) + + ctx = ctx.WithMultiStore(ms) + + task.AbortCh = abortCh + task.Ctx = ctx + select { case <-gCtx.Done(): return gCtx.Err() diff --git a/tasks/scheduler_test.go b/tasks/scheduler_test.go index ba9d97846..f132356ec 100644 --- a/tasks/scheduler_test.go +++ b/tasks/scheduler_test.go @@ -2,57 +2,103 @@ package tasks import ( "context" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/assert" - "github.com/tendermint/tendermint/abci/types" + "errors" + "fmt" "testing" + + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/abci/types" + dbm "github.com/tendermint/tm-db" + + "github.com/cosmos/cosmos-sdk/store/cachekv" + "github.com/cosmos/cosmos-sdk/store/cachemulti" + "github.com/cosmos/cosmos-sdk/store/dbadapter" + sdk "github.com/cosmos/cosmos-sdk/types" ) type mockDeliverTxFunc func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx -func (f mockDeliverTxFunc) DeliverTx(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { - return f(ctx, req) -} +var testStoreKey = sdk.NewKVStoreKey("mock") +var itemKey = []byte("key") func requestList(n int) []types.RequestDeliverTx { tasks := make([]types.RequestDeliverTx, n) for i := 0; i < n; i++ { - tasks[i] = types.RequestDeliverTx{} + tasks[i] = types.RequestDeliverTx{ + Tx: []byte(fmt.Sprintf("%d", i)), + } } return tasks } +func initTestCtx() sdk.Context { + ctx := sdk.Context{}.WithContext(context.Background()) + db := dbm.NewMemDB() + mem := dbadapter.Store{DB: db} + stores := make(map[sdk.StoreKey]sdk.CacheWrapper) + stores[testStoreKey] = cachekv.NewStore(mem, testStoreKey, 1000) + keys := make(map[string]sdk.StoreKey) + keys[testStoreKey.Name()] = testStoreKey + store := cachemulti.NewStore(db, stores, keys, nil, nil, nil) + ctx = ctx.WithMultiStore(&store) + return ctx +} + func TestProcessAll(t *testing.T) { tests := []struct { name string workers int + runs int requests []types.RequestDeliverTx deliverTxFunc mockDeliverTxFunc expectedErr error }{ { - name: "All tasks processed without aborts", - workers: 2, - requests: requestList(5), + name: "Test for conflicts", + workers: 50, + runs: 25, + requests: requestList(50), deliverTxFunc: func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { - return types.ResponseDeliverTx{} + // all txs read and write to the same key to maximize conflicts + kv := ctx.MultiStore().GetKVStore(testStoreKey) + val := string(kv.Get(itemKey)) + + // write to the store with this tx's index + kv.Set(itemKey, req.Tx) + + // return what was read from the store (final attempt should be index-1) + return types.ResponseDeliverTx{ + Info: val, + } }, expectedErr: nil, }, - //TODO: Add more test cases } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - s := NewScheduler(tt.workers, tt.deliverTxFunc.DeliverTx) - ctx := sdk.Context{}.WithContext(context.Background()) - - res, err := s.ProcessAll(ctx, tt.requests) - if err != tt.expectedErr { - t.Errorf("Expected error %v, got %v", tt.expectedErr, err) - } else { - // response for each request exists - assert.Len(t, res, len(tt.requests)) + for i := 0; i < tt.runs; i++ { + s := NewScheduler(tt.workers, tt.deliverTxFunc) + ctx := initTestCtx() + + res, err := s.ProcessAll(ctx, tt.requests) + if !errors.Is(err, tt.expectedErr) { + t.Errorf("Expected error %v, got %v", tt.expectedErr, err) + } else { + require.Len(t, res, len(tt.requests)) + for idx, response := range res { + if idx == 0 { + require.Equal(t, "", response.Info) + } else { + // the info is what was read from the kv store by the tx + // each tx writes its own index, so the info should be the index of the previous tx + require.Equal(t, fmt.Sprintf("%d", idx-1), response.Info) + } + } + // confirm last write made it to the parent store + res := ctx.MultiStore().GetKVStore(testStoreKey).Get(itemKey) + require.Equal(t, []byte(fmt.Sprintf("%d", len(tt.requests)-1)), res) + } } }) } From 1178e0bbed21c5e82c767e31125025491b86f847 Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Fri, 20 Oct 2023 15:12:45 -0400 Subject: [PATCH 15/36] [occ] Fix situation where no stores causes a panic (#338) ## Describe your changes and provide context Some tests from sei-chain don't inject a store, and while I'm not sure if that's a valid scenario I made the scheduler.go tolerant to the situation to avoid introducing this assumption to the system. ## Testing performed to validate your change New unit test confirming lack of crash --- baseapp/deliver_tx_batch_test.go | 40 +++++++++++------ tasks/scheduler.go | 31 +++++++------ tasks/scheduler_test.go | 77 +++++++++++++++++++++----------- 3 files changed, 96 insertions(+), 52 deletions(-) diff --git a/baseapp/deliver_tx_batch_test.go b/baseapp/deliver_tx_batch_test.go index 13cd9fd60..c8a29b8b7 100644 --- a/baseapp/deliver_tx_batch_test.go +++ b/baseapp/deliver_tx_batch_test.go @@ -3,7 +3,6 @@ package baseapp import ( "context" "fmt" - "strconv" "testing" "github.com/stretchr/testify/require" @@ -12,15 +11,27 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) -func toInt(b []byte) int { - r, _ := strconv.Atoi(string(b)) - return r -} +func anteHandler(capKey sdk.StoreKey, storeKey []byte) sdk.AnteHandler { + return func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) { + store := ctx.KVStore(capKey) + txTest := tx.(txTest) + + if txTest.FailOnAnte { + return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "ante handler failure") + } + + val := getIntFromStore(store, storeKey) + setIntOnStore(store, storeKey, val+1) -func toByteArr(i int) []byte { - return []byte(fmt.Sprintf("%d", i)) + ctx.EventManager().EmitEvents( + counterEvent("ante-val", val+1), + ) + + return ctx, nil + } } func handlerKVStore(capKey sdk.StoreKey) sdk.Handler { @@ -40,12 +51,12 @@ func handlerKVStore(capKey sdk.StoreKey) sdk.Handler { store := ctx.KVStore(capKey) // increment per-tx key (no conflict) - val := toInt(store.Get(txKey)) - store.Set(txKey, toByteArr(val+1)) + val := getIntFromStore(store, txKey) + setIntOnStore(store, txKey, val+1) // increment shared key - sharedVal := toInt(store.Get(sharedKey)) - store.Set(sharedKey, toByteArr(sharedVal+1)) + sharedVal := getIntFromStore(store, sharedKey) + setIntOnStore(store, sharedKey, sharedVal+1) // Emit an event with the incremented value and the unique ID ctx.EventManager().EmitEvent( @@ -75,8 +86,11 @@ func requireAttribute(t *testing.T, evts []abci.Event, name string, val string) func TestDeliverTxBatch(t *testing.T) { // test increments in the ante - //anteKey := []byte("ante-key") - anteOpt := func(bapp *BaseApp) {} + anteKey := []byte("ante-key") + + anteOpt := func(bapp *BaseApp) { + bapp.SetAnteHandler(anteHandler(capKey1, anteKey)) + } // test increments in the handler routerOpt := func(bapp *BaseApp) { diff --git a/tasks/scheduler.go b/tasks/scheduler.go index 575fc1547..12179295e 100644 --- a/tasks/scheduler.go +++ b/tasks/scheduler.go @@ -272,24 +272,27 @@ func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { for _, task := range tasks { // initialize the context ctx = ctx.WithTxIndex(task.Index) - - // non-blocking - cms := ctx.MultiStore().CacheMultiStore() abortCh := make(chan occ.Abort, len(s.multiVersionStores)) - // init version stores by store key - vs := make(map[store.StoreKey]*multiversion.VersionIndexedStore) - for storeKey, mvs := range s.multiVersionStores { - vs[storeKey] = mvs.VersionedIndexedStore(task.Index, task.Incarnation, abortCh) - } + // if there are no stores, don't try to wrap, because there's nothing to wrap + if len(s.multiVersionStores) > 0 { + // non-blocking + cms := ctx.MultiStore().CacheMultiStore() - // save off version store so we can ask it things later - task.VersionStores = vs - ms := cms.SetKVStores(func(k store.StoreKey, kvs sdk.KVStore) store.CacheWrap { - return vs[k] - }) + // init version stores by store key + vs := make(map[store.StoreKey]*multiversion.VersionIndexedStore) + for storeKey, mvs := range s.multiVersionStores { + vs[storeKey] = mvs.VersionedIndexedStore(task.Index, task.Incarnation, abortCh) + } - ctx = ctx.WithMultiStore(ms) + // save off version store so we can ask it things later + task.VersionStores = vs + ms := cms.SetKVStores(func(k store.StoreKey, kvs sdk.KVStore) store.CacheWrap { + return vs[k] + }) + + ctx = ctx.WithMultiStore(ms) + } task.AbortCh = abortCh task.Ctx = ctx diff --git a/tasks/scheduler_test.go b/tasks/scheduler_test.go index f132356ec..a2c861f44 100644 --- a/tasks/scheduler_test.go +++ b/tasks/scheduler_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "github.com/cosmos/cosmos-sdk/store/cachemulti" "testing" "github.com/stretchr/testify/require" @@ -11,7 +12,6 @@ import ( dbm "github.com/tendermint/tm-db" "github.com/cosmos/cosmos-sdk/store/cachekv" - "github.com/cosmos/cosmos-sdk/store/cachemulti" "github.com/cosmos/cosmos-sdk/store/dbadapter" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -31,14 +31,16 @@ func requestList(n int) []types.RequestDeliverTx { return tasks } -func initTestCtx() sdk.Context { +func initTestCtx(injectStores bool) sdk.Context { ctx := sdk.Context{}.WithContext(context.Background()) - db := dbm.NewMemDB() - mem := dbadapter.Store{DB: db} - stores := make(map[sdk.StoreKey]sdk.CacheWrapper) - stores[testStoreKey] = cachekv.NewStore(mem, testStoreKey, 1000) keys := make(map[string]sdk.StoreKey) - keys[testStoreKey.Name()] = testStoreKey + stores := make(map[sdk.StoreKey]sdk.CacheWrapper) + db := dbm.NewMemDB() + if injectStores { + mem := dbadapter.Store{DB: db} + stores[testStoreKey] = cachekv.NewStore(mem, testStoreKey, 1000) + keys[testStoreKey.Name()] = testStoreKey + } store := cachemulti.NewStore(db, stores, keys, nil, nil, nil) ctx = ctx.WithMultiStore(&store) return ctx @@ -51,13 +53,16 @@ func TestProcessAll(t *testing.T) { runs int requests []types.RequestDeliverTx deliverTxFunc mockDeliverTxFunc + addStores bool expectedErr error + assertions func(t *testing.T, ctx sdk.Context, res []types.ResponseDeliverTx) }{ { - name: "Test for conflicts", - workers: 50, - runs: 25, - requests: requestList(50), + name: "Test every tx accesses same key", + workers: 50, + runs: 25, + addStores: true, + requests: requestList(50), deliverTxFunc: func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { // all txs read and write to the same key to maximize conflicts kv := ctx.MultiStore().GetKVStore(testStoreKey) @@ -71,6 +76,38 @@ func TestProcessAll(t *testing.T) { Info: val, } }, + assertions: func(t *testing.T, ctx sdk.Context, res []types.ResponseDeliverTx) { + for idx, response := range res { + if idx == 0 { + require.Equal(t, "", response.Info) + } else { + // the info is what was read from the kv store by the tx + // each tx writes its own index, so the info should be the index of the previous tx + require.Equal(t, fmt.Sprintf("%d", idx-1), response.Info) + } + } + // confirm last write made it to the parent store + latest := ctx.MultiStore().GetKVStore(testStoreKey).Get(itemKey) + require.Equal(t, []byte("49"), latest) + }, + expectedErr: nil, + }, + { + name: "Test no stores on context should not panic", + workers: 50, + runs: 1, + addStores: false, + requests: requestList(50), + deliverTxFunc: func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { + return types.ResponseDeliverTx{ + Info: fmt.Sprintf("%d", ctx.TxIndex()), + } + }, + assertions: func(t *testing.T, ctx sdk.Context, res []types.ResponseDeliverTx) { + for idx, response := range res { + require.Equal(t, fmt.Sprintf("%d", idx), response.Info) + } + }, expectedErr: nil, }, } @@ -79,25 +116,15 @@ func TestProcessAll(t *testing.T) { t.Run(tt.name, func(t *testing.T) { for i := 0; i < tt.runs; i++ { s := NewScheduler(tt.workers, tt.deliverTxFunc) - ctx := initTestCtx() + ctx := initTestCtx(tt.addStores) res, err := s.ProcessAll(ctx, tt.requests) + require.Len(t, res, len(tt.requests)) + if !errors.Is(err, tt.expectedErr) { t.Errorf("Expected error %v, got %v", tt.expectedErr, err) } else { - require.Len(t, res, len(tt.requests)) - for idx, response := range res { - if idx == 0 { - require.Equal(t, "", response.Info) - } else { - // the info is what was read from the kv store by the tx - // each tx writes its own index, so the info should be the index of the previous tx - require.Equal(t, fmt.Sprintf("%d", idx-1), response.Info) - } - } - // confirm last write made it to the parent store - res := ctx.MultiStore().GetKVStore(testStoreKey).Get(itemKey) - require.Equal(t, []byte(fmt.Sprintf("%d", len(tt.requests)-1)), res) + tt.assertions(t, ctx, res) } } }) From 6ec162048d777172b8dedb0b0f756a7b97d748c3 Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Mon, 23 Oct 2023 11:40:06 -0400 Subject: [PATCH 16/36] Add occ flag check to context (#340) ## Describe your changes and provide context - Allows sei-chain to ask isOCCEnabled() so that it can choose to use the OCC logic - Sei-chain can set this to true according to desired logic ## Testing performed to validate your change - unit test that sets flag and verifies value --- types/context.go | 11 +++++++++++ types/context_test.go | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/types/context.go b/types/context.go index ef847d3a3..e36e88dc8 100644 --- a/types/context.go +++ b/types/context.go @@ -34,6 +34,7 @@ type Context struct { voteInfo []abci.VoteInfo gasMeter GasMeter blockGasMeter GasMeter + occEnabled bool checkTx bool recheckTx bool // if recheckTx == true, then checkTx must also be true minGasPrice DecCoins @@ -104,6 +105,10 @@ func (c Context) IsReCheckTx() bool { return c.recheckTx } +func (c Context) IsOCCEnabled() bool { + return c.occEnabled +} + func (c Context) MinGasPrices() DecCoins { return c.minGasPrice } @@ -281,6 +286,12 @@ func (c Context) WithIsCheckTx(isCheckTx bool) Context { return c } +// WithIsOCCEnabled enables or disables whether OCC is used as the concurrency algorithm +func (c Context) WithIsOCCEnabled(isOCCEnabled bool) Context { + c.occEnabled = isOCCEnabled + return c +} + // WithIsRecheckTx called with true will also set true on checkTx in order to // enforce the invariant that if recheckTx = true then checkTx = true as well. func (c Context) WithIsReCheckTx(isRecheckTx bool) Context { diff --git a/types/context_test.go b/types/context_test.go index 92f5dccaf..e49a82903 100644 --- a/types/context_test.go +++ b/types/context_test.go @@ -87,6 +87,7 @@ func (s *contextTestSuite) TestContextWithCustom() { height := int64(1) chainid := "chainid" ischeck := true + isOCC := true txbytes := []byte("txbytes") logger := mocks.NewMockLogger(ctrl) voteinfos := []abci.VoteInfo{{}} @@ -106,10 +107,13 @@ func (s *contextTestSuite) TestContextWithCustom() { WithGasMeter(meter). WithMinGasPrices(minGasPrices). WithBlockGasMeter(blockGasMeter). - WithHeaderHash(headerHash) + WithHeaderHash(headerHash). + WithIsOCCEnabled(isOCC) + s.Require().Equal(height, ctx.BlockHeight()) s.Require().Equal(chainid, ctx.ChainID()) s.Require().Equal(ischeck, ctx.IsCheckTx()) + s.Require().Equal(isOCC, ctx.IsOCCEnabled()) s.Require().Equal(txbytes, ctx.TxBytes()) s.Require().Equal(logger, ctx.Logger()) s.Require().Equal(voteinfos, ctx.VoteInfos()) From 9530aeb6933e366b1348572b0f53d4a37216fd5c Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Tue, 24 Oct 2023 10:52:14 -0500 Subject: [PATCH 17/36] [occ] Add struct field and helpers for estimate prefills (#341) ## Describe your changes and provide context This adds in the ability to prefill estimates based on metadata passed along with deliverTxBatch ## Testing performed to validate your change Unit Test to verify that multiversion store initialization is now idempotent, and works properly regardless of whether estimate prefill is enabled --- baseapp/abci.go | 15 +++++++-------- tasks/scheduler.go | 30 ++++++++++++++++++++++++------ tasks/scheduler_test.go | 16 ++++++++++------ types/tx_batch.go | 11 +++++++++-- 4 files changed, 50 insertions(+), 22 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index bd67eaaa7..cb76d677d 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -6,13 +6,14 @@ import ( "encoding/json" "errors" "fmt" - "github.com/cosmos/cosmos-sdk/tasks" "os" "sort" "strings" "syscall" "time" + "github.com/cosmos/cosmos-sdk/tasks" + "github.com/armon/go-metrics" "github.com/gogo/protobuf/proto" abci "github.com/tendermint/tendermint/abci/types" @@ -238,15 +239,13 @@ func (app *BaseApp) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abc // DeliverTxBatch executes multiple txs func (app *BaseApp) DeliverTxBatch(ctx sdk.Context, req sdk.DeliverTxBatchRequest) (res sdk.DeliverTxBatchResponse) { - reqList := make([]abci.RequestDeliverTx, 0, len(req.TxEntries)) - for _, tx := range req.TxEntries { - reqList = append(reqList, tx.Request) - } - scheduler := tasks.NewScheduler(app.concurrencyWorkers, app.DeliverTx) - txRes, err := scheduler.ProcessAll(ctx, reqList) + // This will basically no-op the actual prefill if the metadata for the txs is empty + + // process all txs, this will also initializes the MVS if prefill estimates was disabled + txRes, err := scheduler.ProcessAll(ctx, req.TxEntries) if err != nil { - //TODO: handle error + // TODO: handle error } responses := make([]*sdk.DeliverTxResult, 0, len(req.TxEntries)) diff --git a/tasks/scheduler.go b/tasks/scheduler.go index 12179295e..095deb545 100644 --- a/tasks/scheduler.go +++ b/tasks/scheduler.go @@ -57,7 +57,7 @@ func (dt *deliverTxTask) Increment() { // Scheduler processes tasks concurrently type Scheduler interface { - ProcessAll(ctx sdk.Context, reqs []types.RequestDeliverTx) ([]types.ResponseDeliverTx, error) + ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]types.ResponseDeliverTx, error) } type scheduler struct { @@ -99,11 +99,11 @@ func (s *scheduler) findConflicts(task *deliverTxTask) (bool, []int) { return valid, conflicts } -func toTasks(reqs []types.RequestDeliverTx) []*deliverTxTask { +func toTasks(reqs []*sdk.DeliverTxEntry) []*deliverTxTask { res := make([]*deliverTxTask, 0, len(reqs)) for idx, r := range reqs { res = append(res, &deliverTxTask{ - Request: r, + Request: r.Request, Index: idx, Status: statusPending, }) @@ -119,7 +119,10 @@ func collectResponses(tasks []*deliverTxTask) []types.ResponseDeliverTx { return res } -func (s *scheduler) initMultiVersionStore(ctx sdk.Context) { +func (s *scheduler) tryInitMultiVersionStore(ctx sdk.Context) { + if s.multiVersionStores != nil { + return + } mvs := make(map[sdk.StoreKey]multiversion.MultiVersionStore) keys := ctx.MultiStore().StoreKeys() for _, sk := range keys { @@ -146,8 +149,23 @@ func allValidated(tasks []*deliverTxTask) bool { return true } -func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []types.RequestDeliverTx) ([]types.ResponseDeliverTx, error) { - s.initMultiVersionStore(ctx) +func (s *scheduler) PrefillEstimates(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) { + // iterate over TXs, update estimated writesets where applicable + for i, req := range reqs { + mappedWritesets := req.EstimatedWritesets + // order shouldnt matter for storeKeys because each storeKey partitioned MVS is independent + for storeKey, writeset := range mappedWritesets { + // we use `-1` to indicate a prefill incarnation + s.multiVersionStores[storeKey].SetEstimatedWriteset(i, -1, writeset) + } + } +} + +func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]types.ResponseDeliverTx, error) { + // initialize mutli-version stores if they haven't been initialized yet + s.tryInitMultiVersionStore(ctx) + // prefill estimates + s.PrefillEstimates(ctx, reqs) tasks := toTasks(reqs) toExecute := tasks for !allValidated(tasks) { diff --git a/tasks/scheduler_test.go b/tasks/scheduler_test.go index a2c861f44..5cf2be6ba 100644 --- a/tasks/scheduler_test.go +++ b/tasks/scheduler_test.go @@ -4,9 +4,10 @@ import ( "context" "errors" "fmt" - "github.com/cosmos/cosmos-sdk/store/cachemulti" "testing" + "github.com/cosmos/cosmos-sdk/store/cachemulti" + "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/abci/types" dbm "github.com/tendermint/tm-db" @@ -21,12 +22,15 @@ type mockDeliverTxFunc func(ctx sdk.Context, req types.RequestDeliverTx) types.R var testStoreKey = sdk.NewKVStoreKey("mock") var itemKey = []byte("key") -func requestList(n int) []types.RequestDeliverTx { - tasks := make([]types.RequestDeliverTx, n) +func requestList(n int) []*sdk.DeliverTxEntry { + tasks := make([]*sdk.DeliverTxEntry, n) for i := 0; i < n; i++ { - tasks[i] = types.RequestDeliverTx{ - Tx: []byte(fmt.Sprintf("%d", i)), + tasks[i] = &sdk.DeliverTxEntry{ + Request: types.RequestDeliverTx{ + Tx: []byte(fmt.Sprintf("%d", i)), + }, } + } return tasks } @@ -51,7 +55,7 @@ func TestProcessAll(t *testing.T) { name string workers int runs int - requests []types.RequestDeliverTx + requests []*sdk.DeliverTxEntry deliverTxFunc mockDeliverTxFunc addStores bool expectedErr error diff --git a/types/tx_batch.go b/types/tx_batch.go index a54742fae..b053aa5fa 100644 --- a/types/tx_batch.go +++ b/types/tx_batch.go @@ -1,13 +1,20 @@ package types -import abci "github.com/tendermint/tendermint/abci/types" +import ( + "github.com/cosmos/cosmos-sdk/store/multiversion" + abci "github.com/tendermint/tendermint/abci/types" +) // DeliverTxEntry represents an individual transaction's request within a batch. // This can be extended to include tx-level tracing or metadata type DeliverTxEntry struct { - Request abci.RequestDeliverTx + Request abci.RequestDeliverTx + EstimatedWritesets MappedWritesets } +// EstimatedWritesets represents an estimated writeset for a transaction mapped by storekey to the writeset estimate. +type MappedWritesets map[StoreKey]multiversion.WriteSet + // DeliverTxBatchRequest represents a request object for a batch of transactions. // This can be extended to include request-level tracing or metadata type DeliverTxBatchRequest struct { From 3bdbc968e32d1961dc73f20c0fd4d8d4ab3fa598 Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Mon, 30 Oct 2023 15:37:14 -0400 Subject: [PATCH 18/36] Fix map access panic (#343) ## Describe your changes and provide context - `CollectIteratorItems` needs to hold an RLock to avoid a concurrent access panic ## Testing performed to validate your change - Reproduced through a sei-chain-side test (concurrent instantiates) --- store/multiversion/store.go | 45 +++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/store/multiversion/store.go b/store/multiversion/store.go index 7c14c6415..2ee1c31b9 100644 --- a/store/multiversion/store.go +++ b/store/multiversion/store.go @@ -206,7 +206,7 @@ func (s *Store) SetEstimatedWriteset(index int, incarnation int, writeset WriteS s.txWritesetKeys[index] = writeSetKeys } -// GetWritesetKeys implements MultiVersionStore. +// GetAllWritesetKeys implements MultiVersionStore. func (s *Store) GetAllWritesetKeys() map[int][]string { s.mtx.RLock() defer s.mtx.RUnlock() @@ -243,10 +243,13 @@ func (s *Store) GetIterateset(index int) Iterateset { // CollectIteratorItems implements MultiVersionStore. It will return a memDB containing all of the keys present in the multiversion store within the iteration range prior to (exclusive of) the index. func (s *Store) CollectIteratorItems(index int) *db.MemDB { + s.mtx.RLock() + defer s.mtx.RUnlock() + sortedItems := db.NewMemDB() // get all writeset keys prior to index - keys := s.GetAllWritesetKeys() + keys := s.txWritesetKeys for i := 0; i < index; i++ { indexedWriteset, ok := keys[i] if !ok { @@ -316,21 +319,27 @@ func (s *Store) validateIterator(index int, tracker iterationTracker) bool { } } -// TODO: do we want to return bool + []int where bool indicates whether it was valid and then []int indicates only ones for which we need to wait due to estimates? - yes i think so? -func (s *Store) ValidateTransactionState(index int) (bool, []int) { - defer telemetry.MeasureSince(time.Now(), "store", "mvs", "validate") - conflictSet := map[int]struct{}{} - valid := true +func (s *Store) checkIteratorAtIndex(index int) bool { + s.mtx.RLock() + defer s.mtx.RUnlock() - // TODO: can we parallelize for all iterators? - iterateset := s.GetIterateset(index) + valid := true + iterateset := s.txIterateSets[index] for _, iterationTracker := range iterateset { iteratorValid := s.validateIterator(index, iterationTracker) valid = valid && iteratorValid } + return valid +} + +func (s *Store) checkReadsetAtIndex(index int) (bool, []int) { + s.mtx.RLock() + defer s.mtx.RUnlock() + + conflictSet := make(map[int]struct{}) + readset := s.txReadSets[index] + valid := true - // validate readset - readset := s.GetReadset(index) // iterate over readset and check if the value is the same as the latest value relateive to txIndex in the multiversion store for key, value := range readset { // get the latest value from the multiversion store @@ -357,16 +366,28 @@ func (s *Store) ValidateTransactionState(index int) (bool, []int) { } } - // convert conflictset into sorted indices conflictIndices := make([]int, 0, len(conflictSet)) for index := range conflictSet { conflictIndices = append(conflictIndices, index) } sort.Ints(conflictIndices) + return valid, conflictIndices } +// TODO: do we want to return bool + []int where bool indicates whether it was valid and then []int indicates only ones for which we need to wait due to estimates? - yes i think so? +func (s *Store) ValidateTransactionState(index int) (bool, []int) { + defer telemetry.MeasureSince(time.Now(), "store", "mvs", "validate") + + // TODO: can we parallelize for all iterators? + iteratorValid := s.checkIteratorAtIndex(index) + + readsetValid, conflictIndices := s.checkReadsetAtIndex(index) + + return iteratorValid && readsetValid, conflictIndices +} + func (s *Store) WriteLatestToStore() { s.mtx.Lock() defer s.mtx.Unlock() From 954c5a922ba8501b213353ee157eec1ebb298f3e Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Fri, 3 Nov 2023 09:37:19 -0500 Subject: [PATCH 19/36] Gen estimates writeset (#344) ## Describe your changes and provide context This adds the accesscontrol module behavior to add the tx writeset generation ## Testing performed to validate your change Unit tests + integration with sei-chain and loadtest cluster testing --- tasks/scheduler.go | 2 +- types/accesscontrol/validation.go | 1 + x/accesscontrol/keeper/keeper.go | 63 +++++++++++++++++++++++++++ x/accesscontrol/keeper/keeper_test.go | 37 ++++++++++++++++ x/accesscontrol/keeper/options.go | 8 ++++ 5 files changed, 110 insertions(+), 1 deletion(-) diff --git a/tasks/scheduler.go b/tasks/scheduler.go index 095deb545..a38ef7f96 100644 --- a/tasks/scheduler.go +++ b/tasks/scheduler.go @@ -228,7 +228,7 @@ func (s *scheduler) validateAll(tasks []*deliverTxTask) ([]*deliverTxTask, error } } else if len(conflicts) == 0 { tasks[i].Status = statusValidated - } + } // TODO: do we need to have handling for conflicts existing here? case statusWaiting: // if conflicts are done, then this task is ready to run again diff --git a/types/accesscontrol/validation.go b/types/accesscontrol/validation.go index 40a525a92..ec83885c6 100644 --- a/types/accesscontrol/validation.go +++ b/types/accesscontrol/validation.go @@ -10,6 +10,7 @@ var ( ) type StoreKeyToResourceTypePrefixMap map[string]map[ResourceType][]byte +type ResourceTypeToStoreKeyMap map[ResourceType]string func DefaultStoreKeyToResourceTypePrefixMap() StoreKeyToResourceTypePrefixMap { return StoreKeyToResourceTypePrefixMap{ diff --git a/x/accesscontrol/keeper/keeper.go b/x/accesscontrol/keeper/keeper.go index 2a2719f7e..f047f2c46 100644 --- a/x/accesscontrol/keeper/keeper.go +++ b/x/accesscontrol/keeper/keeper.go @@ -12,6 +12,7 @@ import ( "github.com/yourbasic/graph" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/store/multiversion" "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" acltypes "github.com/cosmos/cosmos-sdk/types/accesscontrol" @@ -40,6 +41,7 @@ type ( MessageDependencyGeneratorMapper DependencyGeneratorMap AccountKeeper authkeeper.AccountKeeper StakingKeeper stakingkeeper.Keeper + ResourceTypeStoreKeyMapping acltypes.ResourceTypeToStoreKeyMap } ) @@ -493,6 +495,67 @@ func (k Keeper) IterateWasmDependencies(ctx sdk.Context, handler func(wasmDepend } } +type storeKeyMap map[string]sdk.StoreKey + +func (k Keeper) GetStoreKeyMap(ctx sdk.Context) storeKeyMap { + storeKeyMap := make(storeKeyMap) + for _, storeKey := range ctx.MultiStore().StoreKeys() { + storeKeyMap[storeKey.Name()] = storeKey + } + return storeKeyMap +} + +func (k Keeper) UpdateWritesetsWithAccessOps(accessOps []acltypes.AccessOperation, mappedWritesets sdk.MappedWritesets, storeKeyMap storeKeyMap) sdk.MappedWritesets { + for _, accessOp := range accessOps { + // we only want writes and unknowns (assumed writes) + if accessOp.AccessType != acltypes.AccessType_WRITE && accessOp.AccessType != acltypes.AccessType_UNKNOWN { + continue + } + // the accessOps should only have SPECIFIC identifiers (we don't want wildcards) + if accessOp.IdentifierTemplate == "*" { + continue + } + // check the resource type to store key map for potential store key + if storeKeyStr, ok := k.ResourceTypeStoreKeyMapping[accessOp.ResourceType]; ok { + // check that we have a storekey corresponding to that string + if storeKey, ok2 := storeKeyMap[storeKeyStr]; ok2 { + // if we have a StoreKey, add it to the writeset - writing empty bytes is ok because it will be saved as EstimatedWriteset + if _, ok := mappedWritesets[storeKey]; !ok { + mappedWritesets[storeKey] = make(multiversion.WriteSet) + } + mappedWritesets[storeKey][accessOp.IdentifierTemplate] = []byte{} + } + } + + } + return mappedWritesets +} + +// GenerateEstimatedWritesets utilizes the existing patterns for access operation generation to estimate the writesets for a transaction +func (k Keeper) GenerateEstimatedWritesets(ctx sdk.Context, txDecoder sdk.TxDecoder, anteDepGen sdk.AnteDepGenerator, txIndex int, txBytes []byte) (sdk.MappedWritesets, error) { + storeKeyMap := k.GetStoreKeyMap(ctx) + writesets := make(sdk.MappedWritesets) + tx, err := txDecoder(txBytes) + if err != nil { + return nil, err + } + // generate antedeps accessOps for tx + anteDeps, err := anteDepGen([]acltypes.AccessOperation{}, tx, txIndex) + if err != nil { + return nil, err + } + writesets = k.UpdateWritesetsWithAccessOps(anteDeps, writesets, storeKeyMap) + + // generate accessOps for each message + msgs := tx.GetMsgs() + for _, msg := range msgs { + msgDependencies := k.GetMessageDependencies(ctx, msg) + // update estimated writeset for each message deps + writesets = k.UpdateWritesetsWithAccessOps(msgDependencies, writesets, storeKeyMap) + } + return writesets, nil +} + func (k Keeper) BuildDependencyDag(ctx sdk.Context, txDecoder sdk.TxDecoder, anteDepGen sdk.AnteDepGenerator, txs [][]byte) (*types.Dag, error) { defer MeasureBuildDagDuration(time.Now(), "BuildDependencyDag") // contains the latest msg index for a specific Access Operation diff --git a/x/accesscontrol/keeper/keeper_test.go b/x/accesscontrol/keeper/keeper_test.go index 52e714f5c..eaa659b2a 100644 --- a/x/accesscontrol/keeper/keeper_test.go +++ b/x/accesscontrol/keeper/keeper_test.go @@ -20,6 +20,7 @@ import ( aclkeeper "github.com/cosmos/cosmos-sdk/x/accesscontrol/keeper" acltestutil "github.com/cosmos/cosmos-sdk/x/accesscontrol/testutil" "github.com/cosmos/cosmos-sdk/x/accesscontrol/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -2670,6 +2671,42 @@ func (suite *KeeperTestSuite) TestBuildSelectorOps_AccessOperationSelectorType_C req.NoError(err) } +func TestGenerateEstimatedDependencies(t *testing.T) { + app := simapp.Setup(false) + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + + accounts := simapp.AddTestAddrsIncremental(app, ctx, 2, sdk.NewInt(30000000)) + // setup test txs + msgs := []sdk.Msg{ + banktypes.NewMsgSend(accounts[0], accounts[1], sdk.NewCoins(sdk.NewCoin("usei", sdk.NewInt(1)))), + } + // set up testing mapping + app.AccessControlKeeper.ResourceTypeStoreKeyMapping = map[acltypes.ResourceType]string{ + acltypes.ResourceType_KV_BANK_BALANCES: banktypes.StoreKey, + acltypes.ResourceType_KV_AUTH_ADDRESS_STORE: authtypes.StoreKey, + } + + storeKeyMap := app.AccessControlKeeper.GetStoreKeyMap(ctx) + + txBuilder := simapp.MakeTestEncodingConfig().TxConfig.NewTxBuilder() + err := txBuilder.SetMsgs(msgs...) + require.NoError(t, err) + bz, err := simapp.MakeTestEncodingConfig().TxConfig.TxEncoder()(txBuilder.GetTx()) + require.NoError(t, err) + + writesets, err := app.AccessControlKeeper.GenerateEstimatedWritesets(ctx, simapp.MakeTestEncodingConfig().TxConfig.TxDecoder(), app.GetAnteDepGenerator(), 0, bz) + require.NoError(t, err) + + // check writesets + require.Equal(t, 2, len(writesets)) + bankWritesets := writesets[storeKeyMap[banktypes.StoreKey]] + require.Equal(t, 3, len(bankWritesets)) + + authWritesets := writesets[storeKeyMap[authtypes.StoreKey]] + require.Equal(t, 1, len(authWritesets)) + +} + func TestKeeperTestSuite(t *testing.T) { t.Parallel() suite.Run(t, new(KeeperTestSuite)) diff --git a/x/accesscontrol/keeper/options.go b/x/accesscontrol/keeper/options.go index 365280ab3..6dd7f3b36 100644 --- a/x/accesscontrol/keeper/options.go +++ b/x/accesscontrol/keeper/options.go @@ -1,5 +1,7 @@ package keeper +import acltypes "github.com/cosmos/cosmos-sdk/types/accesscontrol" + type optsFn func(*Keeper) func (f optsFn) Apply(keeper *Keeper) { @@ -25,3 +27,9 @@ func (oldGenerator DependencyGeneratorMap) Merge(newGenerator DependencyGenerato } return oldGenerator } + +func WithResourceTypeToStoreKeyMap(resourceTypeStoreKeyMapping acltypes.ResourceTypeToStoreKeyMap) optsFn { + return optsFn(func(k *Keeper) { + k.ResourceTypeStoreKeyMapping = resourceTypeStoreKeyMapping + }) +} From be45bb5c4fa3fce9d3c074f5ca1b76a81ace71dd Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Mon, 6 Nov 2023 17:02:10 -0500 Subject: [PATCH 20/36] [OCC] Add trace spans to scheduler (#347) ## Describe your changes and provide context - Adds trace span for `SchedulerValidate` - Adds trace span for `SchedulerExecute` - Mild refactor (extracted methods) to make it easier to defer span ending ## Testing performed to validate your change Example trace (run locally) ![image](https://github.com/sei-protocol/sei-cosmos/assets/6051744/b8a032f1-71b1-4e95-b12e-357455ebcc6d) Example attributes of SchedulerExecute operation ![image](https://github.com/sei-protocol/sei-cosmos/assets/6051744/68992e84-4000-44c1-8597-9d4c10583a66) --- baseapp/abci.go | 2 +- tasks/scheduler.go | 128 +++++++++++++++++++++++++--------------- tasks/scheduler_test.go | 16 ++++- 3 files changed, 93 insertions(+), 53 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index cb76d677d..5495ee0b0 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -239,7 +239,7 @@ func (app *BaseApp) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abc // DeliverTxBatch executes multiple txs func (app *BaseApp) DeliverTxBatch(ctx sdk.Context, req sdk.DeliverTxBatchRequest) (res sdk.DeliverTxBatchResponse) { - scheduler := tasks.NewScheduler(app.concurrencyWorkers, app.DeliverTx) + scheduler := tasks.NewScheduler(app.concurrencyWorkers, app.TracingInfo, app.DeliverTx) // This will basically no-op the actual prefill if the metadata for the txs is empty // process all txs, this will also initializes the MVS if prefill estimates was disabled diff --git a/tasks/scheduler.go b/tasks/scheduler.go index a38ef7f96..7b1afc0d2 100644 --- a/tasks/scheduler.go +++ b/tasks/scheduler.go @@ -1,15 +1,20 @@ package tasks import ( + "crypto/sha256" + "fmt" "sort" "github.com/tendermint/tendermint/abci/types" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" "golang.org/x/sync/errgroup" "github.com/cosmos/cosmos-sdk/store/multiversion" store "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/occ" + "github.com/cosmos/cosmos-sdk/utils/tracing" ) type status string @@ -33,6 +38,7 @@ const ( type deliverTxTask struct { Ctx sdk.Context + Span trace.Span AbortCh chan occ.Abort Status status @@ -64,13 +70,15 @@ type scheduler struct { deliverTx func(ctx sdk.Context, req types.RequestDeliverTx) (res types.ResponseDeliverTx) workers int multiVersionStores map[sdk.StoreKey]multiversion.MultiVersionStore + tracingInfo *tracing.Info } // NewScheduler creates a new scheduler -func NewScheduler(workers int, deliverTxFunc func(ctx sdk.Context, req types.RequestDeliverTx) (res types.ResponseDeliverTx)) Scheduler { +func NewScheduler(workers int, tracingInfo *tracing.Info, deliverTxFunc func(ctx sdk.Context, req types.RequestDeliverTx) (res types.ResponseDeliverTx)) Scheduler { return &scheduler{ - workers: workers, - deliverTx: deliverTxFunc, + workers: workers, + deliverTx: deliverTxFunc, + tracingInfo: tracingInfo, } } @@ -181,7 +189,7 @@ func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]t // validate returns any that should be re-executed // note this processes ALL tasks, not just those recently executed - toExecute, err = s.validateAll(tasks) + toExecute, err = s.validateAll(ctx, tasks) if err != nil { return nil, err } @@ -195,7 +203,11 @@ func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]t return collectResponses(tasks), nil } -func (s *scheduler) validateAll(tasks []*deliverTxTask) ([]*deliverTxTask, error) { +func (s *scheduler) validateAll(ctx sdk.Context, tasks []*deliverTxTask) ([]*deliverTxTask, error) { + spanCtx, span := s.tracingInfo.StartWithContext("SchedulerValidate", ctx.TraceSpanContext()) + ctx = ctx.WithTraceSpanContext(spanCtx) + defer span.End() + var res []*deliverTxTask // find first non-validated entry @@ -263,24 +275,7 @@ func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { if !ok { return nil } - - resp := s.deliverTx(task.Ctx, task.Request) - - close(task.AbortCh) - - if abt, ok := <-task.AbortCh; ok { - task.Status = statusAborted - task.Abort = &abt - continue - } - - // write from version store to multiversion stores - for _, v := range task.VersionStores { - v.WriteToMultiVersionStore() - } - - task.Status = statusExecuted - task.Response = &resp + s.executeTask(task) } } }) @@ -288,32 +283,7 @@ func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { grp.Go(func() error { defer close(ch) for _, task := range tasks { - // initialize the context - ctx = ctx.WithTxIndex(task.Index) - abortCh := make(chan occ.Abort, len(s.multiVersionStores)) - - // if there are no stores, don't try to wrap, because there's nothing to wrap - if len(s.multiVersionStores) > 0 { - // non-blocking - cms := ctx.MultiStore().CacheMultiStore() - - // init version stores by store key - vs := make(map[store.StoreKey]*multiversion.VersionIndexedStore) - for storeKey, mvs := range s.multiVersionStores { - vs[storeKey] = mvs.VersionedIndexedStore(task.Index, task.Incarnation, abortCh) - } - - // save off version store so we can ask it things later - task.VersionStores = vs - ms := cms.SetKVStores(func(k store.StoreKey, kvs sdk.KVStore) store.CacheWrap { - return vs[k] - }) - - ctx = ctx.WithMultiStore(ms) - } - - task.AbortCh = abortCh - task.Ctx = ctx + s.prepareTask(ctx, task) select { case <-gCtx.Done(): @@ -330,3 +300,63 @@ func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { return nil } + +// prepareTask initializes the context and version stores for a task +func (s *scheduler) prepareTask(ctx sdk.Context, task *deliverTxTask) { + // initialize the context + ctx = ctx.WithTxIndex(task.Index) + abortCh := make(chan occ.Abort, len(s.multiVersionStores)) + spanCtx, span := s.tracingInfo.StartWithContext("SchedulerExecute", ctx.TraceSpanContext()) + span.SetAttributes(attribute.String("txHash", fmt.Sprintf("%X", sha256.Sum256(task.Request.Tx)))) + span.SetAttributes(attribute.Int("txIndex", task.Index)) + span.SetAttributes(attribute.Int("txIncarnation", task.Incarnation)) + ctx = ctx.WithTraceSpanContext(spanCtx) + + // if there are no stores, don't try to wrap, because there's nothing to wrap + if len(s.multiVersionStores) > 0 { + // non-blocking + cms := ctx.MultiStore().CacheMultiStore() + + // init version stores by store key + vs := make(map[store.StoreKey]*multiversion.VersionIndexedStore) + for storeKey, mvs := range s.multiVersionStores { + vs[storeKey] = mvs.VersionedIndexedStore(task.Index, task.Incarnation, abortCh) + } + + // save off version store so we can ask it things later + task.VersionStores = vs + ms := cms.SetKVStores(func(k store.StoreKey, kvs sdk.KVStore) store.CacheWrap { + return vs[k] + }) + + ctx = ctx.WithMultiStore(ms) + } + + task.AbortCh = abortCh + task.Ctx = ctx + task.Span = span +} + +// executeTask executes a single task +func (s *scheduler) executeTask(task *deliverTxTask) { + if task.Span != nil { + defer task.Span.End() + } + resp := s.deliverTx(task.Ctx, task.Request) + + close(task.AbortCh) + + if abt, ok := <-task.AbortCh; ok { + task.Status = statusAborted + task.Abort = &abt + return + } + + // write from version store to multiversion stores + for _, v := range task.VersionStores { + v.WriteToMultiVersionStore() + } + + task.Status = statusExecuted + task.Response = &resp +} diff --git a/tasks/scheduler_test.go b/tasks/scheduler_test.go index 5cf2be6ba..accc8bf3e 100644 --- a/tasks/scheduler_test.go +++ b/tasks/scheduler_test.go @@ -6,15 +6,17 @@ import ( "fmt" "testing" - "github.com/cosmos/cosmos-sdk/store/cachemulti" - "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/abci/types" dbm "github.com/tendermint/tm-db" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/trace" "github.com/cosmos/cosmos-sdk/store/cachekv" + "github.com/cosmos/cosmos-sdk/store/cachemulti" "github.com/cosmos/cosmos-sdk/store/dbadapter" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/utils/tracing" ) type mockDeliverTxFunc func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx @@ -119,7 +121,15 @@ func TestProcessAll(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { for i := 0; i < tt.runs; i++ { - s := NewScheduler(tt.workers, tt.deliverTxFunc) + // set a tracer provider + tp := trace.NewNoopTracerProvider() + otel.SetTracerProvider(trace.NewNoopTracerProvider()) + tr := tp.Tracer("scheduler-test") + ti := &tracing.Info{ + Tracer: &tr, + } + + s := NewScheduler(tt.workers, ti, tt.deliverTxFunc) ctx := initTestCtx(tt.addStores) res, err := s.ProcessAll(ctx, tt.requests) From 061ef70e375d660ebeb2562401ab00f207297f0f Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Fri, 10 Nov 2023 15:43:10 -0600 Subject: [PATCH 21/36] [occ] Fix parent store readset validation (#348) ## Describe your changes and provide context This fixes the validation to remove a panic for a case that can actually occur if a transaction writes a key that is later read, and that writing transaction is reverted and then the readset validation reads from parent store. In this case, the readset would have a conflict based on the data available in parent store, so we shouldn't panic. This also adds in the resource types needed for the new DEX_MEM keys ## Testing performed to validate your change Tested in loadtest cluster --- proto/cosmos/accesscontrol/constants.proto | 4 + store/multiversion/store.go | 4 +- store/multiversion/store_test.go | 47 ++++- types/accesscontrol/constants.pb.go | 192 +++++++++++---------- types/accesscontrol/resource.go | 4 + 5 files changed, 154 insertions(+), 97 deletions(-) diff --git a/proto/cosmos/accesscontrol/constants.proto b/proto/cosmos/accesscontrol/constants.proto index a8820fb48..f8c977a73 100644 --- a/proto/cosmos/accesscontrol/constants.proto +++ b/proto/cosmos/accesscontrol/constants.proto @@ -130,7 +130,11 @@ enum ResourceType { KV_DEX_SHORT_ORDER_COUNT = 92; // child of KV_DEX KV_BANK_DEFERRED = 93; // child of KV + reserved 94; KV_BANK_DEFERRED_MODULE_TX_INDEX = 95; // child of KV_BANK_DEFERRED + + KV_DEX_MEM_CONTRACTS_TO_PROCESS = 96; // child of KV_DEX_MEM + KV_DEX_MEM_DOWNSTREAM_CONTRACTS = 97; // child of KV_DEX_MEM } enum WasmMessageSubtype { diff --git a/store/multiversion/store.go b/store/multiversion/store.go index 2ee1c31b9..bc5e8ee4a 100644 --- a/store/multiversion/store.go +++ b/store/multiversion/store.go @@ -345,10 +345,10 @@ func (s *Store) checkReadsetAtIndex(index int) (bool, []int) { // get the latest value from the multiversion store latestValue := s.GetLatestBeforeIndex(index, []byte(key)) if latestValue == nil { - // TODO: maybe we don't even do this check? + // this is possible if we previously read a value from a transaction write that was later reverted, so this time we read from parent store parentVal := s.parentStore.Get([]byte(key)) if !bytes.Equal(parentVal, value) { - panic("there shouldn't be readset conflicts with parent kv store, since it shouldn't change") + valid = false } } else { // if estimate, mark as conflict index - but don't invalidate diff --git a/store/multiversion/store_test.go b/store/multiversion/store_test.go index 84e9f77ac..ae0f3afda 100644 --- a/store/multiversion/store_test.go +++ b/store/multiversion/store_test.go @@ -231,10 +231,51 @@ func TestMultiVersionStoreValidateState(t *testing.T) { valid, conflicts = mvs.ValidateTransactionState(5) require.False(t, valid) require.Equal(t, []int{4}, conflicts) +} + +func TestMultiVersionStoreParentValidationMismatch(t *testing.T) { + parentKVStore := dbadapter.Store{DB: dbm.NewMemDB()} + mvs := multiversion.NewMultiVersionStore(parentKVStore) + + parentKVStore.Set([]byte("key2"), []byte("value0")) + parentKVStore.Set([]byte("key3"), []byte("value3")) + parentKVStore.Set([]byte("key4"), []byte("value4")) + parentKVStore.Set([]byte("key5"), []byte("value5")) + + writeset := make(multiversion.WriteSet) + writeset["key1"] = []byte("value1") + writeset["key2"] = []byte("value2") + writeset["key3"] = nil + mvs.SetWriteset(1, 2, writeset) - // assert panic for parent store mismatch - parentKVStore.Set([]byte("key5"), []byte("value6")) - require.Panics(t, func() { mvs.ValidateTransactionState(5) }) + readset := make(multiversion.ReadSet) + readset["key1"] = []byte("value1") + readset["key2"] = []byte("value2") + readset["key3"] = nil + readset["key4"] = []byte("value4") + readset["key5"] = []byte("value5") + mvs.SetReadset(5, readset) + + // assert no readset is valid + valid, conflicts := mvs.ValidateTransactionState(4) + require.True(t, valid) + require.Empty(t, conflicts) + + // assert readset index 5 is valid + valid, conflicts = mvs.ValidateTransactionState(5) + require.True(t, valid) + require.Empty(t, conflicts) + + // overwrite tx writeset for tx1 - no longer writes key1 + writeset2 := make(multiversion.WriteSet) + writeset2["key2"] = []byte("value2") + writeset2["key3"] = nil + mvs.SetWriteset(1, 3, writeset2) + + // assert readset index 5 is invalid - because of mismatch with parent store + valid, conflicts = mvs.ValidateTransactionState(5) + require.False(t, valid) + require.Empty(t, conflicts) } func TestMVSValidationWithOnlyEstimate(t *testing.T) { diff --git a/types/accesscontrol/constants.pb.go b/types/accesscontrol/constants.pb.go index 1625561ed..c310dc868 100644 --- a/types/accesscontrol/constants.pb.go +++ b/types/accesscontrol/constants.pb.go @@ -195,6 +195,8 @@ const ( ResourceType_KV_DEX_SHORT_ORDER_COUNT ResourceType = 92 ResourceType_KV_BANK_DEFERRED ResourceType = 93 ResourceType_KV_BANK_DEFERRED_MODULE_TX_INDEX ResourceType = 95 + ResourceType_KV_DEX_MEM_CONTRACTS_TO_PROCESS ResourceType = 96 + ResourceType_KV_DEX_MEM_DOWNSTREAM_CONTRACTS ResourceType = 97 ) var ResourceType_name = map[int32]string{ @@ -290,6 +292,8 @@ var ResourceType_name = map[int32]string{ 92: "KV_DEX_SHORT_ORDER_COUNT", 93: "KV_BANK_DEFERRED", 95: "KV_BANK_DEFERRED_MODULE_TX_INDEX", + 96: "KV_DEX_MEM_CONTRACTS_TO_PROCESS", + 97: "KV_DEX_MEM_DOWNSTREAM_CONTRACTS", } var ResourceType_value = map[string]int32{ @@ -385,6 +389,8 @@ var ResourceType_value = map[string]int32{ "KV_DEX_SHORT_ORDER_COUNT": 92, "KV_BANK_DEFERRED": 93, "KV_BANK_DEFERRED_MODULE_TX_INDEX": 95, + "KV_DEX_MEM_CONTRACTS_TO_PROCESS": 96, + "KV_DEX_MEM_DOWNSTREAM_CONTRACTS": 97, } func (x ResourceType) String() string { @@ -432,96 +438,98 @@ func init() { } var fileDescriptor_36568f7561081112 = []byte{ - // 1445 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x56, 0x5b, 0x73, 0x13, 0xbf, - 0x15, 0xcf, 0xfd, 0xa2, 0x04, 0x38, 0x28, 0xdc, 0x13, 0x0c, 0x84, 0x14, 0x68, 0x80, 0x84, 0x4b, - 0xaf, 0xd0, 0x96, 0xca, 0xab, 0x63, 0x7b, 0xe3, 0x5d, 0x69, 0x2d, 0x69, 0x7d, 0xa1, 0xed, 0x68, - 0x12, 0xd7, 0x43, 0x99, 0x92, 0x98, 0x89, 0x4d, 0xa7, 0xfd, 0x0c, 0x7d, 0xe9, 0xc7, 0xea, 0x23, - 0x8f, 0x7d, 0x64, 0xe0, 0x8b, 0x74, 0xb4, 0x2b, 0x9b, 0xb5, 0x09, 0x7f, 0x9e, 0x12, 0x9f, 0xdf, - 0x4f, 0x67, 0x75, 0x7e, 0xe7, 0x26, 0xb2, 0xd3, 0xed, 0x0f, 0x8e, 0xfb, 0x83, 0xfd, 0xc3, 0x6e, - 0xb7, 0x37, 0x18, 0x74, 0xfb, 0x27, 0xc3, 0xd3, 0xfe, 0xbb, 0xfd, 0x6e, 0xff, 0x64, 0x30, 0x3c, - 0x3c, 0x19, 0x0e, 0xf6, 0xde, 0x9f, 0xf6, 0x87, 0x7d, 0xba, 0x95, 0xb3, 0xf6, 0x26, 0x58, 0x7b, - 0xff, 0x78, 0x7a, 0xd4, 0x1b, 0x1e, 0x3e, 0xdd, 0x7d, 0x41, 0x08, 0xcb, 0x00, 0xf3, 0xaf, 0xf7, - 0x3d, 0xba, 0x46, 0x96, 0x53, 0x51, 0x17, 0xb2, 0x25, 0x60, 0x86, 0xae, 0x90, 0x05, 0x85, 0x8c, - 0xc3, 0x2c, 0x5d, 0x25, 0x8b, 0x2d, 0x15, 0x1a, 0x84, 0x39, 0x4a, 0xc8, 0x52, 0x20, 0xe3, 0x38, - 0x34, 0x30, 0xbf, 0xfb, 0xef, 0x39, 0xb2, 0x99, 0x1f, 0x96, 0xef, 0x7b, 0xa7, 0x87, 0xc3, 0xb7, - 0xfd, 0x13, 0xdd, 0x7b, 0xd7, 0xeb, 0x0e, 0xfb, 0xa7, 0x99, 0xb7, 0x15, 0xb2, 0x20, 0xa4, 0x40, - 0x98, 0xa1, 0x4b, 0x64, 0xee, 0xa0, 0x01, 0xb3, 0xf4, 0x32, 0xb9, 0x78, 0xd0, 0xb0, 0x65, 0x0c, - 0x6a, 0xcf, 0x9f, 0x59, 0xc6, 0xb9, 0x42, 0xad, 0x61, 0x8e, 0x96, 0xc8, 0x8d, 0x83, 0x86, 0x8d, - 0x50, 0x54, 0x4d, 0xcd, 0x26, 0x0a, 0x2b, 0x61, 0x1b, 0xf9, 0x18, 0x9f, 0xa7, 0xd7, 0xc9, 0x65, - 0x8d, 0x82, 0xa3, 0x9a, 0x3e, 0xba, 0x40, 0xb7, 0x49, 0xc9, 0x43, 0xdf, 0x3b, 0xbe, 0x48, 0x2f, - 0x11, 0x08, 0xa4, 0x30, 0x8a, 0x05, 0x66, 0x6c, 0x5d, 0xa2, 0x37, 0xc8, 0x95, 0x83, 0x86, 0x8d, - 0x51, 0x6b, 0x56, 0x45, 0x1b, 0x48, 0xc1, 0x43, 0x13, 0x4a, 0xc1, 0x22, 0x58, 0x76, 0x58, 0x20, - 0x85, 0x36, 0x4c, 0x18, 0xab, 0x8d, 0x0a, 0x45, 0xd5, 0x1a, 0x69, 0x6b, 0xd8, 0x86, 0x15, 0x7a, - 0x85, 0xd0, 0xb1, 0x37, 0x85, 0x15, 0x54, 0x28, 0x02, 0x84, 0xd5, 0xdd, 0x4f, 0x1b, 0x64, 0x5d, - 0xf5, 0x06, 0xfd, 0x0f, 0xa7, 0xdd, 0x5e, 0x16, 0xfe, 0x32, 0x99, 0x67, 0xa2, 0x93, 0x47, 0x5f, - 0x6f, 0xc2, 0xac, 0x33, 0xc4, 0xbd, 0xe3, 0x5c, 0x44, 0xde, 0xfb, 0xa7, 0xfb, 0x7f, 0xde, 0x49, - 0x5e, 0x6f, 0xda, 0x32, 0x13, 0x75, 0x58, 0xa0, 0xe7, 0x09, 0xa9, 0x37, 0xad, 0x36, 0xac, 0x1e, - 0x8a, 0x2a, 0x2c, 0x7a, 0xb0, 0xc5, 0x74, 0x0c, 0x4b, 0xf4, 0x1c, 0x59, 0xad, 0x37, 0xad, 0x54, - 0x2c, 0x88, 0x10, 0x96, 0x9d, 0x93, 0x7a, 0xd3, 0xf2, 0xec, 0x4e, 0xeb, 0x64, 0xa5, 0xde, 0xb4, - 0x98, 0xc8, 0xa0, 0x06, 0xab, 0x74, 0x83, 0x5c, 0xa8, 0x37, 0xad, 0x91, 0x75, 0x14, 0x15, 0x16, - 0x18, 0xa9, 0x3a, 0x40, 0x5c, 0x48, 0xe3, 0xd3, 0xb6, 0x29, 0x0d, 0x5a, 0xc3, 0x54, 0x15, 0x8d, - 0x86, 0x35, 0x7a, 0x93, 0x5c, 0xff, 0x8a, 0xb1, 0x6a, 0x55, 0x61, 0x95, 0x99, 0x9c, 0xa5, 0x61, - 0xdd, 0x65, 0xed, 0x2b, 0x5c, 0x41, 0xe4, 0xa8, 0x34, 0x9c, 0x73, 0x59, 0xf9, 0x7a, 0x59, 0xcb, - 0x31, 0x72, 0xa7, 0x42, 0x29, 0xe0, 0x3c, 0xbd, 0x46, 0x2e, 0x15, 0xa0, 0x26, 0x8b, 0x42, 0xce, - 0x8c, 0x54, 0x70, 0xc1, 0x47, 0xc4, 0x52, 0x53, 0x03, 0xf0, 0x1e, 0xdc, 0x8f, 0x51, 0x5e, 0xac, - 0x36, 0x52, 0x21, 0x5c, 0xa4, 0x94, 0x9c, 0xf7, 0xb2, 0x58, 0x9d, 0x26, 0x49, 0xd4, 0x01, 0x4a, - 0x2f, 0x92, 0x73, 0x23, 0x1b, 0x47, 0x21, 0x63, 0xd8, 0x70, 0xa9, 0x1d, 0x99, 0xca, 0x2c, 0x62, - 0x22, 0x40, 0x0d, 0x97, 0xbc, 0xdf, 0xa2, 0x00, 0xfe, 0xc0, 0x65, 0xba, 0x45, 0xae, 0x4d, 0x43, - 0x31, 0x1a, 0xc6, 0x99, 0x61, 0x70, 0xe5, 0xac, 0x83, 0x8c, 0xc7, 0xa1, 0x80, 0xab, 0x74, 0x93, - 0x5c, 0x9d, 0x86, 0x02, 0x85, 0x59, 0x54, 0xd7, 0x3c, 0xe8, 0x15, 0xc2, 0x76, 0x50, 0x63, 0xa2, - 0x8a, 0x56, 0x31, 0x83, 0x70, 0xdd, 0x95, 0xe8, 0x94, 0xf2, 0x09, 0x0a, 0x16, 0x99, 0x8e, 0x0d, - 0x64, 0x2a, 0x0c, 0x2a, 0xb8, 0xe1, 0xaf, 0xe5, 0x39, 0x89, 0x0a, 0x03, 0xb4, 0x5a, 0xb0, 0x44, - 0xd7, 0xa4, 0x81, 0x4d, 0x7a, 0x8b, 0x6c, 0x7e, 0x2b, 0x67, 0x28, 0x85, 0x4d, 0x64, 0x0b, 0x15, - 0x6c, 0xf9, 0xe4, 0x8e, 0x08, 0x46, 0x1a, 0x16, 0x79, 0xec, 0xa6, 0xff, 0xfc, 0x37, 0xb9, 0xd0, - 0xae, 0xe4, 0x33, 0xd9, 0xa1, 0x44, 0xef, 0x92, 0x5b, 0x05, 0x4e, 0x2a, 0xca, 0xae, 0x1b, 0x26, - 0x93, 0x7a, 0x8b, 0xde, 0x27, 0x77, 0x7f, 0x40, 0x72, 0xde, 0xe1, 0xb6, 0x57, 0x63, 0x44, 0x54, - 0x58, 0xf0, 0x72, 0x67, 0xea, 0x53, 0x45, 0xd0, 0x9d, 0xb6, 0x5a, 0x05, 0xb0, 0xfd, 0x23, 0x12, - 0xd7, 0x06, 0xee, 0xd2, 0x3b, 0xe4, 0xe6, 0xf7, 0x48, 0x8d, 0x14, 0x53, 0x84, 0x1d, 0x37, 0x58, - 0xce, 0x8a, 0xdd, 0xe3, 0x3f, 0x9b, 0xc2, 0x6b, 0xa1, 0xab, 0xbe, 0x30, 0x60, 0x91, 0x0d, 0x45, - 0x45, 0xc2, 0xbd, 0xa9, 0x3a, 0x1e, 0x87, 0x0c, 0xf7, 0xbf, 0xaf, 0x6a, 0xb9, 0xe3, 0x95, 0xff, - 0xb9, 0xef, 0x43, 0x1e, 0xba, 0x09, 0x52, 0x4e, 0xb3, 0xf8, 0x1f, 0xf8, 0x4c, 0x17, 0x8d, 0xae, - 0xa5, 0x6c, 0x22, 0x65, 0x04, 0xbb, 0xf4, 0x36, 0xd9, 0x9a, 0x46, 0x13, 0x25, 0x13, 0xa9, 0x51, - 0xd9, 0x3a, 0x76, 0xe0, 0xa1, 0xcf, 0xc2, 0x04, 0x43, 0xa6, 0xc6, 0x8d, 0x2a, 0x9e, 0xcb, 0xd0, - 0x62, 0x8a, 0x6b, 0x78, 0x44, 0x1f, 0x92, 0xfb, 0xd3, 0x44, 0xaf, 0x90, 0x54, 0xb6, 0x15, 0x9a, - 0x1a, 0x57, 0xac, 0x95, 0x17, 0xc0, 0xe3, 0x9f, 0x26, 0x6b, 0xc3, 0x94, 0x71, 0xce, 0x33, 0x55, - 0xf6, 0xe8, 0x2e, 0xb9, 0x37, 0x4d, 0x76, 0x59, 0x29, 0xc8, 0x37, 0xba, 0xc5, 0xfe, 0x59, 0xd7, - 0x75, 0xdc, 0x20, 0x55, 0x0a, 0x85, 0x19, 0x13, 0x9f, 0xd0, 0x07, 0x64, 0xe7, 0x2c, 0x22, 0x0b, - 0x82, 0x34, 0xb6, 0xd9, 0xca, 0xd1, 0xda, 0x29, 0xf8, 0xd4, 0x77, 0xc3, 0x04, 0x53, 0x47, 0x4c, - 0xd7, 0x2c, 0x36, 0x51, 0x18, 0x78, 0x36, 0x92, 0x18, 0xdb, 0x76, 0x3c, 0xa8, 0x23, 0x29, 0xaa, - 0x65, 0x29, 0xeb, 0xf0, 0xdc, 0x0f, 0xbb, 0x09, 0x54, 0xd7, 0xa4, 0x32, 0x19, 0xfc, 0x0b, 0x3f, - 0xec, 0x1c, 0xac, 0xd1, 0x98, 0x08, 0x63, 0xe7, 0xf3, 0x97, 0x6e, 0xea, 0x7b, 0x73, 0xc2, 0x42, - 0xe5, 0xb7, 0x0c, 0xfc, 0x8a, 0x5e, 0x20, 0x6b, 0xde, 0x6e, 0x5a, 0x2c, 0x81, 0x5f, 0x53, 0x20, - 0xeb, 0x23, 0xa2, 0x6b, 0x63, 0xf8, 0x8d, 0x6f, 0x87, 0x49, 0x8f, 0x16, 0x85, 0x51, 0x1d, 0xf8, - 0xad, 0xef, 0x5c, 0x07, 0x2a, 0xac, 0x86, 0xda, 0xa0, 0x42, 0x9e, 0x7d, 0x02, 0x5e, 0x14, 0x5c, - 0x49, 0xc5, 0x51, 0xc1, 0xef, 0xfc, 0x04, 0xcc, 0xee, 0xee, 0x66, 0x5d, 0x04, 0xbf, 0x1f, 0x55, - 0x0c, 0xb6, 0x9d, 0x54, 0x6e, 0x9e, 0x58, 0x16, 0x98, 0xb0, 0x89, 0xf9, 0x19, 0x0d, 0x7f, 0x28, - 0x44, 0xc4, 0xb4, 0x46, 0x63, 0xa3, 0x50, 0x1b, 0xf8, 0xa3, 0xaf, 0x6d, 0x67, 0x16, 0xd8, 0x36, - 0x39, 0xdd, 0x86, 0x1c, 0x58, 0x41, 0xa1, 0x0c, 0x29, 0xdc, 0x3a, 0xe4, 0x50, 0xa6, 0x57, 0xc9, - 0x86, 0x87, 0x63, 0x66, 0x82, 0x9a, 0x55, 0xa8, 0xd3, 0xc8, 0x40, 0xe0, 0xbb, 0x69, 0x2a, 0xd0, - 0xb1, 0x5f, 0x5e, 0xb8, 0x48, 0x6e, 0xcc, 0x14, 0x47, 0x3f, 0xc3, 0x59, 0x10, 0xa0, 0xd6, 0x59, - 0x4a, 0x64, 0x04, 0x55, 0xfa, 0x88, 0x3c, 0x98, 0xb6, 0x66, 0x8b, 0xd0, 0x72, 0x4c, 0xdc, 0xc2, - 0x17, 0x41, 0xc7, 0xc6, 0x2c, 0x49, 0x5c, 0x3b, 0xd6, 0xbc, 0x54, 0x19, 0x1e, 0x48, 0x8e, 0x10, - 0xfa, 0x22, 0xf0, 0x96, 0xa9, 0xe5, 0x7f, 0xe0, 0x65, 0x9f, 0x44, 0xf3, 0xd5, 0x53, 0xf7, 0xc2, - 0x64, 0x98, 0xc6, 0x46, 0xea, 0xd6, 0x7b, 0xd6, 0x7b, 0x91, 0x9f, 0x38, 0x93, 0xa7, 0xdc, 0xe7, - 0x7c, 0xe9, 0x77, 0x20, 0xf6, 0xc5, 0x39, 0x49, 0x29, 0x77, 0x72, 0x56, 0xc8, 0x41, 0x78, 0x71, - 0x33, 0x42, 0x12, 0x0a, 0x81, 0xdc, 0x63, 0xc2, 0x6d, 0x72, 0xe9, 0x3f, 0x91, 0xad, 0xc4, 0x6a, - 0x24, 0xcb, 0x79, 0x07, 0x64, 0x69, 0x15, 0x69, 0x5c, 0x46, 0x05, 0x89, 0x5f, 0xf6, 0x8e, 0xf2, - 0x1a, 0x1a, 0xbe, 0x00, 0x2b, 0x88, 0x55, 0xc5, 0x84, 0x01, 0xe5, 0x77, 0xd8, 0xc8, 0x60, 0x59, - 0x14, 0xc9, 0x96, 0x2b, 0x16, 0xd0, 0x9e, 0x9b, 0x35, 0x8b, 0x93, 0xcd, 0xf8, 0xe2, 0x19, 0x19, - 0xf2, 0x01, 0x1c, 0x56, 0xc5, 0xb8, 0xd7, 0x53, 0xdf, 0x96, 0x63, 0x86, 0x53, 0xd0, 0x26, 0x69, - 0xb9, 0x8e, 0x1d, 0xab, 0x30, 0xca, 0xa7, 0xad, 0x13, 0xa7, 0xe9, 0xd3, 0x98, 0x95, 0x05, 0xc6, - 0xbe, 0x62, 0x5b, 0x85, 0x9c, 0x3b, 0xab, 0xaf, 0xda, 0x76, 0xa1, 0x9d, 0x9c, 0x99, 0x63, 0x22, - 0x75, 0x68, 0xa0, 0x33, 0x1a, 0x99, 0x85, 0xe6, 0x84, 0xd7, 0x85, 0x06, 0x72, 0x6d, 0xec, 0x8b, - 0x27, 0x13, 0x05, 0xfe, 0x54, 0x68, 0xf6, 0xac, 0x8b, 0x27, 0xd0, 0x3f, 0x17, 0xdf, 0x07, 0xdc, - 0xbd, 0xd5, 0x14, 0x72, 0xf8, 0x0b, 0xdd, 0x21, 0xb7, 0xa7, 0xad, 0x36, 0x96, 0x3c, 0x8d, 0xd0, - 0x9a, 0xb6, 0x4f, 0x85, 0xdd, 0x5e, 0x58, 0x79, 0x09, 0x2f, 0xb7, 0x17, 0x56, 0x5e, 0xc1, 0xab, - 0xed, 0x85, 0x95, 0x0a, 0x54, 0x76, 0x1f, 0x11, 0xda, 0x3a, 0x1c, 0x1c, 0xc7, 0xbd, 0xc1, 0xe0, - 0xf0, 0x4d, 0x4f, 0x7f, 0x38, 0x1a, 0xba, 0x77, 0xde, 0x2a, 0x59, 0x6c, 0xa4, 0xa8, 0xdc, 0x4b, - 0x6f, 0x8d, 0x2c, 0x63, 0x1b, 0x83, 0xd4, 0x20, 0xcc, 0x96, 0x0f, 0xfe, 0xfb, 0xb9, 0x34, 0xfb, - 0xf1, 0x73, 0x69, 0xf6, 0xd3, 0xe7, 0xd2, 0xec, 0x7f, 0xbe, 0x94, 0x66, 0x3e, 0x7e, 0x29, 0xcd, - 0xfc, 0xef, 0x4b, 0x69, 0xe6, 0xf5, 0x93, 0x37, 0x6f, 0x87, 0x7f, 0xfb, 0x70, 0xb4, 0xd7, 0xed, - 0x1f, 0xef, 0xfb, 0x37, 0x7c, 0xfe, 0xe7, 0xf1, 0xe0, 0xaf, 0x7f, 0xdf, 0x77, 0x4e, 0xa7, 0x1e, - 0xf5, 0x47, 0x4b, 0xd9, 0x5b, 0xfe, 0xf9, 0xff, 0x03, 0x00, 0x00, 0xff, 0xff, 0x19, 0x02, 0xe2, - 0x5e, 0xf3, 0x0b, 0x00, 0x00, + // 1487 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x57, 0xdb, 0x76, 0x1b, 0xb7, + 0x15, 0x95, 0x2c, 0x4a, 0xa2, 0x20, 0xd9, 0x3e, 0x86, 0xe5, 0xab, 0x64, 0xda, 0x91, 0xd5, 0xd8, + 0x55, 0x1c, 0x29, 0x8e, 0x7b, 0x4d, 0xda, 0xa6, 0xe0, 0xe0, 0x90, 0x1c, 0x71, 0x06, 0x18, 0x02, + 0x18, 0x5e, 0xdc, 0x36, 0xa8, 0xc4, 0x72, 0xa5, 0x59, 0x8d, 0x44, 0x2f, 0x91, 0xee, 0x6a, 0xbf, + 0xa1, 0x2f, 0xfd, 0x94, 0x7e, 0x46, 0x1f, 0xf3, 0xd8, 0xc7, 0x2e, 0xfb, 0x47, 0xba, 0x30, 0x03, + 0xd2, 0xc3, 0x89, 0x5c, 0x3f, 0x49, 0x3c, 0x7b, 0xe3, 0x0c, 0xce, 0x3e, 0xb7, 0x19, 0xb2, 0x3f, + 0x1c, 0x4f, 0xce, 0xc6, 0x93, 0xa3, 0x93, 0xe1, 0x70, 0x34, 0x99, 0x0c, 0xc7, 0xe7, 0xd3, 0x8b, + 0xf1, 0x77, 0x47, 0xc3, 0xf1, 0xf9, 0x64, 0x7a, 0x72, 0x3e, 0x9d, 0x1c, 0xbe, 0xba, 0x18, 0x4f, + 0xc7, 0x74, 0x37, 0x67, 0x1d, 0x2e, 0xb0, 0x0e, 0xff, 0xfa, 0xfc, 0x74, 0x34, 0x3d, 0x79, 0x7e, + 0xf0, 0x05, 0x21, 0x2c, 0x03, 0xcc, 0xdf, 0x5f, 0x8d, 0xe8, 0x26, 0x59, 0x4f, 0x45, 0x5b, 0xc8, + 0x9e, 0x80, 0x25, 0x5a, 0x25, 0x15, 0x85, 0x8c, 0xc3, 0x32, 0xdd, 0x20, 0xab, 0x3d, 0x15, 0x1a, + 0x84, 0x2b, 0x94, 0x90, 0xb5, 0x40, 0xc6, 0x71, 0x68, 0x60, 0xe5, 0xe0, 0x1f, 0x57, 0xc8, 0x4e, + 0x7e, 0x58, 0xbe, 0x1a, 0x5d, 0x9c, 0x4c, 0xbf, 0x1d, 0x9f, 0xeb, 0xd1, 0x77, 0xa3, 0xe1, 0x74, + 0x7c, 0x91, 0x79, 0xab, 0x92, 0x8a, 0x90, 0x02, 0x61, 0x89, 0xae, 0x91, 0x2b, 0xc7, 0x1d, 0x58, + 0xa6, 0xb7, 0xc8, 0x8d, 0xe3, 0x8e, 0xad, 0x63, 0xd0, 0x7a, 0xf1, 0xb9, 0x65, 0x9c, 0x2b, 0xd4, + 0x1a, 0xae, 0xd0, 0x1a, 0xb9, 0x7f, 0xdc, 0xb1, 0x11, 0x8a, 0xa6, 0x69, 0xd9, 0x44, 0x61, 0x23, + 0xec, 0x23, 0x9f, 0xe3, 0x2b, 0xf4, 0x1e, 0xb9, 0xa5, 0x51, 0x70, 0x54, 0xe5, 0xa3, 0x15, 0xba, + 0x47, 0x6a, 0x1e, 0x7a, 0xdf, 0xf1, 0x55, 0xba, 0x4d, 0x20, 0x90, 0xc2, 0x28, 0x16, 0x98, 0xb9, + 0x75, 0x8d, 0xde, 0x27, 0xb7, 0x8f, 0x3b, 0x36, 0x46, 0xad, 0x59, 0x13, 0x6d, 0x20, 0x05, 0x0f, + 0x4d, 0x28, 0x05, 0x8b, 0x60, 0xdd, 0x61, 0x81, 0x14, 0xda, 0x30, 0x61, 0xac, 0x36, 0x2a, 0x14, + 0x4d, 0x6b, 0xa4, 0x6d, 0x61, 0x1f, 0xaa, 0xf4, 0x36, 0xa1, 0x73, 0x6f, 0x0a, 0x1b, 0xa8, 0x50, + 0x04, 0x08, 0x1b, 0x07, 0xff, 0xda, 0x26, 0x5b, 0x6a, 0x34, 0x19, 0xbf, 0xbe, 0x18, 0x8e, 0xb2, + 0xf0, 0xd7, 0xc9, 0x0a, 0x13, 0x83, 0x3c, 0xfa, 0x76, 0x17, 0x96, 0x9d, 0x21, 0x1e, 0x9d, 0xe5, + 0x22, 0xf2, 0xd1, 0xdf, 0xdc, 0xff, 0x2b, 0x4e, 0xf2, 0x76, 0xd7, 0xd6, 0x99, 0x68, 0x43, 0x85, + 0x5e, 0x23, 0xa4, 0xdd, 0xb5, 0xda, 0xb0, 0x76, 0x28, 0x9a, 0xb0, 0xea, 0xc1, 0x1e, 0xd3, 0x31, + 0xac, 0xd1, 0xab, 0x64, 0xa3, 0xdd, 0xb5, 0x52, 0xb1, 0x20, 0x42, 0x58, 0x77, 0x4e, 0xda, 0x5d, + 0xcb, 0xb3, 0x3b, 0x6d, 0x91, 0x6a, 0xbb, 0x6b, 0x31, 0x91, 0x41, 0x0b, 0x36, 0xe8, 0x4d, 0x72, + 0xbd, 0xdd, 0xb5, 0x46, 0xb6, 0x51, 0x34, 0x58, 0x60, 0xa4, 0x1a, 0x00, 0x71, 0x21, 0xcd, 0x4f, + 0xdb, 0xae, 0x34, 0x68, 0x0d, 0x53, 0x4d, 0x34, 0x1a, 0x36, 0xe9, 0x03, 0x72, 0xef, 0x1d, 0xc6, + 0x9a, 0x4d, 0x85, 0x4d, 0x66, 0x72, 0x96, 0x86, 0x2d, 0x97, 0xb5, 0x77, 0x70, 0x03, 0x91, 0xa3, + 0xd2, 0x70, 0xd5, 0x65, 0xe5, 0xdd, 0x65, 0x2d, 0xc7, 0xc8, 0x9d, 0x0a, 0xa5, 0x80, 0x6b, 0xf4, + 0x2e, 0xd9, 0x2e, 0x40, 0x5d, 0x16, 0x85, 0x9c, 0x19, 0xa9, 0xe0, 0xba, 0x8f, 0x88, 0xa5, 0xa6, + 0x05, 0xe0, 0x3d, 0xb8, 0x1f, 0xb3, 0xbc, 0x58, 0x6d, 0xa4, 0x42, 0xb8, 0x41, 0x29, 0xb9, 0xe6, + 0x65, 0xb1, 0x3a, 0x4d, 0x92, 0x68, 0x00, 0x94, 0xde, 0x20, 0x57, 0x67, 0x36, 0x8e, 0x42, 0xc6, + 0x70, 0xd3, 0xa5, 0x76, 0x66, 0xaa, 0xb3, 0x88, 0x89, 0x00, 0x35, 0x6c, 0x7b, 0xbf, 0x45, 0x01, + 0xfc, 0x81, 0x5b, 0x74, 0x97, 0xdc, 0x2d, 0x43, 0x31, 0x1a, 0xc6, 0x99, 0x61, 0x70, 0xfb, 0xb2, + 0x83, 0x8c, 0xc7, 0xa1, 0x80, 0x3b, 0x74, 0x87, 0xdc, 0x29, 0x43, 0x81, 0xc2, 0x2c, 0xaa, 0xbb, + 0x1e, 0xf4, 0x0a, 0x61, 0x3f, 0x68, 0x31, 0xd1, 0x44, 0xab, 0x98, 0x41, 0xb8, 0xe7, 0x4a, 0xb4, + 0xa4, 0x7c, 0x82, 0x82, 0x45, 0x66, 0x60, 0x03, 0x99, 0x0a, 0x83, 0x0a, 0xee, 0xfb, 0x6b, 0x79, + 0x4e, 0xa2, 0xc2, 0x00, 0xad, 0x16, 0x2c, 0xd1, 0x2d, 0x69, 0x60, 0x87, 0x3e, 0x24, 0x3b, 0x3f, + 0x94, 0x33, 0x94, 0xc2, 0x26, 0xb2, 0x87, 0x0a, 0x76, 0x7d, 0x72, 0x67, 0x04, 0x23, 0x0d, 0x8b, + 0x3c, 0xf6, 0xc0, 0x3f, 0xfe, 0x07, 0xb9, 0xd0, 0xae, 0xe4, 0x33, 0xd9, 0xa1, 0x46, 0x1f, 0x93, + 0x87, 0x05, 0x4e, 0x2a, 0xea, 0xae, 0x1b, 0x16, 0x93, 0xfa, 0x90, 0x3e, 0x21, 0x8f, 0x3f, 0x40, + 0x72, 0xde, 0xe1, 0x91, 0x57, 0x63, 0x46, 0x54, 0x58, 0xf0, 0xf2, 0x51, 0xe9, 0x51, 0x45, 0xd0, + 0x9d, 0xb6, 0x5a, 0x05, 0xb0, 0xf7, 0x21, 0x12, 0xd7, 0x06, 0x1e, 0xd3, 0x8f, 0xc8, 0x83, 0xf7, + 0x91, 0x3a, 0x29, 0xa6, 0x08, 0xfb, 0x6e, 0xb0, 0x5c, 0x16, 0xbb, 0xc7, 0x7f, 0x54, 0xc2, 0x5b, + 0xa1, 0xab, 0xbe, 0x30, 0x60, 0x91, 0x0d, 0x45, 0x43, 0xc2, 0xc7, 0xa5, 0x3a, 0x9e, 0x87, 0x0c, + 0x4f, 0xde, 0xaf, 0x6a, 0x7d, 0xe0, 0x95, 0xff, 0xb1, 0xef, 0x43, 0x1e, 0xba, 0x09, 0x52, 0x4f, + 0xb3, 0xf8, 0x9f, 0xfa, 0x4c, 0x17, 0x8d, 0xae, 0xa5, 0x6c, 0x22, 0x65, 0x04, 0x07, 0xf4, 0x11, + 0xd9, 0x2d, 0xa3, 0x89, 0x92, 0x89, 0xd4, 0xa8, 0x6c, 0x1b, 0x07, 0xf0, 0x89, 0xcf, 0xc2, 0x02, + 0x43, 0xa6, 0xc6, 0x8d, 0x2a, 0x9e, 0xcb, 0xd0, 0x63, 0x8a, 0x6b, 0x78, 0x46, 0x3f, 0x21, 0x4f, + 0xca, 0x44, 0xaf, 0x90, 0x54, 0xb6, 0x17, 0x9a, 0x16, 0x57, 0xac, 0x97, 0x17, 0xc0, 0xa7, 0xff, + 0x9f, 0xac, 0x0d, 0x53, 0xc6, 0x39, 0xcf, 0x54, 0x39, 0xa4, 0x07, 0xe4, 0xe3, 0x32, 0xd9, 0x65, + 0xa5, 0x20, 0xdf, 0xec, 0x16, 0x47, 0x97, 0x5d, 0xd7, 0x71, 0x83, 0x54, 0x29, 0x14, 0x66, 0x4e, + 0xfc, 0x8c, 0x3e, 0x25, 0xfb, 0x97, 0x11, 0x59, 0x10, 0xa4, 0xb1, 0xcd, 0x56, 0x8e, 0xd6, 0x4e, + 0xc1, 0xe7, 0xbe, 0x1b, 0x16, 0x98, 0x3a, 0x62, 0xba, 0x65, 0xb1, 0x8b, 0xc2, 0xc0, 0xe7, 0x33, + 0x89, 0xb1, 0x6f, 0xe7, 0x83, 0x3a, 0x92, 0xa2, 0x59, 0x97, 0xb2, 0x0d, 0x2f, 0xfc, 0xb0, 0x5b, + 0x40, 0x75, 0x4b, 0x2a, 0x93, 0xc1, 0x3f, 0xf1, 0xc3, 0xce, 0xc1, 0x1a, 0x8d, 0x89, 0x30, 0x76, + 0x3e, 0x7f, 0xea, 0xa6, 0xbe, 0x37, 0x27, 0x2c, 0x54, 0x7e, 0xcb, 0xc0, 0xcf, 0xe8, 0x75, 0xb2, + 0xe9, 0xed, 0xa6, 0xc7, 0x12, 0xf8, 0x39, 0x05, 0xb2, 0x35, 0x23, 0xba, 0x36, 0x86, 0x5f, 0xf8, + 0x76, 0x58, 0xf4, 0x68, 0x51, 0x18, 0x35, 0x80, 0x5f, 0xfa, 0xce, 0x75, 0xa0, 0xc2, 0x66, 0xa8, + 0x0d, 0x2a, 0xe4, 0xd9, 0x23, 0xe0, 0x8b, 0x82, 0x2b, 0xa9, 0x38, 0x2a, 0xf8, 0x95, 0x9f, 0x80, + 0xd9, 0xdd, 0xdd, 0xac, 0x8b, 0xe0, 0xd7, 0xb3, 0x8a, 0xc1, 0xbe, 0x93, 0xca, 0xcd, 0x13, 0xcb, + 0x02, 0x13, 0x76, 0x31, 0x3f, 0xa3, 0xe1, 0x37, 0x85, 0x88, 0x98, 0xd6, 0x68, 0x6c, 0x14, 0x6a, + 0x03, 0xbf, 0xf5, 0xb5, 0xed, 0xcc, 0x02, 0xfb, 0x26, 0xa7, 0xdb, 0x90, 0x03, 0x2b, 0x28, 0x94, + 0x21, 0x85, 0x5b, 0x87, 0x1c, 0xea, 0xf4, 0x0e, 0xb9, 0xe9, 0xe1, 0x98, 0x99, 0xa0, 0x65, 0x15, + 0xea, 0x34, 0x32, 0x10, 0xf8, 0x6e, 0x2a, 0x05, 0x3a, 0xf7, 0xcb, 0x0b, 0x17, 0xc9, 0x8d, 0x99, + 0xe2, 0xe8, 0x67, 0x38, 0x0b, 0x02, 0xd4, 0x3a, 0x4b, 0x89, 0x8c, 0xa0, 0x49, 0x9f, 0x91, 0xa7, + 0x65, 0x6b, 0xb6, 0x08, 0x2d, 0xc7, 0xc4, 0x2d, 0x7c, 0x11, 0x0c, 0x6c, 0xcc, 0x92, 0xc4, 0xb5, + 0x63, 0xcb, 0x4b, 0x95, 0xe1, 0x81, 0xe4, 0x08, 0xa1, 0x2f, 0x02, 0x6f, 0x29, 0x2d, 0xff, 0x63, + 0x2f, 0xfb, 0x22, 0x9a, 0xaf, 0x9e, 0xb6, 0x17, 0x26, 0xc3, 0x34, 0x76, 0x52, 0xb7, 0xde, 0xb3, + 0xde, 0x8b, 0xfc, 0xc4, 0x59, 0x3c, 0xe5, 0x1e, 0xe7, 0x4b, 0x7f, 0x00, 0xb1, 0x2f, 0xce, 0x45, + 0x4a, 0x7d, 0x90, 0xb3, 0x42, 0x0e, 0xc2, 0x8b, 0x9b, 0x11, 0x92, 0x50, 0x08, 0xe4, 0x1e, 0x13, + 0x6e, 0x93, 0x4b, 0xff, 0x88, 0x6c, 0x25, 0x36, 0x23, 0x59, 0xcf, 0x3b, 0x20, 0x4b, 0xab, 0x48, + 0xe3, 0x3a, 0x2a, 0x48, 0xfc, 0xb2, 0x77, 0x94, 0x97, 0xd0, 0xf1, 0x05, 0xd8, 0x40, 0x6c, 0x2a, + 0x26, 0x0c, 0x28, 0xbf, 0xc3, 0x66, 0x06, 0xcb, 0xa2, 0x48, 0xf6, 0x5c, 0xb1, 0x80, 0xf6, 0xdc, + 0xac, 0x59, 0x9c, 0x6c, 0xc6, 0x17, 0xcf, 0xcc, 0x90, 0x0f, 0xe0, 0xb0, 0x29, 0xe6, 0xbd, 0x9e, + 0xfa, 0xb6, 0x9c, 0x33, 0x9c, 0x82, 0x36, 0x49, 0xeb, 0x6d, 0x1c, 0x58, 0x85, 0x51, 0x3e, 0x6d, + 0x9d, 0x38, 0x5d, 0x9f, 0xc6, 0xac, 0x2c, 0x30, 0xf6, 0x15, 0xdb, 0x2b, 0xe4, 0xdc, 0x59, 0x7d, + 0xd5, 0xf6, 0x0b, 0xed, 0xe4, 0xcc, 0x1c, 0x13, 0xa9, 0x43, 0x03, 0x83, 0xd9, 0xc8, 0x2c, 0x34, + 0x27, 0xbc, 0x2c, 0x34, 0x90, 0x6b, 0x63, 0x5f, 0x3c, 0x99, 0x28, 0xf0, 0xbb, 0x42, 0xb3, 0x67, + 0x5d, 0xbc, 0x80, 0xfe, 0xbe, 0xf8, 0x7e, 0xc0, 0xdd, 0xbb, 0x9a, 0x42, 0x0e, 0x7f, 0xa0, 0xfb, + 0xe4, 0x51, 0xd9, 0x6a, 0x63, 0xc9, 0xd3, 0x08, 0xad, 0xe9, 0xfb, 0x54, 0x58, 0xbf, 0x84, 0xe6, + 0x57, 0xf7, 0xf7, 0xd1, 0xee, 0x4d, 0x30, 0x51, 0xd2, 0xd5, 0x25, 0xfc, 0xb1, 0x44, 0xe2, 0xb2, + 0x27, 0xb4, 0x51, 0xc8, 0x0a, 0x7c, 0x38, 0xd9, 0xab, 0x54, 0xbf, 0x84, 0x2f, 0xf7, 0x2a, 0xd5, + 0xaf, 0xe0, 0xab, 0xbd, 0x4a, 0xb5, 0x01, 0x8d, 0xbd, 0x4a, 0xf5, 0x6b, 0xf8, 0xfa, 0xe0, 0x19, + 0xa1, 0xbd, 0x93, 0xc9, 0x59, 0x3c, 0x9a, 0x4c, 0x4e, 0xbe, 0x19, 0xe9, 0xd7, 0xa7, 0x53, 0xf7, + 0xde, 0xb8, 0x41, 0x56, 0x3b, 0x29, 0x2a, 0xf7, 0xe6, 0xb8, 0x49, 0xd6, 0xb1, 0x8f, 0x41, 0x6a, + 0x10, 0x96, 0xeb, 0xc7, 0xff, 0x7e, 0x53, 0x5b, 0xfe, 0xfe, 0x4d, 0x6d, 0xf9, 0xbf, 0x6f, 0x6a, + 0xcb, 0xff, 0x7c, 0x5b, 0x5b, 0xfa, 0xfe, 0x6d, 0x6d, 0xe9, 0x3f, 0x6f, 0x6b, 0x4b, 0x2f, 0x3f, + 0xfb, 0xe6, 0xdb, 0xe9, 0x9f, 0x5f, 0x9f, 0x1e, 0x0e, 0xc7, 0x67, 0x47, 0xfe, 0x9b, 0x20, 0xff, + 0xf3, 0xe9, 0xe4, 0x4f, 0x7f, 0x39, 0x72, 0x4e, 0x4b, 0x1f, 0x09, 0xa7, 0x6b, 0xd9, 0xb7, 0xc1, + 0x8b, 0xff, 0x05, 0x00, 0x00, 0xff, 0xff, 0x98, 0x43, 0x58, 0x88, 0x43, 0x0c, 0x00, 0x00, } diff --git a/types/accesscontrol/resource.go b/types/accesscontrol/resource.go index a1c4c7190..3ad944683 100644 --- a/types/accesscontrol/resource.go +++ b/types/accesscontrol/resource.go @@ -105,6 +105,8 @@ var ResourceTree = map[ResourceType]TreeNode{ ResourceType_KV_DEX_MEM_DEPOSIT, ResourceType_KV_DEX_LONG_ORDER_COUNT, ResourceType_KV_DEX_SHORT_ORDER_COUNT, + ResourceType_KV_DEX_MEM_CONTRACTS_TO_PROCESS, + ResourceType_KV_DEX_MEM_DOWNSTREAM_CONTRACTS, }}, ResourceType_KV_DEX_CONTRACT_LONGBOOK: {ResourceType_KV_DEX, []ResourceType{}}, ResourceType_KV_DEX_CONTRACT_SHORTBOOK: {ResourceType_KV_DEX, []ResourceType{}}, @@ -194,6 +196,8 @@ var ResourceTree = map[ResourceType]TreeNode{ ResourceType_KV_DEX_MEM_ORDER: {ResourceType_KV_DEX, []ResourceType{}}, ResourceType_KV_DEX_MEM_CANCEL: {ResourceType_KV_DEX, []ResourceType{}}, ResourceType_KV_DEX_MEM_DEPOSIT: {ResourceType_KV_DEX, []ResourceType{}}, + ResourceType_KV_DEX_MEM_CONTRACTS_TO_PROCESS: {ResourceType_KV_DEX, []ResourceType{}}, + ResourceType_KV_DEX_MEM_DOWNSTREAM_CONTRACTS: {ResourceType_KV_DEX, []ResourceType{}}, } // This returns a slice of all resource types that are dependent to a specific resource type From f9541fb8396aaa4ca1589c3bf2f4eff4e312ed37 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Wed, 22 Nov 2023 13:03:37 -0500 Subject: [PATCH 22/36] [occ] OCC scheduler and validation fixes (#359) This makes optimizations to the scheduler and validation --------- Co-authored-by: Steven Landers --- store/multiversion/store.go | 13 ++- tasks/scheduler.go | 215 ++++++++++++++++++++++++------------ tasks/scheduler_test.go | 4 +- 3 files changed, 153 insertions(+), 79 deletions(-) diff --git a/store/multiversion/store.go b/store/multiversion/store.go index bc5e8ee4a..16fb04597 100644 --- a/store/multiversion/store.go +++ b/store/multiversion/store.go @@ -320,11 +320,11 @@ func (s *Store) validateIterator(index int, tracker iterationTracker) bool { } func (s *Store) checkIteratorAtIndex(index int) bool { - s.mtx.RLock() - defer s.mtx.RUnlock() - valid := true + s.mtx.RLock() iterateset := s.txIterateSets[index] + s.mtx.RUnlock() + for _, iterationTracker := range iterateset { iteratorValid := s.validateIterator(index, iterationTracker) valid = valid && iteratorValid @@ -333,11 +333,12 @@ func (s *Store) checkIteratorAtIndex(index int) bool { } func (s *Store) checkReadsetAtIndex(index int) (bool, []int) { - s.mtx.RLock() - defer s.mtx.RUnlock() - conflictSet := make(map[int]struct{}) + + s.mtx.RLock() readset := s.txReadSets[index] + s.mtx.RUnlock() + valid := true // iterate over readset and check if the value is the same as the latest value relateive to txIndex in the multiversion store diff --git a/tasks/scheduler.go b/tasks/scheduler.go index 7b1afc0d2..c00e70dbe 100644 --- a/tasks/scheduler.go +++ b/tasks/scheduler.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "fmt" "sort" + "sync" "github.com/tendermint/tendermint/abci/types" "go.opentelemetry.io/otel/attribute" @@ -38,7 +39,6 @@ const ( type deliverTxTask struct { Ctx sdk.Context - Span trace.Span AbortCh chan occ.Abort Status status @@ -49,10 +49,10 @@ type deliverTxTask struct { Request types.RequestDeliverTx Response *types.ResponseDeliverTx VersionStores map[sdk.StoreKey]*multiversion.VersionIndexedStore + ValidateCh chan struct{} } -func (dt *deliverTxTask) Increment() { - dt.Incarnation++ +func (dt *deliverTxTask) Reset() { dt.Status = statusPending dt.Response = nil dt.Abort = nil @@ -61,6 +61,11 @@ func (dt *deliverTxTask) Increment() { dt.VersionStores = nil } +func (dt *deliverTxTask) Increment() { + dt.Incarnation++ + dt.ValidateCh = make(chan struct{}, 1) +} + // Scheduler processes tasks concurrently type Scheduler interface { ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]types.ResponseDeliverTx, error) @@ -71,6 +76,7 @@ type scheduler struct { workers int multiVersionStores map[sdk.StoreKey]multiversion.MultiVersionStore tracingInfo *tracing.Info + allTasks []*deliverTxTask } // NewScheduler creates a new scheduler @@ -111,9 +117,10 @@ func toTasks(reqs []*sdk.DeliverTxEntry) []*deliverTxTask { res := make([]*deliverTxTask, 0, len(reqs)) for idx, r := range reqs { res = append(res, &deliverTxTask{ - Request: r.Request, - Index: idx, - Status: statusPending, + Request: r.Request, + Index: idx, + Status: statusPending, + ValidateCh: make(chan struct{}, 1), }) } return res @@ -175,6 +182,7 @@ func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]t // prefill estimates s.PrefillEstimates(ctx, reqs) tasks := toTasks(reqs) + s.allTasks = tasks toExecute := tasks for !allValidated(tasks) { var err error @@ -193,9 +201,6 @@ func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]t if err != nil { return nil, err } - for _, t := range toExecute { - t.Increment() - } } for _, mv := range s.multiVersionStores { mv.WriteLatestToStore() @@ -203,52 +208,83 @@ func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]t return collectResponses(tasks), nil } -func (s *scheduler) validateAll(ctx sdk.Context, tasks []*deliverTxTask) ([]*deliverTxTask, error) { - spanCtx, span := s.tracingInfo.StartWithContext("SchedulerValidate", ctx.TraceSpanContext()) - ctx = ctx.WithTraceSpanContext(spanCtx) +func (s *scheduler) shouldRerun(task *deliverTxTask) bool { + switch task.Status { + + case statusAborted, statusPending: + return true + + // validated tasks can become unvalidated if an earlier re-run task now conflicts + case statusExecuted, statusValidated: + if valid, conflicts := s.findConflicts(task); !valid { + s.invalidateTask(task) + + // if the conflicts are now validated, then rerun this task + if indexesValidated(s.allTasks, conflicts) { + return true + } else { + // otherwise, wait for completion + task.Dependencies = conflicts + task.Status = statusWaiting + return false + } + } else if len(conflicts) == 0 { + // mark as validated, which will avoid re-validating unless a lower-index re-validates + task.Status = statusValidated + return false + } + // conflicts and valid, so it'll validate next time + return false + + case statusWaiting: + // if conflicts are done, then this task is ready to run again + return indexesValidated(s.allTasks, task.Dependencies) + } + panic("unexpected status: " + task.Status) +} + +func (s *scheduler) validateTask(ctx sdk.Context, task *deliverTxTask) bool { + _, span := s.traceSpan(ctx, "SchedulerValidate", task) defer span.End() - var res []*deliverTxTask + if s.shouldRerun(task) { + return false + } + return true +} - // find first non-validated entry - var startIdx int - for idx, t := range tasks { +func (s *scheduler) findFirstNonValidated() (int, bool) { + for i, t := range s.allTasks { if t.Status != statusValidated { - startIdx = idx - break + return i, true } } + return 0, false +} - for i := startIdx; i < len(tasks); i++ { - switch tasks[i].Status { - case statusAborted: - // aborted means it can be re-run immediately - res = append(res, tasks[i]) - - // validated tasks can become unvalidated if an earlier re-run task now conflicts - case statusExecuted, statusValidated: - if valid, conflicts := s.findConflicts(tasks[i]); !valid { - s.invalidateTask(tasks[i]) - - // if the conflicts are now validated, then rerun this task - if indexesValidated(tasks, conflicts) { - res = append(res, tasks[i]) - } else { - // otherwise, wait for completion - tasks[i].Dependencies = conflicts - tasks[i].Status = statusWaiting - } - } else if len(conflicts) == 0 { - tasks[i].Status = statusValidated - } // TODO: do we need to have handling for conflicts existing here? - - case statusWaiting: - // if conflicts are done, then this task is ready to run again - if indexesValidated(tasks, tasks[i].Dependencies) { - res = append(res, tasks[i]) +func (s *scheduler) validateAll(ctx sdk.Context, tasks []*deliverTxTask) ([]*deliverTxTask, error) { + ctx, span := s.traceSpan(ctx, "SchedulerValidateAll", nil) + defer span.End() + + var mx sync.Mutex + var res []*deliverTxTask + + wg := sync.WaitGroup{} + for i := 0; i < len(tasks); i++ { + wg.Add(1) + go func(task *deliverTxTask) { + defer wg.Done() + if !s.validateTask(ctx, task) { + task.Reset() + task.Increment() + mx.Lock() + res = append(res, task) + mx.Unlock() } - } + }(tasks[i]) } + wg.Wait() + return res, nil } @@ -256,6 +292,9 @@ func (s *scheduler) validateAll(ctx sdk.Context, tasks []*deliverTxTask) ([]*del // Tasks are updated with their status // TODO: error scenarios func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { + ctx, span := s.traceSpan(ctx, "SchedulerExecuteAll", nil) + defer span.End() + ch := make(chan *deliverTxTask, len(tasks)) grp, gCtx := errgroup.WithContext(ctx.Context()) @@ -265,6 +304,15 @@ func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { workers = len(tasks) } + // validationWg waits for all validations to complete + // validations happen in separate goroutines in order to wait on previous index + validationWg := &sync.WaitGroup{} + validationWg.Add(len(tasks)) + grp.Go(func() error { + validationWg.Wait() + return nil + }) + for i := 0; i < workers; i++ { grp.Go(func() error { for { @@ -275,24 +323,16 @@ func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { if !ok { return nil } - s.executeTask(task) + s.prepareAndRunTask(validationWg, ctx, task) } } }) } - grp.Go(func() error { - defer close(ch) - for _, task := range tasks { - s.prepareTask(ctx, task) - - select { - case <-gCtx.Done(): - return gCtx.Err() - case ch <- task: - } - } - return nil - }) + + for _, task := range tasks { + ch <- task + } + close(ch) if err := grp.Wait(); err != nil { return err @@ -301,16 +341,46 @@ func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { return nil } +func (s *scheduler) prepareAndRunTask(wg *sync.WaitGroup, ctx sdk.Context, task *deliverTxTask) { + eCtx, eSpan := s.traceSpan(ctx, "SchedulerExecute", task) + defer eSpan.End() + task.Ctx = eCtx + + s.executeTask(task.Ctx, task) + go func() { + defer wg.Done() + defer close(task.ValidateCh) + // wait on previous task to finish validation + if task.Index > 0 { + <-s.allTasks[task.Index-1].ValidateCh + } + if !s.validateTask(task.Ctx, task) { + task.Reset() + } + task.ValidateCh <- struct{}{} + }() +} + +func (s *scheduler) traceSpan(ctx sdk.Context, name string, task *deliverTxTask) (sdk.Context, trace.Span) { + spanCtx, span := s.tracingInfo.StartWithContext(name, ctx.TraceSpanContext()) + if task != nil { + span.SetAttributes(attribute.String("txHash", fmt.Sprintf("%X", sha256.Sum256(task.Request.Tx)))) + span.SetAttributes(attribute.Int("txIndex", task.Index)) + span.SetAttributes(attribute.Int("txIncarnation", task.Incarnation)) + } + ctx = ctx.WithTraceSpanContext(spanCtx) + return ctx, span +} + // prepareTask initializes the context and version stores for a task func (s *scheduler) prepareTask(ctx sdk.Context, task *deliverTxTask) { - // initialize the context ctx = ctx.WithTxIndex(task.Index) + + _, span := s.traceSpan(ctx, "SchedulerPrepare", task) + defer span.End() + + // initialize the context abortCh := make(chan occ.Abort, len(s.multiVersionStores)) - spanCtx, span := s.tracingInfo.StartWithContext("SchedulerExecute", ctx.TraceSpanContext()) - span.SetAttributes(attribute.String("txHash", fmt.Sprintf("%X", sha256.Sum256(task.Request.Tx)))) - span.SetAttributes(attribute.Int("txIndex", task.Index)) - span.SetAttributes(attribute.Int("txIncarnation", task.Incarnation)) - ctx = ctx.WithTraceSpanContext(spanCtx) // if there are no stores, don't try to wrap, because there's nothing to wrap if len(s.multiVersionStores) > 0 { @@ -334,14 +404,17 @@ func (s *scheduler) prepareTask(ctx sdk.Context, task *deliverTxTask) { task.AbortCh = abortCh task.Ctx = ctx - task.Span = span } // executeTask executes a single task -func (s *scheduler) executeTask(task *deliverTxTask) { - if task.Span != nil { - defer task.Span.End() - } +func (s *scheduler) executeTask(ctx sdk.Context, task *deliverTxTask) { + + s.prepareTask(ctx, task) + + dCtx, dSpan := s.traceSpan(task.Ctx, "SchedulerDeliverTx", task) + defer dSpan.End() + task.Ctx = dCtx + resp := s.deliverTx(task.Ctx, task.Request) close(task.AbortCh) diff --git a/tasks/scheduler_test.go b/tasks/scheduler_test.go index accc8bf3e..9d24b54a8 100644 --- a/tasks/scheduler_test.go +++ b/tasks/scheduler_test.go @@ -66,7 +66,7 @@ func TestProcessAll(t *testing.T) { { name: "Test every tx accesses same key", workers: 50, - runs: 25, + runs: 50, addStores: true, requests: requestList(50), deliverTxFunc: func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { @@ -94,7 +94,7 @@ func TestProcessAll(t *testing.T) { } // confirm last write made it to the parent store latest := ctx.MultiStore().GetKVStore(testStoreKey).Get(itemKey) - require.Equal(t, []byte("49"), latest) + require.Equal(t, []byte(fmt.Sprintf("%d", len(res)-1)), latest) }, expectedErr: nil, }, From 30b7fabe40fcc388176cca3e1e57f37d74707842 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Mon, 27 Nov 2023 09:56:37 -0500 Subject: [PATCH 23/36] [occ] Add optimizations for multiversion and mvkv (#361) Add optimizations to reduce mutex lock contention and refactor with sync Maps. This also removes telemetry that was added liberally, and we can later add in telemetry more mindfully and feature flagged. loadtest chain testing --- baseapp/baseapp.go | 14 +-- store/cachekv/store.go | 2 - store/multiversion/mvkv.go | 53 +++++---- store/multiversion/store.go | 219 ++++++++++++++++++------------------ store/types/cache.go | 8 +- tasks/scheduler.go | 2 + 6 files changed, 152 insertions(+), 146 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 1c430eb96..1964e552a 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -841,13 +841,13 @@ func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (sdk.Context // and execute successfully. An error is returned otherwise. func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, result *sdk.Result, anteEvents []abci.Event, priority int64, err error) { - defer telemetry.MeasureThroughputSinceWithLabels( - telemetry.TxCount, - []metrics.Label{ - telemetry.NewLabel("mode", modeKeyToString[mode]), - }, - time.Now(), - ) + // defer telemetry.MeasureThroughputSinceWithLabels( + // telemetry.TxCount, + // []metrics.Label{ + // telemetry.NewLabel("mode", modeKeyToString[mode]), + // }, + // time.Now(), + // ) // Reset events after each checkTx or simulateTx or recheckTx // DeliverTx is garbage collected after FinalizeBlocker diff --git a/store/cachekv/store.go b/store/cachekv/store.go index 0317f03cf..f9dee6cbb 100644 --- a/store/cachekv/store.go +++ b/store/cachekv/store.go @@ -176,8 +176,6 @@ func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator { defer store.mtx.Unlock() // TODO: (occ) Note that for iterators, we'll need to have special handling (discussed in RFC) to ensure proper validation - // TODO: (occ) Note that for iterators, we'll need to have special handling (discussed in RFC) to ensure proper validation - var parent, cache types.Iterator if ascending { diff --git a/store/multiversion/mvkv.go b/store/multiversion/mvkv.go index 6eeabd517..1e8437ad7 100644 --- a/store/multiversion/mvkv.go +++ b/store/multiversion/mvkv.go @@ -3,13 +3,10 @@ package multiversion import ( "io" "sort" - "sync" - "time" abci "github.com/tendermint/tendermint/abci/types" "github.com/cosmos/cosmos-sdk/store/types" - "github.com/cosmos/cosmos-sdk/telemetry" scheduler "github.com/cosmos/cosmos-sdk/types/occ" dbm "github.com/tendermint/tm-db" ) @@ -72,7 +69,8 @@ func (item *iterationTracker) SetEarlyStopKey(key []byte) { // Version Indexed Store wraps the multiversion store in a way that implements the KVStore interface, but also stores the index of the transaction, and so store actions are applied to the multiversion store using that index type VersionIndexedStore struct { - mtx sync.Mutex + // TODO: this shouldnt NEED a mutex because its used within single transaction execution, therefore no concurrency + // mtx sync.Mutex // used for tracking reads and writes for eventual validation + persistence into multi-version store // TODO: does this need sync.Map? readset map[string][]byte // contains the key -> value mapping for all keys read from the store (not mvkv, underlying store) @@ -130,9 +128,10 @@ func (store *VersionIndexedStore) Get(key []byte) []byte { // if the key is in the cache, return it // don't have RW mutex because we have to update readset - store.mtx.Lock() - defer store.mtx.Unlock() - defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "get") + // TODO: remove? + // store.mtx.Lock() + // defer store.mtx.Unlock() + // defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "get") types.AssertValidKey(key) strKey := string(key) @@ -176,9 +175,10 @@ func (store *VersionIndexedStore) parseValueAndUpdateReadset(strKey string, mvsV // This function iterates over the readset, validating that the values in the readset are consistent with the values in the multiversion store and underlying parent store, and returns a boolean indicating validity func (store *VersionIndexedStore) ValidateReadset() bool { - store.mtx.Lock() - defer store.mtx.Unlock() - defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "validate_readset") + // TODO: remove? + // store.mtx.Lock() + // defer store.mtx.Unlock() + // defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "validate_readset") // sort the readset keys - this is so we have consistent behavior when theres varying conflicts within the readset (eg. read conflict vs estimate) readsetKeys := make([]string, 0, len(store.readset)) @@ -225,9 +225,10 @@ func (store *VersionIndexedStore) ValidateReadset() bool { // Delete implements types.KVStore. func (store *VersionIndexedStore) Delete(key []byte) { - store.mtx.Lock() - defer store.mtx.Unlock() - defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "delete") + // TODO: remove? + // store.mtx.Lock() + // defer store.mtx.Unlock() + // defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "delete") types.AssertValidKey(key) store.setValue(key, nil, true, true) @@ -241,9 +242,10 @@ func (store *VersionIndexedStore) Has(key []byte) bool { // Set implements types.KVStore. func (store *VersionIndexedStore) Set(key []byte, value []byte) { - store.mtx.Lock() - defer store.mtx.Unlock() - defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "set") + // TODO: remove? + // store.mtx.Lock() + // defer store.mtx.Unlock() + // defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "set") types.AssertValidKey(key) store.setValue(key, value, false, true) @@ -262,8 +264,9 @@ func (v *VersionIndexedStore) ReverseIterator(start []byte, end []byte) dbm.Iter // TODO: still needs iterateset tracking // Iterator implements types.KVStore. func (store *VersionIndexedStore) iterator(start []byte, end []byte, ascending bool) dbm.Iterator { - store.mtx.Lock() - defer store.mtx.Unlock() + // TODO: remove? + // store.mtx.Lock() + // defer store.mtx.Unlock() // get the sorted keys from MVS // TODO: ideally we take advantage of mvs keys already being sorted @@ -334,18 +337,20 @@ func (store *VersionIndexedStore) setValue(key, value []byte, deleted bool, dirt } func (store *VersionIndexedStore) WriteToMultiVersionStore() { - store.mtx.Lock() - defer store.mtx.Unlock() - defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "write_mvs") + // TODO: remove? + // store.mtx.Lock() + // defer store.mtx.Unlock() + // defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "write_mvs") store.multiVersionStore.SetWriteset(store.transactionIndex, store.incarnation, store.writeset) store.multiVersionStore.SetReadset(store.transactionIndex, store.readset) store.multiVersionStore.SetIterateset(store.transactionIndex, store.iterateset) } func (store *VersionIndexedStore) WriteEstimatesToMultiVersionStore() { - store.mtx.Lock() - defer store.mtx.Unlock() - defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "write_mvs") + // TODO: remove? + // store.mtx.Lock() + // defer store.mtx.Unlock() + // defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "write_mvs") store.multiVersionStore.SetEstimatedWriteset(store.transactionIndex, store.incarnation, store.writeset) // TODO: do we need to write readset and iterateset in this case? I don't think so since if this is called it means we aren't doing validation } diff --git a/store/multiversion/store.go b/store/multiversion/store.go index 16fb04597..16b0e626b 100644 --- a/store/multiversion/store.go +++ b/store/multiversion/store.go @@ -4,10 +4,8 @@ import ( "bytes" "sort" "sync" - "time" "github.com/cosmos/cosmos-sdk/store/types" - "github.com/cosmos/cosmos-sdk/telemetry" "github.com/cosmos/cosmos-sdk/types/occ" occtypes "github.com/cosmos/cosmos-sdk/types/occ" db "github.com/tendermint/tm-db" @@ -25,9 +23,11 @@ type MultiVersionStore interface { CollectIteratorItems(index int) *db.MemDB SetReadset(index int, readset ReadSet) GetReadset(index int) ReadSet + ClearReadset(index int) VersionedIndexedStore(index int, incarnation int, abortChannel chan occ.Abort) *VersionIndexedStore SetIterateset(index int, iterateset Iterateset) GetIterateset(index int) Iterateset + ClearIterateset(index int) ValidateTransactionState(index int) (bool, []int) } @@ -38,24 +38,23 @@ type Iterateset []iterationTracker var _ MultiVersionStore = (*Store)(nil) type Store struct { - mtx sync.RWMutex - // map that stores the key -> MultiVersionValue mapping for accessing from a given key - multiVersionMap map[string]MultiVersionValue + // map that stores the key string -> MultiVersionValue mapping for accessing from a given key + multiVersionMap *sync.Map // TODO: do we need to support iterators as well similar to how cachekv does it - yes - txWritesetKeys map[int][]string // map of tx index -> writeset keys - txReadSets map[int]ReadSet - txIterateSets map[int]Iterateset + txWritesetKeys *sync.Map // map of tx index -> writeset keys []string + txReadSets *sync.Map // map of tx index -> readset ReadSet + txIterateSets *sync.Map // map of tx index -> iterateset Iterateset parentStore types.KVStore } func NewMultiVersionStore(parentStore types.KVStore) *Store { return &Store{ - multiVersionMap: make(map[string]MultiVersionValue), - txWritesetKeys: make(map[int][]string), - txReadSets: make(map[int]ReadSet), - txIterateSets: make(map[int]Iterateset), + multiVersionMap: &sync.Map{}, + txWritesetKeys: &sync.Map{}, + txReadSets: &sync.Map{}, + txIterateSets: &sync.Map{}, parentStore: parentStore, } } @@ -67,32 +66,28 @@ func (s *Store) VersionedIndexedStore(index int, incarnation int, abortChannel c // GetLatest implements MultiVersionStore. func (s *Store) GetLatest(key []byte) (value MultiVersionValueItem) { - s.mtx.RLock() - defer s.mtx.RUnlock() - keyString := string(key) + mvVal, found := s.multiVersionMap.Load(keyString) // if the key doesn't exist in the overall map, return nil - if _, ok := s.multiVersionMap[keyString]; !ok { + if !found { return nil } - val, found := s.multiVersionMap[keyString].GetLatest() + latestVal, found := mvVal.(MultiVersionValue).GetLatest() if !found { return nil // this is possible IF there is are writeset that are then removed for that key } - return val + return latestVal } // GetLatestBeforeIndex implements MultiVersionStore. func (s *Store) GetLatestBeforeIndex(index int, key []byte) (value MultiVersionValueItem) { - s.mtx.RLock() - defer s.mtx.RUnlock() - keyString := string(key) + mvVal, found := s.multiVersionMap.Load(keyString) // if the key doesn't exist in the overall map, return nil - if _, ok := s.multiVersionMap[keyString]; !ok { + if !found { return nil } - val, found := s.multiVersionMap[keyString].GetLatestBeforeIndex(index) + val, found := mvVal.(MultiVersionValue).GetLatestBeforeIndex(index) // otherwise, we may have found a value for that key, but its not written before the index passed in if !found { return nil @@ -103,24 +98,15 @@ func (s *Store) GetLatestBeforeIndex(index int, key []byte) (value MultiVersionV // Has implements MultiVersionStore. It checks if the key exists in the multiversion store at or before the specified index. func (s *Store) Has(index int, key []byte) bool { - s.mtx.RLock() - defer s.mtx.RUnlock() keyString := string(key) - if _, ok := s.multiVersionMap[keyString]; !ok { + mvVal, found := s.multiVersionMap.Load(keyString) + // if the key doesn't exist in the overall map, return nil + if !found { return false // this is okay because the caller of this will THEN need to access the parent store to verify that the key doesnt exist there } - _, found := s.multiVersionMap[keyString].GetLatestBeforeIndex(index) - return found -} - -// This function will try to intialize the multiversion item if it doesn't exist for a key specified by byte array -// NOTE: this should be used within an acquired mutex lock -func (s *Store) tryInitMultiVersionItem(keyString string) { - if _, ok := s.multiVersionMap[keyString]; !ok { - multiVersionValue := NewMultiVersionItem() - s.multiVersionMap[keyString] = multiVersionValue - } + _, foundVal := mvVal.(MultiVersionValue).GetLatestBeforeIndex(index) + return foundVal } func (s *Store) removeOldWriteset(index int, newWriteSet WriteSet) { @@ -130,7 +116,9 @@ func (s *Store) removeOldWriteset(index int, newWriteSet WriteSet) { writeset = newWriteSet } // if there is already a writeset existing, we should remove that fully - if keys, ok := s.txWritesetKeys[index]; ok { + oldKeys, loaded := s.txWritesetKeys.LoadAndDelete(index) + if loaded { + keys := oldKeys.([]string) // we need to delete all of the keys in the writeset from the multiversion store for _, key := range keys { // small optimization to check if the new writeset is going to write this key, if so, we can leave it behind @@ -139,59 +127,57 @@ func (s *Store) removeOldWriteset(index int, newWriteSet WriteSet) { continue } // remove from the appropriate item if present in multiVersionMap - if val, ok := s.multiVersionMap[key]; ok { - val.Remove(index) + mvVal, found := s.multiVersionMap.Load(key) + // if the key doesn't exist in the overall map, return nil + if !found { + continue } + mvVal.(MultiVersionValue).Remove(index) } } - // unset the writesetKeys for this index - delete(s.txWritesetKeys, index) } // SetWriteset sets a writeset for a transaction index, and also writes all of the multiversion items in the writeset to the multiversion store. // TODO: returns a list of NEW keys added func (s *Store) SetWriteset(index int, incarnation int, writeset WriteSet) { - s.mtx.Lock() - defer s.mtx.Unlock() - + // TODO: add telemetry spans // remove old writeset if it exists s.removeOldWriteset(index, writeset) writeSetKeys := make([]string, 0, len(writeset)) for key, value := range writeset { writeSetKeys = append(writeSetKeys, key) - s.tryInitMultiVersionItem(key) + loadVal, _ := s.multiVersionMap.LoadOrStore(key, NewMultiVersionItem()) // init if necessary + mvVal := loadVal.(MultiVersionValue) if value == nil { // delete if nil value - s.multiVersionMap[key].Delete(index, incarnation) + // TODO: sync map + mvVal.Delete(index, incarnation) } else { - s.multiVersionMap[key].Set(index, incarnation, value) + mvVal.Set(index, incarnation, value) } } sort.Strings(writeSetKeys) // TODO: if we're sorting here anyways, maybe we just put it into a btree instead of a slice - s.txWritesetKeys[index] = writeSetKeys + s.txWritesetKeys.Store(index, writeSetKeys) } // InvalidateWriteset iterates over the keys for the given index and incarnation writeset and replaces with ESTIMATEs func (s *Store) InvalidateWriteset(index int, incarnation int) { - s.mtx.Lock() - defer s.mtx.Unlock() - - if keys, ok := s.txWritesetKeys[index]; ok { - for _, key := range keys { - // invalidate all of the writeset items - is this suboptimal? - we could potentially do concurrently if slow because locking is on an item specific level - s.tryInitMultiVersionItem(key) // this SHOULD no-op because we're invalidating existing keys - s.multiVersionMap[key].SetEstimate(index, incarnation) - } + keysAny, found := s.txWritesetKeys.Load(index) + if !found { + return + } + keys := keysAny.([]string) + for _, key := range keys { + // invalidate all of the writeset items - is this suboptimal? - we could potentially do concurrently if slow because locking is on an item specific level + val, _ := s.multiVersionMap.LoadOrStore(key, NewMultiVersionItem()) + val.(MultiVersionValue).SetEstimate(index, incarnation) } // we leave the writeset in place because we'll need it for key removal later if/when we replace with a new writeset } // SetEstimatedWriteset is used to directly write estimates instead of writing a writeset and later invalidating func (s *Store) SetEstimatedWriteset(index int, incarnation int, writeset WriteSet) { - s.mtx.Lock() - defer s.mtx.Unlock() - // remove old writeset if it exists s.removeOldWriteset(index, writeset) @@ -199,62 +185,71 @@ func (s *Store) SetEstimatedWriteset(index int, incarnation int, writeset WriteS // still need to save the writeset so we can remove the elements later: for key := range writeset { writeSetKeys = append(writeSetKeys, key) - s.tryInitMultiVersionItem(key) - s.multiVersionMap[key].SetEstimate(index, incarnation) + + mvVal, _ := s.multiVersionMap.LoadOrStore(key, NewMultiVersionItem()) // init if necessary + mvVal.(MultiVersionValue).SetEstimate(index, incarnation) } sort.Strings(writeSetKeys) - s.txWritesetKeys[index] = writeSetKeys + s.txWritesetKeys.Store(index, writeSetKeys) } // GetAllWritesetKeys implements MultiVersionStore. func (s *Store) GetAllWritesetKeys() map[int][]string { - s.mtx.RLock() - defer s.mtx.RUnlock() - return s.txWritesetKeys + writesetKeys := make(map[int][]string) + // TODO: is this safe? + s.txWritesetKeys.Range(func(key, value interface{}) bool { + index := key.(int) + keys := value.([]string) + writesetKeys[index] = keys + return true + }) + + return writesetKeys } func (s *Store) SetReadset(index int, readset ReadSet) { - s.mtx.Lock() - defer s.mtx.Unlock() - - s.txReadSets[index] = readset + s.txReadSets.Store(index, readset) } func (s *Store) GetReadset(index int) ReadSet { - s.mtx.RLock() - defer s.mtx.RUnlock() - - return s.txReadSets[index] + readsetAny, found := s.txReadSets.Load(index) + if !found { + return nil + } + return readsetAny.(ReadSet) } func (s *Store) SetIterateset(index int, iterateset Iterateset) { - s.mtx.Lock() - defer s.mtx.Unlock() - - s.txIterateSets[index] = iterateset + s.txIterateSets.Store(index, iterateset) } func (s *Store) GetIterateset(index int) Iterateset { - s.mtx.RLock() - defer s.mtx.RUnlock() + iteratesetAny, found := s.txIterateSets.Load(index) + if !found { + return nil + } + return iteratesetAny.(Iterateset) +} - return s.txIterateSets[index] +func (s *Store) ClearReadset(index int) { + s.txReadSets.Delete(index) +} + +func (s *Store) ClearIterateset(index int) { + s.txReadSets.Delete(index) } // CollectIteratorItems implements MultiVersionStore. It will return a memDB containing all of the keys present in the multiversion store within the iteration range prior to (exclusive of) the index. func (s *Store) CollectIteratorItems(index int) *db.MemDB { - s.mtx.RLock() - defer s.mtx.RUnlock() - sortedItems := db.NewMemDB() // get all writeset keys prior to index - keys := s.txWritesetKeys for i := 0; i < index; i++ { - indexedWriteset, ok := keys[i] - if !ok { + writesetAny, found := s.txWritesetKeys.Load(i) + if !found { continue } + indexedWriteset := writesetAny.([]string) // TODO: do we want to exclude keys out of the range or just let the iterator handle it? for _, key := range indexedWriteset { // TODO: inefficient because (logn) for each key + rebalancing? maybe theres a better way to add to a tree to reduce rebalancing overhead @@ -278,6 +273,7 @@ func (s *Store) validateIterator(index int, tracker iterationTracker) bool { go func(iterationTracker iterationTracker, items *db.MemDB, returnChan chan bool, abortChan chan occtypes.Abort) { var parentIter types.Iterator expectedKeys := iterationTracker.iteratedKeys + foundKeys := 0 iter := s.newMVSValidationIterator(index, iterationTracker.startKey, iterationTracker.endKey, items, iterationTracker.ascending, iterationTracker.writeset, abortChan) if iterationTracker.ascending { parentIter = s.parentStore.Iterator(iterationTracker.startKey, iterationTracker.endKey) @@ -288,19 +284,21 @@ func (s *Store) validateIterator(index int, tracker iterationTracker) bool { mergeIterator := NewMVSMergeIterator(parentIter, iter, iterationTracker.ascending, NoOpHandler{}) defer mergeIterator.Close() for ; mergeIterator.Valid(); mergeIterator.Next() { - if len(expectedKeys) == 0 { + if (len(expectedKeys) - foundKeys) == 0 { // if we have no more expected keys, then the iterator is invalid returnChan <- false return } key := mergeIterator.Key() + // TODO: is this ok to not delete the key since we shouldnt have duplicate keys? if _, ok := expectedKeys[string(key)]; !ok { // if key isn't found returnChan <- false return } // remove from expected keys - delete(expectedKeys, string(key)) + foundKeys += 1 + // delete(expectedKeys, string(key)) // if our iterator key was the early stop, then we can break if bytes.Equal(key, iterationTracker.earlyStopKey) { @@ -308,7 +306,7 @@ func (s *Store) validateIterator(index int, tracker iterationTracker) bool { return } } - returnChan <- !(len(expectedKeys) > 0) + returnChan <- !((len(expectedKeys) - foundKeys) > 0) }(tracker, sortedItems, validChannel, abortChannel) select { case <-abortChannel: @@ -321,10 +319,11 @@ func (s *Store) validateIterator(index int, tracker iterationTracker) bool { func (s *Store) checkIteratorAtIndex(index int) bool { valid := true - s.mtx.RLock() - iterateset := s.txIterateSets[index] - s.mtx.RUnlock() - + iterateSetAny, found := s.txIterateSets.Load(index) + if !found { + return true + } + iterateset := iterateSetAny.(Iterateset) for _, iterationTracker := range iterateset { iteratorValid := s.validateIterator(index, iterationTracker) valid = valid && iteratorValid @@ -334,13 +333,13 @@ func (s *Store) checkIteratorAtIndex(index int) bool { func (s *Store) checkReadsetAtIndex(index int) (bool, []int) { conflictSet := make(map[int]struct{}) - - s.mtx.RLock() - readset := s.txReadSets[index] - s.mtx.RUnlock() - valid := true + readSetAny, found := s.txReadSets.Load(index) + if !found { + return true, []int{} + } + readset := readSetAny.(ReadSet) // iterate over readset and check if the value is the same as the latest value relateive to txIndex in the multiversion store for key, value := range readset { // get the latest value from the multiversion store @@ -379,7 +378,7 @@ func (s *Store) checkReadsetAtIndex(index int) (bool, []int) { // TODO: do we want to return bool + []int where bool indicates whether it was valid and then []int indicates only ones for which we need to wait due to estimates? - yes i think so? func (s *Store) ValidateTransactionState(index int) (bool, []int) { - defer telemetry.MeasureSince(time.Now(), "store", "mvs", "validate") + // defer telemetry.MeasureSince(time.Now(), "store", "mvs", "validate") // TODO: can we parallelize for all iterators? iteratorValid := s.checkIteratorAtIndex(index) @@ -390,18 +389,20 @@ func (s *Store) ValidateTransactionState(index int) (bool, []int) { } func (s *Store) WriteLatestToStore() { - s.mtx.Lock() - defer s.mtx.Unlock() - // sort the keys - keys := make([]string, 0, len(s.multiVersionMap)) - for key := range s.multiVersionMap { - keys = append(keys, key) - } + keys := []string{} + s.multiVersionMap.Range(func(key, value interface{}) bool { + keys = append(keys, key.(string)) + return true + }) sort.Strings(keys) for _, key := range keys { - mvValue, found := s.multiVersionMap[key].GetLatestNonEstimate() + val, ok := s.multiVersionMap.Load(key) + if !ok { + continue + } + mvValue, found := val.(MultiVersionValue).GetLatestNonEstimate() if !found { // this means that at some point, there was an estimate, but we have since removed it so there isn't anything writeable at the key, so we can skip continue diff --git a/store/types/cache.go b/store/types/cache.go index 53f45d6b3..b00335a76 100644 --- a/store/types/cache.go +++ b/store/types/cache.go @@ -47,7 +47,7 @@ type BoundedCache struct { CacheBackend limit int - mu *sync.Mutex + mu *sync.Mutex metricName []string } @@ -88,7 +88,7 @@ func (c *BoundedCache) emitKeysEvictedMetrics(keysToEvict int) { func (c *BoundedCache) Set(key string, val *CValue) { c.mu.Lock() defer c.mu.Unlock() - defer c.emitCacheSizeMetric() + // defer c.emitCacheSizeMetric() if c.Len() >= c.limit { numEntries := c.Len() @@ -112,7 +112,7 @@ func (c *BoundedCache) Set(key string, val *CValue) { func (c *BoundedCache) Delete(key string) { c.mu.Lock() defer c.mu.Unlock() - defer c.emitCacheSizeMetric() + // defer c.emitCacheSizeMetric() c.CacheBackend.Delete(key) } @@ -120,7 +120,7 @@ func (c *BoundedCache) Delete(key string) { func (c *BoundedCache) DeleteAll() { c.mu.Lock() defer c.mu.Unlock() - defer c.emitCacheSizeMetric() + // defer c.emitCacheSizeMetric() c.CacheBackend.Range(func(key string, _ *CValue) bool { c.CacheBackend.Delete(key) diff --git a/tasks/scheduler.go b/tasks/scheduler.go index c00e70dbe..7fe65ef07 100644 --- a/tasks/scheduler.go +++ b/tasks/scheduler.go @@ -91,6 +91,8 @@ func NewScheduler(workers int, tracingInfo *tracing.Info, deliverTxFunc func(ctx func (s *scheduler) invalidateTask(task *deliverTxTask) { for _, mv := range s.multiVersionStores { mv.InvalidateWriteset(task.Index, task.Incarnation) + mv.ClearReadset(task.Index) + mv.ClearIterateset(task.Index) } } From 4d12564ff511fe30a6eb346d89fd665ffe681acf Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Wed, 29 Nov 2023 08:33:21 -0500 Subject: [PATCH 24/36] [OCC] Add scheduler goroutine pool and optimizations (#362) ## Describe your changes and provide context - adds pool optimizations (bounds by tasks / workers) - adds validateAll shortcut (starts at first non-validated entry) - adds invalidation of future tasks on invalidation ## Testing performed to validate your change - unit tests are passing with full conflicting txs --- tasks/scheduler.go | 172 +++++++++++++++++++++++----------------- tasks/scheduler_test.go | 35 ++++++++ 2 files changed, 136 insertions(+), 71 deletions(-) diff --git a/tasks/scheduler.go b/tasks/scheduler.go index 7fe65ef07..de0466f55 100644 --- a/tasks/scheduler.go +++ b/tasks/scheduler.go @@ -1,21 +1,20 @@ package tasks import ( + "context" "crypto/sha256" "fmt" "sort" "sync" - "github.com/tendermint/tendermint/abci/types" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" - "golang.org/x/sync/errgroup" - "github.com/cosmos/cosmos-sdk/store/multiversion" store "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/occ" "github.com/cosmos/cosmos-sdk/utils/tracing" + "github.com/tendermint/tendermint/abci/types" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" ) type status string @@ -49,7 +48,7 @@ type deliverTxTask struct { Request types.RequestDeliverTx Response *types.ResponseDeliverTx VersionStores map[sdk.StoreKey]*multiversion.VersionIndexedStore - ValidateCh chan struct{} + ValidateCh chan status } func (dt *deliverTxTask) Reset() { @@ -63,7 +62,7 @@ func (dt *deliverTxTask) Reset() { func (dt *deliverTxTask) Increment() { dt.Incarnation++ - dt.ValidateCh = make(chan struct{}, 1) + dt.ValidateCh = make(chan status, 1) } // Scheduler processes tasks concurrently @@ -77,6 +76,8 @@ type scheduler struct { multiVersionStores map[sdk.StoreKey]multiversion.MultiVersionStore tracingInfo *tracing.Info allTasks []*deliverTxTask + executeCh chan func() + validateCh chan func() } // NewScheduler creates a new scheduler @@ -96,6 +97,29 @@ func (s *scheduler) invalidateTask(task *deliverTxTask) { } } +func start(ctx context.Context, ch chan func(), workers int) { + for i := 0; i < workers; i++ { + go func() { + for { + select { + case <-ctx.Done(): + return + case work := <-ch: + work() + } + } + }() + } +} + +func (s *scheduler) DoValidate(work func()) { + s.validateCh <- work +} + +func (s *scheduler) DoExecute(work func()) { + s.executeCh <- work +} + func (s *scheduler) findConflicts(task *deliverTxTask) (bool, []int) { var conflicts []int uniq := make(map[int]struct{}) @@ -122,7 +146,7 @@ func toTasks(reqs []*sdk.DeliverTxEntry) []*deliverTxTask { Request: r.Request, Index: idx, Status: statusPending, - ValidateCh: make(chan struct{}, 1), + ValidateCh: make(chan status, 1), }) } return res @@ -166,7 +190,7 @@ func allValidated(tasks []*deliverTxTask) bool { return true } -func (s *scheduler) PrefillEstimates(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) { +func (s *scheduler) PrefillEstimates(reqs []*sdk.DeliverTxEntry) { // iterate over TXs, update estimated writesets where applicable for i, req := range reqs { mappedWritesets := req.EstimatedWritesets @@ -182,9 +206,27 @@ func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]t // initialize mutli-version stores if they haven't been initialized yet s.tryInitMultiVersionStore(ctx) // prefill estimates - s.PrefillEstimates(ctx, reqs) + s.PrefillEstimates(reqs) tasks := toTasks(reqs) s.allTasks = tasks + s.executeCh = make(chan func(), len(tasks)) + s.validateCh = make(chan func(), len(tasks)) + + // default to number of tasks if workers is negative or 0 by this point + workers := s.workers + if s.workers < 1 { + workers = len(tasks) + } + + workerCtx, cancel := context.WithCancel(ctx.Context()) + defer cancel() + + // execution tasks are limited by workers + start(workerCtx, s.executeCh, workers) + + // validation tasks uses length of tasks to avoid blocking on validation + start(workerCtx, s.validateCh, len(tasks)) + toExecute := tasks for !allValidated(tasks) { var err error @@ -271,19 +313,26 @@ func (s *scheduler) validateAll(ctx sdk.Context, tasks []*deliverTxTask) ([]*del var mx sync.Mutex var res []*deliverTxTask + startIdx, anyLeft := s.findFirstNonValidated() + + if !anyLeft { + return nil, nil + } + wg := sync.WaitGroup{} - for i := 0; i < len(tasks); i++ { + for i := startIdx; i < len(tasks); i++ { + t := tasks[i] wg.Add(1) - go func(task *deliverTxTask) { + s.DoValidate(func() { defer wg.Done() - if !s.validateTask(ctx, task) { - task.Reset() - task.Increment() + if !s.validateTask(ctx, t) { + t.Reset() + t.Increment() mx.Lock() - res = append(res, task) + res = append(res, t) mx.Unlock() } - }(tasks[i]) + }) } wg.Wait() @@ -291,56 +340,47 @@ func (s *scheduler) validateAll(ctx sdk.Context, tasks []*deliverTxTask) ([]*del } // ExecuteAll executes all tasks concurrently -// Tasks are updated with their status -// TODO: error scenarios func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error { ctx, span := s.traceSpan(ctx, "SchedulerExecuteAll", nil) defer span.End() - ch := make(chan *deliverTxTask, len(tasks)) - grp, gCtx := errgroup.WithContext(ctx.Context()) - - // a workers value < 1 means no limit - workers := s.workers - if s.workers < 1 { - workers = len(tasks) - } - // validationWg waits for all validations to complete // validations happen in separate goroutines in order to wait on previous index validationWg := &sync.WaitGroup{} validationWg.Add(len(tasks)) - grp.Go(func() error { - validationWg.Wait() - return nil - }) - for i := 0; i < workers; i++ { - grp.Go(func() error { - for { - select { - case <-gCtx.Done(): - return gCtx.Err() - case task, ok := <-ch: - if !ok { - return nil - } - s.prepareAndRunTask(validationWg, ctx, task) - } - } + for _, task := range tasks { + t := task + s.DoExecute(func() { + s.prepareAndRunTask(validationWg, ctx, t) }) } - for _, task := range tasks { - ch <- task - } - close(ch) + validationWg.Wait() + + return nil +} - if err := grp.Wait(); err != nil { - return err +func (s *scheduler) waitOnPreviousAndValidate(wg *sync.WaitGroup, task *deliverTxTask) { + defer wg.Done() + defer close(task.ValidateCh) + // wait on previous task to finish validation + // if a previous task fails validation, then subsequent should fail too (cascade) + if task.Index > 0 { + res, ok := <-s.allTasks[task.Index-1].ValidateCh + if ok && res != statusValidated { + task.Reset() + task.ValidateCh <- task.Status + return + } + } + // if not validated, reset the task + if !s.validateTask(task.Ctx, task) { + task.Reset() } - return nil + // notify next task of this one's status + task.ValidateCh <- task.Status } func (s *scheduler) prepareAndRunTask(wg *sync.WaitGroup, ctx sdk.Context, task *deliverTxTask) { @@ -348,19 +388,12 @@ func (s *scheduler) prepareAndRunTask(wg *sync.WaitGroup, ctx sdk.Context, task defer eSpan.End() task.Ctx = eCtx - s.executeTask(task.Ctx, task) - go func() { - defer wg.Done() - defer close(task.ValidateCh) - // wait on previous task to finish validation - if task.Index > 0 { - <-s.allTasks[task.Index-1].ValidateCh - } - if !s.validateTask(task.Ctx, task) { - task.Reset() - } - task.ValidateCh <- struct{}{} - }() + s.prepareTask(task) + s.executeTask(task) + + s.DoValidate(func() { + s.waitOnPreviousAndValidate(wg, task) + }) } func (s *scheduler) traceSpan(ctx sdk.Context, name string, task *deliverTxTask) (sdk.Context, trace.Span) { @@ -375,8 +408,8 @@ func (s *scheduler) traceSpan(ctx sdk.Context, name string, task *deliverTxTask) } // prepareTask initializes the context and version stores for a task -func (s *scheduler) prepareTask(ctx sdk.Context, task *deliverTxTask) { - ctx = ctx.WithTxIndex(task.Index) +func (s *scheduler) prepareTask(task *deliverTxTask) { + ctx := task.Ctx.WithTxIndex(task.Index) _, span := s.traceSpan(ctx, "SchedulerPrepare", task) defer span.End() @@ -409,10 +442,7 @@ func (s *scheduler) prepareTask(ctx sdk.Context, task *deliverTxTask) { } // executeTask executes a single task -func (s *scheduler) executeTask(ctx sdk.Context, task *deliverTxTask) { - - s.prepareTask(ctx, task) - +func (s *scheduler) executeTask(task *deliverTxTask) { dCtx, dSpan := s.traceSpan(task.Ctx, "SchedulerDeliverTx", task) defer dSpan.End() task.Ctx = dCtx diff --git a/tasks/scheduler_test.go b/tasks/scheduler_test.go index 9d24b54a8..886bbe5ce 100644 --- a/tasks/scheduler_test.go +++ b/tasks/scheduler_test.go @@ -68,6 +68,41 @@ func TestProcessAll(t *testing.T) { workers: 50, runs: 50, addStores: true, + requests: requestList(100), + deliverTxFunc: func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { + // all txs read and write to the same key to maximize conflicts + kv := ctx.MultiStore().GetKVStore(testStoreKey) + val := string(kv.Get(itemKey)) + + // write to the store with this tx's index + kv.Set(itemKey, req.Tx) + + // return what was read from the store (final attempt should be index-1) + return types.ResponseDeliverTx{ + Info: val, + } + }, + assertions: func(t *testing.T, ctx sdk.Context, res []types.ResponseDeliverTx) { + for idx, response := range res { + if idx == 0 { + require.Equal(t, "", response.Info) + } else { + // the info is what was read from the kv store by the tx + // each tx writes its own index, so the info should be the index of the previous tx + require.Equal(t, fmt.Sprintf("%d", idx-1), response.Info) + } + } + // confirm last write made it to the parent store + latest := ctx.MultiStore().GetKVStore(testStoreKey).Get(itemKey) + require.Equal(t, []byte(fmt.Sprintf("%d", len(res)-1)), latest) + }, + expectedErr: nil, + }, + { + name: "Test few workers many txs", + workers: 5, + runs: 10, + addStores: true, requests: requestList(50), deliverTxFunc: func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { // all txs read and write to the same key to maximize conflicts From a780a58b9d056fac9e50bda83ca6564b6a655b1e Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Tue, 12 Dec 2023 17:25:56 +0800 Subject: [PATCH 25/36] update concurrency workers (#380) ## Describe your changes and provide context Update concurrency workers ## Testing performed to validate your change --- server/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/config/config.go b/server/config/config.go index 042366f1d..9a3a51e88 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -23,7 +23,7 @@ const ( DefaultGRPCWebAddress = "0.0.0.0:9091" // DefaultConcurrencyWorkers defines the default workers to use for concurrent transactions - DefaultConcurrencyWorkers = 10 + DefaultConcurrencyWorkers = 20 ) // BaseConfig defines the server's basic configuration From cb02f7eb9168d3ec9ab2b03931fe5d2f2ca1ce46 Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Tue, 12 Dec 2023 21:26:54 -0500 Subject: [PATCH 26/36] [OCC] Fix hang where abort channel blocks iterator (#379) ## Describe your changes and provide context - instead of assuming one thing will arrive to the abort channel, drain it ## Testing performed to validate your change - new unit test captures situation (tests iterator) --- tasks/scheduler.go | 48 +++++++++++++--- tasks/scheduler_test.go | 121 +++++++++++++++++++++++++++++++++------- 2 files changed, 139 insertions(+), 30 deletions(-) diff --git a/tasks/scheduler.go b/tasks/scheduler.go index de0466f55..c0e7c143d 100644 --- a/tasks/scheduler.go +++ b/tasks/scheduler.go @@ -388,7 +388,6 @@ func (s *scheduler) prepareAndRunTask(wg *sync.WaitGroup, ctx sdk.Context, task defer eSpan.End() task.Ctx = eCtx - s.prepareTask(task) s.executeTask(task) s.DoValidate(func() { @@ -441,27 +440,58 @@ func (s *scheduler) prepareTask(task *deliverTxTask) { task.Ctx = ctx } -// executeTask executes a single task func (s *scheduler) executeTask(task *deliverTxTask) { - dCtx, dSpan := s.traceSpan(task.Ctx, "SchedulerDeliverTx", task) + dCtx, dSpan := s.traceSpan(task.Ctx, "SchedulerExecuteTask", task) defer dSpan.End() task.Ctx = dCtx - resp := s.deliverTx(task.Ctx, task.Request) + s.prepareTask(task) + + // Channel to signal the completion of deliverTx + doneCh := make(chan types.ResponseDeliverTx) + + // Run deliverTx in a separate goroutine + go func() { + doneCh <- s.deliverTx(task.Ctx, task.Request) + }() + + // Flag to mark if abort has happened + var abortOccurred bool + + var wg sync.WaitGroup + wg.Add(1) + + var abort *occ.Abort + // Drain the AbortCh in a non-blocking way + go func() { + defer wg.Done() + for abt := range task.AbortCh { + if !abortOccurred { + abortOccurred = true + abort = &abt + } + } + }() + + // Wait for deliverTx to complete + resp := <-doneCh close(task.AbortCh) - if abt, ok := <-task.AbortCh; ok { + wg.Wait() + + // If abort has occurred, return, else set the response and status + if abortOccurred { task.Status = statusAborted - task.Abort = &abt + task.Abort = abort return } + task.Status = statusExecuted + task.Response = &resp + // write from version store to multiversion stores for _, v := range task.VersionStores { v.WriteToMultiVersionStore() } - - task.Status = statusExecuted - task.Response = &resp } diff --git a/tasks/scheduler_test.go b/tasks/scheduler_test.go index 886bbe5ce..298143584 100644 --- a/tasks/scheduler_test.go +++ b/tasks/scheduler_test.go @@ -4,6 +4,9 @@ import ( "context" "errors" "fmt" + "net/http" + _ "net/http/pprof" + "runtime" "testing" "github.com/stretchr/testify/require" @@ -52,11 +55,26 @@ func initTestCtx(injectStores bool) sdk.Context { return ctx } +func generateTasks(count int) []*deliverTxTask { + var res []*deliverTxTask + for i := 0; i < count; i++ { + res = append(res, &deliverTxTask{Index: i}) + } + return res +} + func TestProcessAll(t *testing.T) { + runtime.SetBlockProfileRate(1) + + go func() { + http.ListenAndServe("localhost:6060", nil) + }() + tests := []struct { name string workers int runs int + before func(ctx sdk.Context) requests []*sdk.DeliverTxEntry deliverTxFunc mockDeliverTxFunc addStores bool @@ -64,18 +82,79 @@ func TestProcessAll(t *testing.T) { assertions func(t *testing.T, ctx sdk.Context, res []types.ResponseDeliverTx) }{ { - name: "Test every tx accesses same key", + name: "Test zero txs does not hang", + workers: 20, + runs: 10, + addStores: true, + requests: requestList(0), + deliverTxFunc: func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { + panic("should not deliver") + }, + assertions: func(t *testing.T, ctx sdk.Context, res []types.ResponseDeliverTx) { + require.Len(t, res, 0) + }, + expectedErr: nil, + }, + { + name: "Test tx writing to a store that another tx is iterating", workers: 50, - runs: 50, + runs: 1, + requests: requestList(500), addStores: true, - requests: requestList(100), + before: func(ctx sdk.Context) { + kv := ctx.MultiStore().GetKVStore(testStoreKey) + // initialize 100 test values in the base kv store so iterating isn't too fast + for i := 0; i < 10; i++ { + kv.Set([]byte(fmt.Sprintf("%d", i)), []byte(fmt.Sprintf("%d", i))) + } + }, + deliverTxFunc: func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { + kv := ctx.MultiStore().GetKVStore(testStoreKey) + if ctx.TxIndex()%2 == 0 { + // For even-indexed transactions, write to the store + kv.Set(req.Tx, req.Tx) + return types.ResponseDeliverTx{ + Info: "write", + } + } else { + // For odd-indexed transactions, iterate over the store + + // just write so we have more writes going on + kv.Set(req.Tx, req.Tx) + iterator := kv.Iterator(nil, nil) + defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { + // Do nothing, just iterate + } + return types.ResponseDeliverTx{ + Info: "iterate", + } + } + }, + assertions: func(t *testing.T, ctx sdk.Context, res []types.ResponseDeliverTx) { + for idx, response := range res { + if idx%2 == 0 { + require.Equal(t, "write", response.Info) + } else { + require.Equal(t, "iterate", response.Info) + } + } + }, + expectedErr: nil, + }, + { + name: "Test no overlap txs", + workers: 20, + runs: 10, + addStores: true, + requests: requestList(1000), deliverTxFunc: func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { // all txs read and write to the same key to maximize conflicts kv := ctx.MultiStore().GetKVStore(testStoreKey) - val := string(kv.Get(itemKey)) // write to the store with this tx's index - kv.Set(itemKey, req.Tx) + kv.Set(req.Tx, req.Tx) + val := string(kv.Get(req.Tx)) // return what was read from the store (final attempt should be index-1) return types.ResponseDeliverTx{ @@ -84,26 +163,22 @@ func TestProcessAll(t *testing.T) { }, assertions: func(t *testing.T, ctx sdk.Context, res []types.ResponseDeliverTx) { for idx, response := range res { - if idx == 0 { - require.Equal(t, "", response.Info) - } else { - // the info is what was read from the kv store by the tx - // each tx writes its own index, so the info should be the index of the previous tx - require.Equal(t, fmt.Sprintf("%d", idx-1), response.Info) - } + require.Equal(t, fmt.Sprintf("%d", idx), response.Info) + } + store := ctx.MultiStore().GetKVStore(testStoreKey) + for i := 0; i < len(res); i++ { + val := store.Get([]byte(fmt.Sprintf("%d", i))) + require.Equal(t, []byte(fmt.Sprintf("%d", i)), val) } - // confirm last write made it to the parent store - latest := ctx.MultiStore().GetKVStore(testStoreKey).Get(itemKey) - require.Equal(t, []byte(fmt.Sprintf("%d", len(res)-1)), latest) }, expectedErr: nil, }, { - name: "Test few workers many txs", - workers: 5, - runs: 10, + name: "Test every tx accesses same key", + workers: 50, + runs: 1, addStores: true, - requests: requestList(50), + requests: requestList(1000), deliverTxFunc: func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { // all txs read and write to the same key to maximize conflicts kv := ctx.MultiStore().GetKVStore(testStoreKey) @@ -136,9 +211,9 @@ func TestProcessAll(t *testing.T) { { name: "Test no stores on context should not panic", workers: 50, - runs: 1, + runs: 10, addStores: false, - requests: requestList(50), + requests: requestList(10), deliverTxFunc: func(ctx sdk.Context, req types.RequestDeliverTx) types.ResponseDeliverTx { return types.ResponseDeliverTx{ Info: fmt.Sprintf("%d", ctx.TxIndex()), @@ -167,6 +242,10 @@ func TestProcessAll(t *testing.T) { s := NewScheduler(tt.workers, ti, tt.deliverTxFunc) ctx := initTestCtx(tt.addStores) + if tt.before != nil { + tt.before(ctx) + } + res, err := s.ProcessAll(ctx, tt.requests) require.Len(t, res, len(tt.requests)) From fa4b76d06ddd57b1608f75eacb355f244da2c74d Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Tue, 2 Jan 2024 08:44:17 -0600 Subject: [PATCH 27/36] Occ iterator fix (#389) ## Describe your changes and provide context This change serves to improve the way we track the values of the keys we iterate over when running iterators. Previously, the iterateset would only track the keys that were iterated, and the behavior of the iterator was thought to not include keys that didn't have values present, OR that the readset would be appropriately updated when reading the value from the iterateset. (I'm not yet 100% sure that updating readset WITHIN the tracked iterator is fully necessary, since it may be the case that the readset modifications may have been sufficient to mitigate this issue, but the change is currently in the PR since this is the version of code running on the loadtest cluster for stability testing. However, in cases when an earlier transaction was writing to the range that would be iterated, it was possible that the stale value was read by the transaction handler, BUT the value that got into the readset was the newer one. I believe this has to do with the readset updating based on directly querying values from underlying stores, and overwriting the prior readset value that indicated that the transaction used a stale value. The fix I have made is that during tx execution, the cache memiterator now reads directly form MVKV instead of individually reading from underlying stores. The key difference here is that IF the key is already in the readset, it will serve that STALE value instead of reading into the underlying store where the value may have since mutated. As a result, the behavior we now expect is that one a key is read, ONLY that value that was read will be utilized for the duration of the transaction. This way, we won't potentially mutate the readset by overwriting the key entry with the later value only to have it incorrectly pass validation. Additionally, to more rigorously enforce this behavior, updating the readset now will only update the map IFF the key doesnt already exist in the readset. This should provide better guarantees around catching any stale reads that occur over the lifespan of the transacation execution. ## Testing performed to validate your change Running a lot of iterator heavy workloads on a loadtest cluster to verify that no nondeterminism remains in the iterator workflow --- store/multiversion/memiterator.go | 86 ++++++++++++++------------- store/multiversion/mergeiterator.go | 4 ++ store/multiversion/mvkv.go | 27 ++++----- store/multiversion/mvkv_test.go | 8 ++- store/multiversion/trackediterator.go | 7 ++- 5 files changed, 71 insertions(+), 61 deletions(-) diff --git a/store/multiversion/memiterator.go b/store/multiversion/memiterator.go index 43e8e306b..32cb257b8 100644 --- a/store/multiversion/memiterator.go +++ b/store/multiversion/memiterator.go @@ -12,19 +12,13 @@ import ( // Implements Iterator. type memIterator struct { types.Iterator - - mvStore MultiVersionStore - writeset WriteSet - index int - abortChannel chan occtypes.Abort - ReadsetHandler + mvkv *VersionIndexedStore } func (store *VersionIndexedStore) newMemIterator( start, end []byte, items *dbm.MemDB, ascending bool, - readsetHandler ReadsetHandler, ) *memIterator { var iter types.Iterator var err error @@ -43,40 +37,25 @@ func (store *VersionIndexedStore) newMemIterator( } return &memIterator{ - Iterator: iter, - mvStore: store.multiVersionStore, - index: store.transactionIndex, - abortChannel: store.abortChannel, - writeset: store.GetWriteset(), - ReadsetHandler: readsetHandler, + Iterator: iter, + mvkv: store, } } -// try to get value from the writeset, otherwise try to get from multiversion store, otherwise try to get from parent iterator +// try to get value from the writeset, otherwise try to get from multiversion store, otherwise try to get from parent func (mi *memIterator) Value() []byte { key := mi.Iterator.Key() + // TODO: verify that this is correct + return mi.mvkv.Get(key) +} - // try fetch from writeset - return if exists - if val, ok := mi.writeset[string(key)]; ok { - return val - } - - // get the value from the multiversion store - val := mi.mvStore.GetLatestBeforeIndex(mi.index, key) - - // if we have an estiamte, write to abort channel - if val.IsEstimate() { - mi.abortChannel <- occtypes.NewEstimateAbort(val.Index()) - } +type validationIterator struct { + types.Iterator - // need to update readset - // if we have a deleted value, return nil - if val.IsDeleted() { - defer mi.ReadsetHandler.UpdateReadSet(key, nil) - return nil - } - defer mi.ReadsetHandler.UpdateReadSet(key, val.Value()) - return val.Value() + mvStore MultiVersionStore + writeset WriteSet + index int + abortChannel chan occtypes.Abort } func (store *Store) newMVSValidationIterator( @@ -86,7 +65,7 @@ func (store *Store) newMVSValidationIterator( ascending bool, writeset WriteSet, abortChannel chan occtypes.Abort, -) *memIterator { +) *validationIterator { var iter types.Iterator var err error @@ -103,12 +82,35 @@ func (store *Store) newMVSValidationIterator( panic(err) } - return &memIterator{ - Iterator: iter, - mvStore: store, - index: index, - abortChannel: abortChannel, - ReadsetHandler: NoOpHandler{}, - writeset: writeset, + return &validationIterator{ + Iterator: iter, + mvStore: store, + index: index, + abortChannel: abortChannel, + writeset: writeset, + } +} + +// try to get value from the writeset, otherwise try to get from multiversion store, otherwise try to get from parent iterator +func (vi *validationIterator) Value() []byte { + key := vi.Iterator.Key() + + // try fetch from writeset - return if exists + if val, ok := vi.writeset[string(key)]; ok { + return val } + + // get the value from the multiversion store + val := vi.mvStore.GetLatestBeforeIndex(vi.index, key) + + // if we have an estimate, write to abort channel + if val.IsEstimate() { + vi.abortChannel <- occtypes.NewEstimateAbort(val.Index()) + } + + // if we have a deleted value, return nil + if val.IsDeleted() { + return nil + } + return val.Value() } diff --git a/store/multiversion/mergeiterator.go b/store/multiversion/mergeiterator.go index 3b5cee741..1e398cf94 100644 --- a/store/multiversion/mergeiterator.go +++ b/store/multiversion/mergeiterator.go @@ -138,6 +138,8 @@ func (iter *mvsMergeIterator) Value() []byte { // If cache is invalid, get the parent value. if !iter.cache.Valid() { value := iter.parent.Value() + // add values read from parent to readset + iter.ReadsetHandler.UpdateReadSet(iter.parent.Key(), value) return value } @@ -148,6 +150,8 @@ func (iter *mvsMergeIterator) Value() []byte { switch cmp { case -1: // parent < cache value := iter.parent.Value() + // add values read from parent to readset + iter.ReadsetHandler.UpdateReadSet(iter.parent.Key(), value) return value case 0, 1: // parent >= cache value := iter.cache.Value() diff --git a/store/multiversion/mvkv.go b/store/multiversion/mvkv.go index 1e8437ad7..13511281f 100644 --- a/store/multiversion/mvkv.go +++ b/store/multiversion/mvkv.go @@ -78,8 +78,6 @@ type VersionIndexedStore struct { iterateset Iterateset // TODO: need to add iterateset here as well - // dirty keys that haven't been sorted yet for iteration - dirtySet map[string]struct{} // used for iterators - populated at the time of iterator instantiation // TODO: when we want to perform iteration, we need to move all the dirty keys (writeset and readset) into the sortedTree and then combine with the iterators for the underlying stores sortedStore *dbm.MemDB // always ascending sorted @@ -102,7 +100,6 @@ func NewVersionIndexedStore(parent types.KVStore, multiVersionStore MultiVersion readset: make(map[string][]byte), writeset: make(map[string][]byte), iterateset: []iterationTracker{}, - dirtySet: make(map[string]struct{}), sortedStore: dbm.NewMemDB(), parent: parent, multiVersionStore: multiVersionStore, @@ -231,7 +228,7 @@ func (store *VersionIndexedStore) Delete(key []byte) { // defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "delete") types.AssertValidKey(key) - store.setValue(key, nil, true, true) + store.setValue(key, nil) } // Has implements types.KVStore. @@ -248,7 +245,7 @@ func (store *VersionIndexedStore) Set(key []byte, value []byte) { // defer telemetry.MeasureSince(time.Now(), "store", "mvkv", "set") types.AssertValidKey(key) - store.setValue(key, value, false, true) + store.setValue(key, value) } // Iterator implements types.KVStore. @@ -278,11 +275,15 @@ func (store *VersionIndexedStore) iterator(start []byte, end []byte, ascending b for key := range store.writeset { memDB.Set([]byte(key), []byte{}) } + // also add readset elements such that they fetch from readset instead of parent + for key := range store.readset { + memDB.Set([]byte(key), []byte{}) + } var parent, memIterator types.Iterator // make a memIterator - memIterator = store.newMemIterator(start, end, memDB, ascending, store) + memIterator = store.newMemIterator(start, end, memDB, ascending) if ascending { parent = store.parent.Iterator(start, end) @@ -293,7 +294,7 @@ func (store *VersionIndexedStore) iterator(start []byte, end []byte, ascending b mergeIterator := NewMVSMergeIterator(parent, memIterator, ascending, store) iterationTracker := NewIterationTracker(start, end, ascending, store.writeset) - trackedIterator := NewTrackedIterator(mergeIterator, iterationTracker, store) + trackedIterator := NewTrackedIterator(mergeIterator, iterationTracker, store, store) // mergeIterator return trackedIterator @@ -326,14 +327,11 @@ func (v *VersionIndexedStore) GetWorkingHash() ([]byte, error) { } // Only entrypoint to mutate writeset -func (store *VersionIndexedStore) setValue(key, value []byte, deleted bool, dirty bool) { +func (store *VersionIndexedStore) setValue(key, value []byte) { types.AssertValidKey(key) keyStr := string(key) store.writeset[keyStr] = value - if dirty { - store.dirtySet[keyStr] = struct{}{} - } } func (store *VersionIndexedStore) WriteToMultiVersionStore() { @@ -358,9 +356,10 @@ func (store *VersionIndexedStore) WriteEstimatesToMultiVersionStore() { func (store *VersionIndexedStore) UpdateReadSet(key []byte, value []byte) { // add to readset keyStr := string(key) - store.readset[keyStr] = value - // add to dirty set - store.dirtySet[keyStr] = struct{}{} + // TODO: maybe only add if not already existing? + if _, ok := store.readset[keyStr]; !ok { + store.readset[keyStr] = value + } } // Write implements types.CacheWrap so this store can exist on the cache multi store diff --git a/store/multiversion/mvkv_test.go b/store/multiversion/mvkv_test.go index 44304fd50..008a7fa61 100644 --- a/store/multiversion/mvkv_test.go +++ b/store/multiversion/mvkv_test.go @@ -375,15 +375,17 @@ func TestIterator(t *testing.T) { mvs.SetEstimatedWriteset(1, 1, map[string][]byte{ "key2": []byte("value1_b"), }) - + // need to reset readset + abortC2 := make(chan scheduler.Abort) + visNew := multiversion.NewVersionIndexedStore(parentKVStore, mvs, 2, 3, abortC2) go func() { // new iter - iter4 := vis.Iterator([]byte("000"), []byte("key5")) + iter4 := visNew.Iterator([]byte("000"), []byte("key5")) defer iter4.Close() for ; iter4.Valid(); iter4.Next() { } }() - abort := <-abortC // read the abort from the channel + abort := <-abortC2 // read the abort from the channel require.Equal(t, 1, abort.DependentTxIdx) } diff --git a/store/multiversion/trackediterator.go b/store/multiversion/trackediterator.go index 361d848cb..24a1d7a16 100644 --- a/store/multiversion/trackediterator.go +++ b/store/multiversion/trackediterator.go @@ -7,16 +7,18 @@ type trackedIterator struct { types.Iterator iterateset iterationTracker + ReadsetHandler IterateSetHandler } // TODO: test -func NewTrackedIterator(iter types.Iterator, iterationTracker iterationTracker, iterateSetHandler IterateSetHandler) *trackedIterator { +func NewTrackedIterator(iter types.Iterator, iterationTracker iterationTracker, iterateSetHandler IterateSetHandler, readSetHandler ReadsetHandler) *trackedIterator { return &trackedIterator{ Iterator: iter, iterateset: iterationTracker, IterateSetHandler: iterateSetHandler, + ReadsetHandler: readSetHandler, } } @@ -43,9 +45,10 @@ func (ti *trackedIterator) Key() []byte { // Value calls the iterator.Key() and adds the key to the iterateset, then returns the value from the iterator func (ti *trackedIterator) Value() []byte { key := ti.Iterator.Key() + val := ti.Iterator.Value() // add key to the tracker ti.iterateset.AddKey(key) - return ti.Iterator.Value() + return val } func (ti *trackedIterator) Next() { From 6bf4d72b62b9a3eff3d0148e2d7284cbfae711b3 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Tue, 2 Jan 2024 09:35:04 -0600 Subject: [PATCH 28/36] fix interface --- storev2/rootmulti/store.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/storev2/rootmulti/store.go b/storev2/rootmulti/store.go index be7f4f2af..2fb19f219 100644 --- a/storev2/rootmulti/store.go +++ b/storev2/rootmulti/store.go @@ -780,3 +780,17 @@ func (rs *Store) Snapshot(height uint64, protoWriter protoio.Writer) error { return nil } + +// SetKVStores implements types.CommitMultiStore. +func (*Store) SetKVStores(handler func(key types.StoreKey, s types.KVStore) types.CacheWrap) types.MultiStore { + panic("unimplemented") +} + +// StoreKeys implements types.CommitMultiStore. +func (s *Store) StoreKeys() []types.StoreKey { + res := make([]types.StoreKey, len(s.storeKeys)) + for _, sk := range s.storeKeys { + res = append(res, sk) + } + return res +} From 6878b9689f7d0a504904c9bab7f3977a6982c593 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Fri, 12 Jan 2024 13:21:48 -0600 Subject: [PATCH 29/36] fix deleteIterateSet and leave some comments (#395) ## Describe your changes and provide context ## Testing performed to validate your change --- store/multiversion/store.go | 2 +- tasks/scheduler.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/store/multiversion/store.go b/store/multiversion/store.go index 16b0e626b..eae1175d5 100644 --- a/store/multiversion/store.go +++ b/store/multiversion/store.go @@ -236,7 +236,7 @@ func (s *Store) ClearReadset(index int) { } func (s *Store) ClearIterateset(index int) { - s.txReadSets.Delete(index) + s.txIterateSets.Delete(index) } // CollectIteratorItems implements MultiVersionStore. It will return a memDB containing all of the keys present in the multiversion store within the iteration range prior to (exclusive of) the index. diff --git a/tasks/scheduler.go b/tasks/scheduler.go index c0e7c143d..84b55d59d 100644 --- a/tasks/scheduler.go +++ b/tasks/scheduler.go @@ -260,6 +260,9 @@ func (s *scheduler) shouldRerun(task *deliverTxTask) bool { // validated tasks can become unvalidated if an earlier re-run task now conflicts case statusExecuted, statusValidated: + // With the current scheduler, we won't actually get to this step if a previous task has already been determined to be invalid, + // since we choose to fail fast and mark the subsequent tasks as invalid as well. + // TODO: in a future async scheduler that no longer exhaustively validates in order, we may need to carefully handle the `valid=true` with conflicts case if valid, conflicts := s.findConflicts(task); !valid { s.invalidateTask(task) From d08b8f0610f543802d12c935ac5c0363fcf3a5ac Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Thu, 18 Jan 2024 15:43:00 -0600 Subject: [PATCH 30/36] Occ enabled cfg (#398) ## Describe your changes and provide context This adds `occ-enabled` as a config for baseapp to control whether to execute transactions with OCC parallelism. ## Testing performed to validate your change Tested on sei-chain --- baseapp/baseapp.go | 7 +++++++ baseapp/baseapp_test.go | 5 +++++ baseapp/options.go | 11 +++++++++++ server/config/config.go | 7 +++++++ server/config/config_test.go | 5 +++++ server/config/toml.go | 9 ++++++--- simapp/simd/cmd/root.go | 1 + 7 files changed, 42 insertions(+), 3 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 1964e552a..fe53f5fe6 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -60,6 +60,7 @@ const ( FlagChainID = "chain-id" FlagConcurrencyWorkers = "concurrency-workers" + FlagOccEnabled = "occ-enabled" ) var ( @@ -167,6 +168,7 @@ type BaseApp struct { //nolint: maligned TracingInfo *tracing.Info concurrencyWorkers int + occEnabled bool } type appStore struct { @@ -318,6 +320,11 @@ func (app *BaseApp) ConcurrencyWorkers() int { return app.concurrencyWorkers } +// OccEnabled returns the whether OCC is enabled for the BaseApp. +func (app *BaseApp) OccEnabled() bool { + return app.occEnabled +} + // Version returns the application's version string. func (app *BaseApp) Version() string { return app.version diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 6fcebace5..20cfc326f 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -140,6 +140,11 @@ func TestSetMinGasPrices(t *testing.T) { require.Equal(t, minGasPrices, app.minGasPrices) } +func TestSetOccEnabled(t *testing.T) { + app := newBaseApp(t.Name(), SetOccEnabled(true)) + require.True(t, app.OccEnabled()) +} + // func TestGetMaximumBlockGas(t *testing.T) { // app := setupBaseApp(t) // app.InitChain(context.Background(), &abci.RequestInitChain{}) diff --git a/baseapp/options.go b/baseapp/options.go index e7a07de2a..1ef2622b3 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -86,6 +86,10 @@ func SetConcurrencyWorkers(workers int) func(*BaseApp) { return func(app *BaseApp) { app.SetConcurrencyWorkers(workers) } } +func SetOccEnabled(occEnabled bool) func(*BaseApp) { + return func(app *BaseApp) { app.SetOccEnabled(occEnabled) } +} + // SetSnapshotKeepRecent sets the recent snapshots to keep. func SetSnapshotKeepRecent(keepRecent uint32) func(*BaseApp) { return func(app *BaseApp) { app.SetSnapshotKeepRecent(keepRecent) } @@ -301,6 +305,13 @@ func (app *BaseApp) SetConcurrencyWorkers(workers int) { app.concurrencyWorkers = workers } +func (app *BaseApp) SetOccEnabled(occEnabled bool) { + if app.sealed { + panic("SetOccEnabled() on sealed BaseApp") + } + app.occEnabled = occEnabled +} + // SetSnapshotKeepRecent sets the number of recent snapshots to keep. func (app *BaseApp) SetSnapshotKeepRecent(snapshotKeepRecent uint32) { if app.sealed { diff --git a/server/config/config.go b/server/config/config.go index 9a3a51e88..e7d6c3c6d 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -24,6 +24,9 @@ const ( // DefaultConcurrencyWorkers defines the default workers to use for concurrent transactions DefaultConcurrencyWorkers = 20 + + // DefaultOccEanbled defines whether to use OCC for tx processing + DefaultOccEnabled = true ) // BaseConfig defines the server's basic configuration @@ -95,6 +98,8 @@ type BaseConfig struct { // ConcurrencyWorkers defines the number of workers to use for concurrent // transaction execution. A value of -1 means unlimited workers. Default value is 10. ConcurrencyWorkers int `mapstructure:"concurrency-workers"` + // Whether to enable optimistic concurrency control for tx execution, default is true + OccEnabled bool `mapstructure:"occ-enabled"` } // APIConfig defines the API listener configuration. @@ -246,6 +251,7 @@ func DefaultConfig() *Config { CompactionInterval: 0, NoVersioning: false, ConcurrencyWorkers: DefaultConcurrencyWorkers, + OccEnabled: DefaultOccEnabled, }, Telemetry: telemetry.Config{ Enabled: false, @@ -323,6 +329,7 @@ func GetConfig(v *viper.Viper) (Config, error) { NumOrphanPerFile: v.GetInt("num-orphan-per-file"), OrphanDirectory: v.GetString("orphan-dir"), ConcurrencyWorkers: v.GetInt("concurrency-workers"), + OccEnabled: v.GetBool("occ-enabled"), }, Telemetry: telemetry.Config{ ServiceName: v.GetString("telemetry.service-name"), diff --git a/server/config/config_test.go b/server/config/config_test.go index 040bfa788..61801e9ca 100644 --- a/server/config/config_test.go +++ b/server/config/config_test.go @@ -28,3 +28,8 @@ func TestSetConcurrencyWorkers(t *testing.T) { cfg := DefaultConfig() require.Equal(t, DefaultConcurrencyWorkers, cfg.ConcurrencyWorkers) } + +func TestOCCEnabled(t *testing.T) { + cfg := DefaultConfig() + require.Equal(t, true, cfg.OccEnabled) +} diff --git a/server/config/toml.go b/server/config/toml.go index 2b35d4ae5..6e1addfc4 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -23,7 +23,7 @@ const DefaultConfigTemplate = `# This is a TOML config file. # specified in this config (e.g. 0.25token1;0.0001token2). minimum-gas-prices = "{{ .BaseConfig.MinGasPrices }}" -# Pruning Strategies: +# Pruning Strategies: # - default: Keep the recent 362880 blocks and prune is triggered every 10 blocks # - nothing: all historic states will be saved, nothing will be deleted (i.e. archiving node) # - everything: all saved states will be deleted, storing only the recent 2 blocks; pruning at every block @@ -75,11 +75,11 @@ inter-block-cache = {{ .BaseConfig.InterBlockCache }} # ["message.sender", "message.recipient"] index-events = {{ .BaseConfig.IndexEvents }} -# IavlCacheSize set the size of the iavl tree cache. +# IavlCacheSize set the size of the iavl tree cache. # Default cache size is 50mb. iavl-cache-size = {{ .BaseConfig.IAVLCacheSize }} -# IAVLDisableFastNode enables or disables the fast node feature of IAVL. +# IAVLDisableFastNode enables or disables the fast node feature of IAVL. # Default is true. iavl-disable-fastnode = {{ .BaseConfig.IAVLDisableFastNode }} @@ -107,6 +107,9 @@ orphan-dir = "{{ .BaseConfig.OrphanDirectory }}" # concurrency-workers defines how many workers to run for concurrent transaction execution # concurrency-workers = {{ .BaseConfig.ConcurrencyWorkers }} +# occ-enabled defines whether OCC is enabled or not for transaction execution +occ-enabled = {{ .BaseConfig.OccEnabled }} + ############################################################################### ### Telemetry Configuration ### ############################################################################### diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index c84c0e835..4b3f5dfee 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -305,6 +305,7 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, t baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(server.FlagIAVLCacheSize))), baseapp.SetIAVLDisableFastNode(cast.ToBool(appOpts.Get(server.FlagIAVLFastNode))), baseapp.SetCompactionInterval(cast.ToUint64(appOpts.Get(server.FlagCompactionInterval))), + baseapp.SetOccEnabled(cast.ToBool(appOpts.Get(baseapp.FlagOccEnabled))), ) } From 75d1151b04f2e141ef894d85005e379de891a5f5 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Thu, 25 Jan 2024 15:21:21 -0600 Subject: [PATCH 31/36] Remove block gas meter in occ (#407) ## Describe your changes and provide context This removes the block gas meter for occ, and will eventually be rebased out with a corresponding change that will end up in main ## Testing performed to validate your change loadtest cluster testing --- baseapp/abci.go | 28 +---- baseapp/baseapp.go | 57 +-------- baseapp/block_gas_test.go | 202 ------------------------------- baseapp/deliver_tx_batch_test.go | 1 - baseapp/deliver_tx_test.go | 31 +---- types/context.go | 43 +++---- types/context_test.go | 3 - x/capability/capability_test.go | 6 +- x/capability/genesis_test.go | 3 +- x/capability/keeper/keeper.go | 11 +- x/upgrade/abci.go | 1 - 11 files changed, 31 insertions(+), 355 deletions(-) delete mode 100644 baseapp/block_gas_test.go diff --git a/baseapp/abci.go b/baseapp/abci.go index 5495ee0b0..f451e8610 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -71,11 +71,6 @@ func (app *BaseApp) InitChain(ctx context.Context, req *abci.RequestInitChain) ( return } - // add block gas meter for any genesis transactions (allow infinite gas) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) - app.prepareProposalState.ctx = app.prepareProposalState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) - app.processProposalState.ctx = app.processProposalState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) - resp := app.initChainer(app.deliverState.ctx, *req) app.initChainer(app.prepareProposalState.ctx, *req) app.initChainer(app.processProposalState.ctx, *req) @@ -1034,16 +1029,9 @@ func (app *BaseApp) ProcessProposal(ctx context.Context, req *abci.RequestProces app.setProcessProposalHeader(header) } - // add block gas meter - var gasMeter sdk.GasMeter - if maxGas := app.getMaximumBlockGas(app.processProposalState.ctx); maxGas > 0 { - gasMeter = sdk.NewGasMeter(maxGas) - } else { - gasMeter = sdk.NewInfiniteGasMeter() - } - // NOTE: header hash is not set in NewContext, so we manually set it here - app.prepareProcessProposalState(gasMeter, req.Hash) + + app.prepareProcessProposalState(req.Hash) defer func() { if err := recover(); err != nil { @@ -1116,22 +1104,14 @@ func (app *BaseApp) FinalizeBlock(ctx context.Context, req *abci.RequestFinalize app.setDeliverStateHeader(header) } - // add block gas meter - var gasMeter sdk.GasMeter - if maxGas := app.getMaximumBlockGas(app.deliverState.ctx); maxGas > 0 { - gasMeter = sdk.NewGasMeter(maxGas) - } else { - gasMeter = sdk.NewInfiniteGasMeter() - } - // NOTE: header hash is not set in NewContext, so we manually set it here - app.prepareDeliverState(gasMeter, req.Hash) + app.prepareDeliverState(req.Hash) // we also set block gas meter to checkState in case the application needs to // verify gas consumption during (Re)CheckTx if app.checkState != nil { - app.checkState.SetContext(app.checkState.ctx.WithBlockGasMeter(gasMeter).WithHeaderHash(req.Hash)) + app.checkState.SetContext(app.checkState.ctx.WithHeaderHash(req.Hash)) } if app.finalizeBlocker != nil { diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index fe53f5fe6..07f577de4 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -613,8 +613,8 @@ func (app *BaseApp) preparePrepareProposalState() { } } -func (app *BaseApp) prepareProcessProposalState(gasMeter sdk.GasMeter, headerHash []byte) { - app.processProposalState.SetContext(app.processProposalState.Context().WithBlockGasMeter(gasMeter). +func (app *BaseApp) prepareProcessProposalState(headerHash []byte) { + app.processProposalState.SetContext(app.processProposalState.Context(). WithHeaderHash(headerHash). WithConsensusParams(app.GetConsensusParams(app.processProposalState.Context()))) @@ -623,9 +623,8 @@ func (app *BaseApp) prepareProcessProposalState(gasMeter sdk.GasMeter, headerHas } } -func (app *BaseApp) prepareDeliverState(gasMeter sdk.GasMeter, headerHash []byte) { +func (app *BaseApp) prepareDeliverState(headerHash []byte) { app.deliverState.SetContext(app.deliverState.Context(). - WithBlockGasMeter(gasMeter). WithHeaderHash(headerHash). WithConsensusParams(app.GetConsensusParams(app.deliverState.Context()))) } @@ -724,27 +723,6 @@ func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp *tmproto.ConsensusP app.paramStore.Set(ctx, ParamStoreKeyABCIParams, cp.Abci) } -// getMaximumBlockGas gets the maximum gas from the consensus params. It panics -// if maximum block gas is less than negative one and returns zero if negative -// one. -func (app *BaseApp) getMaximumBlockGas(ctx sdk.Context) uint64 { - cp := app.GetConsensusParams(ctx) - if cp == nil || cp.Block == nil { - return 0 - } - - maxGas := cp.Block.MaxGas - - // TODO::: This is a temporary fix, max gas causes non-deterministic behavior - // with parallel TX - switch { - case maxGas < -1: - panic(fmt.Sprintf("invalid maximum block gas: %d", maxGas)) - default: - return 0 - } -} - func (app *BaseApp) validateHeight(req abci.RequestBeginBlock) error { if req.Header.Height < 1 { return fmt.Errorf("invalid height: %d", req.Header.Height) @@ -879,11 +857,6 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, txBytes []byte) (gInf ms := ctx.MultiStore() - // only run the tx if there is block gas remaining - if mode == runTxModeDeliver && ctx.BlockGasMeter().IsOutOfGas() { - return gInfo, nil, nil, -1, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx") - } - defer func() { if r := recover(); r != nil { acltypes.SendAllSignalsForTx(ctx.TxCompletionChannels()) @@ -896,27 +869,6 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, txBytes []byte) (gInf gInfo = sdk.GasInfo{GasWanted: gasWanted, GasUsed: ctx.GasMeter().GasConsumed()} }() - blockGasConsumed := false - // consumeBlockGas makes sure block gas is consumed at most once. It must happen after - // tx processing, and must be execute even if tx processing fails. Hence we use trick with `defer` - consumeBlockGas := func() { - if !blockGasConsumed { - blockGasConsumed = true - ctx.BlockGasMeter().ConsumeGas( - ctx.GasMeter().GasConsumedToLimit(), "block gas meter", - ) - } - } - - // If BlockGasMeter() panics it will be caught by the above recover and will - // return an error - in any case BlockGasMeter will consume gas past the limit. - // - // NOTE: This must exist in a separate defer function for the above recovery - // to recover from this one. - if mode == runTxModeDeliver { - defer consumeBlockGas() - } - tx, err := app.txDecoder(txBytes) if err != nil { return sdk.GasInfo{}, nil, nil, 0, err @@ -1004,9 +956,6 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, txBytes []byte) (gInf result, err = app.runMsgs(runMsgCtx, msgs, mode) if err == nil && mode == runTxModeDeliver { - // When block gas exceeds, it'll panic and won't commit the cached store. - consumeBlockGas() - msCache.Write() } // we do this since we will only be looking at result in DeliverTx diff --git a/baseapp/block_gas_test.go b/baseapp/block_gas_test.go deleted file mode 100644 index 2d154709c..000000000 --- a/baseapp/block_gas_test.go +++ /dev/null @@ -1,202 +0,0 @@ -package baseapp_test - -import ( - "context" - "encoding/json" - "fmt" - "math" - "testing" - - "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/libs/log" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - dbm "github.com/tendermint/tm-db" - - "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/tx" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/simapp" - "github.com/cosmos/cosmos-sdk/testutil/testdata" - sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - txtypes "github.com/cosmos/cosmos-sdk/types/tx" - "github.com/cosmos/cosmos-sdk/types/tx/signing" - xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" -) - -func TestBaseApp_BlockGas(t *testing.T) { - testcases := []struct { - name string - gasToConsume uint64 // gas to consume in the msg execution - panicTx bool // panic explicitly in tx execution - expErr bool - }{ - {"less than block gas meter", 10, false, false}, - // {"more than block gas meter", blockMaxGas, false, true}, - // {"more than block gas meter", uint64(float64(blockMaxGas) * 1.2), false, true}, - // {"consume MaxUint64", math.MaxUint64, false, true}, - {"consume MaxGasWanted", txtypes.MaxGasWanted, false, true}, - {"consume block gas when paniced", 10, true, true}, - } - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - var app *simapp.SimApp - routerOpt := func(bapp *baseapp.BaseApp) { - route := (&testdata.TestMsg{}).Route() - bapp.Router().AddRoute(sdk.NewRoute(route, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { - _, ok := msg.(*testdata.TestMsg) - if !ok { - return &sdk.Result{}, fmt.Errorf("Wrong Msg type, expected %T, got %T", (*testdata.TestMsg)(nil), msg) - } - ctx.KVStore(app.GetKey(banktypes.ModuleName)).Set([]byte("ok"), []byte("ok")) - ctx.GasMeter().ConsumeGas(tc.gasToConsume, "TestMsg") - if tc.panicTx { - panic("panic in tx execution") - } - return &sdk.Result{}, nil - })) - } - encCfg := simapp.MakeTestEncodingConfig() - encCfg.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil) - encCfg.InterfaceRegistry.RegisterImplementations((*sdk.Msg)(nil), - &testdata.TestMsg{}, - ) - app = simapp.NewSimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, map[int64]bool{}, "", 0, nil, encCfg, &simapp.EmptyAppOptions{}, routerOpt) - genState := simapp.NewDefaultGenesisState(encCfg.Marshaler) - stateBytes, err := json.MarshalIndent(genState, "", " ") - require.NoError(t, err) - app.InitChain(context.Background(), &abci.RequestInitChain{ - Validators: []abci.ValidatorUpdate{}, - ConsensusParams: simapp.DefaultConsensusParams, - AppStateBytes: stateBytes, - }) - - ctx := app.NewContext(false, tmproto.Header{}) - - // tx fee - feeCoin := sdk.NewCoin("atom", sdk.NewInt(150)) - feeAmount := sdk.NewCoins(feeCoin) - - // test account and fund - priv1, _, addr1 := testdata.KeyTestPubAddr() - err = app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, feeAmount) - require.NoError(t, err) - err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr1, feeAmount) - require.NoError(t, err) - require.Equal(t, feeCoin.Amount, app.BankKeeper.GetBalance(ctx, addr1, feeCoin.Denom).Amount) - seq, _ := app.AccountKeeper.GetSequence(ctx, addr1) - require.Equal(t, uint64(0), seq) - - // msg and signatures - msg := testdata.NewTestMsg(addr1) - - txBuilder := encCfg.TxConfig.NewTxBuilder() - require.NoError(t, txBuilder.SetMsgs(msg)) - txBuilder.SetFeeAmount(feeAmount) - txBuilder.SetGasLimit(txtypes.MaxGasWanted) // tx validation checks that gasLimit can't be bigger than this - - privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{6}, []uint64{0} - _, txBytes, err := createTestTx(encCfg.TxConfig, txBuilder, privs, accNums, accSeqs, ctx.ChainID()) - require.NoError(t, err) - - rsp, _ := app.FinalizeBlock(context.Background(), &abci.RequestFinalizeBlock{ - Height: 1, - Txs: [][]byte{txBytes}, - }) - - // check result - ctx = app.GetContextForDeliverTx(txBytes) - okValue := ctx.KVStore(app.GetKey(banktypes.ModuleName)).Get([]byte("ok")) - - if tc.expErr { - if tc.panicTx { - require.Equal(t, sdkerrors.ErrPanic.ABCICode(), rsp.TxResults[0].Code) - } else { - require.Equal(t, sdkerrors.ErrOutOfGas.ABCICode(), rsp.TxResults[0].Code) - } - require.Empty(t, okValue) - } else { - require.Equal(t, uint32(0), rsp.TxResults[0].Code) - require.Equal(t, []byte("ok"), okValue) - } - // check block gas is always consumed - this value may change if we update the logic for - // how gas is consumed - baseGas := uint64(62766) // baseGas is the gas consumed before tx msg - expGasConsumed := addUint64Saturating(tc.gasToConsume, baseGas) - if expGasConsumed > txtypes.MaxGasWanted { - // capped by gasLimit - expGasConsumed = txtypes.MaxGasWanted - } - require.Equal(t, int(expGasConsumed), int(ctx.BlockGasMeter().GasConsumed())) - // tx fee is always deducted - require.Equal(t, 0, int(app.BankKeeper.GetBalance(ctx, addr1, feeCoin.Denom).Amount.Int64())) - // sender's sequence is always increased - seq, err = app.AccountKeeper.GetSequence(ctx, addr1) - require.NoError(t, err) - require.Equal(t, uint64(1), seq) - }) - } -} - -func createTestTx(txConfig client.TxConfig, txBuilder client.TxBuilder, privs []cryptotypes.PrivKey, accNums []uint64, accSeqs []uint64, chainID string) (xauthsigning.Tx, []byte, error) { - // First round: we gather all the signer infos. We use the "set empty - // signature" hack to do that. - var sigsV2 []signing.SignatureV2 - for i, priv := range privs { - sigV2 := signing.SignatureV2{ - PubKey: priv.PubKey(), - Data: &signing.SingleSignatureData{ - SignMode: txConfig.SignModeHandler().DefaultMode(), - Signature: nil, - }, - Sequence: accSeqs[i], - } - - sigsV2 = append(sigsV2, sigV2) - } - err := txBuilder.SetSignatures(sigsV2...) - if err != nil { - return nil, nil, err - } - - // Second round: all signer infos are set, so each signer can sign. - sigsV2 = []signing.SignatureV2{} - for i, priv := range privs { - signerData := xauthsigning.SignerData{ - ChainID: chainID, - AccountNumber: accNums[i], - Sequence: accSeqs[i], - } - sigV2, err := tx.SignWithPrivKey( - txConfig.SignModeHandler().DefaultMode(), signerData, - txBuilder, priv, txConfig, accSeqs[i]) - if err != nil { - return nil, nil, err - } - - sigsV2 = append(sigsV2, sigV2) - } - err = txBuilder.SetSignatures(sigsV2...) - if err != nil { - return nil, nil, err - } - - txBytes, err := txConfig.TxEncoder()(txBuilder.GetTx()) - if err != nil { - return nil, nil, err - } - - return txBuilder.GetTx(), txBytes, nil -} - -func addUint64Saturating(a, b uint64) uint64 { - if math.MaxUint64-a < b { - return math.MaxUint64 - } - - return a + b -} diff --git a/baseapp/deliver_tx_batch_test.go b/baseapp/deliver_tx_batch_test.go index c8a29b8b7..c3d92b4bd 100644 --- a/baseapp/deliver_tx_batch_test.go +++ b/baseapp/deliver_tx_batch_test.go @@ -111,7 +111,6 @@ func TestDeliverTxBatch(t *testing.T) { for blockN := 0; blockN < nBlocks; blockN++ { header := tmproto.Header{Height: int64(blockN) + 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) var requests []*sdk.DeliverTxEntry diff --git a/baseapp/deliver_tx_test.go b/baseapp/deliver_tx_test.go index c9fdc767d..0e144e4a7 100644 --- a/baseapp/deliver_tx_test.go +++ b/baseapp/deliver_tx_test.go @@ -219,7 +219,6 @@ func TestWithRouter(t *testing.T) { for blockN := 0; blockN < nBlocks; blockN++ { header := tmproto.Header{Height: int64(blockN) + 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) for i := 0; i < txPerHeight; i++ { @@ -267,7 +266,6 @@ func TestBaseApp_EndBlock(t *testing.T) { app.Seal() app.setDeliverState(tmproto.Header{}) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) res := app.EndBlock(app.deliverState.ctx, abci.RequestEndBlock{}) require.Empty(t, app.deliverState.ctx.MultiStore().GetEvents()) @@ -323,7 +321,6 @@ func TestQuery(t *testing.T) { // query is still empty after a DeliverTx before we commit header := tmproto.Header{Height: app.LastBlockHeight() + 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) _, resTx, err = app.Deliver(aminoTxEncoder(), tx) @@ -352,7 +349,6 @@ func TestGRPCQuery(t *testing.T) { app.InitChain(context.Background(), &abci.RequestInitChain{}) header := tmproto.Header{Height: app.LastBlockHeight() + 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) app.SetDeliverStateToCommit() app.Commit(context.Background()) @@ -434,7 +430,6 @@ func TestMultiMsgDeliverTx(t *testing.T) { header := tmproto.Header{Height: 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) tx := newTxCounter(0, 0, 1, 2) txBytes, err := codec.Marshal(tx) @@ -517,7 +512,6 @@ func TestSimulateTx(t *testing.T) { count := int64(blockN + 1) header := tmproto.Header{Height: count} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) tx := newTxCounter(count, count) @@ -577,7 +571,6 @@ func TestRunInvalidTransaction(t *testing.T) { header := tmproto.Header{Height: 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) // transaction with no messages @@ -706,7 +699,6 @@ func TestTxGasLimits(t *testing.T) { header := tmproto.Header{Height: 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) testCases := []struct { @@ -877,7 +869,6 @@ func TestCustomRunTxPanicHandler(t *testing.T) { header := tmproto.Header{Height: 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) app.AddRunTxRecoveryHandler(func(recoveryObj interface{}) error { @@ -921,7 +912,6 @@ func TestBaseAppAnteHandler(t *testing.T) { header := tmproto.Header{Height: app.LastBlockHeight() + 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) // execute a tx that will fail ante handler execution @@ -1035,7 +1025,6 @@ func TestGasConsumptionBadTx(t *testing.T) { header := tmproto.Header{Height: app.LastBlockHeight() + 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewGasMeter(app.getMaximumBlockGas(app.deliverState.ctx))) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) tx := newTxCounter(5, 0) @@ -1046,13 +1035,7 @@ func TestGasConsumptionBadTx(t *testing.T) { res := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes}) require.False(t, res.IsOK(), fmt.Sprintf("%v", res)) - // require next tx to fail due to black gas limit - tx = newTxCounter(5, 0) - txBytes, err = cdc.Marshal(tx) - require.NoError(t, err) - - res = app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes}) - require.False(t, res.IsOK(), fmt.Sprintf("%v", res)) + // removed the block gas exceeded because of removal of block gas meter, gasWanted < max block gas is still fulfilled by various other checks } func TestInitChainer(t *testing.T) { @@ -1132,7 +1115,6 @@ func TestInitChainer(t *testing.T) { // commit and ensure we can still query header := tmproto.Header{Height: app.LastBlockHeight() + 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) app.SetDeliverStateToCommit() app.Commit(context.Background()) @@ -1172,7 +1154,6 @@ func TestBeginBlock_WithInitialHeight(t *testing.T) { ) app.setDeliverState(tmproto.Header{Height: 4}) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) require.PanicsWithError(t, "invalid height: 4; expected: 3", func() { app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{ Header: tmproto.Header{ @@ -1465,11 +1446,9 @@ func TestCheckTx(t *testing.T) { // If a block is committed, CheckTx state should be reset. header := tmproto.Header{Height: 1} app.setDeliverState(header) - app.checkState.ctx = app.checkState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()).WithHeaderHash([]byte("hash")) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) + app.checkState.ctx = app.checkState.ctx.WithHeaderHash([]byte("hash")) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header, Hash: []byte("hash")}) - require.NotNil(t, app.checkState.ctx.BlockGasMeter(), "block gas meter should have been set to checkState") require.NotEmpty(t, app.checkState.ctx.HeaderHash()) app.EndBlock(app.deliverState.ctx, abci.RequestEndBlock{}) @@ -1510,7 +1489,6 @@ func TestDeliverTx(t *testing.T) { for blockN := 0; blockN < nBlocks; blockN++ { header := tmproto.Header{Height: int64(blockN) + 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) for i := 0; i < txPerHeight; i++ { @@ -1663,7 +1641,6 @@ func TestLoadVersionInvalid(t *testing.T) { header := tmproto.Header{Height: 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) app.SetDeliverStateToCommit() app.Commit(context.Background()) @@ -1715,7 +1692,6 @@ func setupBaseAppWithSnapshots(t *testing.T, blocks uint, blockTxs int, options keyCounter := 0 for height := int64(1); height <= int64(blocks); height++ { app.setDeliverState(tmproto.Header{Height: height}) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: tmproto.Header{Height: height}}) for txNum := 0; txNum < blockTxs; txNum++ { tx := txTest{Msgs: []sdk.Msg{}} @@ -1793,7 +1769,6 @@ func TestLoadVersion(t *testing.T) { // execute a block, collect commit ID header := tmproto.Header{Height: 1} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) app.SetDeliverStateToCommit() app.Commit(context.Background()) @@ -1801,7 +1776,6 @@ func TestLoadVersion(t *testing.T) { // execute a block, collect commit ID header = tmproto.Header{Height: 2} app.setDeliverState(header) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) app.SetDeliverStateToCommit() app.Commit(context.Background()) @@ -1885,7 +1859,6 @@ func TestSetLoader(t *testing.T) { // "execute" one block app.setDeliverState(tmproto.Header{Height: 2}) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: tmproto.Header{Height: 2}}) app.SetDeliverStateToCommit() app.Commit(context.Background()) diff --git a/types/context.go b/types/context.go index e36e88dc8..8ba51ef25 100644 --- a/types/context.go +++ b/types/context.go @@ -24,23 +24,22 @@ but please do not over-use it. We try to keep all data structured and standard additions here would be better just to add to the Context struct */ type Context struct { - ctx context.Context - ms MultiStore - header tmproto.Header - headerHash tmbytes.HexBytes - chainID string - txBytes []byte - logger log.Logger - voteInfo []abci.VoteInfo - gasMeter GasMeter - blockGasMeter GasMeter - occEnabled bool - checkTx bool - recheckTx bool // if recheckTx == true, then checkTx must also be true - minGasPrice DecCoins - consParams *tmproto.ConsensusParams - eventManager *EventManager - priority int64 // The tx priority, only relevant in CheckTx + ctx context.Context + ms MultiStore + header tmproto.Header + headerHash tmbytes.HexBytes + chainID string + txBytes []byte + logger log.Logger + voteInfo []abci.VoteInfo + gasMeter GasMeter + occEnabled bool + checkTx bool + recheckTx bool // if recheckTx == true, then checkTx must also be true + minGasPrice DecCoins + consParams *tmproto.ConsensusParams + eventManager *EventManager + priority int64 // The tx priority, only relevant in CheckTx txBlockingChannels acltypes.MessageAccessOpsChannelMapping txCompletionChannels acltypes.MessageAccessOpsChannelMapping @@ -93,10 +92,6 @@ func (c Context) GasMeter() GasMeter { return c.gasMeter } -func (c Context) BlockGasMeter() GasMeter { - return c.blockGasMeter -} - func (c Context) IsCheckTx() bool { return c.checkTx } @@ -274,12 +269,6 @@ func (c Context) WithGasMeter(meter GasMeter) Context { return c } -// WithBlockGasMeter returns a Context with an updated block GasMeter -func (c Context) WithBlockGasMeter(meter GasMeter) Context { - c.blockGasMeter = meter - return c -} - // WithIsCheckTx enables or disables CheckTx value for verifying transactions and returns an updated Context func (c Context) WithIsCheckTx(isCheckTx bool) Context { c.checkTx = isCheckTx diff --git a/types/context_test.go b/types/context_test.go index e49a82903..12d71b67b 100644 --- a/types/context_test.go +++ b/types/context_test.go @@ -92,7 +92,6 @@ func (s *contextTestSuite) TestContextWithCustom() { logger := mocks.NewMockLogger(ctrl) voteinfos := []abci.VoteInfo{{}} meter := types.NewGasMeter(10000) - blockGasMeter := types.NewGasMeter(20000) minGasPrices := types.DecCoins{types.NewInt64DecCoin("feetoken", 1)} headerHash := []byte("headerHash") @@ -106,7 +105,6 @@ func (s *contextTestSuite) TestContextWithCustom() { WithVoteInfos(voteinfos). WithGasMeter(meter). WithMinGasPrices(minGasPrices). - WithBlockGasMeter(blockGasMeter). WithHeaderHash(headerHash). WithIsOCCEnabled(isOCC) @@ -119,7 +117,6 @@ func (s *contextTestSuite) TestContextWithCustom() { s.Require().Equal(voteinfos, ctx.VoteInfos()) s.Require().Equal(meter, ctx.GasMeter()) s.Require().Equal(minGasPrices, ctx.MinGasPrices()) - s.Require().Equal(blockGasMeter, ctx.BlockGasMeter()) s.Require().Equal(headerHash, ctx.HeaderHash().Bytes()) s.Require().False(ctx.WithIsCheckTx(false).IsCheckTx()) diff --git a/x/capability/capability_test.go b/x/capability/capability_test.go index 45a5f6ea4..8f09d6d73 100644 --- a/x/capability/capability_test.go +++ b/x/capability/capability_test.go @@ -61,14 +61,10 @@ func (suite *CapabilityTestSuite) TestInitializeMemStore() { suite.Require().False(newKeeper.IsInitialized(ctx), "memstore initialized flag set before BeginBlock") // Mock app beginblock and ensure that no gas has been consumed and memstore is initialized - ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{}).WithBlockGasMeter(sdk.NewGasMeter(50)) - prevGas := ctx.BlockGasMeter().GasConsumed() + ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{}) restartedModule := capability.NewAppModule(suite.cdc, *newKeeper) restartedModule.BeginBlock(ctx, abci.RequestBeginBlock{}) suite.Require().True(newKeeper.IsInitialized(ctx), "memstore initialized flag not set") - gasUsed := ctx.BlockGasMeter().GasConsumed() - - suite.Require().Equal(prevGas, gasUsed, "beginblocker consumed gas during execution") // Mock the first transaction getting capability and subsequently failing // by using a cached context and discarding all cached writes. diff --git a/x/capability/genesis_test.go b/x/capability/genesis_test.go index 875ea9793..70aabb729 100644 --- a/x/capability/genesis_test.go +++ b/x/capability/genesis_test.go @@ -6,7 +6,6 @@ import ( dbm "github.com/tendermint/tm-db" "github.com/cosmos/cosmos-sdk/simapp" - sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/capability" "github.com/cosmos/cosmos-sdk/x/capability/keeper" @@ -40,7 +39,7 @@ func (suite *CapabilityTestSuite) TestGenesis() { newKeeper := keeper.NewKeeper(suite.cdc, newApp.GetKey(types.StoreKey), newApp.GetMemKey(types.MemStoreKey)) newSk1 := newKeeper.ScopeToModule(banktypes.ModuleName) newSk2 := newKeeper.ScopeToModule(stakingtypes.ModuleName) - deliverCtx, _ := newApp.BaseApp.NewUncachedContext(false, tmproto.Header{}).WithBlockGasMeter(sdk.NewInfiniteGasMeter()).CacheContext() + deliverCtx, _ := newApp.BaseApp.NewUncachedContext(false, tmproto.Header{}).CacheContext() capability.InitGenesis(deliverCtx, *newKeeper, *genState) diff --git a/x/capability/keeper/keeper.go b/x/capability/keeper/keeper.go index 35b8addf4..4ba2579cf 100644 --- a/x/capability/keeper/keeper.go +++ b/x/capability/keeper/keeper.go @@ -111,12 +111,9 @@ func (k *Keeper) InitMemStore(ctx sdk.Context) { panic(fmt.Sprintf("invalid memory store type; got %s, expected: %s", memStoreType, sdk.StoreTypeMemory)) } - // create context with no block gas meter to ensure we do not consume gas during local initialization logic. - noGasCtx := ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) - // check if memory store has not been initialized yet by checking if initialized flag is nil. - if !k.IsInitialized(noGasCtx) { - prefixStore := prefix.NewStore(noGasCtx.KVStore(k.storeKey), types.KeyPrefixIndexCapability) + if !k.IsInitialized(ctx) { + prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixIndexCapability) iterator := sdk.KVStorePrefixIterator(prefixStore, nil) // initialize the in-memory store for all persisted capabilities @@ -128,11 +125,11 @@ func (k *Keeper) InitMemStore(ctx sdk.Context) { var capOwners types.CapabilityOwners k.cdc.MustUnmarshal(iterator.Value(), &capOwners) - k.InitializeCapability(noGasCtx, index, capOwners) + k.InitializeCapability(ctx, index, capOwners) } // set the initialized flag so we don't rerun initialization logic - memStore := noGasCtx.KVStore(k.memKey) + memStore := ctx.KVStore(k.memKey) memStore.Set(types.KeyMemInitialized, []byte{1}) } } diff --git a/x/upgrade/abci.go b/x/upgrade/abci.go index e0791bc0c..a5e5d7ea8 100644 --- a/x/upgrade/abci.go +++ b/x/upgrade/abci.go @@ -111,7 +111,6 @@ func panicUpgradeNeeded(k keeper.Keeper, ctx sdk.Context, plan types.Plan) { func applyUpgrade(k keeper.Keeper, ctx sdk.Context, plan types.Plan) { ctx.Logger().Info(fmt.Sprintf("applying upgrade \"%s\" at %s", plan.Name, plan.DueAt())) - ctx = ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) k.ApplyUpgrade(ctx, plan) } From 48dfff586f729e8a65cdce121eba60bd9e0e3a96 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Thu, 8 Feb 2024 20:37:49 -0600 Subject: [PATCH 32/36] Relax locking contention (#427) ## Describe your changes and provide context This reduces some of the locking contention experienced when executing transactions with OCC. Additionally, undoes an earlier revert that reintroduced some locking for event emission ## Testing performed to validate your change --- baseapp/baseapp.go | 44 ++++++++++++++++++----------- store/cachekv/mergeiterator.go | 23 ++++++--------- store/cachekv/mergeiterator_test.go | 28 ++++-------------- store/cachekv/store.go | 7 +---- 4 files changed, 43 insertions(+), 59 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 07f577de4..d818da3d6 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -165,7 +165,8 @@ type BaseApp struct { //nolint: maligned TmConfig *tmcfg.Config - TracingInfo *tracing.Info + TracingInfo *tracing.Info + TracingEnabled bool concurrencyWorkers int occEnabled bool @@ -244,7 +245,8 @@ func NewBaseApp( tp := trace.NewNoopTracerProvider() otel.SetTracerProvider(trace.NewNoopTracerProvider()) tr := tp.Tracer("component-main") - if tracingEnabled := cast.ToBool(appOpts.Get(tracing.FlagTracing)); tracingEnabled { + tracingEnabled := cast.ToBool(appOpts.Get(tracing.FlagTracing)) + if tracingEnabled { tp, err := tracing.DefaultTracerProvider() if err != nil { panic(err) @@ -267,8 +269,9 @@ func NewBaseApp( grpcQueryRouter: NewGRPCQueryRouter(), msgServiceRouter: NewMsgServiceRouter(), }, - txDecoder: txDecoder, - TmConfig: tmConfig, + txDecoder: txDecoder, + TmConfig: tmConfig, + TracingEnabled: tracingEnabled, TracingInfo: &tracing.Info{ Tracer: &tr, }, @@ -844,11 +847,13 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, txBytes []byte) (gInf // resources are acceessed by the ante handlers and message handlers. defer acltypes.SendAllSignalsForTx(ctx.TxCompletionChannels()) acltypes.WaitForAllSignalsForTx(ctx.TxBlockingChannels()) - // check for existing parent tracer, and if applicable, use it - spanCtx, span := app.TracingInfo.StartWithContext("RunTx", ctx.TraceSpanContext()) - defer span.End() - ctx = ctx.WithTraceSpanContext(spanCtx) - span.SetAttributes(attribute.String("txHash", fmt.Sprintf("%X", sha256.Sum256(txBytes)))) + if app.TracingEnabled { + // check for existing parent tracer, and if applicable, use it + spanCtx, span := app.TracingInfo.StartWithContext("RunTx", ctx.TraceSpanContext()) + defer span.End() + ctx = ctx.WithTraceSpanContext(spanCtx) + span.SetAttributes(attribute.String("txHash", fmt.Sprintf("%X", sha256.Sum256(txBytes)))) + } // NOTE: GasWanted should be returned by the AnteHandler. GasUsed is // determined by the GasMeter. We need access to the context to get the gas @@ -881,9 +886,12 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, txBytes []byte) (gInf } if app.anteHandler != nil { - // trace AnteHandler - _, anteSpan := app.TracingInfo.StartWithContext("AnteHandler", ctx.TraceSpanContext()) - defer anteSpan.End() + var anteSpan trace.Span + if app.TracingEnabled { + // trace AnteHandler + _, anteSpan := app.TracingInfo.StartWithContext("AnteHandler", ctx.TraceSpanContext()) + defer anteSpan.End() + } var ( anteCtx sdk.Context msCache sdk.CacheMultiStore @@ -942,7 +950,9 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, txBytes []byte) (gInf priority = ctx.Priority() msCache.Write() anteEvents = events.ToABCIEvents() - anteSpan.End() + if app.TracingEnabled { + anteSpan.End() + } } // Create a new Context based off of the existing Context with a MultiStore branch @@ -987,9 +997,11 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (*s panic(err) } }() - spanCtx, span := app.TracingInfo.StartWithContext("RunMsgs", ctx.TraceSpanContext()) - defer span.End() - ctx = ctx.WithTraceSpanContext(spanCtx) + if app.TracingEnabled { + spanCtx, span := app.TracingInfo.StartWithContext("RunMsgs", ctx.TraceSpanContext()) + defer span.End() + ctx = ctx.WithTraceSpanContext(spanCtx) + } msgLogs := make(sdk.ABCIMessageLogs, 0, len(msgs)) events := sdk.EmptyEvents() txMsgData := &sdk.TxMsgData{ diff --git a/store/cachekv/mergeiterator.go b/store/cachekv/mergeiterator.go index f13c4025c..a32dfb346 100644 --- a/store/cachekv/mergeiterator.go +++ b/store/cachekv/mergeiterator.go @@ -16,11 +16,10 @@ import ( // // TODO: Optimize by memoizing. type cacheMergeIterator struct { - parent types.Iterator - cache types.Iterator - ascending bool - storeKey sdktypes.StoreKey - eventManager *sdktypes.EventManager + parent types.Iterator + cache types.Iterator + ascending bool + storeKey sdktypes.StoreKey } var _ types.Iterator = (*cacheMergeIterator)(nil) @@ -29,14 +28,12 @@ func NewCacheMergeIterator( parent, cache types.Iterator, ascending bool, storeKey sdktypes.StoreKey, - eventManager *sdktypes.EventManager, ) *cacheMergeIterator { iter := &cacheMergeIterator{ - parent: parent, - cache: cache, - ascending: ascending, - storeKey: storeKey, - eventManager: eventManager, + parent: parent, + cache: cache, + ascending: ascending, + storeKey: storeKey, } return iter @@ -138,14 +135,12 @@ func (iter *cacheMergeIterator) Value() []byte { // If parent is invalid, get the cache value. if !iter.parent.Valid() { value := iter.cache.Value() - iter.eventManager.EmitResourceAccessReadEvent("iterator", iter.storeKey, iter.cache.Key(), value) return value } // If cache is invalid, get the parent value. if !iter.cache.Valid() { value := iter.parent.Value() - iter.eventManager.EmitResourceAccessReadEvent("iterator", iter.storeKey, iter.parent.Key(), value) return value } @@ -156,11 +151,9 @@ func (iter *cacheMergeIterator) Value() []byte { switch cmp { case -1: // parent < cache value := iter.parent.Value() - iter.eventManager.EmitResourceAccessReadEvent("iterator", iter.storeKey, keyP, value) return value case 0, 1: // parent >= cache value := iter.cache.Value() - iter.eventManager.EmitResourceAccessReadEvent("iterator", iter.storeKey, keyC, value) return value default: panic("invalid comparison result") diff --git a/store/cachekv/mergeiterator_test.go b/store/cachekv/mergeiterator_test.go index 00f065151..b2648a865 100644 --- a/store/cachekv/mergeiterator_test.go +++ b/store/cachekv/mergeiterator_test.go @@ -6,7 +6,6 @@ import ( "github.com/cosmos/cosmos-sdk/store/cachekv" "github.com/cosmos/cosmos-sdk/store/dbadapter" "github.com/cosmos/cosmos-sdk/store/types" - sdktypes "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" dbm "github.com/tendermint/tm-db" ) @@ -14,7 +13,6 @@ import ( func TestMangerIterator(t *testing.T) { // initiate mock kvstore mem := dbadapter.Store{DB: dbm.NewMemDB()} - eventManager := sdktypes.NewEventManager() kvstore := cachekv.NewStore(mem, types.NewKVStoreKey("CacheKvTest"), types.DefaultCacheSizeLimit) value := randSlice(defaultValueSizeBz) startKey := randSlice(32) @@ -29,27 +27,13 @@ func TestMangerIterator(t *testing.T) { cache := kvstore.Iterator(nil, nil) for ; cache.Valid(); cache.Next() { } - iter := cachekv.NewCacheMergeIterator(parent, cache, true, types.NewKVStoreKey("CacheKvTest"), eventManager) + iter := cachekv.NewCacheMergeIterator(parent, cache, true, types.NewKVStoreKey("CacheKvTest")) - // get the next value - iter.Value() - - // assert the resource access is still emitted correctly when the cache store is unavailable - require.Equal(t, "access_type", string(eventManager.Events()[0].Attributes[0].Key)) - require.Equal(t, "read", string(eventManager.Events()[0].Attributes[0].Value)) - require.Equal(t, "store_key", string(eventManager.Events()[0].Attributes[1].Key)) - require.Equal(t, "CacheKvTest", string(eventManager.Events()[0].Attributes[1].Value)) - - // assert event emission when cache is available - cache = kvstore.Iterator(keys[1], keys[2]) - iter = cachekv.NewCacheMergeIterator(parent, cache, true, types.NewKVStoreKey("CacheKvTest"), eventManager) + // get the next value and it should not be nil + nextValue := iter.Value() + require.NotNil(t, nextValue) // get the next value - iter.Value() - - // assert the resource access is still emitted correctly when the cache store is available - require.Equal(t, "access_type", string(eventManager.Events()[0].Attributes[0].Key)) - require.Equal(t, "read", string(eventManager.Events()[0].Attributes[0].Value)) - require.Equal(t, "store_key", string(eventManager.Events()[0].Attributes[1].Key)) - require.Equal(t, "CacheKvTest", string(eventManager.Events()[0].Attributes[1].Value)) + nextValue = iter.Value() + require.NotNil(t, nextValue) } diff --git a/store/cachekv/store.go b/store/cachekv/store.go index f9dee6cbb..06d491d04 100644 --- a/store/cachekv/store.go +++ b/store/cachekv/store.go @@ -56,8 +56,6 @@ func (store *Store) GetEvents() []abci.Event { // Implements Store func (store *Store) ResetEvents() { - store.mtx.Lock() - defer store.mtx.Unlock() store.eventManager = sdktypes.NewEventManager() } @@ -77,7 +75,6 @@ func (store *Store) getFromCache(key []byte) []byte { // Get implements types.KVStore. func (store *Store) Get(key []byte) (value []byte) { types.AssertValidKey(key) - store.eventManager.EmitResourceAccessReadEvent("get", store.storeKey, key, value) return store.getFromCache(key) } @@ -86,13 +83,11 @@ func (store *Store) Set(key []byte, value []byte) { types.AssertValidKey(key) types.AssertValidValue(value) store.setCacheValue(key, value, false, true) - store.eventManager.EmitResourceAccessWriteEvent("set", store.storeKey, key, value) } // Has implements types.KVStore. func (store *Store) Has(key []byte) bool { value := store.Get(key) - store.eventManager.EmitResourceAccessReadEvent("has", store.storeKey, key, value) return value != nil } @@ -194,7 +189,7 @@ func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator { }() store.dirtyItems(start, end) cache = newMemIterator(start, end, store.sortedCache, store.deleted, ascending, store.eventManager, store.storeKey) - return NewCacheMergeIterator(parent, cache, ascending, store.storeKey, store.eventManager) + return NewCacheMergeIterator(parent, cache, ascending, store.storeKey) } func findStartIndex(strL []string, startQ string) int { From f9805fe318410843003d37f59557f52419a8a275 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Thu, 8 Feb 2024 20:53:45 -0600 Subject: [PATCH 33/36] Rebase occ main (#428) ## Describe your changes and provide context ## Testing performed to validate your change --------- Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com> --- storev2/rootmulti/store.go | 26 ++++++++++++++++++-------- storev2/state/store.go | 2 +- types/errors/errors.go | 3 +++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/storev2/rootmulti/store.go b/storev2/rootmulti/store.go index 2fb19f219..f7146f09b 100644 --- a/storev2/rootmulti/store.go +++ b/storev2/rootmulti/store.go @@ -91,7 +91,7 @@ func NewStore( // Commit implements interface Committer, called by ABCI Commit func (rs *Store) Commit(bumpVersion bool) types.CommitID { if !bumpVersion { - return rs.lastCommitInfo.CommitID() + panic("Commit should always bump version in root multistore") } if err := rs.flush(); err != nil { panic(err) @@ -141,7 +141,7 @@ func (rs *Store) StateStoreCommit() { // Flush all the pending changesets to commit store. func (rs *Store) flush() error { var changeSets []*proto.NamedChangeSet - currentVersion := rs.lastCommitInfo.Version + currentVersion := rs.lastCommitInfo.Version + 1 for key := range rs.ckvStores { // it'll unwrap the inter-block cache store := rs.GetCommitKVStore(key) @@ -206,8 +206,8 @@ func (rs *Store) GetStoreType() types.StoreType { } // Implements interface CacheWrapper -func (rs *Store) CacheWrap(storeKey types.StoreKey) types.CacheWrap { - return rs.CacheMultiStore().CacheWrap(storeKey) +func (rs *Store) CacheWrap(_ types.StoreKey) types.CacheWrap { + return rs.CacheMultiStore().(types.CacheWrap) } // Implements interface CacheWrapper @@ -356,6 +356,12 @@ func (rs *Store) LoadVersionAndUpgrade(version int64, upgrades *types.StoreUpgra if err := rs.scStore.Initialize(initialStores); err != nil { return err } + if version > 0 { + _, err := rs.scStore.LoadVersion(version, false) + if err != nil { + return nil + } + } var treeUpgrades []*proto.TreeNameUpgrade for _, key := range storesKeys { @@ -488,6 +494,7 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery { return sdkerrors.QueryResult(err) } var store types.Queryable + var commitInfo *types.CommitInfo if !req.Prove && version < rs.lastCommitInfo.Version && rs.ssStore != nil { // Serve abci query from ss store if no proofs needed @@ -500,9 +507,13 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery { return sdkerrors.QueryResult(err) } store = types.Queryable(commitment.NewStore(scStore.GetTreeByName(storeName), rs.logger)) + commitInfo = convertCommitInfo(scStore.LastCommitInfo()) + commitInfo = amendCommitInfo(commitInfo, rs.storesParams) } else { // Serve directly from latest sc store store = types.Queryable(commitment.NewStore(rs.scStore.GetTreeByName(storeName), rs.logger)) + commitInfo = convertCommitInfo(rs.scStore.LastCommitInfo()) + commitInfo = amendCommitInfo(commitInfo, rs.storesParams) } // trim the path and execute the query @@ -511,14 +522,13 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery { if !req.Prove || !rootmulti.RequireProof(subPath) { return res + } else if commitInfo != nil { + // Restore origin path and append proof op. + res.ProofOps.Ops = append(res.ProofOps.Ops, commitInfo.ProofOp(storeName)) } if res.ProofOps == nil || len(res.ProofOps.Ops) == 0 { return sdkerrors.QueryResult(errors.Wrap(sdkerrors.ErrInvalidRequest, "proof is unexpectedly empty; ensure height has not been pruned")) } - commitInfo := convertCommitInfo(rs.scStore.LastCommitInfo()) - commitInfo = amendCommitInfo(commitInfo, rs.storesParams) - // Restore origin path and append proof op. - res.ProofOps.Ops = append(res.ProofOps.Ops, commitInfo.ProofOp(storeName)) return res } diff --git a/storev2/state/store.go b/storev2/state/store.go index 72e4b0ce9..76c129bf5 100644 --- a/storev2/state/store.go +++ b/storev2/state/store.go @@ -97,6 +97,7 @@ func (st *Store) Query(req abci.RequestQuery) (res abci.ResponseQuery) { if req.Height > 0 && req.Height > st.version { return sdkerrors.QueryResult(errors.Wrap(sdkerrors.ErrInvalidHeight, "invalid height")) } + res.Height = st.version switch req.Path { case "/key": // get by key res.Key = req.Data // data holds the key bytes @@ -105,7 +106,6 @@ func (st *Store) Query(req abci.RequestQuery) (res abci.ResponseQuery) { pairs := kv.Pairs{ Pairs: make([]kv.Pair, 0), } - subspace := req.Data res.Key = subspace iterator := types.KVStorePrefixIterator(st, subspace) diff --git a/types/errors/errors.go b/types/errors/errors.go index 163a02199..b69a5f0df 100644 --- a/types/errors/errors.go +++ b/types/errors/errors.go @@ -147,6 +147,9 @@ var ( // ErrInvalidConcurrency defines an error occurred during concurrent execution ErrInvalidConcurrencyExecution = Register(RootCodespace, 41, "error during concurrent execution") + // ErrAlreadyExists defines an error for which the tx failed checkTx because the node has already seen it before + ErrAlreadyExists = Register(RootCodespace, 42, "error tx already exists") + // ErrPanic is only set when we recover from a panic, so we know to // redact potentially sensitive system info ErrPanic = Register(UndefinedCodespace, 111222, "panic") From 6b4342103a8c4da904e93c864bf82d98defa001a Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Fri, 9 Feb 2024 11:13:22 -0600 Subject: [PATCH 34/36] fix assignment to not cause panic (#429) ## Describe your changes and provide context ## Testing performed to validate your change --- baseapp/baseapp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index d818da3d6..0b78550ce 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -889,7 +889,7 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, txBytes []byte) (gInf var anteSpan trace.Span if app.TracingEnabled { // trace AnteHandler - _, anteSpan := app.TracingInfo.StartWithContext("AnteHandler", ctx.TraceSpanContext()) + _, anteSpan = app.TracingInfo.StartWithContext("AnteHandler", ctx.TraceSpanContext()) defer anteSpan.End() } var ( From 5a05027506dcc28cacf85e8e4239a7cc3f994c8e Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Tue, 13 Feb 2024 11:02:28 -0500 Subject: [PATCH 35/36] [OCC] add metrics for scheduler (#431) ## Describe your changes and provide context - **retries** represents number of tx attempts beyond the first attempt - **max_incarnation** is the highest incarnation seen in a given block ## Testing performed to validate your change - lower environment --- tasks/scheduler.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tasks/scheduler.go b/tasks/scheduler.go index 84b55d59d..61a180705 100644 --- a/tasks/scheduler.go +++ b/tasks/scheduler.go @@ -9,6 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/store/multiversion" store "github.com/cosmos/cosmos-sdk/store/types" + "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/occ" "github.com/cosmos/cosmos-sdk/utils/tracing" @@ -78,6 +79,7 @@ type scheduler struct { allTasks []*deliverTxTask executeCh chan func() validateCh chan func() + metrics *schedulerMetrics } // NewScheduler creates a new scheduler @@ -86,6 +88,7 @@ func NewScheduler(workers int, tracingInfo *tracing.Info, deliverTxFunc func(ctx workers: workers, deliverTx: deliverTxFunc, tracingInfo: tracingInfo, + metrics: &schedulerMetrics{}, } } @@ -152,11 +155,16 @@ func toTasks(reqs []*sdk.DeliverTxEntry) []*deliverTxTask { return res } -func collectResponses(tasks []*deliverTxTask) []types.ResponseDeliverTx { +func (s *scheduler) collectResponses(tasks []*deliverTxTask) []types.ResponseDeliverTx { res := make([]types.ResponseDeliverTx, 0, len(tasks)) + var maxIncarnation int for _, t := range tasks { + if t.Incarnation > maxIncarnation { + maxIncarnation = t.Incarnation + } res = append(res, *t.Response) } + s.metrics.maxIncarnation = maxIncarnation return res } @@ -202,6 +210,19 @@ func (s *scheduler) PrefillEstimates(reqs []*sdk.DeliverTxEntry) { } } +// schedulerMetrics contains metrics for the scheduler +type schedulerMetrics struct { + // maxIncarnation is the highest incarnation seen in this set + maxIncarnation int + // retries is the number of tx attempts beyond the first attempt + retries int +} + +func (s *scheduler) emitMetrics() { + telemetry.IncrCounter(float32(s.metrics.retries), "scheduler", "retries") + telemetry.SetGauge(float32(s.metrics.maxIncarnation), "scheduler", "max_incarnation") +} + func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]types.ResponseDeliverTx, error) { // initialize mutli-version stores if they haven't been initialized yet s.tryInitMultiVersionStore(ctx) @@ -211,6 +232,7 @@ func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]t s.allTasks = tasks s.executeCh = make(chan func(), len(tasks)) s.validateCh = make(chan func(), len(tasks)) + defer s.emitMetrics() // default to number of tasks if workers is negative or 0 by this point workers := s.workers @@ -245,11 +267,13 @@ func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]t if err != nil { return nil, err } + // these are retries which apply to metrics + s.metrics.retries += len(toExecute) } for _, mv := range s.multiVersionStores { mv.WriteLatestToStore() } - return collectResponses(tasks), nil + return s.collectResponses(tasks), nil } func (s *scheduler) shouldRerun(task *deliverTxTask) bool { From 00cc70de9e1a210908d27dd7c8dd699d910db924 Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Tue, 13 Feb 2024 15:44:10 -0500 Subject: [PATCH 36/36] change incarnation type --- tasks/scheduler.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tasks/scheduler.go b/tasks/scheduler.go index 61a180705..25ff557c4 100644 --- a/tasks/scheduler.go +++ b/tasks/scheduler.go @@ -164,7 +164,7 @@ func (s *scheduler) collectResponses(tasks []*deliverTxTask) []types.ResponseDel } res = append(res, *t.Response) } - s.metrics.maxIncarnation = maxIncarnation + s.metrics.incarnation = maxIncarnation return res } @@ -213,14 +213,14 @@ func (s *scheduler) PrefillEstimates(reqs []*sdk.DeliverTxEntry) { // schedulerMetrics contains metrics for the scheduler type schedulerMetrics struct { // maxIncarnation is the highest incarnation seen in this set - maxIncarnation int + incarnation int // retries is the number of tx attempts beyond the first attempt retries int } func (s *scheduler) emitMetrics() { telemetry.IncrCounter(float32(s.metrics.retries), "scheduler", "retries") - telemetry.SetGauge(float32(s.metrics.maxIncarnation), "scheduler", "max_incarnation") + telemetry.IncrCounter(float32(s.metrics.incarnation), "scheduler", "incarnation") } func (s *scheduler) ProcessAll(ctx sdk.Context, reqs []*sdk.DeliverTxEntry) ([]types.ResponseDeliverTx, error) {