Skip to content

Commit

Permalink
Cache full namespace details on each orchestrator
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Richardson <andrew.richardson@kaleido.io>
  • Loading branch information
awrichar committed Jul 12, 2022
1 parent cdbfd84 commit b838313
Show file tree
Hide file tree
Showing 30 changed files with 200 additions and 130 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
BEGIN;
ALTER TABLE messages DROP COLUMN namespace_local;
ALTER TABLE groups DROP COLUMN namespace_local;

ALTER TABLE namespaces ADD COLUMN id UUID;
ALTER TABLE namespaces ADD COLUMN message_id UUID;
ALTER TABLE namespaces ADD COLUMN ntype VARCHAR(64);
ALTER TABLE namespaces DROP COLUMN remote_name;
COMMIT;
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ ALTER TABLE messages ALTER COLUMN namespace_local SET NOT NULL;
ALTER TABLE groups ADD COLUMN namespace_local VARCHAR(64);
UPDATE groups SET namespace_local = namespace;
ALTER TABLE groups ALTER COLUMN namespace_local SET NOT NULL;

DROP INDEX namespaces_id;
ALTER TABLE namespaces DROP COLUMN id;
ALTER TABLE namespaces DROP COLUMN message_id;
ALTER TABLE namespaces DROP COLUMN ntype;
ALTER TABLE namespaces ADD COLUMN remote_name VARCHAR(64);
UPDATE namespaces SET remote_name = name;
ALTER TABLE namespaces ALTER COLUMN remote_name SET NOT NULL;
COMMIT;
6 changes: 0 additions & 6 deletions db/migrations/postgres/000096_drop_namespace_fields.up.sql

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
ALTER TABLE messages DROP COLUMN namespace_local;
ALTER TABLE groups DROP COLUMN namespace_local;

ALTER TABLE namespaces ADD COLUMN id UUID;
ALTER TABLE namespaces ADD COLUMN message_id UUID;
ALTER TABLE namespaces ADD COLUMN ntype VARCHAR(64);
ALTER TABLE namespaces DROP COLUMN remote_name;
12 changes: 12 additions & 0 deletions db/migrations/sqlite/000095_change_namespace_fields.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ALTER TABLE messages ADD COLUMN namespace_local VARCHAR(64);
UPDATE messages SET namespace_local = namespace;

ALTER TABLE groups ADD COLUMN namespace_local VARCHAR(64);
UPDATE groups SET namespace_local = namespace;

DROP INDEX namespaces_id;
ALTER TABLE namespaces DROP COLUMN id;
ALTER TABLE namespaces DROP COLUMN message_id;
ALTER TABLE namespaces DROP COLUMN ntype;
ALTER TABLE namespaces ADD COLUMN remote_name VARCHAR(64);
UPDATE namespaces SET remote_name = name;
4 changes: 0 additions & 4 deletions db/migrations/sqlite/000096_drop_namespace_fields.up.sql

This file was deleted.

