Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7394b4bd67 | ||
|
|
d48923fbdf | ||
|
|
461410a8f8 | ||
|
|
11b8609a9b | ||
|
|
67ca504f81 | ||
|
|
0ca41dbd47 | ||
|
|
36cb769379 | ||
|
|
590e8080b8 | ||
|
|
fc4bdb6000 | ||
|
|
a12f3fb0dd | ||
|
|
debbf448db | ||
|
|
847b2d40fd | ||
|
|
9855eb4484 | ||
|
|
4bc402f1f0 | ||
|
|
ba5ca3d6be | ||
|
|
7294a74de0 | ||
|
|
ce9fcb5b0a | ||
|
|
d26bce0dea | ||
|
|
0cb7ae1100 | ||
|
|
5fdfb6c8e4 | ||
|
|
1ebe18296d | ||
|
|
6cbff3a930 | ||
|
|
474b81c8b6 |
No files matched your search
@@ -51,6 +51,10 @@ func (p pushSpaceRequestMatcher) String() string {
|
||||
|
||||
type mockPeer struct{}
|
||||
|
||||
func (m mockPeer) Addr() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m mockPeer) TryClose(objectTTL time.Duration) (res bool, err error) {
|
||||
return true, m.Close()
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"github.com/anytypeio/any-sync/util/cidutil"
|
||||
"github.com/anytypeio/any-sync/util/crypto"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrEmptyChange = errors.New("change payload should not be empty")
|
||||
@@ -20,6 +19,7 @@ type BuilderContent struct {
|
||||
PrivKey crypto.PrivKey
|
||||
ReadKey crypto.SymKey
|
||||
Content []byte
|
||||
Timestamp int64
|
||||
}
|
||||
|
||||
type InitialContent struct {
|
||||
@@ -166,7 +166,7 @@ func (c *changeBuilder) Build(payload BuilderContent) (ch *Change, rawIdChange *
|
||||
AclHeadId: payload.AclHeadId,
|
||||
SnapshotBaseId: payload.SnapshotBaseId,
|
||||
ReadKeyId: payload.ReadKeyId,
|
||||
Timestamp: time.Now().Unix(),
|
||||
Timestamp: payload.Timestamp,
|
||||
Identity: identity,
|
||||
IsSnapshot: payload.IsSnapshot,
|
||||
}
|
||||
|
||||
@@ -251,6 +251,10 @@ func (ot *objectTree) prepareBuilderContent(content SignableChangeContent) (cnt
|
||||
}
|
||||
readKey = ot.currentReadKey
|
||||
}
|
||||
timestamp := content.Timestamp
|
||||
if timestamp <= 0 {
|
||||
timestamp = time.Now().Unix()
|
||||
}
|
||||
cnt = BuilderContent{
|
||||
TreeHeadIds: ot.tree.Heads(),
|
||||
AclHeadId: ot.aclList.Head().Id,
|
||||
@@ -260,6 +264,7 @@ func (ot *objectTree) prepareBuilderContent(content SignableChangeContent) (cnt
|
||||
PrivKey: content.Key,
|
||||
ReadKey: readKey,
|
||||
Content: content.Data,
|
||||
Timestamp: timestamp,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type testTreeContext struct {
|
||||
@@ -19,13 +20,13 @@ type testTreeContext struct {
|
||||
objTree ObjectTree
|
||||
}
|
||||
|
||||
func prepareAclList(t *testing.T) list.AclList {
|
||||
func prepareAclList(t *testing.T) (list.AclList, *accountdata.AccountKeys) {
|
||||
randKeys, err := accountdata.NewRandom()
|
||||
require.NoError(t, err)
|
||||
aclList, err := list.NewTestDerivedAcl("spaceId", randKeys)
|
||||
require.NoError(t, err, "building acl list should be without error")
|
||||
|
||||
return aclList
|
||||
return aclList, randKeys
|
||||
}
|
||||
|
||||
func prepareHistoryTreeDeps(aclList list.AclList) (*MockChangeCreator, objectTreeDeps) {
|
||||
@@ -88,7 +89,57 @@ func prepareContext(
|
||||
}
|
||||
|
||||
func TestObjectTree(t *testing.T) {
|
||||
aclList := prepareAclList(t)
|
||||
aclList, keys := prepareAclList(t)
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("add content", func(t *testing.T) {
|
||||
root, err := CreateObjectTreeRoot(ObjectTreeCreatePayload{
|
||||
PrivKey: keys.SignKey,
|
||||
ChangeType: "changeType",
|
||||
ChangePayload: nil,
|
||||
SpaceId: "spaceId",
|
||||
IsEncrypted: true,
|
||||
}, aclList)
|
||||
require.NoError(t, err)
|
||||
store, _ := treestorage.NewInMemoryTreeStorage(root, []string{root.Id}, []*treechangeproto.RawTreeChangeWithId{root})
|
||||
oTree, err := BuildObjectTree(store, aclList)
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("0 timestamp is changed to current", func(t *testing.T) {
|
||||
start := time.Now()
|
||||
res, err := oTree.AddContent(ctx, SignableChangeContent{
|
||||
Data: []byte("some"),
|
||||
Key: keys.SignKey,
|
||||
IsSnapshot: false,
|
||||
IsEncrypted: true,
|
||||
Timestamp: 0,
|
||||
})
|
||||
end := time.Now()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, oTree.Heads(), 1)
|
||||
require.Equal(t, res.Added[0].Id, oTree.Heads()[0])
|
||||
ch, err := oTree.(*objectTree).changeBuilder.Unmarshall(res.Added[0], true)
|
||||
require.NoError(t, err)
|
||||
require.GreaterOrEqual(t, start.Unix(), ch.Timestamp)
|
||||
require.LessOrEqual(t, end.Unix(), ch.Timestamp)
|
||||
})
|
||||
t.Run("timestamp is set correctly", func(t *testing.T) {
|
||||
someTs := time.Now().Add(time.Hour).Unix()
|
||||
res, err := oTree.AddContent(ctx, SignableChangeContent{
|
||||
Data: []byte("some"),
|
||||
Key: keys.SignKey,
|
||||
IsSnapshot: false,
|
||||
IsEncrypted: true,
|
||||
Timestamp: someTs,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, oTree.Heads(), 1)
|
||||
require.Equal(t, res.Added[0].Id, oTree.Heads()[0])
|
||||
ch, err := oTree.(*objectTree).changeBuilder.Unmarshall(res.Added[0], true)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, ch.Timestamp, someTs)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("add simple", func(t *testing.T) {
|
||||
ctx := prepareTreeContext(t, aclList)
|
||||
|
||||
@@ -5,8 +5,6 @@ import (
|
||||
"github.com/anytypeio/any-sync/commonspace/object/tree/treechangeproto"
|
||||
"github.com/anytypeio/any-sync/commonspace/object/tree/treestorage"
|
||||
"github.com/anytypeio/any-sync/util/crypto"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ObjectTreeCreatePayload struct {
|
||||
@@ -15,6 +13,8 @@ type ObjectTreeCreatePayload struct {
|
||||
ChangePayload []byte
|
||||
SpaceId string
|
||||
IsEncrypted bool
|
||||
Seed []byte
|
||||
Timestamp int64
|
||||
}
|
||||
|
||||
type HistoryTreeParams struct {
|
||||
@@ -85,19 +85,6 @@ func nonVerifiableTreeDeps(
|
||||
}
|
||||
}
|
||||
|
||||
func CreateObjectTreeRoot(payload ObjectTreeCreatePayload, aclList list.AclList) (root *treechangeproto.RawTreeChangeWithId, err error) {
|
||||
bytes := make([]byte, 32)
|
||||
_, err = rand.Read(bytes)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return createObjectTreeRoot(payload, time.Now().Unix(), bytes, aclList)
|
||||
}
|
||||
|
||||
func DeriveObjectTreeRoot(payload ObjectTreeCreatePayload, aclList list.AclList) (root *treechangeproto.RawTreeChangeWithId, err error) {
|
||||
return createObjectTreeRoot(payload, 0, nil, aclList)
|
||||
}
|
||||
|
||||
func BuildEmptyDataObjectTree(treeStorage treestorage.TreeStorage, aclList list.AclList) (ObjectTree, error) {
|
||||
rootChange, err := treeStorage.Root()
|
||||
if err != nil {
|
||||
@@ -168,54 +155,7 @@ func BuildHistoryTree(params HistoryTreeParams) (HistoryTree, error) {
|
||||
return buildHistoryTree(deps, params)
|
||||
}
|
||||
|
||||
func CreateDerivedObjectTree(
|
||||
payload ObjectTreeCreatePayload,
|
||||
aclList list.AclList,
|
||||
createStorage treestorage.TreeStorageCreatorFunc) (objTree ObjectTree, err error) {
|
||||
return createObjectTree(payload, 0, nil, aclList, createStorage)
|
||||
}
|
||||
|
||||
func CreateObjectTree(
|
||||
payload ObjectTreeCreatePayload,
|
||||
aclList list.AclList,
|
||||
createStorage treestorage.TreeStorageCreatorFunc) (objTree ObjectTree, err error) {
|
||||
bytes := make([]byte, 32)
|
||||
_, err = rand.Read(bytes)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return createObjectTree(payload, time.Now().Unix(), bytes, aclList, createStorage)
|
||||
}
|
||||
|
||||
func createObjectTree(
|
||||
payload ObjectTreeCreatePayload,
|
||||
timestamp int64,
|
||||
seed []byte,
|
||||
aclList list.AclList,
|
||||
createStorage treestorage.TreeStorageCreatorFunc) (objTree ObjectTree, err error) {
|
||||
raw, err := createObjectTreeRoot(payload, timestamp, seed, aclList)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// create storage
|
||||
st, err := createStorage(treestorage.TreeStorageCreatePayload{
|
||||
RootRawChange: raw,
|
||||
Changes: []*treechangeproto.RawTreeChangeWithId{raw},
|
||||
Heads: []string{raw.Id},
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return BuildObjectTree(st, aclList)
|
||||
}
|
||||
|
||||
func createObjectTreeRoot(
|
||||
payload ObjectTreeCreatePayload,
|
||||
timestamp int64,
|
||||
seed []byte,
|
||||
aclList list.AclList) (root *treechangeproto.RawTreeChangeWithId, err error) {
|
||||
func CreateObjectTreeRoot(payload ObjectTreeCreatePayload, aclList list.AclList) (root *treechangeproto.RawTreeChangeWithId, err error) {
|
||||
aclList.RLock()
|
||||
aclHeadId := aclList.Head().Id
|
||||
aclList.RUnlock()
|
||||
@@ -229,8 +169,8 @@ func createObjectTreeRoot(
|
||||
SpaceId: payload.SpaceId,
|
||||
ChangeType: payload.ChangeType,
|
||||
ChangePayload: payload.ChangePayload,
|
||||
Timestamp: timestamp,
|
||||
Seed: seed,
|
||||
Timestamp: payload.Timestamp,
|
||||
Seed: payload.Seed,
|
||||
}
|
||||
|
||||
_, root, err = NewChangeBuilder(crypto.NewKeyStorage(), nil).BuildRoot(cnt)
|
||||
|
||||
@@ -4,9 +4,16 @@ import (
|
||||
"github.com/anytypeio/any-sync/util/crypto"
|
||||
)
|
||||
|
||||
// SignableChangeContent is a payload to be passed when we are creating change
|
||||
type SignableChangeContent struct {
|
||||
Data []byte
|
||||
Key crypto.PrivKey
|
||||
IsSnapshot bool
|
||||
// Data is a data provided by the client
|
||||
Data []byte
|
||||
// Key is the key which will be used to sign the change
|
||||
Key crypto.PrivKey
|
||||
// IsSnapshot tells if the change has snapshot of all previous data
|
||||
IsSnapshot bool
|
||||
// IsEncrypted tells if we encrypt the data with the relevant symmetric key
|
||||
IsEncrypted bool
|
||||
// Timestamp is a timestamp of change, if it is <= 0, then we use current timestamp
|
||||
Timestamp int64
|
||||
}
|
||||
+40
-25
@@ -20,6 +20,7 @@ import (
|
||||
"github.com/anytypeio/any-sync/commonspace/spacestorage"
|
||||
"github.com/anytypeio/any-sync/commonspace/spacesyncproto"
|
||||
"github.com/anytypeio/any-sync/commonspace/syncstatus"
|
||||
"github.com/anytypeio/any-sync/metric"
|
||||
"github.com/anytypeio/any-sync/net/peer"
|
||||
"github.com/anytypeio/any-sync/nodeconf"
|
||||
"github.com/anytypeio/any-sync/util/crypto"
|
||||
@@ -55,10 +56,22 @@ type SpaceCreatePayload struct {
|
||||
}
|
||||
|
||||
type HandleMessage struct {
|
||||
Id uint64
|
||||
Deadline time.Time
|
||||
SenderId string
|
||||
Message *spacesyncproto.ObjectSyncMessage
|
||||
Id uint64
|
||||
ReceiveTime time.Time
|
||||
StartHandlingTime time.Time
|
||||
Deadline time.Time
|
||||
SenderId string
|
||||
Message *spacesyncproto.ObjectSyncMessage
|
||||
PeerCtx context.Context
|
||||
}
|
||||
|
||||
func (m HandleMessage) LogFields(fields ...zap.Field) []zap.Field {
|
||||
return append(fields,
|
||||
metric.SpaceId(m.Message.SpaceId),
|
||||
metric.ObjectId(m.Message.ObjectId),
|
||||
metric.QueueDur(m.StartHandlingTime.Sub(m.ReceiveTime)),
|
||||
metric.TotalDur(time.Since(m.ReceiveTime)),
|
||||
)
|
||||
}
|
||||
|
||||
type SpaceDerivePayload struct {
|
||||
@@ -88,7 +101,6 @@ type Space interface {
|
||||
DebugAllHeads() []headsync.TreeHeads
|
||||
Description() (SpaceDescription, error)
|
||||
|
||||
DeriveTree(ctx context.Context, payload objecttree.ObjectTreeCreatePayload) (res treestorage.TreeStorageCreatePayload, err error)
|
||||
CreateTree(ctx context.Context, payload objecttree.ObjectTreeCreatePayload) (res treestorage.TreeStorageCreatePayload, err error)
|
||||
PutTree(ctx context.Context, payload treestorage.TreeStorageCreatePayload, listener updatelistener.UpdateListener) (t objecttree.ObjectTree, err error)
|
||||
BuildTree(ctx context.Context, id string, opts BuildTreeOpts) (t objecttree.ObjectTree, err error)
|
||||
@@ -125,6 +137,7 @@ type space struct {
|
||||
settingsObject settings.SettingsObject
|
||||
peerManager peermanager.PeerManager
|
||||
treeBuilder objecttree.BuildObjectTreeFunc
|
||||
metric metric.Metric
|
||||
|
||||
handleQueue multiqueue.MultiQueue[HandleMessage]
|
||||
|
||||
@@ -188,6 +201,8 @@ func (s *space) Init(ctx context.Context) (err error) {
|
||||
res, err := s.BuildTree(ctx, id, BuildTreeOpts{
|
||||
Listener: listener,
|
||||
WaitTreeRemoteSync: false,
|
||||
// space settings document should not have empty data
|
||||
treeBuilder: objecttree.BuildObjectTree,
|
||||
})
|
||||
log.Debug("building settings tree", zap.String("id", id), zap.String("spaceId", s.id))
|
||||
if err != nil {
|
||||
@@ -242,23 +257,6 @@ func (s *space) DebugAllHeads() []headsync.TreeHeads {
|
||||
return s.headSync.DebugAllHeads()
|
||||
}
|
||||
|
||||
func (s *space) DeriveTree(ctx context.Context, payload objecttree.ObjectTreeCreatePayload) (res treestorage.TreeStorageCreatePayload, err error) {
|
||||
if s.isClosed.Load() {
|
||||
err = ErrSpaceClosed
|
||||
return
|
||||
}
|
||||
root, err := objecttree.DeriveObjectTreeRoot(payload, s.aclList)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res = treestorage.TreeStorageCreatePayload{
|
||||
RootRawChange: root,
|
||||
Changes: []*treechangeproto.RawTreeChangeWithId{root},
|
||||
Heads: []string{root.Id},
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *space) CreateTree(ctx context.Context, payload objecttree.ObjectTreeCreatePayload) (res treestorage.TreeStorageCreatePayload, err error) {
|
||||
if s.isClosed.Load() {
|
||||
err = ErrSpaceClosed
|
||||
@@ -307,6 +305,7 @@ func (s *space) PutTree(ctx context.Context, payload treestorage.TreeStorageCrea
|
||||
type BuildTreeOpts struct {
|
||||
Listener updatelistener.UpdateListener
|
||||
WaitTreeRemoteSync bool
|
||||
treeBuilder objecttree.BuildObjectTreeFunc
|
||||
}
|
||||
|
||||
type HistoryTreeOpts struct {
|
||||
@@ -319,7 +318,10 @@ func (s *space) BuildTree(ctx context.Context, id string, opts BuildTreeOpts) (t
|
||||
err = ErrSpaceClosed
|
||||
return
|
||||
}
|
||||
|
||||
treeBuilder := opts.treeBuilder
|
||||
if treeBuilder == nil {
|
||||
treeBuilder = s.treeBuilder
|
||||
}
|
||||
deps := synctree.BuildDeps{
|
||||
SpaceId: s.id,
|
||||
SyncClient: s.objectSync.SyncClient(),
|
||||
@@ -332,7 +334,7 @@ func (s *space) BuildTree(ctx context.Context, id string, opts BuildTreeOpts) (t
|
||||
SyncStatus: s.syncStatus,
|
||||
WaitTreeRemoteSync: opts.WaitTreeRemoteSync,
|
||||
PeerGetter: s.peerManager,
|
||||
BuildObjectTree: s.treeBuilder,
|
||||
BuildObjectTree: treeBuilder,
|
||||
}
|
||||
if t, err = synctree.BuildSyncTreeOrGetRemote(ctx, id, deps); err != nil {
|
||||
return nil, err
|
||||
@@ -374,6 +376,7 @@ func (s *space) DeleteSpace(ctx context.Context, deleteChange *treechangeproto.R
|
||||
|
||||
func (s *space) HandleMessage(ctx context.Context, hm HandleMessage) (err error) {
|
||||
threadId := hm.Message.ObjectId
|
||||
hm.ReceiveTime = time.Now()
|
||||
if hm.Message.ReplyId != "" {
|
||||
threadId += hm.Message.ReplyId
|
||||
defer func() {
|
||||
@@ -390,12 +393,24 @@ func (s *space) HandleMessage(ctx context.Context, hm HandleMessage) (err error)
|
||||
}
|
||||
|
||||
func (s *space) handleMessage(msg HandleMessage) {
|
||||
var err error
|
||||
msg.StartHandlingTime = time.Now()
|
||||
ctx := peer.CtxWithPeerId(context.Background(), msg.SenderId)
|
||||
ctx = logger.CtxWithFields(ctx, zap.Uint64("msgId", msg.Id), zap.String("senderId", msg.SenderId))
|
||||
defer func() {
|
||||
if s.metric == nil {
|
||||
return
|
||||
}
|
||||
s.metric.RequestLog(msg.PeerCtx, "space.streamOp", msg.LogFields(
|
||||
zap.Error(err),
|
||||
)...)
|
||||
}()
|
||||
|
||||
if !msg.Deadline.IsZero() {
|
||||
now := time.Now()
|
||||
if now.After(msg.Deadline) {
|
||||
log.InfoCtx(ctx, "skip message: deadline exceed")
|
||||
err = context.DeadlineExceeded
|
||||
return
|
||||
}
|
||||
var cancel context.CancelFunc
|
||||
@@ -403,7 +418,7 @@ func (s *space) handleMessage(msg HandleMessage) {
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
if err := s.objectSync.HandleMessage(ctx, msg.SenderId, msg.Message); err != nil {
|
||||
if err = s.objectSync.HandleMessage(ctx, msg.SenderId, msg.Message); err != nil {
|
||||
if msg.Message.ObjectId != "" {
|
||||
// cleanup thread on error
|
||||
_ = s.handleQueue.CloseThread(msg.Message.ObjectId)
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/anytypeio/any-sync/commonspace/spacestorage"
|
||||
"github.com/anytypeio/any-sync/commonspace/spacesyncproto"
|
||||
"github.com/anytypeio/any-sync/commonspace/syncstatus"
|
||||
"github.com/anytypeio/any-sync/metric"
|
||||
"github.com/anytypeio/any-sync/net/peer"
|
||||
"github.com/anytypeio/any-sync/net/pool"
|
||||
"github.com/anytypeio/any-sync/net/rpc/rpcerr"
|
||||
@@ -52,6 +53,7 @@ type spaceService struct {
|
||||
credentialProvider credentialprovider.CredentialProvider
|
||||
treeManager treemanager.TreeManager
|
||||
pool pool.Pool
|
||||
metric metric.Metric
|
||||
}
|
||||
|
||||
func (s *spaceService) Init(a *app.App) (err error) {
|
||||
@@ -68,6 +70,7 @@ func (s *spaceService) Init(a *app.App) (err error) {
|
||||
s.credentialProvider = credentialprovider.NewNoOp()
|
||||
}
|
||||
s.pool = a.MustComponent(pool.CName).(pool.Pool)
|
||||
s.metric, _ = a.Component(metric.CName).(metric.Metric)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -181,6 +184,7 @@ func (s *spaceService) NewSpace(ctx context.Context, id string) (Space, error) {
|
||||
treeBuilder: builder,
|
||||
isClosed: spaceIsClosed,
|
||||
isDeleted: spaceIsDeleted,
|
||||
metric: s.metric,
|
||||
}
|
||||
return sp, nil
|
||||
}
|
||||
|
||||
+2
-2
@@ -6,12 +6,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type PrometheusDRPC struct {
|
||||
type prometheusDRPC struct {
|
||||
drpc.Handler
|
||||
SummaryVec *prometheus.SummaryVec
|
||||
}
|
||||
|
||||
func (ph *PrometheusDRPC) HandleRPC(stream drpc.Stream, rpc string) (err error) {
|
||||
func (ph *prometheusDRPC) HandleRPC(stream drpc.Stream, rpc string) (err error) {
|
||||
st := time.Now()
|
||||
defer func() {
|
||||
ph.SummaryVec.WithLabelValues(rpc).Observe(time.Since(st).Seconds())
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package metric
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/any-sync/net/peer"
|
||||
"go.uber.org/zap"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Method(val string) zap.Field {
|
||||
return zap.String("rpc", val)
|
||||
}
|
||||
|
||||
func QueueDur(val time.Duration) zap.Field {
|
||||
return zap.Float64("queueDur", val.Seconds())
|
||||
}
|
||||
|
||||
func TotalDur(val time.Duration) zap.Field {
|
||||
return zap.Float64("totalDur", val.Seconds())
|
||||
}
|
||||
|
||||
func SpaceId(val string) zap.Field {
|
||||
return zap.String("spaceId", val)
|
||||
}
|
||||
|
||||
func ObjectId(val string) zap.Field {
|
||||
return zap.String("objectId", val)
|
||||
}
|
||||
|
||||
func PeerId(val string) zap.Field {
|
||||
return zap.String("peerId", val)
|
||||
}
|
||||
|
||||
func Identity(val string) zap.Field {
|
||||
return zap.String("identity", val)
|
||||
}
|
||||
|
||||
func FileId(fileId string) zap.Field {
|
||||
return zap.String("fileId", fileId)
|
||||
}
|
||||
|
||||
func Cid(cid string) zap.Field {
|
||||
return zap.String("cid", cid)
|
||||
}
|
||||
|
||||
func Size(size int) zap.Field {
|
||||
return zap.Int("size", size)
|
||||
}
|
||||
|
||||
func (m *metric) RequestLog(ctx context.Context, rpc string, fields ...zap.Field) {
|
||||
if m == nil {
|
||||
return
|
||||
}
|
||||
peerId, _ := peer.CtxPeerId(ctx)
|
||||
ak, _ := peer.CtxPubKey(ctx)
|
||||
var acc string
|
||||
if ak != nil {
|
||||
acc = ak.Account()
|
||||
}
|
||||
m.rpcLog.Info("", append(fields, PeerId(peerId), Identity(acc), Method(rpc))...)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package metric
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/any-sync/app/logger"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLog(t *testing.T) {
|
||||
m := &metric{rpcLog: logger.NewNamed("rpcLog")}
|
||||
m.RequestLog(context.Background(), "rpc")
|
||||
}
|
||||
@@ -3,32 +3,41 @@ package metric
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/any-sync/app"
|
||||
"github.com/anytypeio/any-sync/app/logger"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"storj.io/drpc"
|
||||
"time"
|
||||
)
|
||||
|
||||
const CName = "common.metric"
|
||||
|
||||
var log = logger.NewNamed(CName)
|
||||
|
||||
func New() Metric {
|
||||
return new(metric)
|
||||
}
|
||||
|
||||
type Metric interface {
|
||||
Registry() *prometheus.Registry
|
||||
WrapDRPCHandler(h drpc.Handler) drpc.Handler
|
||||
RequestLog(ctx context.Context, rpc string, fields ...zap.Field)
|
||||
app.ComponentRunnable
|
||||
}
|
||||
|
||||
type metric struct {
|
||||
registry *prometheus.Registry
|
||||
rpcLog logger.CtxLogger
|
||||
config Config
|
||||
}
|
||||
|
||||
func (m *metric) Init(a *app.App) (err error) {
|
||||
m.registry = prometheus.NewRegistry()
|
||||
m.config = a.MustComponent("config").(configSource).GetMetric()
|
||||
m.rpcLog = logger.NewNamed("rpcLog")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -61,6 +70,31 @@ func (m *metric) Registry() *prometheus.Registry {
|
||||
return m.registry
|
||||
}
|
||||
|
||||
func (m *metric) WrapDRPCHandler(h drpc.Handler) drpc.Handler {
|
||||
if m == nil {
|
||||
return h
|
||||
}
|
||||
histVec := prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
||||
Namespace: "drpc",
|
||||
Subsystem: "server",
|
||||
Name: "duration_seconds",
|
||||
Objectives: map[float64]float64{
|
||||
0.5: 0.5,
|
||||
0.85: 0.01,
|
||||
0.95: 0.0005,
|
||||
0.99: 0.0001,
|
||||
},
|
||||
}, []string{"rpc"})
|
||||
if err := m.Registry().Register(histVec); err != nil {
|
||||
log.Warn("can't register prometheus drpc metric", zap.Error(err))
|
||||
return h
|
||||
}
|
||||
return &prometheusDRPC{
|
||||
Handler: h,
|
||||
SummaryVec: histVec,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *metric) Close(ctx context.Context) (err error) {
|
||||
return
|
||||
}
|
||||
+1
-11
@@ -32,12 +32,11 @@ var (
|
||||
var log = logger.NewNamed(CName)
|
||||
|
||||
func New() Dialer {
|
||||
return &dialer{}
|
||||
return &dialer{peerAddrs: map[string][]string{}}
|
||||
}
|
||||
|
||||
type Dialer interface {
|
||||
Dial(ctx context.Context, peerId string) (peer peer.Peer, err error)
|
||||
UpdateAddrs(addrs map[string][]string)
|
||||
SetPeerAddrs(peerId string, addrs []string)
|
||||
app.Component
|
||||
}
|
||||
@@ -62,18 +61,9 @@ func (d *dialer) Name() (name string) {
|
||||
return CName
|
||||
}
|
||||
|
||||
func (d *dialer) UpdateAddrs(addrs map[string][]string) {
|
||||
d.mu.Lock()
|
||||
d.peerAddrs = addrs
|
||||
d.mu.Unlock()
|
||||
}
|
||||
|
||||
func (d *dialer) SetPeerAddrs(peerId string, addrs []string) {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
if d.peerAddrs == nil {
|
||||
return
|
||||
}
|
||||
d.peerAddrs[peerId] = addrs
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ type contextKey uint
|
||||
const (
|
||||
contextKeyPeerId contextKey = iota
|
||||
contextKeyIdentity
|
||||
contextKeyPeerAddr
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -36,6 +37,19 @@ func CtxWithPeerId(ctx context.Context, peerId string) context.Context {
|
||||
return context.WithValue(ctx, contextKeyPeerId, peerId)
|
||||
}
|
||||
|
||||
// CtxPeerAddr returns peer address
|
||||
func CtxPeerAddr(ctx context.Context) string {
|
||||
if p, ok := ctx.Value(contextKeyPeerAddr).(string); ok {
|
||||
return p
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// CtxWithPeerAddr sets peer address to the context
|
||||
func CtxWithPeerAddr(ctx context.Context, addr string) context.Context {
|
||||
return context.WithValue(ctx, contextKeyPeerAddr, addr)
|
||||
}
|
||||
|
||||
// CtxIdentity returns identity from context
|
||||
func CtxIdentity(ctx context.Context) ([]byte, error) {
|
||||
if identity, ok := ctx.Value(contextKeyIdentity).([]byte); ok {
|
||||
|
||||
@@ -26,6 +26,7 @@ type Peer interface {
|
||||
Id() string
|
||||
LastUsage() time.Time
|
||||
UpdateLastUsage()
|
||||
Addr() string
|
||||
TryClose(objectTTL time.Duration) (res bool, err error)
|
||||
drpc.Conn
|
||||
}
|
||||
@@ -86,6 +87,13 @@ func (p *peer) TryClose(objectTTL time.Duration) (res bool, err error) {
|
||||
return true, p.Close()
|
||||
}
|
||||
|
||||
func (p *peer) Addr() string {
|
||||
if p.sc != nil {
|
||||
return p.sc.RemoteAddr().String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (p *peer) Close() (err error) {
|
||||
log.Debug("peer close", zap.String("peerId", p.id))
|
||||
return p.Conn.Close()
|
||||
|
||||
@@ -184,6 +184,10 @@ type testPeer struct {
|
||||
closed chan struct{}
|
||||
}
|
||||
|
||||
func (t *testPeer) Addr() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (t *testPeer) Id() string {
|
||||
return t.id
|
||||
}
|
||||
|
||||
@@ -103,6 +103,10 @@ type testPeer struct {
|
||||
drpc.Conn
|
||||
}
|
||||
|
||||
func (t testPeer) Addr() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (t testPeer) TryClose(objectTTL time.Duration) (res bool, err error) {
|
||||
return true, t.Close()
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/any-sync/net/peer"
|
||||
"github.com/anytypeio/any-sync/net/secureservice"
|
||||
"github.com/libp2p/go-libp2p/core/sec"
|
||||
"github.com/zeebo/errs"
|
||||
@@ -98,8 +99,11 @@ func (s *BaseDrpcServer) serveConn(conn net.Conn) {
|
||||
l.Info("handshake error", zap.Error(err))
|
||||
return
|
||||
}
|
||||
if sc, ok := conn.(sec.SecureConn); ok {
|
||||
ctx = peer.CtxWithPeerId(ctx, sc.RemotePeer().String())
|
||||
}
|
||||
}
|
||||
|
||||
ctx = peer.CtxWithPeerAddr(ctx, conn.RemoteAddr().String())
|
||||
l.Debug("connection opened")
|
||||
if err := s.drpcServer.ServeOne(ctx, conn); err != nil {
|
||||
if errs.Is(err, context.Canceled) || errs.Is(err, io.EOF) {
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
anyNet "github.com/anytypeio/any-sync/net"
|
||||
"github.com/anytypeio/any-sync/net/secureservice"
|
||||
"github.com/libp2p/go-libp2p/core/sec"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"net"
|
||||
"storj.io/drpc"
|
||||
"time"
|
||||
@@ -46,29 +45,12 @@ func (s *drpcServer) Name() (name string) {
|
||||
}
|
||||
|
||||
func (s *drpcServer) Run(ctx context.Context) (err error) {
|
||||
histVec := prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
||||
Namespace: "drpc",
|
||||
Subsystem: "server",
|
||||
Name: "duration_seconds",
|
||||
Objectives: map[float64]float64{
|
||||
0.5: 0.5,
|
||||
0.85: 0.01,
|
||||
0.95: 0.0005,
|
||||
0.99: 0.0001,
|
||||
},
|
||||
}, []string{"rpc"})
|
||||
if err = s.metric.Registry().Register(histVec); err != nil {
|
||||
return
|
||||
}
|
||||
params := Params{
|
||||
BufferSizeMb: s.config.Stream.MaxMsgSizeMb,
|
||||
TimeoutMillis: s.config.Stream.TimeoutMilliseconds,
|
||||
ListenAddrs: s.config.Server.ListenAddrs,
|
||||
Wrapper: func(handler drpc.Handler) drpc.Handler {
|
||||
return &metric.PrometheusDRPC{
|
||||
Handler: handler,
|
||||
SummaryVec: histVec,
|
||||
}
|
||||
return s.metric.WrapDRPCHandler(handler)
|
||||
},
|
||||
Handshake: func(conn net.Conn) (cCtx context.Context, sc sec.SecureConn, err error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
type stream struct {
|
||||
peerId string
|
||||
peerCtx context.Context
|
||||
stream drpc.Stream
|
||||
pool *streamPool
|
||||
streamId uint32
|
||||
@@ -36,7 +37,7 @@ func (sr *stream) readLoop() error {
|
||||
sr.l.Info("msg receive error", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
ctx := streamCtx(context.Background(), sr.streamId, sr.peerId)
|
||||
ctx := streamCtx(sr.peerCtx, sr.streamId, sr.peerId)
|
||||
ctx = logger.CtxWithFields(ctx, zap.String("peerId", sr.peerId))
|
||||
if err := sr.pool.handler.HandleMessage(ctx, sr.peerId, msg); err != nil {
|
||||
sr.l.Info("msg handle error", zap.Error(err))
|
||||
|
||||
@@ -27,9 +27,9 @@ type PeerGetter func(ctx context.Context) (peers []peer.Peer, err error)
|
||||
// StreamPool keeps and read streams
|
||||
type StreamPool interface {
|
||||
// AddStream adds new outgoing stream into the pool
|
||||
AddStream(peerId string, stream drpc.Stream, tags ...string)
|
||||
AddStream(stream drpc.Stream, tags ...string) (err error)
|
||||
// ReadStream adds new incoming stream and synchronously read it
|
||||
ReadStream(peerId string, stream drpc.Stream, tags ...string) (err error)
|
||||
ReadStream(stream drpc.Stream, tags ...string) (err error)
|
||||
// Send sends a message to given peers. A stream will be opened if it is not cached before. Works async.
|
||||
Send(ctx context.Context, msg drpc.Message, target PeerGetter) (err error)
|
||||
// SendById sends a message to given peerIds. Works only if stream exists
|
||||
@@ -63,16 +63,23 @@ type openingProcess struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *streamPool) ReadStream(peerId string, drpcStream drpc.Stream, tags ...string) error {
|
||||
st := s.addStream(peerId, drpcStream, tags...)
|
||||
func (s *streamPool) ReadStream(drpcStream drpc.Stream, tags ...string) error {
|
||||
st, err := s.addStream(drpcStream, tags...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return st.readLoop()
|
||||
}
|
||||
|
||||
func (s *streamPool) AddStream(peerId string, drpcStream drpc.Stream, tags ...string) {
|
||||
st := s.addStream(peerId, drpcStream, tags...)
|
||||
func (s *streamPool) AddStream(drpcStream drpc.Stream, tags ...string) error {
|
||||
st, err := s.addStream(drpcStream, tags...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
_ = st.readLoop()
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *streamPool) Streams(tags ...string) (streams []drpc.Stream) {
|
||||
@@ -86,13 +93,19 @@ func (s *streamPool) Streams(tags ...string) (streams []drpc.Stream) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *streamPool) addStream(peerId string, drpcStream drpc.Stream, tags ...string) *stream {
|
||||
func (s *streamPool) addStream(drpcStream drpc.Stream, tags ...string) (*stream, error) {
|
||||
ctx := drpcStream.Context()
|
||||
peerId, err := peer.CtxPeerId(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.lastStreamId++
|
||||
streamId := s.lastStreamId
|
||||
st := &stream{
|
||||
peerId: peerId,
|
||||
peerCtx: ctx,
|
||||
stream: drpcStream,
|
||||
pool: s,
|
||||
streamId: streamId,
|
||||
@@ -104,7 +117,7 @@ func (s *streamPool) addStream(peerId string, drpcStream drpc.Stream, tags ...st
|
||||
for _, tag := range tags {
|
||||
s.streamIdsByTag[tag] = append(s.streamIdsByTag[tag], streamId)
|
||||
}
|
||||
return st
|
||||
return st, nil
|
||||
}
|
||||
|
||||
func (s *streamPool) Send(ctx context.Context, msg drpc.Message, peerGetter PeerGetter) (err error) {
|
||||
@@ -241,7 +254,10 @@ func (s *streamPool) openStream(ctx context.Context, p peer.Peer) *openingProces
|
||||
op.err = err
|
||||
return
|
||||
}
|
||||
s.AddStream(p.Id(), st, tags...)
|
||||
if err = s.AddStream(st, tags...); err != nil {
|
||||
op.err = nil
|
||||
return
|
||||
}
|
||||
}()
|
||||
return op
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ var ctx = context.Background()
|
||||
func newClientStream(t *testing.T, fx *fixture, peerId string) (st testservice.DRPCTest_TestStreamClient, p peer.Peer) {
|
||||
p, err := fx.tp.Dial(ctx, peerId)
|
||||
require.NoError(t, err)
|
||||
ctx = peer.CtxWithPeerId(ctx, peerId)
|
||||
s, err := testservice.NewDRPCTestClient(p).TestStream(ctx)
|
||||
require.NoError(t, err)
|
||||
return s, p
|
||||
@@ -33,9 +34,9 @@ func TestStreamPool_AddStream(t *testing.T) {
|
||||
defer fx.Finish(t)
|
||||
|
||||
s1, _ := newClientStream(t, fx, "p1")
|
||||
fx.AddStream("p1", s1, "space1", "common")
|
||||
require.NoError(t, fx.AddStream(s1, "space1", "common"))
|
||||
s2, _ := newClientStream(t, fx, "p2")
|
||||
fx.AddStream("p2", s2, "space2", "common")
|
||||
require.NoError(t, fx.AddStream(s2, "space2", "common"))
|
||||
|
||||
require.NoError(t, fx.Broadcast(ctx, &testservice.StreamMessage{ReqData: "space1"}, "space1"))
|
||||
require.NoError(t, fx.Broadcast(ctx, &testservice.StreamMessage{ReqData: "space2"}, "space2"))
|
||||
@@ -64,7 +65,7 @@ func TestStreamPool_AddStream(t *testing.T) {
|
||||
|
||||
s1, p1 := newClientStream(t, fx, "p1")
|
||||
defer s1.Close()
|
||||
fx.AddStream("p1", s1, "space1", "common")
|
||||
require.NoError(t, fx.AddStream(s1, "space1", "common"))
|
||||
|
||||
require.NoError(t, fx.Send(ctx, &testservice.StreamMessage{ReqData: "test"}, func(ctx context.Context) (peers []peer.Peer, err error) {
|
||||
return []peer.Peer{p1}, nil
|
||||
@@ -159,7 +160,7 @@ func TestStreamPool_SendById(t *testing.T) {
|
||||
|
||||
s1, _ := newClientStream(t, fx, "p1")
|
||||
defer s1.Close()
|
||||
fx.AddStream("p1", s1, "space1", "common")
|
||||
require.NoError(t, fx.AddStream(s1, "space1", "common"))
|
||||
|
||||
require.NoError(t, fx.SendById(ctx, &testservice.StreamMessage{ReqData: "test"}, "p1"))
|
||||
var msg *testservice.StreamMessage
|
||||
@@ -177,11 +178,11 @@ func TestStreamPool_Tags(t *testing.T) {
|
||||
|
||||
s1, _ := newClientStream(t, fx, "p1")
|
||||
defer s1.Close()
|
||||
fx.AddStream("p1", s1, "t1")
|
||||
require.NoError(t, fx.AddStream(s1, "t1"))
|
||||
|
||||
s2, _ := newClientStream(t, fx, "p2")
|
||||
defer s1.Close()
|
||||
fx.AddStream("p2", s2, "t2")
|
||||
require.NoError(t, fx.AddStream(s2, "t2"))
|
||||
|
||||
err := fx.AddTagsCtx(streamCtx(ctx, 1, "p1"), "t3", "t3")
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in new issue
Block a user