4 changes: 3 additions & 1 deletion docs/reference/types/namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ nav_order: 20
```json
{
"name": "default",
"remoteName": "default",
"description": "Default predefined namespace",
"created": "2022-05-16T01:23:16Z"
}
Expand All @@ -34,7 +35,8 @@ nav_order: 20

| Field Name | Description | Type |
|------------|-------------|------|
| `name` | The namespace name | `string` |
| `name` | The local namespace name | `string` |
| `remoteName` | The namespace name within the multiparty network | `string` |
| `description` | A description of the namespace | `string` |
| `created` | The time the namespace was created | [`FFTime`](simpletypes#fftime) |

20 changes: 16 additions & 4 deletions docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8788,7 +8788,10 @@ paths:
description: A description of the namespace
type: string
name:
description: The namespace name
description: The local namespace name
type: string
remoteName:
description: The namespace name within the multiparty network
type: string
type: object
type: array
Expand Down Expand Up @@ -8830,7 +8833,10 @@ paths:
description: A description of the namespace
type: string
name:
description: The namespace name
description: The local namespace name
type: string
remoteName:
description: The namespace name within the multiparty network
type: string
type: object
description: Success
Expand Down Expand Up @@ -20518,7 +20524,10 @@ paths:
description: A description of the namespace
type: string
name:
description: The namespace name
description: The local namespace name
type: string
remoteName:
description: The namespace name within the multiparty network
type: string
type: object
node:
Expand Down Expand Up @@ -28069,7 +28078,10 @@ paths:
description: A description of the namespace
type: string
name:
description: The namespace name
description: The local namespace name
type: string
remoteName:
description: The namespace name within the multiparty network
type: string
type: object
node:
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/route_get_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var getNamespace = &ffapi.Route{
CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) {
ns := r.PP["ns"]
or := cr.mgr.Orchestrator(ns)
return or.GetNamespace(cr.ctx, ns)
return or.GetNamespace(cr.ctx), nil
},
},
}
2 changes: 1 addition & 1 deletion internal/apiserver/route_get_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestGetNamespace(t *testing.T) {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

o.On("GetNamespace", mock.Anything, "ns1").
o.On("GetNamespace", mock.Anything).
Return(&core.Namespace{}, nil)
r.ServeHTTP(res, req)

Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/route_spi_get_namespace_by_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var spiGetNamespaceByName = &ffapi.Route{
CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) {
ns := r.PP["ns"]
or := cr.mgr.Orchestrator(ns)
return or.GetNamespace(cr.ctx, ns)
return or.GetNamespace(cr.ctx), nil
},
},
}
2 changes: 1 addition & 1 deletion internal/apiserver/route_spi_get_namespace_by_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestSPIGetNamespaceByName(t *testing.T) {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

o.On("GetNamespace", mock.Anything, "ns1").
o.On("GetNamespace", mock.Anything).
Return(&core.Namespace{}, nil)
r.ServeHTTP(res, req)

Expand Down
3 changes: 2 additions & 1 deletion internal/coremsgs/en_struct_descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ var (
// Namespace field descriptions
NamespaceID = ffm("Namespace.id", "The UUID of the namespace. For locally established namespaces will be different on each node in the network. For broadcast namespaces, will be the same on every node")
NamespaceMessage = ffm("Namespace.message", "The UUID of broadcast message used to establish the namespace. Unset for local namespaces")
NamespaceName = ffm("Namespace.name", "The namespace name")
NamespaceName = ffm("Namespace.name", "The local namespace name")
NamespaceRemoteName = ffm("Namespace.remoteName", "The namespace name within the multiparty network")
NamespaceDescription = ffm("Namespace.description", "A description of the namespace")
NamespaceType = ffm("Namespace.type", "The type of the namespace")
NamespaceCreated = ffm("Namespace.created", "The time the namespace was created")
Expand Down
14 changes: 8 additions & 6 deletions internal/database/sqlcommon/namespace_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
var (
namespaceColumns = []string{
"name",
"remote_name",
"description",
"created",
"firefly_contracts",
Expand All @@ -51,7 +52,7 @@ func (s *SQLCommon) UpsertNamespace(ctx context.Context, namespace *core.Namespa
namespaceRows, _, err := s.queryTx(ctx, namespacesTable, tx,
sq.Select("seq").
From(namespacesTable).
Where(sq.Eq{"name": namespace.Name}),
Where(sq.Eq{"name": namespace.LocalName}),
)
if err != nil {
return err
Expand All @@ -64,12 +65,11 @@ func (s *SQLCommon) UpsertNamespace(ctx context.Context, namespace *core.Namespa
// Update the namespace
if _, err = s.updateTx(ctx, namespacesTable, tx,
sq.Update(namespacesTable).
// Note we do not update ID
Set("name", namespace.Name).
Set("remote_name", namespace.RemoteName).
Set("description", namespace.Description).
Set("created", namespace.Created).
Set("firefly_contracts", namespace.Contracts).
Where(sq.Eq{"name": namespace.Name}),
Where(sq.Eq{"name": namespace.LocalName}),
nil,
); err != nil {
return err
Expand All @@ -79,7 +79,8 @@ func (s *SQLCommon) UpsertNamespace(ctx context.Context, namespace *core.Namespa
sq.Insert(namespacesTable).
Columns(namespaceColumns...).
Values(
namespace.Name,
namespace.LocalName,
namespace.RemoteName,
namespace.Description,
namespace.Created,
namespace.Contracts,
Expand All @@ -96,7 +97,8 @@ func (s *SQLCommon) UpsertNamespace(ctx context.Context, namespace *core.Namespa
func (s *SQLCommon) namespaceResult(ctx context.Context, row *sql.Rows) (*core.Namespace, error) {
namespace := core.Namespace{}
err := row.Scan(
&namespace.Name,
&namespace.LocalName,
&namespace.RemoteName,
&namespace.Description,
&namespace.Created,
&namespace.Contracts,
Expand Down
19 changes: 10 additions & 9 deletions internal/database/sqlcommon/namespace_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ func TestNamespacesE2EWithDB(t *testing.T) {

// Create a new namespace entry
namespace := &core.Namespace{
Name: "namespace1",
Created: fftypes.Now(),
LocalName: "namespace1",
RemoteName: "default",
Created: fftypes.Now(),
Contracts: core.MultipartyContracts{
Active: core.MultipartyContract{
Index: 1,
Expand All @@ -51,7 +52,7 @@ func TestNamespacesE2EWithDB(t *testing.T) {
assert.NoError(t, err)

// Check we get the exact same namespace back
namespaceRead, err := s.GetNamespace(ctx, namespace.Name)
namespaceRead, err := s.GetNamespace(ctx, namespace.LocalName)
assert.NoError(t, err)
assert.NotNil(t, namespaceRead)
namespaceJson, _ := json.Marshal(&namespace)
Expand All @@ -61,15 +62,15 @@ func TestNamespacesE2EWithDB(t *testing.T) {
// Update the namespace (this is testing what's possible at the database layer,
// and does not account for the verification that happens at the higher level)
namespaceUpdated := &core.Namespace{
Name: "namespace1",
LocalName: "namespace1",
Description: "description1",
Created: fftypes.Now(),
}
err = s.UpsertNamespace(context.Background(), namespaceUpdated, true)
assert.NoError(t, err)

// Check we get the exact same data back - note the removal of one of the namespace elements
namespaceRead, err = s.GetNamespace(ctx, namespace.Name)
namespaceRead, err = s.GetNamespace(ctx, namespace.LocalName)
assert.NoError(t, err)
namespaceJson, _ = json.Marshal(&namespaceUpdated)
namespaceReadJson, _ = json.Marshal(&namespaceRead)
Expand All @@ -91,7 +92,7 @@ func TestUpsertNamespaceFailSelect(t *testing.T) {
mock.ExpectBegin()
mock.ExpectQuery("SELECT .*").WillReturnError(fmt.Errorf("pop"))
mock.ExpectRollback()
err := s.UpsertNamespace(context.Background(), &core.Namespace{Name: "name1"}, true)
err := s.UpsertNamespace(context.Background(), &core.Namespace{LocalName: "name1"}, true)
assert.Regexp(t, "FF10115", err)
assert.NoError(t, mock.ExpectationsWereMet())
}
Expand All @@ -102,7 +103,7 @@ func TestUpsertNamespaceFailInsert(t *testing.T) {
mock.ExpectQuery("SELECT .*").WillReturnRows(sqlmock.NewRows([]string{}))
mock.ExpectExec("INSERT .*").WillReturnError(fmt.Errorf("pop"))
mock.ExpectRollback()
err := s.UpsertNamespace(context.Background(), &core.Namespace{Name: "name1"}, true)
err := s.UpsertNamespace(context.Background(), &core.Namespace{LocalName: "name1"}, true)
assert.Regexp(t, "FF10116", err)
assert.NoError(t, mock.ExpectationsWereMet())
}
Expand All @@ -114,7 +115,7 @@ func TestUpsertNamespaceFailUpdate(t *testing.T) {
AddRow("name1"))
mock.ExpectExec("UPDATE .*").WillReturnError(fmt.Errorf("pop"))
mock.ExpectRollback()
err := s.UpsertNamespace(context.Background(), &core.Namespace{Name: "name1"}, true)
err := s.UpsertNamespace(context.Background(), &core.Namespace{LocalName: "name1"}, true)
assert.Regexp(t, "FF10117", err)
assert.NoError(t, mock.ExpectationsWereMet())
}
Expand All @@ -125,7 +126,7 @@ func TestUpsertNamespaceFailCommit(t *testing.T) {
mock.ExpectQuery("SELECT .*").WillReturnRows(sqlmock.NewRows([]string{"name"}))
mock.ExpectExec("INSERT .*").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit().WillReturnError(fmt.Errorf("pop"))
err := s.UpsertNamespace(context.Background(), &core.Namespace{Name: "name1"}, true)
err := s.UpsertNamespace(context.Background(), &core.Namespace{LocalName: "name1"}, true)
assert.Regexp(t, "FF10119", err)
assert.NoError(t, mock.ExpectationsWereMet())
}
Expand Down
14 changes: 7 additions & 7 deletions internal/namespace/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,11 @@ func (nm *namespaceManager) Init(ctx context.Context, cancelCtx context.CancelFu
func (nm *namespaceManager) initNamespace(name string, ns *namespace) error {
or := nm.utOrchestrator
if or == nil {
names := core.NamespaceRef{LocalName: name, RemoteName: ns.remoteName}
or = orchestrator.NewOrchestrator(names, ns.config, ns.plugins, nm.metrics)
or = orchestrator.NewOrchestrator(&core.Namespace{
LocalName: name,
RemoteName: ns.remoteName,
Description: ns.description,
}, ns.config, ns.plugins, nm.metrics)
}
if err := or.Init(nm.ctx, nm.cancelCtx); err != nil {
return err
Expand Down Expand Up @@ -887,11 +890,8 @@ func (nm *namespaceManager) Orchestrator(ns string) orchestrator.Orchestrator {

func (nm *namespaceManager) GetNamespaces(ctx context.Context) ([]*core.Namespace, error) {
results := make([]*core.Namespace, 0, len(nm.namespaces))
for name, ns := range nm.namespaces {
results = append(results, &core.Namespace{
Name: name,
Description: ns.description,
})
for _, ns := range nm.namespaces {
results = append(results, ns.orchestrator.GetNamespace(ctx))
}
return results, nil
}
Expand Down
7 changes: 6 additions & 1 deletion internal/namespace/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1252,10 +1252,15 @@ func TestGetNamespaces(t *testing.T) {
nm := newTestNamespaceManager(true)
defer nm.cleanup(t)

mo := &orchestratormocks.Orchestrator{}
nm.namespaces = map[string]*namespace{
"default": {},
"default": {
orchestrator: mo,
},
}

mo.On("GetNamespace", context.Background()).Return(&core.Namespace{})

results, err := nm.GetNamespaces(context.Background())
assert.Nil(t, err)
assert.Len(t, results, 1)
Expand Down
4 changes: 2 additions & 2 deletions internal/orchestrator/data_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"github.com/hyperledger/firefly/pkg/database"
)

func (or *orchestrator) GetNamespace(ctx context.Context, ns string) (*core.Namespace, error) {
return or.database().GetNamespace(ctx, ns)
func (or *orchestrator) GetNamespace(ctx context.Context) *core.Namespace {
return or.namespace
}

func (or *orchestrator) GetTransactionByID(ctx context.Context, id string) (*core.Transaction, error) {
Expand Down
Loading

0 comments on commit b838313

Please sign in to comment.